-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWin32Directory.cpp
341 lines (281 loc) · 7.09 KB
/
Win32Directory.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
/** This file is part of dirCompare
*
* Copyright 2020 Thomas Erbesdobler <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Win32Directory.h"
#include "gp_exception.h"
#include "win32_error_exception.h"
#include "win32_charset_conversion.h"
#include "platform.h"
#include "win32_charset_conversion.h"
#include "win32_templates.h"
#include <iostream>
#include <algorithm>
extern "C"
{
#include <Windows.h>
#include <AclAPI.h>
#include <AccCtrl.h>
}
using namespace std;
Win32Directory::Win32Directory(const wstring& wpath, shared_ptr<SystemParameters> sp) :
Directory(sp)
{
int pos = wpath.rfind(L'\\');
if (pos == wstring::npos)
{
/* It is a directory name */
this->wname = wpath;
this->wpath = wpath;
}
else
{
if (wpath.size() > 2 && wpath.substr(wpath.size() - 2, 2) == L":\\")
{
/* It is a root directory */
this->wname = L"\\";
this->wpath = wpath;
}
else
{
/* It is a path */
this->wname = wpath.substr(pos + 1LL, wpath.size() - pos - 1);
this->wpath = wpath;
}
}
LPWSTR buf = nullptr;
DWORD len = GetFullPathNameW(this->wpath.c_str(), 0, nullptr, nullptr);
if (len == 0)
{
throw win32_error_exception(GetLastError());
}
buf = new wchar_t[(size_t)len + 1];
if (GetFullPathNameW(this->wpath.c_str(), len + 1, buf, nullptr) == 0)
{
delete[] buf;
throw win32_error_exception(GetLastError());
}
this->wpath = wstring(buf);
this->name = wstring_to_string(this->wname);
this->path = wstring_to_string(this->wpath);
init();
};
Win32Directory::Win32Directory(const wstring& wname, shared_ptr<SystemParameters> sp, shared_ptr<const Directory> dir) :
Directory(sp, dir)
{
this->wname = wname;
auto p = string_to_wstring(dir->getPath());
if (p.size() == 0 || *p.crbegin() != L'\\')
p.push_back(L'\\');
this->wpath = p + this->wname;
this->name = wstring_to_string(this->wname);
this->path = wstring_to_string(this->wpath);
init();
};
void Win32Directory::init()
{
// wcout << L"Directory name: \"" << wname << "\", path: \"" << wpath << L"\"" << endl;
handle = CreateFileW(
wpath.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
throw win32_error_exception(GetLastError());
}
if (!(getAttributes() & FILE_ATTRIBUTE_DIRECTORY))
{
if (!CloseHandle(handle))
{
throw win32_error_exception(
GetLastError(),
L"CloseHandle failed, "
L"closing because file is not a directory: ");
}
handle = INVALID_HANDLE_VALUE;
throw gp_exception("is not a directory");
}
}
Win32Directory::~Win32Directory()
{
if (handle != INVALID_HANDLE_VALUE)
{
if (!CloseHandle(handle))
{
*(sp->getLog()) << endl << "CRITICAL but can't terminate: ~Win32Directory: CloseHandle failed" << endl;
}
}
if (securityInfoValid)
{
LocalFree(pSecurityDescriptor);
}
}
vector<shared_ptr<Item>> Win32Directory::getItems() const
{
vector<shared_ptr<Item>> v;
WIN32_FIND_DATAW fd;
wstring dir_path;
/* "Prepending the string "\\?\" does not allow access to the root directory." - https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew */
if (wpath.size() >= 2 && wpath.substr(wpath.size() - 2, 2) == L":\\")
dir_path = wpath + L"*";
else
dir_path = L"\\\\?\\" + wpath + L"\\*";
// wcout << L"dir search pattern: \"" << dir_path << "\"" << endl;
HANDLE fh = FindFirstFileW(dir_path.c_str(), & fd);
if (fh == INVALID_HANDLE_VALUE)
throw win32_error_exception(GetLastError(), L"Failed to list files in directory \"" + wpath + L"\": ");
auto factory = createItemFactory(sp);
for (;;)
{
wstring name(fd.cFileName);
if (name != L"." && name != L"..")
{
/* The factory will create an invalid file / directory as required. It's
* safe to not care for that here. */
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
v.push_back(factory->createDirectory(wstring_to_string(name), shared_from_this()));
else
v.push_back(factory->createFile(wstring_to_string(name), shared_from_this()));
}
if (!FindNextFileW(fh, &fd))
{
auto e = GetLastError();
FindClose(fh);
if (e == ERROR_NO_MORE_FILES)
break;
else
throw win32_error_exception(e, L"Failed to list files in directory \"" + wpath + L"\": ");
}
}
sort(v.begin(), v.end(), [](shared_ptr<Item> i1, shared_ptr<Item> i2) {
string p1 = i1->getName();
string p2 = i2->getName();
if (p1.compare(p2) < 0)
{
return true;
}
else
{
return false;
}
});
return v;
}
HANDLE Win32Directory::getHandle() const
{
return handle;
}
void Win32Directory::getFileBasicInfo() const
{
if (!fbiValid)
{
memset(&fbi, 0, sizeof fbi);
if (!GetFileInformationByHandleEx(handle, FileBasicInfo, &fbi, sizeof fbi))
{
throw win32_error_exception(GetLastError(), L"Retrieving timestamps: ");
}
}
}
DWORD Win32Directory::getType() const
{
return 0;
}
uint64_t Win32Directory::getSize() const
{
return 0;
}
LARGE_INTEGER Win32Directory::getCreationTime() const
{
getFileBasicInfo();
return fbi.CreationTime;
}
LARGE_INTEGER Win32Directory::getLastAccessTime() const
{
getFileBasicInfo();
return fbi.LastAccessTime;
}
LARGE_INTEGER Win32Directory::getLastWriteTime() const
{
getFileBasicInfo();
return fbi.LastWriteTime;
}
LARGE_INTEGER Win32Directory::getChangeTime() const
{
getFileBasicInfo();
return fbi.ChangeTime;
}
DWORD Win32Directory::getAttributes() const
{
getFileBasicInfo();
return fbi.FileAttributes;
}
void Win32Directory::getSecurityInfo() const
{
if (!securityInfoValid)
{
auto ret = GetSecurityInfo(
handle,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION |
GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION,
&pSidOwner,
&pSidGroup,
&pDacl,
nullptr,
&pSecurityDescriptor);
if (ret != ERROR_SUCCESS)
{
throw win32_error_exception(ret, L"GetSecurityInfo failed: ");
}
securityInfoValid = true;
}
}
const PSID Win32Directory::getOwner() const
{
getSecurityInfo();
return pSidOwner;
}
const PSID Win32Directory::getGroup() const
{
getSecurityInfo();
return pSidGroup;
}
const PACL Win32Directory::getDacl() const
{
getSecurityInfo();
return pDacl;
}
bool Win32Directory::isDaclProtected() const
{
getSecurityInfo();
SECURITY_DESCRIPTOR_CONTROL sdc;
DWORD rev;
if (!GetSecurityDescriptorControl(pSecurityDescriptor, &sdc, &rev))
{
throw win32_error_exception(
GetLastError(),
L"GetSecurityDescriptorControl (DACL) failed: ");
}
return sdc & SE_DACL_PROTECTED;
}
bool Win32Directory::compare_all_attributes_to(const Win32Directory* other, string& reason) const
{
return compare_all_win32_item_attributes(this, other, reason);
}