osutil.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package osutil implements utilities for native OS support.
  16. package osutil
  17. import (
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "sync"
  22. )
  23. // Try to keep this entire operation atomic-like. We shouldn't be doing this
  24. // often enough that there is any contention on this lock.
  25. var renameLock sync.Mutex
  26. // Rename renames a file, while trying hard to succeed on various systems by
  27. // temporarily tweaking directory permissions and removing the destination
  28. // file when necessary. Will make sure to delete the from file if the
  29. // operation fails, so use only for situations like committing a temp file to
  30. // it's final location.
  31. func Rename(from, to string) error {
  32. renameLock.Lock()
  33. defer renameLock.Unlock()
  34. // Make sure the destination directory is writeable
  35. toDir := filepath.Dir(to)
  36. if info, err := os.Stat(toDir); err == nil {
  37. os.Chmod(toDir, 0777)
  38. defer os.Chmod(toDir, info.Mode())
  39. }
  40. // On Windows, make sure the destination file is writeable (or we can't delete it)
  41. if runtime.GOOS == "windows" {
  42. os.Chmod(to, 0666)
  43. err := os.Remove(to)
  44. if err != nil && !os.IsNotExist(err) {
  45. return err
  46. }
  47. }
  48. // Don't leave a dangling temp file in case of rename error
  49. defer os.Remove(from)
  50. return os.Rename(from, to)
  51. }
  52. // InWritableDir calls fn(path), while making sure that the directory
  53. // containing `path` is writable for the duration of the call.
  54. func InWritableDir(fn func(string) error, path string) error {
  55. dir := filepath.Dir(path)
  56. if info, err := os.Stat(dir); err == nil && info.IsDir() && info.Mode()&04 == 0 {
  57. // A non-writeable directory (for this user; we assume that's the
  58. // relevant part). Temporarily change the mode so we can delete the
  59. // file or directory inside it.
  60. err = os.Chmod(dir, 0755)
  61. if err == nil {
  62. defer func() {
  63. err = os.Chmod(dir, info.Mode())
  64. if err != nil {
  65. // We managed to change the permission bits like a
  66. // millisecond ago, so it'd be bizarre if we couldn't
  67. // change it back.
  68. panic(err)
  69. }
  70. }()
  71. }
  72. }
  73. return fn(path)
  74. }