-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscan.cpp
More file actions
365 lines (306 loc) · 10.5 KB
/
Copy pathscan.cpp
File metadata and controls
365 lines (306 loc) · 10.5 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2023 Christopher Antos
// License: http://opensource.org/licenses/MIT
#include "main.h"
#include "data.h"
#include "scan.h"
#include <shellapi.h>
static void get_drive(const WCHAR* path, std::wstring& out)
{
out.clear();
path += has_io_prefix(path);
if (path[0] && path[1] == ':' && unsigned(towlower(path[0]) - 'a') <= ('z' - 'a'))
{
WCHAR tmp[3] = { towupper(path[0]), ':' };
out = tmp;
}
}
static void capitalize_drive_part(std::wstring& inout)
{
std::wstring out;
const WCHAR* path = inout.c_str();
unsigned int pfxlen = has_io_prefix(path);
out.append(path, pfxlen);
path += pfxlen;
std::wstring drive;
get_drive(path, drive);
out.append(drive.c_str());
path += drive.length();
out.append(path);
inout = std::move(out);
}
std::shared_ptr<DirNode> MakeRoot(const WCHAR* _path)
{
std::wstring path;
if (!_path)
{
const DWORD needed = GetCurrentDirectory(0, nullptr);
if (needed)
{
WCHAR* buffer = new WCHAR[needed];
if (buffer)
{
const DWORD used = GetCurrentDirectory(needed, buffer);
if (used > 0 && used < needed)
get_drive(buffer, path);
delete [] buffer;
}
}
if (path.empty())
path = TEXT(".");
}
else
{
path = _path;
}
if (path.empty())
return nullptr;
// Everyone knows about "*" and "?" wildcards. But Windows actually
// supports FIVE wildcards!
//
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-_fsrtl_advanced_fcb_header-fsrtlisnameinexpression
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-_fsrtl_advanced_fcb_header-fsrtldoesnamecontainwildcards
//
// The ntifs.h header file shows the definitions of DOS_DOT, DOS_QM, and
// DOS_STAR.
//
// Here is the full table of wildcards:
// asterisk * Matches zero or more characters.
// question mark ? Matches a single character.
// DOS_DOT < Matches either a period or zero characters beyond
// the name string.
// DOS_QM > Matches any single character or, upon encountering
// a period or end of name string, advances the
// expression to the end of the set of contiguous
// DOS_QMs ('>' characters).
// DOS_STAR " Matches zero or more characters until encountering
// and matching the final . in the name.
if (wcspbrk(path.c_str(), TEXT("*?<>\"")))
return nullptr;
ensure_separator(path);
const DWORD needed = GetFullPathName(path.c_str(), 0, nullptr, nullptr);
if (needed)
{
WCHAR* buffer = new WCHAR[needed];
if (buffer)
{
const DWORD used = GetFullPathName(path.c_str(), needed, buffer, nullptr);
if (used > 0 && used < needed)
{
path = buffer;
ensure_separator(path);
}
delete [] buffer;
}
}
capitalize_drive_part(path);
std::shared_ptr<DirNode> root;
if (is_drive(path.c_str()))
root = std::make_shared<DriveNode>(path.c_str());
else
root = std::make_shared<DirNode>(path.c_str());
return root;
}
#ifdef DEBUG
void AddColorWheelDir(const std::shared_ptr<DirNode> parent, const WCHAR* name, int depth, ScanContext& context)
{
depth--;
if (!depth)
{
std::lock_guard<std::recursive_mutex> lock(context.mutex);
parent->AddFile(TEXT("x"), 1024);
}
else
{
AddColorWheelDir(parent->AddDir(name), name, depth, context);
}
parent->Finish();
}
static void FakeScan(const std::shared_ptr<DirNode> root, size_t index, bool include_free_space, ScanContext& context)
{
switch (g_fake_data)
{
case FDM_COLORWHEEL:
for (int ii = 0; ii < 360; ii += 10)
{
WCHAR sz[100];
swprintf_s(sz, _countof(sz), TEXT("%u to %u"), ii, ii + 10);
AddColorWheelDir(root, sz, ii ? 10 : 11, context);
}
break;
default:
case FDM_SIMULATED:
{
static const ULONGLONG units = 1024;
std::vector<std::shared_ptr<DirNode>> dirs;
if (include_free_space)
{
DriveNode* drive = root->AsDrive();
dirs.emplace_back(root->AddDir(TEXT("Abc")));
dirs.emplace_back(root->AddDir(TEXT("Def")));
std::lock_guard<std::recursive_mutex> lock(context.mutex);
if (drive)
drive->AddFreeSpace(1000 * units, 2000 * units);
}
else if (root->GetParent() && root->GetParent()->GetParent())
{
return;
}
else
{
std::lock_guard<std::recursive_mutex> lock(context.mutex);
root->AddFile(TEXT("Red"), 4000 * units);
root->AddFile(TEXT("Green"), 8000 * units);
if (index > 0)
{
std::shared_ptr<DirNode> d = root->AddDir(TEXT("Blue"));
d->AddFile(TEXT("Lightning"), 12000 * units);
d->Finish();
}
}
for (size_t ii = 0; ii < dirs.size(); ++ii)
FakeScan(dirs[ii], ii, false, context);
}
break;
case FDM_EMPTYDRIVE:
break;
case FDM_ONLYDIRS:
{
std::lock_guard<std::recursive_mutex> lock(context.mutex);
std::vector<std::shared_ptr<DirNode>> dirs;
dirs.emplace_back(root->AddDir(TEXT("Abc")));
dirs.emplace_back(root->AddDir(TEXT("Def")));
dirs.emplace_back(root->AddDir(TEXT("Ghi")));
for (auto& dir : dirs)
dir->Finish();
}
break;
}
root->Finish();
}
#endif
void Scan(const std::shared_ptr<DirNode>& root, const LONG this_generation, volatile LONG* current_generation, ScanContext& context)
{
if (root->AsRecycleBin())
{
std::lock_guard<std::recursive_mutex> lock(context.mutex);
context.current = root;
root->AsRecycleBin()->UpdateRecycleBin(context.mutex);
root->Finish();
return;
}
DriveNode* drive = (root->AsDrive() && !is_subst(root->GetName())) ? root->AsDrive() : nullptr;
std::wstring find;
root->GetFullPath(find);
ensure_separator(find);
#ifdef DEBUG
if (g_fake_data)
{
const bool was = SetFake(true);
FakeScan(root, 0, true, context);
SetFake(was);
return;
}
#endif
const bool use_compressed_size = context.use_compressed_size;
const size_t base_path_len = find.length();
find.append(TEXT("*"));
std::vector<std::shared_ptr<DirNode>> dirs;
std::wstring test(find);
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(find.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
DWORD tick = GetTickCount();
ULONGLONG num = 0;
do
{
std::lock_guard<std::recursive_mutex> lock(context.mutex);
const bool compressed = (use_compressed_size && (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED));
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
root->SetContainsReparsePoint();
continue;
}
if (!wcscmp(fd.cFileName, TEXT(".")) || !wcscmp(fd.cFileName, TEXT("..")))
continue;
if (drive && !wcsicmp(fd.cFileName, TEXT("$recycle.bin")))
continue;
if (context.dontscan.size())
{
bool match = false;
test.resize(base_path_len);
test.append(fd.cFileName);
ensure_separator(test);
for (const auto& ignore : context.dontscan)
{
match = !wcsicmp(ignore.c_str(), test.c_str());
if (match)
break;
}
if (match)
continue;
}
dirs.emplace_back(root->AddDir(fd.cFileName));
assert(dirs.back());
if (compressed)
dirs.back()->SetCompressed();
if (++num > 50 || GetTickCount() - tick > 50)
{
context.current = dirs.back();
LResetFeedbackInterval:
tick = GetTickCount();
num = 0;
}
}
else
{
ULARGE_INTEGER uli;
if (compressed || (fd.dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE))
{
find.resize(base_path_len);
find.append(fd.cFileName);
uli.LowPart = GetCompressedFileSize(find.c_str(), &uli.HighPart);
}
else
{
uli.HighPart = fd.nFileSizeHigh;
uli.LowPart = fd.nFileSizeLow;
}
std::shared_ptr<FileNode> file = root->AddFile(fd.cFileName, uli.QuadPart);
assert(file);
if (compressed)
file->SetCompressed();
if (fd.dwFileAttributes & FILE_ATTRIBUTE_SPARSE_FILE)
file->SetSparse();
if (++num > 50 || GetTickCount() - tick > 50)
{
context.current = file;
goto LResetFeedbackInterval;
}
}
}
while (this_generation == *current_generation && FindNextFile(hFind, &fd));
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
for (const auto dir : dirs)
{
if (this_generation != *current_generation)
break;
Scan(dir, this_generation, current_generation, context);
}
if (this_generation == *current_generation && drive)
{
drive->AddRecycleBin();
const auto recycle = drive->GetRecycleBin();
if (recycle)
{
context.current = recycle;
recycle->UpdateRecycleBin(context.mutex);
recycle->Finish();
}
}
root->Finish();
}