fork/gdu

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

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

master
5.0 KiB256 linesraw
1package analyze
2
3import (
4	"os"
5	"path/filepath"
6	"sync"
7	"time"
8
9	"github.com/dundee/gdu/v5/internal/common"
10	"github.com/dundee/gdu/v5/pkg/fs"
11	log "github.com/sirupsen/logrus"
12)
13
14var pathSep = string(os.PathSeparator)
15
16var _ common.Analyzer = (*TopDirAnalyzer)(nil)
17
18// TopDirAnalyzer implements Analyzer
19// It doesn't return the full directory structure, only the top level directory,
20// thus is suitable only for non-interactive mode.
21// It tries to use only stack for storing state and results.
22type TopDirAnalyzer struct {
23	BaseAnalyzer
24	linkedItems sync.Map
25}
26
27// CreateTopDirAnalyzer returns Analyzer
28func CreateTopDirAnalyzer() *TopDirAnalyzer {
29	a := &TopDirAnalyzer{
30		BaseAnalyzer: BaseAnalyzer{
31			ignoreFileType:      func(string) bool { return false },
32			matchesTimeFilterFn: func(time.Time) bool { return true },
33		},
34	}
35	a.Init()
36	return a
37}
38
39// AnalyzeDir analyzes given path
40func (a *TopDirAnalyzer) AnalyzeDir(
41	path string, ignore common.ShouldDirBeIgnored, fileTypeFilter common.ShouldFileBeIgnored,
42) fs.Item {
43	a.ignoreDir = ignore
44	if fileTypeFilter != nil {
45		a.ignoreFileType = fileTypeFilter
46	}
47
48	var subDirChan = make(chan struct{})
49
50	go a.UpdateProgress()
51
52	files, err := os.ReadDir(path)
53	if err != nil {
54		log.Print(err.Error())
55	}
56
57	dir := SimpleDir{
58		SimpleFile: SimpleFile{
59			Name:      filepath.Base(path),
60			Flag:      getDirFlag(err, len(files)),
61			IsDir:     true,
62			ItemCount: 1,
63		},
64		Files: make([]SimpleFile, 0, len(files)),
65	}
66
67	var topDirs []*TopDir
68
69	for _, f := range files {
70		if a.IsCancelled() {
71			break
72		}
73		name := f.Name()
74		entryPath := filepath.Join(path, name)
75		if f.IsDir() {
76			if a.shouldSkipDir(name, entryPath) {
77				continue
78			}
79			topDir := &TopDir{
80				Name: name,
81				Flag: ' ',
82			}
83			topDirs = append(topDirs, topDir)
84			go func(entryPath string) {
85				a.processSubDir(entryPath, topDir)
86				subDirChan <- struct{}{}
87			}(entryPath)
88		} else {
89			var info os.FileInfo
90			// Apply file type filter if set
91			if a.ignoreFileType(name) {
92				continue // Skip this file
93			}
94
95			info, err = f.Info()
96			if err != nil {
97				log.Print(err.Error())
98				dir.Flag = '!'
99				continue
100			}
101
102			// Apply time filter if set
103			if !a.matchesTimeFilterFn(info.ModTime()) {
104				continue // Skip this file
105			}
106
107			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
108				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
109				if err != nil {
110					log.Print(err.Error())
111					dir.Flag = '!'
112					continue
113				}
114				if infoF != nil {
115					info = infoF
116				}
117			}
118
119			file := SimpleFile{
120				Name:    name,
121				Flag:    getFlag(info),
122				Size:    info.Size(),
123				Symlink: readSymlinkTarget(f.Type(), entryPath),
124			}
125
126			usage, mli := getPlatformSpecificUsageAndMli(info)
127			file.Usage = usage
128
129			if mli > 0 {
130				file.Flag = 'H'
131			}
132
133			dir.Files = append(dir.Files, file)
134		}
135	}
136
137	for i := 0; i < len(topDirs); i++ {
138		<-subDirChan
139	}
140
141	a.wait.Wait()
142
143	for _, topDir := range topDirs {
144		size, usage, itemCount := topDir.GetUsage()
145		dir.Files = append(dir.Files, SimpleFile{
146			Name:      topDir.Name,
147			Flag:      topDir.Flag,
148			Size:      size,
149			Usage:     usage,
150			ItemCount: itemCount,
151			IsDir:     true,
152		})
153	}
154
155	dir.BasePath = filepath.Dir(path)
156
157	a.progressDoneChan <- struct{}{}
158	a.doneChan.Broadcast()
159
160	return &dir
161}
162
163func (a *TopDirAnalyzer) processSubDir(path string, topDir *TopDir) {
164	var (
165		err        error
166		totalSize  int64
167		totalUsage int64
168		totalCount int64
169		info       os.FileInfo
170	)
171
172	files, err := os.ReadDir(path)
173	if err != nil {
174		log.Print(err.Error())
175		topDir.SetFlag('.')
176	}
177
178	for _, f := range files {
179		if a.IsCancelled() {
180			break
181		}
182		name := f.Name()
183		entryPath := path + pathSep + name
184		if f.IsDir() {
185			if a.shouldSkipDir(name, entryPath) {
186				continue
187			}
188
189			totalCount++
190
191			select {
192			case concurrencyLimit <- struct{}{}:
193				a.wait.Add(1)
194				go func(entryPath string) {
195					a.processSubDir(entryPath, topDir)
196					<-concurrencyLimit
197					a.wait.Done()
198				}(entryPath)
199			default:
200				a.processSubDir(entryPath, topDir)
201			}
202		} else {
203			// Apply file type filter if set
204			if a.ignoreFileType(name) {
205				continue // Skip this file
206			}
207
208			info, err = f.Info()
209			if err != nil {
210				log.Print(err.Error())
211				topDir.SetFlag('.')
212				continue
213			}
214
215			// Apply time filter if set
216			if !a.matchesTimeFilterFn(info.ModTime()) {
217				continue // Skip this file
218			}
219
220			totalCount++
221
222			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
223				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
224				if err != nil {
225					log.Print(err.Error())
226					topDir.SetFlag('.')
227					continue
228				}
229				if infoF != nil {
230					info = infoF
231				}
232			}
233
234			usage, mli := getPlatformSpecificUsageAndMli(info)
235
236			if mli > 0 {
237				if _, loaded := a.linkedItems.LoadOrStore(mli, struct{}{}); loaded {
238					continue
239				}
240			}
241
242			totalUsage += usage
243			totalSize += info.Size()
244		}
245	}
246
247	if len(files) == 0 {
248		totalSize = EmptyDirSize
249		totalUsage = 0
250	}
251
252	a.progressItemCount.Add(totalCount)
253	a.progressTotalUsage.Add(totalUsage)
254
255	topDir.AddUsage(totalSize, totalUsage, totalCount)
256}