fork/gdu

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

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

subvolume
12.8 KiB548 linesraw
1package analyze
2
3import (
4	"iter"
5	"os"
6	"path/filepath"
7	"sort"
8	"sync"
9	"time"
10
11	"github.com/dundee/gdu/v5/pkg/fs"
12)
13
14// fileFlagMu protects the mutable Flag field on published File values. File
15// keeps its fields public for compatibility, so hard-link accounting can
16// update Flag after a file has been exposed to preview readers.
17var fileFlagMu sync.RWMutex
18
19var _ fs.SymlinkItem = (*File)(nil)
20
21// File struct
22type File struct {
23	Mtime   time.Time
24	Parent  fs.Item
25	Name    string
26	Symlink string
27	Size    int64
28	Usage   int64
29	Mli     uint64
30	Flag    rune
31}
32
33// GetName returns name of dir
34func (f *File) GetName() string {
35	return f.Name
36}
37
38// IsDir returns false for file
39func (f *File) IsDir() bool {
40	return false
41}
42
43// GetParent returns parent dir
44func (f *File) GetParent() fs.Item {
45	return f.Parent
46}
47
48// SetParent sets parent dir
49func (f *File) SetParent(parent fs.Item) {
50	f.Parent = parent
51}
52
53// GetPath returns absolute Get of the file
54func (f *File) GetPath() string {
55	return filepath.Join(f.Parent.GetPath(), f.Name)
56}
57
58// GetFlag returns flag of the file
59func (f *File) GetFlag() rune {
60	fileFlagMu.RLock()
61	defer fileFlagMu.RUnlock()
62	return f.Flag
63}
64
65// GetSize returns size of the file
66func (f *File) GetSize() int64 {
67	return f.Size
68}
69
70// GetUsage returns usage of the file
71func (f *File) GetUsage() int64 {
72	return f.Usage
73}
74
75// GetMtime returns mtime of the file
76func (f *File) GetMtime() time.Time {
77	return f.Mtime
78}
79
80// GetType returns name type of item
81func (f *File) GetType() string {
82	fileFlagMu.RLock()
83	defer fileFlagMu.RUnlock()
84	if f.Flag == '@' {
85		if f.Symlink != "" {
86			return "Symlink"
87		}
88		return "Other"
89	}
90	return "File"
91}
92
93// GetSymlinkTarget returns the target of a symlink, or empty string
94func (f *File) GetSymlinkTarget() string {
95	return f.Symlink
96}
97
98// GetItemCount returns 1 for file
99func (f *File) GetItemCount() int64 {
100	return 1
101}
102
103// GetMultiLinkedInode returns inode number of multilinked file
104func (f *File) GetMultiLinkedInode() uint64 {
105	return f.Mli
106}
107
108func (f *File) alreadyCounted(linkedItems fs.HardLinkedItems) bool {
109	mli := f.Mli
110	counted := false
111	if mli > 0 {
112		fileFlagMu.Lock()
113		f.Flag = 'H'
114		fileFlagMu.Unlock()
115		if _, ok := linkedItems[mli]; ok {
116			counted = true
117		}
118		linkedItems[mli] = append(linkedItems[mli], f)
119	}
120	return counted
121}
122
123// CreateFileItem creates a File from an os.FileInfo with correct platform-specific attributes
124func CreateFileItem(name string, info os.FileInfo) *File {
125	file := &File{
126		Name: name,
127		Size: info.Size(),
128		Flag: getFlag(info),
129	}
130	setPlatformSpecificAttrs(file, info)
131	return file
132}
133
134// GetItemStats returns 1 as count of items, apparent usage and real usage of this file
135func (f *File) GetItemStats(linkedItems fs.HardLinkedItems, filteringFiles bool) (itemCount, size, usage int64) {
136	if f.alreadyCounted(linkedItems) {
137		return 1, 0, 0
138	}
139	return 1, f.GetSize(), f.GetUsage()
140}
141
142// UpdateStats does nothing on file
143func (f *File) UpdateStats(linkedItems fs.HardLinkedItems)                  {}
144func (f *File) UpdateStatsWithFileFiltering(linkedItems fs.HardLinkedItems) {}
145
146// GetFiles returns all files in directory
147func (f *File) GetFiles(sortBy fs.SortBy, order fs.SortOrder) iter.Seq[fs.Item] {
148	return func(yield func(fs.Item) bool) {}
149}
150
151// GetFilesLocked returns all files in directory
152func (f *File) GetFilesLocked(sortBy fs.SortBy, order fs.SortOrder) iter.Seq[fs.Item] {
153	return f.GetFiles(sortBy, order)
154}
155
156// RLock panics on file
157func (f *File) RLock() func() {
158	panic("RLock should not be called on file")
159}
160
161// AddFile panics on file
162func (f *File) AddFile(item fs.Item) {
163	panic("AddFile should not be called on file")
164}
165
166// RemoveFile panics on file
167func (f *File) RemoveFile(item fs.Item) {
168	panic("RemoveFile should not be called on file")
169}
170
171// RemoveFileByName panics on file
172func (f *File) RemoveFileByName(name string) {
173	panic("RemoveFileByName should not be called on file")
174}
175
176// EmptyDirSize is the apparent size gdu assigns to a directory that has no
177// counted children. It is a fixed sentinel (not the real filesystem block
178// size) written by updateStats, SimpleDir.UpdateStats and the parallel top-dir
179// analyzer, and recognised again on import. Keep every producer and consumer
180// of this value referencing this constant so they can never drift apart.
181const EmptyDirSize int64 = 512
182
183// Dir struct
184type Dir struct {
185	*File
186	BasePath      string
187	Files         fs.Files
188	ItemCount     int64
189	statsFromJSON bool
190	m             sync.RWMutex
191}
192
193func snapshotDir(source *Dir, parent fs.Item) *Dir {
194	source.m.RLock()
195	snapshot := &Dir{
196		File: &File{
197			Mtime:  source.Mtime,
198			Parent: parent,
199			Name:   source.Name,
200			Size:   source.Size,
201			Usage:  source.Usage,
202			Mli:    source.Mli,
203			Flag:   source.Flag,
204		},
205		BasePath:  source.BasePath,
206		Files:     make(fs.Files, 0, len(source.Files)),
207		ItemCount: source.ItemCount,
208	}
209	children := append(fs.Files(nil), source.Files...)
210	source.m.RUnlock()
211
212	for _, child := range children {
213		snapshot.Files = append(snapshot.Files, snapshotItem(child, snapshot))
214	}
215	return snapshot
216}
217
218func snapshotItem(source, parent fs.Item) fs.Item {
219	switch item := source.(type) {
220	case *ZipDir:
221		snapshot := &ZipDir{Dir: snapshotDir(item.Dir, parent), zipPath: item.zipPath}
222		reparentSnapshotChildren(snapshot.Dir, snapshot)
223		return snapshot
224	case *TarDir:
225		snapshot := &TarDir{Dir: snapshotDir(item.Dir, parent), tarPath: item.tarPath}
226		reparentSnapshotChildren(snapshot.Dir, snapshot)
227		return snapshot
228	case *ZipFile:
229		return &ZipFile{
230			File:      snapshotFile(item.File, parent),
231			zipPath:   item.zipPath,
232			inZipPath: item.inZipPath,
233		}
234	case *TarFile:
235		return &TarFile{
236			File:      snapshotFile(item.File, parent),
237			tarPath:   item.tarPath,
238			inTarPath: item.inTarPath,
239		}
240	case *Dir:
241		return snapshotDir(item, parent)
242	case *File:
243		return snapshotFile(item, parent)
244	}
245
246	if source.IsDir() {
247		snapshot := &Dir{
248			File:      snapshotFile(source, parent),
249			Files:     make(fs.Files, 0),
250			ItemCount: source.GetItemCount(),
251		}
252		for child := range source.GetFilesLocked(fs.SortByName, fs.SortAsc) {
253			snapshot.Files = append(snapshot.Files, snapshotItem(child, snapshot))
254		}
255		return snapshot
256	}
257	return snapshotFile(source, parent)
258}
259
260func reparentSnapshotChildren(dir *Dir, parent fs.Item) {
261	for _, child := range dir.Files {
262		child.SetParent(parent)
263	}
264}
265
266func snapshotFile(source, parent fs.Item) *File {
267	return &File{
268		Mtime:  source.GetMtime(),
269		Parent: parent,
270		Name:   source.GetName(),
271		Size:   source.GetSize(),
272		Usage:  source.GetUsage(),
273		Mli:    source.GetMultiLinkedInode(),
274		Flag:   source.GetFlag(),
275	}
276}
277
278// AddFile add item to files
279func (f *Dir) AddFile(item fs.Item) {
280	f.m.Lock()
281	defer f.m.Unlock()
282	f.Files = append(f.Files, item)
283}
284
285// SetFlag updates the directory flag while preserving snapshot consistency.
286func (f *Dir) SetFlag(flag rune) {
287	f.m.Lock()
288	f.Flag = flag
289	f.m.Unlock()
290}
291
292// SetStatsFromJSON marks directory statistics decoded from an export as authoritative.
293func (f *Dir) SetStatsFromJSON() {
294	f.statsFromJSON = true
295}
296
297// HasEmptyDirPlaceholderStats reports whether this directory's stats are
298// indistinguishable from the placeholder updateStats produces for a directory
299// with no counted children (see EmptyDirSize). Such stats carry no information
300// worth pinning from an import: recomputation reproduces exactly the same
301// values, so callers should let updateStats run rather than preserving them.
302//
303// The items == 1 check is load-bearing: a depth-truncated directory that held
304// real content always exports with ItemCount >= 2, so it can never be mistaken
305// for a placeholder and its stats are always preserved.
306func (f *Dir) HasEmptyDirPlaceholderStats() bool {
307	return f.Size == EmptyDirSize && f.Usage == 0 && f.ItemCount == 1
308}
309
310// GetFiles returns all files in directory as a sorted iterator
311func (f *Dir) GetFiles(sortBy fs.SortBy, order fs.SortOrder) iter.Seq[fs.Item] {
312	return func(yield func(fs.Item) bool) {
313		// Make a copy to avoid modifying the original slice
314		sorted := make(fs.Files, len(f.Files))
315		copy(sorted, f.Files)
316		sortFiles(sorted, sortBy, order)
317
318		for _, item := range sorted {
319			if !yield(item) {
320				return
321			}
322		}
323	}
324}
325
326// GetFilesLocked returns all files in directory as a sorted iterator
327// It is safe to call this function from multiple goroutines
328func (f *Dir) GetFilesLocked(sortBy fs.SortBy, order fs.SortOrder) iter.Seq[fs.Item] {
329	return func(yield func(fs.Item) bool) {
330		f.m.RLock()
331		defer f.m.RUnlock()
332
333		// Make a copy to avoid modifying the original slice
334		sorted := make(fs.Files, len(f.Files))
335		copy(sorted, f.Files)
336		sortFiles(sorted, sortBy, order)
337
338		for _, item := range sorted {
339			if !yield(item) {
340				return
341			}
342		}
343	}
344}
345
346// GetType returns name type of item
347func (f *Dir) GetType() string {
348	return "Directory"
349}
350
351// GetFlag returns the current directory flag.
352func (f *Dir) GetFlag() rune {
353	f.m.RLock()
354	defer f.m.RUnlock()
355	return f.Flag
356}
357
358// GetSize returns the current apparent size.
359func (f *Dir) GetSize() int64 {
360	f.m.RLock()
361	defer f.m.RUnlock()
362	return f.Size
363}
364
365// GetUsage returns the current disk usage.
366func (f *Dir) GetUsage() int64 {
367	f.m.RLock()
368	defer f.m.RUnlock()
369	return f.Usage
370}
371
372// GetMtime returns the current modification time.
373func (f *Dir) GetMtime() time.Time {
374	f.m.RLock()
375	defer f.m.RUnlock()
376	return f.Mtime
377}
378
379// GetItemCount returns number of files in dir
380func (f *Dir) GetItemCount() int64 {
381	f.m.RLock()
382	defer f.m.RUnlock()
383	return f.ItemCount
384}
385
386// IsDir returns true for dir
387func (f *Dir) IsDir() bool {
388	return true
389}
390
391// GetPath returns absolute path of the file
392func (f *Dir) GetPath() string {
393	if f.BasePath != "" {
394		return filepath.Join(f.BasePath, f.Name)
395	}
396	if f.Parent != nil {
397		return filepath.Join(f.Parent.GetPath(), f.Name)
398	}
399	return f.Name
400}
401
402// GetItemStats returns item count, apparent usage and real usage of this dir
403func (f *Dir) GetItemStats(linkedItems fs.HardLinkedItems, filteringFiles bool) (itemCount, size, usage int64) {
404	f.updateStats(linkedItems, filteringFiles)
405	return f.GetItemCount(), f.GetSize(), f.GetUsage()
406}
407
408func (f *Dir) UpdateStats(linkedItems fs.HardLinkedItems) {
409	f.updateStats(linkedItems, false)
410}
411
412func (f *Dir) UpdateStatsWithFileFiltering(linkedItems fs.HardLinkedItems) {
413	f.updateStats(linkedItems, true)
414}
415
416// UpdateStats recursively updates size and item count
417func (f *Dir) updateStats(linkedItems fs.HardLinkedItems, filteringFiles bool) {
418	if f.statsFromJSON {
419		return
420	}
421
422	// Snapshot the file list under the read lock so it is safe to compute stats
423	// even while the analyzer is still appending items in another goroutine
424	// (e.g. when previewing a directory mid-scan).
425	f.m.RLock()
426	files := make(fs.Files, len(f.Files))
427	copy(files, f.Files)
428	mtime := f.Mtime
429	flag := f.Flag
430	f.m.RUnlock()
431
432	totalSize := int64(0)
433	totalUsage := int64(0)
434	var itemCount int64 = 1
435	var hasFiles bool
436	for _, entry := range files {
437		count, size, usage := entry.GetItemStats(linkedItems, filteringFiles)
438		totalSize += size
439		totalUsage += usage
440		itemCount += count
441
442		entryMtime := entry.GetMtime()
443		if entryMtime.After(mtime) {
444			mtime = entryMtime
445		}
446
447		if !entry.IsDir() {
448			hasFiles = true
449		}
450
451		switch entry.GetFlag() {
452		case '!', '.':
453			if flag != '!' {
454				flag = '.'
455			}
456		}
457	}
458
459	f.m.Lock()
460	defer f.m.Unlock()
461	f.Mtime = mtime
462	f.Flag = flag
463
464	// no files, or just empty dirs
465	if len(files) == 0 || (!hasFiles && filteringFiles && itemCount == int64(len(files)+1)) {
466		f.ItemCount = 1
467		f.Size = totalSize + EmptyDirSize
468		f.Usage = 0
469	} else {
470		f.ItemCount = itemCount
471		f.Size = totalSize
472		f.Usage = totalUsage
473	}
474}
475
476// RemoveFile removes item from dir, updates size and item count
477func (f *Dir) RemoveFile(item fs.Item) {
478	f.m.Lock()
479	defer f.m.Unlock()
480
481	f.Files = f.Files.Remove(item)
482
483	cur := f
484	for {
485		cur.ItemCount -= item.GetItemCount()
486		cur.Size -= item.GetSize()
487		cur.Usage -= item.GetUsage()
488
489		if cur.Parent == nil {
490			break
491		}
492		cur = cur.Parent.(*Dir)
493	}
494}
495
496// sortFiles sorts files in place according to sortBy and order
497func sortFiles(files fs.Files, sortBy fs.SortBy, order fs.SortOrder) {
498	var sorter sort.Interface
499	switch sortBy {
500	case fs.SortByName:
501		sorter = fs.ByName(files)
502	case fs.SortByItemCount:
503		sorter = fs.ByItemCount(files)
504	case fs.SortByMtime:
505		sorter = fs.ByMtime(files)
506	case fs.SortByApparentSize:
507		sorter = fs.ByApparentSize(files)
508	case fs.SortBySize:
509		sorter = files
510	}
511
512	if order == fs.SortDesc {
513		sort.Sort(sort.Reverse(sorter))
514	} else {
515		sort.Sort(sorter)
516	}
517}
518
519// RLock read locks dir
520func (f *Dir) RLock() func() {
521	f.m.RLock()
522	return f.m.RUnlock
523}
524
525// RemoveFileByName removes item by name from dir
526func (f *Dir) RemoveFileByName(name string) {
527	f.m.Lock()
528	defer f.m.Unlock()
529
530	idx, ok := f.Files.FindByName(name)
531	if !ok {
532		return
533	}
534	item := f.Files[idx]
535	f.Files = append(f.Files[:idx], f.Files[idx+1:]...)
536
537	cur := f
538	for {
539		cur.ItemCount -= item.GetItemCount()
540		cur.Size -= item.GetSize()
541		cur.Usage -= item.GetUsage()
542
543		if cur.Parent == nil {
544			break
545		}
546		cur = cur.Parent.(*Dir)
547	}
548}