You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.3 KiB

2 years ago
  1. package util
  2. import (
  3. "errors"
  4. "os"
  5. "os/user"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. )
  11. func TestFolderWritable(folder string) (err error) {
  12. fileInfo, err := os.Stat(folder)
  13. if err != nil {
  14. return err
  15. }
  16. if !fileInfo.IsDir() {
  17. return errors.New("Not a valid folder!")
  18. }
  19. perm := fileInfo.Mode().Perm()
  20. glog.V(0).Infoln("Folder", folder, "Permission:", perm)
  21. if 0200&perm != 0 {
  22. return nil
  23. }
  24. return errors.New("Not writable!")
  25. }
  26. func GetFileSize(file *os.File) (size int64, err error) {
  27. var fi os.FileInfo
  28. if fi, err = file.Stat(); err == nil {
  29. size = fi.Size()
  30. }
  31. return
  32. }
  33. func FileExists(filename string) bool {
  34. _, err := os.Stat(filename)
  35. if os.IsNotExist(err) {
  36. return false
  37. }
  38. return true
  39. }
  40. func FolderExists(folder string) bool {
  41. fileInfo, err := os.Stat(folder)
  42. if err != nil {
  43. return false
  44. }
  45. return fileInfo.IsDir()
  46. }
  47. func CheckFile(filename string) (exists, canRead, canWrite bool, modTime time.Time, fileSize int64) {
  48. exists = true
  49. fi, err := os.Stat(filename)
  50. if os.IsNotExist(err) {
  51. exists = false
  52. return
  53. }
  54. if err != nil {
  55. glog.Errorf("check %s: %v", filename, err)
  56. return
  57. }
  58. if fi.Mode()&0400 != 0 {
  59. canRead = true
  60. }
  61. if fi.Mode()&0200 != 0 {
  62. canWrite = true
  63. }
  64. modTime = fi.ModTime()
  65. fileSize = fi.Size()
  66. return
  67. }
  68. func ResolvePath(path string) string {
  69. if !strings.Contains(path, "~") {
  70. return path
  71. }
  72. usr, _ := user.Current()
  73. dir := usr.HomeDir
  74. if path == "~" {
  75. // In case of "~", which won't be caught by the "else if"
  76. path = dir
  77. } else if strings.HasPrefix(path, "~/") {
  78. // Use strings.HasPrefix so we don't match paths like
  79. // "/something/~/something/"
  80. path = filepath.Join(dir, path[2:])
  81. }
  82. return path
  83. }
  84. func FileNameBase(filename string) string {
  85. lastDotIndex := strings.LastIndex(filename, ".")
  86. if lastDotIndex < 0 {
  87. return filename
  88. }
  89. return filename[:lastDotIndex]
  90. }
  91. // Copied from os.WriteFile(), adding file sync.
  92. // see https://github.com/golang/go/issues/20599
  93. func WriteFile(name string, data []byte, perm os.FileMode) error {
  94. f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
  95. if err != nil {
  96. return err
  97. }
  98. _, err = f.Write(data)
  99. if err1 := f.Sync(); err1 != nil && err == nil {
  100. err = err1
  101. }
  102. if err1 := f.Close(); err1 != nil && err == nil {
  103. err = err1
  104. }
  105. return err
  106. }