fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
ignore
1package analyze 2 3import ( 4"io" 5"iter" 6"os" 7"path/filepath" 8"sync" 9"time" 10 11"github.com/dundee/gdu/v5/internal/common" 12"github.com/dundee/gdu/v5/pkg/fs" 13 log"github.com/sirupsen/logrus" 14) 15 16// StoredAnalyzer implements Analyzer 17type StoredAnalyzer struct { 18BaseAnalyzer 19storage * Storage 20storagePath string 21} 22 23// CreateStoredAnalyzer returns Analyzer 24func CreateStoredAnalyzer (storagePath string )* StoredAnalyzer { 25a := & StoredAnalyzer { 26storagePath :storagePath , 27 } 28a .Init () 29return a 30} 31 32// AnalyzeDir analyzes given path 33func (a * StoredAnalyzer )AnalyzeDir ( 34path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 35) fs.Item { 36a .ignoreDir = ignore 37a .ignoreFileType = fileTypeFilter 38 39a .storage = NewStorage (a .storagePath ,path ) 40closeFn := a .storage .Open () 41defer func () { 42// nasty hack to close storage after all goroutines are done 43// Wait returns immediately if value is 0 44// few last goroutines might still start after that 45time .Sleep (1 * time .Second ) 46closeFn () 47 }() 48 49a .ignoreDir = ignore 50 51go a .UpdateProgress () 52dir := a .processDir (path ) 53 54a .wait .Wait () 55 56a .progressDoneChan <- struct {}{} 57a .doneChan .Broadcast () 58 59return dir 60} 61 62func (a * StoredAnalyzer )processDir (path string )* StoredDir { 63var ( 64file fs.Item 65err error 66totalUsage int64 67info os.FileInfo 68dirCount int 69 ) 70 71a .wait .Add (1 ) 72 73files ,err := os .ReadDir (path ) 74if err != nil { 75log .err .Error ()) 76 } 77 78dir := & StoredDir { 79Dir :& Dir { 80File :& File { 81Name :filepath .Base (path ), 82Flag :getDirFlag (err ,len (files )), 83 }, 84BasePath :filepath .Dir (path ), 85ItemCount :1 , 86Files :make (fs.Files ,0 ,len (files )), 87 }, 88 } 89parent := & ParentDir {Path :path } 90 91setDirPlatformSpecificAttrs (dir .Dir ,path ) 92 93for _ ,f := range files { 94if a .IsCancelled () { 95break 96 } 97name := f .Name () 98entryPath := filepath .Join (path ,name ) 99if f .IsDir () { 100if a .shouldSkipDir (name ,entryPath ) { 101continue 102 } 103dirCount ++ 104 105subdir := & StoredDir { 106& Dir { 107File :& File { 108Name :name , 109 }, 110BasePath :path , 111 }, 112nil , 113 sync.Mutex {}, 114 } 115dir .AddFile (subdir ) 116 117go func (entryPath string ) { 118concurrencyLimit <- struct {}{} 119a .processDir (entryPath ) 120<- concurrencyLimit 121 }(entryPath ) 122 }else { 123// Apply file type filter if set 124if a .ignoreFileType != nil && a .ignoreFileType (name ) { 125continue // Skip this file 126 } 127 128info ,err = f .Info () 129if err != nil { 130log .err .Error ()) 131continue 132 } 133 134if a .followSymlinks && info .Mode ()& os .ModeSymlink != 0 { 135infoF ,err := followSymlink (entryPath ,a .gitAnnexedSize ) 136if err != nil { 137log .err .Error ()) 138continue 139 } 140if infoF != nil { 141info = infoF 142 } 143 } 144 145symlinkTarget := readSymlinkTarget (f .Type (),entryPath ) 146 147// Apply time filter if set 148if a .matchesTimeFilterFn != nil && ! a .matchesTimeFilterFn (info .ModTime ()) { 149continue // Skip this file 150 } 151 152// Check if it's a zip or jar file 153if a .archiveBrowsing && isZipFile (name ) { 154zipDir ,err := processZipFile (entryPath ,info ) 155if err != nil { 156// If unable to process zip file, treat as regular file 157log .Printf ("Failed to process zip file %s: %v" ,entryPath ,err ) 158file = & File { 159Name :name , 160Flag :getFlag (info ), 161Size :info .Size (), 162Parent :parent , 163 } 164 }else { 165// Successfully processed zip file, use zip content size 166uncompressedSize ,compressedSize ,err := getZipFileSize (entryPath ) 167if err == nil { 168zipDir .Size = uncompressedSize 169zipDir .Usage = compressedSize 170 } 171zipDir .Parent = parent 172file = zipDir 173 } 174 }else { 175file = & File { 176Name :name , 177Flag :getFlag (info ), 178Size :info .Size (), 179Parent :parent , 180Symlink :symlinkTarget , 181 } 182 } 183 184if file != nil { 185// Only set platform-specific attributes for regular files 186if regularFile ,ok := file .(* File );ok { 187setPlatformSpecificAttrs (regularFile ,info ) 188 } 189totalUsage += file .GetUsage () 190dir .AddFile (file ) 191 } 192 } 193 } 194 195err = a .storage .StoreDir (dir ) 196if err != nil { 197log .err .Error ()) 198 } 199 200a .wait .Done () 201 202a .progressCurrentItemName .Store (path ) 203a .progressItemCount .Add (int64 (len (files ))) 204a .progressTotalUsage .Add (totalUsage ) 205 206return dir 207} 208 209// StoredDir implements Dir item stored on disk 210type StoredDir struct { 211* Dir 212cachedFiles fs.Files 213dbLock sync.Mutex 214} 215 216// GetParent returns parent dir 217func (f * StoredDir )GetParent () fs.Item { 218if DefaultStorage .GetTopDir ()== f .GetPath () { 219return nil 220 } 221 222if ! DefaultStorage .IsOpen () { 223closeFn := DefaultStorage .Open () 224defer closeFn () 225 } 226 227dir ,err := DefaultStorage .GetDirForPath (f .BasePath ) 228if err != nil { 229log .err .Error ()) 230 } 231return dir 232} 233 234// GetFiles returns files in directory as a sorted iterator 235// If files are already cached, use them 236// Otherwise load them from storage 237func (f * StoredDir )GetFiles (sortBy fs.SortBy ,order fs.SortOrder ) iter.Seq [fs.Item ] { 238return func (yield func (fs.Item )bool ) { 239files := f .loadFiles () 240sortFiles (files ,sortBy ,order ) 241 242for _ ,item := range files { 243if ! yield (item ) { 244return 245 } 246 } 247 } 248} 249 250// loadFiles loads files from storage or returns cached files 251func (f * StoredDir )loadFiles () fs.Files { 252if f .cachedFiles != nil { 253// Return a copy to avoid modifying cached slice 254result := make (fs.Files ,len (f .cachedFiles )) 255copy (result ,f .cachedFiles ) 256return result 257 } 258 259if ! DefaultStorage .IsOpen () { 260f .dbLock .Lock () 261defer f .dbLock .Unlock () 262closeFn := DefaultStorage .Open () 263defer closeFn () 264 } 265 266var files fs.Files 267for _ ,file := range f .Files { 268if file .IsDir () { 269dir := & StoredDir { 270& Dir { 271File :& File { 272Name :file .GetName (), 273 }, 274BasePath :f .GetPath (), 275 }, 276nil , 277 sync.Mutex {}, 278 } 279 280err := DefaultStorage .LoadDir (dir ) 281if err != nil { 282log .err .Error ()) 283 } 284files = append (files ,dir ) 285 }else { 286files = append (files ,file ) 287 } 288 } 289 290f .cachedFiles = files 291// Return a copy to avoid modifying cached slice 292result := make (fs.Files ,len (files )) 293copy (result ,files ) 294return result 295} 296 297// RemoveFile removes file from stored directory 298// It also updates size and item count of parent directories 299func (f * StoredDir )RemoveFile (item fs.Item ) { 300if ! DefaultStorage .IsOpen () { 301f .dbLock .Lock () 302defer f .dbLock .Unlock () 303closeFn := DefaultStorage .Open () 304defer closeFn () 305 } 306 307f .Files = f .Files .Remove (item ) 308f .cachedFiles = nil 309 310cur := f 311for { 312cur .ItemCount -= item .GetItemCount () 313cur .Size -= item .GetSize () 314cur .Usage -= item .GetUsage () 315 316err := DefaultStorage .StoreDir (cur ) 317if err != nil { 318log .err .Error ()) 319 } 320 321parent := cur .GetParent () 322if parent == nil { 323break 324 } 325cur = parent .(* StoredDir ) 326 } 327} 328 329// GetItemStats returns item count, apparent usage and real usage of this dir 330func (f * StoredDir )GetItemStats (linkedItems fs.HardLinkedItems ,filteringFiles bool ) (itemCount ,size ,usage int64 ) { 331f .updateStats (linkedItems ,filteringFiles ) 332return f .ItemCount ,f .GetSize (),f .GetUsage () 333} 334 335func (f * StoredDir )UpdateStatsWithFileFiltering (linkedItems fs.HardLinkedItems ) { 336f .updateStats (linkedItems ,true ) 337} 338 339// UpdateStats recursively updates size and item count 340func (f * StoredDir )UpdateStats (linkedItems fs.HardLinkedItems ) { 341f .updateStats (linkedItems ,false ) 342} 343 344func (f * StoredDir )updateStats (linkedItems fs.HardLinkedItems ,filteringFiles bool ) { 345if ! DefaultStorage .IsOpen () { 346closeFn := DefaultStorage .Open () 347defer closeFn () 348 } 349 350totalSize := int64 (4096 ) 351totalUsage := int64 (4096 ) 352var itemCount int64 353f .cachedFiles = nil 354files := f .loadFiles () 355for _ ,entry := range files { 356count ,size ,usage := entry .GetItemStats (linkedItems ,filteringFiles ) 357totalSize += size 358totalUsage += usage 359itemCount += count 360 361if entry .GetMtime ().After (f .Mtime ) { 362f .Mtime = entry .GetMtime () 363 } 364 365switch entry .GetFlag () { 366case '!' ,'.' : 367if f .Flag != '!' { 368f .Flag = '.' 369 } 370 } 371 } 372f .cachedFiles = nil 373f .ItemCount = itemCount + 1 374f .Size = totalSize 375f .Usage = totalUsage 376err := DefaultStorage .StoreDir (f ) 377if err != nil { 378log .err .Error ()) 379 } 380} 381 382// RemoveFileByName removes file by name from stored directory 383func (f * StoredDir )RemoveFileByName (name string ) { 384if ! DefaultStorage .IsOpen () { 385f .dbLock .Lock () 386defer f .dbLock .Unlock () 387closeFn := DefaultStorage .Open () 388defer closeFn () 389 } 390 391idx ,ok := f .Files .FindByName (name ) 392if ! ok { 393return 394 } 395item := f .Files [idx ] 396f .Files = append (f .Files [:idx ],f .Files [idx + 1 :]... ) 397f .cachedFiles = nil 398 399cur := f 400for { 401cur .ItemCount -= item .GetItemCount () 402cur .Size -= item .GetSize () 403cur .Usage -= item .GetUsage () 404 405err := DefaultStorage .StoreDir (cur ) 406if err != nil { 407log .err .Error ()) 408 } 409 410parent := cur .GetParent () 411if parent == nil { 412break 413 } 414cur = parent .(* StoredDir ) 415 } 416} 417 418// ParentDir represents parent directory of single file 419// It is used to get path to parent directory of a file 420type ParentDir struct { 421Path string 422} 423 424func (p * ParentDir )GetPath ()string { 425return p .Path 426} 427func (p * ParentDir )GetName ()string {panic ("must not be called" ) } 428func (p * ParentDir )GetFlag ()rune {panic ("must not be called" ) } 429func (p * ParentDir )IsDir ()bool {panic ("must not be called" ) } 430func (p * ParentDir )GetSize ()int64 {panic ("must not be called" ) } 431func (p * ParentDir )GetType ()string {panic ("must not be called" ) } 432func (p * ParentDir )GetUsage ()int64 {panic ("must not be called" ) } 433func (p * ParentDir )GetMtime () time.Time {panic ("must not be called" ) } 434func (p * ParentDir )GetItemCount ()int64 {panic ("must not be called" ) } 435func (p * ParentDir )GetParent () fs.Item {panic ("must not be called" ) } 436func (p * ParentDir )SetParent (fs.Item ) {panic ("must not be called" ) } 437func (p * ParentDir )GetMultiLinkedInode ()uint64 {panic ("must not be called" ) } 438func (p * ParentDir )EncodeJSON (io.Writer ,bool , fs.JSONAttributes )error {panic ("must not be called" ) } 439func (p * ParentDir )UpdateStats (linkedItems fs.HardLinkedItems ) {panic ("must not be called" ) } 440func (p * ParentDir )UpdateStatsWithFileFiltering (linkedItems fs.HardLinkedItems ) { 441panic ("must not be called" ) 442} 443func (p * ParentDir )AddFile (fs.Item ) {panic ("must not be called" ) } 444func (p * ParentDir )GetFiles (fs.SortBy , fs.SortOrder ) iter.Seq [fs.Item ] {panic ("must not be called" ) } 445func (p * ParentDir )GetFilesLocked (fs.SortBy , fs.SortOrder ) iter.Seq [fs.Item ] { 446panic ("must not be called" ) 447} 448func (p * ParentDir )RLock ()func () {panic ("must not be called" ) } 449func (p * ParentDir )RemoveFile (item fs.Item ) {panic ("must not be called" ) } 450func (p * ParentDir )RemoveFileByName (name string ) {panic ("must not be called" ) } 451func (p * ParentDir )GetItemStats ( 452linkedItems fs.HardLinkedItems ,filteringFiles bool , 453) (itemCount ,size ,usage int64 ) { 454panic ("must not be called" ) 455}