upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/file_system_test.cpp
diff options
context:
space:
mode:
authorHiiva <32042291+Hiiva@users.noreply.github.com>2026-11-23 16:07:54 +0200
committerGitHub <noreply@github.com>2026-11-23 08:07:54 -0600
commitd582abc17fa2d39e1d58277d14497d78a7f5adf9 (patch)
tree37e38c699c5b566e5e796968237f224be6d02688 /runtime/test/file_system_test.cpp
parentb9681ec4cea3124f0218048e30bec6345f3bc698 (diff)
runtime: reject deleting read-only package files (#5946)
Squash-merged after review of PR #5946 - BUILD-1899: Respect read-only attributes when deleting package files
Diffstat (limited to 'runtime/test/file_system_test.cpp')
-rw-r--r--runtime/test/file_system_test.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp
index a68b27aa9..53def7ddb 100644
--- a/runtime/test/file_system_test.cpp
+++ b/runtime/test/file_system_test.cpp
@@ -392,6 +392,102 @@ int main() {
392 "the parent folder stays listed after deleting a nested package file"); 392 "the parent folder stays listed after deleting a nested package file");
393 } 393 }
394 394
395 // DeleteFile checks the visible file's read-only bit. A package file is hidden by a marker instead of being
396 // passed to the host DeleteFile, but that must not bypass the same refusal. Compare each API with a real host
397 // file and keep the package and overlay attributes independent.
398 {
399 struct DeleteCase {
400 const char* name;
401 DWORD package_attributes;
402 DWORD overlay_attributes; // zero means there is no overlay copy
403 };
404 constexpr DWORD mixed = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE;
405 const DeleteCase cases[] = {
406 {"package-readonly", FILE_ATTRIBUTE_READONLY, 0},
407 {"package-normal", FILE_ATTRIBUTE_NORMAL, 0},
408 {"package-mixed-readonly", FILE_ATTRIBUTE_READONLY | mixed, 0},
409 {"package-mixed-writable", mixed, 0},
410 {"readonly-package-normal-overlay", FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_NORMAL},
411 {"normal-package-readonly-overlay", FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY},
412 {"readonly-package-readonly-overlay", FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_READONLY},
413 {"readonly-package-mixed-overlay", FILE_ATTRIBUTE_READONLY, mixed},
414 {"normal-package-mixed-readonly-overlay", FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY | mixed},
415 };
416 for (const bool narrow : {false, true}) {
417 for (const auto& item : cases) {
418 const std::string leaf = std::string(narrow ? "a-" : "w-") + item.name + ".bin";
419 const std::filesystem::path relative = std::filesystem::path("delete-attributes") / leaf;
420 const auto package_file = package / relative, overlay_file = overlay / relative;
421 const auto native_file = root / "native-delete" / leaf;
422 const auto deletion_marker = overlay / ".gta6rt-deleted" / relative;
423 const bool has_overlay = item.overlay_attributes != 0;
424 const DWORD visible_attributes = has_overlay ? item.overlay_attributes : item.package_attributes;
425 const char* visible_bytes = has_overlay ? "overlay bytes" : "package bytes";
426 std::printf("DeleteFile%c case: %s\n", narrow ? 'A' : 'W', item.name);
427 write_path(package_file, "package bytes");
428 check(::SetFileAttributesW(package_file.c_str(), item.package_attributes) != FALSE,
429 "set the synthetic package attributes");
430 const DWORD package_before = ::GetFileAttributesW(package_file.c_str());
431 if (has_overlay) {
432 write_path(overlay_file, visible_bytes);
433 check(::SetFileAttributesW(overlay_file.c_str(), item.overlay_attributes) != FALSE,
434 "set the visible overlay attributes");
435 }
436 write_path(native_file, visible_bytes);
437 check(::SetFileAttributesW(native_file.c_str(), visible_attributes) != FALSE,
438 "set the independent native file attributes");
439 std::string native_a;
440 if (narrow) {
441 const UINT code_page = ::AreFileApisANSI() ? CP_ACP : CP_OEMCP;
442 const int size = ::WideCharToMultiByte(code_page, 0, native_file.c_str(), -1, nullptr, 0,
443 nullptr, nullptr);
444 native_a.resize(size > 0 ? static_cast<size_t>(size) : 1);
445 check(size > 0 && ::WideCharToMultiByte(code_page, 0, native_file.c_str(), -1, native_a.data(),
446 size, nullptr, nullptr) == size,
447 "encode the native DeleteFileA control path");
448 }
449 SetLastError(0);
450 const BOOL native_ok = narrow ? ::DeleteFileA(native_a.c_str()) : ::DeleteFileW(native_file.c_str());
451 const DWORD native_error = native_ok ? 0 : GetLastError();
452 const bool readonly = (visible_attributes & FILE_ATTRIBUTE_READONLY) != 0;
453 check((native_ok != FALSE) == !readonly && (native_ok || native_error == ERROR_ACCESS_DENIED),
454 "native deletion rejects read-only and accepts other attribute combinations");
455 const std::wstring logical = L"GAME:\\" + relative.wstring();
456 const std::string logical_a = "GAME:\\delete-attributes\\" + leaf;
457 SetLastError(0);
458 const BOOL mapped_ok = narrow ? delete_a(logical_a.c_str()) : delete_w(logical.c_str());
459 const DWORD mapped_error = mapped_ok ? 0 : GetLastError();
460 check((mapped_ok != FALSE) == (native_ok != FALSE) && mapped_error == native_error,
461 "shim deletion matches its native API's result and failure error");
462 check(read_path(package_file) == "package bytes" &&
463 ::GetFileAttributesW(package_file.c_str()) == package_before,
464 "deletion leaves package bytes and attributes unchanged");
465 check(std::filesystem::exists(deletion_marker) == !readonly,
466 "only an accepted delete publishes a marker");
467 check(std::filesystem::exists(overlay_file) == (has_overlay && readonly),
468 "a refused delete keeps its overlay and never copies the package up");
469 if (readonly) {
470 check(attributes_w(logical.c_str()) == visible_attributes,
471 "a refused delete retains the visible attributes");
472 const HANDLE retained = create_w(logical.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
473 OPEN_EXISTING, 0, nullptr);
474 check(retained != INVALID_HANDLE_VALUE && read_handle(retained) == visible_bytes,
475 "a refused delete leaves the same logical bytes readable");
476 if (retained != INVALID_HANDLE_VALUE) ::CloseHandle(retained);
477 } else {
478 SetLastError(0);
479 check(attributes_w(logical.c_str()) == INVALID_FILE_ATTRIBUTES &&
480 GetLastError() == ERROR_FILE_NOT_FOUND,
481 "an accepted delete hides the logical file");
482 }
483 // Restore only these synthetic files, including on a failed assertion, for remove_all below.
484 ::SetFileAttributesW(package_file.c_str(), FILE_ATTRIBUTE_NORMAL);
485 if (std::filesystem::exists(overlay_file)) ::SetFileAttributesW(overlay_file.c_str(), FILE_ATTRIBUTE_NORMAL);
486 if (std::filesystem::exists(native_file)) ::SetFileAttributesW(native_file.c_str(), FILE_ATTRIBUTE_NORMAL);
487 }
488 }
489 }
490
395 // Deleting one package file must not hide its folder. The overlay has never written to GAME:\pkgonly, so the 491 // Deleting one package file must not hide its folder. The overlay has never written to GAME:\pkgonly, so the
396 // folder is only in the package, and its marker folder under .gta6rt-deleted is not a marker for the folder. 492 // folder is only in the package, and its marker folder under .gta6rt-deleted is not a marker for the folder.
397 { 493 {