mirror of https://github.com/spf13/afero.git
port BasePathFs from filter to filesystem
This commit is contained in:
parent
27539916a7
commit
da00b1fb7c
42
basepath.go
42
basepath.go
|
@ -7,11 +7,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BasePathFs struct {
|
|
||||||
base Fs
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
|
|
||||||
// The BasePathFs restricts all operations to a given path within an Fs.
|
// 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 given file name to the operations on this Fs will be prepended with
|
||||||
// the base path before calling the base Fs.
|
// the base path before calling the base Fs.
|
||||||
|
@ -20,17 +15,14 @@ type BasePathFs struct {
|
||||||
//
|
//
|
||||||
// Note that it does not clean the error messages on return, so you may
|
// Note that it does not clean the error messages on return, so you may
|
||||||
// reveal the real path on errors.
|
// reveal the real path on errors.
|
||||||
func NewBasePathFs(base Fs, path string) FilterFs {
|
type BasePathFs struct {
|
||||||
return &BasePathFs{base: base, path: filepath.Clean(path) + string(os.PathSeparator)}
|
source Fs
|
||||||
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) AddFilter(fs FilterFs) {
|
// NewBasePathFs applies filepath.Clean on creation
|
||||||
fs.SetSource(b.base)
|
func NewBasePathFs(source Fs, path string) *BasePathFs {
|
||||||
b.base = fs
|
return &BasePathFs{source: source, path: filepath.Clean(path) + string(os.PathSeparator)}
|
||||||
}
|
|
||||||
|
|
||||||
func (b *BasePathFs) SetSource(fs Fs) {
|
|
||||||
b.base = fs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// on a file outside the base path it returns the given file name and an error,
|
// on a file outside the base path it returns the given file name and an error,
|
||||||
|
@ -47,14 +39,14 @@ func (b *BasePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"chtimes", name, err}
|
return &os.PathError{"chtimes", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Chtimes(name, atime, mtime)
|
return b.source.Chtimes(name, atime, mtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Chmod(name string, mode os.FileMode) (err error) {
|
func (b *BasePathFs) Chmod(name string, mode os.FileMode) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"chmod", name, err}
|
return &os.PathError{"chmod", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Chmod(name, mode)
|
return b.source.Chmod(name, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Name() string {
|
func (b *BasePathFs) Name() string {
|
||||||
|
@ -65,7 +57,7 @@ func (b *BasePathFs) Stat(name string) (fi os.FileInfo, err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return nil, &os.PathError{"stat", name, err}
|
return nil, &os.PathError{"stat", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Stat(name)
|
return b.source.Stat(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Rename(oldname, newname string) (err error) {
|
func (b *BasePathFs) Rename(oldname, newname string) (err error) {
|
||||||
|
@ -75,56 +67,56 @@ func (b *BasePathFs) Rename(oldname, newname string) (err error) {
|
||||||
if newname, err = b.RealPath(newname); err != nil {
|
if newname, err = b.RealPath(newname); err != nil {
|
||||||
return &os.PathError{"rename", newname, err}
|
return &os.PathError{"rename", newname, err}
|
||||||
}
|
}
|
||||||
return b.base.Rename(oldname, newname)
|
return b.source.Rename(oldname, newname)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) RemoveAll(name string) (err error) {
|
func (b *BasePathFs) RemoveAll(name string) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"remove_all", name, err}
|
return &os.PathError{"remove_all", name, err}
|
||||||
}
|
}
|
||||||
return b.base.RemoveAll(name)
|
return b.source.RemoveAll(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Remove(name string) (err error) {
|
func (b *BasePathFs) Remove(name string) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"remove", name, err}
|
return &os.PathError{"remove", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Remove(name)
|
return b.source.Remove(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File, err error) {
|
func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File, err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return nil, &os.PathError{"openfile", name, err}
|
return nil, &os.PathError{"openfile", name, err}
|
||||||
}
|
}
|
||||||
return b.base.OpenFile(name, flag, mode)
|
return b.source.OpenFile(name, flag, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Open(name string) (f File, err error) {
|
func (b *BasePathFs) Open(name string) (f File, err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return nil, &os.PathError{"open", name, err}
|
return nil, &os.PathError{"open", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Open(name)
|
return b.source.Open(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"mkdir", name, err}
|
return &os.PathError{"mkdir", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Mkdir(name, mode)
|
return b.source.Mkdir(name, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
|
func (b *BasePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return &os.PathError{"mkdir", name, err}
|
return &os.PathError{"mkdir", name, err}
|
||||||
}
|
}
|
||||||
return b.base.MkdirAll(name, mode)
|
return b.source.MkdirAll(name, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Create(name string) (f File, err error) {
|
func (b *BasePathFs) Create(name string) (f File, err error) {
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return nil, &os.PathError{"create", name, err}
|
return nil, &os.PathError{"create", name, err}
|
||||||
}
|
}
|
||||||
return b.base.Create(name)
|
return b.source.Create(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|
||||||
|
|
Loading…
Reference in New Issue