-
Notifications
You must be signed in to change notification settings - Fork 1
/
sub.go
47 lines (40 loc) · 1.14 KB
/
sub.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
package virt
import (
"io/fs"
"path"
)
// Sub returns a new filesystem rooted at dir.
func Sub(fsys FS, dir string) (FS, error) {
if !fs.ValidPath(dir) {
return nil, &fs.PathError{Op: "Sub", Path: dir, Err: fs.ErrInvalid}
}
return &subFS{dir, fsys}, nil
}
type subFS struct {
dir string
fs FS
}
func (s *subFS) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "Open", Path: name, Err: fs.ErrInvalid}
}
return s.fs.Open(path.Join(s.dir, name))
}
func (s *subFS) MkdirAll(name string, perm fs.FileMode) error {
if !fs.ValidPath(name) {
return &fs.PathError{Op: "MkdirAll", Path: name, Err: fs.ErrInvalid}
}
return s.fs.MkdirAll(path.Join(s.dir, name), perm)
}
func (s *subFS) WriteFile(name string, data []byte, perm fs.FileMode) error {
if !fs.ValidPath(name) {
return &fs.PathError{Op: "WriteFile", Path: name, Err: fs.ErrInvalid}
}
return s.fs.WriteFile(path.Join(s.dir, name), data, perm)
}
func (s *subFS) RemoveAll(name string) error {
if !fs.ValidPath(name) {
return &fs.PathError{Op: "RemoveAll", Path: name, Err: fs.ErrInvalid}
}
return s.fs.RemoveAll(path.Join(s.dir, name))
}