osutil.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2014 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 osutil implements utilities for native OS support.
  7. package osutil
  8. import (
  9. "path/filepath"
  10. "strings"
  11. "github.com/syncthing/syncthing/lib/build"
  12. "github.com/syncthing/syncthing/lib/fs"
  13. "github.com/syncthing/syncthing/lib/sync"
  14. )
  15. // Try to keep this entire operation atomic-like. We shouldn't be doing this
  16. // often enough that there is any contention on this lock.
  17. var renameLock = sync.NewMutex()
  18. // RenameOrCopy renames a file, leaving source file intact in case of failure.
  19. // Tries hard to succeed on various systems by temporarily tweaking directory
  20. // permissions and removing the destination file when necessary.
  21. func RenameOrCopy(method fs.CopyRangeMethod, src, dst fs.Filesystem, from, to string) error {
  22. renameLock.Lock()
  23. defer renameLock.Unlock()
  24. return withPreparedTarget(dst, from, to, func() error {
  25. // Optimisation 1
  26. if src.Type() == dst.Type() && src.URI() == dst.URI() {
  27. return src.Rename(from, to)
  28. }
  29. // "Optimisation" 2
  30. // Try to find a common prefix between the two filesystems, use that as the base for the new one
  31. // and try a rename.
  32. if src.Type() == dst.Type() {
  33. commonPrefix := fs.CommonPrefix(src.URI(), dst.URI())
  34. if len(commonPrefix) > 0 {
  35. commonFs := fs.NewFilesystem(src.Type(), commonPrefix)
  36. err := commonFs.Rename(
  37. filepath.Join(strings.TrimPrefix(src.URI(), commonPrefix), from),
  38. filepath.Join(strings.TrimPrefix(dst.URI(), commonPrefix), to),
  39. )
  40. if err == nil {
  41. return nil
  42. }
  43. }
  44. }
  45. // Everything is sad, do a copy and delete.
  46. if _, err := dst.Stat(to); !fs.IsNotExist(err) {
  47. err := dst.Remove(to)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. err := copyFileContents(method, src, dst, from, to)
  53. if err != nil {
  54. _ = dst.Remove(to)
  55. return err
  56. }
  57. return withPreparedTarget(src, from, from, func() error {
  58. return src.Remove(from)
  59. })
  60. })
  61. }
  62. // Copy copies the file content from source to destination.
  63. // Tries hard to succeed on various systems by temporarily tweaking directory
  64. // permissions and removing the destination file when necessary.
  65. func Copy(method fs.CopyRangeMethod, src, dst fs.Filesystem, from, to string) (err error) {
  66. return withPreparedTarget(dst, from, to, func() error {
  67. return copyFileContents(method, src, dst, from, to)
  68. })
  69. }
  70. // Tries hard to succeed on various systems by temporarily tweaking directory
  71. // permissions and removing the destination file when necessary.
  72. func withPreparedTarget(filesystem fs.Filesystem, from, to string, f func() error) error {
  73. // Make sure the destination directory is writeable
  74. toDir := filepath.Dir(to)
  75. if info, err := filesystem.Stat(toDir); err == nil && info.IsDir() && info.Mode()&0200 == 0 {
  76. filesystem.Chmod(toDir, 0755)
  77. defer filesystem.Chmod(toDir, info.Mode())
  78. }
  79. // On Windows, make sure the destination file is writeable (or we can't delete it)
  80. if build.IsWindows {
  81. filesystem.Chmod(to, 0666)
  82. if !strings.EqualFold(from, to) {
  83. err := filesystem.Remove(to)
  84. if err != nil && !fs.IsNotExist(err) {
  85. return err
  86. }
  87. }
  88. }
  89. return f()
  90. }
  91. // copyFileContents copies the contents of the file named src to the file named
  92. // by dst. The file will be created if it does not already exist. If the
  93. // destination file exists, all its contents will be replaced by the contents
  94. // of the source file.
  95. func copyFileContents(method fs.CopyRangeMethod, srcFs, dstFs fs.Filesystem, src, dst string) (err error) {
  96. in, err := srcFs.Open(src)
  97. if err != nil {
  98. return
  99. }
  100. defer in.Close()
  101. out, err := dstFs.Create(dst)
  102. if err != nil {
  103. return
  104. }
  105. defer func() {
  106. cerr := out.Close()
  107. if err == nil {
  108. err = cerr
  109. }
  110. }()
  111. inFi, err := in.Stat()
  112. if err != nil {
  113. return
  114. }
  115. err = fs.CopyRange(method, in, out, 0, 0, inFi.Size())
  116. return
  117. }
  118. func IsDeleted(ffs fs.Filesystem, name string) bool {
  119. if _, err := ffs.Lstat(name); err != nil {
  120. if fs.IsNotExist(err) || fs.IsErrCaseConflict(err) {
  121. return true
  122. }
  123. }
  124. switch TraversesSymlink(ffs, filepath.Dir(name)).(type) {
  125. case *NotADirectoryError, *TraversesSymlinkError:
  126. return true
  127. }
  128. return false
  129. }