fork/gdu

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

yurenchen000Feat: Show symlink target #260 (#612)1eaa266

subvolume
4.6 KiB229 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			// Apply file type filter if set
111			if a.ignoreFileType != nil && a.ignoreFileType(name) {
112				continue // Skip this file
113			}
114
115			info, err = f.Info()
116			if err != nil {
117				log.Print(err.Error())
118				dir.SetFlag('!')
119				continue
120			}
121
122			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
123				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
124				if err != nil {
125					log.Print(err.Error())
126					dir.SetFlag('!')
127					continue
128				}
129				if infoF != nil {
130					info = infoF
131				}
132			}
133
134			symlinkTarget := readSymlinkTarget(f.Type(), entryPath)
135
136			// Apply time filter if set
137			if a.matchesTimeFilterFn != nil && !a.matchesTimeFilterFn(info.ModTime()) {
138				continue // Skip this file
139			}
140
141			switch {
142			case a.archiveBrowsing && isZipFile(name):
143				zipDir, err := processZipFile(entryPath, info)
144				if err != nil {
145					log.Printf("Failed to process zip file %s: %v", entryPath, err)
146					file = &File{
147						Name:   name,
148						Flag:   getFlag(info),
149						Size:   info.Size(),
150						Parent: dir,
151					}
152				} else {
153					uncompressedSize, compressedSize, err := getZipFileSize(entryPath)
154					if err == nil {
155						zipDir.Size = uncompressedSize
156						zipDir.Usage = compressedSize
157					}
158					zipDir.Parent = dir
159					file = zipDir
160				}
161			case a.archiveBrowsing && isTarFile(name):
162				tarDir, err := processTarFile(entryPath, info)
163				if err != nil {
164					log.Printf("Failed to process tar file %s: %v", entryPath, err)
165					file = &File{
166						Name:   name,
167						Flag:   getFlag(info),
168						Size:   info.Size(),
169						Parent: dir,
170					}
171				} else {
172					tarDir.Parent = dir
173					file = tarDir
174				}
175			default:
176				file = &File{
177					Name:    name,
178					Flag:    getFlag(info),
179					Size:    info.Size(),
180					Parent:  dir,
181					Symlink: symlinkTarget,
182				}
183			}
184
185			if file != nil {
186				// Only set platform-specific attributes for regular files
187				if regularFile, ok := file.(*File); ok {
188					setPlatformSpecificAttrs(regularFile, info)
189				}
190				totalUsage += file.GetUsage()
191				dir.AddFile(file)
192			}
193		}
194	}
195
196	go func() {
197		var sub *Dir
198
199		for range dirCount {
200			sub = <-subDirChan
201			addSubDir(dir, sub)
202		}
203
204		a.wait.Done()
205	}()
206
207	a.progressCurrentItemName.Store(path)
208	a.progressItemCount.Add(int64(len(files)))
209	a.progressTotalUsage.Add(totalUsage)
210	return dir
211}
212
213func getDirFlag(err error, items int) rune {
214	switch {
215	case err != nil:
216		return '!'
217	case items == 0:
218		return 'e'
219	default:
220		return ' '
221	}
222}
223
224func getFlag(f os.FileInfo) rune {
225	if f.Mode()&os.ModeSymlink != 0 || f.Mode()&os.ModeSocket != 0 {
226		return '@'
227	}
228	return ' '
229}