fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
master
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"runtime" 7 8"github.com/dundee/gdu/v5/internal/common" 9"github.com/dundee/gdu/v5/pkg/fs" 10 log"github.com/sirupsen/logrus" 11) 12 13var concurrencyLimit = make (chan struct {},2 * runtime .GOMAXPROCS (0 )) 14 15var _ common.Analyzer = (* ParallelAnalyzer )(nil ) 16 17// ParallelAnalyzer implements Analyzer 18type ParallelAnalyzer struct { 19BaseAnalyzer 20} 21 22// CreateAnalyzer returns Analyzer 23func CreateAnalyzer ()* ParallelAnalyzer { 24a := & ParallelAnalyzer {} 25a .Init () 26return a 27} 28 29// AnalyzeDir analyzes given path 30func (a * ParallelAnalyzer )AnalyzeDir ( 31path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 32) fs.Item { 33a .ignoreDir = ignore 34a .ignoreFileType = fileTypeFilter 35 36go a .UpdateProgress () 37dir := a .processDir (path ) 38 39dir .BasePath = filepath .Dir (path ) 40a .setCurrentDir (dir ) 41a .wait .Wait () 42 43a .progressDoneChan <- struct {}{} 44a .doneChan .Broadcast () 45 46return dir 47} 48 49func (a * ParallelAnalyzer )processQueuedDir (path string ,parent * Dir ,result chan <- * Dir ) { 50concurrencyLimit <- struct {}{} 51if a .IsCancelled () { 52<- concurrencyLimit 53result <- nil 54return 55 } 56 57subdir := a .processDir (path ) 58subdir .Parent = parent 59<- concurrencyLimit 60result <- subdir 61} 62 63func addSubDir (parent ,child * Dir ) { 64if child != nil { 65parent .AddFile (child ) 66 } 67} 68 69func (a * ParallelAnalyzer )processDir (path string )* Dir { 70var ( 71file fs.Item 72err error 73totalUsage int64 74info os.FileInfo 75subDirChan = make (chan * Dir ) 76dirCount int 77 ) 78 79a .wait .Add (1 ) 80 81files ,err := os .ReadDir (path ) 82if err != nil { 83log .err .Error ()) 84 } 85 86dir := & Dir { 87File :& File { 88Name :filepath .Base (path ), 89Flag :getDirFlag (err ,len (files )), 90 }, 91ItemCount :1 , 92Files :make (fs.Files ,0 ,len (files )), 93 } 94setDirPlatformSpecificAttrs (dir ,path ) 95 96for _ ,f := range files { 97if a .IsCancelled () { 98break 99 } 100name := f .Name () 101entryPath := filepath .Join (path ,name ) 102if f .IsDir () { 103if a .shouldSkipDir (name ,entryPath ) { 104continue 105 } 106dirCount ++ 107 108go a .processQueuedDir (entryPath ,dir ,subDirChan ) 109 }else { 110// Apply file type filter if set 111if a .ignoreFileType != nil && a .ignoreFileType (name ) { 112continue // Skip this file 113 } 114 115info ,err = f .Info () 116if err != nil { 117log .err .Error ()) 118dir .SetFlag ('!' ) 119continue 120 } 121 122if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 123infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 124if err != nil { 125log .err .Error ()) 126dir .SetFlag ('!' ) 127continue 128 } 129if infoF != nil { 130info = infoF 131 } 132 } 133 134symlinkTarget := readSymlinkTarget (f .Type (),entryPath ) 135 136// Apply time filter if set 137if a .matchesTimeFilterFn != nil && ! a .matchesTimeFilterFn (info .ModTime ()) { 138continue // Skip this file 139 } 140 141switch { 142case a .archiveBrowsing && isZipFile (name ): 143zipDir ,err := processZipFile (entryPath ,info ) 144if err != nil { 145log .Printf ("Failed to process zip file %s: %v" ,entryPath ,err ) 146file = & File { 147Name :name , 148Flag :getFlag (info ), 149Size :info .Size (), 150Parent :dir , 151 } 152 }else { 153uncompressedSize ,compressedSize ,err := getZipFileSize (entryPath ) 154if err == nil { 155zipDir .Size = uncompressedSize 156zipDir .Usage = compressedSize 157 } 158zipDir .Parent = dir 159file = zipDir 160 } 161case a .archiveBrowsing && isTarFile (name ): 162tarDir ,err := processTarFile (entryPath ,info ) 163if err != nil { 164log .Printf ("Failed to process tar file %s: %v" ,entryPath ,err ) 165file = & File { 166Name :name , 167Flag :getFlag (info ), 168Size :info .Size (), 169Parent :dir , 170 } 171 }else { 172tarDir .Parent = dir 173file = tarDir 174 } 175default : 176file = & File { 177Name :name , 178Flag :getFlag (info ), 179Size :info .Size (), 180Parent :dir , 181Symlink :symlinkTarget , 182 } 183 } 184 185if file != nil { 186// Only set platform-specific attributes for regular files 187if regularFile ,ok := file .(* File );ok { 188setPlatformSpecificAttrs (regularFile ,info ) 189 } 190totalUsage += file .GetUsage () 191dir .AddFile (file ) 192 } 193 } 194 } 195 196go func () { 197var sub * Dir 198 199for range dirCount { 200sub = <- subDirChan 201addSubDir (dir ,sub ) 202 } 203 204a .wait .Done () 205 }() 206 207a .progressCurrentItemName .Store (path ) 208a .progressItemCount .Add (int64 (len (files ))) 209a .progressTotalUsage .Add (totalUsage ) 210return dir 211} 212 213func getDirFlag (err error ,items int )rune { 214switch { 215case err != nil : 216return '!' 217case items == 0 : 218return 'e' 219default : 220return ' ' 221 } 222} 223 224func getFlag (f os.FileInfo )rune { 225if f .Mode ()& os .ModeSymlink != 0 || f .Mode ()& os .ModeSocket != 0 { 226return '@' 227 } 228return ' ' 229}