lib/model: Temp names from all platforms should be recognized as such
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3777 LGTM: AudriusButkevicius
This commit is contained in:
parent
e4db86836b
commit
48a229a0cd
@ -19,8 +19,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// We do this to make sure that the temp file required for the tests does
|
// We do this to make sure that the temp file required for the tests
|
||||||
// not get removed during the tests.
|
// does not get removed during the tests. Also set the prefix so it's
|
||||||
|
// found correctly regardless of platform.
|
||||||
|
defTempNamer.prefix = windowsTempPrefix
|
||||||
future := time.Now().Add(time.Hour)
|
future := time.Now().Add(time.Hour)
|
||||||
err := os.Chtimes(filepath.Join("testdata", defTempNamer.TempName("file")), future, future)
|
err := os.Chtimes(filepath.Join("testdata", defTempNamer.TempName("file")), future, future)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -15,26 +15,41 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type tempNamer struct {
|
type tempNamer struct {
|
||||||
prefix string
|
prefix string
|
||||||
|
recognize []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
windowsTempPrefix = "~syncthing~"
|
||||||
|
unixTempPrefix = ".syncthing."
|
||||||
|
)
|
||||||
|
|
||||||
var defTempNamer tempNamer
|
var defTempNamer tempNamer
|
||||||
|
|
||||||
// Real filesystems usually handle 255 bytes. encfs has varying and
|
// Real filesystems usually handle 255 bytes. encfs has varying and
|
||||||
// confusing file name limits. We take a safe way out and switch to hashing
|
// confusing file name limits. We take a safe way out and switch to hashing
|
||||||
// quite early.
|
// quite early.
|
||||||
const maxFilenameLength = 160 - len(".syncthing.") - len(".tmp")
|
const maxFilenameLength = 160 - len(unixTempPrefix) - len(".tmp")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
defTempNamer = tempNamer{"~syncthing~"}
|
defTempNamer = tempNamer{windowsTempPrefix, []string{unixTempPrefix, windowsTempPrefix}}
|
||||||
} else {
|
} else {
|
||||||
defTempNamer = tempNamer{".syncthing."}
|
defTempNamer = tempNamer{unixTempPrefix, []string{unixTempPrefix, windowsTempPrefix}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
||||||
|
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
||||||
|
// are always recognized as temp files.
|
||||||
func (t tempNamer) IsTemporary(name string) bool {
|
func (t tempNamer) IsTemporary(name string) bool {
|
||||||
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
name = filepath.Base(name)
|
||||||
|
for _, prefix := range t.recognize {
|
||||||
|
if strings.HasPrefix(name, prefix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t tempNamer) TempName(name string) string {
|
func (t tempNamer) TempName(name string) string {
|
||||||
|
|||||||
BIN
lib/model/testdata/.syncthing.file.tmp
vendored
BIN
lib/model/testdata/.syncthing.file.tmp
vendored
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user