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.

141 lines
2.9 KiB

2 years ago
  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. }