forked from TrueBlocks/trueblocks-core
/
filescanner.go
110 lines (83 loc) · 2.22 KB
/
filescanner.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
// Parallel directory scanner project
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
//DirectoryWalk walks through directory and prints files
func DirectoryWalk(root string) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
for _, file := range files {
fmt.Println(file)
}
fmt.Println("Count:", len(files))
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage:", os.Args[0], "PATTERN", "DIRECTORY")
fmt.Println("Example: ", os.Args[0], "H", "/home/user/Downloads")
return
}
if len(os.Args[1]) < 1 {
fmt.Println("Pattern cannot be empty")
return
}
pattern := []byte(os.Args[1])
root := os.Args[2]
fmt.Println(pattern, root)
if yes, err := exists(root); yes == false || err != nil {
fmt.Println(root, "Directory available : ", yes, err)
return
}
bytePattern := byte(60) // dummy initialization used to match deb file
bytePattern = pattern[0]
fmt.Println("Start of scan")
ParallelDirectoryScan(bytePattern, root)
fmt.Println("End of scan")
}
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// ParallelDirectoryScan scans given directory in parallel
func ParallelDirectoryScan(patternToSearch byte, directoryToScan string) {
directoriesChannel := make(chan string, 100)
dirScan := directoryScanner{
rootDirectory: directoryToScan,
directoryChannel: directoriesChannel,
walkSubdirectory: true,
}
dirScan.scanInBackground()
patternMatchedChannel := make(chan string, 100)
processor := fileProcessor{
workerCount: runtime.NumCPU(), // set worker equal to number of cpu
directoriesChannel: directoriesChannel,
patternMatchedChannel: patternMatchedChannel,
}
processor.processInBackground(patternToSearch)
collector := fileNameCollector{
patternMatchedChannel: patternMatchedChannel,
}
collector.collect()
dirScan.waitForCompletion()
processor.waitForCompletion()
collector.buildSortedList()
collector.print()
processor.printStatus()
}