upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.cpp
diff options
context:
space:
mode:
authorMatin Sanei <saneimatin33@gmail.com>2026-11-26 08:52:04 +0100
committerMatin Sanei <saneimatin33@gmail.com>2026-11-26 08:52:04 +0100
commitfbafd7cd2ab3be7b5aca78bb73efd8c5807c9a01 (patch)
treeeee1316c1b9b6046a8333e94808ec132269002a0 /runtime/shims/file_system.cpp
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
file_system: add GetDiskFreeSpaceA/W and GetDiskFreeSpaceExA/W for console roots (RT-FS-DISK-FREE-SPACE)
Resolve console roots (GAME:\, G:\, T:\) to mapped host paths before querying host disk geometry and capacity, and provide synthetic Xbox console storage fallback quotas when unmounted.
Diffstat (limited to 'runtime/shims/file_system.cpp')
-rw-r--r--runtime/shims/file_system.cpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 8a20b21e9..f7ced740e 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -1570,6 +1570,127 @@ BOOL WINAPI set_file_attributes_a(LPCSTR name, DWORD attributes) {
1570 return set_file_attributes_w(wide.c_str(), attributes); 1570 return set_file_attributes_w(wide.c_str(), attributes);
1571} 1571}
1572 1572
1573BOOL WINAPI get_disk_free_space_ex_w(LPCWSTR directory_name,
1574 PULARGE_INTEGER free_bytes_available_to_caller,
1575 PULARGE_INTEGER total_number_of_bytes,
1576 PULARGE_INTEGER total_number_of_free_bytes) {
1577 if (!directory_name) {
1578 return ::GetDiskFreeSpaceExW(nullptr, free_bytes_available_to_caller, total_number_of_bytes,
1579 total_number_of_free_bytes);
1580 }
1581
1582 std::vector<std::wstring> parts;
1583 const Parsed parsed = parse_console(directory_name, parts);
1584 if (parsed == Parsed::Host) {
1585 return ::GetDiskFreeSpaceExW(directory_name, free_bytes_available_to_caller, total_number_of_bytes,
1586 total_number_of_free_bytes);
1587 }
1588 if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
1589
1590 const Roots r = roots();
1591 size_t prefix_length = 0;
1592 const Drive drive = console_drive(directory_name, prefix_length);
1593 std::filesystem::path host_dir;
1594 if (drive == Drive::Scratch) {
1595 host_dir = r.overlay.empty() ? std::filesystem::path() : (r.overlay / kScratchDir);
1596 } else {
1597 host_dir = r.game.empty() ? r.overlay : r.game;
1598 }
1599
1600 if (!host_dir.empty()) {
1601 std::error_code ec;
1602 if (std::filesystem::exists(host_dir, ec)) {
1603 return ::GetDiskFreeSpaceExW(win32_io_path(host_dir).c_str(), free_bytes_available_to_caller,
1604 total_number_of_bytes, total_number_of_free_bytes);
1605 }
1606 }
1607
1608 constexpr ULONGLONG kDefaultFree = 500ULL * 1024 * 1024 * 1024;
1609 constexpr ULONGLONG kDefaultTotal = 1000ULL * 1024 * 1024 * 1024;
1610 if (free_bytes_available_to_caller) free_bytes_available_to_caller->QuadPart = kDefaultFree;
1611 if (total_number_of_bytes) total_number_of_bytes->QuadPart = kDefaultTotal;
1612 if (total_number_of_free_bytes) total_number_of_free_bytes->QuadPart = kDefaultFree;
1613 return TRUE;
1614}
1615
1616BOOL WINAPI get_disk_free_space_ex_a(LPCSTR directory_name,
1617 PULARGE_INTEGER free_bytes_available_to_caller,
1618 PULARGE_INTEGER total_number_of_bytes,
1619 PULARGE_INTEGER total_number_of_free_bytes) {
1620 if (!directory_name) {
1621 return get_disk_free_space_ex_w(nullptr, free_bytes_available_to_caller, total_number_of_bytes,
1622 total_number_of_free_bytes);
1623 }
1624 const std::wstring wide = widen_ansi(directory_name);
1625 return get_disk_free_space_ex_w(wide.c_str(), free_bytes_available_to_caller, total_number_of_bytes,
1626 total_number_of_free_bytes);
1627}
1628
1629BOOL WINAPI get_disk_free_space_w(LPCWSTR root_path_name,
1630 LPDWORD sectors_per_cluster,
1631 LPDWORD bytes_per_sector,
1632 LPDWORD number_of_free_clusters,
1633 LPDWORD total_number_of_clusters) {
1634 if (!root_path_name) {
1635 return ::GetDiskFreeSpaceW(nullptr, sectors_per_cluster, bytes_per_sector,
1636 number_of_free_clusters, total_number_of_clusters);
1637 }
1638
1639 std::vector<std::wstring> parts;
1640 const Parsed parsed = parse_console(root_path_name, parts);
1641 if (parsed == Parsed::Host) {
1642 return ::GetDiskFreeSpaceW(root_path_name, sectors_per_cluster, bytes_per_sector,
1643 number_of_free_clusters, total_number_of_clusters);
1644 }
1645 if (parsed == Parsed::Refused) return fail_bool(ERROR_INVALID_NAME);
1646
1647 const Roots r = roots();
1648 size_t prefix_length = 0;
1649 const Drive drive = console_drive(root_path_name, prefix_length);
1650 std::filesystem::path host_dir;
1651 if (drive == Drive::Scratch) {
1652 host_dir = r.overlay.empty() ? std::filesystem::path() : (r.overlay / kScratchDir);
1653 } else {
1654 host_dir = r.game.empty() ? r.overlay : r.game;
1655 }
1656
1657 if (!host_dir.empty()) {
1658 std::error_code ec;
1659 if (std::filesystem::exists(host_dir, ec)) {
1660 std::filesystem::path root_path = host_dir.root_path();
1661 if (root_path.empty()) root_path = host_dir;
1662 std::wstring root_str = root_path.wstring();
1663 if (!root_str.empty() && root_str.back() != L'\\' && root_str.back() != L'/') {
1664 root_str += L'\\';
1665 }
1666 if (::GetDiskFreeSpaceW(root_str.c_str(), sectors_per_cluster, bytes_per_sector,
1667 number_of_free_clusters, total_number_of_clusters)) {
1668 return TRUE;
1669 }
1670 }
1671 }
1672
1673 if (sectors_per_cluster) *sectors_per_cluster = 8;
1674 if (bytes_per_sector) *bytes_per_sector = 512;
1675 if (number_of_free_clusters) *number_of_free_clusters = 500ULL * 1024 * 1024 / 4;
1676 if (total_number_of_clusters) *total_number_of_clusters = 1000ULL * 1024 * 1024 / 4;
1677 return TRUE;
1678}
1679
1680BOOL WINAPI get_disk_free_space_a(LPCSTR root_path_name,
1681 LPDWORD sectors_per_cluster,
1682 LPDWORD bytes_per_sector,
1683 LPDWORD number_of_free_clusters,
1684 LPDWORD total_number_of_clusters) {
1685 if (!root_path_name) {
1686 return get_disk_free_space_w(nullptr, sectors_per_cluster, bytes_per_sector,
1687 number_of_free_clusters, total_number_of_clusters);
1688 }
1689 const std::wstring wide = widen_ansi(root_path_name);
1690 return get_disk_free_space_w(wide.c_str(), sectors_per_cluster, bytes_per_sector,
1691 number_of_free_clusters, total_number_of_clusters);
1692}
1693
1573} // namespace rt::fs 1694} // namespace rt::fs
1574 1695
1575namespace { 1696namespace {
@@ -1639,6 +1760,10 @@ BOOL WINAPI read_file_scatter(HANDLE file, FILE_SEGMENT_ELEMENT segments[], DWOR
1639 {dll, "CopyFile2", reinterpret_cast<void*>(&copy_file2)}, \ 1760 {dll, "CopyFile2", reinterpret_cast<void*>(&copy_file2)}, \
1640 {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \ 1761 {dll, "SetFileAttributesA", reinterpret_cast<void*>(&set_file_attributes_a)}, \
1641 {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)}, \ 1762 {dll, "SetFileAttributesW", reinterpret_cast<void*>(&set_file_attributes_w)}, \
1763 {dll, "GetDiskFreeSpaceA", reinterpret_cast<void*>(&get_disk_free_space_a)}, \
1764 {dll, "GetDiskFreeSpaceW", reinterpret_cast<void*>(&get_disk_free_space_w)}, \
1765 {dll, "GetDiskFreeSpaceExA", reinterpret_cast<void*>(&get_disk_free_space_ex_a)}, \
1766 {dll, "GetDiskFreeSpaceExW", reinterpret_cast<void*>(&get_disk_free_space_ex_w)}, \
1642 {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \ 1767 {dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
1643 {dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \ 1768 {dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \
1644 {dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)} 1769 {dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)}