-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
46 lines (38 loc) · 839 Bytes
/
path.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
package osfs
import (
"io/fs"
"os"
"syscall"
"github.com/jxsl13/osfs/fsi"
)
// used for mkdirAll
func iterateNotExistingDirTree(f fsi.Fs, path string, visitor func(subdir string, fi fs.FileInfo) error) (err error) {
dir, err := f.Stat(path)
if err == nil {
if dir.IsDir() {
return nil
}
return &fs.PathError{Op: "stat", Path: path, Err: syscall.ENOTDIR}
}
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 = iterateNotExistingDirTree(f, path[:j-1], visitor)
if err != nil {
return err
}
}
// Parent now exists; invoke visitor and use its result.
err = visitor(path, dir)
if err != nil {
return err
}
return nil
}