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.

65 lines
1.1 KiB

  1. package util
  2. import (
  3. "errors"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. )
  8. func TestFolderWritable(folder string) (err error) {
  9. fileInfo, err := os.Stat(folder)
  10. if err != nil {
  11. return err
  12. }
  13. if !fileInfo.IsDir() {
  14. return errors.New("Not a valid folder!")
  15. }
  16. perm := fileInfo.Mode().Perm()
  17. glog.V(0).Infoln("Folder", folder, "Permission:", perm)
  18. if 0200&perm != 0 {
  19. return nil
  20. }
  21. return errors.New("Not writable!")
  22. }
  23. func GetFileSize(file *os.File) (size int64, err error) {
  24. var fi os.FileInfo
  25. if fi, err = file.Stat(); err == nil {
  26. size = fi.Size()
  27. }
  28. return
  29. }
  30. func FileExists(filename string) bool {
  31. _, err := os.Stat(filename)
  32. if os.IsNotExist(err) {
  33. return false
  34. }
  35. return true
  36. }
  37. func CheckFile(filename string) (exists, canRead, canWrite bool, modTime time.Time, fileSize int64) {
  38. exists = true
  39. fi, err := os.Stat(filename)
  40. if os.IsNotExist(err) {
  41. exists = false
  42. return
  43. }
  44. if err != nil {
  45. glog.Errorf("check %s: %v", filename, err)
  46. return
  47. }
  48. if fi.Mode()&0400 != 0 {
  49. canRead = true
  50. }
  51. if fi.Mode()&0200 != 0 {
  52. canWrite = true
  53. }
  54. modTime = fi.ModTime()
  55. fileSize = fi.Size()
  56. return
  57. }