mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-22 22:58:25 +00:00
Retain meaningful names for temporary files
This commit is contained in:
parent
285dcc30cf
commit
a692348336
@ -1,17 +1,16 @@
|
|||||||
// Copyright (C) 2014 The Syncthing Authors.
|
// Copyright (C) 2015 The Syncthing Authors.
|
||||||
//
|
//
|
||||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,16 +18,28 @@ type tempNamer struct {
|
|||||||
prefix string
|
prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
var defTempNamer = tempNamer{".syncthing"}
|
var defTempNamer tempNamer
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
defTempNamer = tempNamer{"~syncthing~"}
|
||||||
|
} else {
|
||||||
|
defTempNamer = tempNamer{".syncthing."}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t tempNamer) IsTemporary(name string) bool {
|
func (t tempNamer) IsTemporary(name string) bool {
|
||||||
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t tempNamer) TempName(name string) string {
|
func (t tempNamer) TempName(name string) string {
|
||||||
hash := md5.New()
|
|
||||||
hash.Write([]byte(name))
|
|
||||||
tdir := filepath.Dir(name)
|
tdir := filepath.Dir(name)
|
||||||
tname := fmt.Sprintf("%s.%x", t.prefix, hash.Sum(nil))
|
tbase := filepath.Base(name)
|
||||||
|
if len(tbase) > 240 {
|
||||||
|
hash := md5.New()
|
||||||
|
hash.Write([]byte(name))
|
||||||
|
tbase = fmt.Sprintf("%x", hash.Sum(nil))
|
||||||
|
}
|
||||||
|
tname := fmt.Sprintf("%s%s.tmp", t.prefix, tbase)
|
||||||
return filepath.Join(tdir, tname)
|
return filepath.Join(tdir, tname)
|
||||||
}
|
}
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
// Copyright (C) 2014 The Syncthing Authors.
|
|
||||||
//
|
|
||||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
||||||
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
// +build windows
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/md5"
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type tempNamer struct {
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
var defTempNamer = tempNamer{"~syncthing~"}
|
|
||||||
|
|
||||||
func (t tempNamer) IsTemporary(name string) bool {
|
|
||||||
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t tempNamer) TempName(name string) string {
|
|
||||||
hash := md5.New()
|
|
||||||
hash.Write([]byte(name))
|
|
||||||
tdir := filepath.Dir(name)
|
|
||||||
tname := fmt.Sprintf("%s.%x.tmp", t.prefix, hash.Sum(nil))
|
|
||||||
return filepath.Join(tdir, tname)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user