-
Notifications
You must be signed in to change notification settings - Fork 128
/
FilesystemWindows.cpp
321 lines (235 loc) · 7.8 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
/*
* Copyright 2011-2019 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::WideString(p.string()));
if(attributes == INVALID_FILE_ATTRIBUTES) {
return DoesNotExist;
} else if(attributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
std::time_t last_write_time(const path & p) {
FILETIME creationTime;
FILETIME accessTime;
FILETIME modificationTime;
HANDLE hFile = CreateFileW(platform::WideString(p.string()), 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) {
// Convert FILETIME to time_t: http://support.microsoft.com/default.aspx?scid=KB;en-us;q167296
LONGLONG value = modificationTime.dwHighDateTime;
value <<= 32;
value |= modificationTime.dwLowDateTime;
value -= 116444736000000000;
writeTime = std::time_t(value / 10000000);
}
::CloseHandle(hFile);
return writeTime;
}
u64 file_size(const path & p) {
HANDLE hFile = CreateFileW(platform::WideString(p.string()), 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(is_directory(p)) {
if(RemoveDirectoryW(platform::WideString(p.string()))) {
return true;
}
} else {
if(DeleteFileW(platform::WideString(p.string()))) {
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 create_directory(const path & p) {
if(p.empty()) {
return true;
}
bool ret = CreateDirectoryW(platform::WideString(p.string()), 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::WideString(p.string()), 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::WideString(from_p.string()), platform::WideString(to_p.string()), !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::WideString(old_p.string()), platform::WideString(new_p.string()), 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), m_buffer(NULL) {
std::string searchPath = (p.empty() ? "." : p.string()) + "\\*";
WIN32_FIND_DATAW * data = new WIN32_FIND_DATAW;
m_handle = FindFirstFileW(platform::WideString(searchPath), data);
if(m_handle != INVALID_HANDLE_VALUE) {
m_buffer = data;
if(!wcscmp(data->cFileName, L".") || !wcscmp(data->cFileName, L"..")) {
operator++();
}
} else {
delete data;
}
}
directory_iterator::~directory_iterator() {
if(m_handle != INVALID_HANDLE_VALUE) {
FindClose(m_handle);
}
delete reinterpret_cast<WIN32_FIND_DATAW *>(m_buffer);
}
directory_iterator & directory_iterator::operator++() {
arx_assert(m_buffer != NULL);
arx_assert(m_handle != INVALID_HANDLE_VALUE);
WIN32_FIND_DATAW * data = reinterpret_cast<WIN32_FIND_DATAW *>(m_buffer);
do {
if(!FindNextFileW(m_handle, data)) {
delete data;
m_buffer = NULL;
break;
}
} while(!wcscmp(data->cFileName, L".") || !wcscmp(data->cFileName, L".."));
return *this;
}
bool directory_iterator::end() {
return !m_buffer;
}
std::string directory_iterator::name() {
arx_assert(m_buffer != NULL);
const WIN32_FIND_DATAW * data = reinterpret_cast<const WIN32_FIND_DATAW *>(m_buffer);
return platform::WideString::toUTF8(data->cFileName);
}
FileType directory_iterator::type() {
arx_assert(m_buffer != NULL);
const WIN32_FIND_DATAW * data = reinterpret_cast<const WIN32_FIND_DATAW *>(m_buffer);
if(data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
return Directory;
} else {
return RegularFile;
}
}
} // namespace fs