kos/util/fs/dir.go

36 lines
693 B
Go

package fs
import (
"os"
)
// IsDir Tells whether the filename is a directory
func IsDir(filename string) (bool, error) {
fd, err := os.Stat(filename)
if err != nil {
return false, err
}
return fd.Mode().IsDir(), nil
}
// IsFile Tells whether the filename is a file
func IsFile(filename string) (bool, error) {
fd, err := os.Stat(filename)
if err != nil {
return false, err
}
return !fd.Mode().IsDir(), nil
}
// Mkdir checking directory, is not exists will create
func Mkdir(dirname string, perm os.FileMode) error {
if fi, err := os.Stat(dirname); err != nil {
return os.MkdirAll(dirname, perm)
} else {
if fi.IsDir() {
return nil
}
return os.ErrPermission
}
}