forked from floaterxk/DatDefragAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefragDAT.cpp
359 lines (284 loc) · 7.74 KB
/
DefragDAT.cpp
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
#include "StdAfx.h"
#include "DefragDAT.h"
#include "BTree.h"
DATDefragger* DATDefragger::m_pDefragger = NULL;
void DefragDAT(const char *DATFile, DWORD dwFlags)
{
if (DATDefragger::m_pDefragger)
return;
ClearOutput();
Output("DAT defragmentation initiated.\r\n\r\n");
DATDefragger::Init(DATFile);
}
void DefragCheckup()
{
if (DATDefragger::m_pDefragger)
{
if (DATDefragger::m_pDefragger->Signaled())
{
Output("DAT defragmentation completed.\r\n");
DATDefragger::Release();
}
}
}
void DefragCleanup()
{
if (DATDefragger::m_pDefragger)
{
DATDefragger::m_pDefragger->Terminate();
DATDefragger::Release();
}
}
void DATDefragger::Init(const char *DATFile)
{
m_pDefragger = new DATDefragger(DATFile);
m_pDefragger->Run();
}
void DATDefragger::Release()
{
if (m_pDefragger)
{
delete m_pDefragger;
m_pDefragger = NULL;
}
}
DATDefragger::DATDefragger(const char* DATFile)
{
OpenSourceFile(DATFile);
CreateTempFile();
ZeroMemory(&m_Header, sizeof(m_Header));
m_pBTree = NULL;
}
DATDefragger::~DATDefragger()
{
if (m_pBTree)
delete m_pBTree;
CloseFileHandles();
DeleteTempFileIfExists();
if (m_SourceFilePath)
free(m_SourceFilePath);
if (m_TempFilePath)
free(m_TempFilePath);
}
void DATDefragger::CloseFileHandles()
{
if (m_hSourceFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hSourceFile);
m_hSourceFile = INVALID_HANDLE_VALUE;
}
if (m_hTempFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hTempFile);
m_hTempFile = INVALID_HANDLE_VALUE;
}
}
void DATDefragger::OpenSourceFile(const char *DATFile)
{
m_SourceFilePath = _strdup(DATFile);
m_hSourceFile = CreateFile(m_SourceFilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (m_hSourceFile == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Failed opening source file!", "Catastrophic error", MB_ICONHAND | MB_OK);
Output("Failed opening source file!\r\n");
}
}
void DATDefragger::CreateTempFile()
{
char TempFilePath[MAX_PATH];
const char *TempFolder = GetDefaultDATFolder();
if (!PathFileExists(TempFolder))
TempFolder = ".";
UINT uid = GetTempFileName(TempFolder, "dfrag", 0, TempFilePath);
if (!uid)
{
MessageBox(NULL, "Failed creating temporary file!", "Catastrophic error", MB_ICONHAND | MB_OK);
Output("Failed creating temporary file!\r\n");
m_hTempFile = INVALID_HANDLE_VALUE;
}
else
{
m_TempFilePath = _strdup(TempFilePath);
m_hTempFile = CreateFile(TempFilePath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
}
}
void DATDefragger::DeleteTempFileIfExists()
{
if (PathFileExists(m_TempFilePath))
DeleteFile(m_TempFilePath);
}
BOOL DATDefragger::Work()
{
if (!m_hTempFile || !m_hSourceFile)
return FALSE;
m_BlockLoader.Init(m_hSourceFile, m_hTempFile);
if (!LoadHeader())
return FALSE;
if (!LoadBTree())
return FALSE;
DWORD FilesOffset;
if (!SortBTreeData(&FilesOffset))
return FALSE;
if (!WriteBlankTempFile())
return FALSE;
if (!WriteFileData(FilesOffset))
return FALSE;
if (!WriteBTreeData())
return FALSE;
if (!WriteEmptyBlocks())
return FALSE;
if (!WriteHeaderData())
return FALSE;
if (!FinishDefrag())
return FALSE;
return TRUE;
}
BOOL DATDefragger::FinishDefrag()
{
Output("========================================\r\n");
Output("Finish up defragmentation..\r\n");
CloseFileHandles();
char BackupFile[MAX_PATH + 20];
strncpy(BackupFile, m_SourceFilePath, MAX_PATH);
strcat(BackupFile, "-backup");
if (MoveFileEx(m_SourceFilePath, BackupFile, 0) || (GetLastError() == 183))
{
if (MoveFileEx(m_TempFilePath, m_SourceFilePath, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
{
}
else
{
Output("Failed renaming of temporary file, please restore your backup..\r\n");
goto error_exit;
}
}
else
{
Output("Failed backing up original data, aborting operation..\r\n");
goto error_exit;
}
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("========================================\r\n\r\n");
return FALSE;
}
BOOL DATDefragger::LoadHeader()
{
return m_BlockLoader.LoadHeader(&m_Header);
}
BOOL DATDefragger::LoadBTree()
{
BOOL bResult;
InitProgressBar(0, 200, (COLORREF)0x00AA0000);
Output("========================================\r\n");
Output("Loading BTree data.. please wait.\r\n");
m_pBTree = new BTree(&m_BlockLoader);
if (bResult = m_pBTree->Load(m_Header.BTree))
{
DWORD TotalBranches = m_pBTree->GetTotalBranches();
DWORD TotalEntries = m_pBTree->GetTotalEntries();
Output("Total Branches: %u\r\n", TotalBranches);
Output("Total Entries: %u\r\n", TotalEntries);
}
else
Output("Error loading BTree data!\r\n");
return TRUE;
}
BOOL DATDefragger::WriteBlankTempFile()
{
m_dwNewFileSize = m_Header.FileSize;
if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hTempFile, 0, 0, FILE_BEGIN))
return FALSE;
BYTE BlankHeader[1024];
ZeroMemory(BlankHeader, 1024);
DWORD dwBytesWritten;
WriteFile(m_hTempFile, BlankHeader, 1024, &dwBytesWritten, FALSE);
if (dwBytesWritten != 1024)
return FALSE;
return TRUE;
}
BOOL DATDefragger::SortBTreeData(DWORD *pdwFilesOffset)
{
Output("\r\nSorting BTree data..\r\n");
DWORD dwOffset = 1024;
if (!m_pBTree->AssignNodePositions(&dwOffset))
goto error_exit;
*pdwFilesOffset = dwOffset;
Output("BTree size: %u bytes\r\n", dwOffset - 1024);
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("Error while sorting BTree data!\n");
Output("========================================\r\n\r\n");
return FALSE;
}
BOOL DATDefragger::WriteFileData(DWORD dwOffset)
{
Output("========================================\r\n");
Output("Defragmenting file entries..\r\n");
m_BlockLoader.SetTargetBlockOffset(dwOffset);
InitProgressBar(0, 200, (COLORREF)0x000000FF);
if (!m_pBTree->CopyFiles())
goto error_exit;
m_dwNewFreeHead = m_BlockLoader.GetTargetBlockOffset();
#if _DEBUG
Output("DEBUG: Free blocks will start at %u\r\n", m_dwNewFreeHead);
#endif
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("Error while defragmenting!\r\n");
Output("========================================\r\n\r\n");
return FALSE;
}
BOOL DATDefragger::WriteBTreeData()
{
Output("========================================\r\n");
Output("Defragmenting BTree data..\r\n");
m_BlockLoader.SetTargetBlockOffset(1024);
InitProgressBar(0, 200, (COLORREF)0x008000FF);
if (!m_pBTree->CopyTreeData())
goto error_exit;
#if _DEBUG
Output("DEBUG: End offset %u\r\n", m_BlockLoader.GetTargetBlockOffset());
#endif
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("Error while defragmenting!\r\n");
Output("========================================\r\n\r\n");
return FALSE;
}
BOOL DATDefragger::WriteEmptyBlocks()
{
Output("========================================\r\n");
Output("Writing empty blocks..\r\n");
if (!m_BlockLoader.WriteEmptyBlocks(m_dwNewFreeHead, &m_dwNewFreeTail, &m_dwNewFreeCount, m_dwNewFileSize))
goto error_exit;
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("Error writing empty blocks!\r\n");
Output("========================================\r\n\r\n");
return FALSE;
}
BOOL DATDefragger::WriteHeaderData()
{
Output("========================================\r\n");
Output("Writing header data..\r\n");
DATHeader NewHeader = m_Header;
NewHeader.FileSize = m_dwNewFileSize;
NewHeader.FreeHead = m_dwNewFreeHead;
NewHeader.FreeTail = m_dwNewFreeTail;
NewHeader.FreeCount = m_dwNewFreeCount;
NewHeader.BTree = 1024;
if (!m_BlockLoader.WriteTempData(&NewHeader, sizeof(DATHeader), 0x140))
goto error_exit;
Output("========================================\r\n\r\n");
return TRUE;
error_exit:
Output("Error writing header!\r\n");
Output("========================================\r\n\r\n");
return FALSE;
}