-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfinder.go
executable file
·120 lines (107 loc) · 2.77 KB
/
alfinder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
var systemDirs = []string{
"/bin",
"/boot",
"/dev",
"/etc",
"/lib",
"/lib64",
"/proc",
"/root",
"/run",
"/sbin",
"/sys",
"/tmp",
"/usr",
"/var",
}
func findFileOrFolder(directory string, name string, fileType string, results chan<- string) {
filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsPermission(err) {
return filepath.SkipDir // Skip directories that cause permission errors
}
fmt.Println("Error accessing:", path, "-", err)
return nil
}
baseName := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
matchesName := strings.Contains(strings.ToLower(baseName), strings.ToLower(name))
matchesType := true
if fileType != "" {
matchesType = false
switch fileType {
case "pdf":
matchesType = strings.HasSuffix(strings.ToLower(path), ".pdf")
case "img":
matchesType = strings.HasSuffix(strings.ToLower(path), ".jpg") ||
strings.HasSuffix(strings.ToLower(path), ".jpeg") ||
strings.HasSuffix(strings.ToLower(path), ".png") ||
strings.HasSuffix(strings.ToLower(path), ".gif") ||
strings.HasSuffix(strings.ToLower(path), ".bmp")
case "txt":
matchesType = strings.HasSuffix(strings.ToLower(path), ".txt")
}
}
if matchesName && matchesType {
results <- path
}
return nil
})
}
func main() {
var directory, name, fileType string
switch len(os.Args) {
case 2:
directory = "/" // default to root for global search
name = os.Args[1]
case 3:
directory = "/" // default to root for global search
name = os.Args[1]
fileType = os.Args[2][1:] // remove the leading '-'
case 4:
directory = os.Args[1]
name = os.Args[2]
fileType = os.Args[3][1:] // remove the leading '-'
default:
fmt.Printf("Usage: %s [directory] <name> [<file type>]\n", os.Args[0])
os.Exit(1)
}
fmt.Printf("Searching for '%s' in directory '%s' with file type '%s'\n", name, directory, fileType)
results := make(chan string, 100) // increase buffer size for better performance
go func() {
if directory == "/" {
for _, dir := range systemDirs {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return filepath.SkipDir
}
return nil
})
}
filepath.Walk("/home", func(path string, info os.FileInfo, err error) error {
if err == nil {
findFileOrFolder(path, name, fileType, results)
}
return nil
})
findFileOrFolder("/Users", name, fileType, results)
} else {
findFileOrFolder(directory, name, fileType, results)
}
close(results)
}()
found := false
for result := range results {
fmt.Println("Found:", result)
found = true
}
if !found {
fmt.Println("No matching files or folders found.")
}
}