-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
222 lines (181 loc) · 5.79 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
// A one copy read-only global list of "bad words"
var badWords []string
// Total count of words removed from all files
var badCount uint64
// Total count of words processed
var totalWords uint64
// Panic if an error is not `nil`
// `e` error: The error to check
func check(e error) {
if e != nil {
panic(e)
}
}
// Check if the word in question contains any word from `badWords`
// `word` string: The word in question
//
// Returns: bool (false if contains bad word, otherwise true)
func checkWord(word string) bool {
lowerStr := strings.ToLower(word)
for _, s := range badWords {
if strings.Contains(lowerStr, s) {
return false
}
}
return true
}
// Remove "bad words" from a file.
// Recurse when the filepath is a directory, with an actual file being the base case.
// `fpath` string: The filepath of the input file
// `opath` string: The output directory path
// `threads` int: The maximum number of concurrent goroutines processing the file
func sanitizeList(fpath string, opath string, threads int) {
// Print the current filepath cause leet
fmt.Println(fpath)
// Obtain file information for the current path
info, err := os.Stat(fpath)
check(err)
// Check if file is directory (Base Case Check)
if info.IsDir() {
// File is directory, obtain directory contents
dir, err := ioutil.ReadDir(fpath)
check(err)
// Call `sanitizeList` recursively on each listing in current path
for _, f := range dir {
sanitizeList(filepath.Join(fpath, f.Name()), opath, threads)
}
} else {
// File is NOT a directory: Base Case Reached
// Read file into memory
content, err := ioutil.ReadFile(fpath)
check(err)
// Split content of file into array of whitespace separated words
words := strings.Fields(string(content))
// Append file word count to global word count
totalWords += uint64(len(words))
// Create channels for passing strings and queueing work
results := make(chan string)
queue := make(chan string)
// If `threads` is greater than file word count,
// Reduce threads to word count to remove excessive resource allocation
if threads > len(words) {
threads = len(words)
}
// Create Blocking WaitGroup for worker goroutines
// Add number of threads to WaitGroup
var waitGroup sync.WaitGroup
waitGroup.Add(threads)
// Create a goroutine for each "thread"
for i := 0; i < threads; i++ {
go func() {
// Decrease WorkGroup before function exits
defer waitGroup.Done()
// Wait for words from work queue, breaks when `queue` closes
for s := range queue {
// Push word to results if good, otherwise increment global bad word counter
if checkWord(s) {
results <- s
} else {
badCount++
}
}
}()
}
// Lock mutex to prevent parent from exiting prematurely
var mutex sync.Mutex
mutex.Lock()
// Goroutine creating new file and processing results from workers
go func() {
// Unlock mutex when function is finished
defer mutex.Unlock()
// Split filepath into array of directory names
tempPath := fpath
if opath != "." {
tempPath = filepath.Join(opath, fpath)
}
dirs := strings.Split(strings.ReplaceAll(tempPath, "\\", "/"), "/")
// Append -clean to each directory and filename
for i := 0; i < len(dirs); i++ {
if dirs[i] != "." {
dirs[i] = dirs[i] + "-clean"
}
}
// Create the new directory structure
if len(dirs) > 1 {
err := os.MkdirAll(filepath.Join(dirs[:len(dirs)-1]...), os.ModePerm)
check(err)
}
// Create and open the new file
f, err := os.Create(filepath.Join(dirs...))
check(err)
defer f.Close()
// Create buffer for new file
w := bufio.NewWriter(f)
defer w.Flush()
// Wait for words from results channel, and write them to the new file.
// Breaks when `results` closes
for s := range results {
_, err := w.WriteString(s + "\n")
check(err)
}
}()
// Add all words to work queue and then immediately close queue channel
for _, s := range words {
queue <- s
}
close(queue)
// Wait for workers to finish, then close results channel
waitGroup.Wait()
close(results)
// Obtain lock on mutex
// Prevents function from exiting while results are still being processed and file is still open
mutex.Lock()
mutex.Unlock()
}
}
// Entry point
func main() {
// Obtain filepath of executable to find path of default bad words list
ex, err := os.Executable()
check(err)
defaultBadPath := filepath.Join(filepath.Dir(ex), "bad-words.txt")
// Parse command line arguments with `flag` package
var inPath string
flag.StringVar(&inPath, "path", ".", "The path of the target file or directory.\n"+
"May also be passed after all flags as a positional argument.")
var outPath string
flag.StringVar(&outPath, "out", ".", "The output directory.")
var badPath string
flag.StringVar(&badPath, "bad", defaultBadPath, "The list of words to be stripped.")
var threads int
flag.IntVar(&threads, "threads", 100, "Concurrent worker count.")
flag.Parse()
// If extra arguments tail flags, use as `inPath`
if len(flag.Args()) > 0 {
inPath = strings.Join(flag.Args(), " ")
}
// Read bad words into memory
badWordsContent, err := ioutil.ReadFile(badPath)
check(err)
// Split bad words into lowercase whitespace separated array (available globally)
badWords = strings.Fields(string(badWordsContent))
for i := 0; i < len(badWords); i++ {
badWords[i] = strings.ToLower(badWords[i])
}
// Call `sanitizeList`. If the input path is a directory, `sanitizeList` will handle the recursion internally
sanitizeList(inPath, outPath, threads)
// After `sanitizeList` is done, print the number of removed/processed words cause leet
fmt.Printf("%d bad words were removed out of %d words.", badCount, totalWords)
}
// BUY DOGECOIN