diff --git a/basepath.go b/basepath.go index 4b325ed..b9e842f 100644 --- a/basepath.go +++ b/basepath.go @@ -29,7 +29,6 @@ func NewBasePathFs(source Fs, path string) Fs { // 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) { - if err := validateBasePathName(name); err != nil { return "", err } @@ -39,6 +38,11 @@ func (b *BasePathFs) RealPath(name string) (path string, err error) { if !strings.HasPrefix(path, bpath) { return name, os.ErrNotExist } + + if parentBasePathFs, ok := b.source.(*BasePathFs); ok { + return parentBasePathFs.RealPath(path) + } + return path, nil } diff --git a/basepath_test.go b/basepath_test.go index 3c91f5d..bfe39c4 100644 --- a/basepath_test.go +++ b/basepath_test.go @@ -90,3 +90,40 @@ func TestRealPath(t *testing.T) { } } + +func TestNestedBasePaths(t *testing.T) { + type dirSpec struct { + Dir1, Dir2, Dir3 string + } + dirSpecs := []dirSpec{ + dirSpec{Dir1: "/", Dir2: "/", Dir3: "/"}, + dirSpec{Dir1: "/", Dir2: "/path2", Dir3: "/"}, + dirSpec{Dir1: "/path1/dir", Dir2: "/path2/dir/", Dir3: "/path3/dir"}, + } + + for _, ds := range dirSpecs { + memFs := NewMemMapFs() + level1Fs := NewBasePathFs(memFs, ds.Dir1) + level2Fs := NewBasePathFs(level1Fs, ds.Dir2) + level3Fs := NewBasePathFs(level2Fs, ds.Dir3) + + type spec struct { + BaseFs Fs + FileName string + ExpectedPath string + } + specs := []spec{ + spec{BaseFs: level3Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, ds.Dir2, ds.Dir3, "f.txt")}, + spec{BaseFs: level2Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, ds.Dir2, "f.txt")}, + spec{BaseFs: level1Fs, FileName: "f.txt", ExpectedPath: filepath.Join(ds.Dir1, "f.txt")}, + } + + for _, s := range specs { + if actualPath, err := s.BaseFs.(*BasePathFs).RealPath(s.FileName); err != nil { + t.Errorf("Got error %s", err.Error()) + } else if actualPath != s.ExpectedPath { + t.Errorf("Expected \n%s got \n%s", s.ExpectedPath, actualPath) + } + } + } +}