64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package os
|
|
|
|
import (
|
|
"syscall"
|
|
|
|
"github.com/goplus/llgo/internal/lib/internal/filepathlite"
|
|
)
|
|
|
|
// MkdirAll creates a directory named path,
|
|
// along with any necessary parents, and returns nil,
|
|
// or else returns an error.
|
|
// The permission bits perm (before umask) are used for all
|
|
// directories that MkdirAll creates.
|
|
// If path is already a directory, MkdirAll does nothing
|
|
// and returns nil.
|
|
func MkdirAll(path string, perm FileMode) error {
|
|
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
|
dir, err := Stat(path)
|
|
if err == nil {
|
|
if dir.IsDir() {
|
|
return nil
|
|
}
|
|
return &PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
|
|
}
|
|
|
|
// Slow path: make sure parent exists and then call Mkdir for path.
|
|
|
|
// Extract the parent folder from path by first removing any trailing
|
|
// path separator and then scanning backward until finding a path
|
|
// separator or reaching the beginning of the string.
|
|
i := len(path) - 1
|
|
for i >= 0 && IsPathSeparator(path[i]) {
|
|
i--
|
|
}
|
|
for i >= 0 && !IsPathSeparator(path[i]) {
|
|
i--
|
|
}
|
|
if i < 0 {
|
|
i = 0
|
|
}
|
|
|
|
// If there is a parent directory, and it is not the volume name,
|
|
// recurse to ensure parent directory exists.
|
|
if parent := path[:i]; len(parent) > len(filepathlite.VolumeName(path)) {
|
|
err = MkdirAll(parent, perm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Parent now exists; invoke Mkdir and use its result.
|
|
err = Mkdir(path, perm)
|
|
if err != nil {
|
|
// Handle arguments like "foo/." by
|
|
// double-checking that directory doesn't exist.
|
|
dir, err1 := Lstat(path)
|
|
if err1 == nil && dir.IsDir() {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|