mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 10:58:57 +00:00
lib/fs: Optimize TempName + some cosmetic changes (#7911)
This commit is contained in:
parent
2816780b52
commit
ed98039aa5
@ -20,43 +20,40 @@ const (
|
|||||||
UnixTempPrefix = ".syncthing."
|
UnixTempPrefix = ".syncthing."
|
||||||
)
|
)
|
||||||
|
|
||||||
var TempPrefix string
|
func tempPrefix() string {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return WindowsTempPrefix
|
||||||
|
} else {
|
||||||
|
return UnixTempPrefix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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(UnixTempPrefix) - len(".tmp")
|
const maxFilenameLength = 160 - len(UnixTempPrefix) - len(".tmp")
|
||||||
|
|
||||||
func init() {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
TempPrefix = WindowsTempPrefix
|
|
||||||
} else {
|
|
||||||
TempPrefix = UnixTempPrefix
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
||||||
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
||||||
// are always recognized as temp files.
|
// are always recognized as temp files.
|
||||||
func IsTemporary(name string) bool {
|
func IsTemporary(name string) bool {
|
||||||
name = filepath.Base(name)
|
name = filepath.Base(name)
|
||||||
if strings.HasPrefix(name, WindowsTempPrefix) ||
|
return strings.HasPrefix(name, WindowsTempPrefix) ||
|
||||||
strings.HasPrefix(name, UnixTempPrefix) {
|
strings.HasPrefix(name, UnixTempPrefix)
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TempNameWithPrefix(name, prefix string) string {
|
func TempNameWithPrefix(name, prefix string) string {
|
||||||
tdir := filepath.Dir(name)
|
tdir := filepath.Dir(name)
|
||||||
tbase := filepath.Base(name)
|
tbase := filepath.Base(name)
|
||||||
|
var tname string
|
||||||
if len(tbase) > maxFilenameLength {
|
if len(tbase) > maxFilenameLength {
|
||||||
tbase = fmt.Sprintf("%x", sha256.Sum256([]byte(name)))
|
tname = fmt.Sprintf("%s%x.tmp", prefix, sha256.Sum256([]byte(tbase)))
|
||||||
|
} else {
|
||||||
|
tname = prefix + tbase + ".tmp"
|
||||||
}
|
}
|
||||||
tname := fmt.Sprintf("%s%s.tmp", prefix, tbase)
|
|
||||||
return filepath.Join(tdir, tname)
|
return filepath.Join(tdir, tname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TempName(name string) string {
|
func TempName(name string) string {
|
||||||
return TempNameWithPrefix(name, TempPrefix)
|
return TempNameWithPrefix(name, tempPrefix())
|
||||||
}
|
}
|
||||||
|
@ -7,20 +7,31 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLongTempFilename(t *testing.T) {
|
func TestLongTempFilename(t *testing.T) {
|
||||||
filename := ""
|
filename := strings.Repeat("l", 300)
|
||||||
for i := 0; i < 300; i++ {
|
|
||||||
filename += "l"
|
|
||||||
}
|
|
||||||
tFile := TempName(filename)
|
tFile := TempName(filename)
|
||||||
if len(tFile) < 10 || len(tFile) > 200 {
|
if len(tFile) < 10 || len(tFile) > 160 {
|
||||||
t.Fatal("Invalid long filename")
|
t.Fatal("Invalid long filename")
|
||||||
}
|
}
|
||||||
if !strings.HasSuffix(TempName("short"), "short.tmp") {
|
if !strings.HasSuffix(TempName("short"), "short.tmp") {
|
||||||
t.Fatal("Invalid short filename", TempName("short"))
|
t.Fatal("Invalid short filename", TempName("short"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func benchmarkTempName(b *testing.B, filename string) {
|
||||||
|
filename = filepath.Join("/Users/marieantoinette", filename)
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
TempName(filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTempNameShort(b *testing.B) { benchmarkTempName(b, "somefile.txt") }
|
||||||
|
func BenchmarkTempNameLong(b *testing.B) { benchmarkTempName(b, strings.Repeat("a", 270)) }
|
||||||
|
Loading…
Reference in New Issue
Block a user