fork/gdu

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

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

ignore
3.6 KiB176 linesraw
1package analyze
2
3import (
4	"os"
5	"path/filepath"
6
7	"github.com/dundee/gdu/v5/internal/common"
8	"github.com/dundee/gdu/v5/pkg/fs"
9	log "github.com/sirupsen/logrus"
10)
11
12// ParallelStableOrderAnalyzer implements Analyzer
13type ParallelStableOrderAnalyzer struct {
14	BaseAnalyzer
15}
16
17// CreateStableOrderAnalyzer returns parallel Analyzer which keeps stable order of files
18func CreateStableOrderAnalyzer() *ParallelStableOrderAnalyzer {
19	a := &ParallelStableOrderAnalyzer{}
20	a.Init()
21	return a
22}
23
24// AnalyzeDir analyzes given path
25func (a *ParallelStableOrderAnalyzer) AnalyzeDir(
26	path string, ignore common.ShouldDirBeIgnored, fileTypeFilter common.ShouldFileBeIgnored,
27) fs.Item {
28	a.ignoreDir = ignore
29	a.ignoreFileType = fileTypeFilter
30
31	go a.UpdateProgress()
32	dir := a.processDir(path)
33
34	dir.BasePath = filepath.Dir(path)
35	a.wait.Wait()
36
37	a.progressDoneChan <- struct{}{}
38	a.doneChan.Broadcast()
39
40	return dir
41}
42
43func (a *ParallelStableOrderAnalyzer) processDir(path string) *Dir {
44	type indexedItem struct {
45		index int
46		item  fs.Item
47	}
48
49	var (
50		file      *File
51		err       error
52		totalSize int64
53		info      os.FileInfo
54		itemCount int
55		dirCount  int
56	)
57
58	a.wait.Add(1)
59
60	files, err := os.ReadDir(path)
61	if err != nil {
62		log.Print(err.Error())
63	}
64
65	dir := &Dir{
66		File: &File{
67			Name: filepath.Base(path),
68			Flag: getDirFlag(err, len(files)),
69		},
70		ItemCount: 1,
71		Files:     make(fs.Files, 0, len(files)),
72	}
73	setDirPlatformSpecificAttrs(dir, path)
74
75	// Buffer channel to prevent deadlock when sending files synchronously
76	itemChan := make(chan indexedItem, len(files))
77
78	for _, f := range files {
79		if a.IsCancelled() {
80			break
81		}
82		name := f.Name()
83		entryPath := filepath.Join(path, name)
84
85		if f.IsDir() {
86			if a.shouldSkipDir(name, entryPath) {
87				continue
88			}
89			currentIndex := itemCount
90			itemCount++
91			dirCount++
92
93			go func(entryPath string, idx int) {
94				concurrencyLimit <- struct{}{}
95				if a.IsCancelled() {
96					<-concurrencyLimit
97					itemChan <- indexedItem{idx, nil}
98					return
99				}
100				subdir := a.processDir(entryPath)
101				subdir.Parent = dir
102
103				<-concurrencyLimit
104				itemChan <- indexedItem{idx, subdir}
105			}(entryPath, currentIndex)
106		} else {
107			// Apply file type filter if set
108			if a.ignoreFileType != nil && a.ignoreFileType(name) {
109				continue // Skip this file
110			}
111
112			info, err = f.Info()
113			if err != nil {
114				log.Print(err.Error())
115				dir.SetFlag('!')
116				continue
117			}
118
119			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
120				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
121				if err != nil {
122					log.Print(err.Error())
123					dir.SetFlag('!')
124					continue
125				}
126				if infoF != nil {
127					info = infoF
128				}
129			}
130
131			// Apply time filter if set
132			if a.matchesTimeFilterFn != nil && !a.matchesTimeFilterFn(info.ModTime()) {
133				continue // Skip this file
134			}
135
136			file = &File{
137				Name:    name,
138				Flag:    getFlag(info),
139				Size:    info.Size(),
140				Parent:  dir,
141				Symlink: readSymlinkTarget(f.Type(), entryPath),
142			}
143			setPlatformSpecificAttrs(file, info)
144
145			totalSize += file.Usage
146
147			// Send file to channel with its index
148			itemChan <- indexedItem{itemCount, file}
149			itemCount++
150		}
151	}
152
153	go func() {
154		items := make([]indexedItem, itemCount)
155
156		// Collect all items (both files and subdirs)
157		for i := 0; i < itemCount; i++ {
158			indexed := <-itemChan
159			items[indexed.index] = indexed
160		}
161
162		// Add all items in their original order
163		for i := range itemCount {
164			if items[i].item != nil {
165				dir.AddFile(items[i].item)
166			}
167		}
168
169		a.wait.Done()
170	}()
171
172	a.progressCurrentItemName.Store(path)
173	a.progressItemCount.Add(int64(len(files)))
174	a.progressTotalUsage.Add(totalSize)
175	return dir
176}