Skip to content

Commit

Permalink
newunionfs: sort ReadDir result
Browse files Browse the repository at this point in the history
The previous code tried to make the result deterministic (See commit
74851f1), by always having the largest name at the end. This would
work for simple hashtable. Go does not use this, as discussed in
#476 (comment)

Use sort.Slice() to sort the entries by name.

Change-Id: Ibf576b00a214e38aa6bcd6b64ee6b9844408e04a
  • Loading branch information
hanwen committed Sep 6, 2024
1 parent f24a8ff commit 4e71f87
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions newunionfs/unionfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"log"
"path/filepath"
"sort"
"syscall"

"github.com/hanwen/go-fuse/v2/fs"
Expand Down Expand Up @@ -346,29 +347,19 @@ func (n *unionFSNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno)
// deepest root first.
readRoot(root.roots[len(root.roots)-i-1], dir, names)
}
result := make([]fuse.DirEntry, 0, 2*len(names))
maxIdx := -1
maxName := ""
result := make([]fuse.DirEntry, 0, len(names))
for nm, mode := range names {
marker := filePathHash(filepath.Join(dir, nm))
if _, ok := markers[marker]; ok {
continue
}
if nm > maxName {
maxName = nm
maxIdx = len(result)
}

result = append(result, fuse.DirEntry{
Name: nm,
Mode: mode,
})
}

if len(result) > 0 {
result = append(result[maxIdx:], result[:maxIdx]...)
}

sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
return fs.NewListDirStream(result), 0
}

Expand Down

0 comments on commit 4e71f87

Please sign in to comment.