osutil.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "errors"
  10. "io"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "github.com/syncthing/syncthing/lib/fs"
  15. "github.com/syncthing/syncthing/lib/sync"
  16. )
  17. // Try to keep this entire operation atomic-like. We shouldn't be doing this
  18. // often enough that there is any contention on this lock.
  19. var renameLock = sync.NewMutex()
  20. // TryRename renames a file, leaving source file intact in case of failure.
  21. // Tries hard to succeed on various systems by temporarily tweaking directory
  22. // permissions and removing the destination file when necessary.
  23. func TryRename(filesystem fs.Filesystem, from, to string) error {
  24. renameLock.Lock()
  25. defer renameLock.Unlock()
  26. return withPreparedTarget(filesystem, from, to, func() error {
  27. return filesystem.Rename(from, to)
  28. })
  29. }
  30. // Rename moves a temporary file to its final place.
  31. // Will make sure to delete the from file if the operation fails, so use only
  32. // for situations like committing a temp file to its final location.
  33. // Tries hard to succeed on various systems by temporarily tweaking directory
  34. // permissions and removing the destination file when necessary.
  35. func Rename(filesystem fs.Filesystem, from, to string) error {
  36. // Don't leave a dangling temp file in case of rename error
  37. if !(runtime.GOOS == "windows" && strings.EqualFold(from, to)) {
  38. defer func() { _ = filesystem.Remove(from) }()
  39. }
  40. return TryRename(filesystem, from, to)
  41. }
  42. // Copy copies the file content from source to destination.
  43. // Tries hard to succeed on various systems by temporarily tweaking directory
  44. // permissions and removing the destination file when necessary.
  45. func Copy(filesystem fs.Filesystem, from, to string) (err error) {
  46. return withPreparedTarget(filesystem, from, to, func() error {
  47. return copyFileContents(filesystem, from, to)
  48. })
  49. }
  50. // InWritableDir calls fn(path), while making sure that the directory
  51. // containing `path` is writable for the duration of the call.
  52. func InWritableDir(fn func(string) error, fs fs.Filesystem, path string) error {
  53. dir := filepath.Dir(path)
  54. info, err := fs.Stat(dir)
  55. if err != nil {
  56. return err
  57. }
  58. if !info.IsDir() {
  59. return errors.New("Not a directory: " + path)
  60. }
  61. if info.Mode()&0200 == 0 {
  62. // A non-writeable directory (for this user; we assume that's the
  63. // relevant part). Temporarily change the mode so we can delete the
  64. // file or directory inside it.
  65. err = fs.Chmod(dir, 0755)
  66. if err == nil {
  67. defer func() {
  68. err = fs.Chmod(dir, info.Mode())
  69. if err != nil {
  70. // We managed to change the permission bits like a
  71. // millisecond ago, so it'd be bizarre if we couldn't
  72. // change it back.
  73. panic(err)
  74. }
  75. }()
  76. }
  77. }
  78. return fn(path)
  79. }
  80. // Tries hard to succeed on various systems by temporarily tweaking directory
  81. // permissions and removing the destination file when necessary.
  82. func withPreparedTarget(filesystem fs.Filesystem, from, to string, f func() error) error {
  83. // Make sure the destination directory is writeable
  84. toDir := filepath.Dir(to)
  85. if info, err := filesystem.Stat(toDir); err == nil && info.IsDir() && info.Mode()&0200 == 0 {
  86. _ = filesystem.Chmod(toDir, 0755)
  87. defer func() { _ = filesystem.Chmod(toDir, info.Mode()) }()
  88. }
  89. // On Windows, make sure the destination file is writeable (or we can't delete it)
  90. if runtime.GOOS == "windows" {
  91. _ = filesystem.Chmod(to, 0666)
  92. if !strings.EqualFold(from, to) {
  93. err := filesystem.Remove(to)
  94. if err != nil && !fs.IsNotExist(err) {
  95. return err
  96. }
  97. }
  98. }
  99. return f()
  100. }
  101. // copyFileContents copies the contents of the file named src to the file named
  102. // by dst. The file will be created if it does not already exist. If the
  103. // destination file exists, all its contents will be replaced by the contents
  104. // of the source file.
  105. func copyFileContents(filesystem fs.Filesystem, src, dst string) (err error) {
  106. in, err := filesystem.Open(src)
  107. if err != nil {
  108. return
  109. }
  110. defer in.Close()
  111. out, err := filesystem.Create(dst)
  112. if err != nil {
  113. return
  114. }
  115. defer func() {
  116. cerr := out.Close()
  117. if err == nil {
  118. err = cerr
  119. }
  120. }()
  121. _, err = io.Copy(out, in)
  122. return
  123. }
  124. func IsDeleted(ffs fs.Filesystem, name string) bool {
  125. if _, err := ffs.Lstat(name); fs.IsNotExist(err) {
  126. return true
  127. }
  128. switch TraversesSymlink(ffs, filepath.Dir(name)).(type) {
  129. case *NotADirectoryError, *TraversesSymlinkError:
  130. return true
  131. }
  132. return false
  133. }