fork/gdu
git clone https://git.julia.blue/fork/gdu
da638da
subvolume
1package analyze 2 3import ( 4"sort" 5 6"github.com/dundee/gdu/v5/pkg/fs" 7) 8 9// TopList is a list of top largest files 10type TopList struct { 11Items fs.Files 12Count int 13MinSize int64 14} 15 16// NewTopList creates new TopList 17func NewTopList (count int )* TopList { 18return & TopList {Count :count } 19} 20 21// Add adds file to the list 22func (tl * TopList )Add (file fs.Item ) { 23if file .GetSize ()> tl .MinSize || len (tl .Items )< tl .Count { 24tl .Items = append (tl .Items ,file ) 25sort .Sort (fs .ByApparentSize (tl .Items )) 26if len (tl .Items )> tl .Count { 27tl .Items = tl .Items [1 :] 28 } 29tl .MinSize = tl .Items [0 ].GetSize () 30 } 31} 32 33func CollectTopFiles (dir fs.Item ,count int ) fs.Files { 34topList := NewTopList (count ) 35walkDir (dir ,topList ) 36sort .Sort (sort .Reverse (fs .ByApparentSize (topList .Items ))) 37return topList .Items 38} 39 40func walkDir (dir fs.Item ,topList * TopList ) { 41for item := range dir .GetFiles (fs .SortBySize ,fs .SortDesc ) { 42if item .IsDir () { 43walkDir (item ,topList ) 44 }else { 45topList .Add (item ) 46 } 47 } 48}