-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirs.go
116 lines (85 loc) · 1.68 KB
/
dirs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package testfs
import (
"os"
"path"
)
func (t *TestFS) Mkdir(name string, perm os.FileMode) error {
// Ensure the dir mode is set
perm |= os.ModeDir
dir, err := t.find(path.Dir(name))
if err != nil {
return err
}
if !dir.IsDir() {
return os.ErrInvalid
}
// Create the directory
return dir.new(path.Base(name), Uid, Gid, perm)
}
func (t *TestFS) MkdirAll(name string, perm os.FileMode) error {
// Ensure the dir mode is set
perm |= os.ModeDir
terms, err := parsePath(name)
if err != nil {
return err
}
var dir *inode
if name[0] == '/' {
dir = &t.dirTree
} else {
dir = t.cwd
}
return dir.mkdirAll(terms, perm)
}
func (i *inode) mkdirAll(terms []string, perm os.FileMode) error {
if len(terms) == 0 {
return nil
}
err := i.new(terms[0], Uid, Gid, perm)
if len(terms) == 1 {
if os.IsExist(err) {
return nil
}
return err
}
dir := i.children[terms[0]]
switch {
case err == nil:
return dir.mkdirAll(terms[1:], perm)
case os.IsExist(err):
// If the child is not a directory, fail
if !i.children[terms[0]].IsDir() {
return err
}
// If it is a directory, just continue
return dir.mkdirAll(terms[1:], perm)
default:
// Some other error
return err
}
}
func (t *TestFS) Chdir(dir string) error {
d, err := t.find(dir)
if err != nil {
return err
}
if !d.IsDir() {
return os.ErrInvalid
}
if !checkPerm(d, 'r', 'x') {
return os.ErrPermission
}
t.cwd = d
switch {
case dir[0] == '/':
t.cwdPath = dir
case t.cwdPath[len(t.cwdPath)-1] == '/':
t.cwdPath = t.cwdPath + dir
default:
t.cwdPath = t.cwdPath + "/" + dir
}
return nil
}
func (t *TestFS) Getwd() (dir string, err error) {
return t.cwdPath, nil
}