diff --git a/memmap.go b/memmap.go index c139aa15..d276a301 100644 --- a/memmap.go +++ b/memmap.go @@ -15,14 +15,14 @@ package afero import ( "fmt" + "github.com/spf13/afero/mem" "log" "os" "path/filepath" + "sort" "strings" "sync" "time" - - "github.com/spf13/afero/mem" ) const chmodBits = os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky // Only a subset of bits are allowed to be changed. Documented under os.Chmod() @@ -96,6 +96,12 @@ func (m *MemMapFs) findDescendants(name string) []*mem.FileData { } } + sort.Slice(descendants, func(i, j int) bool { + cur := len(strings.Split(descendants[i].Name(), FilePathSeparator)) + next := len(strings.Split(descendants[j].Name(), FilePathSeparator)) + return cur < next + }) + return descendants } @@ -320,14 +326,15 @@ func (m *MemMapFs) Rename(oldName, newName string) error { return err } + fileData := m.getData()[oldName] + mem.ChangeFileName(fileData, newName) + m.getData()[newName] = fileData + err = m.renameDescendants(oldName, newName) if err != nil { return err } - fileData := m.getData()[oldName] - mem.ChangeFileName(fileData, newName) - m.getData()[newName] = fileData delete(m.getData(), oldName) m.registerWithParent(fileData, 0)