fork/gdu

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

Julia RyanMake -x respect subvolumes4d478cf

subvolume
7.8 KiB269 linesraw
1// Package common contains commong logic and interfaces used across Gdu
2// nolint: revive //Why: this is common package
3package common
4
5import (
6	"bufio"
7	"os"
8	"path/filepath"
9	"regexp"
10	"strings"
11	"syscall"
12
13	log "github.com/sirupsen/logrus"
14)
15
16// CreateIgnorePattern creates one pattern from all path patterns
17func CreateIgnorePattern(paths []string) (compiled *regexp.Regexp, err error) {
18	for i, path := range paths {
19		if _, err = regexp.Compile(path); err != nil {
20			return nil, err
21		}
22		if !filepath.IsAbs(path) {
23			absPath, err := filepath.Abs(path)
24			if err == nil {
25				paths = append(paths, absPath)
26			}
27		} else {
28			relPath, err := filepath.Rel("/", path)
29			if err == nil {
30				paths = append(paths, relPath)
31			}
32		}
33		paths[i] = "(" + path + ")"
34	}
35
36	ignore := `^(?:` + strings.Join(paths, "|") + `)$`
37	return regexp.Compile(ignore)
38}
39
40// SetIgnoreDirPaths sets paths to ignore
41func (ui *UI) SetIgnoreDirPaths(paths []string) {
42	log.Printf("Ignoring dirs %s", strings.Join(paths, ", "))
43	ui.IgnoreDirPaths = make(map[string]struct{}, len(paths)*2)
44	for _, path := range paths {
45		ui.IgnoreDirPaths[path] = struct{}{}
46		if !filepath.IsAbs(path) {
47			if absPath, err := filepath.Abs(path); err == nil {
48				ui.IgnoreDirPaths[absPath] = struct{}{}
49			}
50		} else {
51			if relPath, err := filepath.Rel("/", path); err == nil {
52				ui.IgnoreDirPaths[relPath] = struct{}{}
53			}
54		}
55	}
56}
57
58// SetIgnoreDirPatterns sets regular patterns of dirs to ignore
59func (ui *UI) SetIgnoreDirPatterns(paths []string) error {
60	var err error
61	log.Printf("Ignoring dir patterns %s", strings.Join(paths, ", "))
62	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
63	return err
64}
65
66// SetIgnoreFromFile sets regular patterns of dirs to ignore
67func (ui *UI) SetIgnoreFromFile(ignoreFile string) error {
68	var err error
69	var paths []string
70	log.Printf("Reading ignoring dir patterns from file '%s'", ignoreFile)
71
72	file, err := os.Open(ignoreFile)
73	if err != nil {
74		return err
75	}
76	defer file.Close()
77
78	scanner := bufio.NewScanner(file)
79	for scanner.Scan() {
80		paths = append(paths, scanner.Text())
81	}
82
83	if err := scanner.Err(); err != nil {
84		return err
85	}
86
87	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
88	return err
89}
90
91// SetIgnoreTypes sets file types to ignore
92func (ui *UI) SetIgnoreTypes(types []string) {
93	log.Printf("Ignoring file types: %s", strings.Join(types, ", "))
94	ui.IgnoreTypes = types
95}
96
97// SetIncludeTypes sets file types to include (whitelist)
98func (ui *UI) SetIncludeTypes(types []string) {
99	log.Printf("Including only file types: %s", strings.Join(types, ", "))
100	ui.IncludeTypes = types
101}
102
103// SetIgnoreHidden sets flags if hidden dirs should be ignored
104func (ui *UI) SetIgnoreHidden(value bool) {
105	log.Printf("Ignoring hidden dirs")
106	ui.IgnoreHidden = value
107}
108
109// ShouldDirBeIgnored returns true if given path should be ignored
110func (ui *UI) ShouldDirBeIgnored(name, path string) bool {
111	_, shouldIgnore := ui.IgnoreDirPaths[path]
112	if shouldIgnore {
113		log.Printf("Directory %s ignored", path)
114	}
115	return shouldIgnore
116}
117
118// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored
119func (ui *UI) ShouldDirBeIgnoredUsingPattern(name, path string) bool {
120	shouldIgnore := ui.IgnoreDirPathPatterns.MatchString(path)
121	if shouldIgnore {
122		log.Printf("Directory %s ignored", path)
123	}
124	return shouldIgnore
125}
126
127// IsHiddenDir returns if the dir name begins with dot
128func (ui *UI) IsHiddenDir(name, path string) bool {
129	shouldIgnore := name[0] == '.'
130	if shouldIgnore {
131		log.Printf("Directory %s ignored", path)
132	}
133	return shouldIgnore
134}
135
136// ShouldFileBeIgnoredByType returns true if file should be ignored based on its extension
137func (ui *UI) ShouldFileBeIgnoredByType(name string) bool {
138	if len(ui.IgnoreTypes) == 0 {
139		return false
140	}
141
142	ext := strings.ToLower(filepath.Ext(name))
143	if ext == "" {
144		return false // No extension, don't ignore
145	}
146
147	// Remove leading dot from extension
148	ext = strings.TrimPrefix(ext, ".")
149
150	for _, ignoreType := range ui.IgnoreTypes {
151		// Remove leading dot from ignoreType
152		cleanIgnoreType := strings.TrimPrefix(strings.ToLower(ignoreType), ".")
153		if cleanIgnoreType == ext {
154			log.Printf("File %s ignored by type", name)
155			return true
156		}
157	}
158	return false
159}
160
161// ShouldFileBeIncludedByType returns true if file should be included based on its extension
162func (ui *UI) ShouldFileBeIncludedByType(name string) bool {
163	if len(ui.IncludeTypes) == 0 {
164		return true // No include filter, include all
165	}
166
167	ext := strings.ToLower(filepath.Ext(name))
168	if ext == "" {
169		return false // No extension, don't include if we have include filter
170	}
171
172	// Remove leading dot from extension
173	ext = strings.TrimPrefix(ext, ".")
174
175	for _, includeType := range ui.IncludeTypes {
176		// Remove leading dot from includeType
177		cleanIncludeType := strings.TrimPrefix(strings.ToLower(includeType), ".")
178		if cleanIncludeType == ext {
179			return true
180		}
181	}
182
183	log.Printf("File %s excluded by type filter", name)
184	return false
185}
186
187// CreateIgnoreFunc returns function for detecting if dir should be ignored
188// nolint: gocyclo // Why: This function is a switch statement that is not too complex
189func (ui *UI) CreateIgnoreFunc() ShouldDirBeIgnored {
190	switch {
191	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && !ui.IgnoreHidden:
192		return ui.ShouldDirBeIgnored
193	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
194		return func(name, path string) bool {
195			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path)
196		}
197	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
198		return func(name, path string) bool {
199			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
200		}
201	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
202		return func(name, path string) bool {
203			return ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
204		}
205	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
206		return ui.ShouldDirBeIgnoredUsingPattern
207	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
208		return ui.IsHiddenDir
209	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
210		return func(name, path string) bool {
211			return ui.ShouldDirBeIgnored(name, path) || ui.IsHiddenDir(name, path)
212		}
213	default:
214		return func(name, path string) bool { return false }
215	}
216}
217
218// CreateFileTypeFilter returns function for detecting if file should be ignored based on type
219func (ui *UI) CreateFileTypeFilter() ShouldFileBeIgnored {
220	// If we have include types, use whitelist mode
221	if len(ui.IncludeTypes) > 0 {
222		return func(name string) bool {
223			return !ui.ShouldFileBeIncludedByType(name)
224		}
225	}
226
227	// If we have ignore types, use blacklist mode
228	if len(ui.IgnoreTypes) > 0 {
229		return func(name string) bool {
230			return ui.ShouldFileBeIgnoredByType(name)
231		}
232	}
233
234	// No type filtering - return nil to indicate no filtering is needed
235	return nil
236}
237
238// IsFilteringFiles returns true if we have any file type filters set
239func (ui *UI) IsFilteringFiles() bool {
240	return len(ui.IgnoreTypes) > 0 || len(ui.IncludeTypes) > 0 || ui.FilteringFiles
241}
242
243func (ui *UI) IsCrossFS(name, path string) bool {
244	parent := filepath.Dir(path)
245	var a, b syscall.Stat_t
246	err1 := syscall.Stat(parent, &a)
247	err2 := syscall.Stat(path, &b)
248	if err1 != nil || err2 != nil {
249		return true
250	}
251	return a.Dev != b.Dev
252}
253
254// CreateIgnoreFunc returns function for detecting if dir should be ignored
255func (ui *UI) ShouldIgnore(name, path string) bool {
256	if len(ui.IgnoreDirPaths) > 0 && ui.ShouldDirBeIgnored(name, path) {
257		return true
258	}
259	if ui.IgnoreDirPathPatterns != nil && ui.ShouldDirBeIgnoredUsingPattern(name, path) {
260		return true
261	}
262	if ui.IgnoreHidden && ui.IsHiddenDir(name, path) {
263		return true
264	}
265	if ui.IgnoreCrossFS && ui.IsCrossFS(name, path) {
266		return true
267	}
268	return false
269}