dir.go 693 B

1234567891011121314151617181920212223242526272829303132333435
  1. package fs
  2. import (
  3. "os"
  4. )
  5. // IsDir Tells whether the filename is a directory
  6. func IsDir(filename string) (bool, error) {
  7. fd, err := os.Stat(filename)
  8. if err != nil {
  9. return false, err
  10. }
  11. return fd.Mode().IsDir(), nil
  12. }
  13. // IsFile Tells whether the filename is a file
  14. func IsFile(filename string) (bool, error) {
  15. fd, err := os.Stat(filename)
  16. if err != nil {
  17. return false, err
  18. }
  19. return !fd.Mode().IsDir(), nil
  20. }
  21. // Mkdir checking directory, is not exists will create
  22. func Mkdir(dirname string, perm os.FileMode) error {
  23. if fi, err := os.Stat(dirname); err != nil {
  24. return os.MkdirAll(dirname, perm)
  25. } else {
  26. if fi.IsDir() {
  27. return nil
  28. }
  29. return os.ErrPermission
  30. }
  31. }