mountlib: make directory entries be returned in sorted order

This commit is contained in:
Nick Craig-Wood 2017-10-27 22:07:59 +01:00
parent ca19fd2d7e
commit 6da6b2556b
2 changed files with 12 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package mountlib
import (
"os"
"path"
"sort"
"strings"
"sync"
"time"
@ -288,8 +289,8 @@ func (d *Dir) Lookup(name string) (node Node, err error) {
return node, nil
}
// ReadDirAll reads the contents of the directory
func (d *Dir) ReadDirAll() (items []Node, err error) {
// ReadDirAll reads the contents of the directory sorted
func (d *Dir) ReadDirAll() (items Nodes, err error) {
// fs.Debugf(d.path, "Dir.ReadDirAll")
d.mu.Lock()
defer d.mu.Unlock()
@ -301,6 +302,7 @@ func (d *Dir) ReadDirAll() (items []Node, err error) {
for _, item := range d.items {
items = append(items, item)
}
sort.Sort(items)
// fs.Debugf(d.path, "Dir.ReadDirAll OK with %d entries", len(items))
return items, nil
}

View File

@ -27,6 +27,14 @@ var (
_ Node = (*Dir)(nil)
)
// Nodes is a slice of Node
type Nodes []Node
// Sort functions
func (ns Nodes) Len() int { return len(ns) }
func (ns Nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
func (ns Nodes) Less(i, j int) bool { return ns[i].DirEntry().Remote() < ns[j].DirEntry().Remote() }
// Noder represents something which can return a node
type Noder interface {
fmt.Stringer