fork/gdu

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

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

ignore
11.0 KiB455 linesraw
1package analyze
2
3import (
4	"io"
5	"iter"
6	"os"
7	"path/filepath"
8	"sync"
9	"time"
10
11	"github.com/dundee/gdu/v5/internal/common"
12	"github.com/dundee/gdu/v5/pkg/fs"
13	log "github.com/sirupsen/logrus"
14)
15
16// StoredAnalyzer implements Analyzer
17type StoredAnalyzer struct {
18	BaseAnalyzer
19	storage     *Storage
20	storagePath string
21}
22
23// CreateStoredAnalyzer returns Analyzer
24func CreateStoredAnalyzer(storagePath string) *StoredAnalyzer {
25	a := &StoredAnalyzer{
26		storagePath: storagePath,
27	}
28	a.Init()
29	return a
30}
31
32// AnalyzeDir analyzes given path
33func (a *StoredAnalyzer) AnalyzeDir(
34	path string, ignore common.ShouldDirBeIgnored, fileTypeFilter common.ShouldFileBeIgnored,
35) fs.Item {
36	a.ignoreDir = ignore
37	a.ignoreFileType = fileTypeFilter
38
39	a.storage = NewStorage(a.storagePath, path)
40	closeFn := a.storage.Open()
41	defer func() {
42		// nasty hack to close storage after all goroutines are done
43		// Wait returns immediately if value is 0
44		// few last goroutines might still start after that
45		time.Sleep(1 * time.Second)
46		closeFn()
47	}()
48
49	a.ignoreDir = ignore
50
51	go a.UpdateProgress()
52	dir := a.processDir(path)
53
54	a.wait.Wait()
55
56	a.progressDoneChan <- struct{}{}
57	a.doneChan.Broadcast()
58
59	return dir
60}
61
62func (a *StoredAnalyzer) processDir(path string) *StoredDir {
63	var (
64		file       fs.Item
65		err        error
66		totalUsage int64
67		info       os.FileInfo
68		dirCount   int
69	)
70
71	a.wait.Add(1)
72
73	files, err := os.ReadDir(path)
74	if err != nil {
75		log.Print(err.Error())
76	}
77
78	dir := &StoredDir{
79		Dir: &Dir{
80			File: &File{
81				Name: filepath.Base(path),
82				Flag: getDirFlag(err, len(files)),
83			},
84			BasePath:  filepath.Dir(path),
85			ItemCount: 1,
86			Files:     make(fs.Files, 0, len(files)),
87		},
88	}
89	parent := &ParentDir{Path: path}
90
91	setDirPlatformSpecificAttrs(dir.Dir, path)
92
93	for _, f := range files {
94		if a.IsCancelled() {
95			break
96		}
97		name := f.Name()
98		entryPath := filepath.Join(path, name)
99		if f.IsDir() {
100			if a.shouldSkipDir(name, entryPath) {
101				continue
102			}
103			dirCount++
104
105			subdir := &StoredDir{
106				&Dir{
107					File: &File{
108						Name: name,
109					},
110					BasePath: path,
111				},
112				nil,
113				sync.Mutex{},
114			}
115			dir.AddFile(subdir)
116
117			go func(entryPath string) {
118				concurrencyLimit <- struct{}{}
119				a.processDir(entryPath)
120				<-concurrencyLimit
121			}(entryPath)
122		} else {
123			// Apply file type filter if set
124			if a.ignoreFileType != nil && a.ignoreFileType(name) {
125				continue // Skip this file
126			}
127
128			info, err = f.Info()
129			if err != nil {
130				log.Print(err.Error())
131				continue
132			}
133
134			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
135				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
136				if err != nil {
137					log.Print(err.Error())
138					continue
139				}
140				if infoF != nil {
141					info = infoF
142				}
143			}
144
145			symlinkTarget := readSymlinkTarget(f.Type(), entryPath)
146
147			// Apply time filter if set
148			if a.matchesTimeFilterFn != nil && !a.matchesTimeFilterFn(info.ModTime()) {
149				continue // Skip this file
150			}
151
152			// Check if it's a zip or jar file
153			if a.archiveBrowsing && isZipFile(name) {
154				zipDir, err := processZipFile(entryPath, info)
155				if err != nil {
156					// If unable to process zip file, treat as regular file
157					log.Printf("Failed to process zip file %s: %v", entryPath, err)
158					file = &File{
159						Name:   name,
160						Flag:   getFlag(info),
161						Size:   info.Size(),
162						Parent: parent,
163					}
164				} else {
165					// Successfully processed zip file, use zip content size
166					uncompressedSize, compressedSize, err := getZipFileSize(entryPath)
167					if err == nil {
168						zipDir.Size = uncompressedSize
169						zipDir.Usage = compressedSize
170					}
171					zipDir.Parent = parent
172					file = zipDir
173				}
174			} else {
175				file = &File{
176					Name:    name,
177					Flag:    getFlag(info),
178					Size:    info.Size(),
179					Parent:  parent,
180					Symlink: symlinkTarget,
181				}
182			}
183
184			if file != nil {
185				// Only set platform-specific attributes for regular files
186				if regularFile, ok := file.(*File); ok {
187					setPlatformSpecificAttrs(regularFile, info)
188				}
189				totalUsage += file.GetUsage()
190				dir.AddFile(file)
191			}
192		}
193	}
194
195	err = a.storage.StoreDir(dir)
196	if err != nil {
197		log.Print(err.Error())
198	}
199
200	a.wait.Done()
201
202	a.progressCurrentItemName.Store(path)
203	a.progressItemCount.Add(int64(len(files)))
204	a.progressTotalUsage.Add(totalUsage)
205
206	return dir
207}
208
209// StoredDir implements Dir item stored on disk
210type StoredDir struct {
211	*Dir
212	cachedFiles fs.Files
213	dbLock      sync.Mutex
214}
215
216// GetParent returns parent dir
217func (f *StoredDir) GetParent() fs.Item {
218	if DefaultStorage.GetTopDir() == f.GetPath() {
219		return nil
220	}
221
222	if !DefaultStorage.IsOpen() {
223		closeFn := DefaultStorage.Open()
224		defer closeFn()
225	}
226
227	dir, err := DefaultStorage.GetDirForPath(f.BasePath)
228	if err != nil {
229		log.Print(err.Error())
230	}
231	return dir
232}
233
234// GetFiles returns files in directory as a sorted iterator
235// If files are already cached, use them
236// Otherwise load them from storage
237func (f *StoredDir) GetFiles(sortBy fs.SortBy, order fs.SortOrder) iter.Seq[fs.Item] {
238	return func(yield func(fs.Item) bool) {
239		files := f.loadFiles()
240		sortFiles(files, sortBy, order)
241
242		for _, item := range files {
243			if !yield(item) {
244				return
245			}
246		}
247	}
248}
249
250// loadFiles loads files from storage or returns cached files
251func (f *StoredDir) loadFiles() fs.Files {
252	if f.cachedFiles != nil {
253		// Return a copy to avoid modifying cached slice
254		result := make(fs.Files, len(f.cachedFiles))
255		copy(result, f.cachedFiles)
256		return result
257	}
258
259	if !DefaultStorage.IsOpen() {
260		f.dbLock.Lock()
261		defer f.dbLock.Unlock()
262		closeFn := DefaultStorage.Open()
263		defer closeFn()
264	}
265
266	var files fs.Files
267	for _, file := range f.Files {
268		if file.IsDir() {
269			dir := &StoredDir{
270				&Dir{
271					File: &File{
272						Name: file.GetName(),
273					},
274					BasePath: f.GetPath(),
275				},
276				nil,
277				sync.Mutex{},
278			}
279
280			err := DefaultStorage.LoadDir(dir)
281			if err != nil {
282				log.Print(err.Error())
283			}
284			files = append(files, dir)
285		} else {
286			files = append(files, file)
287		}
288	}
289
290	f.cachedFiles = files
291	// Return a copy to avoid modifying cached slice
292	result := make(fs.Files, len(files))
293	copy(result, files)
294	return result
295}
296
297// RemoveFile removes file from stored directory
298// It also updates size and item count of parent directories
299func (f *StoredDir) RemoveFile(item fs.Item) {
300	if !DefaultStorage.IsOpen() {
301		f.dbLock.Lock()
302		defer f.dbLock.Unlock()
303		closeFn := DefaultStorage.Open()
304		defer closeFn()
305	}
306
307	f.Files = f.Files.Remove(item)
308	f.cachedFiles = nil
309
310	cur := f
311	for {
312		cur.ItemCount -= item.GetItemCount()
313		cur.Size -= item.GetSize()
314		cur.Usage -= item.GetUsage()
315
316		err := DefaultStorage.StoreDir(cur)
317		if err != nil {
318			log.Print(err.Error())
319		}
320
321		parent := cur.GetParent()
322		if parent == nil {
323			break
324		}
325		cur = parent.(*StoredDir)
326	}
327}
328
329// GetItemStats returns item count, apparent usage and real usage of this dir
330func (f *StoredDir) GetItemStats(linkedItems fs.HardLinkedItems, filteringFiles bool) (itemCount, size, usage int64) {
331	f.updateStats(linkedItems, filteringFiles)
332	return f.ItemCount, f.GetSize(), f.GetUsage()
333}
334
335func (f *StoredDir) UpdateStatsWithFileFiltering(linkedItems fs.HardLinkedItems) {
336	f.updateStats(linkedItems, true)
337}
338
339// UpdateStats recursively updates size and item count
340func (f *StoredDir) UpdateStats(linkedItems fs.HardLinkedItems) {
341	f.updateStats(linkedItems, false)
342}
343
344func (f *StoredDir) updateStats(linkedItems fs.HardLinkedItems, filteringFiles bool) {
345	if !DefaultStorage.IsOpen() {
346		closeFn := DefaultStorage.Open()
347		defer closeFn()
348	}
349
350	totalSize := int64(4096)
351	totalUsage := int64(4096)
352	var itemCount int64
353	f.cachedFiles = nil
354	files := f.loadFiles()
355	for _, entry := range files {
356		count, size, usage := entry.GetItemStats(linkedItems, filteringFiles)
357		totalSize += size
358		totalUsage += usage
359		itemCount += count
360
361		if entry.GetMtime().After(f.Mtime) {
362			f.Mtime = entry.GetMtime()
363		}
364
365		switch entry.GetFlag() {
366		case '!', '.':
367			if f.Flag != '!' {
368				f.Flag = '.'
369			}
370		}
371	}
372	f.cachedFiles = nil
373	f.ItemCount = itemCount + 1
374	f.Size = totalSize
375	f.Usage = totalUsage
376	err := DefaultStorage.StoreDir(f)
377	if err != nil {
378		log.Print(err.Error())
379	}
380}
381
382// RemoveFileByName removes file by name from stored directory
383func (f *StoredDir) RemoveFileByName(name string) {
384	if !DefaultStorage.IsOpen() {
385		f.dbLock.Lock()
386		defer f.dbLock.Unlock()
387		closeFn := DefaultStorage.Open()
388		defer closeFn()
389	}
390
391	idx, ok := f.Files.FindByName(name)
392	if !ok {
393		return
394	}
395	item := f.Files[idx]
396	f.Files = append(f.Files[:idx], f.Files[idx+1:]...)
397	f.cachedFiles = nil
398
399	cur := f
400	for {
401		cur.ItemCount -= item.GetItemCount()
402		cur.Size -= item.GetSize()
403		cur.Usage -= item.GetUsage()
404
405		err := DefaultStorage.StoreDir(cur)
406		if err != nil {
407			log.Print(err.Error())
408		}
409
410		parent := cur.GetParent()
411		if parent == nil {
412			break
413		}
414		cur = parent.(*StoredDir)
415	}
416}
417
418// ParentDir represents parent directory of single file
419// It is used to get path to parent directory of a file
420type ParentDir struct {
421	Path string
422}
423
424func (p *ParentDir) GetPath() string {
425	return p.Path
426}
427func (p *ParentDir) GetName() string                                     { panic("must not be called") }
428func (p *ParentDir) GetFlag() rune                                       { panic("must not be called") }
429func (p *ParentDir) IsDir() bool                                         { panic("must not be called") }
430func (p *ParentDir) GetSize() int64                                      { panic("must not be called") }
431func (p *ParentDir) GetType() string                                     { panic("must not be called") }
432func (p *ParentDir) GetUsage() int64                                     { panic("must not be called") }
433func (p *ParentDir) GetMtime() time.Time                                 { panic("must not be called") }
434func (p *ParentDir) GetItemCount() int64                                 { panic("must not be called") }
435func (p *ParentDir) GetParent() fs.Item                                  { panic("must not be called") }
436func (p *ParentDir) SetParent(fs.Item)                                   { panic("must not be called") }
437func (p *ParentDir) GetMultiLinkedInode() uint64                         { panic("must not be called") }
438func (p *ParentDir) EncodeJSON(io.Writer, bool, fs.JSONAttributes) error { panic("must not be called") }
439func (p *ParentDir) UpdateStats(linkedItems fs.HardLinkedItems)          { panic("must not be called") }
440func (p *ParentDir) UpdateStatsWithFileFiltering(linkedItems fs.HardLinkedItems) {
441	panic("must not be called")
442}
443func (p *ParentDir) AddFile(fs.Item)                                    { panic("must not be called") }
444func (p *ParentDir) GetFiles(fs.SortBy, fs.SortOrder) iter.Seq[fs.Item] { panic("must not be called") }
445func (p *ParentDir) GetFilesLocked(fs.SortBy, fs.SortOrder) iter.Seq[fs.Item] {
446	panic("must not be called")
447}
448func (p *ParentDir) RLock() func()                { panic("must not be called") }
449func (p *ParentDir) RemoveFile(item fs.Item)      { panic("must not be called") }
450func (p *ParentDir) RemoveFileByName(name string) { panic("must not be called") }
451func (p *ParentDir) GetItemStats(
452	linkedItems fs.HardLinkedItems, filteringFiles bool,
453) (itemCount, size, usage int64) {
454	panic("must not be called")
455}