fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
master
1package analyze 2 3import ( 4"os" 5"path/filepath" 6 7"github.com/dundee/gdu/v5/internal/common" 8"github.com/dundee/gdu/v5/pkg/fs" 9 log"github.com/sirupsen/logrus" 10) 11 12// ParallelStableOrderAnalyzer implements Analyzer 13type ParallelStableOrderAnalyzer struct { 14BaseAnalyzer 15} 16 17// CreateStableOrderAnalyzer returns parallel Analyzer which keeps stable order of files 18func CreateStableOrderAnalyzer ()* ParallelStableOrderAnalyzer { 19a := & ParallelStableOrderAnalyzer {} 20a .Init () 21return a 22} 23 24// AnalyzeDir analyzes given path 25func (a * ParallelStableOrderAnalyzer )AnalyzeDir ( 26path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 27) fs.Item { 28a .ignoreDir = ignore 29a .ignoreFileType = fileTypeFilter 30 31go a .UpdateProgress () 32dir := a .processDir (path ) 33 34dir .BasePath = filepath .Dir (path ) 35a .wait .Wait () 36 37a .progressDoneChan <- struct {}{} 38a .doneChan .Broadcast () 39 40return dir 41} 42 43func (a * ParallelStableOrderAnalyzer )processDir (path string )* Dir { 44type indexedItem struct { 45index int 46item fs.Item 47 } 48 49var ( 50file * File 51err error 52totalSize int64 53info os.FileInfo 54itemCount int 55dirCount int 56 ) 57 58a .wait .Add (1 ) 59 60files ,err := os .ReadDir (path ) 61if err != nil { 62log .err .Error ()) 63 } 64 65dir := & Dir { 66File :& File { 67Name :filepath .Base (path ), 68Flag :getDirFlag (err ,len (files )), 69 }, 70ItemCount :1 , 71Files :make (fs.Files ,0 ,len (files )), 72 } 73setDirPlatformSpecificAttrs (dir ,path ) 74 75// Buffer channel to prevent deadlock when sending files synchronously 76itemChan := make (chan indexedItem ,len (files )) 77 78for _ ,f := range files { 79if a .IsCancelled () { 80break 81 } 82name := f .Name () 83entryPath := filepath .Join (path ,name ) 84 85if f .IsDir () { 86if a .shouldSkipDir (name ,entryPath ) { 87continue 88 } 89currentIndex := itemCount 90itemCount ++ 91dirCount ++ 92 93go func (entryPath string ,idx int ) { 94concurrencyLimit <- struct {}{} 95if a .IsCancelled () { 96<- concurrencyLimit 97itemChan <- indexedItem {idx ,nil } 98return 99 } 100subdir := a .processDir (entryPath ) 101subdir .Parent = dir 102 103<- concurrencyLimit 104itemChan <- indexedItem {idx ,subdir } 105 }(entryPath ,currentIndex ) 106 }else { 107// Apply file type filter if set 108if a .ignoreFileType != nil && a .ignoreFileType (name ) { 109continue // Skip this file 110 } 111 112info ,err = f .Info () 113if err != nil { 114log .err .Error ()) 115dir .SetFlag ('!' ) 116continue 117 } 118 119if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 120infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 121if err != nil { 122log .err .Error ()) 123dir .SetFlag ('!' ) 124continue 125 } 126if infoF != nil { 127info = infoF 128 } 129 } 130 131// Apply time filter if set 132if a .matchesTimeFilterFn != nil && ! a .matchesTimeFilterFn (info .ModTime ()) { 133continue // Skip this file 134 } 135 136file = & File { 137Name :name , 138Flag :getFlag (info ), 139Size :info .Size (), 140Parent :dir , 141Symlink :readSymlinkTarget (f .Type (),entryPath ), 142 } 143setPlatformSpecificAttrs (file ,info ) 144 145totalSize += file .Usage 146 147// Send file to channel with its index 148itemChan <- indexedItem {itemCount ,file } 149itemCount ++ 150 } 151 } 152 153go func () { 154items := make ([]indexedItem ,itemCount ) 155 156// Collect all items (both files and subdirs) 157for i := 0 ;i < itemCount ;i ++ { 158indexed := <- itemChan 159items [indexed .index ]= indexed 160 } 161 162// Add all items in their original order 163for i := range itemCount { 164if items [i ].item != nil { 165dir .AddFile (items [i ].item ) 166 } 167 } 168 169a .wait .Done () 170 }() 171 172a .progressCurrentItemName .Store (path ) 173a .progressItemCount .Add (int64 (len (files ))) 174a .progressTotalUsage .Add (totalSize ) 175return dir 176}