Play your Steam library's Windows games on a jailbroken PS5.
C++
Find unrecorded Playden title folders
Leonardo Lobatodffccabe3085
diff --git a/cmake/PlaydenCore.cmake b/cmake/PlaydenCore.cmakeindex 6418abf..723ad8c 100644--- a/cmake/PlaydenCore.cmake+++ b/cmake/PlaydenCore.cmake@@ -74,10 +74,14 @@ add_library(playden_game_paths STATIC "${CMAKE_SOURCE_DIR}/src/steam/backend/gamtarget_include_directories(playden_game_paths PUBLIC "${CMAKE_SOURCE_DIR}/src")target_compile_features(playden_game_paths PUBLIC cxx_std_17)-add_library(playden_runtime_installs STATIC "${CMAKE_SOURCE_DIR}/src/runtime/game_install.cpp")+add_library(playden_runtime_installs STATIC "${CMAKE_SOURCE_DIR}/src/runtime/game_install.cpp"+ "${CMAKE_SOURCE_DIR}/src/runtime/leftover_titles.cpp")target_include_directories(playden_runtime_installs PUBLIC "${CMAKE_SOURCE_DIR}/src")target_compile_features(playden_runtime_installs PUBLIC cxx_std_17)target_link_libraries(playden_runtime_installs PUBLIC playden_library playden_storage playden_steam_protocol playden_game_paths)+if(PLAYDEN_PLATFORM_PS5)+ target_compile_definitions(playden_runtime_installs PRIVATE PLAYDEN_PLATFORM_PS5=1)+endif()add_library(playden_runtime_install_service STATIC "${CMAKE_SOURCE_DIR}/src/runtime/install_service.cpp")target_include_directories(playden_runtime_install_service PUBLIC "${CMAKE_SOURCE_DIR}/src")@@ -242,6 +246,15 @@ if(BUILD_TESTING AND NOT PLAYDEN_PLATFORM_PS5)add_executable(runtime_game_install_test "${CMAKE_SOURCE_DIR}/tests/runtime_game_install_test.cpp")target_link_libraries(runtime_game_install_test PRIVATE playden_runtime_installs ${CMAKE_DL_LIBS})add_test(NAME runtime_game_install COMMAND runtime_game_install_test)+ add_executable(runtime_leftover_titles_test "${CMAKE_SOURCE_DIR}/tests/runtime_leftover_titles_test.cpp")+ target_link_libraries(runtime_leftover_titles_test PRIVATE playden_runtime_installs)+ add_test(NAME runtime_leftover_titles COMMAND runtime_leftover_titles_test)+ add_executable(runtime_leftover_titles_native_policy_test+ "${CMAKE_SOURCE_DIR}/tests/runtime_leftover_titles_test.cpp"+ "${CMAKE_SOURCE_DIR}/src/runtime/leftover_titles.cpp")+ target_compile_definitions(runtime_leftover_titles_native_policy_test PRIVATE PLAYDEN_PLATFORM_PS5=1)+ target_link_libraries(runtime_leftover_titles_native_policy_test PRIVATE playden_runtime_installs)+ add_test(NAME runtime_leftover_titles_native_policy COMMAND runtime_leftover_titles_native_policy_test)add_executable(local_scanner_test "${CMAKE_SOURCE_DIR}/tests/local_scanner_test.cpp")target_link_libraries(local_scanner_test PRIVATE playden_local_store)add_test(NAME local_scanner COMMAND local_scanner_test)diff --git a/src/runtime/game_install.cpp b/src/runtime/game_install.cppindex b917410..979ee86 100644--- a/src/runtime/game_install.cpp+++ b/src/runtime/game_install.cpp@@ -54,13 +54,6 @@ bool IsDescriptorId(const std::string& value) {});}-bool IsManagedTitle(const std::string& title) {- if (title.size() != 9 || title.compare(0, 6, "FAKE78") != 0) return false;- return std::all_of(title.begin() + 6, title.end(), [](unsigned char c) {- return std::isdigit(c);- });-}-bool IsAbsoluteNormalizedPath(const std::string& path) {if (path.empty() || path.size() > kMaxPathBytes || path.front() != '/' ||path.find('\0') != std::string::npos) return false;@@ -347,6 +340,13 @@ void WriteRecord(TextOutput& output, const RuntimeInstall& record) {} // namespace+bool IsManagedTitle(const std::string& title) {+ if (title.size() != 9 || title.compare(0, 6, "FAKE78") != 0) return false;+ return std::all_of(title.begin() + 6, title.end(), [](unsigned char c) {+ return std::isdigit(c);+ });+}+bool ValidateRuntimeInstallRecord(const RuntimeInstall& record) { return ValidRecord(record); }bool SameRuntimeInstallIdentity(const RuntimeInstall& a, const RuntimeInstall& b) {diff --git a/src/runtime/game_install.h b/src/runtime/game_install.hindex b379a99..8f45240 100644--- a/src/runtime/game_install.h+++ b/src/runtime/game_install.h@@ -91,6 +91,9 @@ struct RuntimeInstall {std::string moved_from;};+// The title range reserved for Playden games; excludes the launcher itself.+bool IsManagedTitle(const std::string& title);+// Shared OWNER record validation; this grants no filesystem ownership or readiness.bool ValidateRuntimeInstallRecord(const RuntimeInstall& record);// Same install: install, title, runtime number and game, data root and game folder. Runtimediff --git a/src/runtime/leftover_titles.cpp b/src/runtime/leftover_titles.cppnew file mode 100644index 0000000..243aca6--- /dev/null+++ b/src/runtime/leftover_titles.cpp@@ -0,0 +1,180 @@+// SPDX-License-Identifier: GPL-3.0-or-later+#include "runtime/leftover_titles.h"++#include "platform/directory_walk.h"+#include "steam/backend/json.h"++#include <algorithm>+#include <cerrno>+#include <limits>+#include <unordered_set>+#include <dirent.h>+#include <fcntl.h>+#include <sys/stat.h>+#include <unistd.h>++namespace playden::runtime {+namespace {+namespace fs = std::filesystem;+namespace json = steam::json;++bool SameDirectory(const struct stat& first, const struct stat& second) {+ return S_ISDIR(second.st_mode) && first.st_dev == second.st_dev && first.st_ino == second.st_ino;+}++// Takes ownership of fd. Refuse incomplete trees rather than show a misleading+// size or follow a link. The bound also keeps malformed trees off small stacks.+bool DirectoryBytes(int fd, const fs::path& path, std::uint64_t& bytes,+ std::size_t& entries, unsigned depth = 0) {+ DIR* directory = ::fdopendir(fd);+ if (!directory) { ::close(fd); return false; }+ bool ok = depth <= 32;+ while (ok) {+ errno = 0;+ const auto entry = ::readdir(directory);+ if (!entry) { ok = errno == 0; break; }+ const std::string name = entry->d_name;+ if (name == "." || name == "..") continue;+ if (++entries > 100000) { ok = false; break; }+ struct stat listed{};+ if (::lstat((path / name).c_str(), &listed) != 0) { ok = false; break; }+ if (S_ISLNK(listed.st_mode)) continue;+ if (!S_ISDIR(listed.st_mode) && !S_ISREG(listed.st_mode)) continue;+ const int child = platform::OpenWalkChild(fd, name.c_str(),+ O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK |+ (S_ISDIR(listed.st_mode) ? O_DIRECTORY : 0), entry->d_ino);+ struct stat opened{};+ if (child < 0 || ::fstat(child, &opened) != 0 ||+ opened.st_dev != listed.st_dev || opened.st_ino != listed.st_ino) {+ if (child >= 0) ::close(child);+ ok = false; break;+ }+ if (S_ISDIR(opened.st_mode)) {+ ok = DirectoryBytes(child, path / name, bytes, entries, depth + 1);+ } else {+ ::close(child);+ if (!S_ISREG(opened.st_mode) || opened.st_size < 0 ||+ static_cast<std::uint64_t>(opened.st_size) > std::numeric_limits<std::uint64_t>::max() - bytes) {+ ok = false; break;+ }+ bytes += static_cast<std::uint64_t>(opened.st_size);+ }+ }+ ::closedir(directory);+ return ok;+}++std::string Name(const json::Value* value) {+ if (!value || !value->is_object()) return {};+ const auto name = value->Find("titleName");+ if (!name || !name->is_string() || name->text().size() > 512) return {};+ for (const unsigned char c : name->text()) if (c < 0x20 || c == 0x7f) return {};+ return name->text();+}++std::string TitleName(int title_fd) {+ const int system = platform::OpenWalkChild(title_fd, "sce_sys", O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);+ if (system < 0) return {};+ const int fd = platform::OpenWalkChild(system, "param.json", O_RDONLY | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK);+ ::close(system);+ if (fd < 0) return {};+ struct stat info{};+ std::string bytes;+ bool ok = ::fstat(fd, &info) == 0 && S_ISREG(info.st_mode) && info.st_size >= 0 && info.st_size <= 64 * 1024;+ if (ok) {+ bytes.resize(static_cast<std::size_t>(info.st_size));+ std::size_t offset = 0;+ while (offset < bytes.size()) {+ const auto count = ::read(fd, bytes.data() + offset, bytes.size() - offset);+ if (count < 0 && errno == EINTR) continue;+ if (count <= 0) { ok = false; break; }+ offset += static_cast<std::size_t>(count);+ }+ }+ ::close(fd);+ const auto parsed = ok ? json::Parse(bytes, {64 * 1024, 8, 4096, 4096}) : std::nullopt;+ if (!parsed) return {};+ auto name = Name(&*parsed);+ if (!name.empty()) return name;+ const auto localized = parsed->Find("localizedParameters");+ if (!localized || !localized->is_object()) return {};+ const auto language = localized->Find("defaultLanguage");+ if (language && language->is_string()) name = Name(localized->Find(language->text()));+ if (name.empty()) name = Name(localized->Find("en-US"));+ if (name.empty()) for (const auto& entry : localized->object()) {+ name = Name(&entry.second);+ if (!name.empty()) break;+ }+ return name;+}+} // namespace++std::optional<std::vector<LeftoverTitleCandidate>> ListLeftoverTitleCandidates(+ const fs::path& homebrew_root, uid_t effective_uid,+ const std::unordered_set<std::string>& excluded_title_ids) {+ const int root = platform::OpenDirectoryWalk(homebrew_root);+ if (root < 0) return std::nullopt;+ DIR* directory = ::fdopendir(root);+ if (!directory) { ::close(root); return std::nullopt; }+ std::vector<LeftoverTitleCandidate> candidates;+ bool ok = true;+ while (true) {+ errno = 0;+ const auto entry = ::readdir(directory);+ if (!entry) { ok = errno == 0; break; }+ const std::string id = entry->d_name;+ if (!IsManagedTitle(id) || excluded_title_ids.count(id)) continue;+ struct stat listed{};+ if (::lstat((homebrew_root / id).c_str(), &listed) != 0 ||+ !S_ISDIR(listed.st_mode) || listed.st_uid != effective_uid) continue;+ const int fd = platform::OpenWalkChild(root, id.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC,+ entry->d_ino);+ struct stat opened{};+ if (fd < 0 || ::fstat(fd, &opened) != 0 || !SameDirectory(listed, opened) || opened.st_uid != effective_uid) {+ if (fd >= 0) ::close(fd);+ continue;+ }+ // The by-path walk may enumerate a held directory to verify a child inode.+ // Keep metadata's enumeration separate from the size walk's file position.+ const int metadata_fd = platform::OpenWalkChild(root, id.c_str(),+ O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC, entry->d_ino);+ struct stat metadata_info{};+ std::string name;+ if (metadata_fd >= 0) {+ if (::fstat(metadata_fd, &metadata_info) == 0 && SameDirectory(opened, metadata_info))+ name = TitleName(metadata_fd);+ ::close(metadata_fd);+ }+ LeftoverTitleCandidate candidate{id, true, false, opened.st_uid, false, std::move(name), 0,+ static_cast<std::uint64_t>(opened.st_dev), static_cast<std::uint64_t>(opened.st_ino)};+ std::size_t entries = 0;+ candidate.readable = DirectoryBytes(fd, homebrew_root / id, candidate.bytes, entries);+ candidates.push_back(std::move(candidate));+ }+ ::closedir(directory);+ return ok ? std::optional<std::vector<LeftoverTitleCandidate>>(std::move(candidates)) : std::nullopt;+}++std::vector<LeftoverTitle> FindLeftoverTitles(+ const fs::path& homebrew_root, bool repository_loaded, const std::vector<RuntimeInstall>& records,+ const LeftoverTitleActivity& activity, uid_t effective_uid, const LeftoverTitleLister& lister) {+ if (!repository_loaded || !lister || !activity) return {};+ std::unordered_set<std::string> reserved;+ for (const auto& record : records) reserved.insert(record.title_id);+ const auto candidates = lister(homebrew_root, effective_uid, reserved);+ if (!candidates) return {};+ std::vector<LeftoverTitle> leftovers;+ for (const auto& candidate : *candidates) {+ if (!IsManagedTitle(candidate.title_id) || !candidate.is_directory || candidate.is_symlink ||+ candidate.owner != effective_uid || !candidate.readable || reserved.count(candidate.title_id) ||+ activity(candidate.title_id) != NativeActivity::kStopped) continue;+ leftovers.push_back({candidate.title_id, homebrew_root / candidate.title_id, candidate.name, candidate.bytes,+ candidate.device, candidate.inode});+ reserved.insert(candidate.title_id);+ }+ std::sort(leftovers.begin(), leftovers.end(), [](const auto& first, const auto& second) {+ return first.title_id < second.title_id;+ });+ return leftovers;+}+} // namespace playden::runtimediff --git a/src/runtime/leftover_titles.h b/src/runtime/leftover_titles.hnew file mode 100644index 0000000..f5420ce--- /dev/null+++ b/src/runtime/leftover_titles.h@@ -0,0 +1,55 @@+// SPDX-License-Identifier: GPL-3.0-or-later+#pragma once++#include "runtime/install_service.h"++#include <cstdint>+#include <filesystem>+#include <functional>+#include <optional>+#include <string>+#include <sys/types.h>+#include <unordered_set>+#include <vector>++namespace playden::runtime {++struct LeftoverTitle {+ std::string title_id;+ std::filesystem::path title_path;+ std::string name;+ std::uint64_t bytes = 0;+ std::uint64_t device = 0;+ std::uint64_t inode = 0;+};++// Observations from lstat and a read-only walk. Paths are constructed by the+// detector from the root and validated title ID, never supplied by the lister.+struct LeftoverTitleCandidate {+ std::string title_id;+ bool is_directory = false;+ bool is_symlink = false;+ uid_t owner = 0;+ bool readable = false;+ std::string name;+ std::uint64_t bytes = 0;+ std::uint64_t device = 0;+ std::uint64_t inode = 0;+};++using LeftoverTitleLister = std::function<std::optional<std::vector<LeftoverTitleCandidate>>(+ const std::filesystem::path&, uid_t, const std::unordered_set<std::string>& excluded_title_ids)>;+using LeftoverTitleActivity = std::function<NativeActivity(const std::string&)>;++std::optional<std::vector<LeftoverTitleCandidate>> ListLeftoverTitleCandidates(+ const std::filesystem::path& homebrew_root, uid_t effective_uid,+ const std::unordered_set<std::string>& excluded_title_ids);++// An unread repository is never interpreted as an empty one. Any record,+// including an interrupted removal, reserves its title regardless of state.+std::vector<LeftoverTitle> FindLeftoverTitles(+ const std::filesystem::path& homebrew_root, bool repository_loaded,+ const std::vector<RuntimeInstall>& records, const LeftoverTitleActivity& activity,+ uid_t effective_uid, const LeftoverTitleLister& lister = ListLeftoverTitleCandidates);++} // namespace playden::runtimediff --git a/src/runtime/package_installer.cpp b/src/runtime/package_installer.cppindex 0914b64..e8c87c3 100644--- a/src/runtime/package_installer.cpp+++ b/src/runtime/package_installer.cpp@@ -116,15 +116,6 @@ bool IsHexDigest(const std::string& value) {});}-bool IsManagedTitle(const std::string& value) {- if (value.size() != 9 || value.compare(0, 4, "FAKE") != 0) return false;- if (!std::all_of(value.begin() + 4, value.end(), [](unsigned char byte) {- return byte >= '0' && byte <= '9';- }))- return false;- return value.compare("FAKE78000") >= 0 && value.compare("FAKE78999") <= 0;-}-bool IsSafeRelative(const std::string& value) {if (value.empty() || value.find('\\') != std::string::npos) return false;const fs::path path(value);diff --git a/tests/runtime_leftover_titles_test.cpp b/tests/runtime_leftover_titles_test.cppnew file mode 100644index 0000000..c2ce60e--- /dev/null+++ b/tests/runtime_leftover_titles_test.cpp@@ -0,0 +1,131 @@+// SPDX-License-Identifier: GPL-3.0-or-later+#include "runtime/leftover_titles.h"++#include <cstdio>+#include <cstdlib>+#include <filesystem>+#include <fstream>+#include <sys/stat.h>+#include <unistd.h>++using namespace playden::runtime;+namespace fs = std::filesystem;++#define CHECK(condition) do { if (!(condition)) { \+ std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #condition); \+ std::exit(1); } } while (false)++namespace {+constexpr uid_t kOwner = 1234;+NativeActivity Stopped(const std::string&) { return NativeActivity::kStopped; }++LeftoverTitleCandidate Candidate(std::string id = "FAKE78001") {+ return {std::move(id), true, false, kOwner, true, "Half-Life 2", 42, 3, 99};+}++std::vector<LeftoverTitle> Detect(const std::vector<LeftoverTitleCandidate>& candidates,+ const std::vector<RuntimeInstall>& records = {},+ const LeftoverTitleActivity& activity = Stopped) {+ return FindLeftoverTitles("/data/homebrew", true, records, activity, kOwner,+ [&](const fs::path&, uid_t, const std::unordered_set<std::string>&) { return candidates; });+}++void FindsOnlyUnrecordedOwnedStoppedTitles() {+ const auto found = Detect({Candidate()});+ CHECK(found.size() == 1);+ CHECK(found[0].title_id == "FAKE78001");+ CHECK(found[0].title_path == "/data/homebrew/FAKE78001");+ CHECK(found[0].name == "Half-Life 2");+ CHECK(found[0].bytes == 42);+ CHECK(found[0].device == 3 && found[0].inode == 99);++ auto foreign = Candidate(); foreign.owner = 999;+ auto symlink = Candidate(); symlink.is_symlink = true;+ auto file = Candidate(); file.is_directory = false;+ auto unreadable = Candidate(); unreadable.readable = false;+ CHECK(Detect({foreign, symlink, file, unreadable}).empty());+ CHECK(Detect({Candidate("FAKE77031"), Candidate("PPSA97031"), Candidate("FAKE79001"),+ Candidate("FAKE7800"), Candidate("FAKE780001"), Candidate("FAKE78a01"),+ Candidate("../FAKE78001")}).empty());+ CHECK(Detect({Candidate()}, {}, [](const std::string&) { return NativeActivity::kActive; }).empty());+ CHECK(Detect({Candidate()}, {}, [](const std::string&) { return NativeActivity::kUnknown; }).empty());+ CHECK(Detect({Candidate()}, {}, {}).empty());++ for (const auto state : {PreparationState::kDownloaded, PreparationState::kPreparing,+ PreparationState::kRegistering, PreparationState::kReady, PreparationState::kFailed,+ PreparationState::kUnavailable, PreparationState::kRemoving}) {+ RuntimeInstall record; record.title_id = "FAKE78001"; record.state = state;+ CHECK(Detect({Candidate()}, {record}).empty());+ record.journal = PreparationJournal{"remove-op", "remove", "unregister"};+ CHECK(Detect({Candidate()}, {record}).empty());+ }+}++void DoesNotScanAnUnreadRepositoryOrUseFailedListings() {+ bool scanned = false;+ CHECK(FindLeftoverTitles("/data/homebrew", false, {}, Stopped, kOwner,+ [&](const fs::path&, uid_t, const std::unordered_set<std::string>&) {+ scanned = true; return std::vector<LeftoverTitleCandidate>{Candidate()};+ }).empty());+ CHECK(!scanned);+ CHECK(FindLeftoverTitles("/data/homebrew", true, {}, Stopped, kOwner,+ [](const fs::path&, uid_t, const std::unordered_set<std::string>&) { return std::nullopt; }).empty());+}++struct TemporaryDirectory {+ TemporaryDirectory() {+ auto pattern = (fs::temp_directory_path() / "playden-leftovers-XXXXXX").string();+ std::vector<char> writable(pattern.begin(), pattern.end()); writable.push_back('\0');+ const auto made = ::mkdtemp(writable.data()); CHECK(made);+ path = fs::canonical(made);+ }+ ~TemporaryDirectory() { fs::remove_all(path); }+ fs::path path;+};++void ReadsRealDirectoriesWithoutFollowingLinks() {+ TemporaryDirectory temp;+ const auto title = temp.path / "homebrew/FAKE78001";+ fs::create_directories(title / "sce_sys");+ const std::string metadata = "{\"localizedParameters\":{\"en-US\":{\"titleName\":\"Half-Life 2\"}}}";+ std::ofstream(title / "sce_sys/param.json") << metadata;+ std::ofstream(title / "eboot.bin") << "game";+ fs::create_directories(temp.path / "outside");+ std::ofstream(temp.path / "outside/data") << "not counted";+ fs::create_directory_symlink(temp.path / "outside", title / "link");+ fs::create_symlink(temp.path / "outside/data", title / "linked-file");+ fs::create_directory_symlink(title, temp.path / "homebrew/FAKE78002");+ fs::create_directories(temp.path / "homebrew/FAKE77031");+ const auto found = FindLeftoverTitles(temp.path / "homebrew", true, {}, Stopped, ::geteuid());+ CHECK(found.size() == 1);+ CHECK(found[0].title_id == "FAKE78001");+ CHECK(found[0].name == "Half-Life 2");+ CHECK(found[0].bytes == metadata.size() + 4);+ CHECK(found[0].inode != 0);+ const auto excluded = ListLeftoverTitleCandidates(temp.path / "homebrew", ::geteuid(), {"FAKE78001"});+ CHECK(excluded && excluded->empty());++ fs::create_directory_symlink(temp.path / "homebrew", temp.path / "root-link");+ CHECK(FindLeftoverTitles(temp.path / "root-link", true, {}, Stopped, ::geteuid()).empty());+ CHECK(FindLeftoverTitles(temp.path / "missing", true, {}, Stopped, ::geteuid()).empty());+ fs::remove(title / "sce_sys/param.json");+ fs::create_symlink(temp.path / "outside/data", title / "sce_sys/param.json");+ const auto no_name = FindLeftoverTitles(temp.path / "homebrew", true, {}, Stopped, ::geteuid());+ CHECK(no_name.size() == 1 && no_name[0].name.empty());+ CHECK(no_name[0].bytes == 4);+ // A partial tree cannot produce a trustworthy size. Root can bypass modes+ // on host filesystems, so exercise the permission failure as an ordinary user.+ if (::geteuid() != 0) {+ CHECK(::chmod((title / "sce_sys").c_str(), 0000) == 0);+ const auto unreadable = FindLeftoverTitles(temp.path / "homebrew", true, {}, Stopped, ::geteuid());+ CHECK(::chmod((title / "sce_sys").c_str(), 0700) == 0);+ CHECK(unreadable.empty());+ }+}+} // namespace++int main() {+ FindsOnlyUnrecordedOwnedStoppedTitles();+ DoesNotScanAnUnreadRepositoryOrUseFailedListings();+ ReadsRealDirectoriesWithoutFollowingLinks();+}