fork/gdu
git clone https://git.julia.blue/fork/gdu
1eaa266
subvolume
1package analyze 2 3import ( 4"iter" 5"os" 6"path/filepath" 7"sort" 8"sync" 9"time" 10 11"github.com/dundee/gdu/v5/pkg/fs" 12) 13 14// fileFlagMu protects the mutable Flag field on published File values. File 15// keeps its fields public for compatibility, so hard-link accounting can 16// update Flag after a file has been exposed to preview readers. 17var fileFlagMu sync.RWMutex 18 19var _ fs.SymlinkItem = (* File )(nil ) 20 21// File struct 22type File struct { 23Mtime time.Time 24Parent fs.Item 25Name string 26Symlink string 27Size int64 28Usage int64 29Mli uint64 30Flag rune 31} 32 33// GetName returns name of dir 34func (f * File )GetName ()string { 35return f .Name 36} 37 38// IsDir returns false for file 39func (f * File )IsDir ()bool { 40return false 41} 42 43// GetParent returns parent dir 44func (f * File )GetParent () fs.Item { 45return f .Parent 46} 47 48// SetParent sets parent dir 49func (f * File )SetParent (parent fs.Item ) { 50f .Parent = parent 51} 52 53// GetPath returns absolute Get of the file 54func (f * File )GetPath ()string { 55return filepath .Join (f .Parent .GetPath (),f .Name ) 56} 57 58// GetFlag returns flag of the file 59func (f * File )GetFlag ()rune { 60fileFlagMu .RLock () 61defer fileFlagMu .RUnlock () 62return f .Flag 63} 64 65// GetSize returns size of the file 66func (f * File )GetSize ()int64 { 67return f .Size 68} 69 70// GetUsage returns usage of the file 71func (f * File )GetUsage ()int64 { 72return f .Usage 73} 74 75// GetMtime returns mtime of the file 76func (f * File )GetMtime () time.Time { 77return f .Mtime 78} 79 80// GetType returns name type of item 81func (f * File )GetType ()string { 82fileFlagMu .RLock () 83defer fileFlagMu .RUnlock () 84if f .Flag == '@' { 85if f .Symlink != "" { 86return "Symlink" 87 } 88return "Other" 89 } 90return "File" 91} 92 93// GetSymlinkTarget returns the target of a symlink, or empty string 94func (f * File )GetSymlinkTarget ()string { 95return f .Symlink 96} 97 98// GetItemCount returns 1 for file 99func (f * File )GetItemCount ()int64 { 100return 1 101} 102 103// GetMultiLinkedInode returns inode number of multilinked file 104func (f * File )GetMultiLinkedInode ()uint64 { 105return f .Mli 106} 107 108func (f * File )alreadyCounted (linkedItems fs.HardLinkedItems )bool { 109mli := f .Mli 110counted := false 111if mli > 0 { 112fileFlagMu .Lock () 113f .Flag = 'H' 114fileFlagMu .Unlock () 115if _ ,ok := linkedItems [mli ];ok { 116counted = true 117 } 118linkedItems [mli ]= append (linkedItems [mli ],f ) 119 } 120return counted 121} 122 123// CreateFileItem creates a File from an os.FileInfo with correct platform-specific attributes 124func CreateFileItem (name string ,info os.FileInfo )* File { 125file := & File { 126Name :name , 127Size :info .Size (), 128Flag :getFlag (info ), 129 } 130setPlatformSpecificAttrs (file ,info ) 131return file 132} 133 134// GetItemStats returns 1 as count of items, apparent usage and real usage of this file 135func (f * File )GetItemStats (linkedItems fs.HardLinkedItems ,filteringFiles bool ) (itemCount ,size ,usage int64 ) { 136if f .alreadyCounted (linkedItems ) { 137return 1 ,0 ,0 138 } 139return 1 ,f .GetSize (),f .GetUsage () 140} 141 142// UpdateStats does nothing on file 143func (f * File )UpdateStats (linkedItems fs.HardLinkedItems ) {} 144func (f * File )UpdateStatsWithFileFiltering (linkedItems fs.HardLinkedItems ) {} 145 146// GetFiles returns all files in directory 147func (f * File )GetFiles (sortBy fs.SortBy ,order fs.SortOrder ) iter.Seq [fs.Item ] { 148return func (yield func (fs.Item )bool ) {} 149} 150 151// GetFilesLocked returns all files in directory 152func (f * File )GetFilesLocked (sortBy fs.SortBy ,order fs.SortOrder ) iter.Seq [fs.Item ] { 153return f .GetFiles (sortBy ,order ) 154} 155 156// RLock panics on file 157func (f * File )RLock ()func () { 158panic ("RLock should not be called on file" ) 159} 160 161// AddFile panics on file 162func (f * File )AddFile (item fs.Item ) { 163panic ("AddFile should not be called on file" ) 164} 165 166// RemoveFile panics on file 167func (f * File )RemoveFile (item fs.Item ) { 168panic ("RemoveFile should not be called on file" ) 169} 170 171// RemoveFileByName panics on file 172func (f * File )RemoveFileByName (name string ) { 173panic ("RemoveFileByName should not be called on file" ) 174} 175 176// EmptyDirSize is the apparent size gdu assigns to a directory that has no 177// counted children. It is a fixed sentinel (not the real filesystem block 178// size) written by updateStats, SimpleDir.UpdateStats and the parallel top-dir 179// analyzer, and recognised again on import. Keep every producer and consumer 180// of this value referencing this constant so they can never drift apart. 181const EmptyDirSize int64 = 512 182 183// Dir struct 184type Dir struct { 185* File 186BasePath string 187Files fs.Files 188ItemCount int64 189statsFromJSON bool 190m sync.RWMutex 191} 192 193func snapshotDir (source * Dir ,parent fs.Item )* Dir { 194source .m .RLock () 195snapshot := & Dir { 196File :& File { 197Mtime :source .Mtime , 198Parent :parent , 199Name :source .Name , 200Size :source .Size , 201Usage :source .Usage , 202Mli :source .Mli , 203Flag :source .Flag , 204 }, 205BasePath :source .BasePath , 206Files :make (fs.Files ,0 ,len (source .Files )), 207ItemCount :source .ItemCount , 208 } 209children := append (fs .Files (nil ),source .Files ... ) 210source .m .RUnlock () 211 212for _ ,child := range children { 213snapshot .Files = append (snapshot .Files ,snapshotItem (child ,snapshot )) 214 } 215return snapshot 216} 217 218func snapshotItem (source ,parent fs.Item ) fs.Item { 219switch item := source .(type ) { 220case * ZipDir : 221snapshot := & ZipDir {Dir :snapshotDir (item .Dir ,parent ),zipPath :item .zipPath } 222reparentSnapshotChildren (snapshot .Dir ,snapshot ) 223return snapshot 224case * TarDir : 225snapshot := & TarDir {Dir :snapshotDir (item .Dir ,parent ),tarPath :item .tarPath } 226reparentSnapshotChildren (snapshot .Dir ,snapshot ) 227return snapshot 228case * ZipFile : 229return & ZipFile { 230File :snapshotFile (item .File ,parent ), 231zipPath :item .zipPath , 232inZipPath :item .inZipPath , 233 } 234case * TarFile : 235return & TarFile { 236File :snapshotFile (item .File ,parent ), 237tarPath :item .tarPath , 238inTarPath :item .inTarPath , 239 } 240case * Dir : 241return snapshotDir (item ,parent ) 242case * File : 243return snapshotFile (item ,parent ) 244 } 245 246if source .IsDir () { 247snapshot := & Dir { 248File :snapshotFile (source ,parent ), 249Files :make (fs.Files ,0 ), 250ItemCount :source .GetItemCount (), 251 } 252for child := range source .GetFilesLocked (fs .SortByName ,fs .SortAsc ) { 253snapshot .Files = append (snapshot .Files ,snapshotItem (child ,snapshot )) 254 } 255return snapshot 256 } 257return snapshotFile (source ,parent ) 258} 259 260func reparentSnapshotChildren (dir * Dir ,parent fs.Item ) { 261for _ ,child := range dir .Files { 262child .SetParent (parent ) 263 } 264} 265 266func snapshotFile (source ,parent fs.Item )* File { 267return & File { 268Mtime :source .GetMtime (), 269Parent :parent , 270Name :source .GetName (), 271Size :source .GetSize (), 272Usage :source .GetUsage (), 273Mli :source .GetMultiLinkedInode (), 274Flag :source .GetFlag (), 275 } 276} 277 278// AddFile add item to files 279func (f * Dir )AddFile (item fs.Item ) { 280f .m .Lock () 281defer f .m .Unlock () 282f .Files = append (f .Files ,item ) 283} 284 285// SetFlag updates the directory flag while preserving snapshot consistency. 286func (f * Dir )SetFlag (flag rune ) { 287f .m .Lock () 288f .Flag = flag 289f .m .Unlock () 290} 291 292// SetStatsFromJSON marks directory statistics decoded from an export as authoritative. 293func (f * Dir )SetStatsFromJSON () { 294f .statsFromJSON = true 295} 296 297// HasEmptyDirPlaceholderStats reports whether this directory's stats are 298// indistinguishable from the placeholder updateStats produces for a directory 299// with no counted children (see EmptyDirSize). Such stats carry no information 300// worth pinning from an import: recomputation reproduces exactly the same 301// values, so callers should let updateStats run rather than preserving them. 302// 303// The items == 1 check is load-bearing: a depth-truncated directory that held 304// real content always exports with ItemCount >= 2, so it can never be mistaken 305// for a placeholder and its stats are always preserved. 306func (f * Dir )HasEmptyDirPlaceholderStats ()bool { 307return f .Size == EmptyDirSize && f .Usage == 0 && f .ItemCount == 1 308} 309 310// GetFiles returns all files in directory as a sorted iterator 311func (f * Dir )GetFiles (sortBy fs.SortBy ,order fs.SortOrder ) iter.Seq [fs.Item ] { 312return func (yield func (fs.Item )bool ) { 313// Make a copy to avoid modifying the original slice 314sorted := make (fs.Files ,len (f .Files )) 315copy (sorted ,f .Files ) 316sortFiles (sorted ,sortBy ,order ) 317 318for _ ,item := range sorted { 319if ! yield (item ) { 320return 321 } 322 } 323 } 324} 325 326// GetFilesLocked returns all files in directory as a sorted iterator 327// It is safe to call this function from multiple goroutines 328func (f * Dir )GetFilesLocked (sortBy fs.SortBy ,order fs.SortOrder ) iter.Seq [fs.Item ] { 329return func (yield func (fs.Item )bool ) { 330f .m .RLock () 331defer f .m .RUnlock () 332 333// Make a copy to avoid modifying the original slice 334sorted := make (fs.Files ,len (f .Files )) 335copy (sorted ,f .Files ) 336sortFiles (sorted ,sortBy ,order ) 337 338for _ ,item := range sorted { 339if ! yield (item ) { 340return 341 } 342 } 343 } 344} 345 346// GetType returns name type of item 347func (f * Dir )GetType ()string { 348return "Directory" 349} 350 351// GetFlag returns the current directory flag. 352func (f * Dir )GetFlag ()rune { 353f .m .RLock () 354defer f .m .RUnlock () 355return f .Flag 356} 357 358// GetSize returns the current apparent size. 359func (f * Dir )GetSize ()int64 { 360f .m .RLock () 361defer f .m .RUnlock () 362return f .Size 363} 364 365// GetUsage returns the current disk usage. 366func (f * Dir )GetUsage ()int64 { 367f .m .RLock () 368defer f .m .RUnlock () 369return f .Usage 370} 371 372// GetMtime returns the current modification time. 373func (f * Dir )GetMtime () time.Time { 374f .m .RLock () 375defer f .m .RUnlock () 376return f .Mtime 377} 378 379// GetItemCount returns number of files in dir 380func (f * Dir )GetItemCount ()int64 { 381f .m .RLock () 382defer f .m .RUnlock () 383return f .ItemCount 384} 385 386// IsDir returns true for dir 387func (f * Dir )IsDir ()bool { 388return true 389} 390 391// GetPath returns absolute path of the file 392func (f * Dir )GetPath ()string { 393if f .BasePath != "" { 394return filepath .Join (f .BasePath ,f .Name ) 395 } 396if f .Parent != nil { 397return filepath .Join (f .Parent .GetPath (),f .Name ) 398 } 399return f .Name 400} 401 402// GetItemStats returns item count, apparent usage and real usage of this dir 403func (f * Dir )GetItemStats (linkedItems fs.HardLinkedItems ,filteringFiles bool ) (itemCount ,size ,usage int64 ) { 404f .updateStats (linkedItems ,filteringFiles ) 405return f .GetItemCount (),f .GetSize (),f .GetUsage () 406} 407 408func (f * Dir )UpdateStats (linkedItems fs.HardLinkedItems ) { 409f .updateStats (linkedItems ,false ) 410} 411 412func (f * Dir )UpdateStatsWithFileFiltering (linkedItems fs.HardLinkedItems ) { 413f .updateStats (linkedItems ,true ) 414} 415 416// UpdateStats recursively updates size and item count 417func (f * Dir )updateStats (linkedItems fs.HardLinkedItems ,filteringFiles bool ) { 418if f .statsFromJSON { 419return 420 } 421 422// Snapshot the file list under the read lock so it is safe to compute stats 423// even while the analyzer is still appending items in another goroutine 424// (e.g. when previewing a directory mid-scan). 425f .m .RLock () 426files := make (fs.Files ,len (f .Files )) 427copy (files ,f .Files ) 428mtime := f .Mtime 429flag := f .Flag 430f .m .RUnlock () 431 432totalSize := int64 (0 ) 433totalUsage := int64 (0 ) 434var itemCount int64 = 1 435var hasFiles bool 436for _ ,entry := range files { 437count ,size ,usage := entry .GetItemStats (linkedItems ,filteringFiles ) 438totalSize += size 439totalUsage += usage 440itemCount += count 441 442entryMtime := entry .GetMtime () 443if entryMtime .After (mtime ) { 444mtime = entryMtime 445 } 446 447if ! entry .IsDir () { 448hasFiles = true 449 } 450 451switch entry .GetFlag () { 452case '!' ,'.' : 453if flag != '!' { 454flag = '.' 455 } 456 } 457 } 458 459f .m .Lock () 460defer f .m .Unlock () 461f .Mtime = mtime 462f .Flag = flag 463 464// no files, or just empty dirs 465if len (files )== 0 || (! hasFiles && filteringFiles && itemCount == int64 (len (files )+ 1 )) { 466f .ItemCount = 1 467f .Size = totalSize + EmptyDirSize 468f .Usage = 0 469 }else { 470f .ItemCount = itemCount 471f .Size = totalSize 472f .Usage = totalUsage 473 } 474} 475 476// RemoveFile removes item from dir, updates size and item count 477func (f * Dir )RemoveFile (item fs.Item ) { 478f .m .Lock () 479defer f .m .Unlock () 480 481f .Files = f .Files .Remove (item ) 482 483cur := f 484for { 485cur .ItemCount -= item .GetItemCount () 486cur .Size -= item .GetSize () 487cur .Usage -= item .GetUsage () 488 489if cur .Parent == nil { 490break 491 } 492cur = cur .Parent .(* Dir ) 493 } 494} 495 496// sortFiles sorts files in place according to sortBy and order 497func sortFiles (files fs.Files ,sortBy fs.SortBy ,order fs.SortOrder ) { 498var sorter sort.Interface 499switch sortBy { 500case fs .SortByName : 501sorter = fs .ByName (files ) 502case fs .SortByItemCount : 503sorter = fs .ByItemCount (files ) 504case fs .SortByMtime : 505sorter = fs .ByMtime (files ) 506case fs .SortByApparentSize : 507sorter = fs .ByApparentSize (files ) 508case fs .SortBySize : 509sorter = files 510 } 511 512if order == fs .SortDesc { 513sort .Sort (sort .Reverse (sorter )) 514 }else { 515sort .Sort (sorter ) 516 } 517} 518 519// RLock read locks dir 520func (f * Dir )RLock ()func () { 521f .m .RLock () 522return f .m .RUnlock 523} 524 525// RemoveFileByName removes item by name from dir 526func (f * Dir )RemoveFileByName (name string ) { 527f .m .Lock () 528defer f .m .Unlock () 529 530idx ,ok := f .Files .FindByName (name ) 531if ! ok { 532return 533 } 534item := f .Files [idx ] 535f .Files = append (f .Files [:idx ],f .Files [idx + 1 :]... ) 536 537cur := f 538for { 539cur .ItemCount -= item .GetItemCount () 540cur .Size -= item .GetSize () 541cur .Usage -= item .GetUsage () 542 543if cur .Parent == nil { 544break 545 } 546cur = cur .Parent .(* Dir ) 547 } 548}