osutil.go 4.0 KB

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