kos/util/fs/dir.go

36 lines
693 B
Go
Raw Normal View History

2023-04-23 17:57:36 +08:00
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
}
2024-04-29 10:52:19 +08:00
return fd.Mode().IsDir(), nil
2023-04-23 17:57:36 +08:00
}
2024-04-29 10:52:19 +08:00
// 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 {
2023-04-23 17:57:36 +08:00
if fi, err := os.Stat(dirname); err != nil {
2024-04-29 10:52:19 +08:00
return os.MkdirAll(dirname, perm)
2023-04-23 17:57:36 +08:00
} else {
if fi.IsDir() {
return nil
}
2024-04-29 10:52:19 +08:00
return os.ErrPermission
2023-04-23 17:57:36 +08:00
}
}