-
Notifications
You must be signed in to change notification settings - Fork 53
/
fs.go
343 lines (312 loc) · 7.73 KB
/
fs.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package memfs
import (
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"github.com/spf13/afero"
)
type ModeAwareMemMapFs struct {
Fs afero.Fs
}
func NewModeAwareMemFs() *ModeAwareMemMapFs {
return &ModeAwareMemMapFs{
Fs: afero.NewMemMapFs(),
}
}
func (m *ModeAwareMemMapFs) canRead(f fs.FileInfo) error {
perm := f.Mode().Perm()
if perm&0444 != 0 {
return nil
}
return &os.PathError{Op: "read", Path: f.Name(), Err: syscall.EACCES}
}
func (m *ModeAwareMemMapFs) canWrite(f fs.FileInfo) error {
perm := f.Mode().Perm()
if perm&0222 != 0 {
return nil
}
return &os.PathError{Op: "write", Path: f.Name(), Err: syscall.EACCES}
}
func (m *ModeAwareMemMapFs) canExec(f fs.FileInfo) error {
perm := f.Mode().Perm()
if perm&0111 != 0 {
return nil
}
return &os.PathError{Op: "exec", Path: f.Name(), Err: syscall.EACCES}
}
// Create creates a file in the filesystem, returning the file and an
// error, if any happens.
func (m *ModeAwareMemMapFs) Create(name string) (afero.File, error) {
dir := filepath.Dir(name)
f, err := m.Stat(dir)
if err != nil {
return nil, err
}
if !f.IsDir() {
return nil, &os.PathError{Op: "create", Path: name, Err: syscall.ENOTDIR}
}
if err := m.canWrite(f); err != nil {
return nil, err
}
if err := m.canExec(f); err != nil {
return nil, err
}
return m.Fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
}
// Mkdir creates a directory in the filesystem, return an error if any
// happens.
func (m *ModeAwareMemMapFs) Mkdir(name string, perm os.FileMode) error {
dir := filepath.Dir(name)
f, err := m.Stat(dir)
if err != nil {
return err
}
if err := m.canWrite(f); err != nil {
return err
}
if err := m.canExec(f); err != nil {
return err
}
return m.Fs.Mkdir(name, perm)
}
// MkdirAll creates a directory path and all parents that does not exist
// yet.
func (m *ModeAwareMemMapFs) MkdirAll(path string, perm os.FileMode) error {
// implementation from os.MkdirAll
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
dir, err := m.Stat(path)
if err == nil {
if dir.IsDir() {
return nil
}
return &os.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 = m.MkdirAll(path[:j-1], perm)
if err != nil {
return err
}
}
// Parent now exists; invoke Mkdir and use its result.
err = m.Mkdir(path, perm)
if err != nil && !os.IsExist(err) {
// Handle arguments like "foo/." by
// double-checking that directory doesn't exist.
dir, err1 := m.Stat(path)
if err1 == nil && dir.IsDir() {
return nil
}
return err
}
return nil
}
// Open opens a file, returning it or an error, if any happens.
func (m *ModeAwareMemMapFs) Open(name string) (afero.File, error) {
f, err := m.Fs.Open(name)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
if err := m.canRead(fi); err != nil {
return nil, err
}
return f, nil
}
// OpenFile opens a file using the given flags and the given mode.
func (m *ModeAwareMemMapFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
var fileExists bool
if _, err := m.Fs.Stat(name); err == nil {
fileExists = true
}
if !fileExists && perm == 0 {
runtime.Breakpoint()
}
f, err := m.Fs.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
if perm&fs.FileMode(os.O_WRONLY|os.O_RDWR|os.O_APPEND|os.O_CREATE|os.O_TRUNC) != 0 {
if !fileExists {
// check write permission on parent directory
dir := filepath.Dir(name)
f, err := m.Stat(dir)
if err != nil {
return nil, err
}
if err := m.canWrite(f); err != nil {
return nil, err
}
}
if err := m.canWrite(fi); err != nil {
return nil, err
}
}
if perm&fs.FileMode(os.O_RDONLY) != 0 {
if err := m.canRead(fi); err != nil {
return nil, err
}
}
return f, nil
}
// Remove removes a file identified by name, returning an error, if any
// happens.
func (m *ModeAwareMemMapFs) Remove(name string) error {
dir := filepath.Dir(name)
f, err := m.Stat(dir)
if err != nil {
return err
}
if err := m.canWrite(f); err != nil {
return err
}
if err := m.canExec(f); err != nil {
return err
}
return m.Fs.Remove(name)
}
// RemoveAll removes a directory path and any children it contains. It
// does not fail if the path does not exist (return nil).
func (m *ModeAwareMemMapFs) RemoveAll(path string) error {
// not correct but good enough for testing
dir := filepath.Dir(path)
f, err := m.Stat(dir)
if err != nil {
return err
}
if err := m.canWrite(f); err != nil {
return err
}
if err := m.canExec(f); err != nil {
return err
}
return m.Fs.RemoveAll(path)
}
// Rename renames a file.
func (m *ModeAwareMemMapFs) Rename(oldname string, newname string) error {
newDir := filepath.Dir(newname)
newF, err := m.Stat(newDir)
if err != nil {
return err
}
oldF, err := m.Stat(oldname)
if err != nil {
return err
}
if err := m.canRead(oldF); err != nil {
return err
}
if err := m.canWrite(newF); err != nil {
return err
}
return m.Fs.Rename(oldname, newname)
}
func (m *ModeAwareMemMapFs) Stat(name string) (os.FileInfo, error) {
var dir fs.FileInfo
// ensure we have exec permission on all parents
if name != "/" {
var err error
dir, err = m.Stat(filepath.Dir(name))
if err != nil {
return nil, err
}
} else {
dir, _ = m.Fs.Stat(name)
}
info, err := m.Fs.Stat(name)
if err != nil {
return nil, err
}
if info.IsDir() {
if err := m.canExec(info); err != nil {
return nil, err
}
}
if err := m.canExec(dir); err != nil {
return nil, err
}
return info, nil
}
func (m *ModeAwareMemMapFs) Chmod(name string, mode os.FileMode) error {
_, err := m.Stat(filepath.Dir(name))
if err != nil {
return err
}
return m.Fs.Chmod(name, mode)
}
func (m *ModeAwareMemMapFs) Chown(name string, uid, gid int) error {
_, err := m.Stat(filepath.Dir(name))
if err != nil {
return err
}
return m.Fs.Chown(name, uid, gid)
}
func (m *ModeAwareMemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
_, err := m.Stat(filepath.Dir(name))
if err != nil {
return err
}
return m.Fs.Chtimes(name, atime, mtime)
}
func (m *ModeAwareMemMapFs) Name() string {
return "ModeAwareMemMapFs"
}
// A filesystem wrapper whose implementation of Rename will return EXDEV if the
// source and destination paths do not share the same first path component.
// This is to simulate cross-device rename errors.
type CrossDeviceTestFs struct {
afero.Fs
}
func (c *CrossDeviceTestFs) Rename(oldpath, newpath string) error {
oldpath = filepath.Clean(oldpath)
newpath = filepath.Clean(newpath)
if !filepath.IsAbs(oldpath) {
var err error
oldpath, err = filepath.Abs(oldpath)
if err != nil {
panic("test error: could not get absolute path for oldpath")
}
}
if !filepath.IsAbs(newpath) {
var err error
newpath, err = filepath.Abs(newpath)
if err != nil {
panic("test error: could not get absolute path for newpath")
}
}
// check if the first path component is the same, starting from the root
oldpathComponents := strings.Split(oldpath, string(filepath.Separator))
newpathComponents := strings.Split(newpath, string(filepath.Separator))
if len(oldpathComponents) < 2 || len(newpathComponents) < 2 {
panic("test error: Rename called with invalid paths")
}
if oldpathComponents[1] == newpathComponents[1] {
return c.Fs.Rename(oldpath, newpath)
}
return &os.LinkError{
Op: "rename",
Old: oldpath,
New: newpath,
Err: syscall.EXDEV,
}
}