-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsub.go
More file actions
41 lines (35 loc) · 754 Bytes
/
sub.go
File metadata and controls
41 lines (35 loc) · 754 Bytes
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
package hackpadfs
import (
"path"
)
var _ interface {
FS
MountFS
} = &subFS{}
type subFS struct {
rootFS FS
basePath string
}
func newSubFS(fs FS, dir string) (FS, error) {
if !ValidPath(dir) {
return nil, &PathError{Op: "sub", Path: dir, Err: ErrInvalid}
}
return &subFS{
basePath: dir,
rootFS: fs,
}, nil
}
func (fs *subFS) Open(name string) (File, error) {
if !ValidPath(name) {
return nil, &PathError{Op: "open", Path: name, Err: ErrInvalid}
}
mount, subPath := fs.Mount(name)
file, err := mount.Open(subPath)
return file, stripErrPathPrefix(err, name, subPath)
}
func (fs *subFS) Mount(p string) (mount FS, subPath string) {
if !ValidPath(p) {
return fs.rootFS, p
}
return fs.rootFS, path.Join(fs.basePath, p)
}