forked from jamersonpro/ntfsmarkbad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNtfsMarkBad.cpp
310 lines (263 loc) · 8.09 KB
/
NtfsMarkBad.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
// NtfsMarkBad.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "stdafx.h"
#include <fstream>
#include "common.h"
#include "ulib.hxx"
#include "message.hxx"
#include "system.hxx"
#include "ifssys.hxx"
#include "ntfsvol.hxx"
#include "TextUtils.h"
#define VERSION_TEXT "0.0.2"
BOOLEAN DefineClassDescriptors()
{
return UlibDefineClassDescriptors() && IfsutilDefineClassDescriptors() && UntfsDefineClassDescriptors();
}
void OutputVersion(MESSAGE& Message)
{
Message.Out("NTFSMARKBAD " VERSION_TEXT
#if defined(_M_AMD64)
" x64"
#else
" x32"
#endif
" https://github.com/jamersonpro/ntfsmarkbad"
);
Message.Out("");
}
void OutputAboutBanner(MESSAGE& Message)
{
Message.Out("Mark clusters as bad on NTFS without checking\n"
"\n"
"Basic usage:\n"
"NTFSMARKBAD <drive>: <first_sector_number> <last_sector_number>\n"
"Batch mode:\n"
"NTFSMARKBAD <drive>: /B <sector_numbers_file>\n"
"Info mode:\n"
"NTFSMARKBAD <drive>:\n");
}
int ParseSectorsFile(MESSAGE& Message, const std::string& filename, std::vector<sectors_range>& runTargets)
{
Message.Out("Reading ", filename, "...");
std::ifstream input_file(filename.c_str());
if (input_file.is_open())
{
std::string line;
int lineCounter = 0;
while (std::getline(input_file, line))
{
lineCounter++;
trim(line);
if (line.empty()) continue;
std::vector<std::string> parts = split(line, " \t,;");
if (parts.empty())
{
continue;
}
else if (parts.size() == 1)
{
std::string firstSectorStr = trim(parts[0]);
__int64 firstSector = parse_int64(firstSectorStr);
if (firstSector < 0)
{
Message.Out("Invalid sector number in file, line ", lineCounter, " value ", firstSectorStr);
return 1;
}
if (!runTargets.empty() && runTargets.back().lastSector == firstSector - 1)
{
runTargets.back().lastSector = firstSector;
}
else
{
runTargets.push_back(sectors_range(firstSector, firstSector));
}
}
else if (parts.size() == 2)
{
std::string firstSectorStr = trim(parts[0]);
__int64 firstSector = parse_int64(firstSectorStr);
if (firstSector < 0)
{
Message.Out("Invalid first sector number in file, line ", lineCounter, " value ", firstSectorStr);
return 1;
}
std::string lastSectorStr = trim(parts[1]);
__int64 lastSector = parse_int64(lastSectorStr);
if (lastSector < 0)
{
Message.Out("Invalid last sector number in file with sectors list, line ", lineCounter, " value ", lastSectorStr);
return 1;
}
if (lastSector < firstSector)
{
Message.Out("Last sector number is less than first number in file, line ", lineCounter);
return 1;
}
if (!runTargets.empty() && runTargets.back().lastSector == firstSector - 1)
{
runTargets.back().lastSector = lastSector;
}
else
{
runTargets.push_back(sectors_range(firstSector, lastSector));
}
}
else
{
Message.Out("Wrong line in file, line #", lineCounter, " value ", line);
return 1;
}
}
input_file.close();
}
else
{
Message.Out("Failed to open file with sectors list.");
return 1;
}
if (runTargets.empty())
{
Message.Out("Empty file with sectors list.");
return 1;
}
return 0;
}
int __cdecl
main(
ULONG nArgCount,
PSTR arrArguments[]
)
{
MESSAGE Message;
Message.Initialize();
OutputVersion(Message);
DefineClassDescriptors();
std::vector<sectors_range> runTargets;
std::string runDrive;
if (nArgCount != 2 && nArgCount != 4)
{
OutputAboutBanner(Message);
return 1;
}
runDrive = str_toupper(arrArguments[1]);
if (runDrive.length() != 2
|| runDrive[0] < 'A' || runDrive[0] > 'Z'
|| runDrive[1] != ':')
{
Message.Out("Invalid drive.");
return 1;
}
if (nArgCount == 4)
{
std::string argumentStr2 = arrArguments[2];
std::string argumentStr3 = arrArguments[3];
if (str_toupper(argumentStr2) == "/B") //batch mode
{
if (ParseSectorsFile(Message, argumentStr3, runTargets))
return 1;
}
else //basic mode
{
std::string firstSectorStr = arrArguments[2];
__int64 firstSector = parse_int64(firstSectorStr);
if (firstSector < 0)
{
Message.Out("Invalid first sector number.");
return 1;
}
std::string lastSectorStr = arrArguments[3];
__int64 lastSector = parse_int64(lastSectorStr);
if (lastSector < 0 || lastSector < firstSector)
{
Message.Out("Invalid last sector number.");
return 1;
}
runTargets.push_back(sectors_range(firstSector, lastSector));
}
}
//sort and join sectors ranges
std::sort(runTargets.begin(), runTargets.end());
std::vector<sectors_range> sortedRunTargets;
for (std::vector<sectors_range>::iterator current = runTargets.begin(); current != runTargets.end(); ++current)
{
if (!sortedRunTargets.empty())
{
sectors_range& last = sortedRunTargets.back();
if (last.contains(current->firstSector) || last.lastSector + 1 == current->firstSector)
{
if (!last.contains(current->lastSector))
{
last.lastSector = current->lastSector;
}
continue;
}
}
sortedRunTargets.push_back(*current);
}
runTargets = sortedRunTargets;
DSTRING CurrentDrive;
if (!SYSTEM::QueryCurrentDosDriveName(&CurrentDrive))
{
Message.Out("Error.");
return 1;
}
DSTRING InputParamDrive;
InputParamDrive.Initialize(runDrive.c_str());
DSTRING NtDriveName;
NtDriveName.Initialize("\\??\\");
NtDriveName.Strcat(&InputParamDrive);
if (CurrentDrive == InputParamDrive)
{
Message.Out("Cannot lock current drive. Change current drive and rerun the program.");
return 1;
}
DSTRING ntfs_name;
if (!ntfs_name.Initialize("NTFS"))
{
Message.Out("Error.");
return 1;
}
NTSTATUS Status;
DSTRING drivename;
BOOL FsNameIsNtfs;
if (!IFS_SYSTEM::QueryFileSystemNameIsNtfs(&NtDriveName,
&FsNameIsNtfs,
&Status))
{
if (Status == STATUS_ACCESS_DENIED)
{
Message.Out("Access denied. Run the program as administrator.");
}
else if (Status != STATUS_SUCCESS)
{
Message.Out("Cannot open volume for direct access.");
}
else
{
Message.Out("Cannot determine file system of drive: ", runDrive);
}
return 1;
}
if (!FsNameIsNtfs) //NOT NTFS
{
Message.Out("Only NTFS file system supported.");
return 1;
}
NTFS_VOL NtfsVol;
BOOLEAN Result;
Result = NtfsVol.Initialize(&NtDriveName, &Message);
if (!Result)
{
Message.Out("Failed to initialize.");
return 1;
}
Result = NtfsVol.MarkBad(runTargets, &Message);
if (!Result)
{
Message.Out("An error has occurred.");
return 1;
}
Message.Out("Completed.");
return 0;
}