fork/gdu

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

ShivamB25feat: stop scanning and keep partial results (#606)99a7f2a

subvolume
4.0 KiB139 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// GetDone returns channel for checking when analysis is done
88func (a *BaseAnalyzer) GetDone() common.SignalGroup {
89	return 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() {
95	a.cancelled.Store(true)
96}
97
98// IsCancelled reports whether the current scan has been cancelled.
99func (a *BaseAnalyzer) IsCancelled() bool {
100	return a.cancelled.Load()
101}
102
103func (a *BaseAnalyzer) shouldSkipDir(name, path string) bool {
104	return 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() {
110	if a.progressTicker != nil {
111		a.progressTicker.Stop()
112	}
113	a.Init()
114}
115
116func (a *BaseAnalyzer) GetProgress() common.CurrentProgress {
117	return common.CurrentProgress{
118		CurrentItemName: a.progressCurrentItemName.Load().(string),
119		ItemCount:       a.progressItemCount.Load(),
120		TotalUsage:      a.progressTotalUsage.Load(),
121	}
122}
123
124// UpdateProgress updates progress
125func (a *BaseAnalyzer) UpdateProgress() {
126	ticker := a.progressTicker
127	defer ticker.Stop()
128	for {
129		select {
130		case <-a.progressDoneChan:
131			return
132		case <-ticker.C:
133			select {
134			case a.progressOutChan <- a.GetProgress():
135			default:
136			}
137		}
138	}
139}