Moonshark/core/utils/dirs.go
2025-03-06 21:39:36 -06:00

33 lines
746 B
Go

package utils
import (
"os"
"path/filepath"
)
// EnsureDir checks if a directory exists and creates it if it doesn't.
// Returns any error encountered during directory creation.
func EnsureDir(path string) error {
// Clean the path to handle any malformed input
path = filepath.Clean(path)
// Check if the directory exists
info, err := os.Stat(path)
// If no error, check if it's a directory
if err == nil {
if info.IsDir() {
return nil // Directory already exists
}
return os.ErrExist // Path exists but is not a directory
}
// If the error is not that the path doesn't exist, return it
if !os.IsNotExist(err) {
return err
}
// Create the directory with default permissions (0755)
return os.MkdirAll(path, 0755)
}