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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <zencore/filesystem.h>
#include <zencore/except.h>
#include <zencore/fmtutils.h>
#include <zencore/iobuffer.h>
#include <zencore/logging.h>
#include <zencore/string.h>
#include <zencore/windows.h>
#include <atlbase.h>
#include <atlfile.h>
#include <winioctl.h>
#include <winnt.h>
#include <filesystem>
#include <gsl/gsl-lite.hpp>
namespace zen {
using namespace std::literals;
static bool
DeleteReparsePoint(const wchar_t* Path, DWORD dwReparseTag)
{
CHandle hDir(CreateFileW(Path,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
nullptr));
if (hDir != INVALID_HANDLE_VALUE)
{
REPARSE_GUID_DATA_BUFFER Rgdb = {};
Rgdb.ReparseTag = dwReparseTag;
DWORD dwBytes;
const BOOL bOK =
DeviceIoControl(hDir, FSCTL_DELETE_REPARSE_POINT, &Rgdb, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE, nullptr, 0, &dwBytes, nullptr);
return bOK == TRUE;
}
return false;
}
bool
CreateDirectories(const wchar_t* Dir)
{
return std::filesystem::create_directories(Dir);
}
bool
CreateDirectories(const std::filesystem::path& Dir)
{
return std::filesystem::create_directories(Dir);
}
// Erase all files and directories in a given directory, leaving an empty directory
// behind
static bool
WipeDirectory(const wchar_t* DirPath)
{
ExtendableWideStringBuilder<128> Pattern;
Pattern.Append(DirPath);
Pattern.Append(L"\\*");
WIN32_FIND_DATAW FindData;
HANDLE hFind = FindFirstFileW(Pattern.c_str(), &FindData);
if (hFind != nullptr)
{
do
{
bool IsRegular = true;
if (FindData.cFileName[0] == L'.')
{
if (FindData.cFileName[1] == L'.')
{
if (FindData.cFileName[2] == L'\0')
{
IsRegular = false;
}
}
else if (FindData.cFileName[1] == L'\0')
{
IsRegular = false;
}
}
if (IsRegular)
{
ExtendableWideStringBuilder<128> Path;
Path.Append(DirPath);
Path.Append(L'\\');
Path.Append(FindData.cFileName);
// if (fd.dwFileAttributes & FILE_ATTRIBUTE_RECALL_ON_OPEN)
// deleteReparsePoint(path.c_str(), fd.dwReserved0);
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_RECALL_ON_OPEN)
{
DeleteReparsePoint(Path.c_str(), FindData.dwReserved0);
}
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS)
{
DeleteReparsePoint(Path.c_str(), FindData.dwReserved0);
}
bool Success = DeleteDirectories(Path.c_str());
if (!Success)
{
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
DeleteReparsePoint(Path.c_str(), FindData.dwReserved0);
}
}
}
else
{
DeleteFileW(Path.c_str());
}
}
} while (FindNextFileW(hFind, &FindData) == TRUE);
}
FindClose(hFind);
return true;
}
bool
DeleteDirectories(const wchar_t* DirPath)
{
return WipeDirectory(DirPath) && RemoveDirectoryW(DirPath) == TRUE;
}
bool
CleanDirectory(const wchar_t* DirPath)
{
if (std::filesystem::exists(DirPath))
{
return WipeDirectory(DirPath);
}
else
{
return CreateDirectories(DirPath);
}
}
bool
DeleteDirectories(const std::filesystem::path& Dir)
{
return DeleteDirectories(Dir.c_str());
}
bool
CleanDirectory(const std::filesystem::path& Dir)
{
return CleanDirectory(Dir.c_str());
}
//////////////////////////////////////////////////////////////////////////
bool
SupportsBlockRefCounting(std::filesystem::path Path)
{
ATL::CHandle Handle(CreateFileW(Path.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
nullptr));
if (Handle == INVALID_HANDLE_VALUE)
{
Handle.Detach();
return false;
}
ULONG FileSystemFlags = 0;
if (!GetVolumeInformationByHandleW(Handle, nullptr, 0, nullptr, nullptr, /* lpFileSystemFlags */ &FileSystemFlags, nullptr, 0))
{
return false;
}
if (!(FileSystemFlags & FILE_SUPPORTS_BLOCK_REFCOUNTING))
{
return false;
}
return true;
}
bool
CloneFile(std::filesystem::path FromPath, std::filesystem::path ToPath)
{
ATL::CHandle FromFile(CreateFileW(FromPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr));
if (FromFile == INVALID_HANDLE_VALUE)
{
FromFile.Detach();
return false;
}
ULONG FileSystemFlags;
if (!GetVolumeInformationByHandleW(FromFile, nullptr, 0, nullptr, nullptr, /* lpFileSystemFlags */ &FileSystemFlags, nullptr, 0))
{
return false;
}
if (!(FileSystemFlags & FILE_SUPPORTS_BLOCK_REFCOUNTING))
{
SetLastError(ERROR_NOT_CAPABLE);
return false;
}
FILE_END_OF_FILE_INFO FileSize;
if (!GetFileSizeEx(FromFile, &FileSize.EndOfFile))
{
return false;
}
FILE_BASIC_INFO BasicInfo;
if (!GetFileInformationByHandleEx(FromFile, FileBasicInfo, &BasicInfo, sizeof BasicInfo))
{
return false;
}
DWORD dwBytesReturned = 0;
FSCTL_GET_INTEGRITY_INFORMATION_BUFFER GetIntegrityInfoBuffer;
if (!DeviceIoControl(FromFile,
FSCTL_GET_INTEGRITY_INFORMATION,
nullptr,
0,
&GetIntegrityInfoBuffer,
sizeof GetIntegrityInfoBuffer,
&dwBytesReturned,
nullptr))
{
return false;
}
SetFileAttributesW(ToPath.c_str(), FILE_ATTRIBUTE_NORMAL);
ATL::CHandle TargetFile(CreateFileW(ToPath.c_str(),
GENERIC_READ | GENERIC_WRITE | DELETE,
/* no sharing */ FILE_SHARE_READ,
nullptr,
OPEN_ALWAYS,
0,
/* hTemplateFile */ FromFile));
if (TargetFile == INVALID_HANDLE_VALUE)
{
TargetFile.Detach();
return false;
}
// Delete target file when handle is closed (we only reset this if the copy succeeds)
FILE_DISPOSITION_INFO FileDisposition = {TRUE};
if (!SetFileInformationByHandle(TargetFile, FileDispositionInfo, &FileDisposition, sizeof FileDisposition))
{
TargetFile.Close();
DeleteFileW(ToPath.c_str());
return false;
}
// Make file sparse so we don't end up allocating space when we change the file size
if (!DeviceIoControl(TargetFile, FSCTL_SET_SPARSE, nullptr, 0, nullptr, 0, &dwBytesReturned, nullptr))
{
return false;
}
// Copy integrity checking information
FSCTL_SET_INTEGRITY_INFORMATION_BUFFER IntegritySet = {GetIntegrityInfoBuffer.ChecksumAlgorithm,
GetIntegrityInfoBuffer.Reserved,
GetIntegrityInfoBuffer.Flags};
if (!DeviceIoControl(TargetFile, FSCTL_SET_INTEGRITY_INFORMATION, &IntegritySet, sizeof IntegritySet, nullptr, 0, nullptr, nullptr))
{
return false;
}
// Resize file - note that the file is sparse at this point so no additional data will be written
if (!SetFileInformationByHandle(TargetFile, FileEndOfFileInfo, &FileSize, sizeof FileSize))
{
return false;
}
constexpr auto RoundToClusterSize = [](LONG64 FileSize, ULONG ClusterSize) -> LONG64 {
return (FileSize + ClusterSize - 1) / ClusterSize * ClusterSize;
};
static_assert(RoundToClusterSize(5678, 4 * 1024) == 8 * 1024);
// Loop for cloning file contents. This is necessary as the API has a 32-bit size
// limit for some reason
const LONG64 SplitThreshold = (1LL << 32) - GetIntegrityInfoBuffer.ClusterSizeInBytes;
DUPLICATE_EXTENTS_DATA DuplicateExtentsData{.FileHandle = FromFile};
for (LONG64 CurrentByteOffset = 0,
RemainingBytes = RoundToClusterSize(FileSize.EndOfFile.QuadPart, GetIntegrityInfoBuffer.ClusterSizeInBytes);
RemainingBytes > 0;
CurrentByteOffset += SplitThreshold, RemainingBytes -= SplitThreshold)
{
DuplicateExtentsData.SourceFileOffset.QuadPart = CurrentByteOffset;
DuplicateExtentsData.TargetFileOffset.QuadPart = CurrentByteOffset;
DuplicateExtentsData.ByteCount.QuadPart = std::min(SplitThreshold, RemainingBytes);
if (!DeviceIoControl(TargetFile,
FSCTL_DUPLICATE_EXTENTS_TO_FILE,
&DuplicateExtentsData,
sizeof DuplicateExtentsData,
nullptr,
0,
&dwBytesReturned,
nullptr))
{
return false;
}
}
// Make the file not sparse again now that we have populated the contents
if (!(BasicInfo.FileAttributes & FILE_ATTRIBUTE_SPARSE_FILE))
{
FILE_SET_SPARSE_BUFFER SetSparse = {FALSE};
if (!DeviceIoControl(TargetFile, FSCTL_SET_SPARSE, &SetSparse, sizeof SetSparse, nullptr, 0, &dwBytesReturned, nullptr))
{
return false;
}
}
// Update timestamps (but don't lie about the creation time)
BasicInfo.CreationTime.QuadPart = 0;
if (!SetFileInformationByHandle(TargetFile, FileBasicInfo, &BasicInfo, sizeof BasicInfo))
{
return false;
}
if (!FlushFileBuffers(TargetFile))
{
return false;
}
// Finally now everything is done - make sure the file is not deleted on close!
FileDisposition = {FALSE};
const bool AllOk = (TRUE == SetFileInformationByHandle(TargetFile, FileDispositionInfo, &FileDisposition, sizeof FileDisposition));
return AllOk;
}
bool
CopyFile(std::filesystem::path FromPath, std::filesystem::path ToPath, const CopyFileOptions& Options)
{
bool Success = false;
if (Options.EnableClone)
{
Success = CloneFile(FromPath.native(), ToPath.native());
if (Success)
{
return true;
}
}
if (Options.MustClone)
{
return false;
}
BOOL CancelFlag = FALSE;
Success = !!::CopyFileExW(FromPath.c_str(),
ToPath.c_str(),
/* lpProgressRoutine */ nullptr,
/* lpData */ nullptr,
&CancelFlag,
/* dwCopyFlags */ 0);
if (!Success)
{
throw std::system_error(std::error_code(::GetLastError(), std::system_category()), "file copy failed");
}
return Success;
}
void
WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t BufferCount)
{
using namespace fmt::literals;
CAtlFile Outfile;
HRESULT hRes = Outfile.Create(Path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS);
if (hRes == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
{
zen::CreateDirectories(Path.parent_path());
hRes = Outfile.Create(Path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS);
}
if (FAILED(hRes))
{
zen::ThrowSystemException(hRes, "File open failed for '{}'"_format(Path).c_str());
}
// TODO: this should be block-enlightened
for (size_t i = 0; i < BufferCount; ++i)
{
uint64_t WriteSize = Data[i]->Size();
const void* DataPtr = Data[i]->Data();
while (WriteSize)
{
const uint64_t ChunkSize = zen::Min<uint64_t>(WriteSize, uint64_t(2) * 1024 * 1024 * 1024);
hRes = Outfile.Write(DataPtr, gsl::narrow_cast<uint32_t>(WriteSize));
if (FAILED(hRes))
{
zen::ThrowSystemException(hRes, "File write failed for '{}'"_format(Path).c_str());
}
WriteSize -= ChunkSize;
DataPtr = reinterpret_cast<const uint8_t*>(DataPtr) + ChunkSize;
}
}
}
void
WriteFile(std::filesystem::path Path, IoBuffer Data)
{
const IoBuffer* const DataPtr = &Data;
WriteFile(Path, &DataPtr, 1);
}
FileContents
ReadFile(std::filesystem::path Path)
{
ATL::CHandle FromFile(CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr));
if (FromFile == INVALID_HANDLE_VALUE)
{
FromFile.Detach();
return FileContents{.ErrorCode = std::error_code(::GetLastError(), std::system_category())};
}
FILE_END_OF_FILE_INFO FileSize;
if (!GetFileSizeEx(FromFile, &FileSize.EndOfFile))
{
return FileContents{.ErrorCode = std::error_code(::GetLastError(), std::system_category())};
}
const uint64_t FileSizeBytes = FileSize.EndOfFile.QuadPart;
FileContents Contents;
Contents.Data.emplace_back(IoBuffer(IoBuffer::File, FromFile.Detach(), 0, FileSizeBytes));
return Contents;
}
bool
ScanFile(std::filesystem::path Path, const uint64_t ChunkSize, std::function<void(const void* Data, size_t Size)>&& ProcessFunc)
{
ATL::CHandle FromFile(CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr));
if (FromFile == INVALID_HANDLE_VALUE)
{
FromFile.Detach();
return false;
}
std::vector<uint8_t> ReadBuffer(ChunkSize);
for (;;)
{
DWORD dwBytesRead = 0;
BOOL Success = ::ReadFile(FromFile, ReadBuffer.data(), (DWORD)ReadBuffer.size(), &dwBytesRead, nullptr);
if (!Success)
{
throw std::system_error(std::error_code(::GetLastError(), std::system_category()), "file scan failed");
}
if (dwBytesRead == 0)
break;
ProcessFunc(ReadBuffer.data(), dwBytesRead);
}
return true;
}
std::string
ToUtf8(const std::filesystem::path& Path)
{
return WideToUtf8(Path.native().c_str());
}
void
FileSystemTraversal::TraverseFileSystem(const std::filesystem::path& RootDir, TreeVisitor& Visitor)
{
uint64_t FileInfoBuffer[8 * 1024];
FILE_INFO_BY_HANDLE_CLASS FibClass = FileIdBothDirectoryRestartInfo;
bool Continue = true;
std::wstring RootDirPath = RootDir.native();
CAtlFile RootDirHandle;
HRESULT hRes = RootDirHandle.Create(RootDirPath.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS);
if (FAILED(hRes))
{
zen::ThrowSystemException(hRes, "Failed to open handle to volume root");
}
while (Continue)
{
BOOL Success = GetFileInformationByHandleEx(RootDirHandle, FibClass, FileInfoBuffer, sizeof FileInfoBuffer);
FibClass = FileIdBothDirectoryInfo; // Set up for next iteration
uint64_t EntryOffset = 0;
if (!Success)
{
DWORD LastError = GetLastError();
if (LastError == ERROR_NO_MORE_FILES)
{
break;
}
throw std::system_error(std::error_code(LastError, std::system_category()), "file system traversal error");
}
for (;;)
{
const FILE_ID_BOTH_DIR_INFO* DirInfo =
reinterpret_cast<const FILE_ID_BOTH_DIR_INFO*>(reinterpret_cast<const uint8_t*>(FileInfoBuffer) + EntryOffset);
std::wstring_view FileName(DirInfo->FileName, DirInfo->FileNameLength / sizeof(wchar_t));
if (DirInfo->FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (FileName == L"."sv || FileName == L".."sv)
{
// Not very interesting
}
else
{
const bool ShouldDescend = Visitor.VisitDirectory(RootDir, FileName);
if (ShouldDescend)
{
// Note that this recursion combined with the buffer could
// blow the stack, we should consider a different strategy
std::filesystem::path FullPath = RootDir / FileName;
TraverseFileSystem(FullPath, Visitor);
}
}
}
else if (DirInfo->FileAttributes & FILE_ATTRIBUTE_DEVICE)
{
ZEN_WARN("encountered device node during file system traversal: {} found in {}", WideToUtf8(FileName), RootDir);
}
else
{
std::filesystem::path FullPath = RootDir / FileName;
Visitor.VisitFile(RootDir, FileName, DirInfo->EndOfFile.QuadPart);
}
const uint64_t NextOffset = DirInfo->NextEntryOffset;
if (NextOffset == 0)
{
break;
}
EntryOffset += NextOffset;
}
}
}
std::filesystem::path
PathFromHandle(void* NativeHandle)
{
if (NativeHandle == nullptr || NativeHandle == INVALID_HANDLE_VALUE)
{
return std::filesystem::path();
}
const DWORD RequiredLengthIncludingNul = GetFinalPathNameByHandleW(NativeHandle, nullptr, 0, FILE_NAME_OPENED);
std::wstring FullPath;
FullPath.resize(RequiredLengthIncludingNul - 1);
const DWORD FinalLength = GetFinalPathNameByHandleW(NativeHandle, FullPath.data(), RequiredLengthIncludingNul, FILE_NAME_OPENED);
ZEN_UNUSED(FinalLength);
return FullPath;
}
std::filesystem::path
GetRunningExecutablePath()
{
TCHAR ExePath[MAX_PATH];
DWORD PathLength = GetModuleFileName(NULL, ExePath, ZEN_ARRAY_COUNT(ExePath));
return {std::wstring_view(ExePath, PathLength)};
}
} // namespace zen
|