MCPcopy Create free account
hub / github.com/ElyPrismLauncher/Launcher / win_ioctl_clone

Function win_ioctl_clone

launcher/FileSystem.cpp:1390–1550  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1388}
1389
1390bool win_ioctl_clone(const std::wstring& src_path, const std::wstring& dst_path, std::error_code& ec)
1391{
1392 /**
1393 * This algorithm inspired from https://github.com/0xbadfca11/reflink
1394 * LICENSE MIT
1395 *
1396 * Additional references
1397 * https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_duplicate_extents_to_file
1398 * https://github.com/microsoft/CopyOnWrite/blob/main/lib/Windows/WindowsCopyOnWriteFilesystem.cs#L94
1399 */
1400
1401 HANDLE hSourceFile = CreateFileW(src_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
1402 if (hSourceFile == INVALID_HANDLE_VALUE) {
1403 ec = std::error_code(GetLastError(), std::system_category());
1404 qDebug() << "Failed to open source file" << src_path.c_str();
1405 return false;
1406 }
1407
1408 ULONG fs_flags;
1409 if (!GetVolumeInformationByHandleW(hSourceFile, nullptr, 0, nullptr, nullptr, &fs_flags, nullptr, 0)) {
1410 ec = std::error_code(GetLastError(), std::system_category());
1411 qDebug() << "Failed to get Filesystem information for" << src_path.c_str();
1412 CloseHandle(hSourceFile);
1413 return false;
1414 }
1415 if (!(fs_flags & FILE_SUPPORTS_BLOCK_REFCOUNTING)) {
1416 SetLastError(ERROR_NOT_CAPABLE);
1417 ec = std::error_code(GetLastError(), std::system_category());
1418 qWarning() << "Filesystem at" << src_path.c_str() << "does not support reflink";
1419 CloseHandle(hSourceFile);
1420 return false;
1421 }
1422
1423 FILE_END_OF_FILE_INFO sourceFileLength;
1424 if (!GetFileSizeEx(hSourceFile, &sourceFileLength.EndOfFile)) {
1425 ec = std::error_code(GetLastError(), std::system_category());
1426 qDebug() << "Failed to size of source file" << src_path.c_str();
1427 CloseHandle(hSourceFile);
1428 return false;
1429 }
1430 FILE_BASIC_INFO sourceFileBasicInfo;
1431 if (!GetFileInformationByHandleEx(hSourceFile, FileBasicInfo, &sourceFileBasicInfo, sizeof(sourceFileBasicInfo))) {
1432 ec = std::error_code(GetLastError(), std::system_category());
1433 qDebug() << "Failed to source file info" << src_path.c_str();
1434 CloseHandle(hSourceFile);
1435 return false;
1436 }
1437 ULONG junk;
1438 FSCTL_GET_INTEGRITY_INFORMATION_BUFFER sourceFileIntegrity;
1439 if (!DeviceIoControl(hSourceFile, FSCTL_GET_INTEGRITY_INFORMATION, nullptr, 0, &sourceFileIntegrity, sizeof(sourceFileIntegrity), &junk,
1440 nullptr)) {
1441 ec = std::error_code(GetLastError(), std::system_category());
1442 qDebug() << "Failed to source file integrity info" << src_path.c_str();
1443 CloseHandle(hSourceFile);
1444 return false;
1445 }
1446
1447 HANDLE hDestFile = CreateFileW(dst_path.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE, 0, nullptr, CREATE_NEW, 0, hSourceFile);

Callers 1

clone_fileFunction · 0.85

Calls 2

RoundUpToPowerOf2Function · 0.85
QStringClass · 0.70

Tested by

no test coverage detected