13
_ Lstater = (*BasePathFs)(nil)
14
_ fs.ReadDirFile = (*BasePathFile)(nil)
17
// The BasePathFs restricts all operations to a given path within an Fs.
18
// The given file name to the operations on this Fs will be prepended with
19
// the base path before calling the base Fs.
20
// Any file name (after filepath.Clean()) outside this base path will be
21
// treated as non existing file.
23
// Note that it does not clean the error messages on return, so you may
24
// reveal the real path on errors.
25
type BasePathFs struct {
30
type BasePathFile struct {
35
func (f *BasePathFile) Name() string {
36
sourcename := f.File.Name()
37
return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
40
func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) {
41
if rdf, ok := f.File.(fs.ReadDirFile); ok {
44
return readDirFile{f.File}.ReadDir(n)
47
func NewBasePathFs(source Fs, path string) Fs {
48
return &BasePathFs{source: source, path: path}
51
// on a file outside the base path it returns the given file name and an error,
52
// else the given file with the base path prepended
53
func (b *BasePathFs) RealPath(name string) (path string, err error) {
54
if err := validateBasePathName(name); err != nil {
58
bpath := filepath.Clean(b.path)
59
path = filepath.Clean(filepath.Join(bpath, name))
60
if !strings.HasPrefix(path, bpath) {
61
return name, os.ErrNotExist
67
func validateBasePathName(name string) error {
68
if runtime.GOOS != "windows" {
69
// Not much to do here;
70
// the virtual file paths all look absolute on *nix.
74
// On Windows a common mistake would be to provide an absolute OS path
75
// We could strip out the base part, but that would not be very portable.
76
if filepath.IsAbs(name) {
83
func (b *BasePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
84
if name, err = b.RealPath(name); err != nil {
85
return &os.PathError{Op: "chtimes", Path: name, Err: err}
87
return b.source.Chtimes(name, atime, mtime)
90
func (b *BasePathFs) Chmod(name string, mode os.FileMode) (err error) {
91
if name, err = b.RealPath(name); err != nil {
92
return &os.PathError{Op: "chmod", Path: name, Err: err}
94
return b.source.Chmod(name, mode)
97
func (b *BasePathFs) Chown(name string, uid, gid int) (err error) {
98
if name, err = b.RealPath(name); err != nil {
99
return &os.PathError{Op: "chown", Path: name, Err: err}
101
return b.source.Chown(name, uid, gid)
104
func (b *BasePathFs) Name() string {
108
func (b *BasePathFs) Stat(name string) (fi os.FileInfo, err error) {
109
if name, err = b.RealPath(name); err != nil {
110
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
112
return b.source.Stat(name)
115
func (b *BasePathFs) Rename(oldname, newname string) (err error) {
116
if oldname, err = b.RealPath(oldname); err != nil {
117
return &os.PathError{Op: "rename", Path: oldname, Err: err}
119
if newname, err = b.RealPath(newname); err != nil {
120
return &os.PathError{Op: "rename", Path: newname, Err: err}
122
return b.source.Rename(oldname, newname)
125
func (b *BasePathFs) RemoveAll(name string) (err error) {
126
if name, err = b.RealPath(name); err != nil {
127
return &os.PathError{Op: "remove_all", Path: name, Err: err}
129
return b.source.RemoveAll(name)
132
func (b *BasePathFs) Remove(name string) (err error) {
133
if name, err = b.RealPath(name); err != nil {
134
return &os.PathError{Op: "remove", Path: name, Err: err}
136
return b.source.Remove(name)
139
func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File, err error) {
140
if name, err = b.RealPath(name); err != nil {
141
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
143
sourcef, err := b.source.OpenFile(name, flag, mode)
147
return &BasePathFile{sourcef, b.path}, nil
150
func (b *BasePathFs) Open(name string) (f File, err error) {
151
if name, err = b.RealPath(name); err != nil {
152
return nil, &os.PathError{Op: "open", Path: name, Err: err}
154
sourcef, err := b.source.Open(name)
158
return &BasePathFile{File: sourcef, path: b.path}, nil
161
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
162
if name, err = b.RealPath(name); err != nil {
163
return &os.PathError{Op: "mkdir", Path: name, Err: err}
165
return b.source.Mkdir(name, mode)
168
func (b *BasePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
169
if name, err = b.RealPath(name); err != nil {
170
return &os.PathError{Op: "mkdir", Path: name, Err: err}
172
return b.source.MkdirAll(name, mode)
175
func (b *BasePathFs) Create(name string) (f File, err error) {
176
if name, err = b.RealPath(name); err != nil {
177
return nil, &os.PathError{Op: "create", Path: name, Err: err}
179
sourcef, err := b.source.Create(name)
183
return &BasePathFile{File: sourcef, path: b.path}, nil
186
func (b *BasePathFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
187
name, err := b.RealPath(name)
189
return nil, false, &os.PathError{Op: "lstat", Path: name, Err: err}
191
if lstater, ok := b.source.(Lstater); ok {
192
return lstater.LstatIfPossible(name)
194
fi, err := b.source.Stat(name)
195
return fi, false, err
198
func (b *BasePathFs) SymlinkIfPossible(oldname, newname string) error {
199
oldname, err := b.RealPath(oldname)
201
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
203
newname, err = b.RealPath(newname)
205
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
207
if linker, ok := b.source.(Linker); ok {
208
return linker.SymlinkIfPossible(oldname, newname)
210
return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: ErrNoSymlink}
213
func (b *BasePathFs) ReadlinkIfPossible(name string) (string, error) {
214
name, err := b.RealPath(name)
216
return "", &os.PathError{Op: "readlink", Path: name, Err: err}
218
if reader, ok := b.source.(LinkReader); ok {
219
return reader.ReadlinkIfPossible(name)
221
return "", &os.PathError{Op: "readlink", Path: name, Err: ErrNoReadlink}