From 239f6825f76d3046198ab556b1851e40422c9558 Mon Sep 17 00:00:00 2001 From: W-Mark Kubacki Date: Mon, 18 Apr 2016 23:05:08 +0200 Subject: [PATCH] browse: When sorting by size, offset directories Assigns negative sizes to directories in order to have them listed reliably before any zero-sized files. That order is what most users expect when sorting by size. As side effect directories will appear before files on all filesystem implementations. To give an example: before this change directories had a size of 4 KiB when using Linux with ext4 or tmpfs, and with ZFS a size resembling an estimation of the number of leaves within said directory. --- middleware/browse/browse.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/middleware/browse/browse.go b/middleware/browse/browse.go index e350c07a..a21b2304 100644 --- a/middleware/browse/browse.go +++ b/middleware/browse/browse.go @@ -135,9 +135,20 @@ func (l byName) Less(i, j int) bool { } // By Size -func (l bySize) Len() int { return len(l.Items) } -func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } -func (l bySize) Less(i, j int) bool { return l.Items[i].Size < l.Items[j].Size } +func (l bySize) Len() int { return len(l.Items) } +func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } + +const directoryOffset = -1 << 31 // = math.MinInt32 +func (l bySize) Less(i, j int) bool { + iSize, jSize := l.Items[i].Size, l.Items[j].Size + if l.Items[i].IsDir { + iSize = directoryOffset + iSize + } + if l.Items[j].IsDir { + jSize = directoryOffset + jSize + } + return iSize < jSize +} // By Time func (l byTime) Len() int { return len(l.Items) }