2015-12-30 10:16:22 +03:00
|
|
|
package afero
|
|
|
|
|
|
|
|
import (
|
2016-02-15 15:34:47 +03:00
|
|
|
"errors"
|
2015-12-30 10:16:22 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-02-15 15:34:47 +03:00
|
|
|
"runtime"
|
2015-12-30 10:16:22 +03:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2017-11-29 08:11:19 +03:00
|
|
|
type BasePathFile struct {
|
|
|
|
File
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasePathFile) Name() string {
|
|
|
|
sourcename := f.File.Name()
|
|
|
|
return strings.TrimPrefix(sourcename, f.path)
|
|
|
|
}
|
|
|
|
|
2016-01-12 05:41:03 +03:00
|
|
|
func NewBasePathFs(source Fs, path string) Fs {
|
|
|
|
return &BasePathFs{source: source, path: path}
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-02-15 15:34:47 +03:00
|
|
|
if err := validateBasePathName(name); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-01-12 08:34:15 +03:00
|
|
|
bpath := filepath.Clean(b.path)
|
2016-01-12 00:32:08 +03:00
|
|
|
path = filepath.Clean(filepath.Join(bpath, name))
|
|
|
|
if !strings.HasPrefix(path, bpath) {
|
2015-12-30 10:16:22 +03:00
|
|
|
return name, os.ErrNotExist
|
|
|
|
}
|
2016-04-18 20:29:19 +03:00
|
|
|
|
2016-04-18 21:14:10 +03:00
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:34:47 +03:00
|
|
|
func validateBasePathName(name string) error {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// Not much to do here;
|
|
|
|
// the virtual file paths all look absolute on *nix.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Windows a common mistake would be to provide an absolute OS path
|
|
|
|
// We could strip out the base part, but that would not be very portable.
|
|
|
|
if filepath.IsAbs(name) {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "realPath", Path: name, Err: errors.New("got a real OS path instead of a virtual")}
|
2016-02-15 15:34:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-30 10:16:22 +03:00
|
|
|
func (b *BasePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "chtimes", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "chmod", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return nil, &os.PathError{Op: "stat", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if oldname, err = b.RealPath(oldname); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "rename", Path: oldname, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
2016-04-20 04:23:58 +03:00
|
|
|
if newname, err = b.RealPath(newname); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "rename", Path: newname, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "remove_all", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "remove", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
2017-11-29 08:11:19 +03:00
|
|
|
sourcef, err := b.source.OpenFile(name, flag, mode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &BasePathFile{sourcef, b.path}, nil
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Open(name string) (f File, err error) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return nil, &os.PathError{Op: "open", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
2017-11-29 08:11:19 +03:00
|
|
|
sourcef, err := b.source.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &BasePathFile{File: sourcef, path: b.path}, nil
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
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) {
|
2016-04-20 04:23:58 +03:00
|
|
|
if name, err = b.RealPath(name); err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return nil, &os.PathError{Op: "create", Path: name, Err: err}
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
2017-11-29 08:11:19 +03:00
|
|
|
sourcef, err := b.source.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &BasePathFile{File: sourcef, path: b.path}, nil
|
2015-12-30 10:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|