fork/gdu
git clone https://git.julia.blue/fork/gdu
04c9958
ignore
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 { 110if a .ignoreDir (name ,entryPath ) { 111continue // Skip this file 112 } 113 114// Apply file type filter if set 115if a .ignoreFileType != nil && a .ignoreFileType (name ) { 116continue // Skip this file 117 } 118 119info ,err = f .Info () 120if err != nil { 121log .err .Error ()) 122dir .SetFlag ('!' ) 123continue 124 } 125 126if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 127infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 128if err != nil { 129log .err .Error ()) 130dir .SetFlag ('!' ) 131continue 132 } 133if infoF != nil { 134info = infoF 135 } 136 } 137 138symlinkTarget := readSymlinkTarget (f .Type (),entryPath ) 139 140// Apply time filter if set 141if a .matchesTimeFilterFn != nil && ! a .matchesTimeFilterFn (info .ModTime ()) { 142continue // Skip this file 143 } 144 145switch { 146case a .archiveBrowsing && isZipFile (name ): 147zipDir ,err := processZipFile (entryPath ,info ) 148if err != nil { 149log .Printf ("Failed to process zip file %s: %v" ,entryPath ,err ) 150file = & File { 151Name :name , 152Flag :getFlag (info ), 153Size :info .Size (), 154Parent :dir , 155 } 156 }else { 157uncompressedSize ,compressedSize ,err := getZipFileSize (entryPath ) 158if err == nil { 159zipDir .Size = uncompressedSize 160zipDir .Usage = compressedSize 161 } 162zipDir .Parent = dir 163file = zipDir 164 } 165case a .archiveBrowsing && isTarFile (name ): 166tarDir ,err := processTarFile (entryPath ,info ) 167if err != nil { 168log .Printf ("Failed to process tar file %s: %v" ,entryPath ,err ) 169file = & File { 170Name :name , 171Flag :getFlag (info ), 172Size :info .Size (), 173Parent :dir , 174 } 175 }else { 176tarDir .Parent = dir 177file = tarDir 178 } 179default : 180file = & File { 181Name :name , 182Flag :getFlag (info ), 183Size :info .Size (), 184Parent :dir , 185Symlink :symlinkTarget , 186 } 187 } 188 189if file != nil { 190// Only set platform-specific attributes for regular files 191if regularFile ,ok := file .(* File );ok { 192setPlatformSpecificAttrs (regularFile ,info ) 193 } 194totalUsage += file .GetUsage () 195dir .AddFile (file ) 196 } 197 } 198 } 199 200go func () { 201var sub * Dir 202 203for range dirCount { 204sub = <- subDirChan 205addSubDir (dir ,sub ) 206 } 207 208a .wait .Done () 209 }() 210 211a .progressCurrentItemName .Store (path ) 212a .progressItemCount .Add (int64 (len (files ))) 213a .progressTotalUsage .Add (totalUsage ) 214return dir 215} 216 217func getDirFlag (err error ,items int )rune { 218switch { 219case err != nil : 220return '!' 221case items == 0 : 222return 'e' 223default : 224return ' ' 225 } 226} 227 228func getFlag (f os.FileInfo )rune { 229if f .Mode ()& os .ModeSymlink != 0 || f .Mode ()& os .ModeSocket != 0 { 230return '@' 231 } 232return ' ' 233}