-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask11.cpp
More file actions
319 lines (276 loc) · 10.3 KB
/
Task11.cpp
File metadata and controls
319 lines (276 loc) · 10.3 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
#include "Task11.h"
#include "ConsoleMenu.h"
#include <iostream>
#include <sstream>
void Task11::diskList()
{
std::cout << "Drives: " << GetLogicalDrives() << " Bit mask: "<< std::endl;
printBitMask<DWORD>(GetLogicalDrives());
std::cout << std::endl;
std::cout << "Drives' names: " << std::endl;
const uint32_t bSize = 64;
auto buffer = new WCHAR[bSize];
DWORD written = GetLogicalDriveStringsW(bSize, buffer);
for(const std::wstring& name : getParsedNames(buffer, written))
std::wcout << name << std::endl;
delete[] buffer;
}
void Task11::diskInfo()
{
std::wstring driveName;
std::cout << "Drive's name: ";
std::wcin >> driveName;
std::cout << std::endl;
const DWORD driveType = GetDriveTypeW(driveName.c_str());
std::wcout << "Drive type: " << driveTypeCodeToString(driveType) << std::endl;
if(driveType == 0) return;
const DWORD volNameSize = 64, fileSystemNameSize = 64;
auto volNameBuffer = new WCHAR[volNameSize],
fileSystemNameBuffer = new WCHAR[fileSystemNameSize];
DWORD volSerialNumber,
maxComponentLength,
fileSystemFlags;
if(!GetVolumeInformationW(driveName.c_str(), volNameBuffer, volNameSize,
&volSerialNumber, &maxComponentLength, &fileSystemFlags,
fileSystemNameBuffer, fileSystemNameSize))
{
std::cout << "Not all requested fields are retrieved!" << std::endl;
return;
}
std::cout << std::endl;
std::wcout << L"Volume name: " << volNameBuffer << std::endl
<< L"Volume serial number: " << volSerialNumber << std::endl
<< L"Max file name length: " << maxComponentLength << std::endl
<< L"File system flags: " << std::hex << fileSystemFlags << std::dec << std::endl
<< L"File case preserved names: " << bool(fileSystemFlags & FILE_CASE_PRESERVED_NAMES) << std::endl
<< L"File case sensitivesearch: " << bool(fileSystemFlags & FILE_CASE_SENSITIVE_SEARCH) << std::endl
<< L"File compression: " << bool(fileSystemFlags & FILE_FILE_COMPRESSION) << std::endl
<< L"File named streams: " << bool(fileSystemFlags & FILE_NAMED_STREAMS) << std::endl
<< L"Persistent acls: " << bool(fileSystemFlags & FILE_PERSISTENT_ACLS) << std::endl
<< L"Read only: " << bool(fileSystemFlags & FILE_READ_ONLY_VOLUME) << std::endl
<< L"Supports unicode: " << bool(fileSystemFlags & FILE_UNICODE_ON_DISK) << std::endl
<< L"Supports sparse: " << bool(fileSystemFlags & FILE_SUPPORTS_SPARSE_FILES) << std::endl
<< L"Supports transactions: " << bool(fileSystemFlags & FILE_SUPPORTS_TRANSACTIONS) << std::endl
<< L"Compressed: " << bool(fileSystemFlags & FILE_VOLUME_IS_COMPRESSED) << std::endl
<< L"Supports object ids: " << bool(fileSystemFlags & FILE_SUPPORTS_OBJECT_IDS) << std::endl
<< L"Supports open by id: " << bool(fileSystemFlags & FILE_SUPPORTS_OPEN_BY_FILE_ID) << std::endl
<< L"File system name: " << fileSystemNameBuffer << std::endl;
printBitMask(fileSystemFlags);
std::cout << std::endl;
DWORD sectorsPerCluster,
bytesPerSector,
nOfFreeClusters,
totalNOfClusters;
if(!GetDiskFreeSpaceW(driveName.c_str(), §orsPerCluster, &bytesPerSector,
&nOfFreeClusters, &totalNOfClusters))
{
std::cout << "Not all requested info received!" << std::endl;
return;
}
std::cout << std::endl;
std::cout << "Sectors per cluster: " << sectorsPerCluster << std::endl
<< "Bytes per cluster: " << bytesPerSector << std::endl
<< "Number of free clusters: " << nOfFreeClusters << std::endl
<< "Total number of clusters: " << totalNOfClusters << std::endl;
delete[] volNameBuffer;
delete[] fileSystemNameBuffer;
}
void Task11::createRemoveDir()
{
ConsoleMenu<std::string> menu;
menu.addOption("Create directory", [](){
std::wstring dirName;
std::cout << "Directory name to create: ";
std::wcin >> dirName;
if(!CreateDirectoryW(dirName.c_str(), NULL))
std::wcout << "Directory '" << dirName << "' was not created!" << std::endl;
});
menu.addOption("Remove directory", [](){
std::wstring dirName;
std::cout << "Directory name to remove: ";
std::wcin >> dirName;
if(!RemoveDirectoryW(dirName.c_str()))
std::wcout << "Directory '" << dirName << "' was not created!" << std::endl;
});
menu.exec();
}
void Task11::createFile()
{
std::wstring filename;
std::cout << "File name: ";
std::wcin >> filename;
const HANDLE hFile = CreateFileW(filename.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
std::cout << "File has not been created!" << std::endl;
CloseHandle(hFile);
}
void Task11::copyMoveFile()
{
ConsoleMenu<std::string> menu;
menu.addOption("Copy file", [](){
std::wstring existingFilename, newFilename;
std::cout << "File to copy: ";
std::wcin >> existingFilename;
std::cout << "New file: ";
std::wcin >> newFilename;
if(!CopyFileW(existingFilename.c_str(), newFilename.c_str(), TRUE))
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
std::cout << "File is not found!" << std::endl;
else
std::cout << "File with that name already exists" << std::endl;
}
});
menu.addOption("Move file", [](){
moveFileDialog();
});
menu.exec();
}
void Task11::changeFileAttr()
{
ConsoleMenu<std::string> menu;
menu.addOption("Show file attributes: ", fileAttrsDialog);
menu.addOption("Change file attributes: ", setFileAttrsDialog);
menu.exec();
}
std::vector<std::wstring> Task11::getParsedNames(const WCHAR *buffer, const DWORD length)
{
if(!length) return {};
std::vector<std::wstring> result;
size_t curLenght = 0;
while(curLenght < length)
{
result.emplace_back(buffer);
curLenght += wcslen(buffer) + 1;
buffer += wcslen(buffer) + 1;
}
return result;
}
std::wstring Task11::driveTypeCodeToString(const uint32_t code)
{
switch(code)
{
case 0:
return L"Unknown drive type";
case 1:
return L"Root path is invalid";
case 2:
return L"Removable drive";
case 3:
return L"Fixed drive";
case 4:
return L"Remote drive";
case 5:
return L"CD-ROM drive";
case 6:
return L"RAM disk";
default:
return {};
}
}
void Task11::moveFileDialog()
{
std::wstring existingFilename, newFilename;
std::cout << "File to move: ";
std::wcin >> existingFilename;
std::cout << "New filename: ";
std::wcin >> newFilename;
if(!MoveFileW(existingFilename.c_str(), newFilename.c_str()))
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
std::cout << "File is not found!" << std::endl;
else
{
bool prompt = false;
std::cout << "File already exists. Force move? (1/0): ";
std::cin >> prompt;
if(prompt)
MoveFileExW(existingFilename.c_str(), newFilename.c_str(), MOVEFILE_REPLACE_EXISTING);
}
}
}
void Task11::fileAttrsDialog()
{
std::wstring filename;
std::cout << "File: ";
std::wcin >> filename;
std::cout << std::endl;
const HANDLE hFile = CreateFileW(filename.c_str(), NULL, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
BY_HANDLE_FILE_INFORMATION info;
if(!GetFileInformationByHandle(hFile, &info))
{
std::cout << "Cannot get info by handle!";
return;
}
printFileInfo(info);
CloseHandle(hFile);
}
void Task11::setFileAttrsDialog()
{
std::wstring filename;
std::cout << "File: ";
std::wcin >> filename;
std::cout << std::endl;
const HANDLE hFile = CreateFileW(filename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_WRITE_ATTRIBUTES, NULL);
const DWORD attrs = fillFileAttrsDialog();
const std::vector<FILETIME> filetimes = fillFileTimeDialog();
if(!SetFileAttributesW(filename.c_str(), attrs) ||
!SetFileTime(hFile, &filetimes[0], &filetimes[1], &filetimes[2]))
std::cout << "Something went wrong upon setting attributes" << std::endl;
CloseHandle(hFile);
}
void Task11::printDecodedAttributes(const DWORD attrs)
{
std::cout << "Archive: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_ARCHIVE) << std::endl;
std::cout << "Compressed: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_COMPRESSED) << std::endl;
std::cout << "Directory: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_DIRECTORY) << std::endl;
std::cout << "Encrypted: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_ENCRYPTED) << std::endl;
std::cout << "Hidden: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_HIDDEN) << std::endl;
std::cout << "Read only: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_READONLY) << std::endl;
std::cout << "Not fully present locally: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS) << std::endl;
std::cout << "Reparse point: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_REPARSE_POINT) << std::endl;
std::cout << "Sparse: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_SPARSE_FILE) << std::endl;
std::cout << "Temporary: " << static_cast<bool>(attrs & FILE_ATTRIBUTE_TEMPORARY) << std::endl;
}
void Task11::printFileInfo(const BY_HANDLE_FILE_INFORMATION& info)
{
printDecodedAttributes(info.dwFileAttributes);
std::cout << "Volume serial number: " << info.dwVolumeSerialNumber << std::endl;
std::cout << "Number of links: " << info.nNumberOfLinks << std::endl;
std::cout << "High order index: " << info.nFileIndexHigh << std::endl;
std::cout << "Low order index: " << info.nFileIndexLow << std::endl;
std::cout << "Time created: " << getReadableFileTime(info.ftCreationTime).c_str() << std::endl;
std::cout << "Time written: " << getReadableFileTime(info.ftLastWriteTime).c_str() << std::endl;
std::cout << "Time accessed: " << getReadableFileTime(info.ftLastAccessTime).c_str() << std::endl;
}
std::string Task11::getReadableFileTime(const FILETIME& time)
{
SYSTEMTIME sysTime;
FileTimeToSystemTime(&time, &sysTime);
std::stringstream readableTime;
readableTime << sysTime.wDay << "." << sysTime.wMonth << "." << sysTime.wYear << " T "
<< sysTime.wHour << ":" << sysTime.wMinute << ":" << sysTime.wSecond << "." << sysTime.wMilliseconds;
return readableTime.str();
}
const DWORD Task11::fillFileAttrsDialog()
{
DWORD result = 0;
bool answer = false;
std::cout << "Make file hidden? (1/0): ";
std::cin >> answer;
if(answer)
result |= FILE_ATTRIBUTE_HIDDEN;
std::cout << "Make file read only? (1/0): ";
std::cin >> answer;
if(answer)
result |= FILE_ATTRIBUTE_READONLY;
return result;
}
const std::vector<FILETIME> Task11::fillFileTimeDialog()
{
// \todo dates input will be written someday!
return {{0, 1}, {0, 2}, {0, 3}};
}