2015-12-30 10:16:22 +03:00
|
|
|
package afero
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The BasePathFs restricts all operations to a given path within an Fs.
|
|
|
|
// The given file name to the operations on this Fs will be prepended with
|
|
|
|
// the base path before calling the base Fs.
|
|
|
|
// Any file name (after filepath.Clean()) outside this base path will be
|
|
|
|
// treated as non existing file.
|
|
|
|
//
|
|
|
|
// Note that it does not clean the error messages on return, so you may
|
|
|
|
// reveal the real path on errors.
|
2016-01-11 23:30:26 +03:00
|
|
|
type BasePathFs struct {
|
|
|
|
source Fs
|
|
|
|
path string
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
2016-01-11 23:30:26 +03:00
|
|
|
// NewBasePathFs applies filepath.Clean on creation
|
|
|
|
func NewBasePathFs(source Fs, path string) *BasePathFs {
|
|
|
|
return &BasePathFs{source: source, path: filepath.Clean(path) + string(os.PathSeparator)}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// on a file outside the base path it returns the given file name and an error,
|
|
|
|
// else the given file with the base path prepended
|
|
|
|
func (b *BasePathFs) RealPath(name string) (path string, err error) {
|
|
|
|
path = filepath.Clean(filepath.Join(b.path, name))
|
|
|
|
if !strings.HasPrefix(path, b.path) {
|
|
|
|
return name, os.ErrNotExist
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"chtimes", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Chtimes(name, atime, mtime)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Chmod(name string, mode os.FileMode) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"chmod", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Chmod(name, mode)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Name() string {
|
|
|
|
return "BasePathFs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Stat(name string) (fi os.FileInfo, err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return nil, &os.PathError{"stat", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Stat(name)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Rename(oldname, newname string) (err error) {
|
|
|
|
if oldname, err = b.RealPath(oldname); err != nil {
|
|
|
|
return &os.PathError{"rename", oldname, err}
|
|
|
|
}
|
|
|
|
if newname, err = b.RealPath(newname); err != nil {
|
|
|
|
return &os.PathError{"rename", newname, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Rename(oldname, newname)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) RemoveAll(name string) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"remove_all", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.RemoveAll(name)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Remove(name string) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"remove", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Remove(name)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File, err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return nil, &os.PathError{"openfile", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.OpenFile(name, flag, mode)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Open(name string) (f File, err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return nil, &os.PathError{"open", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Open(name)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"mkdir", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Mkdir(name, mode)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return &os.PathError{"mkdir", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.MkdirAll(name, mode)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Create(name string) (f File, err error) {
|
|
|
|
if name, err = b.RealPath(name); err != nil {
|
|
|
|
return nil, &os.PathError{"create", name, err}
|
|
|
|
}
|
2016-01-11 23:30:26 +03:00
|
|
|
return b.source.Create(name)
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|