-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirfs.go
104 lines (85 loc) · 2.38 KB
/
dirfs.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
package wrfs
import (
"os"
"time"
)
// DirFS returns a file system (an fs.FS) for the tree of files rooted at the directory dir.
//
// Note that DirFS("/prefix") only guarantees that the Open calls it makes to the
// operating system will begin with "/prefix": DirFS("/prefix").Open("file") is the
// same as os.Open("/prefix/file"). So if /prefix/file is a symbolic link pointing outside
// the /prefix tree, then using DirFS does not stop the access any more than using
// os.Open does. DirFS is therefore not a general substitute for a chroot-style security
// mechanism when the directory tree contains arbitrary content.
func DirFS(dir string) FS {
return &subFS{fsys: hostFS{}, dir: dir}
}
type hostFS struct{}
func (hostFS) Chmod(name string, mode FileMode) error {
return os.Chmod(name, mode)
}
func (hostFS) Chown(name string, uid, gid int) error {
return os.Chown(name, uid, gid)
}
func (hostFS) Chtimes(name string, atime, mtime time.Time) error {
return os.Chtimes(name, atime, mtime)
}
func (hostFS) Mkdir(path string, perm FileMode) error {
return os.Mkdir(path, perm)
}
func (hostFS) Open(name string) (File, error) {
f, err := os.Open(name)
if err != nil {
return nil, err // nil fs.File
}
return f, nil
}
func (hostFS) OpenFile(name string, flag int, perm FileMode) (File, error) {
file, err := os.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
return file, nil
}
func (hostFS) Stat(name string) (FileInfo, error) {
fi, err := os.Stat(name)
if err != nil {
return nil, err
}
return fi, nil
}
func (hostFS) Lstat(name string) (FileInfo, error) {
fi, err := os.Lstat(name)
if err != nil {
return nil, err
}
return fi, nil
}
func (hostFS) Readlink(name string) (string, error) {
link, err := os.Readlink(name)
if err != nil {
return "", err
}
return link, nil
}
func (hostFS) Remove(name string) error {
return os.Remove(name)
}
func (hostFS) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (hostFS) Rename(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
}
func (hostFS) SameFile(fi1, fi2 FileInfo) bool {
return os.SameFile(fi1, fi2)
}
func (hostFS) Symlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
}
func (hostFS) Link(oldname, newname string) error {
return os.Link(oldname, newname)
}
func (hostFS) Truncate(name string, size int64) error {
return os.Truncate(name, size)
}