/
FilesystemWindows.cpp
391 lines (283 loc) · 9.06 KB
/
FilesystemWindows.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* Copyright 2011-2020 Arx Libertatis Team (see the AUTHORS file)
*
* This file is part of Arx Libertatis.
*
* Arx Libertatis is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arx Libertatis is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Arx Libertatis. If not, see <http://www.gnu.org/licenses/>.
*/
#include "io/fs/Filesystem.h"
#include "Configure.h"
#include <vector>
#include <windows.h>
#include <wchar.h>
#include "io/fs/FilePath.h"
#include "io/log/Logger.h"
#include "platform/Thread.h"
#include "platform/WindowsUtils.h"
namespace fs {
FileType get_type(const path & p) {
if(p.empty()) {
return Directory;
}
DWORD attributes = GetFileAttributesW(platform::WinPath(p));
if(attributes == INVALID_FILE_ATTRIBUTES) {
return DoesNotExist;
} else if(attributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
FileType get_link_type(const path & p) {
if(p.empty()) {
return Directory;
}
DWORD attributes = GetFileAttributesW(platform::WinPath(p));
if(attributes == INVALID_FILE_ATTRIBUTES) {
return DoesNotExist;
} else if(attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
return SymbolicLink;
} else if(attributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
static std::time_t filetime_to_time_t(const FILETIME & time) {
// Convert FILETIME to time_t: http://support.microsoft.com/default.aspx?scid=KB;en-us;q167296
LONGLONG value = time.dwHighDateTime;
value <<= 32;
value |= time.dwLowDateTime;
value -= 116444736000000000;
return std::time_t(value / 10000000);
}
std::time_t last_write_time(const path & p) {
FILETIME creationTime;
FILETIME accessTime;
FILETIME modificationTime;
HANDLE hFile = CreateFileW(platform::WinPath(p), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE) {
return 0;
}
std::time_t writeTime = 0;
BOOL res = GetFileTime(hFile, &creationTime, &accessTime, &modificationTime);
if(res) {
writeTime = filetime_to_time_t(modificationTime);
}
::CloseHandle(hFile);
return writeTime;
}
u64 file_size(const path & p) {
HANDLE hFile = CreateFileW(platform::WinPath(p), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE) {
return u64(-1);
}
u64 fileSize = u64(-1);
LARGE_INTEGER retSize;
BOOL res = GetFileSizeEx(hFile, &retSize);
if(res) {
fileSize = retSize.QuadPart;
}
::CloseHandle(hFile);
return fileSize;
}
bool remove(const path & p) {
for(int tries = 1;; tries++) {
if(DeleteFileW(platform::WinPath(p))) {
return true;
}
DWORD error = GetLastError();
if(error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) {
return true;
}
LogWarning << "Failed to remove file " << p << ": " << error << " = "
<< platform::getErrorString(error) << " (try " << tries << ")";
if(tries < 10 && (error == ERROR_ACCESS_DENIED || error == ERROR_SHARING_VIOLATION ||
error == ERROR_LOCK_VIOLATION)) {
Sleep(100);
continue;
}
return false;
}
}
bool remove_directory(const path & p) {
for(int tries = 1;; tries++) {
if(RemoveDirectoryW(platform::WinPath(p))) {
return true;
}
DWORD error = GetLastError();
if(error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) {
return true;
}
if(error == ERROR_DIR_NOT_EMPTY) {
return false;
}
LogWarning << "Failed to remove directory " << p << ": " << error << " = "
<< platform::getErrorString(error) << " (try " << tries << ")";
if(tries < 10 && (error == ERROR_ACCESS_DENIED || error == ERROR_SHARING_VIOLATION ||
error == ERROR_LOCK_VIOLATION)) {
Sleep(100);
continue;
}
return false;
}
}
bool create_directory(const path & p) {
if(p.empty()) {
return true;
}
bool ret = CreateDirectoryW(platform::WinPath(p), NULL) == TRUE;
if(!ret) {
int lastError = GetLastError();
ret = lastError == ERROR_ALREADY_EXISTS;
if(!ret) {
LogWarning << "CreateDirectory(" << p << ", NULL) failed: "
<< platform::getErrorString();
}
}
return ret;
}
static void update_last_write_time(const path & p) {
HANDLE handle = CreateFileW(platform::WinPath(p), GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
if(handle == INVALID_HANDLE_VALUE) {
return;
}
FILETIME filetime;
GetSystemTimeAsFileTime(&filetime);
SetFileTime(handle, &filetime, &filetime, &filetime);
CloseHandle(handle);
}
bool copy_file(const path & from_p, const path & to_p, bool overwrite) {
for(int tries = 1;; tries++) {
if(CopyFileW(platform::WinPath(from_p), platform::WinPath(to_p), !overwrite)) {
update_last_write_time(to_p);
return true;
}
DWORD error = GetLastError();
if(error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND || error == ERROR_FILE_EXISTS) {
return false;
}
LogWarning << "Failed to copy file from " << from_p << " to " << to_p << ": " << error << " = "
<< platform::getErrorString(error) << " (try " << tries << ")";
if(tries < 10 && (error == ERROR_ACCESS_DENIED || error == ERROR_SHARING_VIOLATION ||
error == ERROR_LOCK_VIOLATION)) {
Sleep(100);
continue;
}
return false;
}
}
bool rename(const path & old_p, const path & new_p, bool overwrite) {
DWORD flags = MOVEFILE_COPY_ALLOWED | (overwrite ? MOVEFILE_REPLACE_EXISTING : 0);
for(int tries = 1;; tries++) {
if(MoveFileExW(platform::WinPath(old_p), platform::WinPath(new_p), flags)) {
return true;
}
DWORD error = GetLastError();
if(error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND || error == ERROR_FILE_EXISTS) {
return false;
}
LogWarning << "Failed to move file from " << old_p << " to " << new_p << ": " << error << " = "
<< platform::getErrorString(error) << " (try " << tries << ")";
if(tries < 10 && (error == ERROR_ACCESS_DENIED || error == ERROR_SHARING_VIOLATION ||
error == ERROR_LOCK_VIOLATION)) {
Sleep(100);
continue;
}
return false;
}
}
path current_path() {
platform::WideString buffer;
buffer.allocate(buffer.capacity());
DWORD length = GetCurrentDirectoryW(buffer.size(), buffer.data());
if(length > buffer.size()) {
buffer.allocate(length);
length = GetCurrentDirectoryW(buffer.size(), buffer.data());
}
if(length == 0 || length > buffer.size()) {
return path();
}
buffer.resize(length);
return path(buffer.toUTF8());
}
directory_iterator::directory_iterator(const path & p) : m_handle(INVALID_HANDLE_VALUE) {
platform::WinPath searchPath(p);
if(p.empty()) {
searchPath = L".\\*";
} else {
searchPath.append(L"\\*");
}
m_handle = FindFirstFileExW(searchPath, FindExInfoBasic, &m_data, FindExSearchNameMatch, NULL, 0);
if(m_handle != INVALID_HANDLE_VALUE) {
if(!wcscmp(m_data.cFileName, L".") || !wcscmp(m_data.cFileName, L"..")) {
operator++();
}
}
}
directory_iterator::~directory_iterator() {
if(m_handle != INVALID_HANDLE_VALUE) {
FindClose(m_handle);
}
}
directory_iterator & directory_iterator::operator++() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
do {
if(!FindNextFileW(m_handle, &m_data)) {
CloseHandle(m_handle);
m_handle = INVALID_HANDLE_VALUE;
break;
}
} while(!wcscmp(m_data.cFileName, L".") || !wcscmp(m_data.cFileName, L".."));
return *this;
}
bool directory_iterator::end() {
return m_handle == INVALID_HANDLE_VALUE;
}
std::string directory_iterator::name() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
return platform::WideString::toUTF8(m_data.cFileName);
}
FileType directory_iterator::type() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
if(m_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
FileType directory_iterator::link_type() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
if(m_data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
return SymbolicLink;
} else if(m_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
std::time_t directory_iterator::last_write_time() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
return filetime_to_time_t(m_data.ftLastWriteTime);
}
u64 directory_iterator::file_size() {
arx_assert(m_handle != INVALID_HANDLE_VALUE);
return (u64(m_data.nFileSizeHigh) << 32) + u64(m_data.nFileSizeLow);
}
} // namespace fs