fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildefix: StoreDir fixes (#644)35cba00

master
4.8 KiB153 linesraw
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 {
13	progressOutChan         chan common.CurrentProgress
14	progressDoneChan        chan struct{}
15	progressItemCount       atomic.Int64
16	progressTotalUsage      atomic.Int64
17	progressCurrentItemName atomic.Value
18	currentDir              atomic.Value
19	doneChan                common.SignalGroup
20	cancelled               atomic.Bool
21	wait                    *WaitGroup
22	ignoreDir               common.ShouldDirBeIgnored
23	ignoreFileType          common.ShouldFileBeIgnored
24	followSymlinks          bool
25	gitAnnexedSize          bool
26	matchesTimeFilterFn     common.TimeFilter
27	archiveBrowsing         bool
28	progressTicker          *time.Ticker
29}
30
31// Init initializes the BaseAnalyzer
32func (a *BaseAnalyzer) Init() {
33	a.progressOutChan = make(chan common.CurrentProgress, 1)
34	a.progressDoneChan = make(chan struct{})
35	a.doneChan = make(common.SignalGroup)
36	a.wait = (&WaitGroup{}).Init()
37	a.progressItemCount.Store(0)
38	a.progressTotalUsage.Store(0)
39	a.progressCurrentItemName.Store("")
40	a.currentDir.Store((*Dir)(nil))
41	a.cancelled.Store(false)
42	a.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) {
48	a.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 {
55	dir, _ := a.currentDir.Load().(*Dir)
56	if dir == nil {
57		return nil
58	}
59	return snapshotDir(dir, nil)
60}
61
62// SetFollowSymlinks sets whether symlink to files should be followed
63func (a *BaseAnalyzer) SetFollowSymlinks(v bool) {
64	a.followSymlinks = v
65}
66
67// SetShowAnnexedSize sets whether to use annexed size of git-annex files
68func (a *BaseAnalyzer) SetShowAnnexedSize(v bool) {
69	a.gitAnnexedSize = v
70}
71
72// SetTimeFilter sets the time filter function for file inclusion
73func (a *BaseAnalyzer) SetTimeFilter(matchesTimeFilterFn common.TimeFilter) {
74	a.matchesTimeFilterFn = matchesTimeFilterFn
75}
76
77// SetArchiveBrowsing sets whether browsing of zip/jar/tar archives is enabled
78func (a *BaseAnalyzer) SetArchiveBrowsing(v bool) {
79	a.archiveBrowsing = v
80}
81
82// SetFileTypeFilter sets the file type filter function
83func (a *BaseAnalyzer) SetFileTypeFilter(filter common.ShouldFileBeIgnored) {
84	a.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 {
98	return a.ignoreFileType != nil || a.matchesTimeFilterFn != nil
99}
100
101// GetDone returns channel for checking when analysis is done
102func (a *BaseAnalyzer) GetDone() common.SignalGroup {
103	return 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() {
109	a.cancelled.Store(true)
110}
111
112// IsCancelled reports whether the current scan has been cancelled.
113func (a *BaseAnalyzer) IsCancelled() bool {
114	return a.cancelled.Load()
115}
116
117func (a *BaseAnalyzer) shouldSkipDir(name, path string) bool {
118	return 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() {
124	if a.progressTicker != nil {
125		a.progressTicker.Stop()
126	}
127	a.Init()
128}
129
130func (a *BaseAnalyzer) GetProgress() common.CurrentProgress {
131	return common.CurrentProgress{
132		CurrentItemName: a.progressCurrentItemName.Load().(string),
133		ItemCount:       a.progressItemCount.Load(),
134		TotalUsage:      a.progressTotalUsage.Load(),
135	}
136}
137
138// UpdateProgress updates progress
139func (a *BaseAnalyzer) UpdateProgress() {
140	ticker := a.progressTicker
141	defer ticker.Stop()
142	for {
143		select {
144		case <-a.progressDoneChan:
145			return
146		case <-ticker.C:
147			select {
148			case a.progressOutChan <- a.GetProgress():
149			default:
150			}
151		}
152	}
153}