From 9034a12a59ea8a66ac3cf63ffde84e3c12477666 Mon Sep 17 00:00:00 2001 From: Albert Z Wang Date: Wed, 29 Nov 2017 00:11:19 -0500 Subject: [PATCH 1/5] add BasePathFile to fix File.Name() on BasePathFS --- basepath.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/basepath.go b/basepath.go index 5e4fc2e..b24fb0f 100644 --- a/basepath.go +++ b/basepath.go @@ -22,6 +22,16 @@ type BasePathFs struct { path string } +type BasePathFile struct { + File + path string +} + +func (f *BasePathFile) Name() string { + sourcename := f.File.Name() + return strings.TrimPrefix(sourcename, f.path) +} + func NewBasePathFs(source Fs, path string) Fs { 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 { 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) { if name, err = b.RealPath(name); err != nil { 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) { @@ -139,7 +157,11 @@ func (b *BasePathFs) Create(name string) (f File, err error) { if name, err = b.RealPath(name); err != nil { 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 From f12a64564921ed967b3d7ad971b59914ed534c87 Mon Sep 17 00:00:00 2001 From: Albert Z Wang Date: Wed, 29 Nov 2017 13:44:12 -0500 Subject: [PATCH 2/5] add tests (thanks to @ts2909) * copied from https://github.com/ts2909/afero/commit/6c031db617e46c3d6b8415e9f64780339555a288 --- basepath_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/basepath_test.go b/basepath_test.go index abc22b9..33d310d 100644 --- a/basepath_test.go +++ b/basepath_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "runtime" "testing" + "fmt" ) func TestBasePath(t *testing.T) { @@ -140,3 +141,53 @@ 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()) != "/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()) != "/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) + } + fmt.Println(tDir) + if filepath.Dir(tDir) != "/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) + } +} \ No newline at end of file From c5621b3590b79644b5c6feba96b41fd502671762 Mon Sep 17 00:00:00 2001 From: Albert Z Wang Date: Wed, 29 Nov 2017 13:56:26 -0500 Subject: [PATCH 3/5] clean basepath before stripping --- basepath.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basepath.go b/basepath.go index b24fb0f..004aeaa 100644 --- a/basepath.go +++ b/basepath.go @@ -29,7 +29,7 @@ type BasePathFile struct { func (f *BasePathFile) Name() string { sourcename := f.File.Name() - return strings.TrimPrefix(sourcename, f.path) + return strings.TrimPrefix(sourcename, filepath.Clean(f.path)) } func NewBasePathFs(source Fs, path string) Fs { From 4f332f79e74c34224b74286536c151403cb3cf92 Mon Sep 17 00:00:00 2001 From: Albert Z Wang Date: Wed, 29 Nov 2017 14:01:03 -0500 Subject: [PATCH 4/5] clean paths in test comparisons --- basepath_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/basepath_test.go b/basepath_test.go index 33d310d..9001322 100644 --- a/basepath_test.go +++ b/basepath_test.go @@ -151,7 +151,7 @@ func TestBasePathOpenFile(t *testing.T) { if err != nil { t.Fatalf("failed to open file: %v", err) } - if filepath.Dir(f.Name()) != "/tmp" { + if filepath.Dir(f.Name()) != filepath.Clean("/tmp") { t.Fatalf("realpath leaked: %s", f.Name()) } } @@ -164,7 +164,7 @@ func TestBasePathCreate(t *testing.T) { if err != nil { t.Fatalf("failed to create file: %v", err) } - if filepath.Dir(f.Name()) != "/tmp" { + if filepath.Dir(f.Name()) != filepath.Clean("/tmp") { t.Fatalf("realpath leaked: %s", f.Name()) } } @@ -179,7 +179,7 @@ func TestBasePathTempFile(t *testing.T) { t.Fatalf("Failed to TempDir: %v", err) } fmt.Println(tDir) - if filepath.Dir(tDir) != "/tmp" { + if filepath.Dir(tDir) != filepath.Clean("/tmp") { t.Fatalf("Tempdir realpath leaked: %s", tDir) } tempFile, err := TempFile(bp, tDir, "") From 3d990a04946373d6dc788229279fdde214e8a575 Mon Sep 17 00:00:00 2001 From: Albert Z Wang Date: Tue, 12 Dec 2017 20:08:50 -0500 Subject: [PATCH 5/5] remove debug output --- basepath_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/basepath_test.go b/basepath_test.go index 9001322..219527d 100644 --- a/basepath_test.go +++ b/basepath_test.go @@ -5,7 +5,6 @@ import ( "path/filepath" "runtime" "testing" - "fmt" ) func TestBasePath(t *testing.T) { @@ -142,7 +141,6 @@ func TestNestedBasePaths(t *testing.T) { } } - func TestBasePathOpenFile(t *testing.T) { baseFs := &MemMapFs{} baseFs.MkdirAll("/base/path/tmp", 0777) @@ -178,7 +176,6 @@ func TestBasePathTempFile(t *testing.T) { if err != nil { t.Fatalf("Failed to TempDir: %v", err) } - fmt.Println(tDir) if filepath.Dir(tDir) != filepath.Clean("/tmp") { t.Fatalf("Tempdir realpath leaked: %s", tDir) } @@ -190,4 +187,4 @@ func TestBasePathTempFile(t *testing.T) { if expected, actual := tDir, filepath.Dir(tempFile.Name()); expected != actual { t.Fatalf("TempFile realpath leaked: expected %s, got %s", expected, actual) } -} \ No newline at end of file +}