file_util.go 2.9 KB

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