forked from twpayne/go-vfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkdirall.go
56 lines (53 loc) · 1.68 KB
/
mkdirall.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
package vfs
import (
"os"
"path/filepath"
)
// A MkdirStater implements all the functionality needed by MkdirAll.
type MkdirStater interface {
Mkdir(name string, perm os.FileMode) error
Stat(name string) (os.FileInfo, error)
}
// MkdirAll is equivalent to os.MkdirAll but operates on fs.
func MkdirAll(fs MkdirStater, path string, perm os.FileMode) error {
err := fs.Mkdir(path, perm)
switch {
case err == nil:
// Mkdir was successful.
return nil
case os.IsExist(err):
// path already exists, but we don't know whether it's a directory or
// something else. We get this error if we try to create a subdirectory
// of a non-directory, for example if the parent directory of path is a
// file. There's a race condition here between the call to Mkdir and the
// call to Stat but we can't avoid it because there's not enough
// information in the returned error from Mkdir. We need to distinguish
// between "path already exists and is already a directory" and "path
// already exists and is not a directory". Between the call to Mkdir and
// the call to Stat path might have changed.
info, statErr := fs.Stat(path)
if statErr != nil {
return statErr
}
if !info.IsDir() {
return err
}
return nil
case os.IsNotExist(err):
// Parent directory does not exist. Create the parent directory
// recursively, then try again.
parentDir := filepath.Dir(path)
if parentDir == "/" || parentDir == "." {
// We cannot create the root directory or the current directory, so
// return the original error.
return err
}
if err := MkdirAll(fs, parentDir, perm); err != nil {
return err
}
return fs.Mkdir(path, perm)
default:
// Some other error.
return err
}
}