forked from seanofw/spacemonger1
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFolderTree.cpp
More file actions
345 lines (295 loc) · 8.93 KB
/
Copy pathFolderTree.cpp
File metadata and controls
345 lines (295 loc) · 8.93 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
#include "stdafx.h"
#include "spacemonger.h"
#include "FolderTree.h"
#include "FolderView.h"
#include "AsyncScanEngine.h"
#include "PathUtil.h"
#include "Lang.h"
#include <cmath>
CFolderTree::CFolderTree()
{
root = cur = NULL;
freespace = usedspace = totalspace = 0;
m_path = "";
}
CFolderTree::~CFolderTree()
{
if (root != NULL) delete root;
}
BOOL CFolderTree::LoadTree(const CString &path, BOOL includespace, CWnd *modalwin)
{
CFolderDialog dialog;
if (path == "") return 1;
dialog.Create(IDD_SCAN_DIALOG, modalwin);
dialog.Reset();
m_path = path;
GetSpace(path);
if (root != NULL) { delete root; root = NULL; }
nameArena.Reset();
cur = NULL;
filespace = 0;
std::wstring widePath = PathUtil::AnsiToWide((LPCTSTR)path);
std::wstring absPath = PathUtil::GetAbsolutePath(widePath);
absPath = PathUtil::EnsureTrailingBackslash(absPath);
std::wstring preparedPath = PathUtil::PrepareLongPath(absPath);
preparedPath = PathUtil::EnsureTrailingBackslash(preparedPath);
BOOL aligned = (clustersize != 0 && (clustersize & (clustersize - 1)) == 0);
AsyncScanEngine engine;
if (!engine.StartScan(preparedPath, clustersize - 1, aligned)) {
dialog.DestroyWindow();
return 0;
}
// Pumps pending messages; returns 0 if WM_QUIT arrived (scan is torn down).
auto pumpMessages = [&]() -> BOOL {
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
engine.Cancel();
engine.WaitForCompletion();
dialog.DestroyWindow();
::PostQuitMessage((int)msg.wParam);
return 0;
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return 1;
};
DWORD last_ui_tick = 0;
DWORD last_render_tick = 0;
ui64 last_render_bytes = (ui64)-1;
ui64 last_render_entries = (ui64)-1;
while (engine.IsScanning()) {
if (!pumpMessages()) return 0;
if (dialog.cancelled) {
engine.Cancel();
break;
}
DWORD now = ::GetTickCount();
if (now - last_ui_tick >= 50) {
last_ui_tick = now;
dialog.UpdateFromProgress(engine.GetProgress(), usedspace);
}
if (now - last_render_tick >= 250) {
last_render_tick = now;
CFolderView *fv = (CFolderView *)theApp.m_view;
if (fv != NULL && ::IsWindow(fv->m_hWnd)) {
// Re-walking and re-laying-out the tree stalls the workers
// (it holds the tree lock), so skip it when nothing changed.
ScanProgress progress = engine.GetProgress();
ui64 entries = progress.numFiles + progress.numFolders;
if (progress.bytesScanned != last_render_bytes || entries != last_render_entries) {
last_render_bytes = progress.bytesScanned;
last_render_entries = entries;
fv->UpdateLiveScanLayout(engine, totalspace, freespace);
}
}
}
::Sleep(15);
}
// Keep pumping while the workers wind down so the UI stays responsive even
// if one of them is stuck in a slow filesystem call.
engine.RequestStop();
while (!engine.IsFinished()) {
if (!pumpMessages()) return 0;
if (dialog.cancelled) engine.Cancel();
::Sleep(10);
}
engine.WaitForCompletion();
ScanProgress resultProgress = engine.GetProgress();
if (dialog.cancelled || engine.IsCancelled() || resultProgress.isFailed) {
// The view may still show the last live-scan frame; drop it so a
// dead scan's half-built map doesn't linger on screen.
CFolderView *fv = (CFolderView *)theApp.m_view;
if (fv != NULL && ::IsWindow(fv->m_hWnd)) {
fv->ClearLiveScanLayout();
}
if (root != NULL) { delete root; root = NULL; }
nameArena.Reset();
root = cur = NULL;
freespace = usedspace = totalspace = 0;
m_path = "";
dialog.DestroyWindow();
if (resultProgress.isFailed) {
CString message;
message.Format(CurLang->scan_failed_format, resultProgress.firstError);
AfxMessageBox(message, MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}
root = engine.DetachResult(nameArena);
if (root == NULL) {
nameArena.Reset();
root = cur = NULL;
freespace = usedspace = totalspace = 0;
m_path = "";
dialog.DestroyWindow();
return 0;
}
cur = root;
ScanProgress finalProgress = engine.GetProgress();
numfiles = finalProgress.numFiles;
numfolders = finalProgress.numFolders;
filespace = finalProgress.bytesScanned;
dialog.UpdateFromProgress(finalProgress, usedspace);
if (includespace
&& !root->AddFileWithArena(nameArena, L"<<<<<<<<<<<<<<<<<<<<", 1, freespace, freespace, 0)) {
delete root;
nameArena.Reset();
root = cur = NULL;
freespace = usedspace = totalspace = 0;
m_path = "";
dialog.DestroyWindow();
return 0;
}
root->Finalize();
dialog.DestroyWindow();
if (resultProgress.isPartial) {
CString message;
message.Format(CurLang->scan_partial_format,
resultProgress.skippedDirectories, resultProgress.firstError);
AfxMessageBox(message, MB_OK | MB_ICONEXCLAMATION);
}
if (modalwin != NULL && ::IsWindow(modalwin->m_hWnd)) {
modalwin->SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
}
return 1;
}
void CFolderTree::GetSpace(const CString &path)
{
DWORD SecPerClus = 0, BytesPerSec = 0, ClusPerDisk = 0, FreeClus = 0;
ui64 oddfree = 0, total = 0, totalfree = 0;
BOOL gotclusters;
typedef BOOL (WINAPI *GetFreeDiskSpaceExFunc)(LPCTSTR pathname,
ui64 *oddfree, ui64 *total, ui64 *totalfree);
// First, compute the cluster size (which will be needed later)
gotclusters = GetDiskFreeSpace(path, &SecPerClus, &BytesPerSec, &FreeClus, &ClusPerDisk);
clustersize = gotclusters ? (ui64)BytesPerSec * (ui64)SecPerClus : 1;
// Next, load in Kernel32 and use GetDiskFreeSpaceEx to find out
// the size of the disk. If GetDiskFreeSpaceEx doesn't exist, then
// fall back on the values from GetDiskFreeSpace.
HINSTANCE hLibrary = LoadLibrary("KERNEL32.DLL");
GetFreeDiskSpaceExFunc getfreediskspaceex =
hLibrary == NULL ? NULL : (GetFreeDiskSpaceExFunc)GetProcAddress(hLibrary, "GetDiskFreeSpaceExA");
if (getfreediskspaceex != NULL && getfreediskspaceex(path, &oddfree, &total, &totalfree)) {
freespace = totalfree;
totalspace = total;
}
else if (gotclusters) {
freespace = clustersize * (ui64)FreeClus;
totalspace = clustersize * (ui64)ClusPerDisk;
}
else {
freespace = totalspace = 0;
}
if (hLibrary != NULL) FreeLibrary(hLibrary);
usedspace = totalspace - freespace;
}
CFolder *CFolderTree::GetRoot(void)
{
return(root);
}
CFolder *CFolderTree::SetCur(const CString &path)
{
return(cur);
}
CFolder *CFolderTree::GetCur(void)
{
return(cur);
}
CFolder *CFolderTree::Down(unsigned int index)
{
if (index < cur->cur) {
CFolder *newfolder = cur->children[index];
if (newfolder != NULL) {
cur = newfolder;
return(cur);
}
}
return(NULL);
}
CFolder *CFolderTree::Up(void)
{
if (cur != root) {
cur = cur->parent;
return(cur);
}
return(NULL);
}
//////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CFolderDialog, CDialog)
BEGIN_MESSAGE_MAP(CFolderDialog, CDialog)
//{{AFX_MSG_MAP(CFolderDialog)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CFolderDialog::CFolderDialog()
{
cur_frame = 0;
cancelled = 0;
}
CFolderDialog::~CFolderDialog()
{
}
void CFolderDialog::OnCancel(void)
{
cancelled = 1;
CDialog::OnCancel();
}
void CFolderDialog::Reset(void)
{
if (!IsWindow(m_hWnd)) return;
cur_frame = 0;
cancelled = 0;
SetWindowText(CurLang->scanning);
SetDlgItemText(IDC_STATIC_FILESFOUND, CurLang->files_found);
SetDlgItemText(IDC_STATIC_FOLDERSFOUND, CurLang->folders_found);
SetDlgItemText(IDCANCEL, CurLang->cancel);
SetDlgItemText(IDC_PATH, "");
SetDlgItemInt(IDC_FILES, 0);
SetDlgItemInt(IDC_FOLDERS, 0);
CProgressCtrl *progress = (CProgressCtrl *)GetDlgItem(IDC_LOAD_PROGRESS);
if (progress != NULL) {
progress->SetRange(0, 4096);
progress->SetPos(0);
}
DrawScanAnimation(0);
}
void CFolderDialog::DrawScanAnimation(ui32 frame)
{
CClientDC dc(this);
CDC srcdc;
srcdc.CreateCompatibleDC(&dc);
::SelectObject(srcdc.m_hDC, theApp.m_scan_animation[frame]);
CRect rect;
GetClientRect(&rect);
dc.BitBlt(8, rect.bottom - rect.top - (48 + 8) - 12, 128, 48, &srcdc, 0, 0, SRCCOPY);
}
void CFolderDialog::UpdateFromProgress(const ScanProgress& progress, ui64 usedspace)
{
if (!IsWindow(m_hWnd)) return;
std::string ansiPath = PathUtil::WideToAnsi(PathUtil::RemoveLongPathPrefix(progress.currentPath));
SetDlgItemText(IDC_PATH, ansiPath.c_str());
SetDlgItemInt(IDC_FILES, (UINT)progress.numFiles);
SetDlgItemInt(IDC_FOLDERS, (UINT)progress.numFolders);
CProgressCtrl *ctrl = (CProgressCtrl *)GetDlgItem(IDC_LOAD_PROGRESS);
if (ctrl != NULL) {
int pos = 0;
if (progress.isComplete) {
pos = 4096;
} else if (usedspace > 0 && progress.bytesScanned > 0) {
double ratio = (double)progress.bytesScanned / (double)usedspace;
if (ratio > 1.0) ratio = 1.0;
if (ratio < 0.0) ratio = 0.0;
// Use a perceptual power curve (ratio^1.5) to prevent front-loaded large files
// from prematurely filling the progress bar before directory walking completes
double smoothed = pow(ratio, 1.5);
pos = (int)(smoothed * 4000.0);
if (pos > 4000) pos = 4000;
}
ctrl->SetPos(pos);
}
cur_frame = (cur_frame + 1) & 7;
if (!(cur_frame & 1)) {
DrawScanAnimation(cur_frame >> 1);
}
}