Skip to content

Commit

Permalink
Merge branch 'mem-rename-descendants'
Browse files Browse the repository at this point in the history
  • Loading branch information
hanagantig committed Jun 25, 2022
2 parents 1c61a71 + 95a0334 commit 7bc0a84
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions memmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7bc0a84

Please sign in to comment.