fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
subvolume
1package fs 2 3import ( 4"io" 5"iter" 6"time" 7 8"github.com/maruel/natural" 9) 10 11// SortBy represents the field to sort files by 12type SortBy int 13 14const ( 15SortBySize SortBy = iota 16SortByName 17SortByItemCount 18SortByMtime 19SortByApparentSize 20) 21 22// SortOrder represents the sort direction 23type SortOrder int 24 25const ( 26SortAsc SortOrder = iota 27SortDesc 28) 29 30// Item is a FS item (file or dir) 31type Item interface { 32GetPath ()string 33GetName ()string 34GetFlag ()rune 35IsDir ()bool 36GetSize ()int64 37GetType ()string 38GetUsage ()int64 39GetMtime () time.Time 40GetItemCount ()int64 41GetParent ()Item 42SetParent (Item ) 43GetMultiLinkedInode ()uint64 44EncodeJSON (writer io.Writer ,topLevel bool ,attributes JSONAttributes )error 45GetItemStats (linkedItems HardLinkedItems ,filteringFiles bool ) (itemCount int64 ,size ,usage int64 ) 46UpdateStats (linkedItems HardLinkedItems ) 47UpdateStatsWithFileFiltering (linkedItems HardLinkedItems ) 48AddFile (Item ) 49GetFiles (SortBy ,SortOrder ) iter.Seq [Item ] 50GetFilesLocked (SortBy ,SortOrder ) iter.Seq [Item ] 51RemoveFile (Item ) 52RemoveFileByName (name string ) 53RLock ()func () 54} 55 56// SymlinkItem is an optional interface implemented by items that carry a 57// symlink target. Consumers type-assert an Item to it to display the target 58// (e.g. "name -> target"); items that are not symlinks return an empty string. 59type SymlinkItem interface { 60GetSymlinkTarget ()string 61} 62 63// Files - slice of pointers to File 64type Files []Item 65 66// HardLinkedItems maps inode number to array of all hard linked items 67type HardLinkedItems map [uint64 ]Files 68 69// IndexOf searches File in Files and returns its index 70func (f Files )IndexOf (file Item ) (int ,bool ) { 71for i ,item := range f { 72if item == file { 73return i ,true 74 } 75 } 76return 0 ,false 77} 78 79// FindByName searches name in Files and returns its index 80func (f Files )FindByName (name string ) (int ,bool ) { 81for i ,item := range f { 82if item .GetName ()== name { 83return i ,true 84 } 85 } 86return 0 ,false 87} 88 89// Remove removes File from Files 90func (f Files )Remove (file Item )Files { 91index ,ok := f .IndexOf (file ) 92if ! ok { 93return f 94 } 95return append (f [:index ],f [index + 1 :]... ) 96} 97 98// RemoveByName removes File from Files 99func (f Files )RemoveByName (name string )Files { 100index ,ok := f .FindByName (name ) 101if ! ok { 102return f 103 } 104return append (f [:index ],f [index + 1 :]... ) 105} 106 107func (f Files )Len ()int {return len (f ) } 108func (f Files )Swap (i ,j int ) {f [i ],f [j ]= f [j ],f [i ] } 109func (f Files )Less (i ,j int )bool { 110if f [i ].GetUsage ()!= f [j ].GetUsage () { 111return f [i ].GetUsage ()< f [j ].GetUsage () 112 } 113// if usage is the same, sort by name 114return natural .Less (f [i ].GetName (),f [j ].GetName ()) 115} 116 117// ByApparentSize sorts files by apparent size 118type ByApparentSize Files 119 120func (f ByApparentSize )Len ()int {return len (f ) } 121func (f ByApparentSize )Swap (i ,j int ) {f [i ],f [j ]= f [j ],f [i ] } 122func (f ByApparentSize )Less (i ,j int )bool { 123if f [i ].GetSize ()!= f [j ].GetSize () { 124return f [i ].GetSize ()< f [j ].GetSize () 125 } 126// if size is the same, sort by name 127return natural .Less (f [i ].GetName (),f [j ].GetName ()) 128} 129 130// ByItemCount sorts files by item count 131type ByItemCount Files 132 133func (f ByItemCount )Len ()int {return len (f ) } 134func (f ByItemCount )Swap (i ,j int ) {f [i ],f [j ]= f [j ],f [i ] } 135func (f ByItemCount )Less (i ,j int )bool { 136if f [i ].GetItemCount ()!= f [j ].GetItemCount () { 137return f [i ].GetItemCount ()< f [j ].GetItemCount () 138 } 139// if item count is the same, sort by name 140return natural .Less (f [i ].GetName (),f [j ].GetName ()) 141} 142 143// ByName sorts files by name 144type ByName Files 145 146func (f ByName )Len ()int {return len (f ) } 147func (f ByName )Swap (i ,j int ) {f [i ],f [j ]= f [j ],f [i ] } 148func (f ByName )Less (i ,j int )bool {return natural .Less (f [i ].GetName (),f [j ].GetName ()) } 149 150// ByMtime sorts files by name 151type ByMtime Files 152 153func (f ByMtime )Len ()int {return len (f ) } 154func (f ByMtime )Swap (i ,j int ) {f [i ],f [j ]= f [j ],f [i ] } 155func (f ByMtime )Less (i ,j int )bool { 156if ! f [i ].GetMtime ().Equal (f [j ].GetMtime ()) { 157return f [i ].GetMtime ().Before (f [j ].GetMtime ()) 158 } 159// if item count is the same, sort by name 160return natural .Less (f [i ].GetName (),f [j ].GetName ()) 161} 162 163// ParseSortBy converts a string to SortBy 164func ParseSortBy (s string )SortBy { 165switch s { 166case "name" : 167return SortByName 168case "size" : 169return SortBySize 170case "itemCount" : 171return SortByItemCount 172case "mtime" : 173return SortByMtime 174default : 175return SortBySize 176 } 177} 178 179// ParseSortOrder converts a string to SortOrder 180func ParseSortOrder (s string )SortOrder { 181if s == "asc" { 182return SortAsc 183 } 184return SortDesc 185}