sys: synchronize mkdirall() with latest os.MkDirAll()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-14 16:56:31 +02:00
parent 063c5f9804
commit 972399538d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -67,11 +67,7 @@ func mkdirall(path string, perm *windows.SecurityAttributes) error {
if dir.IsDir() { if dir.IsDir() {
return nil return nil
} }
return &os.PathError{ return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
Op: "mkdir",
Path: path,
Err: syscall.ENOTDIR,
}
} }
// Slow path: make sure parent exists and then call Mkdir for path. // Slow path: make sure parent exists and then call Mkdir for path.
@ -86,14 +82,14 @@ func mkdirall(path string, perm *windows.SecurityAttributes) error {
} }
if j > 1 { if j > 1 {
// Create parent // Create parent.
err = mkdirall(path[0:j-1], perm) err = mkdirall(fixRootDirectory(path[:j-1]), perm)
if err != nil { if err != nil {
return err return err
} }
} }
// Parent now exists; invoke os.Mkdir or mkdirWithACL and use its result. // Parent now exists; invoke Mkdir and use its result.
err = mkdirWithACL(path, perm) err = mkdirWithACL(path, perm)
if err != nil { if err != nil {
// Handle arguments like "foo/." by // Handle arguments like "foo/." by
@ -131,6 +127,17 @@ func mkdirWithACL(name string, sa *windows.SecurityAttributes) error {
return nil return nil
} }
// fixRootDirectory fixes a reference to a drive's root directory to
// have the required trailing slash.
func fixRootDirectory(p string) string {
if len(p) == len(`\\?\c:`) {
if os.IsPathSeparator(p[0]) && os.IsPathSeparator(p[1]) && p[2] == '?' && os.IsPathSeparator(p[3]) && p[5] == ':' {
return p + `\`
}
}
return p
}
func makeSecurityAttributes(sddl string) (*windows.SecurityAttributes, error) { func makeSecurityAttributes(sddl string) (*windows.SecurityAttributes, error) {
var sa windows.SecurityAttributes var sa windows.SecurityAttributes
sa.Length = uint32(unsafe.Sizeof(sa)) sa.Length = uint32(unsafe.Sizeof(sa))