file_util.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 fi.Mode()&0400 != 0 {
  45. canRead = true
  46. }
  47. if fi.Mode()&0200 != 0 {
  48. canWrite = true
  49. }
  50. modTime = fi.ModTime()
  51. fileSize = fi.Size()
  52. return
  53. }