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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
Mod Organizer archive handling
Copyright (C) 2020 MO2 Team. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fileio.h"
#ifdef _WIN32
inline bool BOOLToBool(BOOL v)
{
return (v != FALSE);
}
namespace IO
{
// FileBase
bool FileBase::Close() noexcept
{
if (m_Handle == INVALID_HANDLE_VALUE)
return true;
if (!::CloseHandle(m_Handle))
return false;
m_Handle = INVALID_HANDLE_VALUE;
return true;
}
bool FileBase::GetPosition(UInt64& position) noexcept
{
return Seek(0, FILE_CURRENT, position);
}
bool FileBase::GetLength(UInt64& length) const noexcept
{
DWORD sizeHigh;
DWORD sizeLow = ::GetFileSize(m_Handle, &sizeHigh);
if (sizeLow == 0xFFFFFFFF)
if (::GetLastError() != NO_ERROR)
return false;
length = (((UInt64)sizeHigh) << 32) + sizeLow;
return true;
}
bool FileBase::Seek(Int64 distanceToMove, DWORD moveMethod,
UInt64& newPosition) noexcept
{
LONG high = (LONG)(distanceToMove >> 32);
DWORD low = ::SetFilePointer(m_Handle, (LONG)(distanceToMove & 0xFFFFFFFF), &high,
moveMethod);
if (low == 0xFFFFFFFF)
if (::GetLastError() != NO_ERROR)
return false;
newPosition = (((UInt64)(UInt32)high) << 32) + low;
return true;
}
bool FileBase::Seek(UInt64 position, UInt64& newPosition) noexcept
{
return Seek(position, FILE_BEGIN, newPosition);
}
bool FileBase::SeekToBegin() noexcept
{
UInt64 newPosition;
return Seek(0, newPosition);
}
bool FileBase::SeekToEnd(UInt64& newPosition) noexcept
{
return Seek(0, FILE_END, newPosition);
}
bool FileBase::Create(std::filesystem::path const& path, DWORD desiredAccess,
DWORD shareMode, DWORD creationDisposition,
DWORD flagsAndAttributes) noexcept
{
if (!Close()) {
return false;
}
m_Handle =
::CreateFileW(path.c_str(), desiredAccess, shareMode, (LPSECURITY_ATTRIBUTES)NULL,
creationDisposition, flagsAndAttributes, (HANDLE)NULL);
return m_Handle != INVALID_HANDLE_VALUE;
}
bool FileBase::GetFileInformation(std::filesystem::path const& path,
FileInfo* info) noexcept
{
// Use FileBase to open/close the file:
FileBase file;
if (!file.Create(path, 0, FILE_SHARE_READ, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS))
return false;
BY_HANDLE_FILE_INFORMATION finfo;
if (!BOOLToBool(GetFileInformationByHandle(file.m_Handle, &finfo))) {
return false;
}
*info = FileInfo(path, finfo);
return true;
}
// FileIn
bool FileIn::Open(std::filesystem::path const& filepath, DWORD shareMode,
DWORD creationDisposition, DWORD flagsAndAttributes) noexcept
{
bool res = Create(filepath.c_str(), GENERIC_READ, shareMode, creationDisposition,
flagsAndAttributes);
return res;
}
bool FileIn::OpenShared(std::filesystem::path const& filepath,
bool shareForWrite) noexcept
{
return Open(filepath, FILE_SHARE_READ | (shareForWrite ? FILE_SHARE_WRITE : 0),
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
}
bool FileIn::Open(std::filesystem::path const& filepath) noexcept
{
return OpenShared(filepath, false);
}
bool FileIn::Read(void* data, UInt32 size, UInt32& processedSize) noexcept
{
processedSize = 0;
do {
UInt32 processedLoc = 0;
bool res = ReadPart(data, size, processedLoc);
processedSize += processedLoc;
if (!res)
return false;
if (processedLoc == 0)
return true;
data = (void*)((unsigned char*)data + processedLoc);
size -= processedLoc;
} while (size > 0);
return true;
}
bool FileIn::Read1(void* data, UInt32 size, UInt32& processedSize) noexcept
{
DWORD processedLoc = 0;
bool res = BOOLToBool(::ReadFile(m_Handle, data, size, &processedLoc, NULL));
processedSize = (UInt32)processedLoc;
return res;
}
bool FileIn::ReadPart(void* data, UInt32 size, UInt32& processedSize) noexcept
{
if (size > kChunkSizeMax)
size = kChunkSizeMax;
return Read1(data, size, processedSize);
}
// FileOut
bool FileOut::Open(std::filesystem::path const& fileName, DWORD shareMode,
DWORD creationDisposition, DWORD flagsAndAttributes) noexcept
{
return Create(fileName, GENERIC_WRITE, shareMode, creationDisposition,
flagsAndAttributes);
}
bool FileOut::Open(std::filesystem::path const& fileName) noexcept
{
return Open(fileName, FILE_SHARE_READ, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
}
bool FileOut::SetTime(const FILETIME* cTime, const FILETIME* aTime,
const FILETIME* mTime) noexcept
{
return BOOLToBool(::SetFileTime(m_Handle, cTime, aTime, mTime));
}
bool FileOut::SetMTime(const FILETIME* mTime) noexcept
{
return SetTime(NULL, NULL, mTime);
}
bool FileOut::Write(const void* data, UInt32 size, UInt32& processedSize) noexcept
{
processedSize = 0;
do {
UInt32 processedLoc = 0;
bool res = WritePart(data, size, processedLoc);
processedSize += processedLoc;
if (!res)
return false;
if (processedLoc == 0)
return true;
data = (const void*)((const unsigned char*)data + processedLoc);
size -= processedLoc;
} while (size > 0);
return true;
}
bool FileOut::SetLength(UInt64 length) noexcept
{
UInt64 newPosition;
if (!Seek(length, newPosition))
return false;
if (newPosition != length)
return false;
return SetEndOfFile();
}
bool FileOut::SetEndOfFile() noexcept
{
return BOOLToBool(::SetEndOfFile(m_Handle));
}
bool FileOut::WritePart(const void* data, UInt32 size, UInt32& processedSize) noexcept
{
if (size > kChunkSizeMax)
size = kChunkSizeMax;
DWORD processedLoc = 0;
bool res = BOOLToBool(::WriteFile(m_Handle, data, size, &processedLoc, NULL));
processedSize = (UInt32)processedLoc;
return res;
}
} // namespace IO
#else // Linux
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <cstring>
#include <cerrno>
namespace IO
{
// FileBase
bool FileBase::Close() noexcept
{
if (m_Fd == -1)
return true;
if (::close(m_Fd) != 0)
return false;
m_Fd = -1;
return true;
}
bool FileBase::GetPosition(UInt64& position) noexcept
{
return Seek(0, SEEK_CUR, position);
}
bool FileBase::GetLength(UInt64& length) const noexcept
{
struct stat st;
if (fstat(m_Fd, &st) != 0)
return false;
length = (UInt64)st.st_size;
return true;
}
bool FileBase::Seek(Int64 distanceToMove, int whence, UInt64& newPosition) noexcept
{
off_t result = ::lseek(m_Fd, (off_t)distanceToMove, whence);
if (result == (off_t)-1)
return false;
newPosition = (UInt64)result;
return true;
}
bool FileBase::Seek(UInt64 position, UInt64& newPosition) noexcept
{
return Seek((Int64)position, SEEK_SET, newPosition);
}
bool FileBase::SeekToBegin() noexcept
{
UInt64 newPosition;
return Seek(0, newPosition);
}
bool FileBase::SeekToEnd(UInt64& newPosition) noexcept
{
return Seek(0, SEEK_END, newPosition);
}
bool FileBase::Create(std::filesystem::path const& path, int flags, int mode) noexcept
{
if (!Close()) {
return false;
}
m_Fd = ::open(path.c_str(), flags, mode);
return m_Fd != -1;
}
bool FileBase::GetFileInformation(std::filesystem::path const& path,
FileInfo* info) noexcept
{
struct stat st;
if (::stat(path.c_str(), &st) != 0) {
return false;
}
*info = FileInfo(path, st);
return true;
}
// FileIn
bool FileIn::Open(std::filesystem::path const& filepath) noexcept
{
return Create(filepath, O_RDONLY);
}
bool FileIn::Read(void* data, UInt32 size, UInt32& processedSize) noexcept
{
processedSize = 0;
do {
UInt32 processedLoc = 0;
bool res = ReadPart(data, size, processedLoc);
processedSize += processedLoc;
if (!res)
return false;
if (processedLoc == 0)
return true;
data = (void*)((unsigned char*)data + processedLoc);
size -= processedLoc;
} while (size > 0);
return true;
}
bool FileIn::Read1(void* data, UInt32 size, UInt32& processedSize) noexcept
{
ssize_t result = ::read(m_Fd, data, size);
if (result < 0) {
processedSize = 0;
return false;
}
processedSize = (UInt32)result;
return true;
}
bool FileIn::ReadPart(void* data, UInt32 size, UInt32& processedSize) noexcept
{
if (size > kChunkSizeMax)
size = kChunkSizeMax;
return Read1(data, size, processedSize);
}
// FileOut
bool FileOut::Open(std::filesystem::path const& fileName) noexcept
{
return Create(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
bool FileOut::SetTime(const FILETIME* /*cTime*/, const FILETIME* /*aTime*/,
const FILETIME* mTime) noexcept
{
return SetMTime(mTime);
}
bool FileOut::SetMTime(const FILETIME* mTime) noexcept
{
if (!mTime) return true;
// Convert FILETIME to timespec
// FILETIME is 100ns intervals since 1601-01-01
// Unix time is seconds since 1970-01-01
constexpr UInt64 EPOCH_DIFF = 116444736000000000ULL;
UInt64 ticks = ((UInt64)mTime->dwHighDateTime << 32) | mTime->dwLowDateTime;
if (ticks < EPOCH_DIFF) return false;
ticks -= EPOCH_DIFF;
struct timespec times[2];
// atime - keep current
times[0].tv_sec = 0;
times[0].tv_nsec = UTIME_OMIT;
// mtime
times[1].tv_sec = (time_t)(ticks / 10000000ULL);
times[1].tv_nsec = (long)((ticks % 10000000ULL) * 100);
return futimens(m_Fd, times) == 0;
}
bool FileOut::Write(const void* data, UInt32 size, UInt32& processedSize) noexcept
{
processedSize = 0;
do {
UInt32 processedLoc = 0;
bool res = WritePart(data, size, processedLoc);
processedSize += processedLoc;
if (!res)
return false;
if (processedLoc == 0)
return true;
data = (const void*)((const unsigned char*)data + processedLoc);
size -= processedLoc;
} while (size > 0);
return true;
}
bool FileOut::SetLength(UInt64 length) noexcept
{
UInt64 newPosition;
if (!Seek(length, newPosition))
return false;
if (newPosition != length)
return false;
return SetEndOfFile();
}
bool FileOut::SetEndOfFile() noexcept
{
UInt64 pos;
if (!GetPosition(pos))
return false;
return ftruncate(m_Fd, (off_t)pos) == 0;
}
bool FileOut::WritePart(const void* data, UInt32 size, UInt32& processedSize) noexcept
{
if (size > kChunkSizeMax)
size = kChunkSizeMax;
ssize_t result = ::write(m_Fd, data, size);
if (result < 0) {
processedSize = 0;
return false;
}
processedSize = (UInt32)result;
return true;
}
} // namespace IO
#endif
|