-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFileSearcher.cs
212 lines (189 loc) · 7.23 KB
/
FileSearcher.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using System.Threading;
namespace SharpSearch
{
public sealed class FileSearcher
{
public readonly string[] FilterExtensions;
public readonly string[] BlockExtensions;
public readonly string[] SearchTerms;
public readonly string FilterPattern;
public readonly string Year;
private ConcurrentQueue<Task<string[]>> _filteredFiles = new ConcurrentQueue<Task<string[]>>();
private ConcurrentQueue<Task> _tasks = new ConcurrentQueue<Task>();
public FileSearcher(string[] filterExtensions = null, string[] blockExtensions = null, string[] searchTerms = null, string filterPattern=null, string year = null)
{
FilterExtensions = filterExtensions == null ? new string[0] : filterExtensions;
BlockExtensions = blockExtensions == null ? new string[0] : blockExtensions;
SearchTerms = searchTerms == null ? new string[0] : searchTerms;
FilterPattern = filterPattern == null ? "*" : filterPattern;
Year = year == null ? "" : year;
}
public string[] Search(string path)
{
Task t = new Task(() =>
{
ParseDirectory(path);
});
t.Start();
_tasks.Enqueue(t);
while(_tasks.TryDequeue(out Task runningT))
{
try
{
runningT.Wait();
} catch (Exception ex)
{
Console.WriteLine($"[-] Error waiting for task: {ex.Message}");
}
}
List<string> results = new List<string>();
while(_filteredFiles.TryDequeue(out Task<string[]> filterTask))
{
if (filterTask == null)
{
continue;
}
try
{
filterTask.Wait();
string[] files = filterTask.Result;
if (files.Length > 0)
{
results.AddRange(files);
}
}
catch (Exception ex)
{
Console.WriteLine($"[-] Error waiting for filter file task: {ex.Message}");
}
}
return results.ToArray();
}
private void ParseDirectory(string dir)
{
try
{
string[] files = Directory.GetFiles(dir, FilterPattern);
Task<string[]> t = ParseFiles(files);
t.Start();
_filteredFiles.Enqueue(t);
_tasks.Enqueue(t);
} catch { }
try
{
string[] dirs = Directory.GetDirectories(dir);
Parallel.ForEach(dirs, sDir =>
{
Task t = new Task(() => { ParseDirectory(sDir); });
_tasks.Enqueue(t);
t.Start();
});
} catch { }
}
private Task<string[]> ParseFiles(string[] files)
{
return new Task<string[]>(() =>
{
List<string> validFiles = new List<string>(files);
Mutex mtx = new Mutex();
if (FilterExtensions.Length > 0)
{
Parallel.ForEach(validFiles.ToArray(), fName =>
{
if (!FileExtensionHandler.EndsWithExtension(fName, FilterExtensions))
{
mtx.WaitOne();
validFiles.Remove(fName);
mtx.ReleaseMutex();
}
});
}
if (BlockExtensions.Length > 0)
{
Parallel.ForEach(validFiles.ToArray(), fName =>
{
if (FileExtensionHandler.EndsWithExtension(fName, BlockExtensions))
{
mtx.WaitOne();
validFiles.Remove(fName);
mtx.ReleaseMutex();
}
});
}
if (!string.IsNullOrEmpty(Year))
{
Parallel.ForEach(validFiles.ToArray(), fName =>
{
FileInfo fInfo = new FileInfo(fName);
string lastWrite = File.GetLastWriteTime(fInfo.FullName).Date.ToString();
if (!lastWrite.Contains(Year))
{
mtx.WaitOne();
validFiles.Remove(fName);
mtx.ReleaseMutex();
}
});
}
if (SearchTerms.Length > 0)
{
Parallel.ForEach(validFiles.ToArray(), fName =>
{
if (FileExtensionHandler.HasCleanExtension(fName) &&
!FileContainsStrings(fName))
{
mtx.WaitOne();
validFiles.Remove(fName);
mtx.ReleaseMutex();
} else if (!FileExtensionHandler.HasCleanExtension(fName))
{
Console.WriteLine($"[-] Removing file {fName} as it cannot be parsed for search terms.");
mtx.WaitOne();
validFiles.Remove(fName);
mtx.ReleaseMutex();
}
});
}
return validFiles.ToArray();
});
}
private bool FileContainsStrings(string path)
{
try
{
var data = File.ReadAllLines(path);
foreach (var s in data)
{
// make sure its not null doesn't start with an empty line or something.
if (s != null && !string.IsNullOrEmpty(s) && !s.StartsWith(" ") && s.Length > 0)
{
string line = s.ToLower().Trim();
// use regex to find some key in your case the "ID".
// look into regex and word boundry find only lines with ID
// double check the below regex below going off memory. \B is for boundry
foreach (string searchTerm in SearchTerms)
{
var regex = new Regex(searchTerm);
var isMatch = regex.Match(s.ToLower());
if (isMatch.Success)
{
return true;
}
}
}
}
}
catch (IOException ex)
{
}
return false;
}
}
}