file_util.go 827 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package util
  2. import (
  3. "bufio"
  4. "errors"
  5. "os"
  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 Readln(r *bufio.Reader) ([]byte, error) {
  24. var (
  25. isPrefix = true
  26. err error
  27. line, ln []byte
  28. )
  29. for isPrefix && err == nil {
  30. line, isPrefix, err = r.ReadLine()
  31. ln = append(ln, line...)
  32. }
  33. return ln, err
  34. }
  35. func GetFileSize(file *os.File) (size int64, err error) {
  36. var fi os.FileInfo
  37. if fi, err = file.Stat(); err == nil {
  38. size = fi.Size()
  39. }
  40. return
  41. }