fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
1package analyze 2 3import ( 4"sync/atomic" 5"time" 6 7"github.com/dundee/gdu/v5/internal/common" 8"github.com/dundee/gdu/v5/pkg/fs" 9) 10 11// BaseAnalyzer provides common logic for all analyzers 12type BaseAnalyzer struct { 13progressOutChan chan common.CurrentProgress 14progressDoneChan chan struct {} 15progressItemCount atomic.Int64 16progressTotalUsage atomic.Int64 17progressCurrentItemName atomic.Value 18currentDir atomic.Value 19doneChan common.SignalGroup 20cancelled atomic.Bool 21wait * WaitGroup 22ignoreDir common.ShouldDirBeIgnored 23ignoreFileType common.ShouldFileBeIgnored 24followSymlinks bool 25gitAnnexedSize bool 26matchesTimeFilterFn common.TimeFilter 27archiveBrowsing bool 28progressTicker * time.Ticker 29} 30 31// Init initializes the BaseAnalyzer 32func (a * BaseAnalyzer )Init () { 33a .progressOutChan = make (chan common.CurrentProgress ,1 ) 34a .progressDoneChan = make (chan struct {}) 35a .doneChan = make (common.SignalGroup ) 36a .wait = (& WaitGroup {}).Init () 37a .progressItemCount .Store (0 ) 38a .progressTotalUsage .Store (0 ) 39a .progressCurrentItemName .Store ("" ) 40a .currentDir .Store ((* Dir )(nil )) 41a .cancelled .Store (false ) 42a .progressTicker = time .NewTicker (50 * time .Millisecond ) 43} 44 45// setCurrentDir stores the root directory currently being analyzed so it can be 46// inspected (e.g. previewed) while the scan is still running. 47func (a * BaseAnalyzer )setCurrentDir (dir * Dir ) { 48a .currentDir .Store (dir ) 49} 50 51// GetCurrentDir returns an independent snapshot of the directory tree built so 52// far, or nil if no analysis has started yet. Callers may aggregate or navigate 53// the snapshot without mutating the live scan state. 54func (a * BaseAnalyzer )GetCurrentDir () fs.Item { 55dir ,_ := a .currentDir .Load ().(* Dir ) 56if dir == nil { 57return nil 58 } 59return snapshotDir (dir ,nil ) 60} 61 62// SetFollowSymlinks sets whether symlink to files should be followed 63func (a * BaseAnalyzer )SetFollowSymlinks (v bool ) { 64a .followSymlinks = v 65} 66 67// SetShowAnnexedSize sets whether to use annexed size of git-annex files 68func (a * BaseAnalyzer )SetShowAnnexedSize (v bool ) { 69a .gitAnnexedSize = v 70} 71 72// SetTimeFilter sets the time filter function for file inclusion 73func (a * BaseAnalyzer )SetTimeFilter (matchesTimeFilterFn common.TimeFilter ) { 74a .matchesTimeFilterFn = matchesTimeFilterFn 75} 76 77// SetArchiveBrowsing sets whether browsing of zip/jar/tar archives is enabled 78func (a * BaseAnalyzer )SetArchiveBrowsing (v bool ) { 79a .archiveBrowsing = v 80} 81 82// SetFileTypeFilter sets the file type filter function 83func (a * BaseAnalyzer )SetFileTypeFilter (filter common.ShouldFileBeIgnored ) { 84a .ignoreFileType = filter 85} 86 87// isFilteringFiles reports whether a file-level filter is active. It mirrors 88// common.UI.IsFilteringFiles, which is what decides between UpdateStats and 89// UpdateStatsWithFileFiltering, so analyzers that finalize stats during the 90// scan can apply the same empty-directory rule (see resolveDirStats). 91// 92// This relies on the convention that a nil filter means "no filtering": 93// common.UI.CreateFileTypeFilter returns nil rather than a predicate that 94// accepts everything, and SetTimeFilter is only called when a time filter was 95// actually requested. Callers must not pass an always-false filter to mean 96// "unfiltered", or a filtered scan's accounting will be applied to it. 97func (a * BaseAnalyzer )isFilteringFiles ()bool { 98return a .ignoreFileType != nil || a .matchesTimeFilterFn != nil 99} 100 101// GetDone returns channel for checking when analysis is done 102func (a * BaseAnalyzer )GetDone () common.SignalGroup { 103return a .doneChan 104} 105 106// Cancel stops scheduling new scan work. In-flight filesystem operations are 107// allowed to finish so the caller can safely use the partial directory tree. 108func (a * BaseAnalyzer )Cancel () { 109a .cancelled .Store (true ) 110} 111 112// IsCancelled reports whether the current scan has been cancelled. 113func (a * BaseAnalyzer )IsCancelled ()bool { 114return a .cancelled .Load () 115} 116 117func (a * BaseAnalyzer )shouldSkipDir (name ,path string )bool { 118return a .ignoreDir (name ,path )|| a .IsCancelled () 119} 120 121// ResetProgress prepares the analyzer for a new scan. Call it only after the 122// previous scan has completed and before exposing the new scan to cancellation. 123func (a * BaseAnalyzer )ResetProgress () { 124if a .progressTicker != nil { 125a .progressTicker .Stop () 126 } 127a .Init () 128} 129 130func (a * BaseAnalyzer )GetProgress () common.CurrentProgress { 131return common.CurrentProgress { 132CurrentItemName :a .progressCurrentItemName .Load ().(string ), 133ItemCount :a .progressItemCount .Load (), 134TotalUsage :a .progressTotalUsage .Load (), 135 } 136} 137 138// UpdateProgress updates progress 139func (a * BaseAnalyzer )UpdateProgress () { 140ticker := a .progressTicker 141defer ticker .Stop () 142for { 143select { 144case <- a .progressDoneChan : 145return 146case <- ticker .C : 147select { 148case a .progressOutChan <- a .GetProgress (): 149default : 150 } 151 } 152 } 153}