fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
subvolume
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// GetDone returns channel for checking when analysis is done 88func (a * BaseAnalyzer )GetDone () common.SignalGroup { 89return a .doneChan 90} 91 92// Cancel stops scheduling new scan work. In-flight filesystem operations are 93// allowed to finish so the caller can safely use the partial directory tree. 94func (a * BaseAnalyzer )Cancel () { 95a .cancelled .Store (true ) 96} 97 98// IsCancelled reports whether the current scan has been cancelled. 99func (a * BaseAnalyzer )IsCancelled ()bool { 100return a .cancelled .Load () 101} 102 103func (a * BaseAnalyzer )shouldSkipDir (name ,path string )bool { 104return a .ignoreDir (name ,path )|| a .IsCancelled () 105} 106 107// ResetProgress prepares the analyzer for a new scan. Call it only after the 108// previous scan has completed and before exposing the new scan to cancellation. 109func (a * BaseAnalyzer )ResetProgress () { 110if a .progressTicker != nil { 111a .progressTicker .Stop () 112 } 113a .Init () 114} 115 116func (a * BaseAnalyzer )GetProgress () common.CurrentProgress { 117return common.CurrentProgress { 118CurrentItemName :a .progressCurrentItemName .Load ().(string ), 119ItemCount :a .progressItemCount .Load (), 120TotalUsage :a .progressTotalUsage .Load (), 121 } 122} 123 124// UpdateProgress updates progress 125func (a * BaseAnalyzer )UpdateProgress () { 126ticker := a .progressTicker 127defer ticker .Stop () 128for { 129select { 130case <- a .progressDoneChan : 131return 132case <- ticker .C : 133select { 134case a .progressOutChan <- a .GetProgress (): 135default : 136 } 137 } 138 } 139}