osutil.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "errors"
  19. "fmt"
  20. "os"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "sync"
  25. )
  26. var ErrNoHome = errors.New("No home directory found - set $HOME (or the platform equivalent).")
  27. // Try to keep this entire operation atomic-like. We shouldn't be doing this
  28. // often enough that there is any contention on this lock.
  29. var renameLock sync.Mutex
  30. // Rename renames a file, while trying hard to succeed on various systems by
  31. // temporarily tweaking directory permissions and removing the destination
  32. // file when necessary. Will make sure to delete the from file if the
  33. // operation fails, so use only for situations like committing a temp file to
  34. // it's final location.
  35. func Rename(from, to string) error {
  36. renameLock.Lock()
  37. defer renameLock.Unlock()
  38. // Make sure the destination directory is writeable
  39. toDir := filepath.Dir(to)
  40. if info, err := os.Stat(toDir); err == nil {
  41. os.Chmod(toDir, 0777)
  42. defer os.Chmod(toDir, info.Mode())
  43. }
  44. // On Windows, make sure the destination file is writeable (or we can't delete it)
  45. if runtime.GOOS == "windows" {
  46. os.Chmod(to, 0666)
  47. err := os.Remove(to)
  48. if err != nil && !os.IsNotExist(err) {
  49. return err
  50. }
  51. }
  52. // Don't leave a dangling temp file in case of rename error
  53. defer os.Remove(from)
  54. return os.Rename(from, to)
  55. }
  56. // InWritableDir calls fn(path), while making sure that the directory
  57. // containing `path` is writable for the duration of the call.
  58. func InWritableDir(fn func(string) error, path string) error {
  59. dir := filepath.Dir(path)
  60. if info, err := os.Stat(dir); err == nil && info.IsDir() && info.Mode()&0200 == 0 {
  61. // A non-writeable directory (for this user; we assume that's the
  62. // relevant part). Temporarily change the mode so we can delete the
  63. // file or directory inside it.
  64. err = os.Chmod(dir, 0755)
  65. if err == nil {
  66. defer func() {
  67. err = os.Chmod(dir, info.Mode())
  68. if err != nil {
  69. // We managed to change the permission bits like a
  70. // millisecond ago, so it'd be bizarre if we couldn't
  71. // change it back.
  72. panic(err)
  73. }
  74. }()
  75. }
  76. }
  77. return fn(path)
  78. }
  79. func ExpandTilde(path string) (string, error) {
  80. if path == "~" {
  81. return getHomeDir()
  82. }
  83. path = filepath.FromSlash(path)
  84. if !strings.HasPrefix(path, fmt.Sprintf("~%c", os.PathSeparator)) {
  85. return path, nil
  86. }
  87. home, err := getHomeDir()
  88. if err != nil {
  89. return "", err
  90. }
  91. return filepath.Join(home, path[2:]), nil
  92. }
  93. func getHomeDir() (string, error) {
  94. var home string
  95. switch runtime.GOOS {
  96. case "windows":
  97. home = filepath.Join(os.Getenv("HomeDrive"), os.Getenv("HomePath"))
  98. if home == "" {
  99. home = os.Getenv("UserProfile")
  100. }
  101. default:
  102. home = os.Getenv("HOME")
  103. }
  104. if home == "" {
  105. return "", ErrNoHome
  106. }
  107. return home, nil
  108. }