tempname.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "crypto/sha256"
  9. "fmt"
  10. "path/filepath"
  11. "strings"
  12. "github.com/syncthing/syncthing/lib/build"
  13. )
  14. const (
  15. WindowsTempPrefix = "~syncthing~"
  16. UnixTempPrefix = ".syncthing."
  17. )
  18. func tempPrefix() string {
  19. if build.IsWindows {
  20. return WindowsTempPrefix
  21. } else {
  22. return UnixTempPrefix
  23. }
  24. }
  25. // Real filesystems usually handle 255 bytes. encfs has varying and
  26. // confusing file name limits. We take a safe way out and switch to hashing
  27. // quite early.
  28. const maxFilenameLength = 160 - len(UnixTempPrefix) - len(".tmp")
  29. // IsTemporary is true if the file name has the temporary prefix. Regardless
  30. // of the normally used prefix, the standard Windows and Unix temp prefixes
  31. // are always recognized as temp files.
  32. func IsTemporary(name string) bool {
  33. name = filepath.Base(name)
  34. return strings.HasPrefix(name, WindowsTempPrefix) ||
  35. strings.HasPrefix(name, UnixTempPrefix)
  36. }
  37. func TempNameWithPrefix(name, prefix string) string {
  38. tdir := filepath.Dir(name)
  39. tbase := filepath.Base(name)
  40. var tname string
  41. if len(tbase) > maxFilenameLength {
  42. tname = fmt.Sprintf("%s%x.tmp", prefix, sha256.Sum256([]byte(tbase)))
  43. } else {
  44. tname = prefix + tbase + ".tmp"
  45. }
  46. return filepath.Join(tdir, tname)
  47. }
  48. func TempName(name string) string {
  49. return TempNameWithPrefix(name, tempPrefix())
  50. }