fork/gdu

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

Julia RyanDan's preferred ignore behavior04c9958

ignore
4.7 KiB233 linesraw
1package analyze
2
3import (
4	"os"
5	"path/filepath"
6	"runtime"
7
8	"github.com/dundee/gdu/v5/internal/common"
9	"github.com/dundee/gdu/v5/pkg/fs"
10	log "github.com/sirupsen/logrus"
11)
12
13var concurrencyLimit = make(chan struct{}, 2*runtime.GOMAXPROCS(0))
14
15var _ common.Analyzer = (*ParallelAnalyzer)(nil)
16
17// ParallelAnalyzer implements Analyzer
18type ParallelAnalyzer struct {
19	BaseAnalyzer
20}
21
22// CreateAnalyzer returns Analyzer
23func CreateAnalyzer() *ParallelAnalyzer {
24	a := &ParallelAnalyzer{}
25	a.Init()
26	return a
27}
28
29// AnalyzeDir analyzes given path
30func (a *ParallelAnalyzer) AnalyzeDir(
31	path string, ignore common.ShouldDirBeIgnored, fileTypeFilter common.ShouldFileBeIgnored,
32) fs.Item {
33	a.ignoreDir = ignore
34	a.ignoreFileType = fileTypeFilter
35
36	go a.UpdateProgress()
37	dir := a.processDir(path)
38
39	dir.BasePath = filepath.Dir(path)
40	a.setCurrentDir(dir)
41	a.wait.Wait()
42
43	a.progressDoneChan <- struct{}{}
44	a.doneChan.Broadcast()
45
46	return dir
47}
48
49func (a *ParallelAnalyzer) processQueuedDir(path string, parent *Dir, result chan<- *Dir) {
50	concurrencyLimit <- struct{}{}
51	if a.IsCancelled() {
52		<-concurrencyLimit
53		result <- nil
54		return
55	}
56
57	subdir := a.processDir(path)
58	subdir.Parent = parent
59	<-concurrencyLimit
60	result <- subdir
61}
62
63func addSubDir(parent, child *Dir) {
64	if child != nil {
65		parent.AddFile(child)
66	}
67}
68
69func (a *ParallelAnalyzer) processDir(path string) *Dir {
70	var (
71		file       fs.Item
72		err        error
73		totalUsage int64
74		info       os.FileInfo
75		subDirChan = make(chan *Dir)
76		dirCount   int
77	)
78
79	a.wait.Add(1)
80
81	files, err := os.ReadDir(path)
82	if err != nil {
83		log.Print(err.Error())
84	}
85
86	dir := &Dir{
87		File: &File{
88			Name: filepath.Base(path),
89			Flag: getDirFlag(err, len(files)),
90		},
91		ItemCount: 1,
92		Files:     make(fs.Files, 0, len(files)),
93	}
94	setDirPlatformSpecificAttrs(dir, path)
95
96	for _, f := range files {
97		if a.IsCancelled() {
98			break
99		}
100		name := f.Name()
101		entryPath := filepath.Join(path, name)
102		if f.IsDir() {
103			if a.shouldSkipDir(name, entryPath) {
104				continue
105			}
106			dirCount++
107
108			go a.processQueuedDir(entryPath, dir, subDirChan)
109		} else {
110			if a.ignoreDir(name, entryPath) {
111				continue // Skip this file
112			}
113
114			// Apply file type filter if set
115			if a.ignoreFileType != nil && a.ignoreFileType(name) {
116				continue // Skip this file
117			}
118
119			info, err = f.Info()
120			if err != nil {
121				log.Print(err.Error())
122				dir.SetFlag('!')
123				continue
124			}
125
126			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
127				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
128				if err != nil {
129					log.Print(err.Error())
130					dir.SetFlag('!')
131					continue
132				}
133				if infoF != nil {
134					info = infoF
135				}
136			}
137
138			symlinkTarget := readSymlinkTarget(f.Type(), entryPath)
139
140			// Apply time filter if set
141			if a.matchesTimeFilterFn != nil && !a.matchesTimeFilterFn(info.ModTime()) {
142				continue // Skip this file
143			}
144
145			switch {
146			case a.archiveBrowsing && isZipFile(name):
147				zipDir, err := processZipFile(entryPath, info)
148				if err != nil {
149					log.Printf("Failed to process zip file %s: %v", entryPath, err)
150					file = &File{
151						Name:   name,
152						Flag:   getFlag(info),
153						Size:   info.Size(),
154						Parent: dir,
155					}
156				} else {
157					uncompressedSize, compressedSize, err := getZipFileSize(entryPath)
158					if err == nil {
159						zipDir.Size = uncompressedSize
160						zipDir.Usage = compressedSize
161					}
162					zipDir.Parent = dir
163					file = zipDir
164				}
165			case a.archiveBrowsing && isTarFile(name):
166				tarDir, err := processTarFile(entryPath, info)
167				if err != nil {
168					log.Printf("Failed to process tar file %s: %v", entryPath, err)
169					file = &File{
170						Name:   name,
171						Flag:   getFlag(info),
172						Size:   info.Size(),
173						Parent: dir,
174					}
175				} else {
176					tarDir.Parent = dir
177					file = tarDir
178				}
179			default:
180				file = &File{
181					Name:    name,
182					Flag:    getFlag(info),
183					Size:    info.Size(),
184					Parent:  dir,
185					Symlink: symlinkTarget,
186				}
187			}
188
189			if file != nil {
190				// Only set platform-specific attributes for regular files
191				if regularFile, ok := file.(*File); ok {
192					setPlatformSpecificAttrs(regularFile, info)
193				}
194				totalUsage += file.GetUsage()
195				dir.AddFile(file)
196			}
197		}
198	}
199
200	go func() {
201		var sub *Dir
202
203		for range dirCount {
204			sub = <-subDirChan
205			addSubDir(dir, sub)
206		}
207
208		a.wait.Done()
209	}()
210
211	a.progressCurrentItemName.Store(path)
212	a.progressItemCount.Add(int64(len(files)))
213	a.progressTotalUsage.Add(totalUsage)
214	return dir
215}
216
217func getDirFlag(err error, items int) rune {
218	switch {
219	case err != nil:
220		return '!'
221	case items == 0:
222		return 'e'
223	default:
224		return ' '
225	}
226}
227
228func getFlag(f os.FileInfo) rune {
229	if f.Mode()&os.ModeSymlink != 0 || f.Mode()&os.ModeSocket != 0 {
230		return '@'
231	}
232	return ' '
233}