From 4e71f87b0b3a1cac73c4c45c79fb527ea040504b Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Fri, 6 Sep 2024 14:52:13 +0200 Subject: [PATCH] newunionfs: sort ReadDir result The previous code tried to make the result deterministic (See commit 74851f1f), by always having the largest name at the end. This would work for simple hashtable. Go does not use this, as discussed in https://github.com/hanwen/go-fuse/issues/476#issuecomment-1594751105 Use sort.Slice() to sort the entries by name. Change-Id: Ibf576b00a214e38aa6bcd6b64ee6b9844408e04a --- newunionfs/unionfs.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/newunionfs/unionfs.go b/newunionfs/unionfs.go index 97f9c66e2..a51d3e690 100644 --- a/newunionfs/unionfs.go +++ b/newunionfs/unionfs.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "log" "path/filepath" + "sort" "syscall" "github.com/hanwen/go-fuse/v2/fs" @@ -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 }