mirror of https://github.com/spf13/afero.git
Merge pull request #147 from arbelt/fix-basepathfile
fix File.Name() on BasePathFS (wrap File objects returned by BasePathFs)
This commit is contained in:
commit
6e0c3784c4
28
basepath.go
28
basepath.go
|
@ -22,6 +22,16 @@ type BasePathFs struct {
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BasePathFile struct {
|
||||||
|
File
|
||||||
|
path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *BasePathFile) Name() string {
|
||||||
|
sourcename := f.File.Name()
|
||||||
|
return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
|
||||||
|
}
|
||||||
|
|
||||||
func NewBasePathFs(source Fs, path string) Fs {
|
func NewBasePathFs(source Fs, path string) Fs {
|
||||||
return &BasePathFs{source: source, path: path}
|
return &BasePathFs{source: source, path: path}
|
||||||
}
|
}
|
||||||
|
@ -111,14 +121,22 @@ func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File,
|
||||||
if name, err = b.RealPath(name); err != nil {
|
if name, err = b.RealPath(name); err != nil {
|
||||||
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
|
return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
|
||||||
}
|
}
|
||||||
return b.source.OpenFile(name, flag, mode)
|
sourcef, err := b.source.OpenFile(name, flag, mode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &BasePathFile{sourcef, b.path}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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{Op: "open", Path: name, Err: err}
|
return nil, &os.PathError{Op: "open", Path: name, Err: err}
|
||||||
}
|
}
|
||||||
return b.source.Open(name)
|
sourcef, err := b.source.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &BasePathFile{File: sourcef, path: b.path}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
|
||||||
|
@ -139,7 +157,11 @@ 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{Op: "create", Path: name, Err: err}
|
return nil, &os.PathError{Op: "create", Path: name, Err: err}
|
||||||
}
|
}
|
||||||
return b.source.Create(name)
|
sourcef, err := b.source.Create(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &BasePathFile{File: sourcef, path: b.path}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|
// vim: ts=4 sw=4 noexpandtab nolist syn=go
|
||||||
|
|
|
@ -140,3 +140,51 @@ func TestNestedBasePaths(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBasePathOpenFile(t *testing.T) {
|
||||||
|
baseFs := &MemMapFs{}
|
||||||
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
||||||
|
bp := NewBasePathFs(baseFs, "/base/path")
|
||||||
|
f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0600)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to open file: %v", err)
|
||||||
|
}
|
||||||
|
if filepath.Dir(f.Name()) != filepath.Clean("/tmp") {
|
||||||
|
t.Fatalf("realpath leaked: %s", f.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasePathCreate(t *testing.T) {
|
||||||
|
baseFs := &MemMapFs{}
|
||||||
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
||||||
|
bp := NewBasePathFs(baseFs, "/base/path")
|
||||||
|
f, err := bp.Create("/tmp/file.txt")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create file: %v", err)
|
||||||
|
}
|
||||||
|
if filepath.Dir(f.Name()) != filepath.Clean("/tmp") {
|
||||||
|
t.Fatalf("realpath leaked: %s", f.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasePathTempFile(t *testing.T) {
|
||||||
|
baseFs := &MemMapFs{}
|
||||||
|
baseFs.MkdirAll("/base/path/tmp", 0777)
|
||||||
|
bp := NewBasePathFs(baseFs, "/base/path")
|
||||||
|
|
||||||
|
tDir, err := TempDir(bp, "/tmp", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to TempDir: %v", err)
|
||||||
|
}
|
||||||
|
if filepath.Dir(tDir) != filepath.Clean("/tmp") {
|
||||||
|
t.Fatalf("Tempdir realpath leaked: %s", tDir)
|
||||||
|
}
|
||||||
|
tempFile, err := TempFile(bp, tDir, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to TempFile: %v", err)
|
||||||
|
}
|
||||||
|
defer tempFile.Close()
|
||||||
|
if expected, actual := tDir, filepath.Dir(tempFile.Name()); expected != actual {
|
||||||
|
t.Fatalf("TempFile realpath leaked: expected %s, got %s", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue