Browse Source

Use md5 hash of filename for temporary file (fixes #1786)

Jakob Borg 10 years ago
parent
commit
285dcc30cf

+ 4 - 1
internal/model/tempname.go

@@ -9,6 +9,7 @@
 package model
 
 import (
+	"crypto/md5"
 	"fmt"
 	"path/filepath"
 	"strings"
@@ -25,7 +26,9 @@ func (t tempNamer) IsTemporary(name string) bool {
 }
 
 func (t tempNamer) TempName(name string) string {
+	hash := md5.New()
+	hash.Write([]byte(name))
 	tdir := filepath.Dir(name)
-	tname := fmt.Sprintf("%s.%s", t.prefix, filepath.Base(name))
+	tname := fmt.Sprintf("%s.%x", t.prefix, hash.Sum(nil))
 	return filepath.Join(tdir, tname)
 }

+ 4 - 1
internal/model/tempname_windows.go

@@ -9,6 +9,7 @@
 package model
 
 import (
+	"crypto/md5"
 	"fmt"
 	"path/filepath"
 	"strings"
@@ -25,7 +26,9 @@ func (t tempNamer) IsTemporary(name string) bool {
 }
 
 func (t tempNamer) TempName(name string) string {
+	hash := md5.New()
+	hash.Write([]byte(name))
 	tdir := filepath.Dir(name)
-	tname := fmt.Sprintf("%s.%s.tmp", t.prefix, filepath.Base(name))
+	tname := fmt.Sprintf("%s.%x.tmp", t.prefix, hash.Sum(nil))
 	return filepath.Join(tdir, tname)
 }

+ 0 - 0
internal/model/testdata/.syncthing.file → internal/model/testdata/.syncthing.8c7dd922ad47494fc02c388e12c00eac


+ 0 - 0
internal/model/testdata/~syncthing~.file.tmp → internal/model/testdata/~syncthing~.8c7dd922ad47494fc02c388e12c00eac.tmp