fork/gdu

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

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

master
3.7 KiB173 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// SequentialAnalyzer implements Analyzer
13type SequentialAnalyzer struct {
14	BaseAnalyzer
15}
16
17// CreateSeqAnalyzer returns Analyzer
18func CreateSeqAnalyzer() *SequentialAnalyzer {
19	a := &SequentialAnalyzer{}
20	a.Init()
21	return a
22}
23
24// AnalyzeDir analyzes given path
25func (a *SequentialAnalyzer) 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.setCurrentDir(dir)
36
37	a.progressDoneChan <- struct{}{}
38	a.doneChan.Broadcast()
39
40	return dir
41}
42
43func (a *SequentialAnalyzer) processDir(path string) *Dir {
44	var (
45		file      fs.Item
46		err       error
47		totalSize int64
48		info      os.FileInfo
49		dirCount  int
50	)
51
52	files, err := os.ReadDir(path)
53	if err != nil {
54		log.Print(err.Error())
55	}
56
57	dir := &Dir{
58		File: &File{
59			Name: filepath.Base(path),
60			Flag: getDirFlag(err, len(files)),
61		},
62		ItemCount: 1,
63		Files:     make(fs.Files, 0, len(files)),
64	}
65	setDirPlatformSpecificAttrs(dir, path)
66
67	for _, f := range files {
68		if a.IsCancelled() {
69			break
70		}
71		name := f.Name()
72		entryPath := filepath.Join(path, name)
73		if f.IsDir() {
74			if a.shouldSkipDir(name, entryPath) {
75				continue
76			}
77			dirCount++
78
79			subdir := a.processDir(entryPath)
80			subdir.Parent = dir
81			dir.AddFile(subdir)
82		} else {
83			// Apply file type filter if set
84			if a.ignoreFileType != nil && a.ignoreFileType(name) {
85				continue // Skip this file
86			}
87
88			info, err = f.Info()
89			if err != nil {
90				log.Print(err.Error())
91				dir.SetFlag('!')
92				continue
93			}
94
95			if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 {
96				infoF, err := followSymlink(entryPath, a.gitAnnexedSize)
97				if err != nil {
98					log.Print(err.Error())
99					dir.SetFlag('!')
100					continue
101				}
102				if infoF != nil {
103					info = infoF
104				}
105			}
106
107			symlinkTarget := readSymlinkTarget(f.Type(), entryPath)
108
109			// Apply time filter if set
110			if a.matchesTimeFilterFn != nil && !a.matchesTimeFilterFn(info.ModTime()) {
111				continue // Skip this file
112			}
113
114			switch {
115			case a.archiveBrowsing && isZipFile(name):
116				zipDir, err := processZipFile(entryPath, info)
117				if err != nil {
118					log.Printf("Failed to process zip file %s: %v", entryPath, err)
119					file = &File{
120						Name:   name,
121						Flag:   getFlag(info),
122						Size:   info.Size(),
123						Parent: dir,
124					}
125				} else {
126					uncompressedSize, compressedSize, err := getZipFileSize(entryPath)
127					if err == nil {
128						zipDir.Size = uncompressedSize
129						zipDir.Usage = compressedSize
130					}
131					zipDir.Parent = dir
132					file = zipDir
133				}
134			case a.archiveBrowsing && isTarFile(name):
135				tarDir, err := processTarFile(entryPath, info)
136				if err != nil {
137					log.Printf("Failed to process tar file %s: %v", entryPath, err)
138					file = &File{
139						Name:   name,
140						Flag:   getFlag(info),
141						Size:   info.Size(),
142						Parent: dir,
143					}
144				} else {
145					tarDir.Parent = dir
146					file = tarDir
147				}
148			default:
149				file = &File{
150					Name:    name,
151					Flag:    getFlag(info),
152					Size:    info.Size(),
153					Parent:  dir,
154					Symlink: symlinkTarget,
155				}
156			}
157
158			if file != nil {
159				// Only set platform-specific attributes for regular files
160				if regularFile, ok := file.(*File); ok {
161					setPlatformSpecificAttrs(regularFile, info)
162				}
163				totalSize += file.GetUsage()
164				dir.AddFile(file)
165			}
166		}
167	}
168
169	a.progressCurrentItemName.Store(path)
170	a.progressItemCount.Add(int64(len(files)))
171	a.progressTotalUsage.Add(totalSize)
172	return dir
173}