Repository navigation
Expand file tree
/
Copy pathSearcher.cs
More file actions
268 lines (228 loc) · 8.49 KB
/
Copy pathSearcher.cs
File metadata and controls
268 lines (228 loc) · 8.49 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Threading;
using System.IO;
namespace FileSearcher
{
public class Searcher
{
public delegate void FoundInfoEventHandler(FoundInfoEventArgs e);
public static event FoundInfoEventHandler FoundInfo;
public delegate void ThreadEndedEventHandler(ThreadEndedEventArgs e);
public static event ThreadEndedEventHandler ThreadEnded;
public delegate void StringSearchEndedHandler(EventArgs e);
public static event StringSearchEndedHandler StringSearchEnded;
private static Thread m_thread ;
private static Boolean m_stop;
private static SearcherParams m_pars;
private static Byte[] m_containingBytes;
public static Boolean Start(SearcherParams pars)
{
var success = false;
if (m_thread == null)
{
ResetVariables();
m_pars = pars;
m_thread = new Thread(SearchThread);
m_thread.Start();
success = true;
}
return success;
}
public static void Stop()
{
m_stop = true;
}
private static void ResetVariables()
{
m_thread = null;
m_stop = false;
m_pars = null;
m_containingBytes = null;
}
private static void SearchThread()
{
var success = true;
var errorMsg = "";
// Поиск информации о файле, соответсвующей параметрам
if ((m_pars.SearchDir.Length >= 3) && (Directory.Exists(m_pars.SearchDir)))
{
if (m_pars.FileNames.Count > 0)
{
if (m_pars.ContainingChecked)
{
if (m_pars.ContainingText != "")
{
try
{
m_containingBytes = m_pars.Encoding.GetBytes(m_pars.ContainingText);
}
catch (Exception)
{
success = false;
errorMsg = "The string\r\n" + m_pars.ContainingText + "\r\ncannot be converted into bytes.";
}
}
else
{
success = false;
errorMsg = "The string to search for must not be empty.";
}
}
if (success)
{
// Получение информации о папке
DirectoryInfo dirInfo = null;
try
{
dirInfo = new DirectoryInfo(m_pars.SearchDir);
}
catch (Exception ex)
{
success = false;
errorMsg = ex.Message;
}
if (success)
{
if (FoundInfo != null)
{
FoundInfo(new FoundInfoEventArgs(dirInfo));
}
SearchDirectory(dirInfo);
}
}
}
else
{
success = false;
errorMsg = "Please enter one or more filenames to search for.";
}
}
else
{
success = false;
errorMsg = "The directory\r\n" + m_pars.SearchDir + "\r\ndoes not exist.";
}
m_thread = null;
if (ThreadEnded != null)
{
ThreadEnded(new ThreadEndedEventArgs(success, errorMsg));
}
}
private static void SearchDirectory(DirectoryInfo dirInfo)
{
if (!m_stop)
{
try
{
foreach (var fileName in m_pars.FileNames)
{
var infos = dirInfo.GetFileSystemInfos(fileName);
foreach (var info in infos)
{
if (m_stop)
{
break;
}
if (MatchesRestrictions(info))
{
if (FoundInfo != null)
{
FoundInfo(new FoundInfoEventArgs(info));
}
}
}
}
if (m_pars.IncludeSubDirsChecked)
{
var subDirInfos = dirInfo.GetDirectories();
foreach (var subDirInfo in subDirInfos)
{
if (m_stop)
{
break;
}
SearchDirectory(subDirInfo);
}
}
}
catch (Exception)
{
}
}
}
private static Boolean MatchesRestrictions(FileSystemInfo info)
{
var matches = true;
if (m_pars.ContainingChecked)
{
matches = false;
if (info is FileInfo)
{
matches = FileContainsBytes(info.FullName, m_containingBytes);
}
}
if (StringSearchEnded != null)
{
StringSearchEnded(new EventArgs());
}
return matches;
}
private static Boolean FileContainsBytes(String path, Byte[] compare)
{
var contains = false;
const int blockSize = 4096;
if ((compare.Length >= 1) && (compare.Length <= blockSize))
{
var block = new Byte[compare.Length - 1 + blockSize];
try
{
var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
// Read the first bytes from the file into "block":
var bytesRead = fs.Read(block, 0, block.Length);
do
{
// Search "block" for the sequence "compare":
var endPos = bytesRead - compare.Length + 1;
for (var i = 0; i < endPos; i++)
{
// Read "compare.Length" bytes at position "i" from the buffer,
// and compare them with "compare":
Int32 j;
for (j = 0; j < compare.Length; j++)
{
if (block[i + j] != compare[j])
{
break;
}
}
if (j == compare.Length)
{
// "block" contains the sequence "compare":
contains = true;
break;
}
}
// Search completed?
if (contains || (fs.Position >= fs.Length))
{
break;
}
// Copy the last "compare.Length - 1" bytes to the beginning of "block":
for (var i = 0; i < (compare.Length - 1); i++)
{
block[i] = block[blockSize + i];
}
// Read the next "blockSize" bytes into "block":
bytesRead = compare.Length - 1 + fs.Read(block, compare.Length - 1, blockSize);
}
while (!m_stop);
fs.Close();
}
catch (Exception)
{
}
}
return contains;
}
}
}