Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper root init for MemMapFs in windows #226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ type BasePathFile struct {

func (f *BasePathFile) Name() string {
sourcename := f.File.Name()
return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
sourcename = strings.TrimPrefix(sourcename, filepath.VolumeName(sourcename))
sourcename = strings.TrimPrefix(sourcename, filepath.Clean(f.path))
return sourcename
}

func NewBasePathFs(source Fs, path string) Fs {
Expand Down
2 changes: 1 addition & 1 deletion composite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) {
overlay := &MemMapFs{}

for i := 0; i < 5; i++ {
WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0777)
WriteFile(base, fmt.Sprintf("/file%d.txt", i), []byte("afero"), 0777)
}

ufs := &CopyOnWriteFs{base: base, layer: overlay}
Expand Down
98 changes: 80 additions & 18 deletions memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ func NewMemMapFs() Fs {
func (m *MemMapFs) getData() map[string]*mem.FileData {
m.init.Do(func() {
m.data = make(map[string]*mem.FileData)
// Root should always exist, right?
// TODO: what about windows?
m.data[FilePathSeparator] = mem.CreateDir(FilePathSeparator)
// Root should always exist
absolutePath, err := filepath.Abs(FilePathSeparator)
if err != nil {
log.Fatal(err)
panic(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is panic needed if you log.Fatal above?

}
m.data[absolutePath] = mem.CreateDir(absolutePath)
})
return m.data
}

func (*MemMapFs) Name() string { return "MemMapFS" }

func (m *MemMapFs) Create(name string) (File, error) {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return nil, err
}

m.mu.Lock()
file := mem.CreateFile(name)
m.getData()[name] = file
Expand Down Expand Up @@ -109,7 +118,12 @@ func (m *MemMapFs) registerWithParent(f *mem.FileData) {
}

func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return err
}

x, ok := m.getData()[name]
if ok {
// Only return ErrFileExists if it's a file, not a directory.
Expand All @@ -126,7 +140,11 @@ func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
}

func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return err
}

m.mu.RLock()
_, ok := m.getData()[name]
Expand Down Expand Up @@ -158,16 +176,22 @@ func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error {
}

// Handle some relative paths
func normalizePath(path string) string {
func normalizePath(path string) (string, error) {
path = filepath.Clean(path)
rootAbs, err := filepath.Abs(FilePathSeparator)
if err != nil {
return "", err
}

switch path {
case ".":
return FilePathSeparator
return rootAbs, nil
case "..":
return FilePathSeparator
return rootAbs, nil
case FilePathSeparator:
return rootAbs, nil
default:
return path
return filepath.Abs(path)
}
}

Expand All @@ -188,7 +212,12 @@ func (m *MemMapFs) openWrite(name string) (File, error) {
}

func (m *MemMapFs) open(name string) (*mem.FileData, error) {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return nil, err
}


m.mu.RLock()
f, ok := m.getData()[name]
Expand All @@ -200,7 +229,12 @@ func (m *MemMapFs) open(name string) (*mem.FileData, error) {
}

func (m *MemMapFs) lockfreeOpen(name string) (*mem.FileData, error) {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return nil, err
}

f, ok := m.getData()[name]
if ok {
return f, nil
Expand Down Expand Up @@ -243,7 +277,12 @@ func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, erro
}

func (m *MemMapFs) Remove(name string) error {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return err
}


m.mu.Lock()
defer m.mu.Unlock()
Expand All @@ -261,7 +300,12 @@ func (m *MemMapFs) Remove(name string) error {
}

func (m *MemMapFs) RemoveAll(path string) error {
path = normalizePath(path)
var err error
path, err = normalizePath(path)
if err != nil {
return err
}

m.mu.Lock()
m.unRegisterWithParent(path)
m.mu.Unlock()
Expand All @@ -282,8 +326,16 @@ func (m *MemMapFs) RemoveAll(path string) error {
}

func (m *MemMapFs) Rename(oldname, newname string) error {
oldname = normalizePath(oldname)
newname = normalizePath(newname)
var err error
oldname, err = normalizePath(oldname)
if err != nil {
return err
}

newname, err = normalizePath(newname)
if err != nil {
return err
}

if oldname == newname {
return nil
Expand Down Expand Up @@ -318,7 +370,12 @@ func (m *MemMapFs) Stat(name string) (os.FileInfo, error) {
}

func (m *MemMapFs) Chmod(name string, mode os.FileMode) error {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return err
}


m.mu.RLock()
f, ok := m.getData()[name]
Expand All @@ -335,7 +392,12 @@ func (m *MemMapFs) Chmod(name string, mode os.FileMode) error {
}

func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
name = normalizePath(name)
var err error
name, err = normalizePath(name)
if err != nil {
return err
}


m.mu.RLock()
f, ok := m.getData()[name]
Expand Down
22 changes: 15 additions & 7 deletions memmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ func TestNormalizePath(t *testing.T) {
expected string
}

rootAbs, err := filepath.Abs(FilePathSeparator)
if err != nil {
t.Fatal(err)
}

data := []test{
{".", FilePathSeparator},
{"./", FilePathSeparator},
{"..", FilePathSeparator},
{"../", FilePathSeparator},
{"./..", FilePathSeparator},
{"./../", FilePathSeparator},
{".", rootAbs},
{"./", rootAbs},
{"..", rootAbs},
{"../", rootAbs},
{"./..", rootAbs},
{"./../", rootAbs},
}

for i, d := range data {
cpath := normalizePath(d.input)
cpath, err := normalizePath(d.input)
if err != nil {
t.Error(err)
}
if d.expected != cpath {
t.Errorf("Test %d failed. Expected %q got %q", i, d.expected, cpath)
}
Expand Down
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestIsDir(t *testing.T) {
data := []test{
{"./", true},
{"/", true},
{"./this-directory-does-not-existi", false},
{"./this-directory-does-not-exist", false},
{"/this-absolute-directory/does-not-exist", false},
}

Expand Down