Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions cleanse.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
package memoryfs

import (
"path/filepath"
"strings"
)

func cleanse(path string) string {
path = strings.ReplaceAll(path, "/", separator)
path = filepath.Clean(path)
path = strings.TrimPrefix(path, ".")
path = strings.TrimPrefix(path, separator)
var accepted []string
for _, part := range strings.Split(path, separator) {
switch {
case part == "" || part == ".":
continue
case part == "..":
if len(accepted) > 0 {
accepted = accepted[:len(accepted)-1]
}
continue
}
accepted = append(accepted, part)
}
return strings.Join(accepted, separator)
return path
}
3 changes: 1 addition & 2 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"
)

var separator = "/"
var separator = string(filepath.Separator)

type dir struct {
sync.RWMutex
Expand Down Expand Up @@ -82,7 +82,6 @@ func (d *dir) getDir(name string) (*dir, error) {
}

func (d *dir) ReadDir(name string) ([]fs.DirEntry, error) {

if name == "" {
var entries []fs.DirEntry
d.RLock()
Expand Down
8 changes: 5 additions & 3 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package memoryfs
import (
"io/fs"
"io/ioutil"
"strings"
"time"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ func (m *FS) MkdirAll(path string, perm fs.FileMode) error {
// The caller is permitted to modify the returned byte slice.
// This method should return a copy of the underlying data.
func (m *FS) ReadFile(name string) ([]byte, error) {
f, err := m.dir.Open(name)
f, err := m.dir.Open(cleanse(name))
if err != nil {
return nil, err
}
Expand All @@ -84,7 +85,7 @@ func (m *FS) ReadFile(name string) ([]byte, error) {

// Sub returns an FS corresponding to the subtree rooted at dir.
func (m *FS) Sub(dir string) (fs.FS, error) {
d, err := m.dir.getDir(dir)
d, err := m.dir.getDir(cleanse(dir))
if err != nil {
return nil, err
}
Expand All @@ -102,11 +103,12 @@ func (m *FS) Sub(dir string) (fs.FS, error) {
// The only possible returned error is ErrBadPattern, when pattern
// is malformed.
func (m *FS) Glob(pattern string) ([]string, error) {
pattern = strings.ReplaceAll(pattern, "/", separator)
return m.dir.glob(pattern)
}

// WriteLazyFile creates (or overwrites) the named file.
// The contents of the file are not set at this time, but are read on-demand later using the provided LazyOpener.
func (m *FS) WriteLazyFile(path string, opener LazyOpener, perm fs.FileMode) error {
return m.dir.WriteLazyFile(path, opener, perm)
return m.dir.WriteLazyFile(cleanse(path), opener, perm)
}
4 changes: 2 additions & 2 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Test_AllOperations(t *testing.T) {
results, err = memfs.Glob("files/*/b/*/*.txt")
require.NoError(t, err)
assert.Len(t, results, 1)
assert.Contains(t, results, "files/a/b/c/note.txt")
assert.Contains(t, results, strings.ReplaceAll("files/a/b/c/note.txt", "/", separator))
})

t.Run("Lazy read", func(t *testing.T) {
Expand Down Expand Up @@ -275,7 +275,7 @@ func Test_MkdirAllRoot(t *testing.T) {
err := memfs.MkdirAll(".", 0o644)
require.NoError(t, err)
var count int
err = fs.WalkDir(memfs, ".", func(path string, info fs.DirEntry, err error) error {
err = fs.WalkDir(memfs, ".", func(_ string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand Down