Skip to content

Commit

Permalink
refine BasePathFs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed Jan 12, 2016
1 parent 76b64e1 commit a3ed628
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
11 changes: 4 additions & 7 deletions basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ type BasePathFs struct {
path string
}

// NewBasePathFs applies filepath.Clean on creation
func NewBasePathFs(source Fs, path string) *BasePathFs {
return &BasePathFs{source: source, path: filepath.Clean(path) + string(os.PathSeparator)}
}

// 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) {
path = filepath.Clean(filepath.Join(b.path, name))
if !strings.HasPrefix(path, b.path) {
bpath := filepath.Clean(b.path) + string(os.PathSeparator)

path = filepath.Clean(filepath.Join(bpath, name))
if !strings.HasPrefix(path, bpath) {
return name, os.ErrNotExist
}
return path, nil
Expand Down
2 changes: 1 addition & 1 deletion basepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func TestBasePath(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
bp := NewBasePathFs(baseFs, "/base/path")
bp := &BasePathFs{source: baseFs, path: "/base/path"}

if _, err := bp.Create("/tmp/foo"); err != nil {
t.Errorf("Failed to set real path")
Expand Down

0 comments on commit a3ed628

Please sign in to comment.