-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkdir.go
90 lines (76 loc) · 2.36 KB
/
mkdir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package wrfs
import (
"os"
"syscall"
)
// MkdirFS is a file system that supports the Mkdir function.
type MkdirFS interface {
FS
// Mkdir creates a new directory with the specified name and permission bits.
Mkdir(name string, perm FileMode) error
}
// Mkdir creates a new directory with the specified name and permission bits.
func Mkdir(fsys FS, name string, perm FileMode) error {
if fsys, ok := fsys.(MkdirFS); ok {
return fsys.Mkdir(name, perm)
}
return &PathError{Op: "mkdir", Path: name, Err: ErrUnsupported}
}
type MkdirAllFS interface {
FS
// 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.
MkdirAll(path string, perm FileMode) error
}
// 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(fsys FS, path string, perm FileMode) error {
if fsys, ok := fsys.(MkdirAllFS); ok {
return fsys.MkdirAll(path, perm)
}
fsys, ok := fsys.(MkdirFS)
if !ok {
return &PathError{Op: "mkdir", Path: path, Err: ErrUnsupported}
}
// Based on os.MkdirAll
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
dir, err := Stat(fsys, 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.
i := len(path)
for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
i--
}
j := i
for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
j--
}
if j > 1 {
// Create parent.
err = MkdirAll(fsys, path[:j-1], perm)
if err != nil {
return err
}
}
// Parent now exists; invoke Mkdir and use its result.
err = Mkdir(fsys, path, perm)
if err != nil {
// Handle arguments like "foo/." by
// double-checking that directory doesn't exist.
dir, err1 := Stat(fsys, path)
if err1 == nil && dir.IsDir() {
return nil
}
return err
}
return nil
}