forked from jamersonpro/ntfsmarkbad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNtfsMarkBad.cpp
215 lines (172 loc) · 4.17 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
// NtfsMarkBad.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "stdafx.h"
#include "common.h"
#include "ulib.hxx"
#include "message.hxx"
#include "system.hxx"
#include "ifssys.hxx"
#include "ntfsvol.hxx"
#include <iostream> // std::cout
#include <string> // std::string, std::stoll
#include <algorithm>
#include <stdlib.h>
#include <errno.h>
BOOLEAN DefineClassDescriptors()
{
return UlibDefineClassDescriptors() && IfsutilDefineClassDescriptors() && UntfsDefineClassDescriptors();
}
unsigned char char_toupper(unsigned char c)
{
return toupper(c);
}
std::string str_toupper(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), char_toupper);
return s;
}
void About()
{
std::cout << "Mark clusters as bad on NTFS\n\nUsage:\nNTFSMARKBAD drive: first_drive_sector_to_mark last_drive_sector_to_mark\n";
};
// convert string to long long
__int64 stoll(const std::string& str)
{
const char* ptr = str.c_str();
char* error_ptr;
__int64 result = _strtoi64(ptr, &error_ptr, 10);
if (ptr == error_ptr)
{
throw std::out_of_range("invalid stoll argument");
}
if (errno == EINVAL)
{
throw std::out_of_range("stoll argument out of range");
}
return result;
}
int __cdecl
main(
ULONG nArgCount,
PSTR arrArguments[]
)
{
MESSAGE Message;
Message.Initialize();
Message.Out("NTFSMARKBAD ", "0.0.1",
#if defined(_M_AMD64)
" x64"
#else
" x32"
#endif
);
Message.Out("");
DefineClassDescriptors();
if (nArgCount != 4)
{
About();
return 1;
}
std::string drive = arrArguments[1];
drive = str_toupper(drive);
if (drive.length() != 2
|| drive[0] < 'A' || drive[0]>'Z'
|| drive[1] != ':')
{
Message.Out("Invalid drive.");
}
std::string firstSectorStr = arrArguments[2];
__int64 firstSector;
try
{
firstSector = ::stoll(firstSectorStr);
}
catch (const std::out_of_range&)
{
firstSector = -1;
}
if (firstSector < 0)
{
Message.Out("Invalid first sector number.");
return 1;
}
std::string lastSectorStr = arrArguments[3];
__int64 lastSector;
try
{
lastSector = ::stoll(lastSectorStr);
}
catch (const std::out_of_range&)
{
lastSector = -1;
}
if (lastSector < 0 || lastSector < firstSector)
{
Message.Out("Invalid last sector number.");
return 1;
}
DSTRING CurrentDrive;
if (!SYSTEM::QueryCurrentDosDriveName(&CurrentDrive))
{
Message.Out("Error.");
return 1;
}
DSTRING InputParamDrive;
InputParamDrive.Initialize(drive.c_str());
DSTRING NtDriveName;
NtDriveName.Initialize("\\??\\");
NtDriveName.Strcat(&InputParamDrive);
if (CurrentDrive == InputParamDrive)
{
Message.Out("Cannot lock current drive.");
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.");
}
else if (Status != STATUS_SUCCESS)
{
Message.Out("Cannot open volume for direct access.");
}
else
{
Message.Out("Cannot determine file system of drive: ", drive.c_str());
}
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(firstSector, lastSector, &Message);
if (!Result)
{
Message.Out("An error has occurred.");
return 1;
}
Message.Out("Completed.");
return 0;
}