kos/util/fs/dir.go

33 lines
606 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package fs
import (
"errors"
"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
}
fm := fd.Mode()
return fm.IsDir(), nil
}
// DirectoryOrCreate checking directory, is not exists will create
func DirectoryOrCreate(dirname string) error {
if fi, err := os.Stat(dirname); err != nil {
if errors.Is(err, os.ErrNotExist) {
return os.MkdirAll(dirname, 0755)
} else {
return err
}
} else {
if fi.IsDir() {
return nil
}
return errors.New("file not directory")
}
}