Play your Steam library's Windows games on a jailbroken PS5.
C++
Import Windows games from drives and the drop folder
Leonardo Lobato8fddcb690376
diff --git a/cmake/HomebrewPS5.cmake b/cmake/HomebrewPS5.cmakeindex 05b0137..2a67a69 100644--- a/cmake/HomebrewPS5.cmake+++ b/cmake/HomebrewPS5.cmake@@ -128,7 +128,7 @@ function(playden_add_homebrew_ps5_title)$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions;-fno-rtti>)target_compile_features(playden_ps5_app PRIVATE cxx_std_20)target_link_libraries(playden_ps5_app PRIVATE playden_hui playden_library playden_steam_auth- playden_homebrew_controller playden_local_session_repository playden_steam_library playden_steam_cm playden_native_access playden_runtime_layout_migration)+ playden_homebrew_controller playden_local_session_repository playden_steam_library playden_steam_cm playden_native_access playden_runtime_layout_migration playden_local_store playden_local_copy)set(host_tool "${host}/ps5-native-tool")set(libc_prx "${host}/libc.prx")@@ -153,7 +153,7 @@ function(playden_add_homebrew_ps5_title)"${sdk_deps}/compiler-rt/libclang_rt.builtins-x86_64.a" $<TARGET_FILE:playden_hui> $<TARGET_FILE:playden_homebrew_ui> $<TARGET_FILE:playden_homebrew_model>$<TARGET_FILE:playden_homebrew_qr> $<TARGET_FILE:playden_homebrew_controller>$<TARGET_FILE:playden_homebrew_artwork_cache> $<TARGET_FILE:playden_homebrew_artwork> $<TARGET_FILE:playden_persistence_controller> $<TARGET_FILE:playden_persistence_service> $<TARGET_FILE:playden_preferences> $<TARGET_FILE:playden_library_controller> $<TARGET_FILE:playden_library_service> $<TARGET_FILE:playden_steam_library> $<TARGET_FILE:playden_steam_cm> $<TARGET_FILE:playden_steam_cm_codec> $<TARGET_FILE:playden_depot_codec>- $<TARGET_FILE:playden_local_session_repository> $<TARGET_FILE:playden_auth_diagnostics>+ $<TARGET_FILE:playden_local_session_repository> $<TARGET_FILE:playden_auth_diagnostics> $<TARGET_FILE:playden_local_store> $<TARGET_FILE:playden_local_copy>$<TARGET_FILE:playden_self_update> $<TARGET_FILE:playden_miniz> $<TARGET_FILE:playden_steam_http> $<TARGET_FILE:playden_steam_protocol>$<TARGET_FILE:playden_steam_auth>$<TARGET_FILE:playden_steam_core> $<TARGET_FILE:playden_diagnostic_log> $<TARGET_FILE:playden_storage>@@ -162,7 +162,7 @@ function(playden_add_homebrew_ps5_title)--end-group --as-needed ${link_stubs}WORKING_DIRECTORY "${out}/link"DEPENDS "${common_dialog_stub}" playden_ps5_runtime playden_ps5_shims playden_ps5_app playden_hui playden_homebrew_ui playden_homebrew_model ${title_database_targets}- playden_homebrew_qr playden_homebrew_controller playden_local_session_repository playden_auth_diagnostics+ playden_homebrew_qr playden_homebrew_controller playden_local_session_repository playden_auth_diagnostics playden_local_store playden_local_copyplayden_homebrew_artwork_cache playden_homebrew_artwork playden_persistence_controller playden_persistence_service playden_preferences playden_library_controller playden_library_service playden_steam_library playden_steam_cm playden_steam_cm_codec playden_depot_codecplayden_library playden_self_update playden_miniz playden_steam_auth playden_steam_http playden_steam_protocol playden_steam_core playden_diagnostic_log playden_storage playden_native_access playden_managed_directory playden_runtime_steamworks playden_runtime_artifact_hash playden_runtime_settings_storage playden_runtime_private_directories playden_runtime_session_reconciler playden_runtime_launch_service playden_runtime_install_service playden_runtime_installs playden_runtime_layout_migration playden_game_paths$<TARGET_OBJECTS:playden_ps5_runtime> $<TARGET_OBJECTS:playden_ps5_shims>@@ -173,7 +173,7 @@ function(playden_add_homebrew_ps5_title)$<TARGET_FILE:playden_library> $<TARGET_FILE:playden_native_access> $<TARGET_FILE:playden_managed_directory> $<TARGET_FILE:playden_runtime_steamworks> $<TARGET_FILE:playden_runtime_artifact_hash> $<TARGET_FILE:playden_runtime_settings_storage> $<TARGET_FILE:playden_runtime_private_directories> $<TARGET_FILE:playden_runtime_session_reconciler> $<TARGET_FILE:playden_runtime_launch_service> $<TARGET_FILE:playden_runtime_install_service> $<TARGET_FILE:playden_runtime_installs> $<TARGET_FILE:playden_runtime_layout_migration> $<TARGET_FILE:playden_game_paths>$<TARGET_FILE:playden_homebrew_qr> $<TARGET_FILE:playden_homebrew_controller>$<TARGET_FILE:playden_homebrew_artwork_cache> $<TARGET_FILE:playden_homebrew_artwork> $<TARGET_FILE:playden_persistence_controller> $<TARGET_FILE:playden_persistence_service> $<TARGET_FILE:playden_preferences> $<TARGET_FILE:playden_library_controller> $<TARGET_FILE:playden_library_service> $<TARGET_FILE:playden_steam_library> $<TARGET_FILE:playden_steam_cm> $<TARGET_FILE:playden_steam_cm_codec> $<TARGET_FILE:playden_depot_codec>- $<TARGET_FILE:playden_local_session_repository> $<TARGET_FILE:playden_auth_diagnostics>+ $<TARGET_FILE:playden_local_session_repository> $<TARGET_FILE:playden_auth_diagnostics> $<TARGET_FILE:playden_local_store> $<TARGET_FILE:playden_local_copy>"${gl_source}/native-app/ps5-pie.ld" "${gl_source}/native-app/app-symbols.map""${gl_sdk}/lib/libPS5OpenGL.a" "${sdk_deps}/compiler-rt/libclang_rt.builtins-x86_64.a"${PLAYDEN_STEAM_LINK_ARCHIVES}diff --git a/cmake/HomebrewUI.cmake b/cmake/HomebrewUI.cmakeindex e967417..fcc85a5 100644--- a/cmake/HomebrewUI.cmake+++ b/cmake/HomebrewUI.cmake@@ -239,7 +239,7 @@ target_compile_features(playden_homebrew_controller PUBLIC cxx_std_20)target_include_directories(playden_homebrew_controller PUBLIC "${CMAKE_SOURCE_DIR}/src")target_link_libraries(playden_homebrew_controller PUBLICplayden_homebrew_model playden_steam_core playden_auth_diagnostics playden_library_controller playden_persistence_controller playden_runtime_settings_storage- playden_homebrew_runtime_host playden_self_update)+ playden_homebrew_runtime_host playden_self_update playden_local_store)# Worker-owned per-game runtime composition (OWNER records, native inventory, kit layer).add_library(playden_homebrew_runtime_host STATIC "${CMAKE_SOURCE_DIR}/src/homebrew/runtime_host.cpp"@@ -400,7 +400,7 @@ target_compile_definitions(playden-homebrew PRIVATEPLAYDEN_HOMEBREW_CA_BUNDLE="${playden_staged_ca}")target_link_libraries(playden-homebrew PRIVATEplayden_homebrew_ui playden_homebrew_controller playden_local_session_repository- playden_library playden_steam_auth playden_steam_library playden_steam_cm PkgConfig::SDL2 OpenGL::GL)+ playden_library playden_steam_auth playden_steam_library playden_steam_cm playden_local_copy PkgConfig::SDL2 OpenGL::GL)add_dependencies(playden-homebrew playden_homebrew_assets playden_steam_trust)set_target_properties(playden-homebrew PROPERTIESRUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")@@ -461,7 +461,7 @@ if(BUILD_TESTING)PLAYDEN_HOMEBREW_ASSETS_DIR="${playden_homebrew_assets}")target_link_libraries(homebrew_host_input_test PRIVATE playden_homebrew_uiplayden_homebrew_controller playden_local_session_repository playden_library playden_steam_auth playden_steam_library playden_steam_cm- PkgConfig::SDL2 OpenGL::GL)+ playden_local_copy PkgConfig::SDL2 OpenGL::GL)add_test(NAME homebrew_host_input COMMAND homebrew_host_input_test "${CMAKE_SOURCE_DIR}/fixtures/ui-comparison")add_executable(homebrew_browse_test "${CMAKE_SOURCE_DIR}/tests/homebrew_browse_test.cpp")target_compile_features(homebrew_browse_test PRIVATE cxx_std_20)diff --git a/cmake/PlaydenCore.cmake b/cmake/PlaydenCore.cmakeindex de3efbc..b2f1507 100644--- a/cmake/PlaydenCore.cmake+++ b/cmake/PlaydenCore.cmake@@ -120,6 +120,27 @@ target_compile_features(playden_runtime_session_reconciler PUBLIC cxx_std_17)target_link_libraries(playden_runtime_session_reconciler PUBLIC playden_runtime_install_service playden_storagePRIVATE playden_steam_protocol)+# Windows games imported from a drive or the drop folder: the folder scanner, imports.json and+# the Local store's library source.+add_library(playden_local_store STATIC+ "${CMAKE_SOURCE_DIR}/src/stores/local/scanner.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/import_store.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/library_source.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/drop_folder_access.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/copy_plan.cpp")+target_include_directories(playden_local_store PUBLIC "${CMAKE_SOURCE_DIR}/src")+target_compile_features(playden_local_store PUBLIC cxx_std_17)+if(PLAYDEN_PLATFORM_PS5)+ target_compile_definitions(playden_local_store PRIVATE PLAYDEN_PLATFORM_PS5=1)+endif()+target_link_libraries(playden_local_store PUBLIC playden_storage playden_library Threads::Threads PRIVATE playden_steam_protocol)++# Host tool: what the import scanner sees in a game folder (tools/import-scan/main.cpp).+if(NOT PLAYDEN_PLATFORM_PS5)+ add_executable(import-scan "${CMAKE_SOURCE_DIR}/tools/import-scan/main.cpp")+ target_link_libraries(import-scan PRIVATE playden_local_store)+endif()+add_library(playden_native_access STATIC"${CMAKE_SOURCE_DIR}/src/platform/ps5/native_access.cpp""${CMAKE_SOURCE_DIR}/src/platform/ps5/native_access_posix.cpp")@@ -216,6 +237,31 @@ 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(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)+ # The console's walk by path (no descriptor-relative opens), run on the host.+ add_executable(local_scanner_native_policy_test+ "${CMAKE_SOURCE_DIR}/tests/local_scanner_test.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/scanner.cpp")+ target_include_directories(local_scanner_native_policy_test PRIVATE "${CMAKE_SOURCE_DIR}/src")+ target_compile_features(local_scanner_native_policy_test PRIVATE cxx_std_17)+ target_compile_definitions(local_scanner_native_policy_test PRIVATE PLAYDEN_PLATFORM_PS5=1)+ add_test(NAME local_scanner_native_policy COMMAND local_scanner_native_policy_test)+ add_executable(local_drop_folder_access_test "${CMAKE_SOURCE_DIR}/tests/local_drop_folder_access_test.cpp")+ target_link_libraries(local_drop_folder_access_test PRIVATE playden_local_store)+ add_test(NAME local_drop_folder_access COMMAND local_drop_folder_access_test)+ # The console's walk by path (no descriptor-relative opens), run on the host.+ add_executable(local_drop_folder_access_native_policy_test+ "${CMAKE_SOURCE_DIR}/tests/local_drop_folder_access_test.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/drop_folder_access.cpp")+ target_include_directories(local_drop_folder_access_native_policy_test PRIVATE "${CMAKE_SOURCE_DIR}/src")+ target_compile_features(local_drop_folder_access_native_policy_test PRIVATE cxx_std_17)+ target_compile_definitions(local_drop_folder_access_native_policy_test PRIVATE PLAYDEN_PLATFORM_PS5=1)+ add_test(NAME local_drop_folder_access_native_policy COMMAND local_drop_folder_access_native_policy_test)+ add_executable(local_import_store_test "${CMAKE_SOURCE_DIR}/tests/local_import_store_test.cpp")+ target_link_libraries(local_import_store_test PRIVATE playden_local_store)+ add_test(NAME local_import_store COMMAND local_import_store_test)add_executable(diagnostic_log_test "${CMAKE_SOURCE_DIR}/tests/diagnostic_log_test.cpp")target_link_libraries(diagnostic_log_test PRIVATE playden_diagnostic_log)add_test(NAME diagnostic_log COMMAND diagnostic_log_test)diff --git a/cmake/SteamBackend.cmake b/cmake/SteamBackend.cmakeindex 9453b4d..28b7536 100644--- a/cmake/SteamBackend.cmake+++ b/cmake/SteamBackend.cmake@@ -166,6 +166,17 @@ if(PLAYDEN_PLATFORM_PS5)target_compile_options(playden_steam_cm PRIVATE -fno-exceptions -fno-rtti)endif()+# "Copy into Playden" for imported games: the install service's runner for Store::kLocal (it+# removes partial copies with the depot installer's RemoveManagedInstall).+add_library(playden_local_copy STATIC "${CMAKE_SOURCE_DIR}/src/stores/local/copy_runner.cpp")+target_include_directories(playden_local_copy PUBLIC "${CMAKE_SOURCE_DIR}/src")+target_compile_features(playden_local_copy PUBLIC cxx_std_17)+target_link_libraries(playden_local_copy PUBLIC playden_local_store playden_steam_cm playden_steam_core)+if(PLAYDEN_PLATFORM_PS5)+ target_compile_definitions(playden_local_copy PRIVATE PLAYDEN_PLATFORM_PS5=1)+ target_compile_options(playden_local_copy PRIVATE -fno-exceptions -fno-rtti)+endif()+set(PLAYDEN_STEAM_CA_BUNDLE "${PLAYDEN_STEAM_PREFIX}/cacert.pem"CACHE INTERNAL "Verified Steam HTTPS trust bundle")set(PLAYDEN_STEAM_LINK_ARCHIVES@@ -251,6 +262,18 @@ function(playden_register_steam_backend_tests)add_executable(install_volumes_test "${CMAKE_SOURCE_DIR}/tests/install_volumes_test.cpp")target_link_libraries(install_volumes_test PRIVATE playden_steam_core)add_test(NAME install_volumes COMMAND install_volumes_test)+ add_executable(local_copy_test "${CMAKE_SOURCE_DIR}/tests/local_copy_test.cpp")+ target_link_libraries(local_copy_test PRIVATE playden_local_copy)+ add_test(NAME local_copy COMMAND local_copy_test)+ # The console's walk by path (no descriptor-relative opens), run on the host.+ add_executable(local_copy_native_policy_test "${CMAKE_SOURCE_DIR}/tests/local_copy_test.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/copy_runner.cpp" "${CMAKE_SOURCE_DIR}/src/stores/local/copy_plan.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/scanner.cpp" "${CMAKE_SOURCE_DIR}/src/stores/local/import_store.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/library_source.cpp"+ "${CMAKE_SOURCE_DIR}/src/stores/local/drop_folder_access.cpp")+ target_compile_definitions(local_copy_native_policy_test PRIVATE PLAYDEN_PLATFORM_PS5=1)+ target_link_libraries(local_copy_native_policy_test PRIVATE playden_steam_cm playden_storage playden_library)+ add_test(NAME local_copy_native_policy COMMAND local_copy_native_policy_test)add_executable(depot_installer_test "${CMAKE_SOURCE_DIR}/tests/depot_installer_test.cpp")target_link_libraries(depot_installer_test PRIVATE playden_steam_cm)add_test(NAME depot_installer COMMAND depot_installer_test)diff --git a/src/app/library_controller.cpp b/src/app/library_controller.cppindex 9721733..6ec4fc5 100644--- a/src/app/library_controller.cpp+++ b/src/app/library_controller.cpp@@ -66,6 +66,9 @@ void LibraryController::Refresh() {Loading();request_=steam_.Refresh(account_,generation_);offline_=false;if(!request_) model_.UpdateLibrary({{},false,"Library is busy. Retry to refresh.",model_.state().library_offline,model_.state().account().cache_available,{}});}+void LibraryController::Refresh(Store store) {+ for(auto& other:others_) if(other.source->store()==store) RefreshOther(other);+}void LibraryController::Reset() {steam_.Invalidate(++generation_);request_=0;account_=0;clearing_=false;clear_result_.reset();offline_=false;needs_renewal_=false;}diff --git a/src/app/library_controller.h b/src/app/library_controller.hindex 09ba252..90c8677 100644--- a/src/app/library_controller.h+++ b/src/app/library_controller.h@@ -20,6 +20,8 @@ class LibraryController {// Steam: the signed-in account's library.void Activate(std::uint64_t account_id,bool offline);void Refresh();+ // One store's library only (not Steam's): its games changed (an imported game was added or removed).+ void Refresh(Store store);// Steam only: Reset drops Steam's pending work, Clear deletes Steam's saved library.void Reset();void Clear();diff --git a/src/homebrew/browse.cpp b/src/homebrew/browse.cppindex c3cda98..918552c 100644--- a/src/homebrew/browse.cpp+++ b/src/homebrew/browse.cpp@@ -18,12 +18,18 @@ constexpr int kLibraryColumns = 6;bool catalog_available(const State& state){+ // Steam's catalog needs its account; another store's games (imported ones) are listed signed out too.return state.account().cache_available || state.account().authentication == AuthState::SignedIn ||- state.account().authentication == AuthState::Offline;+ state.account().authentication == AuthState::Offline ||+ std::any_of(state.games.begin(), state.games.end(), [](const Game& game) { return game.id.store != Store::kSteam; });}bool installed(const State& state, GameId id){+ // A game playing in place from its own folder (imported) counts as installed here.+ if (std::any_of(state.external_games.begin(), state.external_games.end(),+ [id](const LiveExternalGame& value) { return value.game_id == id; }))+ return true;return std::any_of(state.installations.begin(), state.installations.end(),[&state, id](const Installation& value) {// Live installs are known from the install service's records, evendiff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex c13a297..447a082 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -2,7 +2,9 @@#include "diagnostics/redaction.h"#include "steam/install_volumes.h"#include "steam/secret.h"+#include <algorithm>#include <chrono>+#include <utility>namespace playden::homebrew {namespace {@@ -327,6 +329,7 @@ void Controller::Tick() {lap(3);TickStoreDetails();lap(4);+ TickImports();TickInstalls();TickSelfUpdate();lap(5);@@ -757,7 +760,12 @@ void Controller::TickRuntime() {}// A quit to Home waits for the running preparation; the queued ones would be cut off.runtime_->HoldPreparations(model_.state().quit_waiting);- for (const auto id : model_.TakeRuntimePrepareRequests()) RequestPrepare(id);+ // A drop-folder game's files are opened before each Play and Prepare.+ std::vector<GameId> opened_plays, opened_prepares;+ ReleaseDropFolderOpens(opened_plays, opened_prepares);+ for (const auto id : model_.TakeRuntimePrepareRequests())+ if (!HoldForDropFolder(id, false)) RequestPrepare(id);+ for (const auto id : opened_prepares) RequestPrepare(id);// A game Playden still holds back moves to the front once it is asked for.// The latest choice wins: a game Playden still holds back moves first once it is asked for.for (const auto id : model_.TakePrepareNextRequests()) {@@ -805,6 +813,8 @@ void Controller::TickRuntime() {}}auto plays = model_.TakeRuntimePlayRequests();+ std::erase_if(plays, [this](GameId id) { return HoldForDropFolder(id, true); });+ plays.insert(plays.end(), opened_plays.begin(), opened_plays.end());{const auto prepared_shared = runtime_->Shared();const auto& prepared = *prepared_shared;@@ -1275,8 +1285,13 @@ void Controller::TickRuntime() {announce_ready();}void Controller::BeginUninstall(GameId id, bool delete_saves) {- // Only Playden's own installs are uninstalled: a game played in place is never deleted.- if (const auto* location = LocationOf(id); location && !location->managed()) return;+ // Only Playden's own installs are uninstalled: a game played in place is never deleted. An imported+ // one leaves the library instead, once its PS5 title is gone (its files stay, its bottle is kept).+ const auto* location = LocationOf(id);+ const bool external = location && !location->managed();+ if (external && !(id.store == Store::kLocal && local_)) return;+ if (external) delete_saves = false;+ if (!runtime_ && external) { local_->Remove(id.value); return; }// Without the runtime there is no PS5 title to remove: the download goes as before.if (!runtime_) { if (installs_) installs_->Uninstall(id); return; }// A preparation still waiting would make a title for files about to be deleted: it leaves the queue.@@ -1324,26 +1339,150 @@ void Controller::TickUninstalls() {++play_notes_revision_;break;// The title is gone, so no title points at the files about to be deleted.- case UninstallAction::Kind::kRemoveDownload: if (installs_) installs_->Uninstall(id); ++play_notes_revision_; break;+ case UninstallAction::Kind::kRemoveDownload:+ if (const auto* location = LocationOf(id); location && !location->managed()) {+ if (id.store == Store::kLocal && local_) local_->Remove(id.value);+ } else if (installs_) {+ installs_->Uninstall(id);+ }+ ++play_notes_revision_;+ break;case UninstallAction::Kind::kStop: StopUninstall(id, action.reason); break;}}}void Controller::ProjectLocations(const std::vector<steam::InstalledGame>& managed) {- auto locations = installs::ProjectInstalledLocations(- managed, external_locations_ ? external_locations_() : std::vector<installs::InstalledLocation>{});+ auto external = external_locations_ ? external_locations_() : std::vector<installs::InstalledLocation>{};+ if (local_)+ for (auto& location : local_->Locations()) external.push_back(std::move(location));+ auto locations = installs::ProjectInstalledLocations(managed, external);if (locations != locations_) {locations_ = std::move(locations);++locations_revision_;+ // The model's details page offers Play for games played in place too.+ std::vector<LiveExternalGame> games;+ for (const auto& location : locations_)+ if (!location.managed()) games.push_back({location.game, location.volume_id, location.available, location.reason});+ if (games != external_projected_) {+ external_projected_ = games;+ model_.UpdateExternalGames(std::move(games));+ }}game_roots_.clear();game_volumes_.clear();for (const auto& location : locations_)- if (location.available) {+ if (location.available && location.reason.empty()) {game_roots_[location.game] = location.root;game_volumes_[location.game] = location.volume_id;}}+void Controller::TickImports() {+ if (!local_) return;+ // The locations reflect a Local snapshot once one was seen on an earlier tick (TickInstalls projects them).+ const bool locations_known = local_revision_ != 0;+ for (const auto& request : model_.TakeImportRequests()) {+ switch (request.kind) {+ case ImportRequestKind::LookForGames: local_->LookForGames(); break;+ case ImportRequestKind::Add: local_->Add(request.volume_id, request.folder, request.program); break;+ case ImportRequestKind::Remove:+ if (request.game_id.store == Store::kLocal) held_import_removals_.push_back(request.game_id);+ break;+ case ImportRequestKind::CopyIntoPlayden:+ if (request.game_id.store == Store::kLocal) {+ // The copy's install record carries the game's runtime number (given out now if it has+ // none); a game set up from its drive folder keeps its PS5 title, which follows the copy.+ const auto number = runtime_ ? RuntimeApp(*runtime_, request.game_id).value_or(0u) : 0u;+ local_->CopyIntoPlayden(request.game_id.value, request.volume_id, number);+ }+ break;+ case ImportRequestKind::MoveIntoPlayden:+ if (request.game_id.store == Store::kLocal) {+ const auto number = runtime_ ? RuntimeApp(*runtime_, request.game_id).value_or(0u) : 0u;+ local_->MoveIntoPlayden(request.game_id.value, number);+ }+ break;+ }+ }+ if (locations_known)+ for (const auto& id : std::exchange(held_import_removals_, {})) {+ // A game with a folder to play from goes like an uninstall: its PS5 title first.+ if (LocationOf(id)) BeginUninstall(id, false);+ else local_->Remove(id.value);+ }+ // A drive plugged in or out: its games become (un)available and its folders are looked at.+ const auto now = std::chrono::steady_clock::now();+ if (now - local_volumes_checked_at_ >= std::chrono::seconds(5)) {+ local_->CheckVolumes();+ local_volumes_checked_at_ = now;+ }+ const auto snapshot = local_->Snapshot();+ // Once imports.json is read: copies that finished or stopped while Playden was closed are settled.+ // A move a restart interrupted is finished then too, once the runtime has started (its PS5 title+ // follows the folder).+ if (!copies_reconciled_ && snapshot->loaded && installs_ &&+ (!runtime_ || runtime_->Shared()->status != RuntimeHostStatus::kStarting)) {+ copies_reconciled_ = true;+ auto* installs = installs_; // outlives the Local store's worker+ auto* runtime = runtime_; // so does the runtime host+ local_->Reconcile([installs] { return installs->Snapshot(); }, [installs](const GameId& id) { installs->Cancel(id); },+ [runtime](const GameId& id) { return runtime ? RuntimeApp(*runtime, id).value_or(0u) : 0u; });+ // A copy restored at start waited for imports.json: it carries on now.+ installs_->RetryWaiting(Store::kLocal);+ }+ if (snapshot->revision == local_revision_) return;+ local_revision_ = snapshot->revision;+ std::vector<LiveImportCandidate> candidates;+ for (const auto& candidate : snapshot->candidates) {+ LiveImportCandidate live{candidate.volume, candidate.folder, candidate.path, candidate.title, {},+ candidate.total_bytes, candidate.ascii_ok, candidate.partial, {}};+ for (const auto& program : candidate.programs) live.programs.push_back({program.path, program.bytes, program.helper});+ if (!candidate.imported_id.empty()) live.imported = GameId::Local(candidate.imported_id);+ candidates.push_back(std::move(live));+ }+ model_.UpdateImportCandidates(std::move(candidates), snapshot->scanning);+ // Added or removed games: the Local library loads again (Steam's is left alone).+ if (snapshot->games_revision != local_games_revision_) {+ local_games_revision_ = snapshot->games_revision;+ if (library_) library_->Refresh(Store::kLocal);+ }+}+bool Controller::HoldForDropFolder(GameId id, bool play) {+ if (id.store != Store::kLocal || !local_) return false;+ const auto* location = LocationOf(id);+ if (!location || location->managed() || !location->available ||+ location->volume_id != stores::local::ImportStore::kDropFolderVolume)+ return false;+ auto& open = drop_folder_opens_[id];+ if (!open.request && !(open.request = local_->OpenForPlay(id.value))) {+ drop_folder_opens_.erase(id);+ model_.NotifyPlayStopped(id, "Playden is busy. Press Play again in a moment.");+ return true;+ }+ (play ? open.play : open.prepare) = true;+ play_notes_[id] = "Opening the game's files…";+ ++play_notes_revision_;+ return true;+}+void Controller::ReleaseDropFolderOpens(std::vector<GameId>& plays, std::vector<GameId>& prepares) {+ if (drop_folder_opens_.empty() || !local_) return;+ const auto snapshot = local_->Snapshot();+ for (auto it = drop_folder_opens_.begin(); it != drop_folder_opens_.end();) {+ const auto outcome = std::find_if(snapshot->outcomes.begin(), snapshot->outcomes.end(),+ [&](const stores::local::ImportOutcome& o) { return o.request == it->second.request; });+ if (outcome == snapshot->outcomes.end()) { ++it; continue; }+ const auto id = it->first;+ play_notes_.erase(id);+ ++play_notes_revision_;+ if (!outcome->ok) {+ model_.NotifyPlayStopped(id, outcome->message);+ } else {+ if (it->second.play) plays.push_back(id);+ // Play prepares a game that needs it on its own.+ else if (it->second.prepare) prepares.push_back(id);+ }+ it = drop_folder_opens_.erase(it);+ }+}const installs::InstalledLocation* Controller::LocationOf(GameId id) const {for (const auto& location : locations_)if (location.game == id) return &location;@@ -1499,6 +1638,13 @@ void Controller::SubmitPrepare(GameId id, bool announce) {if (!app) return;if (announce) ready_watch_[id] = {false, std::chrono::steady_clock::now()};else ready_watch_.erase(id);+ // An imported game starts the program chosen when it was added: Game settings has it before its+ // first preparation, unless the player picked another one there.+ if (id.store == Store::kLocal && local_) {+ const auto snapshot = local_->Snapshot();+ for (const auto& game : snapshot->games)+ if (game.id == id.value) model_.SeedGameProgram(id, game.program);+ }RuntimePrepareRequest request{*app, root->second, game_volumes_[id], {}};request.game = id;for (const auto& game : model_.state().games) if (game.id == id) request.title_name = game.title;diff --git a/src/homebrew/controller.hpp b/src/homebrew/controller.hppindex da0ca71..aca5e5e 100644--- a/src/homebrew/controller.hpp+++ b/src/homebrew/controller.hpp@@ -13,6 +13,7 @@#include "homebrew/runtime_host.hpp"#include "stores/registry.h"#include "stores/steam.h"+#include "stores/local/library_source.h"#include "steam/download_rate.h"#include "update/update_service.h"#include <chrono>@@ -65,6 +66,11 @@ class Controller {void SetExternalLocations(std::function<std::vector<installs::InstalledLocation>()> locations) {external_locations_ = std::move(locations);}+ // The Local store (imported games), registered in the registry as Store::kLocal's library. Its+ // games playing in place become external locations next to SetExternalLocations' ones; the+ // model's import requests (look for games, add, remove) go to it, and removing an imported game+ // removes its PS5 title first, keeping its bottle as kept data. Set before the first Tick.+ void SetLocalLibrary(stores::local::LocalLibrarySource* local) { local_ = local; }Controller(const Controller&) = delete;Controller& operator=(const Controller&) = delete;private:@@ -123,6 +129,29 @@ class Controller {std::uint64_t locations_revision_ = 0;std::uint64_t runtime_locations_revision_ = 0;void ProjectLocations(const std::vector<steam::InstalledGame>& managed);+ // The Local store: model requests in, candidates and library changes out.+ void TickImports();+ stores::local::LocalLibrarySource* local_ = nullptr;+ std::uint64_t local_revision_ = 0;+ std::uint64_t local_games_revision_ = 0;+ std::chrono::steady_clock::time_point local_volumes_checked_at_{};+ std::vector<LiveExternalGame> external_projected_;+ // Removals asked for before the Local store's first snapshot reached the locations: until then a+ // game's folder (and so its PS5 title's removal first) isn't known.+ std::vector<GameId> held_import_removals_;+ // Copies into Playden were reconciled with the install service after the first imports.json read.+ bool copies_reconciled_ = false;+ // Play and Prepare of a game playing from the drop folder wait for its files to be opened+ // (LocalLibrarySource::OpenForPlay); a failure stops them with the walk's sentence.+ struct DropFolderOpen {+ std::uint64_t request = 0;+ bool play = false, prepare = false;+ };+ std::map<GameId, DropFolderOpen> drop_folder_opens_;+ // True when the request waits (or was stopped) for the drop folder.+ bool HoldForDropFolder(GameId id, bool play);+ // Finished openings: their requests go on (into plays and prepares) or stop.+ void ReleaseDropFolderOpens(std::vector<GameId>& plays, std::vector<GameId>& prepares);const installs::InstalledLocation* LocationOf(GameId id) const;// Whether Cloud saves apply to the game: a Cloud service exists and the game's store has them.bool CloudCapable(GameId id) const { return cloud_ && Traits(id.store).cloud_saves; }diff --git a/src/homebrew/host.cpp b/src/homebrew/host.cppindex 85e0631..d84c591 100644--- a/src/homebrew/host.cpp+++ b/src/homebrew/host.cpp@@ -7,6 +7,8 @@#include "homebrew/simulation.hpp"#include "homebrew/controller.hpp"#include "steam/install_volumes.h"+#include "stores/local/copy_runner.h"+#include "stores/local/library_source.h"#include "app/diagnostic_log.h"#include "runtime/settings_storage.h"#include "homebrew/input_adapter.hpp"@@ -424,6 +426,9 @@ int main(int argc, char** argv) {std::unique_ptr<playden::steam::CdnClient> install_cdn;std::unique_ptr<playden::steam::SteamInstallRunner> install_runner;std::unique_ptr<playden::LocalSessionRepository> install_sessions;+ std::unique_ptr<playden::Storage> import_storage; // imports.json; declared first: the copy runner and the Local store use it+ std::unique_ptr<playden::stores::local::ImportStore> imports;+ std::unique_ptr<playden::stores::local::LocalCopyRunner> copy_runner; // declared first: the install service uses itstd::unique_ptr<playden::steam::InstallService> installs;std::unique_ptr<playden::steam::CmClient> cloud_cm;std::unique_ptr<playden::steam::HttpClient> cloud_http;@@ -524,8 +529,18 @@ int main(int argc, char** argv) {candidates.push_back({"extra" + std::to_string(index + 1), "Extra drive " + std::to_string(index + 1),mount.string(), (mount / "playden" / "games").string()});}- installs = std::make_unique<playden::steam::InstallService>(*install_runner,- playden::steam::DiscoverInstallVolumes(candidates));+ // Imported games: imports.json, and "Copy into Playden" as the install service's Local runner.+ import_storage = std::make_unique<playden::Storage>(options->data_directory.string());+ imports = std::make_unique<playden::stores::local::ImportStore>(*import_storage);+ playden::stores::local::LocalCopyOptions copy_options;+ copy_options.log = [log_directory](const std::string& line) {+ playden::app::AppendLogLine(log_directory, "install.log", line);+ };+ copy_runner = std::make_unique<playden::stores::local::LocalCopyRunner>(*imports, std::move(copy_options));+ installs = std::make_unique<playden::steam::InstallService>(+ playden::installs::InstallService::Runners{{playden::Store::kSteam, install_runner.get()},+ {playden::Store::kLocal, copy_runner.get()}},+ playden::steam::DiscoverInstallVolumes(candidates));cloud_cm = std::make_unique<playden::steam::CmClient>(PLAYDEN_HOMEBREW_CA_BUNDLE, *library_storage);cloud_http = std::make_unique<playden::steam::HttpClient>(PLAYDEN_HOMEBREW_CA_BUNDLE);cloud_runner = std::make_unique<playden::steam::SteamCloudRunner>(*cloud_cm, *cloud_http);@@ -543,8 +558,33 @@ int main(int argc, char** argv) {stores->Add(std::make_unique<playden::stores::SteamAccount>(*auth, *sessions, auth_log));stores->Add(std::make_unique<playden::stores::SteamLibrarySource>(*library_service));stores->Add(std::make_unique<playden::stores::SteamDetailsSource>(*app_details));+ // Imported games: <data>/playden-import is the drop folder, playden/import on each extra drive.+ playden::stores::local::LocalLibraryConfig import_config;+ import_config.volumes = {{playden::steam::InstallService::kPrimaryVolumeId, options->data_directory.string(),+ "playden-import", false}};+ for (std::size_t index = 0; index < options->extra_volumes.size(); ++index)+ import_config.volumes.push_back({"extra" + std::to_string(index + 1), options->extra_volumes[index].string(),+ "playden/import", false});+ import_config.log = [log_directory](const std::string& line) {+ playden::app::AppendLogLine(log_directory, "install.log", line);+ };+ import_config.copy_into_playden = [service = installs.get(), runner = copy_runner.get()](+ const playden::stores::local::ImportEntry& entry, const std::string& source,+ const std::string& volume_id, std::uint32_t runtime_number) {+ return runner->Enqueue(*service, entry, source, volume_id, runtime_number);+ };+ import_config.move_into_playden = [service = installs.get(), runner = copy_runner.get()](+ const playden::stores::local::ImportEntry& entry, const std::string& source,+ std::uint32_t runtime_number) {+ return runner->Move(*service, entry, source, runtime_number);+ };+ auto local_library = std::make_unique<playden::stores::local::LocalLibrarySource>(*imports, std::move(import_config));+ auto* local = local_library.get();+ copy_runner->SetChanged([local] { local->Reload(); });+ stores->Add(std::move(local_library));services = std::make_unique<playden::homebrew::Controller>(simulation, *stores, persistence.get(), installs.get(),cloud.get(), nullptr, auth_log);+ services->SetLocalLibrary(local);store_details = std::make_unique<playden::steam::StoreDetailsService>(playden::homebrew::StoreFetch(PLAYDEN_HOMEBREW_CA_BUNDLE));services->SetStoreDetails(store_details.get());}diff --git a/src/homebrew/ps5.cpp b/src/homebrew/ps5.cppindex 780f8fb..6a855a9 100644--- a/src/homebrew/ps5.cpp+++ b/src/homebrew/ps5.cpp@@ -7,6 +7,8 @@#include "homebrew/store_art.hpp"#include "homebrew/controller.hpp"#include "steam/install_volumes.h"+#include "stores/local/copy_runner.h"+#include "stores/local/library_source.h"#include "app/diagnostic_log.h"#include "platform/ps5/title_remover.h"#include "platform/ps5/title_runtime.h"@@ -153,8 +155,8 @@ std::vector<playden::steam::InstallRequest> take_install_requests(bool runtime_rFILE* request = std::fopen(request_path.c_str(), "r");if (!request) return {};std::string text;- char line[64] = {};- while (text.size() < 4096 && std::fgets(line, sizeof(line), request)) {+ char line[512] = {};+ while (text.size() < 16384 && std::fgets(line, sizeof(line), request)) {// Reject an overlong line rather than executing its truncated prefix.if (!std::strchr(line, '\n') && !std::feof(request)) {int value;@@ -554,7 +556,51 @@ int main() {for (const auto& volume : install_volumes)playden::app::AppendLogLine(log_root, "install.log", "volume id=" + volume.id + " root=" + volume.games_root +" writable=" + (volume.available ? "1" : "0"));- playden::steam::InstallService installs(install_runner, std::move(install_volumes));+ // imports.json, declared before the install service and the registry: the copy runner and the+ // Local store read it until they stop.+ std::unique_ptr<playden::Storage> import_storage;+ std::unique_ptr<playden::stores::local::ImportStore> imports;+ std::unique_ptr<playden::stores::local::LocalCopyRunner> copy_runner;+ if (!paths.runtime_data_root.empty()) {+ import_storage = std::make_unique<playden::Storage>(+ (std::filesystem::path(paths.runtime_data_root) / "runtime-state").string());+ imports = std::make_unique<playden::stores::local::ImportStore>(*import_storage);+ // "Copy into Playden": imported games become managed installs.+ playden::stores::local::LocalCopyOptions copy_options;+ copy_options.log = [](const std::string& line) { playden::app::AppendLogLine(log_root, "install.log", line); };+ // A game set up to play from its folder keeps its PS5 title: the title follows the copy,+ // or the move's rename runs under the title's lease (RuntimeHost::Relocate).+ copy_options.relocate = [host = runtime_host.get(), volumes = install_volumes](+ const playden::stores::local::LocalRelocation& relocation) {+ using Local = playden::stores::local::LocalRelocationResult;+ using Runtime = playden::homebrew::RuntimeRelocationResult;+ Local answer;+ if (!host) {+ answer.outcome = Local::Outcome::kBusy;+ answer.message = "The runtime services are unavailable.";+ return answer;+ }+ playden::homebrew::RuntimeRelocation request;+ request.runtime_number = relocation.runtime_number;+ request.to = relocation.root;+ request.volume_id = relocation.volume_id;+ if (request.volume_id.empty())+ for (const auto& volume : volumes)+ if (relocation.root.rfind(volume.games_root + "/", 0) == 0) request.volume_id = volume.id;+ request.move_files = relocation.move_files;+ const auto result = host->Relocate(std::move(request), std::chrono::minutes(2));+ answer.outcome = result.outcome == Runtime::Outcome::kDone ? Local::Outcome::kDone+ : result.outcome == Runtime::Outcome::kBusy ? Local::Outcome::kBusy+ : Local::Outcome::kFailed;+ answer.message = result.message;+ return answer;+ };+ copy_options.as_root = [](const std::function<bool()>& work) { return playden::ps5::WithRoot(work); };+ copy_runner = std::make_unique<playden::stores::local::LocalCopyRunner>(*imports, std::move(copy_options));+ }+ playden::installs::InstallService::Runners install_runners{{playden::Store::kSteam, &install_runner}};+ if (copy_runner) install_runners[playden::Store::kLocal] = copy_runner.get();+ playden::steam::InstallService installs(std::move(install_runners), std::move(install_volumes));// A games folder made over FTP is often root's and 0755: checked (and opened up) as a download starts.// Only the games folder itself: the primary volume's parent is /data, never chmodded.installs.SetFolderCheck([](const std::string& games_root) {@@ -569,7 +615,34 @@ int main() {stores.Add(std::make_unique<playden::stores::SteamAccount>(auth, sessions, auth_log));stores.Add(std::make_unique<playden::stores::SteamLibrarySource>(library_service));stores.Add(std::make_unique<playden::stores::SteamDetailsSource>(app_details));+ // Imported games: imports.json next to installs.json in the runtime state folder; their+ // folders in /data/playden-import and playden/import on each drive (a drive counts while mounted).+ playden::stores::local::LocalLibrarySource* local_library = nullptr;+ if (imports) {+ playden::stores::local::LocalLibraryConfig import_config;+ import_config.log = [](const std::string& line) { playden::app::AppendLogLine(log_root, "install.log", line); };+ // A drop-folder game's root-owned FTP files are opened (0777/0666) at Add and before each Play.+ playden::stores::local::DropFolderAccessOptions drop_access;+ drop_access.as_root = [](const std::function<bool()>& work) { return playden::ps5::WithRoot(work); };+ drop_access.log = import_config.log;+ import_config.open_drop_folder = playden::stores::local::MakeDropFolderOpener(std::move(drop_access));+ import_config.copy_into_playden = [&installs, runner = copy_runner.get()](+ const playden::stores::local::ImportEntry& entry, const std::string& source,+ const std::string& volume_id, std::uint32_t runtime_number) {+ return runner->Enqueue(installs, entry, source, volume_id, runtime_number);+ };+ import_config.move_into_playden = [&installs, runner = copy_runner.get()](+ const playden::stores::local::ImportEntry& entry, const std::string& source,+ std::uint32_t runtime_number) {+ return runner->Move(installs, entry, source, runtime_number);+ };+ auto source = std::make_unique<playden::stores::local::LocalLibrarySource>(*imports, std::move(import_config));+ local_library = source.get();+ copy_runner->SetChanged([local_library] { local_library->Reload(); });+ stores.Add(std::move(source));+ }playden::homebrew::Controller services(simulation, stores, &persistence, &installs, &cloud, runtime_host.get(), auth_log);+ services.SetLocalLibrary(local_library);playden::steam::StoreDetailsService store_details(playden::homebrew::StoreFetch(paths.trust_store));services.SetStoreDetails(&store_details);services.SetSelfUpdate(&self_update);@@ -616,6 +689,8 @@ int main() {std::array<hui::PadSample, 64> samples{};bool presented = false;bool was_connected = true;+ std::vector<playden::GameId> dev_prepares; // imported games the request file asked to prepare+ std::uint64_t dev_prepares_until = 0;std::uint64_t last_render_us = 0;int slow_frames = 0;for (;;) {@@ -705,16 +780,57 @@ int main() {#if !PLAYDEN_UI_SIMULATIONfor (const auto& request : take_install_requests(native_access.runtime_ready)) {using Action = playden::steam::InstallRequest::Action;+ using playden::homebrew::ImportRequest;+ using playden::homebrew::ImportRequestKind;const char* action = "queued";- const auto game = playden::GameId::Steam(request.app);- if (request.action == Action::kUninstall) { action = "uninstall"; installs.Uninstall(game); }+ const auto& game = request.game;+ if (request.action == Action::kCopy) {+ action = "copy";+ simulation.RequestImport({ImportRequestKind::CopyIntoPlayden, game, request.volume, {}, {}});+ } else if (request.action == Action::kMove) {+ action = "move";+ simulation.RequestImport({ImportRequestKind::MoveIntoPlayden, game, {}, {}, {}});+ } else if (request.action == Action::kImport) {+ // The same path as the Import sheet's add: the Local store checks the folder and program.+ action = "import";+ simulation.RequestImport({ImportRequestKind::Add, {}, request.volume, request.folder, request.program});+ } else if (game.store == playden::Store::kLocal) {+ if (request.action == Action::kUninstall) {+ action = "remove";+ simulation.RequestImport({ImportRequestKind::Remove, game, {}, {}, {}});+ } else {+ // Prepared once the Local store has said where the game's folder is.+ action = "prepare";+ dev_prepares.push_back(game);+ }+ } else if (request.action == Action::kUninstall) { action = "uninstall"; installs.Uninstall(game); }else if (request.action == Action::kCancel) { action = "cancel"; installs.Cancel(game); }else installs.Install(game);playden::app::AppendLogLine(log_root, "install.log", "install-request " + std::string(action) +- " app=" + std::to_string(request.app));+ (request.action == Action::kImport+ ? " volume=" + request.volume + " folder=" + request.folder ++ " program=" + request.program+ : " game=" + playden::ToKey(game)));}+ dev_prepares_until = now + 60ull * 1000000ull;#endif}+#if !PLAYDEN_UI_SIMULATION+ // dev/install-request.txt's imported games to prepare, once their folder is known (a minute at most).+ if (!dev_prepares.empty()) {+ for (auto it = dev_prepares.begin(); it != dev_prepares.end();) {+ if (!simulation.state().runtime_games.count(*it)) { ++it; continue; }+ simulation.RequestRuntimePrepare(*it);+ it = dev_prepares.erase(it);+ }+ if (now > dev_prepares_until) {+ for (const auto& game : dev_prepares)+ playden::app::AppendLogLine(log_root, "install.log", "install-request prepare dropped game=" ++ playden::ToKey(game) + ": no folder to play from");+ dev_prepares.clear();+ }+ }+#endif// Work advances only through the documented manual fixture clock.pad.tick(1.0F / 60.0F);}diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 4fc6852..a91f7ea 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -1153,6 +1153,28 @@ rt::NativeActivity RuntimeHost::ActivityNow(std::uint32_t app_id, std::chrono::mreturn snapshot_.activity.at(app_id).first;}+RuntimeRelocationResult RuntimeHost::Relocate(RuntimeRelocation request, std::chrono::milliseconds timeout) {+ RuntimeRelocationResult busy;+ busy.outcome = RuntimeRelocationResult::Outcome::kBusy;+ busy.message = "Playden is busy with this game. Try again in a moment.";+ auto pending = std::make_shared<PendingRelocation>();+ pending->request = std::move(request);+ std::unique_lock<std::mutex> lock(mutex_);+ if (stopping_) return busy;+ relocation_requests_.push_back(pending);+ wake_.notify_all();+ const auto settled = [&] { return pending->result.has_value() || (stopping_ && !pending->started); };+ if (!published_.wait_for(lock, timeout, [&] { return settled() || pending->started; })) {+ // Not started yet: it never will.+ pending->abandoned = true;+ return busy;+ }+ // Once started it runs to the end (the files may be moving): wait for its answer.+ published_.wait(lock, settled);+ if (!pending->result) return busy;+ return *pending->result;+}+void RuntimeHost::ReportWork(std::uint64_t done, std::uint64_t total) {if (total == 0) return;const int percent = static_cast<int>(std::min<std::uint64_t>(done, total) * 100 / total);@@ -1778,6 +1800,113 @@ void RuntimeHost::FinishGamesMove(Composition& c) {if (done) c.games_move.reset();}+RuntimeRelocationResult RuntimeHost::RunRelocate(Composition& c, const RuntimeRelocation& request) {+ using Outcome = RuntimeRelocationResult::Outcome;+ const auto answer = [](Outcome outcome, std::string message = {}) {+ RuntimeRelocationResult result;+ result.outcome = outcome;+ result.message = std::move(message);+ return result;+ };+ const auto app = std::to_string(request.runtime_number);+ std::optional<rt::RuntimeInstall> found;+ if (request.runtime_number != 0)+ for (const auto& record : c.repository.Snapshot())+ if (record.runtime_number == request.runtime_number) found = record;+ if (!found) {+ // No PS5 title: nothing names the folder but the caller's own records. Preparations run on+ // this worker too, so none starts until the files are in place.+ if (!request.move_files) return answer(Outcome::kDone);+ const auto moved = request.move_files();+ Log("relocate app=" + app + " to=" + request.to + " no title " + (moved.empty() ? "moved" : "failed: " + moved));+ return moved.empty() ? answer(Outcome::kDone) : answer(Outcome::kFailed, moved);+ }+ auto record = *found;+ if (record.install_path != request.to) {+ if (record.state == rt::PreparationState::kRemoving)+ return answer(Outcome::kBusy, "This game's PS5 title is being removed. Uninstall or Play finishes that first.");+ // As the 0.6 games move: the lease first, then the title's activity, both held through the+ // move and the record write.+ const auto control_path = HostPath(record.data_root) / ".playden-control";+ auto control = platform::ManagedDirectory::Open(control_path, platform::DirectoryPolicy::kPrivate);+ auto lease = control ? platform::ManagedFileLock::Acquire(*control, "publication.lock", platform::LockMode::kPublisher)+ : std::nullopt;+ if (!lease) {+ Log("relocate app=" + app + " waits: the publication lock for " + record.title_id + " is unavailable or held");+ return answer(Outcome::kBusy, "The game's PS5 title is in use. Close it, then try again.");+ }+ std::string refusal;+ if (record.state == rt::PreparationState::kReady) {+ refusal = MaintenanceRefusal(c, record);+ } else if (record.state == rt::PreparationState::kPreparing || record.state == rt::PreparationState::kRegistering) {+ // Staged titles name the old folder and nothing would republish them.+ refusal = "Finish setting up the game first.";+ } else if (record.journal) {+ refusal = "Finish the PS5 title's recovery first.";+ } else if (!record.title_id.empty()) {+ const auto activity = c.coordinator->Activity(record.title_id);+ if (activity == rt::NativeActivity::kUnknown) refusal = kActivityUnknown;+ else if (activity != rt::NativeActivity::kStopped) refusal = "Close the game first.";+ }+ if (!refusal.empty()) {+ Log("relocate app=" + app + " waits: " + refusal);+ return answer(Outcome::kBusy, refusal);+ }+ if (request.move_files) {+ if (const auto moved = request.move_files(); !moved.empty()) {+ Log("relocate app=" + app + " move failed: " + moved);+ return answer(Outcome::kFailed, moved);+ }+ }+ // One write names the new folder. A published title keeps the old one in moved_from until a+ // publication names the new folder; one never published has nothing to verify.+ const auto from = record.install_path;+ if (record.state == rt::PreparationState::kReady) record.moved_from = from;+ record.install_path = request.to;+ if (!request.volume_id.empty()) record.volume_id = request.volume_id;+ const auto written = c.repository.UpdateAll({record});+ Log("relocate app=" + app + " title=" + record.title_id + " from=" + from + " to=" + request.to + " " ++ (written.ok() ? std::string("record rewritten") : "failed: " + written.message()));+ PublishInstalls(c, record.install_id);+ if (!written.ok()) return answer(Outcome::kFailed, "The game's record could not be saved. Try again.");+ // The publication takes the lease itself.+ }+ const auto current = c.records.Find(record.install_id);+ if (!current || current->moved_from.empty() || current->state != rt::PreparationState::kReady)+ return answer(Outcome::kDone);+ // The marked republish, as after the 0.6 games move: same revision, the new folder in the+ // descriptor, profile and dxvk.conf; the old publication is checked against the new folder.+ if (const auto busy = MaintenanceRefusal(c, *current); !busy.empty()) {+ // Committed: Play and the next start publish it.+ Log("relocate app=" + app + " republish waits: " + busy);+ return answer(Outcome::kDone, busy);+ }+ bool published = false;+ const auto refused = RepublishSettings(c, *current, &published);+ if (refused.empty()) {+ Log("relocate app=" + app + " republished at " + request.to);+ return answer(Outcome::kDone);+ }+ if (!c.kit) {+ Log("relocate app=" + app + " republish waits: " + refused);+ return answer(Outcome::kDone, refused);+ }+ // The moved publication can't be verified: as for a game installed again on another drive, the+ // old title goes, keeping its saves, and the game is prepared again where it is now.+ Log("relocate app=" + app + " republish failed, removing the old title keeping saves: " + refused);+ auto result = answer(Outcome::kDone);+ result.prepare_again = true;+ Publish([&](RuntimeSnapshot& s) { s.removing_app = request.runtime_number; });+ const auto removed = RunRemove(c, request.runtime_number, false, true);+ PublishKeptBottles(c);+ Publish([&](RuntimeSnapshot& s) { s.removing_app.reset(); });+ if (!removed.empty()) {+ Log("relocate app=" + app + " old title removal stopped: " + removed);+ result.message = "The game's old PS5 title could not be removed yet. Play tries again.";+ }+ return result;+}+// Fresh facts for one launch, from the package layer, the bottle's start check// and the console's inventory and activity. Runs on the worker; the application// thread reuses the worker's latest result while it is fresh.@@ -3498,6 +3627,7 @@ void RuntimeHost::Run() {bool names_changed = false;std::vector<PreparedGame> dismissals;bool clear_prepared = false;+ std::vector<std::shared_ptr<PendingRelocation>> relocations;{std::unique_lock<std::mutex> lock(mutex_);// Preparation changes install records that Handoff reads on the@@ -3509,7 +3639,8 @@ void RuntimeHost::Run() {(!remove_requests_.empty() && !snapshot_.handoff_ready) ||(!kept_delete_requests_.empty() && !snapshot_.handoff_ready) ||!scan_requests_.empty() || !prepared_dismissals_.empty() || prepared_clear_requested_ ||- (!maintenance_requests_.empty() && !snapshot_.handoff_ready);+ (!maintenance_requests_.empty() && !snapshot_.handoff_ready) ||+ (!relocation_requests_.empty() && !snapshot_.handoff_ready);};if (launch_poll_) wake_.wait_for(lock, kLaunchPollInterval, ready);else wake_.wait(lock, [&] { return ready() || launch_poll_; });@@ -3521,6 +3652,8 @@ void RuntimeHost::Run() {scans.swap(scan_requests_);// Maintenance changes the bottle and install record, like removal.if (!snapshot_.handoff_ready) maintenance.swap(maintenance_requests_);+ // A relocation rewrites the install record, like maintenance.+ if (!snapshot_.handoff_ready) relocations.swap(relocation_requests_);poll = launch_poll_;cleanup = cleanup_requested_;cleanup_requested_ = false;@@ -3597,9 +3730,11 @@ void RuntimeHost::Run() {remove_requests_.insert(remove_requests_.begin(), removals.begin(), removals.end());kept_delete_requests_.insert(kept_delete_requests_.begin(), kept_deletes.begin(), kept_deletes.end());maintenance_requests_.insert(maintenance_requests_.begin(), maintenance.begin(), maintenance.end());+ relocation_requests_.insert(relocation_requests_.begin(), relocations.begin(), relocations.end());removals.clear();kept_deletes.clear();maintenance.clear();+ relocations.clear();}}for (const auto& [app, delete_saves] : removals) {@@ -3644,6 +3779,31 @@ void RuntimeHost::Run() {++s.maintenance_handled;});}+ for (const auto& pending : relocations) {+ {+ std::lock_guard<std::mutex> lock(mutex_);+ if (pending->abandoned) continue;+ pending->started = true;+ }+ published_.notify_all();+ RuntimeRelocationResult result;+ if (!composition) {+ result.outcome = RuntimeRelocationResult::Outcome::kBusy;+ const auto current = Snapshot();+ result.message = current.reason.empty() ? "The runtime services are unavailable." : current.reason;+ } else {+ result = RunRelocate(*composition, pending->request);+ }+ ClearWork();+ Log("relocate result app=" + std::to_string(pending->request.runtime_number) + " outcome=" ++ std::to_string(static_cast<int>(result.outcome)) + (result.prepare_again ? " prepare-again" : "") ++ (result.message.empty() ? std::string() : " " + result.message));+ {+ std::lock_guard<std::mutex> lock(mutex_);+ pending->result = std::move(result);+ }+ published_.notify_all();+ }if (poll && composition) {const bool again = PollLaunch(*composition);std::lock_guard<std::mutex> lock(mutex_);diff --git a/src/homebrew/runtime_host.hpp b/src/homebrew/runtime_host.hppindex ce0b91a..ed292ff 100644--- a/src/homebrew/runtime_host.hpp+++ b/src/homebrew/runtime_host.hpp@@ -333,6 +333,31 @@ struct RuntimePrepareRequest {GameId game;};+// An imported game's folder becoming its folder in Playden ("Copy into Playden" once the copy is+// verified, "Move into Playden" for a drop-folder game): RuntimeHost::Relocate.+struct RuntimeRelocation {+ std::uint32_t runtime_number = 0; // 0: the game has no number yet, so no PS5 title+ std::string to; // console path of the game's folder in Playden+ std::string volume_id; // the install volume of `to`+ // The move itself, run on the worker under the game's publication lease once its title is+ // stopped (and at once for a game without a title): empty, or why not. It must be safe to run+ // again after it succeeded (a restart in between). Empty: the files are at `to` already (a copy).+ std::function<std::string()> move_files;+};++struct RuntimeRelocationResult {+ enum class Outcome {+ kDone, // the game plays from `to` (its record names it; a marked republish may still wait)+ kBusy, // nothing changed: its title runs or is unknown, or the runtime is busy; try again+ kFailed, // nothing changed, or the files moved but the record could not follow (try again)+ };+ Outcome outcome = Outcome::kFailed;+ std::string message; // for the player+ // The moved publication could not be verified: the old PS5 title was removed keeping its saves,+ // and the next Play prepares the game again where it is now.+ bool prepare_again = false;+};+// The worker constructs and owns every runtime collaborator, so durable work and// native inventory never run on the main thread. The main thread reads snapshots// and queues requests. Play is split the same way: the worker checks the game@@ -401,6 +426,15 @@ class RuntimeHost {// Nothing when the game is invalid, the runtime records are not loaded (yet) or the write fails;// the runtime log says which. RuntimeSnapshot::runtime_games lists the number before it returns.std::optional<std::uint32_t> RuntimeNumberFor(const GameId& game);+ // Makes `request.to` the game's folder, keeping its PS5 title, runtime number and bottle (the+ // 0.6 games move's republish, for one game): with the game's publication lease held and its title+ // stopped, move_files runs, then one record write names the new folder (moved_from the old one)+ // and the game is published again at its revision. A publication that can't be verified falls+ // back to removing the old title, keeping its saves (prepare_again). A record already naming `to`+ // (a restart after its write) only finishes the republish. Blocks the calling thread (never the+ // worker's) until the worker answers; a request the worker hasn't started within `timeout` is+ // dropped and answers kBusy.+ RuntimeRelocationResult Relocate(RuntimeRelocation request, std::chrono::milliseconds timeout);// Application thread only: starts the title of a prepared launch session.// Returns false when no session is waiting.bool Handoff();@@ -450,6 +484,14 @@ class RuntimeHost {bool MoveGames(Composition& composition, std::string* error);bool MoveGameRecords(Composition& composition);void FinishGamesMove(Composition& composition);+ // Relocate on the worker.+ RuntimeRelocationResult RunRelocate(Composition& composition, const RuntimeRelocation& request);+ struct PendingRelocation {+ RuntimeRelocation request;+ bool started = false;+ bool abandoned = false; // the caller stopped waiting before it started+ std::optional<RuntimeRelocationResult> result;+ };// Empty when the game's Ready title is stopped with no launch pending, otherwise why not.std::string MaintenanceRefusal(Composition& composition, const runtime::RuntimeInstall& record);// Runs one maintenance action; empty message on refusal means none; *ok tells success.@@ -557,6 +599,7 @@ class RuntimeHost {std::vector<std::pair<std::uint32_t, std::string>> scan_requests_;std::vector<std::pair<std::uint32_t, RuntimeMaintenance>> maintenance_requests_;std::vector<std::uint32_t> activity_requests_;+ std::vector<std::shared_ptr<PendingRelocation>> relocation_requests_; // guarded by mutex_int work_percent_ = -1; // worker thread only: the published work percent, -1 for nonebool launch_poll_ = false;bool cleanup_requested_ = false;diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cppindex d4bfd78..9a264a5 100644--- a/src/homebrew/simulation.cpp+++ b/src/homebrew/simulation.cpp@@ -764,6 +764,37 @@ void Simulation::UpdateKeptBottles(std::vector<LiveKeptBottle> kept) {rebuild_view();}+void Simulation::UpdateExternalGames(std::vector<LiveExternalGame> games) {+ if (!live() || games == state_.external_games) return;+ state_.external_games = std::move(games);+ rebuild_view();+}++void Simulation::UpdateImportCandidates(std::vector<LiveImportCandidate> candidates, bool scanning) {+ if (!live() || (candidates == state_.import_candidates && scanning == state_.import_scanning)) return;+ state_.import_candidates = std::move(candidates);+ state_.import_scanning = scanning;+ rebuild_view();+}++void Simulation::RequestImport(ImportRequest request) {+ if (live()) pending_import_requests_.push_back(std::move(request));+}++std::vector<ImportRequest> Simulation::TakeImportRequests() { return std::exchange(pending_import_requests_, {}); }++bool Simulation::SeedGameProgram(GameId id, const std::string& program) {+ if (!live() || !id.valid() || settings_load_failed_ || game_settings_file_.empty()) return false;+ if (!rt::ValidateSettingValue(rt::GameRecipe{}, "program", program, nullptr)) return false;+ auto& record = state_.game_settings[id];+ if (record.overrides.count("program")) return false;+ record.overrides["program"] = program;+ ++record.revision;+ save_profile();+ rebuild_view();+ return saved_game_settings_ == state_.game_settings;+}+std::vector<std::string> Simulation::collection_sheet_actions(const std::string& sheet) const {std::vector<std::string> actions;if (sheet == "collections") {@@ -1635,7 +1666,9 @@ void Simulation::clear_account_specific_state() {state_.session = SessionState::Idle;state_.session_game_id = {};state_.session_error.clear();- state_.runtime = RuntimeState::Unavailable;+ // The live runtime belongs to the console: the controller projects it only when it changes, so a+ // Steam sign-in or sign-out must not reset it (games of other stores still play).+ if (!live()) state_.runtime = RuntimeState::Unavailable;state_.library_loading = !loading_sources_.empty();state_.library_syncing = state_.library_loading;state_.notices.erase(std::remove_if(state_.notices.begin(), state_.notices.end(),@@ -3724,16 +3757,18 @@ void Simulation::rebuild_view() {: state_.account().authentication == AuthState::Offline? (state_.account().error.empty() ? "Offline — showing available library" : state_.account().error): "Signed out";+ bool steam_shown = false;for (const auto* game : visible_games()) {if (view_.rows.size() >= 12) break;auto& item = row(game_key(game->id), RowKind::Game, game->title, "open-game");item.game_id = game->id;item.badge = state_.library.favorites.contains(game->id) ? "Favorite" : "";+ steam_shown = steam_shown || game->id.store == Store::kSteam;}- if (view_.rows.empty()) {- row("home-library", RowKind::Action, "Open Library", "library");- if (state_.account().authentication != AuthState::SignedIn) row("home-connect", RowKind::Action, "Connect a store", "connect");- }+ // Signed out with only other stores' games (imported ones) listed, Steam is still offered.+ const bool connect = state_.account().authentication != AuthState::SignedIn && !steam_shown;+ if (view_.rows.empty()) row("home-library", RowKind::Action, "Open Library", "library");+ if (connect) row("home-connect", RowKind::Action, "Connect a store", "connect");auto add_shelf = [this](const playden::homebrew::Shelf& source, int shelf_index) {ViewData::Shelf shelf;shelf.key = source.key; shelf.title = source.title;@@ -3793,6 +3828,19 @@ void Simulation::rebuild_view() {view_.title = game ? game->title : "Game unavailable";view_.subtitle = game ? game->description : "The selected fixture no longer exists.";Installation* installation = game ? find_installation(state_, game->id) : nullptr;+ // A game playing in place from its own folder (an imported game) has the same Play and setup+ // rows as an installed one; it is never in Installed games.+ std::optional<Installation> in_place;+ std::string in_place_reason;+ if (!installation && game && live())+ for (const auto& external : state_.external_games)+ if (external.game_id == game->id) {+ const auto runtime_game = state_.runtime_games.find(game->id);+ in_place = Installation{game->id, external.volume_id, 0, external.available && external.reason.empty(),+ runtime_game != state_.runtime_games.end() && runtime_game->second.ready, false};+ in_place_reason = external.reason;+ installation = &*in_place;+ }// Live installations are only finished installs; their size may be unknown on an absent drive.const bool installed = installation && (live() || installation->installed_bytes > 0);if (!live()) {@@ -3805,7 +3853,11 @@ void Simulation::rebuild_view() {// Real install state: an install job, a finished install, or what Steam allows.const auto* job = find_job(state_, ToKey(game->id));if (installed) {- if (!installation->available) {+ if (!installation->available && !in_place_reason.empty()) {+ auto& status = row("details-installed", RowKind::Status, "Can't play from this folder", "");+ status.subtitle = in_place_reason;+ status.enabled = false;+ } else if (!installation->available) {const auto* volume = find_volume(state_, installation->volume_id);auto& status = row("details-installed", RowKind::Status, "Storage disconnected", "");status.subtitle = "Connect " + (volume ? volume->label : std::string("its storage")) + " to use this game";diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hppindex 8d93cc0..09ca205 100644--- a/src/homebrew/simulation.hpp+++ b/src/homebrew/simulation.hpp@@ -192,6 +192,46 @@ struct LiveKeptBottle {std::uint64_t bytes = 0;bool operator==(const LiveKeptBottle&) const = default;};+// A game that plays in place from a folder Playden doesn't manage (an imported game on a drive):+// never in Installed games, its files never deleted.+struct LiveExternalGame {+ GameId game_id;+ std::string volume_id;+ bool available = true; // its drive and folder are there and the PS5 can play from them+ std::string reason; // why a folder that is there can't be played from; empty otherwise+ bool operator==(const LiveExternalGame&) const = default;+};+// A folder in an import folder that could be added to the library (stores/local/library_source.h).+struct LiveImportProgram {+ std::string path; // inside the game folder, '/' separators+ std::uint64_t bytes = 0;+ bool helper = false; // an installer or tool, ranked last+ bool operator==(const LiveImportProgram&) const = default;+};+struct LiveImportCandidate {+ std::string volume_id;+ std::string folder; // relative to the volume's mount+ std::string path; // console path+ std::string title;+ std::vector<LiveImportProgram> programs; // ranked: the likely game first+ std::uint64_t total_bytes = 0; // estimate+ bool ascii_ok = false; // can play in place+ bool partial = false; // the scan stopped at a limit or depth+ GameId imported; // already in the library as this game; invalid when not+ bool operator==(const LiveImportCandidate&) const = default;+};+// Requests for the Local store: look for games, add a candidate (in place), remove an imported game.+// CopyIntoPlayden: copy an imported game playing in place into Playden's own storage (volume_id:+// an install volume, empty for the default one). MoveIntoPlayden: rename a drop-folder game into+// Playden's games folder on the console storage (instant; its PS5 title, if any, follows).+enum class ImportRequestKind { LookForGames, Add, Remove, CopyIntoPlayden, MoveIntoPlayden };+struct ImportRequest {+ ImportRequestKind kind = ImportRequestKind::LookForGames;+ GameId game_id; // Remove, CopyIntoPlayden, MoveIntoPlayden+ std::string volume_id; // Add, CopyIntoPlayden+ std::string folder; // Add: relative to the volume's mount+ std::string program; // Add: relative to the folder+};// Live Steam Cloud save state for one game, projected from the cloud service.// Waiting: transfers that need no choice wait for the player (an uninstalled game's saves, or// saves deleted here that Cloud still has).@@ -507,6 +547,9 @@ struct State {LiveKitInfo kit; // live: the compatibility kit in useLiveSelfUpdate self_update; // live: Playden's own updatestd::vector<LiveKeptBottle> kept_bottles; // live: saves kept from uninstalled games+ std::vector<LiveExternalGame> external_games; // live: games playing in place (imported)+ std::vector<LiveImportCandidate> import_candidates; // live: folders on the drives that could be imported+ bool import_scanning = false; // live: Playden is looking for games to importLiveSavesJob saves_job; // live: the Saves tab's running check or Sync allstd::map<GameId, std::uint64_t> save_sizes; // live: Windows user folders of games without Steam CloudNavigation navigation;@@ -653,6 +696,15 @@ public:[[nodiscard]] bool TakeRuntimeCleanupRequest();// Live mode: kept game data, deletions the player confirmed, and why an uninstall stopped.void UpdateKeptBottles(std::vector<LiveKeptBottle> kept);+ // Live mode: games playing in place, and what the drives offer to import.+ void UpdateExternalGames(std::vector<LiveExternalGame> games);+ void UpdateImportCandidates(std::vector<LiveImportCandidate> candidates, bool scanning);+ // Live mode: requests for the Local store (the Import sheet, the dev request file).+ void RequestImport(ImportRequest request);+ [[nodiscard]] std::vector<ImportRequest> TakeImportRequests();+ // Writes the program an imported game starts with into its Game settings, unless the player+ // chose one already; saved at once so the next preparation reads it. True when it was written.+ bool SeedGameProgram(GameId id, const std::string& program);[[nodiscard]] std::vector<GameId> TakeKeptDeleteRequests();// Live mode: maintenance the player confirmed (and settings to apply when the sheet// closes), and games whose title activity the sheet needs to know.@@ -815,6 +867,7 @@ private:std::uint64_t editor_serial_ = 0;bool system_keyboard_available_ = false;std::vector<InstallRequest> pending_install_requests_;+ std::vector<ImportRequest> pending_import_requests_;std::vector<GameId> pending_runtime_prepare_requests_;std::vector<GameId> pending_prepare_next_requests_;std::vector<LiveCompletedPreparation> pending_prepared_dismissals_;diff --git a/src/installs/install_service.cpp b/src/installs/install_service.cppindex 6ce37b7..f4f3d88 100644--- a/src/installs/install_service.cpp+++ b/src/installs/install_service.cpp@@ -24,10 +24,20 @@ std::vector<InstallRequest> ParseInstallRequests(const std::string& text) {const auto newline = remaining.find('\n');auto line = remaining.substr(0, newline);remaining = newline == std::string_view::npos ? std::string_view{} : remaining.substr(newline + 1);- const auto token = [&line]() {+ bool malformed = false;+ // The next word; a quoted one runs to its closing quote and must be followed by a space or the end.+ const auto token = [&line, &malformed]() {const auto first = line.find_first_not_of(" \t\r\v\f");if (first == std::string_view::npos) { line = {}; return std::string_view{}; }line.remove_prefix(first);+ if (line.front() == '"') {+ const auto close = line.find('"', 1);+ if (close == std::string_view::npos || close == 1) { malformed = true; line = {}; return std::string_view{}; }+ const auto word = line.substr(1, close - 1);+ line.remove_prefix(close + 1);+ if (!line.empty() && line.find_first_of(" \t\r\v\f") != 0) malformed = true;+ return word;+ }const auto end = line.find_first_of(" \t\r\v\f");const auto word = line.substr(0, end);line = end == std::string_view::npos ? std::string_view{} : line.substr(end);@@ -36,14 +46,53 @@ std::vector<InstallRequest> ParseInstallRequests(const std::string& text) {auto id = token();if (id.empty()) continue;InstallRequest request;+ if (id == "import") {+ request.action = InstallRequest::Action::kImport;+ request.volume = std::string(token());+ request.folder = std::string(token());+ request.program = std::string(token());+ if (malformed || request.volume.empty() || request.folder.empty() || request.program.empty() || !token().empty() ||+ malformed)+ continue;+ requests.push_back(std::move(request));+ continue;+ }+ if (id == "copy") {+ // copy local-<uuid> [volume]+ request.action = InstallRequest::Action::kCopy;+ const auto game = ParseKey(token());+ request.volume = std::string(token());+ if (malformed || !game || game->store != Store::kLocal || !token().empty() || malformed) continue;+ request.game = *game;+ requests.push_back(std::move(request));+ continue;+ }+ if (id == "move") {+ // move local-<uuid>: a drop-folder game into Playden's games folder on the console storage+ request.action = InstallRequest::Action::kMove;+ const auto game = ParseKey(token());+ if (malformed || !game || game->store != Store::kLocal || !token().empty() || malformed) continue;+ request.game = *game;+ requests.push_back(std::move(request));+ continue;+ }if (id == "uninstall" || id == "cancel") {request.action = id == "uninstall" ? InstallRequest::Action::kUninstall : InstallRequest::Action::kCancel;id = token();}- if (id.empty() || !token().empty()) continue;+ if (malformed || id.empty() || !token().empty() || malformed) continue;+ if (id.rfind("local-", 0) == 0) {+ // Imported games have no download to cancel.+ const auto game = ParseKey(id);+ if (!game || game->store != Store::kLocal || request.action == InstallRequest::Action::kCancel) continue;+ request.game = *game;+ requests.push_back(std::move(request));+ continue;+ }const auto parsed = std::from_chars(id.data(), id.data() + id.size(), request.app);if (parsed.ec != std::errc{} || parsed.ptr != id.data() + id.size() || request.app == 0) continue;- requests.push_back(request);+ request.game = GameId::Steam(request.app);+ requests.push_back(std::move(request));}return requests;}@@ -542,6 +591,21 @@ void InstallService::Uninstall(GameId id) {wake_.notify_all();}+bool InstallService::Adopt(GameId id, const std::string& volume_id, std::uint64_t install_bytes) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (std::any_of(installed_.begin(), installed_.end(), [id](const InstalledGame& game) { return game.id == id; }))+ return true;+ const auto* volume = FindVolume(volume_id);+ if (stopping_ || !id.valid() || !RunnerFor(id.store) || Find(id) || !volume || !volume->available) return false;+ const auto root = RootOn(volume_id, id);+ if (!Exists(root + "/" + kRecord)) return false;+ installed_.push_back({id, install_bytes, root, volume_id, true, RecordTime(root + "/" + kRecord)});+ std::sort(installed_.begin(), installed_.end(),+ [](const InstalledGame& left, const InstalledGame& right) { return left.id < right.id; });+ Publish();+ return true;+}+void InstallService::SetDefaultVolume(const std::string& volume_id) {std::lock_guard<std::mutex> lock(mutex_);const auto* volume = FindVolume(volume_id);diff --git a/src/installs/install_service.h b/src/installs/install_service.hindex 94a7323..5306851 100644--- a/src/installs/install_service.h+++ b/src/installs/install_service.h@@ -21,11 +21,29 @@namespace playden::installs {struct InstallRequest {- enum class Action { kInstall, kUninstall, kCancel };+ enum class Action { kInstall, kUninstall, kCancel, kImport, kCopy, kMove };Action action = Action::kInstall;- std::uint32_t app = 0;+ std::uint32_t app = 0; // a Steam game's app ID; 0 for other games+ GameId game; // the game (kImport: none yet)+ // kImport: the drive (install volume id), the game's folder and the program, as LocalLibrarySource::Add takes them.+ // kCopy: the install volume to copy to (empty: the default).+ std::string volume;+ std::string folder;+ std::string program;};-// Development request file: strict app IDs, preserving command order, at most 64 commands.+// Development request file (dev/install-request.txt), one command per line, in order, at most 64:+// <game> Steam: install; Local: prepare (set up) the imported game+// uninstall <game> Steam: uninstall; Local: remove from the library (files stay)+// cancel <app id> Steam only: cancel the download+// copy <local game> [volume] copy an imported game playing in place into Playden (volume:+// an install volume id; default: the default volume)+// move <local game> move an imported game from the drop folder into Playden+// import <volume> <folder> <program> add a game playing in place (volume: internal, m2, extended,+// usb0, usb1; folder: a name in that drive's import folder, or a+// path from the drive's root; program: inside the folder)+// <game> is a Steam app ID (strict decimal) or "local-<uuid>". Words are separated by spaces or tabs;+// a word with spaces goes in double quotes ("Old School Rally"), which can't hold a quote themselves.+// A line that doesn't parse is skipped.std::vector<InstallRequest> ParseInstallRequests(const std::string& text);enum class InstallJobState { kQueued, kRunning, kPaused, kFailed };@@ -146,6 +164,10 @@ class InstallService {void MoveQueued(GameId id, bool up);void Cancel(GameId id);void Uninstall(GameId id);+ // Lists a complete managed install that was made outside the queue (an imported game moved+ // into Playden): its folder on `volume_id` holds the install record. True when it is listed now+ // (or was already); false when the game has a job, its store no runner, or the record is missing.+ bool Adopt(GameId id, const std::string& volume_id, std::uint64_t install_bytes);// Chooses the volume for installs queued from now on; ignored unless it names an available volume.void SetDefaultVolume(const std::string& volume_id);// Takes a finished download, or every one, off the Completed list in Downloads.diff --git a/src/installs/installed_location.h b/src/installs/installed_location.hindex 1bb89d6..b3d1004 100644--- a/src/installs/installed_location.h+++ b/src/installs/installed_location.h@@ -20,13 +20,15 @@ struct InstalledLocation {std::string volume_id;bool available = true; // the folder is there nowOwnership ownership = Ownership::kManaged;+ // Why an available drive's folder still can't be played from (a name the PS5 can't open); empty otherwise.+ std::string reason;bool managed() const { return ownership == Ownership::kManaged; }};inline bool operator==(const InstalledLocation& left, const InstalledLocation& right) {return left.game == right.game && left.root == right.root && left.volume_id == right.volume_id &&- left.available == right.available && left.ownership == right.ownership;+ left.available == right.available && left.ownership == right.ownership && left.reason == right.reason;}inline bool operator!=(const InstalledLocation& left, const InstalledLocation& right) { return !(left == right); }@@ -36,7 +38,7 @@ inline std::vector<InstalledLocation> ProjectInstalledLocations(const std::vectoconst std::vector<InstalledLocation>& external) {std::vector<InstalledLocation> locations;for (const auto& game : managed)- locations.push_back({game.id, game.root, game.volume_id, game.available, InstalledLocation::Ownership::kManaged});+ locations.push_back({game.id, game.root, game.volume_id, game.available, InstalledLocation::Ownership::kManaged, {}});for (const auto& location : external) {if (!location.game.valid() || location.root.empty()) continue;if (std::any_of(locations.begin(), locations.end(), [&](const InstalledLocation& known) { return known.game == location.game; }))diff --git a/src/platform/ps5/native_startup.cpp b/src/platform/ps5/native_startup.cppindex 9061c3a..442ef23 100644--- a/src/platform/ps5/native_startup.cpp+++ b/src/platform/ps5/native_startup.cpp@@ -188,6 +188,11 @@ NativeAccessResult PrepareNativeStartup(const std::string& title_id) {result.runtime_ready = false;result.error = moved.notice;}+ // The drop folder for games copied over FTP (/data/playden-import, 0777). Still single-threaded;+ // a failure is only logged: games can still be imported from a drive.+ if (result.runtime_ready)+ runtime::PrepareDropFolder(data_root.parent_path() / "playden-import", WithRoot,+ [&layout_lines](const std::string& line) { layout_lines.push_back(line); });}const std::string logs = result.launcher_ready ? EffectiveLogRoot(result.paths) : "/app0/logs";for (const auto& line : layout_lines) app::AppendLogLine(logs, "native-startup.log", line);diff --git a/src/runtime/writable_folders.cpp b/src/runtime/writable_folders.cppindex 025dd75..3eab405 100644--- a/src/runtime/writable_folders.cpp+++ b/src/runtime/writable_folders.cpp@@ -36,9 +36,10 @@ int Probe(const std::filesystem::path& folder) {mode_t RepairMode(const std::filesystem::path& folder, const std::filesystem::path& namespace_root) {const auto relative = folder.lexically_relative(namespace_root).generic_string();if (relative == "data/playden" || relative == "data/homebrew") return 01777;- if (relative == "data/playden-games") return 0777;+ if (relative == "data/playden-games" || relative == "data/playden-import") return 0777;for (const auto* volume : {"ext0", "ext1", "usb0", "usb1"})- if (relative == std::string("mnt/") + volume + "/playden/games") return 0777;+ for (const auto* folder : {"/playden/games", "/playden/import"})+ if (relative == std::string("mnt/") + volume + folder) return 0777;return 0;}@@ -138,4 +139,47 @@ std::string PrepareRuntimeRoot(const std::filesystem::path& root, const Writable"). Make the folder writable over FTP (permissions 777), then try again.";}+std::string PrepareDropFolder(const std::filesystem::path& folder, const WritableFoldersAsRoot& as_root,+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root) {+ if (folder != namespace_root / "data/playden-import") {+ if (log) log("drop folder refused path=" + folder.string());+ return "Playden can't make this import folder.";+ }+ std::string failed = "root access";+ int failed_error = 0;+ bool made = false;+ const bool prepared = as_root && as_root([&] {+ const auto fail = [&](const char* step) { failed = step; failed_error = errno; return false; };+ const int parent = platform::OpenDirectoryWalk(folder.parent_path());+ if (parent < 0) return fail("open /data");+ constexpr int kFlags = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;+ int fd = platform::OpenWalkChild(parent, "playden-import", kFlags);+ if (fd < 0 && errno == ENOENT) {+ made = platform::MakeWalkChild(parent, "playden-import", 0777) == 0;+ if (made || errno == EEXIST) fd = platform::OpenWalkChild(parent, "playden-import", kFlags);+ }+ // A link or a file in its place is left alone (O_NOFOLLOW, O_DIRECTORY).+ bool ok = fd >= 0 || fail("open the folder");+ // 0777 without the sticky bit, like /data/playden-games (whose /data drops it anyway): FTP+ // writes games here as root, Playden opens them in place, and "Move into Playden" renames+ // folders out of here whoever made them. mkdir is umasked, and an FTP-made folder is 0755.+ struct stat info{};+ if (ok && ::fstat(fd, &info) != 0) ok = fail("stat");+ if (ok && (info.st_mode & 07777) != 0777 && ::fchmod(fd, 0777) != 0) ok = fail("chmod");+ if (ok && made && ::fsync(parent) != 0 && log) log("drop folder path=" + folder.string() + " parent fsync=" + Error(errno));+ if (fd >= 0) ::close(fd);+ ::close(parent);+ return ok;+ });+ if (!prepared && failed == "root access") failed_error = as_root ? errno : ENOSYS;+ if (prepared) {+ if (made && log) log("drop folder made path=" + folder.string());+ return {};+ }+ const std::string detail = failed + " failed" + (failed_error ? " error=" + Error(failed_error) : "");+ if (log) log("drop folder failed path=" + folder.string() + " " + detail);+ return "Playden can't make " + folder.string() + " (" + detail + "). Games can still be imported from a drive.";+}+} // namespace playden::runtimediff --git a/src/runtime/writable_folders.h b/src/runtime/writable_folders.hindex 307b1c3..2a3f32a 100644--- a/src/runtime/writable_folders.h+++ b/src/runtime/writable_folders.h@@ -28,4 +28,12 @@ std::string PrepareRuntimeRoot(const std::filesystem::path& root, const Writableconst std::function<void(const std::string&)>& log,const std::filesystem::path& namespace_root = "/");+// The drop folder, /data/playden-import (0777): FTP copies games into it and Playden plays them in+// place (stores/local/drop_folder_access.h opens each game's files). Made, or opened to 0777, as+// root after the grant; a link or a file at that path is refused. Failing doesn't stop Playden:+// the message is for the log, and games can still come from a drive.+std::string PrepareDropFolder(const std::filesystem::path& folder, const WritableFoldersAsRoot& as_root,+ const std::function<void(const std::string&)>& log,+ const std::filesystem::path& namespace_root = "/");+} // namespace playden::runtimediff --git a/src/steam/backend/depot_installer.cpp b/src/steam/backend/depot_installer.cppindex b3d3b35..c30f497 100644--- a/src/steam/backend/depot_installer.cpp+++ b/src/steam/backend/depot_installer.cpp@@ -25,6 +25,8 @@namespace playden::steam {namespace {+// An imported game's copy journal (stores/local/copy_plan.h kCopyJournalDir).+constexpr const char* kCopyJournalDir = ".playden-copy";using Clock = std::chrono::steady_clock;struct ScopeExit {@@ -839,8 +841,10 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTabool RemoveManagedInstall(const std::string& install_root) {if (install_root.size() < 2 || install_root[0] != '/' || install_root.find("/..") != std::string::npos) return false;if (!Exists(install_root)) return true;+ // The copy of an imported game keeps its plan in .playden-copy (stores/local/copy_plan.h).+ const std::string copy_dir = install_root + "/" + kCopyJournalDir;if (!Exists(install_root + "/" + DepotInstaller::kInstallRecord) &&- !Exists(install_root + "/" + DepotInstaller::kJournalDir))+ !Exists(install_root + "/" + DepotInstaller::kJournalDir) && !Exists(copy_dir))return false; // not a Playden-managed folder// Remove exactly the paths the install recorded, deepest first, then Playden's own files.std::vector<std::string> files;@@ -871,6 +875,9 @@ bool RemoveManagedInstall(const std::string& install_root) {const std::string journal_dir = install_root + "/" + DepotInstaller::kJournalDir;std::remove((journal_dir + "/journal").c_str());rmdir(journal_dir.c_str());+ std::remove((copy_dir + "/plan.json").c_str());+ std::remove((copy_dir + "/plan.json.tmp").c_str());+ rmdir(copy_dir.c_str());std::remove((install_root + "/" + DepotInstaller::kFileList).c_str());std::remove((install_root + "/" + DepotInstaller::kInstallRecord).c_str());if (rmdir(install_root.c_str()) == 0) return true;diff --git a/src/steam/backend/depot_installer.h b/src/steam/backend/depot_installer.hindex 3e54bc5..6e10009 100644--- a/src/steam/backend/depot_installer.h+++ b/src/steam/backend/depot_installer.h@@ -132,7 +132,7 @@ class DepotInstaller {};// Removes a managed install directory: only paths under install_root, and only when it holds-// Playden's install record or download journal.+// Playden's install record, download journal or an imported game's copy journal (.playden-copy).bool RemoveManagedInstall(const std::string& install_root);} // namespace playden::steamdiff --git a/src/stores/local/copy_plan.cpp b/src/stores/local/copy_plan.cppnew file mode 100644index 0000000..d63a9ef--- /dev/null+++ b/src/stores/local/copy_plan.cpp@@ -0,0 +1,365 @@+#include "stores/local/copy_plan.h"++#include "platform/directory_walk.h"+#include "steam/backend/json.h"++#include <algorithm>+#include <cerrno>+#include <cstdio>+#include <cstring>+#include <dirent.h>+#include <fcntl.h>+#include <memory>+#include <sys/stat.h>+#include <unistd.h>++namespace playden::stores::local {+namespace {++constexpr int kOpenAny = O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC;+constexpr std::size_t kMaxLineBytes = 16 * 1024;+constexpr const char* kFileList = ".playden-files"; // DepotInstaller::kFileList++bool Unsupported(int error) { return error == ENOSYS || error == EINVAL; }+bool IsLinkError(int error) { return error == ELOOP || error == EMLINK; }++std::string Trimmed(std::string folder) {+ while (folder.size() > 1 && folder.back() == '/') folder.pop_back();+ return folder;+}++std::string Quoted(const std::string& path) { return "\"" + path + "\""; }++std::string WithThousands(std::uint64_t value) {+ auto digits = std::to_string(value);+ for (int at = static_cast<int>(digits.size()) - 3; at > 0; at -= 3) digits.insert(static_cast<std::size_t>(at), ",");+ return digits;+}++std::string Escape(const std::string& value) {+ std::string output;+ output.reserve(value.size() + 2);+ for (const unsigned char c : value) {+ if (c == '"') output += "\\\"";+ else if (c == '\\') output += "\\\\";+ else if (c < 0x20) {+ static constexpr char kHex[] = "0123456789abcdef";+ output += "\\u00";+ output.push_back(kHex[c >> 4]);+ output.push_back(kHex[c & 15]);+ } else {+ output.push_back(static_cast<char>(c));+ }+ }+ return output;+}++std::int64_t Seconds(std::int64_t value) { return value < 0 ? 0 : value; }++struct Frame {+ int fd = -1;+ DIR* listing = nullptr;+ std::string relative;+};++void Close(Frame& frame) {+ if (frame.listing) ::closedir(frame.listing);+ if (frame.fd >= 0) ::close(frame.fd);+ frame.listing = nullptr;+ frame.fd = -1;+}++// Takes `fd`.+bool OpenFrame(int fd, std::string relative, Frame* frame) {+ const int listing_fd = ::dup(fd);+ DIR* listing = listing_fd >= 0 ? ::fdopendir(listing_fd) : nullptr;+ if (!listing) {+ const int error = errno;+ if (listing_fd >= 0) ::close(listing_fd);+ ::close(fd);+ errno = error;+ return false;+ }+ *frame = Frame{fd, listing, std::move(relative)};+ return true;+}++CopyPlanBuild Failed(std::string failure, int error = 0) {+ CopyPlanBuild build;+ build.failure = std::move(failure);+ build.error = error;+ return build;+}++std::string LinkWords(const std::string& relative) {+ return "The game's folder has a link, " + Quoted(relative) ++ ". Playden can't copy links: replace it with the files it points to, then copy the game again.";+}++std::string UnreadableWords(const std::string& relative) {+ return "Playden can't read " + Quoted(relative) + " in the game's folder.";+}++bool WriteAll(FILE* file, const std::string& text) {+ return std::fwrite(text.data(), 1, text.size(), file) == text.size();+}++// Writes `text` to `path` through a synced temporary file.+bool WriteDurably(const std::string& path, const std::string& text) {+ const auto temporary = path + ".tmp";+ FILE* file = std::fopen(temporary.c_str(), "w");+ if (!file) return false;+ bool ok = WriteAll(file, text) && std::fflush(file) == 0 && ::fsync(::fileno(file)) == 0;+ ok = std::fclose(file) == 0 && ok;+ ok = ok && std::rename(temporary.c_str(), path.c_str()) == 0;+ if (!ok) std::remove(temporary.c_str());+ return ok;+}++bool MakeFolder(const std::string& path) {+ if (::mkdir(path.c_str(), 0777) == 0) return true;+ struct stat info {};+ return errno == EEXIST && ::stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);+}++// One value of the plan's line layout: the line without its end and trailing comma.+std::string Entry(std::string line) {+ while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) line.pop_back();+ if (!line.empty() && line.back() == ',') line.pop_back();+ return line;+}++std::optional<steam::json::Value> ParseLine(const std::string& text) {+ steam::json::Limits limits;+ limits.max_bytes = kMaxLineBytes;+ limits.max_depth = 2;+ limits.max_values = 16;+ limits.max_string_bytes = 8192;+ return steam::json::Parse(text, limits);+}++std::optional<std::uint64_t> NumberField(const steam::json::Value& object, const char* key) {+ const auto* value = object.Find(key);+ return value && value->is_number() ? value->AsUint64() : std::nullopt;+}++const std::string* TextField(const steam::json::Value& object, const char* key) {+ const auto* value = object.Find(key);+ return value && value->is_string() ? &value->text() : nullptr;+}++bool SafeRelative(const std::string& path) {+ if (path.empty() || path.size() > 4096 || path.front() == '/' || path.back() == '/') return false;+ if (path.find('\0') != std::string::npos) return false;+ std::size_t start = 0;+ for (;;) {+ const auto slash = path.find('/', start);+ const auto part = path.substr(start, slash == std::string::npos ? std::string::npos : slash - start);+ if (part.empty() || part == "." || part == "..") return false;+ if (slash == std::string::npos) return true;+ start = slash + 1;+ }+}++} // namespace++std::string CopyPlanPath(const std::string& root) { return root + "/" + kCopyJournalDir + "/" + kCopyPlanFile; }++bool PlanHasFile(const CopyPlan& plan, const std::string& path) {+ return std::any_of(plan.files.begin(), plan.files.end(), [&](const CopyFile& file) { return file.path == path; });+}++CopyPlanBuild BuildCopyPlan(const std::string& source_path, std::size_t max_files, std::uint64_t max_bytes) {+ const auto source = Trimmed(source_path);+ const int root = platform::OpenDirectoryWalk(source);+ if (root < 0) {+ const int error = errno;+ if (Unsupported(error)) return Failed("The game's folder can't be read on this console.", error);+ if (error == ENOENT) return Failed("The game's folder is not there. Check the drive.", error);+ if (error == ENOTDIR) return Failed("The game's folder is not a folder.", error);+ if (IsLinkError(error)) return Failed(LinkWords(source), error);+ return Failed("Playden can't read the game's folder.", error);+ }+ CopyPlanBuild build;+ build.plan.source = source;+ // One frame per level below the folder, on the heap: no depth limit and no recursion.+ std::vector<Frame> stack;+ Frame first;+ if (!OpenFrame(root, {}, &first)) return Failed("Playden can't read the game's folder.", errno);+ stack.push_back(std::move(first));+ const auto fail = [&](std::string failure, int error) {+ for (auto& frame : stack) Close(frame);+ return Failed(std::move(failure), error);+ };+ while (!stack.empty()) {+ Frame& frame = stack.back();+ errno = 0;+ const auto* entry = ::readdir(frame.listing);+ if (!entry) {+ if (errno != 0) {+ const int error = errno;+ return fail(UnreadableWords(frame.relative.empty() ? source : frame.relative), error);+ }+ Close(frame);+ stack.pop_back();+ continue;+ }+ const std::string name = entry->d_name;+ if (name == "." || name == "..") continue;+ const auto relative = frame.relative.empty() ? name : frame.relative + "/" + name;+ if (entry->d_type == DT_LNK) return fail(LinkWords(relative), ELOOP);+ const int child = platform::OpenWalkChild(frame.fd, name.c_str(), kOpenAny, static_cast<std::uint64_t>(entry->d_fileno));+ if (child < 0) {+ const int error = errno;+ if (Unsupported(error)) return fail("The game's folder can't be read on this console.", error);+ // ELOOP/EMLINK: a link whose listing said DT_UNKNOWN (exFAT does).+ if (IsLinkError(error)) return fail(LinkWords(relative), error);+ return fail(UnreadableWords(relative), error);+ }+ struct stat info {};+ if (::fstat(child, &info) != 0) {+ const int error = errno;+ ::close(child);+ return fail(UnreadableWords(relative), error);+ }+ if (S_ISDIR(info.st_mode)) {+ if (build.plan.dirs.size() >= max_files)+ return fail("The game has more than " + WithThousands(max_files) + " folders. Playden can't copy it.", 0);+ build.plan.dirs.push_back(relative);+ Frame next;+ if (!OpenFrame(child, relative, &next)) return fail(UnreadableWords(relative), errno);+ stack.push_back(std::move(next)); // `frame` is not used after this+ continue;+ }+ ::close(child);+ if (S_ISLNK(info.st_mode)) return fail(LinkWords(relative), ELOOP);+ if (!S_ISREG(info.st_mode)) continue;+ if (build.plan.files.size() >= max_files)+ return fail("The game has more than " + WithThousands(max_files) + " files. Playden can't copy it.", 0);+ CopyFile file;+ file.path = relative;+ file.bytes = info.st_size > 0 ? static_cast<std::uint64_t>(info.st_size) : 0;+#if defined(__APPLE__)+ file.mtime = Seconds(static_cast<std::int64_t>(info.st_mtimespec.tv_sec));+ file.mtime_nsec = static_cast<std::int64_t>(info.st_mtimespec.tv_nsec);+#else+ file.mtime = Seconds(static_cast<std::int64_t>(info.st_mtim.tv_sec));+ file.mtime_nsec = static_cast<std::int64_t>(info.st_mtim.tv_nsec);+#endif+ build.plan.total += file.bytes;+ if (build.plan.total > max_bytes) {+ const auto tib = max_bytes >> 40;+ return fail(tib > 0 && (max_bytes & ((1ull << 40) - 1)) == 0+ ? "The game is larger than " + std::to_string(tib) + " TiB. Playden can't copy it."+ : "The game is larger than " + WithThousands(max_bytes) + " bytes. Playden can't copy it.",+ 0);+ }+ build.plan.files.push_back(std::move(file));+ }+ build.ok = true;+ return build;+}++std::string SaveCopyPlan(const CopyPlan& plan, const std::string& root) {+ const auto journal = root + "/" + kCopyJournalDir;+ if (!MakeFolder(root) || !MakeFolder(journal)) return "Could not write to the storage.";+ std::string text;+ text.reserve(256 + plan.files.size() * 96 + plan.dirs.size() * 48);+ text += "{\"version\":1,\"source\":\"" + Escape(plan.source) + "\",\"program\":\"" + Escape(plan.program) ++ "\",\"runtime_number\":" + std::to_string(plan.runtime_number) + ",\"total\":" + std::to_string(plan.total) ++ ",\"file_count\":" + std::to_string(plan.files.size()) + ",\"dir_count\":" + std::to_string(plan.dirs.size()) ++ ",\n\"dirs\":[\n";+ for (std::size_t index = 0; index < plan.dirs.size(); ++index)+ text += "\"" + Escape(plan.dirs[index]) + (index + 1 < plan.dirs.size() ? "\",\n" : "\"\n");+ text += "],\n\"files\":[\n";+ for (std::size_t index = 0; index < plan.files.size(); ++index) {+ const auto& file = plan.files[index];+ text += "{\"path\":\"" + Escape(file.path) + "\",\"bytes\":" + std::to_string(file.bytes) ++ ",\"mtime\":" + std::to_string(Seconds(file.mtime)) + ",\"mtime_nsec\":" + std::to_string(file.mtime_nsec) ++ (index + 1 < plan.files.size() ? "},\n" : "}\n");+ }+ text += "]}\n";+ if (!WriteDurably(CopyPlanPath(root), text)) return "Could not write to the storage.";+ return SaveFileList(plan, root);+}++std::string SaveFileList(const CopyPlan& plan, const std::string& root) {+ // The paths the copy may create, as the depot installer writes them before any game data.+ std::string list;+ list.reserve(plan.files.size() * 64 + plan.dirs.size() * 32);+ for (const auto& dir : plan.dirs) list += "d\t" + dir + "\n";+ for (const auto& file : plan.files) list += "f\t" + file.path + "\n";+ if (!WriteDurably(root + "/" + kFileList, list)) return "Could not write to the storage.";+ return {};+}++std::optional<CopyPlan> LoadCopyPlan(const std::string& root) {+ FILE* file = std::fopen(CopyPlanPath(root).c_str(), "r");+ if (!file) return std::nullopt;+ std::unique_ptr<char[]> buffer(new char[kMaxLineBytes]);+ bool bad = false;+ const auto next = [&](std::string* line) {+ if (bad || !std::fgets(buffer.get(), static_cast<int>(kMaxLineBytes), file)) return false;+ line->assign(buffer.get());+ if (line->empty() || line->back() != '\n') { bad = true; return false; } // too long, or cut short+ return true;+ };+ CopyPlan plan;+ std::string line;+ std::optional<std::uint64_t> file_count, dir_count, total;+ if (next(&line)) {+ auto header = Entry(line);+ if (!header.empty()) header += "}";+ const auto value = ParseLine(header);+ const auto version = value && value->is_object() ? NumberField(*value, "version") : std::nullopt;+ const auto* source = value ? TextField(*value, "source") : nullptr;+ const auto* program = value ? TextField(*value, "program") : nullptr;+ const auto number = value ? NumberField(*value, "runtime_number") : std::nullopt;+ total = value ? NumberField(*value, "total") : std::nullopt;+ file_count = value ? NumberField(*value, "file_count") : std::nullopt;+ dir_count = value ? NumberField(*value, "dir_count") : std::nullopt;+ if (version != 1u || !source || !program || !number || *number > 0xffffffffu || !total || !file_count || !dir_count ||+ *file_count > kMaxCopyFiles || *dir_count > kMaxCopyFiles)+ bad = true;+ if (!bad) {+ plan.source = *source;+ plan.program = *program;+ plan.runtime_number = static_cast<std::uint32_t>(*number);+ }+ } else {+ bad = true;+ }+ if (!bad && !(next(&line) && line == "\"dirs\":[\n")) bad = true;+ for (std::uint64_t index = 0; !bad && dir_count && index < *dir_count; ++index) {+ if (!next(&line)) { bad = true; break; }+ const auto value = ParseLine(Entry(line));+ if (!value || !value->is_string() || !SafeRelative(value->text())) { bad = true; break; }+ plan.dirs.push_back(value->text());+ }+ if (!bad && !(next(&line) && line == "],\n")) bad = true;+ if (!bad && !(next(&line) && line == "\"files\":[\n")) bad = true;+ std::uint64_t sum = 0;+ for (std::uint64_t index = 0; !bad && file_count && index < *file_count; ++index) {+ if (!next(&line)) { bad = true; break; }+ const auto value = ParseLine(Entry(line));+ const auto* path = value && value->is_object() ? TextField(*value, "path") : nullptr;+ const auto bytes = path ? NumberField(*value, "bytes") : std::nullopt;+ const auto mtime = path ? NumberField(*value, "mtime") : std::nullopt;+ const auto nsec = path ? NumberField(*value, "mtime_nsec") : std::nullopt;+ if (!path || !SafeRelative(*path) || !bytes || !mtime || !nsec || *mtime > static_cast<std::uint64_t>(INT64_MAX) ||+ *nsec >= 1000000000u) {+ bad = true;+ break;+ }+ sum += *bytes;+ plan.files.push_back({*path, *bytes, static_cast<std::int64_t>(*mtime), static_cast<std::int64_t>(*nsec)});+ }+ if (!bad && !(next(&line) && line == "]}\n")) bad = true;+ if (!bad && std::fgets(buffer.get(), static_cast<int>(kMaxLineBytes), file)) bad = true; // nothing after the end+ std::fclose(file);+ if (bad || !total || sum != *total) return std::nullopt;+ plan.total = sum;+ return plan;+}++} // namespace playden::stores::localdiff --git a/src/stores/local/copy_plan.h b/src/stores/local/copy_plan.hnew file mode 100644index 0000000..56ba53e--- /dev/null+++ b/src/stores/local/copy_plan.h@@ -0,0 +1,75 @@+#pragma once+// The copy plan of an imported game: every folder and regular file of its folder, walked once+// before a "Copy into Playden" is queued and saved in the destination, so the copy, a resume after+// a restart and a cancel all work from the same list. The source is walked with+// platform/directory_walk.h only; a PS5 title can't read a link, so any link fails the plan.+//+// <dest>/.playden-copy/plan.json is one JSON document written one value per line, so it can be+// read back a line at a time (500 000 files would not fit a parsed document on the console):+// {"version":1,"source":"/mnt/usb0/playden/import/Rally","program":"Bin/Rally.exe",+// "runtime_number":4026531841,"total":938000000,"file_count":2,"dir_count":1,+// "dirs":[+// "Bin"+// ],+// "files":[+// {"path":"Bin/Rally.exe","bytes":1000,"mtime":1760000000,"mtime_nsec":0},+// {"path":"data.pak","bytes":937999000,"mtime":1760000000,"mtime_nsec":0}+// ]}+// The folder .playden-copy is the copy's journal folder: RemoveManagedInstall treats a folder+// holding it as Playden's own, so a partial copy can be cancelled or uninstalled.+#include <cstddef>+#include <cstdint>+#include <optional>+#include <string>+#include <vector>++namespace playden::stores::local {++inline constexpr const char* kCopyJournalDir = ".playden-copy";+inline constexpr const char* kCopyPlanFile = "plan.json";+inline constexpr std::size_t kMaxCopyFiles = 500000;+inline constexpr std::uint64_t kMaxCopyBytes = 1ull << 40; // 1 TiB++struct CopyFile {+ std::string path; // relative to the game folder, '/' separators+ std::uint64_t bytes = 0;+ std::int64_t mtime = 0; // seconds+ std::int64_t mtime_nsec = 0;+};++struct CopyPlan {+ std::string source; // the game folder (a path this process opens)+ std::string program; // relative to the folder; must be one of `files`+ std::uint32_t runtime_number = 0; // written to the install record's app_id; 0 when not known yet+ std::vector<std::string> dirs; // parents before children+ std::vector<CopyFile> files; // in copy order+ std::uint64_t total = 0; // bytes of `files`+};++struct CopyPlanBuild {+ bool ok = false;+ CopyPlan plan;+ std::string failure; // a sentence for the player, naming the path when one is at fault+ int error = 0; // errno of the failure, when there was one+};++// Walks `source` (absolute): every folder and regular file, hidden ones included, no depth limit.+// A link anywhere fails it, naming the link; so do an unreadable entry and more than `max_files`+// files or `max_bytes` bytes. Other kinds of entries (sockets, devices) are left out.+CopyPlanBuild BuildCopyPlan(const std::string& source, std::size_t max_files = kMaxCopyFiles,+ std::uint64_t max_bytes = kMaxCopyBytes);++bool PlanHasFile(const CopyPlan& plan, const std::string& path);++// <root>/.playden-copy/plan.json, and <root>/.playden-files (the paths the copy may create, as the+// depot installer lists them, so removal never needs to list folders). Creates `root`; its parent+// must exist. Written to a temporary name, synced and renamed. Empty, or why it failed.+std::string SaveCopyPlan(const CopyPlan& plan, const std::string& root);+// <root>/.playden-files alone (SaveCopyPlan writes it too): the plan's paths, so RemoveManagedInstall+// removes exactly them. Empty, or why it failed.+std::string SaveFileList(const CopyPlan& plan, const std::string& root);+// The plan saved in `root`; nothing when it's missing or doesn't parse.+std::optional<CopyPlan> LoadCopyPlan(const std::string& root);+std::string CopyPlanPath(const std::string& root);++} // namespace playden::stores::localdiff --git a/src/stores/local/copy_runner.cpp b/src/stores/local/copy_runner.cppnew file mode 100644index 0000000..3c4ba36--- /dev/null+++ b/src/stores/local/copy_runner.cpp@@ -0,0 +1,701 @@+#include "stores/local/copy_runner.h"++#include "platform/directory_walk.h"+#include "steam/backend/depot_installer.h"+#include "stores/local/scanner.h"++#include <algorithm>+#include <cerrno>+#include <cstdio>+#include <dirent.h>+#include <fcntl.h>+#include <sys/stat.h>+#include <unistd.h>+#include <unordered_map>+#include <utility>+#include <vector>++namespace playden::stores::local {+namespace {++constexpr const char* kRecord = ".playden-install.json"; // DepotInstaller::kInstallRecord+constexpr int kOpenFolder = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;+constexpr int kOpenFile = O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC;++bool Exists(const std::string& path) {+ struct stat info {};+ return ::stat(path.c_str(), &info) == 0;+}++std::optional<std::uint64_t> FileSize(const std::string& path) {+ struct stat info {};+ if (::stat(path.c_str(), &info) != 0 || !S_ISREG(info.st_mode)) return std::nullopt;+ return info.st_size > 0 ? static_cast<std::uint64_t>(info.st_size) : 0;+}++std::vector<std::string> Split(const std::string& path) {+ std::vector<std::string> parts;+ std::size_t start = 0;+ for (;;) {+ const auto slash = path.find('/', start);+ parts.push_back(path.substr(start, slash == std::string::npos ? std::string::npos : slash - start));+ if (slash == std::string::npos) return parts;+ start = slash + 1;+ }+}++bool SameTime(const struct stat& info, const CopyFile& file) {+#if defined(__APPLE__)+ const auto seconds = static_cast<std::int64_t>(info.st_mtimespec.tv_sec);+ const auto nsec = static_cast<std::int64_t>(info.st_mtimespec.tv_nsec);+#else+ const auto seconds = static_cast<std::int64_t>(info.st_mtim.tv_sec);+ const auto nsec = static_cast<std::int64_t>(info.st_mtim.tv_nsec);+#endif+ return (seconds < 0 ? 0 : seconds) == file.mtime && nsec == file.mtime_nsec;+}++bool AsPlanned(const struct stat& info, const CopyFile& file) {+ return S_ISREG(info.st_mode) && info.st_size >= 0 && static_cast<std::uint64_t>(info.st_size) == file.bytes &&+ SameTime(info, file);+}++// Opens the plan's files from the game folder, one walk step at a time (platform/directory_walk.h):+// the folders of the current file stay open, so files in the same folder open in one step.+class SourceWalk {+ public:+ explicit SourceWalk(std::string source) : source_(std::move(source)) {}+ ~SourceWalk() {+ for (auto& level : chain_) ::close(level.fd);+ if (root_.fd >= 0) ::close(root_.fd);+ }+ SourceWalk(const SourceWalk&) = delete;+ SourceWalk& operator=(const SourceWalk&) = delete;++ // A descriptor for the file, or -1 with errno.+ int OpenFile(const std::string& relative) {+ if (root_.fd < 0) {+ root_.fd = platform::OpenDirectoryWalk(source_);+ if (root_.fd < 0) return -1;+ }+ const auto parts = Split(relative);+ std::size_t common = 0;+ while (common < chain_.size() && common + 1 < parts.size() && chain_[common].name == parts[common]) ++common;+ while (chain_.size() > common) {+ ::close(chain_.back().fd);+ chain_.pop_back();+ }+ for (std::size_t index = common; index + 1 < parts.size(); ++index) {+ Level& parent = chain_.empty() ? root_ : chain_.back();+ const int fd = platform::OpenWalkChild(parent.fd, parts[index].c_str(), kOpenFolder, Listed(parent, parts[index]));+ if (fd < 0) return -1;+ chain_.push_back(Level{parts[index], fd, {}, false});+ }+ Level& folder = chain_.empty() ? root_ : chain_.back();+ return platform::OpenWalkChild(folder.fd, parts.back().c_str(), kOpenFile, Listed(folder, parts.back()));+ }++ private:+ struct Level {+ std::string name;+ int fd = -1;+ std::unordered_map<std::string, std::uint64_t> listed;+ bool listed_read = false;+ };+ // A walk by path checks each step against its folder's listing; the listing is read once per+ // folder instead of once per file (0: the walk reads it itself).+ std::uint64_t Listed(Level& level, const std::string& name) {+#if defined(PLAYDEN_WALK_BY_PATH)+ if (!level.listed_read) {+ level.listed_read = true;+ const int listing_fd = ::dup(level.fd);+ DIR* listing = listing_fd >= 0 ? ::fdopendir(listing_fd) : nullptr;+ if (!listing && listing_fd >= 0) ::close(listing_fd);+ if (listing) {+ while (const auto* entry = ::readdir(listing)) level.listed[entry->d_name] = entry->d_fileno;+ ::closedir(listing);+ }+ }+ const auto found = level.listed.find(name);+ return found == level.listed.end() ? 0 : found->second;+#else+ (void)level;+ (void)name;+ return 0;+#endif+ }++ std::string source_;+ Level root_;+ std::vector<Level> chain_;+};++enum class CopyStatus { kOk, kChanged, kCanceled, kReadFailed, kWriteFailed, kNoSpace };++bool WriteAll(int fd, const char* data, std::size_t size, int* error) {+ while (size > 0) {+ const auto written = ::write(fd, data, size);+ if (written < 0) {+ if (errno == EINTR) continue;+ *error = errno;+ return false;+ }+ data += written;+ size -= static_cast<std::size_t>(written);+ }+ return true;+}++CopyStatus CopyOne(SourceWalk& walk, const CopyFile& file, const std::string& destination, std::vector<char>& buffer,+ const std::atomic<bool>& cancel, const std::function<void(std::uint64_t)>& copied_bytes) {+ const int in = walk.OpenFile(file.path);+ if (in < 0) {+ const int error = errno;+ // Gone, a link or a folder in its place, or another file under its name.+ if (error == ENOENT || error == ENOTDIR || error == ELOOP || error == EMLINK || error == ESTALE || error == EISDIR)+ return CopyStatus::kChanged;+ return CopyStatus::kReadFailed;+ }+ struct stat before {};+ if (::fstat(in, &before) != 0) { ::close(in); return CopyStatus::kReadFailed; }+ if (!AsPlanned(before, file)) { ::close(in); return CopyStatus::kChanged; }+ const auto part = destination + ".part";+ const int out = ::open(part.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);+ if (out < 0) {+ const int error = errno;+ ::close(in);+ return error == ENOSPC ? CopyStatus::kNoSpace : CopyStatus::kWriteFailed;+ }+ const auto stop = [&](CopyStatus status) {+ ::close(in);+ ::close(out);+ std::remove(part.c_str());+ return status;+ };+ std::uint64_t copied = 0;+ for (;;) {+ if (cancel.load()) return stop(CopyStatus::kCanceled);+ const auto count = ::read(in, buffer.data(), buffer.size());+ if (count < 0) {+ if (errno == EINTR) continue;+ return stop(CopyStatus::kReadFailed);+ }+ if (count == 0) break;+ if (copied + static_cast<std::uint64_t>(count) > file.bytes) return stop(CopyStatus::kChanged); // it grew+ int error = 0;+ if (!WriteAll(out, buffer.data(), static_cast<std::size_t>(count), &error))+ return stop(error == ENOSPC ? CopyStatus::kNoSpace : CopyStatus::kWriteFailed);+ copied += static_cast<std::uint64_t>(count);+ copied_bytes(static_cast<std::uint64_t>(count));+ }+ struct stat after {};+ if (copied != file.bytes || ::fstat(in, &after) != 0 || !AsPlanned(after, file)) return stop(CopyStatus::kChanged);+ ::close(in);+ if (::fsync(out) != 0) {+ const int error = errno;+ ::close(out);+ std::remove(part.c_str());+ return error == ENOSPC ? CopyStatus::kNoSpace : CopyStatus::kWriteFailed;+ }+ if (::close(out) != 0 || std::rename(part.c_str(), destination.c_str()) != 0) {+ std::remove(part.c_str());+ return CopyStatus::kWriteFailed;+ }+ return CopyStatus::kOk;+}++bool MakeFolder(const std::string& path) {+ if (::mkdir(path.c_str(), 0777) == 0) return true;+ struct stat info {};+ return errno == EEXIST && ::stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);+}++// The install record, as the depot installer writes it for another store's game.+bool WriteRecord(const std::string& root, const GameId& game, const CopyPlan& plan) {+ char text[512];+ const int length = std::snprintf(text, sizeof(text),+ "{\"version\":1,\"app_id\":%u,\"store\":\"%s\",\"game\":\"%s\",\"install_bytes\":%llu,"+ "\"files\":%llu,\"depots\":[]}\n",+ plan.runtime_number, std::string(Traits(game.store).key).c_str(), game.value.c_str(),+ static_cast<unsigned long long>(plan.total),+ static_cast<unsigned long long>(plan.files.size()));+ if (length <= 0 || static_cast<std::size_t>(length) >= sizeof(text)) return false;+ const auto path = root + "/" + kRecord;+ const auto temporary = path + ".tmp";+ FILE* file = std::fopen(temporary.c_str(), "w");+ if (!file) return false;+ bool ok = std::fwrite(text, 1, static_cast<std::size_t>(length), file) == static_cast<std::size_t>(length) &&+ std::fflush(file) == 0 && ::fsync(::fileno(file)) == 0;+ ok = std::fclose(file) == 0 && ok;+ ok = ok && std::rename(temporary.c_str(), path.c_str()) == 0;+ if (!ok) std::remove(temporary.c_str());+ return ok;+}++// Partial files the plan's list doesn't name, so RemoveManagedInstall can remove exactly its paths.+void RemoveParts(const std::string& root, const CopyPlan* plan) {+ if (!plan) return;+ for (const auto& file : plan->files) std::remove((root + "/" + file.path + ".part").c_str());+}++bool IsFolder(const std::string& path) {+ struct stat info {};+ return ::lstat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);+}++bool Missing(const std::string& path) {+ struct stat info {};+ return ::lstat(path.c_str(), &info) != 0 && errno == ENOENT;+}++std::string Parent(const std::string& path) {+ const auto slash = path.find_last_of('/');+ return slash == std::string::npos || slash == 0 ? std::string("/") : path.substr(0, slash);+}++bool SyncFolder(const std::string& path) {+ const int fd = ::open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);+ if (fd < 0) return false;+ const bool ok = ::fsync(fd) == 0;+ ::close(fd);+ return ok;+}++// Playden's own files in a moved game's folder, left out of its file list.+bool PlaydenFile(const std::string& path) {+ return path == kRecord || path == std::string(kRecord) + ".tmp" || path == ".playden-files" ||+ path == ".playden-files.tmp";+}++std::string Basename(const std::string& path) {+ const auto slash = path.find_last_of('/');+ return slash == std::string::npos ? path : path.substr(slash + 1);+}++} // namespace++LocalCopyRunner::LocalCopyRunner(ImportStore& store, LocalCopyOptions options)+ : store_(store), options_(std::move(options)) {+ if (options_.buffer_bytes == 0) options_.buffer_bytes = 1024 * 1024;+}++void LocalCopyRunner::SetChanged(std::function<void()> changed) {+ std::lock_guard<std::mutex> lock(changed_mutex_);+ changed_ = std::move(changed);+}++void LocalCopyRunner::Changed() {+ std::function<void()> changed;+ {+ std::lock_guard<std::mutex> lock(changed_mutex_);+ changed = changed_;+ }+ if (changed) changed();+}++void LocalCopyRunner::Log(const std::string& line) const {+ if (options_.log) options_.log(line);+}++std::string LocalCopyRunner::Enqueue(installs::InstallService& service, const ImportEntry& entry,+ const std::string& source, const std::string& volume_id,+ std::uint32_t runtime_number) {+ const auto game = GameId::Local(entry.id);+ if (entry.state == ImportState::kCopied) return "The game is already in Playden.";+ if (entry.state != ImportState::kInPlace) return "The game is already being copied.";+ auto snapshot = service.Snapshot();+ if (std::any_of(snapshot.installed.begin(), snapshot.installed.end(),+ [&](const installs::InstalledGame& installed) { return installed.id == game; }))+ return "The game is already in Playden.";+ if (std::any_of(snapshot.jobs.begin(), snapshot.jobs.end(), [&](const installs::InstallJobView& job) { return job.id == game; }))+ return "The game is already being copied.";+ const auto target = volume_id.empty() ? snapshot.default_volume_id : volume_id;+ const auto volume = std::find_if(snapshot.volumes.begin(), snapshot.volumes.end(),+ [&](const installs::InstallVolume& known) { return known.id == target; });+ if (volume == snapshot.volumes.end() || !volume->available) return "The storage is disconnected.";++ auto build = BuildCopyPlan(source);+ if (!build.ok) {+ Log("import copy plan failed id=" + entry.id + " errno=" + std::to_string(build.error) + ": " + build.failure);+ return build.failure;+ }+ auto& plan = build.plan;+ plan.program = entry.program;+ plan.runtime_number = runtime_number;+ if (!PlanHasFile(plan, plan.program)) return "The program is not in the game's folder.";++ const auto root = volume->games_root + "/" + ToKey(game);+ MakeFolder(volume->games_root);+ // A folder left by an earlier copy is Playden's own and goes; anything else stays and refuses.+ if (!steam::RemoveManagedInstall(root) || Exists(root)) {+ Log("import copy refused id=" + entry.id + ": " + root + " is in the way");+ return "A folder in Playden's games folder is in the way of the copy.";+ }+ if (const auto saved = SaveCopyPlan(plan, root); !saved.empty()) {+ steam::RemoveManagedInstall(root);+ return saved;+ }+ const auto queued = store_.QueueCopy(entry.id);+ if (!queued.ok()) {+ steam::RemoveManagedInstall(root);+ return queued.error() == ImportError::kWrongState ? "The game is already being copied."+ : "Playden's list of imported games could not be saved.";+ }+ service.Install(game, plan.total, target);+ snapshot = service.Snapshot();+ const auto job = std::find_if(snapshot.jobs.begin(), snapshot.jobs.end(),+ [&](const installs::InstallJobView& view) { return view.id == game; });+ if (job == snapshot.jobs.end() || job->state == installs::InstallJobState::kFailed) {+ // Refused at once (free space, the drive, a full Downloads): nothing stays behind.+ const std::string failure = job == snapshot.jobs.end() ? "Downloads is full. Try again later." : job->failure;+ store_.CancelCopy(entry.id, failure);+ if (job != snapshot.jobs.end()) service.Cancel(game); // its folder goes through Remove+ else steam::RemoveManagedInstall(root);+ Log("import copy refused id=" + entry.id + ": " + failure);+ return failure;+ }+ Log("import copy queued id=" + entry.id + " volume=" + target + " files=" + std::to_string(plan.files.size()) ++ " bytes=" + std::to_string(plan.total));+ return {};+}++installs::InstallRunner::Result LocalCopyRunner::Abandon(const ImportEntry& entry, const std::string& root,+ const CopyPlan* plan, const std::string& failure) {+ RemoveParts(root, plan);+ steam::RemoveManagedInstall(root);+ store_.CancelCopy(entry.id, failure);+ Log("import copy failed id=" + entry.id + ": " + failure);+ Changed();+ Result result;+ result.failure = failure;+ return result;+}++installs::InstallRunner::Result LocalCopyRunner::Install(+ const GameId& id, const std::string& root, const std::atomic<bool>& cancel,+ const std::function<void(std::uint64_t, std::uint64_t, std::uint64_t)>& progress,+ const std::function<void(installs::InstallStep)>& step) {+ Result result;+ if (id.store != Store::kLocal) {+ result.failure = "This game can't be copied.";+ return result;+ }+ if (!store_.loaded()) {+ // Playden is starting: the Local source reads imports.json first.+ result.retry_later = true;+ result.waiting = "Waiting for the list of imported games.";+ return result;+ }+ auto entry = store_.Find(id.value);+ if (!entry) {+ result.failure = "The game is no longer in the library.";+ return result;+ }+ if (entry->state == ImportState::kCopied) {+ result.failure = "The game is already in Playden.";+ return result;+ }+ if (entry->state == ImportState::kInPlace) {+ result.failure = entry->failure.empty() ? kCopyStoppedFailure : entry->failure;+ return result;+ }+ if (entry->state == ImportState::kCopyQueued) {+ const auto started = store_.StartCopy(entry->id);+ if (!started.ok()) {+ result.failure = "Playden's list of imported games could not be saved.";+ return result;+ }+ Changed();+ }+ const auto plan = LoadCopyPlan(root);+ if (!plan) return Abandon(*entry, root, nullptr, kCopyStoppedFailure);++ // What a resume already has: files at their final name and planned size.+ std::vector<char> done(plan->files.size(), 0);+ std::uint64_t done_bytes = 0;+ for (std::size_t index = 0; index < plan->files.size(); ++index) {+ const auto size = FileSize(root + "/" + plan->files[index].path);+ if (size && *size == plan->files[index].bytes) {+ done[index] = 1;+ done_bytes += *size;+ }+ }+ if (progress) progress(done_bytes, plan->total, done_bytes);++ // Free space for what is left, as when it was queued (the console storage reserve left out).+ const auto space = options_.space ? options_.space(root) : steam::QueryVolumeSpace(root);+ const auto remaining = plan->total - done_bytes;+ if (space) {+ if (space->free_bytes < steam::kInstallSpaceMargin || remaining > space->free_bytes - steam::kInstallSpaceMargin) {+ result.failure = "Not enough free space. Make room, then Resume.";+ return result;+ }+ } else if (options_.space || steam::VolumeSpaceSupported()) {+ result.failure = "Free space is unavailable. Check the storage and try again.";+ return result;+ }++ for (const auto& dir : plan->dirs)+ if (!MakeFolder(root + "/" + dir)) {+ result.failure = "Could not write the game files.";+ return result;+ }+ Log("import copy started id=" + entry->id + " files=" + std::to_string(plan->files.size()) ++ " bytes=" + std::to_string(plan->total) + " resumed_bytes=" + std::to_string(done_bytes));++ if (step) step(installs::InstallStep::kDownloading);+ std::vector<char> buffer(options_.buffer_bytes);+ SourceWalk walk(plan->source);+ std::uint64_t copied = done_bytes;+ const auto on_bytes = [&](std::uint64_t count) {+ copied += count;+ if (progress) progress(copied, plan->total, copied);+ };+ for (std::size_t index = 0; index < plan->files.size(); ++index) {+ if (done[index]) continue;+ if (cancel.load()) {+ result.canceled = true;+ return result;+ }+ const auto& file = plan->files[index];+ const auto before = copied;+ const auto status = CopyOne(walk, file, root + "/" + file.path, buffer, cancel, on_bytes);+ if (status != CopyStatus::kOk && copied != before) {+ copied = before; // the file's bytes went with its .part+ if (progress) progress(copied, plan->total, copied);+ }+ switch (status) {+ case CopyStatus::kOk: break;+ case CopyStatus::kCanceled: result.canceled = true; return result;+ case CopyStatus::kChanged:+ Log("import copy source changed id=" + entry->id + " path=" + file.path);+ return Abandon(*entry, root, &*plan, kCopyChangedFailure);+ case CopyStatus::kReadFailed:+ Log("import copy read failed id=" + entry->id + " path=" + file.path + " errno=" + std::to_string(errno));+ result.failure = "Could not read the game's files. Check the drive, then Resume.";+ return result;+ case CopyStatus::kNoSpace: result.failure = "Not enough free space. Make room, then Resume."; return result;+ case CopyStatus::kWriteFailed:+ Log("import copy write failed id=" + entry->id + " path=" + file.path + " errno=" + std::to_string(errno));+ result.failure = "Could not write the game files.";+ return result;+ }+ if (options_.after_file) options_.after_file(file.path);+ }++ // Verify: every planned file at its size, and the program there.+ for (const auto& file : plan->files) {+ const auto size = FileSize(root + "/" + file.path);+ if (!size || *size != file.bytes) {+ Log("import copy verify failed id=" + entry->id + " path=" + file.path);+ return Abandon(*entry, root, &*plan, "The copy could not be verified. Copy it again.");+ }+ }+ if (!PlanHasFile(*plan, plan->program) || CheckProgram(root, plan->program) != ProgramCheck::kOk)+ return Abandon(*entry, root, &*plan, "The game's program is missing from the copy. Copy it again.");++ // A game set up to play from its drive folder: its PS5 title follows the copy first. Until that+ // commits the drive folder stays the active one; a resume after a restart finds the files copied+ // and comes back here (a relocation already written only finishes its republish).+ {+ LocalRelocation relocation;+ relocation.game = id;+ relocation.runtime_number = plan->runtime_number;+ relocation.root = root; // its volume: the job's (the relocate hook asks the service)+ const auto relocated = Relocate(relocation);+ if (relocated.outcome == LocalRelocationResult::Outcome::kBusy) {+ Log("import copy waits id=" + entry->id + ": " + relocated.message);+ result.retry_later = true;+ result.waiting = relocated.message.empty() ? "Waiting for the game to close." : relocated.message;+ return result;+ }+ if (relocated.outcome == LocalRelocationResult::Outcome::kFailed) {+ Log("import copy relocation failed id=" + entry->id + ": " + relocated.message);+ result.failure = relocated.message.empty() ? "The game's PS5 title could not follow the copy." : relocated.message;+ return result;+ }+ }++ // Commit: the record makes it a complete managed install, then the plan goes, then imports.json.+ if (!WriteRecord(root, id, *plan)) {+ result.failure = "Could not write the game files.";+ return result;+ }+ const auto journal = root + "/" + kCopyJournalDir;+ std::remove(CopyPlanPath(root).c_str());+ ::rmdir(journal.c_str());+ result.ok = true;+ result.install_bytes = plan->total;+ if (options_.before_finish && !options_.before_finish(id, root)) {+ Log("import copy complete id=" + entry->id + "; entry left copying");+ return result;+ }+ const auto finished = store_.FinishCopy(entry->id);+ // A failed write is finished by the reconcile at the next start.+ Log("import copy complete id=" + entry->id + (finished.ok() ? "" : " (imports.json not saved: " + finished.message() + ")"));+ Changed();+ return result;+}++LocalRelocationResult LocalCopyRunner::Relocate(const LocalRelocation& relocation) {+ if (options_.relocate) return options_.relocate(relocation);+ LocalRelocationResult result;+ if (relocation.move_files) {+ result.message = relocation.move_files();+ if (!result.message.empty()) result.outcome = LocalRelocationResult::Outcome::kFailed;+ }+ return result;+}++std::string LocalCopyRunner::Move(installs::InstallService& service, const ImportEntry& entry, const std::string& source,+ std::uint32_t runtime_number) {+ const auto game = GameId::Local(entry.id);+ if (entry.state == ImportState::kCopied) return "The game is already in Playden.";+ // copying without a copy job: a move a restart (or a busy title) interrupted.+ const bool resuming = entry.state == ImportState::kCopying;+ if (!resuming && entry.state != ImportState::kInPlace) return "The game is already being copied.";+ if (entry.volume != ImportStore::kDropFolderVolume) return "Only games in the drop folder can be moved. Copy it instead.";+ auto snapshot = service.Snapshot();+ if (std::any_of(snapshot.jobs.begin(), snapshot.jobs.end(), [&](const installs::InstallJobView& job) { return job.id == game; }))+ return "The game is already being copied.";+ const bool listed = std::any_of(snapshot.installed.begin(), snapshot.installed.end(),+ [&](const installs::InstalledGame& installed) { return installed.id == game; });+ if (listed && !resuming) return "The game is already in Playden.";+ const auto volume = std::find_if(snapshot.volumes.begin(), snapshot.volumes.end(), [](const installs::InstallVolume& known) {+ return known.id == installs::InstallService::kPrimaryVolumeId;+ });+ if (volume == snapshot.volumes.end() || !volume->available) return "The console storage is unavailable.";+ const auto root = volume->games_root + "/" + ToKey(game);+ const auto back_in_place = [&](const std::string& failure, const std::string& why) {+ store_.CancelCopy(entry.id, failure);+ Log("import move stopped id=" + entry.id + ": " + why);+ Changed();+ return why;+ };++ CopyPlan plan;+ if (!resuming) {+ if (!Missing(root)) {+ Log("import move refused id=" + entry.id + ": " + root + " is in the way");+ return "A folder in Playden's games folder is in the way of the move.";+ }+ // The files are listed first: a link refuses the move as it would a copy, and the list goes+ // into the moved folder for its uninstall.+ auto build = BuildCopyPlan(source);+ if (!build.ok) {+ Log("import move plan failed id=" + entry.id + " errno=" + std::to_string(build.error) + ": " + build.failure);+ return build.failure;+ }+ plan = std::move(build.plan);+ if (!PlanHasFile(plan, entry.program)) return "The program is not in the game's folder.";+ MakeFolder(volume->games_root);+ // copying from here on: a restart finishes the move from what the folders say.+ const auto queued = store_.QueueCopy(entry.id);+ const auto started = queued.ok() ? store_.StartCopy(entry.id) : queued;+ if (!started.ok()) {+ if (queued.ok()) store_.CancelCopy(entry.id);+ return "Playden's list of imported games could not be saved.";+ }+ Changed();+ } else {+ const bool source_there = IsFolder(source);+ const bool root_there = IsFolder(root);+ if (!root_there && source_there) return back_in_place({}, kMoveStoppedFailure); // the rename never ran+ if (!root_there) return back_in_place("The game's folder is missing.", "The game's folder is missing.");+ if (source_there) return back_in_place({}, "A folder in Playden's games folder is in the way of the move.");+ // Renamed already: the files are listed where they are now.+ auto build = BuildCopyPlan(root);+ if (!build.ok) {+ Log("import move plan failed id=" + entry.id + " errno=" + std::to_string(build.error) + ": " + build.failure);+ return build.failure;+ }+ plan = std::move(build.plan);+ plan.files.erase(std::remove_if(plan.files.begin(), plan.files.end(), [](const CopyFile& file) { return PlaydenFile(file.path); }),+ plan.files.end());+ plan.total = 0;+ for (const auto& file : plan.files) plan.total += file.bytes;+ }+ plan.source = source;+ plan.program = entry.program;+ plan.runtime_number = runtime_number;++ // The rename, under the title's publication lease once it is stopped (RuntimeHost::Relocate).+ // Both parents are synced before the title's record names the new folder.+ LocalRelocation relocation;+ relocation.game = game;+ relocation.runtime_number = runtime_number;+ relocation.root = root;+ relocation.volume_id = volume->id;+ relocation.move_files = [this, source, root]() -> std::string {+ if (Missing(source) && IsFolder(root)) return {}; // done before a restart+ int error = 0;+ const auto rename = [&] {+ if (std::rename(source.c_str(), root.c_str()) == 0) return true;+ error = errno;+ return false;+ };+ const bool renamed = options_.as_root ? options_.as_root(rename) : rename();+ if (!renamed) {+ Log("import move rename failed from=" + source + " to=" + root + " errno=" + std::to_string(error));+ return "The game could not be moved into Playden (error " + std::to_string(error) + ").";+ }+ if (!SyncFolder(Parent(source)) || !SyncFolder(Parent(root))) {+ Log("import move sync failed from=" + source + " to=" + root + " errno=" + std::to_string(errno));+ return "The move could not be saved on the storage. Move it again.";+ }+ Log("import move renamed from=" + source + " to=" + root);+ return {};+ };+ const auto relocated = Relocate(relocation);+ if (relocated.outcome != LocalRelocationResult::Outcome::kDone) {+ const auto message = relocated.message.empty() ? std::string("The game could not be moved into Playden.") : relocated.message;+ // Not renamed: it plays from the drop folder as before. Renamed but not recorded: copying, so+ // Move again (or the next start) finishes it.+ if (IsFolder(source) && Missing(root)) return back_in_place({}, message);+ Log("import move interrupted id=" + entry.id + ": " + message);+ return message;+ }++ // Managed from here: the file list, then the record, then the install service lists it.+ if (!SaveFileList(plan, root).empty() || !WriteRecord(root, game, plan)) {+ Log("import move markers failed id=" + entry.id);+ return "Could not write the game files. Move it again.";+ }+ if (!service.Adopt(game, volume->id, plan.total)) {+ Log("import move not listed id=" + entry.id);+ return "Playden could not list the moved game. Move it again.";+ }+ if (options_.before_finish && !options_.before_finish(game, root)) {+ Log("import move complete id=" + entry.id + "; entry left copying");+ return {};+ }+ const auto finished = store_.FinishCopy(entry.id);+ Log("import move complete id=" + entry.id + " files=" + std::to_string(plan.files.size()) ++ (finished.ok() ? "" : " (imports.json not saved: " + finished.message() + ")"));+ Changed();+ return {};+}++bool LocalCopyRunner::Remove(const std::string& root) {+ const auto plan = LoadCopyPlan(root);+ RemoveParts(root, plan ? &*plan : nullptr);+ const bool removed = steam::RemoveManagedInstall(root);+ const auto game = ParseKey(Basename(root));+ if (!game || game->store != Store::kLocal || !store_.loaded()) return removed;+ const auto entry = store_.Find(game->value);+ if (!entry) return removed;+ if (entry->state == ImportState::kCopyQueued || entry->state == ImportState::kCopying) {+ store_.CancelCopy(entry->id);+ Log("import copy cancelled id=" + entry->id + (removed ? "" : " (folder not removed)"));+ Changed();+ } else if (entry->state == ImportState::kCopied) {+ // Uninstalled: the game leaves the library (its bottle stays, like any kept data). The service+ // has already forgotten the install, so a folder that didn't go is left as it is.+ store_.Uninstalled(entry->id);+ Log("import copy uninstalled id=" + entry->id + (removed ? "" : " (folder not removed)"));+ Changed();+ }+ return removed;+}++} // namespace playden::stores::localdiff --git a/src/stores/local/copy_runner.h b/src/stores/local/copy_runner.hnew file mode 100644index 0000000..9e8a390--- /dev/null+++ b/src/stores/local/copy_runner.h@@ -0,0 +1,126 @@+#pragma once+// "Copy into Playden": the install service's runner for imported games (Store::kLocal). An+// imported game is copied from its drive folder into <games_root>/local-<id> on the chosen volume+// and becomes a managed install there; the drive folder is never touched.+//+// Flow: Enqueue (on the Local source's worker) walks the folder into a copy plan, saves it in the+// destination (copy_plan.h), sets the entry copy_queued and queues the job with the plan's total+// (the service's free-space check). Install (on the service's worker) sets it copying, checks free+// space again, copies the files in plan order (<path>.part, fsync, rename; a resume skips files+// already at their planned size), verifies every file and the program, writes the install record+// and sets the entry copied. Remove (cancel or uninstall) deletes the folder and moves the entry+// back to in_place (a copy) or to the removed list (a copied game).+//+// A game already prepared to play from its folder keeps its PS5 title, runtime number and bottle:+// once the copy is verified, and before the install record makes it a managed install, its title+// is relocated to the copy (LocalCopyOptions::relocate). While its title runs the job waits and+// tries again; the drive folder stays the active one until the relocation commits.+//+// "Move into Playden" (Move) is for a drop-folder game, on the same file system as Playden's games+// folder: one rename(2) as root of /data/playden-import/<name> to <games_root>/local-<id>, done by+// the relocation under the title's publication lease (a game without a title at once), then the+// install record and the file list, the install service lists it (InstallService::Adopt) and the+// entry becomes copied. The entry says copying meanwhile, so a restart finishes it (Move again).+//+// Failures that a Resume can't fix (the source changed, the program is missing, the plan is+// gone) delete the partial copy and set the entry back to in_place with the failure; the job then+// waits in Downloads as failed until cancelled. Others (no space, a read or write error) keep the+// partial copy and the entry copying, so Resume carries on.+#include "installs/install_service.h"+#include "steam/install_volumes.h"+#include "stores/local/copy_plan.h"+#include "stores/local/import_store.h"++#include <atomic>+#include <cstddef>+#include <cstdint>+#include <functional>+#include <mutex>+#include <optional>+#include <string>++namespace playden::stores::local {++inline constexpr const char* kCopyChangedFailure = "The game's files changed while copying. Copy it again.";+inline constexpr const char* kCopyStoppedFailure = "The copy stopped. Copy it again.";++inline constexpr const char* kMoveStoppedFailure = "The move stopped. Move it again.";++// What LocalCopyOptions::relocate is asked: the game's folder in Playden becomes the one it plays from.+struct LocalRelocation {+ GameId game;+ std::uint32_t runtime_number = 0; // 0: none yet (no PS5 title)+ std::string root; // the folder in Playden, as this process opens it+ std::string volume_id; // the install volume of `root`; empty: the game's copy job's+ // The move's rename (empty for a copy): run once the game's title is stopped; empty, or why not.+ // Safe to run again after it succeeded.+ std::function<std::string()> move_files;+};+struct LocalRelocationResult {+ enum class Outcome { kDone, kBusy, kFailed };+ Outcome outcome = Outcome::kDone;+ std::string message;+};++struct LocalCopyOptions {+ std::function<void(const std::string&)> log;+ // Free space of the drive holding a folder; empty: steam::QueryVolumeSpace (it leaves out the+ // console storage reserve).+ std::function<std::optional<steam::VolumeSpace>(const std::string& path)> space;+ std::size_t buffer_bytes = 1024 * 1024;+ // Moves the game's PS5 title to its folder in Playden (RuntimeHost::Relocate): after a copy is+ // verified, and for a move (which runs move_files). Empty: no game has a title (move_files runs at+ // once).+ std::function<LocalRelocationResult(const LocalRelocation&)> relocate;+ // Runs work with root raised (ps5::WithRoot): the move's rename of a root-owned drop folder.+ // Empty: as the app user.+ std::function<bool(const std::function<bool()>& work)> as_root;+ // Tests: called once the copy is a complete managed install (record written), before+ // imports.json says `copied`. False stops there, as a crash would: the entry stays `copying` and+ // the next start's reconcile (LocalLibrarySource::Reconcile) finishes it.+ std::function<bool(const GameId& game, const std::string& root)> before_finish;+ // Tests: after each file reaches its final name.+ std::function<void(const std::string& path)> after_file;+};++class LocalCopyRunner final : public installs::InstallRunner {+ public:+ explicit LocalCopyRunner(ImportStore& store, LocalCopyOptions options = {});++ // Called after the runner changed imports.json, so the Local source shows it+ // (LocalLibrarySource::Reload). May be set while the service runs.+ void SetChanged(std::function<void()> changed);++ // Queues the copy of `entry` (in_place) from `source` (its folder, as this process opens it) to+ // `volume_id` (empty: the service's default volume). `runtime_number` goes into the install+ // record. Empty, or a sentence for the player; on failure nothing is left behind and the entry+ // stays in_place (a free-space refusal records its failure on the entry).+ std::string Enqueue(installs::InstallService& service, const ImportEntry& entry, const std::string& source,+ const std::string& volume_id, std::uint32_t runtime_number);++ // "Move into Playden" of a drop-folder entry (in_place, or copying after an interrupted move)+ // from `source` (its folder, as this process opens it) into the primary volume's games folder.+ // `runtime_number` goes into the install record. Runs on the caller's thread (the Local source's+ // worker) and blocks for the relocation. Empty, or a sentence for the player; a refusal before+ // the rename leaves the entry in_place, one after it leaves it copying for a retry.+ std::string Move(installs::InstallService& service, const ImportEntry& entry, const std::string& source,+ std::uint32_t runtime_number);++ Result Install(const GameId& id, const std::string& root, const std::atomic<bool>& cancel,+ const std::function<void(std::uint64_t done, std::uint64_t total, std::uint64_t held)>& progress,+ const std::function<void(installs::InstallStep)>& step) override;+ bool Remove(const std::string& root) override;++ private:+ LocalRelocationResult Relocate(const LocalRelocation& relocation);+ Result Abandon(const ImportEntry& entry, const std::string& root, const CopyPlan* plan, const std::string& failure);+ void Changed();+ void Log(const std::string& line) const;++ ImportStore& store_;+ LocalCopyOptions options_;+ std::mutex changed_mutex_;+ std::function<void()> changed_;+};++} // namespace playden::stores::localdiff --git a/src/stores/local/drop_folder_access.cpp b/src/stores/local/drop_folder_access.cppnew file mode 100644index 0000000..0be4216--- /dev/null+++ b/src/stores/local/drop_folder_access.cpp@@ -0,0 +1,336 @@+// drop_folder_access.cpp - Opens the files of a game copied over FTP into the drop folder.+// Copyright (c) 2026 Playden contributors+// SPDX-License-Identifier: MIT+#include "stores/local/drop_folder_access.h"+#include "platform/directory_walk.h"++#include <algorithm>+#include <cerrno>+#include <cstdint>+#include <cstring>+#include <dirent.h>+#include <fcntl.h>+#include <sys/stat.h>+#include <unistd.h>+#include <utility>+#include <vector>++namespace playden::stores::local {+namespace {+namespace fs = std::filesystem;++constexpr int kMaxDepth = 64;+constexpr std::size_t kMaxEntries = 1000000;+constexpr int kOpenFlags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC;++std::string Error(int error) { return std::to_string(error) + " (" + std::strerror(error) + ")"; }+bool Unsupported(int error) { return error == ENOSYS || error == EINVAL; }+bool IsLink(int error) { return error == ELOOP || error == EMLINK; } // EMLINK: FreeBSD's O_NOFOLLOW++bool PlainName(const std::string& name) {+ return !name.empty() && name != "." && name != ".." && name.find('/') == std::string::npos &&+ name.find('\0') == std::string::npos;+}++struct Child {+ std::string name;+ std::uint64_t inode = 0;+ unsigned char type = DT_UNKNOWN;+};+struct Frame {+ int fd = -1;+ fs::path relative; // to the entry; empty for the entry itself+ std::vector<Child> children;+ std::size_t next = 0;+ int depth = 0;+};+// A root-owned entry whose mode the next raise changes (the descriptor is the item's).+struct Pending {+ int fd = -1;+ mode_t mode = 0;+ fs::path relative;+};++class Walk {+ public:+ Walk(std::string entry, const DropFolderAccessOptions& options)+ : entry_(std::move(entry)), options_(options), app_(options.app_uid ? *options.app_uid : ::geteuid()) {}+ ~Walk() {+ for (auto& frame : stack_) if (frame.fd >= 0) ::close(frame.fd);+ for (auto& item : pending_) ::close(item.fd);+ }++ DropFolderAccessResult Run() {+ const fs::path console = entry_;+ if (!OpenEntry(console.filename().string())) return Finish();+ while (!stack_.empty() && message_.empty()) {+ Frame& top = stack_.back();+ if (top.next == top.children.size()) {+ ::close(top.fd);+ stack_.pop_back();+ continue;+ }+ const Child child = top.children[top.next++];+ Visit(top, child);+ if (pending_.size() >= std::max<std::size_t>(options_.batch, 1)) Flush();+ }+ if (message_.empty()) Flush();+ result_.ok = message_.empty();+ return Finish();+ }++ private:+ std::string Console(const fs::path& relative) const {+ return relative.empty() ? entry_ : entry_ + "/" + relative.generic_string();+ }+ fs::path Host(const std::string& console) const { return options_.namespace_root / fs::path(console).relative_path(); }+ void Note(const std::string& line) const { if (options_.log) options_.log(line); }++ bool Fail(const fs::path& relative, const std::string& detail, const std::string& log_detail) {+ if (!message_.empty()) return false;+ message_ = "Playden can't open the game's files in " + entry_ + ": " + detail ++ ". Fix it over FTP (folders 777, files 666), then try again.";+ Note("drop folder access failed entry=" + entry_ + " path=" + Console(relative) + " " + log_detail);+ return false;+ }+ bool Refuse(const fs::path& relative, int error) {+ return Fail(relative, "this console can't walk the folder safely", "unsupported=" + Error(error));+ }++ bool AsRoot(const std::function<bool()>& work) {+ ++result_.raises;+ return options_.as_root && options_.as_root(work);+ }++ // Opens `name` in `parent` as the app user, or in a raise when the app user may not (its mode is+ // queued like any other). -1 with message_ set, or -1 with errno for a link or a gone entry.+ int Open(int parent, const fs::path& relative, const std::string& name, std::uint64_t inode, int flags) {+ int fd = platform::OpenWalkChild(parent, name.c_str(), flags, inode);+ if (fd >= 0) return fd;+ int error = errno;+ if (Unsupported(error)) { Refuse(relative, error); return -1; }+ if (error != EACCES && error != EPERM) { errno = error; return -1; }+ bool opened = false;+ const bool ran = AsRoot([&] {+ fd = platform::OpenWalkChild(parent, name.c_str(), flags, inode);+ error = errno;+ opened = fd >= 0;+ return opened;+ });+ // An fd opened while root could not be dropped again is not used.+ if (!ran && fd >= 0) { ::close(fd); fd = -1; }+ if (opened && ran) return fd;+ if (Unsupported(error)) { Refuse(relative, error); return -1; }+ if (IsLink(error) || error == ENOENT) { errno = error; return -1; }+ Fail(relative, Console(relative) + " can't be opened", "open=" + Error(error) + (options_.as_root ? "" : " root=unavailable"));+ return -1;+ }++ // Wants `mode` on fd (owned by this call when queued for root).+ bool Change(int fd, const struct stat& info, mode_t mode, const fs::path& relative, bool keep_fd) {+ if ((info.st_mode & 07777) == mode) return true;+ ++result_.changed;+ if (info.st_uid == app_) {+ if (::fchmod(fd, mode) == 0) return true;+ return Fail(relative, Console(relative) + " can't be changed", "chmod=" + Error(errno));+ }+ const int owned = keep_fd ? ::dup(fd) : fd;+ if (owned < 0) return Fail(relative, Console(relative) + " can't be changed", "dup=" + Error(errno));+ pending_.push_back({owned, mode, relative});+ return true;+ }++ // One raise for the queued root-owned entries.+ void Flush() {+ if (pending_.empty()) return;+ auto items = std::move(pending_);+ pending_.clear();+ std::size_t done = 0;+ int error = 0;+ const bool ran = AsRoot([&] {+ for (; done < items.size(); ++done)+ if (::fchmod(items[done].fd, items[done].mode) != 0) { error = errno; return false; }+ return true;+ });+ if (!ran && message_.empty()) {+ const auto& failed = items[std::min(done, items.size() - 1)];+ Fail(failed.relative, Console(failed.relative) + " can't be changed",+ error ? "chmod=" + Error(error) : options_.as_root ? "root=refused" : "root=unavailable");+ }+ for (auto& item : items) ::close(item.fd);+ }++ // Takes fd: checks the folder, queues its mode and lists it onto the stack.+ bool EnterFolder(int fd, const fs::path& relative, int depth) {+ struct stat info{};+ if (::fstat(fd, &info) != 0) { const int e = errno; ::close(fd); return Fail(relative, Console(relative) + " can't be read", "fstat=" + Error(e)); }+ if (!S_ISDIR(info.st_mode)) { ::close(fd); return Fail(relative, Console(relative) + " is not a folder", "type=not-folder"); }+ if (depth > kMaxDepth) { ::close(fd); return Fail(relative, "its folders go too deep", "depth=" + std::to_string(depth)); }+ ++result_.folders;+ if (!Change(fd, info, 0777, relative, true)) { ::close(fd); return false; }+ Frame frame;+ frame.fd = fd;+ frame.relative = relative;+ frame.depth = depth;+ const int listing_fd = ::dup(fd);+ DIR* listing = listing_fd >= 0 ? ::fdopendir(listing_fd) : nullptr;+ if (!listing) {+ const int e = errno;+ if (listing_fd >= 0) ::close(listing_fd);+ ::close(fd);+ return Fail(relative, Console(relative) + " can't be listed", "list=" + Error(e));+ }+ int error = 0;+ for (;;) {+ errno = 0;+ const auto* entry = ::readdir(listing);+ if (!entry) { error = errno; break; }+ const std::string name = entry->d_name;+ if (name == "." || name == "..") continue;+ if (++entries_ > kMaxEntries) { error = EFBIG; break; }+ frame.children.push_back({name, static_cast<std::uint64_t>(entry->d_fileno), entry->d_type});+ }+ ::closedir(listing);+ if (error) {+ ::close(fd);+ return error == EFBIG ? Fail(relative, "it has too many files", "entries>" + std::to_string(kMaxEntries))+ : Fail(relative, Console(relative) + " can't be listed", "readdir=" + Error(error));+ }+ stack_.push_back(std::move(frame));+ return true;+ }++ bool OpenEntry(const std::string& name) {+ const int parent = platform::OpenDirectoryWalk(Host(kDropFolder));+ if (parent < 0) {+ const int e = errno;+ if (Unsupported(e)) return Refuse({}, e);+ return Fail({}, std::string(kDropFolder) + " can't be opened", "open-parent=" + Error(e));+ }+ const int fd = Open(parent, {}, name, 0, kOpenFlags | O_DIRECTORY);+ const int error = errno;+ ::close(parent);+ if (fd < 0) {+ if (!message_.empty()) return false;+ if (IsLink(error) || error == ENOTDIR) return Fail({}, "the game's folder is a link or a file", "open=" + Error(error));+ return Fail({}, "the game's folder is missing", "open=" + Error(error));+ }+ return EnterFolder(fd, {}, 0);+ }++ // A link stays; one that leads outside the entry fails the walk.+ void CheckLink(const Frame& folder, const std::string& name) {+ ++result_.links;+ const auto relative = folder.relative / name;+#if defined(PLAYDEN_WALK_BY_PATH)+ // A title has neither readlinkat with a descriptor nor readlink (a libkernel_sys-only import,+ // null in the title): where the link leads can't be checked, so it fails like one that leaves.+ (void)folder;+ Fail(relative, "the link " + Console(relative) + " can't be checked on the PS5 (remove it over FTP)",+ "link=unchecked");+#else+ char target[4096];+ const auto length = ::readlinkat(folder.fd, name.c_str(), target, sizeof(target) - 1);+ if (length < 0) {+ const int e = errno;+ if (e == ENOENT) return;+ Fail(relative, "the link " + Console(relative) + " can't be read", "readlink=" + Error(e));+ return;+ }+ target[length] = '\0';+ const fs::path to(target);+ bool inside;+ if (to.is_absolute()) {+ const auto within = to.lexically_normal().lexically_relative(entry_);+ inside = !within.empty() && *within.begin() != "..";+ } else {+ const auto resolved = (folder.relative / to).lexically_normal();+ inside = resolved.empty() || (!resolved.is_absolute() && *resolved.begin() != "..");+ }+ if (!inside)+ Fail(relative, "the link " + Console(relative) + " leads outside the game's folder", "link-target=" + std::string(target));+#endif+ }++ void Visit(Frame& folder, const Child& child) {+ const auto relative = folder.relative / child.name;+ if (child.type == DT_LNK) return CheckLink(folder, child.name);+ if (child.type != DT_REG && child.type != DT_DIR && child.type != DT_UNKNOWN) return; // never opened+ const int fd = Open(folder.fd, relative, child.name, child.inode, kOpenFlags);+ if (fd < 0) {+ if (!message_.empty()) return;+ const int error = errno;+ if (IsLink(error)) return CheckLink(folder, child.name); // a listing that said DT_UNKNOWN+ if (error == ENOENT) return; // removed meanwhile+ Fail(relative, Console(relative) + " can't be opened", "open=" + Error(error));+ return;+ }+ struct stat info{};+ if (::fstat(fd, &info) != 0) {+ const int e = errno;+ ::close(fd);+ Fail(relative, Console(relative) + " can't be read", "fstat=" + Error(e));+ return;+ }+ if (S_ISDIR(info.st_mode)) {+ // `folder` is invalid once a frame is pushed.+ EnterFolder(fd, relative, folder.depth + 1);+ return;+ }+ if (!S_ISREG(info.st_mode)) { ::close(fd); return; }+ ++result_.files;+ if (info.st_nlink != 1) {+ // Another name has the same content: left alone, as the shared profile does.+ ++result_.hard_linked;+ Note("drop folder access skips " + Console(relative) + ": it has " + std::to_string(info.st_nlink) + " links");+ ::close(fd);+ return;+ }+ const mode_t wanted = 0666 | (info.st_mode & 0111);+ const bool queued = (info.st_mode & 07777) != wanted && info.st_uid != app_;+ if (!Change(fd, info, wanted, relative, false) || !queued) ::close(fd);+ }++ DropFolderAccessResult Finish() {+ result_.message = message_;+ if (message_.empty())+ Note("drop folder opened entry=" + entry_ + " folders=" + std::to_string(result_.folders) ++ " files=" + std::to_string(result_.files) + " changed=" + std::to_string(result_.changed) ++ " hard-linked=" + std::to_string(result_.hard_linked) + " links=" + std::to_string(result_.links) ++ " raises=" + std::to_string(result_.raises));+ return result_;+ }++ const std::string entry_;+ const DropFolderAccessOptions& options_;+ const uid_t app_;+ std::vector<Frame> stack_;+ std::vector<Pending> pending_;+ std::size_t entries_ = 0;+ std::string message_;+ DropFolderAccessResult result_;+};++} // namespace++bool IsDropFolderEntry(const std::string& console_path) {+ const fs::path path(console_path);+ if (console_path.find('\0') != std::string::npos || path.lexically_normal().string() != console_path) return false;+ return path.parent_path() == kDropFolder && PlainName(path.filename().string());+}++DropFolderAccessResult OpenDropFolderEntry(const std::string& entry, const DropFolderAccessOptions& options) {+ if (!IsDropFolderEntry(entry) || !options.namespace_root.is_absolute()) {+ DropFolderAccessResult refused;+ refused.message = "Playden only opens game folders directly inside " + std::string(kDropFolder) + ", not " + entry + ".";+ if (options.log) options.log("drop folder access refused entry=" + entry);+ return refused;+ }+ return Walk(entry, options).Run();+}++DropFolderOpener MakeDropFolderOpener(DropFolderAccessOptions options) {+ return [options = std::move(options)](const std::string& entry) { return OpenDropFolderEntry(entry, options).message; };+}++} // namespace playden::stores::localdiff --git a/src/stores/local/drop_folder_access.h b/src/stores/local/drop_folder_access.hnew file mode 100644index 0000000..fe7a49d--- /dev/null+++ b/src/stores/local/drop_folder_access.h@@ -0,0 +1,67 @@+#pragma once+// drop_folder_access.h - Opens the files of a game copied over FTP into the drop folder.+// Copyright (c) 2026 Playden contributors+// SPDX-License-Identifier: MIT+//+// FTP writes as root: a game in /data/playden-import/<entry> has root-owned folders (0755) and+// files (0644), which the app user can read but a game can't rewrite (a shipped .ini, a save+// next to the program). Root can't bypass permissions on the console, but it can chmod what it+// owns, so the walk opens the entry's folders to 0777 and its files to 0666 (an executable keeps+// its x bits). It never changes owners and never leaves the entry:+// - only /data/playden-import/<one folder name> is walked; any other path is refused;+// - every step goes through platform/directory_walk.h, which never follows a link; calls the+// console doesn't support (ENOSYS, EINVAL) refuse the walk, with no path fallback as root;+// - a link is left as it is, but one that leads outside the entry fails the walk, naming it. On+// the console, where a title can't read a link (readlink is a libkernel_sys-only import), every+// link fails the walk;+// - files with more than one link are left alone (their content may be another file's);+// - root is raised in short bursts (as_root: ps5::WithRoot, the process-wide root lock): only to+// open an entry the app user can't, and to chmod a batch of root-owned entries. seteuid is per+// process, so a file another thread creates while root is raised would belong to root.+// Resumable and idempotent: an interrupted walk is simply run again, and on an open tree it+// changes nothing and raises root not at all.+#include "runtime/writable_folders.h"++#include <cstddef>+#include <filesystem>+#include <functional>+#include <optional>+#include <string>+#include <sys/types.h>++namespace playden::stores::local {++inline constexpr const char* kDropFolder = "/data/playden-import";++struct DropFolderAccessOptions {+ runtime::WritableFoldersAsRoot as_root; // empty: root-owned entries can't be opened+ std::function<void(const std::string&)> log;+ // Prepended to the console paths walked (a host test's namespace); messages name console paths.+ std::filesystem::path namespace_root = "/";+ // Entries this user owns are changed without root. Default: the effective UID at the call.+ std::optional<uid_t> app_uid;+ // Root-owned entries changed in one raise.+ std::size_t batch = 64;+};++struct DropFolderAccessResult {+ bool ok = false;+ std::string message; // a sentence naming the folder, when not ok+ std::size_t folders = 0, files = 0;+ std::size_t changed = 0; // modes changed+ std::size_t hard_linked = 0; // files skipped: more than one link+ std::size_t links = 0; // links left as they are (all inside the entry)+ std::size_t raises = 0; // as_root calls+};++// Whether `console_path` is /data/playden-import/<name>, with one plain folder name.+bool IsDropFolderEntry(const std::string& console_path);++// Walks the entry (a console path, see IsDropFolderEntry). Run with no root held by this thread.+DropFolderAccessResult OpenDropFolderEntry(const std::string& entry, const DropFolderAccessOptions& options);++// OpenDropFolderEntry as a callback: the empty string, or the failure's sentence.+using DropFolderOpener = std::function<std::string(const std::string& entry)>;+DropFolderOpener MakeDropFolderOpener(DropFolderAccessOptions options);++} // namespace playden::stores::localdiff --git a/src/stores/local/import_store.cpp b/src/stores/local/import_store.cppnew file mode 100644index 0000000..6602904--- /dev/null+++ b/src/stores/local/import_store.cpp@@ -0,0 +1,484 @@+#include "stores/local/import_store.h"++#include "steam/backend/json.h"+#include "stores/local/scanner.h"++#include <algorithm>+#include <cstdio>+#include <ctime>+#include <fcntl.h>+#include <unistd.h>++namespace playden::stores::local {+namespace {++using JsonValue = steam::json::Value;++constexpr std::size_t kMaxTitleBytes = 512;+constexpr std::size_t kMaxPathBytes = 1024;+constexpr std::size_t kMaxFailureBytes = 1024;+constexpr std::size_t kMaxVolumeBytes = 32;++ImportResult<void> Failure(ImportError error, std::string message) {+ return ImportResult<void>::Failure(error, std::move(message));+}+template <typename T>+ImportResult<T> FailureOf(ImportError error, std::string message) {+ return ImportResult<T>::Failure(error, std::move(message));+}++std::string Escape(const std::string& value) {+ std::string output;+ output.reserve(value.size() + 2);+ for (const unsigned char c : value) {+ switch (c) {+ case '"': output += "\\\""; break;+ case '\\': output += "\\\\"; break;+ case '\b': output += "\\b"; break;+ case '\f': output += "\\f"; break;+ case '\n': output += "\\n"; break;+ case '\r': output += "\\r"; break;+ case '\t': output += "\\t"; break;+ default:+ if (c < 0x20) {+ static constexpr char kHex[] = "0123456789abcdef";+ output += "\\u00";+ output.push_back(kHex[c >> 4]);+ output.push_back(kHex[c & 15]);+ } else {+ output.push_back(static_cast<char>(c));+ }+ }+ }+ return output;+}++void Field(std::string& output, const char* name, const std::string& value, bool comma = true) {+ output += '"';+ output += name;+ output += "\":\"";+ output += Escape(value);+ output += '"';+ if (comma) output += ',';+}++const JsonValue* Required(const JsonValue& object, const char* key, JsonValue::Type type) {+ const auto* value = object.Find(key);+ return value && value->type() == type ? value : nullptr;+}++bool ValidVolume(const std::string& volume) {+ return !volume.empty() && volume.size() <= kMaxVolumeBytes &&+ std::all_of(volume.begin(), volume.end(), [](unsigned char c) {+ return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');+ });+}+bool ValidPath(const std::string& path) { return path.size() <= kMaxPathBytes && IsSafeRelativePath(path); }+bool ValidTitle(const std::string& title) {+ return !title.empty() && title.size() <= kMaxTitleBytes && title.find('\0') == std::string::npos;+}++std::optional<ImportState> ParseState(const std::string& key) {+ for (const auto state : {ImportState::kInPlace, ImportState::kCopyQueued, ImportState::kCopying, ImportState::kCopied})+ if (key == ImportStateKey(state)) return state;+ return std::nullopt;+}++std::optional<ImportEntry> ParseGame(const JsonValue& value) {+ if (!value.is_object()) return std::nullopt;+ const auto* id = Required(value, "id", JsonValue::Type::kString);+ const auto* title = Required(value, "title", JsonValue::Type::kString);+ const auto* volume = Required(value, "volume", JsonValue::Type::kString);+ const auto* folder = Required(value, "folder", JsonValue::Type::kString);+ const auto* program = Required(value, "program", JsonValue::Type::kString);+ const auto* state = Required(value, "state", JsonValue::Type::kString);+ const auto* added = Required(value, "added_at", JsonValue::Type::kNumber);+ if (!id || !title || !volume || !folder || !program || !state || !added) return std::nullopt;+ const auto parsed_state = ParseState(state->text());+ const auto added_at = added->AsUint64();+ if (!parsed_state || !added_at || !IsImportId(id->text()) || !ValidTitle(title->text()) ||+ !ValidVolume(volume->text()) || !ValidPath(folder->text()) || !ValidPath(program->text()))+ return std::nullopt;+ ImportEntry entry{id->text(), title->text(), volume->text(), folder->text(), program->text(), *parsed_state,+ *added_at, {}};+ if (const auto* failure = value.Find("failure")) {+ if (!failure->is_string() || failure->text().size() > kMaxFailureBytes) return std::nullopt;+ entry.failure = failure->text();+ }+ return entry;+}++std::optional<RemovedImport> ParseRemoved(const JsonValue& value) {+ if (!value.is_object()) return std::nullopt;+ const auto* id = Required(value, "id", JsonValue::Type::kString);+ const auto* volume = Required(value, "volume", JsonValue::Type::kString);+ const auto* folder = Required(value, "folder", JsonValue::Type::kString);+ const auto* program = Required(value, "program", JsonValue::Type::kString);+ if (!id || !volume || !folder || !program || !IsImportId(id->text()) || !ValidVolume(volume->text()) ||+ !ValidPath(folder->text()) || !ValidPath(program->text()))+ return std::nullopt;+ return RemovedImport{id->text(), volume->text(), folder->text(), program->text()};+}++bool SameFolder(const std::string& volume_a, const std::string& folder_a, const std::string& volume_b,+ const std::string& folder_b) {+ return volume_a == volume_b && folder_a == folder_b;+}++} // namespace++const char* ImportStateKey(ImportState state) {+ switch (state) {+ case ImportState::kInPlace: return "in_place";+ case ImportState::kCopyQueued: return "copy_queued";+ case ImportState::kCopying: return "copying";+ case ImportState::kCopied: return "copied";+ }+ return "in_place";+}++bool IsImportId(const std::string& id) {+ if (id.size() != 36) return false;+ for (std::size_t i = 0; i < id.size(); ++i) {+ const char c = id[i];+ if (i == 8 || i == 13 || i == 18 || i == 23) {+ if (c != '-') return false;+ } else if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) {+ return false;+ }+ }+ return true;+}++std::optional<std::string> RandomImportId() {+ // Not std::random_device: on the console it goes through libc arc4random, which may abort.+ unsigned char bytes[16];+ const int fd = ::open("/dev/urandom", O_RDONLY | O_CLOEXEC);+ if (fd < 0) return std::nullopt;+ std::size_t read_bytes = 0;+ while (read_bytes < sizeof(bytes)) {+ const auto count = ::read(fd, bytes + read_bytes, sizeof(bytes) - read_bytes);+ if (count <= 0) { ::close(fd); return std::nullopt; }+ read_bytes += static_cast<std::size_t>(count);+ }+ ::close(fd);+ bytes[6] = static_cast<unsigned char>((bytes[6] & 0x0f) | 0x40); // version 4+ bytes[8] = static_cast<unsigned char>((bytes[8] & 0x3f) | 0x80); // RFC 4122 variant+ char text[37];+ std::snprintf(text, sizeof(text), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",+ bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8], bytes[9],+ bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);+ return std::string(text);+}++ImportStore::ImportStore(Storage& storage, std::string relative_path, IdSource ids, Clock clock)+ : storage_(storage), relative_path_(std::move(relative_path)), ids_(std::move(ids)), clock_(std::move(clock)) {+ if (!ids_) ids_ = RandomImportId;+ if (!clock_) clock_ = [] {+ const auto now = std::time(nullptr);+ return now > 0 ? static_cast<std::uint64_t>(now) : 0;+ };+}++ImportResult<void> ImportStore::Load() {+ std::lock_guard<std::mutex> lock(mutex_);+ loaded_ = false;+ const auto read = storage_.ReadDurable(relative_path_, kMaxFileBytes);+ if (!read.ok()) {+ if (read.error() == StorageError::kNotFound) {+ games_.clear();+ removed_.clear();+ loaded_ = true;+ return ImportResult<void>::Success();+ }+ return Failure(read.error() == StorageError::kReadFailed ? ImportError::kBounds : ImportError::kIo,+ "imports could not be read");+ }+ const std::string text(read.value().begin(), read.value().end());+ steam::json::Limits limits;+ limits.max_bytes = kMaxFileBytes;+ limits.max_depth = 4;+ limits.max_values = (kMaxGames + kMaxRemoved) * 24 + 16;+ limits.max_string_bytes = kMaxPathBytes;+ const auto root = steam::json::Parse(text, limits);+ if (!root || !root->is_object()) return Failure(ImportError::kMalformed, "imports are malformed");+ const auto* version = Required(*root, "version", JsonValue::Type::kNumber);+ const auto version_number = version ? version->AsUint64() : std::nullopt;+ if (!version_number) return Failure(ImportError::kMalformed, "imports version is malformed");+ if (*version_number != kVersion) return Failure(ImportError::kUnsupportedVersion, "imports version is unsupported");+ const auto* games = Required(*root, "games", JsonValue::Type::kArray);+ const auto* removed = Required(*root, "removed", JsonValue::Type::kArray);+ if (!games || !removed || games->array().size() > kMaxGames || removed->array().size() > kMaxRemoved)+ return Failure(ImportError::kMalformed, "imports entries are malformed");+ std::vector<ImportEntry> next_games;+ std::vector<RemovedImport> next_removed;+ const auto id_used = [&](const std::string& id) {+ return std::any_of(next_games.begin(), next_games.end(), [&](const auto& e) { return e.id == id; }) ||+ std::any_of(next_removed.begin(), next_removed.end(), [&](const auto& e) { return e.id == id; });+ };+ for (const auto& value : games->array()) {+ auto entry = ParseGame(value);+ if (!entry || id_used(entry->id) ||+ std::any_of(next_games.begin(), next_games.end(), [&](const auto& other) {+ return SameFolder(other.volume, other.folder, entry->volume, entry->folder);+ }))+ return Failure(ImportError::kMalformed, "an imported game is invalid");+ next_games.push_back(std::move(*entry));+ }+ for (const auto& value : removed->array()) {+ auto entry = ParseRemoved(value);+ if (!entry || id_used(entry->id)) return Failure(ImportError::kMalformed, "a removed imported game is invalid");+ next_removed.push_back(std::move(*entry));+ }+ games_ = std::move(next_games);+ removed_ = std::move(next_removed);+ loaded_ = true;+ return ImportResult<void>::Success();+}++ImportResult<void> ImportStore::Persist() {+ if (games_.size() > kMaxGames || removed_.size() > kMaxRemoved)+ return Failure(ImportError::kBounds, "imports exceed their entry limit");+ std::string output = "{\"version\":1,\"games\":[";+ for (std::size_t i = 0; i < games_.size(); ++i) {+ const auto& game = games_[i];+ if (i) output += ',';+ output += '{';+ Field(output, "id", game.id);+ Field(output, "title", game.title);+ Field(output, "volume", game.volume);+ Field(output, "folder", game.folder);+ Field(output, "program", game.program);+ Field(output, "state", ImportStateKey(game.state));+ if (!game.failure.empty()) Field(output, "failure", game.failure);+ output += "\"added_at\":" + std::to_string(game.added_at) + '}';+ }+ output += "],\"removed\":[";+ for (std::size_t i = 0; i < removed_.size(); ++i) {+ const auto& entry = removed_[i];+ if (i) output += ',';+ output += '{';+ Field(output, "id", entry.id);+ Field(output, "volume", entry.volume);+ Field(output, "folder", entry.folder);+ Field(output, "program", entry.program, false);+ output += '}';+ }+ output += "]}";+ if (output.size() > kMaxFileBytes) return Failure(ImportError::kBounds, "imports exceed their size limit");+ const auto written = storage_.WriteDurable(relative_path_, Bytes(output.begin(), output.end()));+ if (!written.ok()) {+ // The file may or may not hold the change: reload before anything else.+ loaded_ = false;+ return Failure(ImportError::kIo, "imports could not be written");+ }+ return ImportResult<void>::Success();+}++ImportResult<void> ImportStore::Save() {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return Failure(ImportError::kNotLoaded, "imports are not loaded");+ return Persist();+}++bool ImportStore::loaded() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return loaded_;+}++std::vector<ImportEntry> ImportStore::Games() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return loaded_ ? games_ : std::vector<ImportEntry>{};+}++std::vector<RemovedImport> ImportStore::Removed() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return loaded_ ? removed_ : std::vector<RemovedImport>{};+}++std::optional<ImportEntry> ImportStore::Find(const std::string& id) const {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return std::nullopt;+ for (const auto& game : games_)+ if (game.id == id) return game;+ return std::nullopt;+}++std::optional<ImportEntry> ImportStore::FindByFolder(const std::string& volume, const std::string& folder) const {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return std::nullopt;+ for (const auto& game : games_)+ if (SameFolder(game.volume, game.folder, volume, folder)) return game;+ return std::nullopt;+}++std::optional<RemovedImport> ImportStore::FindRemoved(const std::string& volume, const std::string& folder) const {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return std::nullopt;+ for (const auto& entry : removed_)+ if (SameFolder(entry.volume, entry.folder, volume, folder)) return entry;+ return std::nullopt;+}++ImportEntry* ImportStore::Mutable(const std::string& id) {+ for (auto& game : games_)+ if (game.id == id) return &game;+ return nullptr;+}++void ImportStore::MoveToRemoved(std::size_t index) {+ const auto game = games_[index];+ games_.erase(games_.begin() + static_cast<std::ptrdiff_t>(index));+ removed_.erase(std::remove_if(removed_.begin(), removed_.end(),+ [&](const auto& entry) {+ return entry.id == game.id ||+ SameFolder(entry.volume, entry.folder, game.volume, game.folder);+ }),+ removed_.end());+ removed_.push_back({game.id, game.volume, game.folder, game.program});+ while (removed_.size() > kMaxRemoved) removed_.erase(removed_.begin());+}++ImportResult<ImportEntry> ImportStore::Add(const std::string& volume, const std::string& folder,+ const std::string& title, const std::string& program) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return FailureOf<ImportEntry>(ImportError::kNotLoaded, "imports are not loaded");+ if (!ValidVolume(volume) || !ValidPath(folder) || !ValidPath(program) || !ValidTitle(title))+ return FailureOf<ImportEntry>(ImportError::kInvalid, "the game's folder, program or title is invalid");+ for (const auto& game : games_)+ if (SameFolder(game.volume, game.folder, volume, folder))+ return FailureOf<ImportEntry>(ImportError::kDuplicate, "this folder is already in the library");+ if (games_.size() >= kMaxGames) return FailureOf<ImportEntry>(ImportError::kBounds, "too many imported games");+ const auto removed = std::find_if(removed_.begin(), removed_.end(), [&](const auto& entry) {+ return SameFolder(entry.volume, entry.folder, volume, folder);+ });+ std::string id;+ if (removed != removed_.end()) {+ id = removed->id;+ } else {+ for (int attempt = 0; attempt < 4 && id.empty(); ++attempt) {+ const auto made = ids_();+ if (!made || !IsImportId(*made)) break;+ const bool used = Mutable(*made) || std::any_of(removed_.begin(), removed_.end(),+ [&](const auto& entry) { return entry.id == *made; });+ if (!used) id = *made;+ }+ if (id.empty()) return FailureOf<ImportEntry>(ImportError::kNoRandom, "no ID could be made for the game");+ }+ const auto previous_games = games_;+ const auto previous_removed = removed_;+ if (removed != removed_.end()) removed_.erase(removed);+ ImportEntry entry{id, title, volume, folder, program, ImportState::kInPlace, clock_(), {}};+ games_.push_back(entry);+ const auto persisted = Persist();+ if (!persisted.ok()) {+ games_ = previous_games;+ removed_ = previous_removed;+ return FailureOf<ImportEntry>(persisted.error(), persisted.message());+ }+ return ImportResult<ImportEntry>::Success(entry);+}++ImportResult<ImportEntry> ImportStore::SetProgram(const std::string& id, const std::string& program) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return FailureOf<ImportEntry>(ImportError::kNotLoaded, "imports are not loaded");+ if (!ValidPath(program)) return FailureOf<ImportEntry>(ImportError::kInvalid, "the program path is invalid");+ auto* game = Mutable(id);+ if (!game) return FailureOf<ImportEntry>(ImportError::kNotFound, "the game is not in the library");+ const auto previous = *game;+ game->program = program;+ const auto persisted = Persist();+ if (!persisted.ok()) {+ *game = previous;+ return FailureOf<ImportEntry>(persisted.error(), persisted.message());+ }+ return ImportResult<ImportEntry>::Success(*game);+}++ImportResult<ImportEntry> ImportStore::Transition(const std::string& id, ImportState from, ImportState to) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return FailureOf<ImportEntry>(ImportError::kNotLoaded, "imports are not loaded");+ auto* game = Mutable(id);+ if (!game) return FailureOf<ImportEntry>(ImportError::kNotFound, "the game is not in the library");+ if (game->state != from)+ return FailureOf<ImportEntry>(ImportError::kWrongState, std::string("the game is ") + ImportStateKey(game->state));+ const auto previous = *game;+ game->state = to;+ if (to == ImportState::kCopyQueued || to == ImportState::kCopied) game->failure.clear();+ const auto persisted = Persist();+ if (!persisted.ok()) {+ *game = previous;+ return FailureOf<ImportEntry>(persisted.error(), persisted.message());+ }+ return ImportResult<ImportEntry>::Success(*game);+}++ImportResult<ImportEntry> ImportStore::QueueCopy(const std::string& id) {+ return Transition(id, ImportState::kInPlace, ImportState::kCopyQueued);+}+ImportResult<ImportEntry> ImportStore::StartCopy(const std::string& id) {+ return Transition(id, ImportState::kCopyQueued, ImportState::kCopying);+}+ImportResult<ImportEntry> ImportStore::FinishCopy(const std::string& id) {+ return Transition(id, ImportState::kCopying, ImportState::kCopied);+}++ImportResult<ImportEntry> ImportStore::CancelCopy(const std::string& id, const std::string& failure) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return FailureOf<ImportEntry>(ImportError::kNotLoaded, "imports are not loaded");+ auto* found = Mutable(id);+ if (!found) return FailureOf<ImportEntry>(ImportError::kNotFound, "the game is not in the library");+ if (found->state != ImportState::kCopyQueued && found->state != ImportState::kCopying)+ return FailureOf<ImportEntry>(ImportError::kWrongState, std::string("the game is ") + ImportStateKey(found->state));+ const auto previous = games_;+ // The drive folder, and a drop-folder game's folder, are still there: the game plays in place again.+ found->state = ImportState::kInPlace;+ found->failure = failure.substr(0, kMaxFailureBytes);+ const auto kept = *found;+ const auto persisted = Persist();+ if (!persisted.ok()) {+ games_ = previous;+ return FailureOf<ImportEntry>(persisted.error(), persisted.message());+ }+ return ImportResult<ImportEntry>::Success(kept);+}++ImportResult<void> ImportStore::Remove(const std::string& id) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return Failure(ImportError::kNotLoaded, "imports are not loaded");+ const auto found = std::find_if(games_.begin(), games_.end(), [&](const auto& game) { return game.id == id; });+ if (found == games_.end()) return Failure(ImportError::kNotFound, "the game is not in the library");+ if (found->state == ImportState::kCopied)+ return Failure(ImportError::kWrongState, "uninstall the game's copy before removing it");+ if (found->state != ImportState::kInPlace)+ return Failure(ImportError::kWrongState, "cancel the game's copy before removing it");+ const auto previous_games = games_;+ const auto previous_removed = removed_;+ MoveToRemoved(static_cast<std::size_t>(found - games_.begin()));+ const auto persisted = Persist();+ if (!persisted.ok()) {+ games_ = previous_games;+ removed_ = previous_removed;+ }+ return persisted;+}++ImportResult<void> ImportStore::Uninstalled(const std::string& id) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!loaded_) return Failure(ImportError::kNotLoaded, "imports are not loaded");+ const auto found = std::find_if(games_.begin(), games_.end(), [&](const auto& game) { return game.id == id; });+ if (found == games_.end()) return Failure(ImportError::kNotFound, "the game is not in the library");+ if (found->state != ImportState::kCopied)+ return Failure(ImportError::kWrongState, std::string("the game is ") + ImportStateKey(found->state));+ const auto previous_games = games_;+ const auto previous_removed = removed_;+ MoveToRemoved(static_cast<std::size_t>(found - games_.begin()));+ const auto persisted = Persist();+ if (!persisted.ok()) {+ games_ = previous_games;+ removed_ = previous_removed;+ }+ return persisted;+}++} // namespace playden::stores::localdiff --git a/src/stores/local/import_store.h b/src/stores/local/import_store.hnew file mode 100644index 0000000..2ac13f4--- /dev/null+++ b/src/stores/local/import_store.h@@ -0,0 +1,171 @@+#pragma once+// imports.json: the Windows games the player added from a drive or the drop folder, and the ones+// they removed (so adding the same folder again brings back its ID, bottle and settings).+//+// A game is named by `volume` (an install volume ID: internal, m2, extended, usb0, usb1) and+// `folder`, relative to that volume's mount (scanner.h ImportRoots): "playden/import/Delores" on+// usb0 is /mnt/usb0/playden/import/Delores; the drop folder's games are volume "internal",+// folder "playden-import/<name>" (/data/playden-import/<name>). The store never touches the game+// files; callers check the folder and program with the scanner before adding.+#include "platform/storage.h"++#include <cstddef>+#include <cstdint>+#include <functional>+#include <mutex>+#include <optional>+#include <string>+#include <utility>+#include <vector>++namespace playden::stores::local {++enum class ImportState {+ kInPlace, // plays from the drive folder+ kCopyQueued, // a copy into Playden waits; still plays from the drive folder if it did+ kCopying,+ kCopied, // plays from <games_root>/local-<id>+};+const char* ImportStateKey(ImportState state);++struct ImportEntry {+ std::string id; // lower-case random UUID+ std::string title;+ std::string volume;+ std::string folder;+ std::string program; // relative to the folder, '/' separators+ ImportState state = ImportState::kInPlace;+ std::uint64_t added_at = 0; // seconds since the epoch+ std::string failure; // the last copy's failure, empty when none+};++struct RemovedImport {+ std::string id;+ std::string volume;+ std::string folder;+ std::string program;+};++enum class ImportError {+ kNone,+ kNotLoaded,+ kIo,+ kMalformed,+ kUnsupportedVersion,+ kBounds,+ kInvalid, // a field the store refuses (volume, folder, program, title)+ kDuplicate, // the folder is already in the library+ kNotFound,+ kWrongState, // the transition isn't in the state table+ kNoRandom, // no ID could be made+};++template <typename T>+class ImportResult {+ public:+ static ImportResult Success(T value) { return ImportResult(std::move(value), ImportError::kNone, {}); }+ static ImportResult Failure(ImportError error, std::string message) {+ return ImportResult(std::nullopt, error, std::move(message));+ }+ bool ok() const { return value_.has_value(); }+ const T& value() const { return *value_; }+ ImportError error() const { return error_; }+ const std::string& message() const { return message_; }++ private:+ ImportResult(std::optional<T> value, ImportError error, std::string message)+ : value_(std::move(value)), error_(error), message_(std::move(message)) {}+ std::optional<T> value_;+ ImportError error_;+ std::string message_;+};++template <>+class ImportResult<void> {+ public:+ static ImportResult Success() { return ImportResult(ImportError::kNone, {}); }+ static ImportResult Failure(ImportError error, std::string message) { return ImportResult(error, std::move(message)); }+ bool ok() const { return error_ == ImportError::kNone; }+ ImportError error() const { return error_; }+ const std::string& message() const { return message_; }++ private:+ ImportResult(ImportError error, std::string message) : error_(error), message_(std::move(message)) {}+ ImportError error_;+ std::string message_;+};++// Lower-case canonical UUID text (8-4-4-4-12 hex digits).+bool IsImportId(const std::string& id);+// A random (version 4) UUID from /dev/urandom, or nothing.+std::optional<std::string> RandomImportId();++class ImportStore {+ public:+ static constexpr std::uint32_t kVersion = 1;+ static constexpr std::size_t kMaxFileBytes = 256 * 1024;+ static constexpr std::size_t kMaxGames = 256;+ static constexpr std::size_t kMaxRemoved = 256; // the oldest removed entry goes first+ static constexpr const char* kDropFolderVolume = "internal";++ using IdSource = std::function<std::optional<std::string>()>;+ using Clock = std::function<std::uint64_t()>;++ // `storage` is the private runtime state folder (the one holding installs.json); writes are+ // durable private writes, which hold the effective-UID lock root raises use. Empty `ids` and+ // `clock` use RandomImportId and the system time.+ explicit ImportStore(Storage& storage, std::string relative_path = "imports.json", IdSource ids = {},+ Clock clock = {});++ // A missing file loads empty. A damaged or newer file fails and leaves the store unloaded: every+ // change is then refused (kNotLoaded), so the file is never replaced by an empty one.+ ImportResult<void> Load();+ ImportResult<void> Save();+ bool loaded() const;++ std::vector<ImportEntry> Games() const; // in the order they were added+ std::vector<RemovedImport> Removed() const;+ std::optional<ImportEntry> Find(const std::string& id) const;+ std::optional<ImportEntry> FindByFolder(const std::string& volume, const std::string& folder) const;+ std::optional<RemovedImport> FindRemoved(const std::string& volume, const std::string& folder) const;++ // Adds a folder, playing in place. A folder on the removed list gets its old ID back; one already+ // in the library is refused (kDuplicate).+ ImportResult<ImportEntry> Add(const std::string& volume, const std::string& folder, const std::string& title,+ const std::string& program);+ // The program Game settings chose (a program check precedes it).+ ImportResult<ImportEntry> SetProgram(const std::string& id, const std::string& program);++ // State table. Every call refuses (kWrongState) a move the table doesn't have, and writes the file.+ // in_place -> copy_queued (clears the last failure).+ ImportResult<ImportEntry> QueueCopy(const std::string& id);+ // copy_queued -> copying.+ ImportResult<ImportEntry> StartCopy(const std::string& id);+ // copying -> copied.+ ImportResult<ImportEntry> FinishCopy(const std::string& id);+ // copy_queued/copying -> in_place, keeping `failure` (empty for a plain cancel). A drop-folder+ // game too: its folder stays, and it plays in place again.+ ImportResult<ImportEntry> CancelCopy(const std::string& id, const std::string& failure = {});+ // in_place -> removed list (its files stay). A copied game must be uninstalled first; a copy in+ // progress must be cancelled first.+ ImportResult<void> Remove(const std::string& id);+ // copied -> removed list, once its files in Playden are deleted (the bottle stays like any kept data).+ ImportResult<void> Uninstalled(const std::string& id);++ private:+ ImportResult<void> Persist();+ ImportEntry* Mutable(const std::string& id);+ void MoveToRemoved(std::size_t index);+ ImportResult<ImportEntry> Transition(const std::string& id, ImportState from, ImportState to);++ Storage& storage_;+ std::string relative_path_;+ IdSource ids_;+ Clock clock_;+ mutable std::mutex mutex_;+ bool loaded_ = false;+ std::vector<ImportEntry> games_;+ std::vector<RemovedImport> removed_;+};++} // namespace playden::stores::localdiff --git a/src/stores/local/library_source.cpp b/src/stores/local/library_source.cppnew file mode 100644index 0000000..abc43ba--- /dev/null+++ b/src/stores/local/library_source.cpp@@ -0,0 +1,564 @@+#include "stores/local/library_source.h"++#include <cerrno>+#include <sys/stat.h>++#include <algorithm>+#include <utility>++namespace playden::stores::local {+namespace {++std::string Join(const std::string& base, const std::string& relative) {+ if (relative.empty()) return base;+ if (base.empty() || base == "/") return "/" + relative;+ return base + "/" + relative;+}++bool IsFolder(const std::string& path) {+ struct stat info {};+ return stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);+}++// The same rule the install volumes use: a drive's folder counts only while a file system other+// than its parent's is mounted there.+bool IsMountPoint(const std::string& path) {+ struct stat mount {}, parent {};+ const auto slash = path.find_last_of('/');+ if (slash == std::string::npos) return false;+ const auto parent_path = slash == 0 ? std::string("/") : path.substr(0, slash);+ if (lstat(path.c_str(), &mount) != 0 || !S_ISDIR(mount.st_mode) || stat(parent_path.c_str(), &parent) != 0)+ return false;+ return mount.st_dev != parent.st_dev;+}++const char* ProgramWords(ProgramCheck check) {+ switch (check) {+ case ProgramCheck::kOk: return "";+ case ProgramCheck::kInvalidPath: return "The program's path is not inside the game's folder.";+ case ProgramCheck::kMissing: return "The program is not in the game's folder.";+ case ProgramCheck::kNotRegularFile: return "The program is not a file.";+ case ProgramCheck::kUnsupported: return "The game's folder can't be read on this console.";+ case ProgramCheck::kFailed: return "The game's folder could not be read.";+ }+ return "The program could not be checked.";+}++const char* ImportWords(ImportError error) {+ switch (error) {+ case ImportError::kDuplicate: return "This folder is already in the library.";+ case ImportError::kBounds: return "The library has as many imported games as it can hold.";+ case ImportError::kInvalid: return "The game's folder, program or name can't be used.";+ case ImportError::kNotFound: return "The game is not in the library.";+ case ImportError::kWrongState: return "Cancel the game's copy first.";+ case ImportError::kNotLoaded: return "Playden's list of imported games could not be read.";+ default: return "Playden's list of imported games could not be saved.";+ }+}++} // namespace++std::vector<ImportVolume> ConsoleImportVolumes() {+ std::vector<ImportVolume> volumes;+ for (const auto& root : ImportRoots())+ volumes.push_back({root.volume, root.mount, root.folder, std::string(root.volume) != ImportStore::kDropFolderVolume});+ return volumes;+}++LocalLibrarySource::LocalLibrarySource(ImportStore& store, LocalLibraryConfig config)+ : store_(store), config_(std::move(config)) {+ // Imports load, the drives are checked, and the import folders are scanned.+ Job first;+ first.command = Command::kScan;+ first.request = ++request_;+ pending_.push_back(std::move(first));+ scan_waiting_ = true;+ auto snapshot = std::make_shared<LocalSnapshot>();+ snapshot->scanning = true;+ snapshot_ = std::move(snapshot);+ worker_ = std::thread([this] { Run(); });+}++LocalLibrarySource::~LocalLibrarySource() { Shutdown(); }++void LocalLibrarySource::Shutdown() {+ {+ std::lock_guard<std::mutex> lock(mutex_);+ stopping_ = true;+ }+ wake_.notify_all();+ if (worker_.joinable()) worker_.join();+}++std::uint64_t LocalLibrarySource::Submit(Job job) {+ std::uint64_t request = 0;+ {+ std::lock_guard<std::mutex> lock(mutex_);+ if (stopping_ || pending_.size() >= 64) return 0;+ request = job.request = ++request_;+ pending_.push_back(std::move(job));+ }+ wake_.notify_one();+ return request;+}++std::uint64_t LocalLibrarySource::Refresh(std::uint64_t account, std::uint64_t generation) {+ Job job;+ job.command = Command::kList;+ job.account = account;+ job.generation = generation;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::Clear(std::uint64_t account, std::uint64_t generation) {+ Job job;+ job.command = Command::kClear;+ job.account = account;+ job.generation = generation;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::LookForGames() {+ std::uint64_t request = 0;+ {+ std::lock_guard<std::mutex> lock(mutex_);+ if (stopping_) return 0;+ // A look that hasn't started yet answers this one too.+ if (scan_waiting_)+ for (const auto& job : pending_)+ if (job.command == Command::kScan) return job.request;+ if (pending_.size() >= 64) return 0;+ Job job;+ job.command = Command::kScan;+ request = job.request = ++request_;+ pending_.push_back(std::move(job));+ scan_waiting_ = true; // with the job, so the worker can't take it first+ auto next = std::make_shared<LocalSnapshot>(*snapshot_);+ next->scanning = true;+ ++next->revision;+ snapshot_ = std::move(next);+ }+ wake_.notify_one();+ return request;+}++void LocalLibrarySource::CheckVolumes() {+ {+ std::lock_guard<std::mutex> lock(mutex_);+ if (check_waiting_ || scan_waiting_) return;+ check_waiting_ = true;+ }+ Job job;+ job.command = Command::kCheckVolumes;+ if (!Submit(std::move(job))) {+ std::lock_guard<std::mutex> lock(mutex_);+ check_waiting_ = false;+ }+}++std::uint64_t LocalLibrarySource::Add(const std::string& volume, const std::string& folder, const std::string& program) {+ Job job;+ job.command = Command::kAdd;+ job.volume = volume;+ job.folder = folder;+ job.program = program;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::Remove(const std::string& id) {+ Job job;+ job.command = Command::kRemove;+ job.id = id;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::OpenForPlay(const std::string& id) {+ Job job;+ job.command = Command::kOpen;+ job.id = id;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::CopyIntoPlayden(const std::string& id, const std::string& volume_id,+ std::uint32_t runtime_number) {+ Job job;+ job.command = Command::kCopy;+ job.id = id;+ job.volume = volume_id;+ job.runtime_number = runtime_number;+ return Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::MoveIntoPlayden(const std::string& id, std::uint32_t runtime_number) {+ Job job;+ job.command = Command::kMove;+ job.id = id;+ job.runtime_number = runtime_number;+ return Submit(std::move(job));+}++void LocalLibrarySource::Reload() {+ Job job;+ job.command = Command::kReload;+ Submit(std::move(job));+}++std::uint64_t LocalLibrarySource::Reconcile(std::function<installs::InstallSnapshot()> installs,+ std::function<void(const GameId&)> cancel_job,+ std::function<std::uint32_t(const GameId&)> runtime_number) {+ if (!installs) return 0;+ Job job;+ job.command = Command::kReconcile;+ job.installs = std::move(installs);+ job.cancel_job = std::move(cancel_job);+ job.number_for = std::move(runtime_number);+ return Submit(std::move(job));+}++std::vector<LibraryEvent> LocalLibrarySource::Pump(std::size_t max_events) {+ std::lock_guard<std::mutex> lock(mutex_);+ std::vector<LibraryEvent> result;+ while (!events_.empty() && result.size() < max_events) {+ result.push_back(std::move(events_.front()));+ events_.pop_front();+ }+ return result;+}++std::shared_ptr<const LocalSnapshot> LocalLibrarySource::Snapshot() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return snapshot_;+}++std::vector<installs::InstalledLocation> LocalLibrarySource::Locations() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return snapshot_->locations;+}++void LocalLibrarySource::Run() {+ for (;;) {+ Job job;+ {+ std::unique_lock<std::mutex> lock(mutex_);+ wake_.wait(lock, [this] { return stopping_ || !pending_.empty(); });+ if (stopping_) return;+ // A scan can take a while on a full drive: library loads, adds and removes go first.+ auto next = std::find_if(pending_.begin(), pending_.end(), [](const Job& waiting) {+ return waiting.command != Command::kScan && waiting.command != Command::kCheckVolumes;+ });+ if (next == pending_.end()) next = pending_.begin();+ job = std::move(*next);+ pending_.erase(next);+ if (job.command == Command::kScan) scan_waiting_ = false;+ if (job.command == Command::kCheckVolumes) check_waiting_ = false;+ }+ Execute(job);+ }+}++void LocalLibrarySource::EnsureLoaded() {+ if (store_.loaded() || load_tried_) return;+ load_tried_ = true;+ const auto loaded = store_.Load();+ load_error_ = loaded.ok() ? std::string{} : loaded.message();+ if (!loaded.ok()) Log("imports load failed: " + loaded.message());+ ++games_revision_;+}++void LocalLibrarySource::Execute(const Job& job) {+ EnsureLoaded();+ switch (job.command) {+ case Command::kList:+ case Command::kClear: {+ LibraryEvent event;+ event.request_id = job.request;+ event.account_id = job.account;+ event.account_generation = job.generation;+ if (job.command == Command::kClear) {+ event.kind = LibraryEventKind::kCleared;+ } else if (!store_.loaded()) {+ event.kind = LibraryEventKind::kFailure;+ event.error = steam::LibraryError::kPersistence;+ event.cache_error = StorageError::kInvalidData;+ } else {+ event.kind = LibraryEventKind::kComplete;+ for (const auto& game : store_.Games()) {+ GameSummary summary;+ summary.id = GameId::Local(game.id);+ summary.title = game.title;+ if (game.added_at) summary.added_at_unix_seconds = game.added_at;+ event.games.push_back(std::move(summary));+ }+ event.cache_available = true;+ }+ std::lock_guard<std::mutex> lock(mutex_);+ events_.push_back(std::move(event));+ return;+ }+ case Command::kScan:+ // The games and the drives are published before the folders are walked.+ CheckPresence();+ Project();+ Scan();+ Project();+ return;+ case Command::kCheckVolumes: {+ const bool changed = CheckPresence();+ if (changed) Scan();+ Project();+ return;+ }+ case Command::kAdd: {+ const auto* volume = Volume(job.volume);+ if (!volume) return Outcome(job, false, {}, "Playden doesn't know the drive \"" + job.volume + "\".");+ if (CheckPresence()) Scan();+ if (!present_.count(volume->volume)) return Outcome(job, false, {}, "The game's drive is not connected.");+ if (!IsSafeRelativePath(job.folder)) return Outcome(job, false, {}, "The game's folder can't be used.");+ // A bare name is a folder in the drive's import folder.+ const auto folder = job.folder.find('/') == std::string::npos ? Join(volume->folder, job.folder) : job.folder;+ const auto path = Join(volume->mount, folder);+ // Opened first: a root-owned folder FTP made may not even be listable by the app user.+ if (const auto opened = OpenDropFolder(*volume, path); !opened.empty()) return Outcome(job, false, {}, opened);+ const auto check = CheckProgram(HostPath(path), job.program);+ if (check != ProgramCheck::kOk) return Outcome(job, false, {}, ProgramWords(check));+ std::string title;+ for (const auto& candidate : candidates_)+ if (candidate.volume == job.volume && candidate.folder == folder) title = candidate.title;+ if (title.empty()) title = TitleForFolder(path);+ const auto added = store_.Add(job.volume, folder, title, job.program);+ if (!added.ok()) {+ Log("import add failed volume=" + job.volume + " folder=" + folder + ": " + added.message());+ return Outcome(job, false, {}, ImportWords(added.error()));+ }+ Log("import added id=" + added.value().id + " volume=" + job.volume + " folder=" + folder ++ " program=" + job.program);+ for (auto& candidate : candidates_)+ if (candidate.volume == job.volume && candidate.folder == folder) candidate.imported_id = added.value().id;+ ++games_revision_;+ return Outcome(job, true, added.value().id, {});+ }+ case Command::kOpen: {+ const auto game = store_.Find(job.id);+ const auto* volume = game ? Volume(game->volume) : nullptr;+ // Only a game playing from the drop folder has files to open.+ if (!game || !volume || game->state == ImportState::kCopied) return Outcome(job, true, job.id, {});+ const auto opened = OpenDropFolder(*volume, Join(volume->mount, game->folder));+ return Outcome(job, opened.empty(), job.id, opened);+ }+ case Command::kCopy: {+ const auto game = store_.Find(job.id);+ const auto* volume = game ? Volume(game->volume) : nullptr;+ if (!game) return Outcome(job, false, job.id, ImportWords(ImportError::kNotFound));+ if (!config_.copy_into_playden) return Outcome(job, false, job.id, "Playden can't copy games here.");+ if (game->state == ImportState::kCopied) return Outcome(job, false, job.id, "The game is already in Playden.");+ if (game->state != ImportState::kInPlace) return Outcome(job, false, job.id, "The game is already being copied.");+ if (CheckPresence()) Scan();+ if (!volume || !present_.count(volume->volume)) return Outcome(job, false, job.id, "The game's drive is not connected.");+ const auto path = Join(volume->mount, game->folder);+ // A drop-folder game's files may be root-owned (FTP): opened first, as before a Play.+ if (const auto opened = OpenDropFolder(*volume, path); !opened.empty()) return Outcome(job, false, job.id, opened);+ const auto failure = config_.copy_into_playden(*game, HostPath(path), job.volume, job.runtime_number);+ if (!failure.empty()) {+ Log("import copy not queued id=" + job.id + ": " + failure);+ return Outcome(job, false, job.id, failure);+ }+ return Outcome(job, true, job.id, {});+ }+ case Command::kMove: {+ const auto game = store_.Find(job.id);+ const auto* volume = game ? Volume(game->volume) : nullptr;+ if (!game) return Outcome(job, false, job.id, ImportWords(ImportError::kNotFound));+ if (!config_.move_into_playden) return Outcome(job, false, job.id, "Playden can't move games here.");+ if (game->state == ImportState::kCopied) return Outcome(job, false, job.id, "The game is already in Playden.");+ if (game->state == ImportState::kCopyQueued) return Outcome(job, false, job.id, "The game is already being copied.");+ if (!volume || game->volume != ImportStore::kDropFolderVolume)+ return Outcome(job, false, job.id, "Only games in the drop folder can be moved. Copy it instead.");+ const auto path = Join(volume->mount, game->folder);+ // A move that stopped after its rename (copying) is finished as it is; a new one opens the+ // files first, as before a Play (FTP may have left root-owned ones).+ if (game->state == ImportState::kInPlace)+ if (const auto opened = OpenDropFolder(*volume, path); !opened.empty()) return Outcome(job, false, job.id, opened);+ const auto failure = config_.move_into_playden(*game, HostPath(path), job.runtime_number);+ ++games_revision_;+ Project();+ if (!failure.empty()) {+ Log("import move not done id=" + job.id + ": " + failure);+ return Outcome(job, false, job.id, failure);+ }+ return Outcome(job, true, job.id, {});+ }+ case Command::kReload:+ ++games_revision_;+ Project();+ return;+ case Command::kReconcile: {+ if (!store_.loaded()) return Outcome(job, false, {}, ImportWords(ImportError::kNotLoaded));+ const auto service = job.installs();+ bool changed = false;+ const auto installed = [&](const GameId& id) {+ return std::any_of(service.installed.begin(), service.installed.end(),+ [&](const installs::InstalledGame& game) { return game.id == id; });+ };+ const auto queued = [&](const GameId& id) {+ return std::any_of(service.jobs.begin(), service.jobs.end(),+ [&](const installs::InstallJobView& view) { return view.id == id; });+ };+ for (const auto& game : store_.Games()) {+ if (game.state != ImportState::kCopyQueued && game.state != ImportState::kCopying) continue;+ const auto id = GameId::Local(game.id);+ if (installed(id)) {+ // The copy committed its install record; Playden stopped before imports.json said so.+ if (game.state == ImportState::kCopyQueued) store_.StartCopy(game.id);+ const auto finished = store_.FinishCopy(game.id);+ Log("import copy reconciled id=" + game.id + (finished.ok() ? " copied" : " failed: " + finished.message()));+ changed = true;+ } else if (!queued(id)) {+ // A move renames the drop folder away: one whose rename happened is finished, never put+ // back in place (its folder is in Playden now).+ const auto* volume = Volume(game.volume);+ if (volume && game.volume == ImportStore::kDropFolderVolume && config_.move_into_playden) {+ struct stat info {};+ const auto source = HostPath(Join(volume->mount, game.folder));+ if (::lstat(source.c_str(), &info) != 0 && errno == ENOENT) {+ const auto number = job.number_for ? job.number_for(id) : 0u;+ const auto failure = config_.move_into_playden(game, source, number);+ Log("import move reconciled id=" + game.id + (failure.empty() ? " copied" : ": " + failure));+ changed = true;+ continue;+ }+ }+ store_.CancelCopy(game.id, "The copy stopped. Copy it again.");+ Log("import copy reconciled id=" + game.id + " in_place (no copy job)");+ changed = true;+ }+ }+ // A job left from a copy that failed or was put back in place (its entry moved first).+ for (const auto& view : service.jobs) {+ if (view.id.store != Store::kLocal || !job.cancel_job) continue;+ const auto game = store_.Find(view.id.value);+ if (game && game->state != ImportState::kInPlace) continue;+ Log("import copy job cancelled id=" + view.id.value + " (not being copied)");+ job.cancel_job(view.id);+ }+ if (changed) ++games_revision_;+ return Outcome(job, true, {}, {});+ }+ case Command::kRemove: {+ const auto removed = store_.Remove(job.id);+ if (!removed.ok()) {+ Log("import remove failed id=" + job.id + ": " + removed.message());+ return Outcome(job, false, job.id, ImportWords(removed.error()));+ }+ Log("import removed id=" + job.id);+ for (auto& candidate : candidates_)+ if (candidate.imported_id == job.id) candidate.imported_id.clear();+ ++games_revision_;+ return Outcome(job, true, job.id, {});+ }+ }+}++void LocalLibrarySource::Outcome(const Job& job, bool ok, std::string id, std::string message) {+ outcomes_.push_back({job.request, ok, std::move(id), std::move(message)});+ if (outcomes_.size() > kMaxOutcomes) outcomes_.erase(outcomes_.begin());+ Project();+}++bool LocalLibrarySource::CheckPresence() {+ std::set<std::string> present;+ for (const auto& volume : config_.volumes) {+ const bool here = config_.present ? config_.present(volume)+ : volume.require_mount_point ? IsMountPoint(HostPath(volume.mount)) : IsFolder(HostPath(volume.mount));+ if (here) present.insert(volume.volume);+ }+ if (present == present_) return false;+ std::string line = "import volumes present=";+ for (const auto& volume : present) line += volume + ",";+ Log(line);+ present_ = std::move(present);+ return true;+}++void LocalLibrarySource::Scan() {+ std::vector<ImportCandidate> candidates;+ for (const auto& volume : config_.volumes) {+ if (!present_.count(volume.volume)) continue;+ const auto root = Join(volume.mount, volume.folder);+ const auto listed = ScanImportRoot(HostPath(root));+ if (listed.status != ScanStatus::kOk) {+ Log("import scan root=" + root + " failed errno=" + std::to_string(listed.error));+ continue;+ }+ for (const auto& name : listed.folders) {+ ImportCandidate candidate;+ candidate.volume = volume.volume;+ candidate.folder = Join(volume.folder, name);+ candidate.path = Join(root, name);+ const auto scan = ScanGameFolder(HostPath(candidate.path));+ candidate.status = scan.status;+ candidate.title = scan.title.empty() ? TitleForFolder(candidate.path) : scan.title;+ candidate.programs = scan.programs;+ candidate.total_bytes = scan.total_bytes;+ candidate.file_count = scan.file_count;+ candidate.ascii_ok = IsPrintableAscii(candidate.path);+ candidate.partial = scan.limit_reached || scan.deeper_folders;+ if (const auto known = store_.FindByFolder(candidate.volume, candidate.folder)) candidate.imported_id = known->id;+ candidates.push_back(std::move(candidate));+ }+ }+ Log("import scan candidates=" + std::to_string(candidates.size()));+ candidates_ = std::move(candidates);+}++void LocalLibrarySource::Project() {+ auto next = std::make_shared<LocalSnapshot>();+ next->loaded = store_.loaded();+ next->load_error = load_error_;+ next->games = store_.Games();+ next->present_volumes = present_;+ next->candidates = candidates_;+ next->outcomes = outcomes_;+ next->games_revision = games_revision_;+ for (const auto& game : next->games) {+ // A game copied into Playden plays from Playden's own folder (a managed install).+ if (game.state == ImportState::kCopied) continue;+ const auto* volume = Volume(game.volume);+ if (!volume) continue;+ installs::InstalledLocation location;+ location.game = GameId::Local(game.id);+ location.root = Join(volume->mount, game.folder);+ location.volume_id = game.volume;+ location.ownership = installs::InstalledLocation::Ownership::kExternal;+ location.available = present_.count(game.volume) && IsFolder(HostPath(location.root));+ if (location.available && !IsPrintableAscii(location.root)) {+ location.available = false;+ location.reason = kNotAsciiReason;+ }+ next->locations.push_back(std::move(location));+ }+ std::lock_guard<std::mutex> lock(mutex_);+ next->scanning = scan_waiting_;+ next->revision = snapshot_->revision + 1;+ snapshot_ = std::move(next);+}++const ImportVolume* LocalLibrarySource::Volume(const std::string& id) const {+ for (const auto& volume : config_.volumes)+ if (volume.volume == id) return &volume;+ return nullptr;+}++std::string LocalLibrarySource::HostPath(const std::string& console_path) const {+ return config_.host_prefix.empty() ? console_path : config_.host_prefix + console_path;+}++std::string LocalLibrarySource::OpenDropFolder(const ImportVolume& volume, const std::string& path) {+ if (volume.volume != ImportStore::kDropFolderVolume || !config_.open_drop_folder) return {};+ return config_.open_drop_folder(path);+}++void LocalLibrarySource::Log(const std::string& line) const {+ if (config_.log) config_.log(line);+}++} // namespace playden::stores::localdiff --git a/src/stores/local/library_source.h b/src/stores/local/library_source.hnew file mode 100644index 0000000..f6a6cfc--- /dev/null+++ b/src/stores/local/library_source.h@@ -0,0 +1,216 @@+#pragma once+// The Local store: Windows games the player added from a drive folder or the drop folder+// (imports.json), and the folders on the attached drives that could be added (candidates). A+// worker owns every file access: imports.json, the import folder scans and the drive checks; the+// presentation thread reads snapshots.+#include "installs/installed_location.h"+#include "stores/library_source.h"+#include "stores/local/drop_folder_access.h"+#include "stores/local/import_store.h"+#include "stores/local/scanner.h"++#include <condition_variable>+#include <cstddef>+#include <cstdint>+#include <deque>+#include <functional>+#include <memory>+#include <mutex>+#include <set>+#include <string>+#include <thread>+#include <vector>++namespace playden::stores::local {++// Where games to import are looked for on one install volume: `folder` under `mount`.+struct ImportVolume {+ std::string volume; // install volume id ("internal", "usb0", ...)+ std::string mount; // console path of the drive ("/mnt/usb0"; the drop folder's is "/data")+ std::string folder; // the import folder under it ("playden/import", "playden-import")+ // An external drive: there only while something is mounted on `mount` (the console keeps+ // empty /mnt/usb* folders without a drive).+ bool require_mount_point = false;+};+// ImportRoots() as volumes: the drop folder, then the external drives.+std::vector<ImportVolume> ConsoleImportVolumes();++// A folder in an import folder that could be added.+struct ImportCandidate {+ std::string volume;+ std::string folder; // relative to the volume's mount, as imports.json names it+ std::string path; // console path+ std::string title;+ std::vector<Program> programs; // ranked: the likely game first; helpers flagged+ std::uint64_t total_bytes = 0; // estimate (scanner.h)+ std::uint64_t file_count = 0;+ bool ascii_ok = false; // printable-ASCII path: can play in place+ bool partial = false; // the scan stopped at a limit or a depth+ ScanStatus status = ScanStatus::kOk;+ std::string imported_id; // already in the library as this game; empty when not+};++// The outcome of an Add or Remove.+struct ImportOutcome {+ std::uint64_t request = 0;+ bool ok = false;+ std::string id; // the game's import ID+ std::string message; // a sentence for the player or the log when it failed+};++struct LocalSnapshot {+ std::uint64_t revision = 0; // anything below changed+ std::uint64_t games_revision = 0; // the imported games changed: the library reloads them+ bool loaded = false;+ std::string load_error; // imports.json could not be read+ std::vector<ImportEntry> games;+ std::set<std::string> present_volumes;+ std::vector<ImportCandidate> candidates;+ bool scanning = false; // a look for games is queued or running+ // Where each game playing in place is, for the controller (external locations).+ std::vector<installs::InstalledLocation> locations;+ std::vector<ImportOutcome> outcomes; // the latest ones, oldest first (at most kMaxOutcomes)+};++struct LocalLibraryConfig {+ std::vector<ImportVolume> volumes = ConsoleImportVolumes();+ // Prepended to every path the worker opens (a host test's namespace); never to the paths it reports.+ std::string host_prefix;+ // Whether a volume's drive is there now; empty: its mount is a folder (and a mount point when required).+ std::function<bool(const ImportVolume&)> present;+ std::function<void(const std::string&)> log;+ // Opens the files of a drop-folder game (drop_folder_access.h) with the console path of its+ // folder, at Add and before each Play; the empty string or a sentence naming the folder. Empty:+ // drop-folder games play as they are (host builds, whose files are the user's own).+ DropFolderOpener open_drop_folder;+ // Queues "Copy into Playden" for an in_place entry from `source` (its folder, as this process+ // opens it) to an install volume (empty: the default); LocalCopyRunner::Enqueue. The empty+ // string, or a sentence for the player. Empty: games can't be copied.+ std::function<std::string(const ImportEntry& entry, const std::string& source, const std::string& volume_id,+ std::uint32_t runtime_number)>+ copy_into_playden;+ // "Move into Playden" of a drop-folder entry from `source` (its folder, as this process opens it);+ // LocalCopyRunner::Move. Blocks the worker for the rename and its PS5 title's relocation. The+ // empty string, or a sentence for the player. Empty: games can't be moved.+ std::function<std::string(const ImportEntry& entry, const std::string& source, std::uint32_t runtime_number)>+ move_into_playden;+};++// The words for a game that can't play from its folder because of its name (the descriptor rule).+inline constexpr const char* kNotAsciiReason =+ "The game's folder name has characters the PS5 can't play from. Copy the game into Playden instead.";++class LocalLibrarySource final : public LibrarySource {+ public:+ static constexpr std::size_t kMaxOutcomes = 16;++ // `store` outlives the source. The worker starts at once: it loads imports.json, checks the+ // drives and looks for games.+ LocalLibrarySource(ImportStore& store, LocalLibraryConfig config = {});+ ~LocalLibrarySource() override;+ LocalLibrarySource(const LocalLibrarySource&) = delete;+ LocalLibrarySource& operator=(const LocalLibrarySource&) = delete;++ Store store() const override { return Store::kLocal; }+ // The imported games (a game on an absent drive stays listed).+ std::uint64_t Refresh(std::uint64_t account, std::uint64_t generation) override;+ std::uint64_t LoadCached(std::uint64_t account, std::uint64_t generation) override { return Refresh(account, generation); }+ // Imported games belong to the console, not to an account: nothing is deleted.+ std::uint64_t Clear(std::uint64_t account, std::uint64_t generation) override;+ void Invalidate(std::uint64_t) override {}+ void Shutdown() override;+ std::vector<LibraryEvent> Pump(std::size_t max_events) override;++ // "Look for games": the drives are checked and every import folder is scanned again.+ std::uint64_t LookForGames();+ // The drives are checked; when one came or went, the import folders are scanned again. A check+ // already waiting makes this a no-op.+ void CheckVolumes();+ // Adds a folder to the library, playing in place with `program` (relative to the folder). `folder`+ // is relative to the volume's mount, or a bare name in the volume's import folder ("Delores" on+ // usb0 is playden/import/Delores). The title comes from the folder's scan.+ std::uint64_t Add(const std::string& volume, const std::string& folder, const std::string& program);+ // Takes the game out of the library; its files stay where they are.+ std::uint64_t Remove(const std::string& id);+ // Before a Play or Prepare: a drop-folder game's files are opened again (FTP may have added+ // root-owned ones, or an earlier walk stopped). The outcome says whether the game may go on;+ // a game anywhere else is ok at once.+ std::uint64_t OpenForPlay(const std::string& id);+ // "Copy into Playden": the game's folder is planned and its copy queued on `volume_id` (empty:+ // the install service's default). `runtime_number` (the runtime's number for the game, 0 when+ // unknown) goes into the copy's install record; a game already set up to play from its folder+ // keeps its PS5 title, which follows the copy once it is verified. The outcome says whether it+ // was queued.+ std::uint64_t CopyIntoPlayden(const std::string& id, const std::string& volume_id = {},+ std::uint32_t runtime_number = 0);+ // "Move into Playden": a drop-folder game's folder is renamed into Playden's games folder (its+ // PS5 title, if any, follows). The outcome says whether it moved; a move a restart or a running+ // title interrupted after the rename is finished by asking again.+ std::uint64_t MoveIntoPlayden(const std::string& id, std::uint32_t runtime_number = 0);+ // imports.json changed outside the source (the copy runner): the games are read again.+ void Reload();+ // After a restart, against the install service (`installs` and `cancel_job` are called on the+ // worker, in order with copies being queued): a copy that finished as a managed install but whose+ // entry still says copy_queued/copying becomes copied; one the service has neither as a job nor+ // as an install goes back to in_place, unless it is a move whose rename happened (its drop+ // folder is gone): that one is finished (move_into_playden, with `runtime_number`'s number); and+ // a copy job whose game isn't being copied (back in place, or gone) is cancelled, which removes+ // its partial folder.+ std::uint64_t Reconcile(std::function<installs::InstallSnapshot()> installs,+ std::function<void(const GameId&)> cancel_job,+ std::function<std::uint32_t(const GameId&)> runtime_number = {});++ std::shared_ptr<const LocalSnapshot> Snapshot() const;+ // Cheap: a copy of the last projection.+ std::vector<installs::InstalledLocation> Locations() const;++ private:+ enum class Command { kList, kClear, kScan, kCheckVolumes, kAdd, kRemove, kOpen, kCopy, kMove, kReload, kReconcile };+ struct Job {+ Command command = Command::kList;+ std::uint64_t request = 0;+ std::uint64_t account = 0;+ std::uint64_t generation = 0;+ std::string volume, folder, program, id;+ std::uint32_t runtime_number = 0;+ std::function<installs::InstallSnapshot()> installs; // kReconcile+ std::function<void(const GameId&)> cancel_job;+ std::function<std::uint32_t(const GameId&)> number_for;+ };+ std::uint64_t Submit(Job job);+ void Run();+ void Execute(const Job& job);+ void EnsureLoaded();+ bool CheckPresence(); // true when the present volumes changed+ void Scan();+ void Project();+ void Outcome(const Job& job, bool ok, std::string id, std::string message);+ const ImportVolume* Volume(const std::string& id) const;+ std::string HostPath(const std::string& console_path) const;+ // The empty string, or why the drop-folder game at `path` (a console path) can't be played.+ std::string OpenDropFolder(const ImportVolume& volume, const std::string& path);+ void Log(const std::string& line) const;++ ImportStore& store_;+ LocalLibraryConfig config_;+ // Worker-only.+ bool load_tried_ = false;+ std::string load_error_;+ std::set<std::string> present_;+ std::vector<ImportCandidate> candidates_;+ std::vector<ImportOutcome> outcomes_;+ std::uint64_t games_revision_ = 0;++ mutable std::mutex mutex_;+ std::condition_variable wake_;+ std::deque<Job> pending_;+ std::deque<LibraryEvent> events_;+ std::shared_ptr<const LocalSnapshot> snapshot_;+ std::uint64_t request_ = 0;+ bool scan_waiting_ = false;+ bool check_waiting_ = false;+ bool stopping_ = false;+ std::thread worker_;+};++} // namespace playden::stores::localdiff --git a/src/stores/local/scanner.cpp b/src/stores/local/scanner.cppnew file mode 100644index 0000000..fbafcd8--- /dev/null+++ b/src/stores/local/scanner.cpp@@ -0,0 +1,339 @@+#include "stores/local/scanner.h"++#include "platform/directory_walk.h"++#include <algorithm>+#include <cerrno>+#include <dirent.h>+#include <fcntl.h>+#include <sys/stat.h>+#include <unistd.h>++namespace playden::stores::local {+namespace {++constexpr int kOpenAny = O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC;+constexpr int kOpenFolder = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;++char Lower(char c) { return c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c; }++std::string Lowercase(std::string text) {+ for (auto& c : text) c = Lower(c);+ return text;+}++// Lower-case ASCII letters only; other bytes (digits, spaces, UTF-8) are dropped.+std::string Letters(const std::string& text) {+ std::string letters;+ for (const char c : text) {+ const char lower = Lower(c);+ if (lower >= 'a' && lower <= 'z') letters.push_back(lower);+ }+ return letters;+}++std::vector<std::string> Split(const std::string& path) {+ std::vector<std::string> parts;+ std::size_t start = 0;+ for (;;) {+ const auto slash = path.find('/', start);+ parts.push_back(path.substr(start, slash == std::string::npos ? std::string::npos : slash - start));+ if (slash == std::string::npos) break;+ start = slash + 1;+ }+ return parts;+}++std::string Trimmed(std::string folder) {+ while (folder.size() > 1 && folder.back() == '/') folder.pop_back();+ return folder;+}++bool EndsWithExe(const std::string& name) {+ return name.size() > 4 && Lowercase(name.substr(name.size() - 4)) == ".exe";+}++bool Unsupported(int error) { return error == ENOSYS || error == EINVAL; }++bool IsImportRootFolder(const std::vector<std::string>& parts, std::size_t parent) {+ if (parts[parent] == "playden-import") return true;+ return parts[parent] == "import" && parent > 0 && parts[parent - 1] == "playden";+}++// One open folder of the walk: its descriptor, its listing and where it is in the game folder.+struct Frame {+ int fd = -1;+ DIR* listing = nullptr;+ std::string relative; // empty for the game folder itself+ int level = 0; // its children are level + 1 components deep+};++void Close(Frame& frame) {+ if (frame.listing) ::closedir(frame.listing);+ if (frame.fd >= 0) ::close(frame.fd);+ frame.listing = nullptr;+ frame.fd = -1;+}++// Takes `fd`. False (errno set) when the folder can't be listed.+bool OpenFrame(int fd, std::string relative, int level, Frame* frame) {+ const int listing_fd = ::dup(fd);+ DIR* listing = listing_fd >= 0 ? ::fdopendir(listing_fd) : nullptr;+ if (!listing) {+ const int error = errno;+ if (listing_fd >= 0) ::close(listing_fd);+ ::close(fd);+ errno = error;+ return false;+ }+ *frame = Frame{fd, listing, std::move(relative), level};+ return true;+}++ScanStatus StatusForOpen(int error) {+ if (Unsupported(error)) return ScanStatus::kUnsupported;+ if (error == ENOENT) return ScanStatus::kMissing;+ if (error == ENOTDIR) return ScanStatus::kNotFolder;+ return ScanStatus::kFailed;+}++} // namespace++bool IsPrintableAscii(const std::string& text) {+ return std::all_of(text.begin(), text.end(), [](unsigned char c) { return c >= 0x20 && c < 0x7f; });+}++bool IsSafeRelativePath(const std::string& path) {+ if (path.empty() || path.size() > 4096 || path.front() == '/') return false;+ if (path.find('\\') != std::string::npos || path.find('\0') != std::string::npos) return false;+ for (const auto& part : Split(path))+ if (part.empty() || part == "." || part == "..") return false;+ return true;+}++bool IsLikelyHelper(const std::string& relative_path) {+ static const char* const kHelperNames[] = {+ "unins", "setup", "install", "vcredist", "vc_redist", "dxsetup", "dxwebsetup", "directx", "dotnet", "ndp",+ "physx", "oalinst", "crash", "unitycrashhandler", "bugreport", "errorreport", "reporter", "redist", "ue4prereq",+ "easyanticheat", "battleye", "uninstall", "update", "patch", "config", "settings", "dxsdk"};+ static const char* const kHelperFolders[] = {+ "_redist", "redist", "_commonredist", "commonredist", "redistributables", "__installer", "directx",+ "support", "dotnet", "vcredist", "_installer", "installers", "easyanticheat", "battleye"};+ auto parts = Split(Lowercase(relative_path));+ parts.erase(std::remove(parts.begin(), parts.end(), std::string()), parts.end());+ if (parts.empty()) return false;+ const auto& name = parts.back();+ for (const auto* prefix : kHelperNames)+ if (name.compare(0, std::char_traits<char>::length(prefix), prefix) == 0) return true;+ for (std::size_t i = 0; i + 1 < parts.size(); ++i)+ for (const auto* folder : kHelperFolders)+ if (parts[i] == folder) return true;+ return false;+}++std::string TitleForFolder(const std::string& folder) {+ static const char* const kGeneric[] = {"game", "games", "bin", "bin32", "bin64", "x64", "x86",+ "win32", "win64", "binaries", "app", "files", "data"};+ auto parts = Split(Trimmed(folder));+ parts.erase(std::remove(parts.begin(), parts.end(), std::string()), parts.end());+ if (parts.empty()) return {};+ const auto& name = parts.back();+ const auto lower = Lowercase(name);+ const bool generic = std::any_of(std::begin(kGeneric), std::end(kGeneric),+ [&](const char* word) { return lower == word; });+ if (!generic || parts.size() < 2 || IsImportRootFolder(parts, parts.size() - 2)) return name;+ return parts[parts.size() - 2];+}++void RankPrograms(std::vector<Program>& programs, const std::string& title) {+ const auto wanted = Letters(title);+ const auto matches = [&](const Program& program) {+ auto name = program.path.substr(program.path.find_last_of('/') + 1);+ const auto dot = name.find_last_of('.');+ if (dot != std::string::npos && dot > 0) name.erase(dot);+ const auto letters = Letters(name);+ return !wanted.empty() && !letters.empty() &&+ (letters.find(wanted) != std::string::npos || wanted.find(letters) != std::string::npos);+ };+ const auto depth = [](const Program& program) {+ return std::count(program.path.begin(), program.path.end(), '/');+ };+ std::stable_sort(programs.begin(), programs.end(), [&](const Program& a, const Program& b) {+ if (a.helper != b.helper) return !a.helper;+ const bool match_a = matches(a), match_b = matches(b);+ if (match_a != match_b) return match_a;+ const auto depth_a = depth(a), depth_b = depth(b);+ if (depth_a != depth_b) return depth_a < depth_b;+ if (a.bytes != b.bytes) return a.bytes > b.bytes;+ return a.path < b.path;+ });+}++FolderScan ScanGameFolder(const std::string& folder_path) {+ FolderScan scan;+ const auto folder = Trimmed(folder_path);+ scan.title = TitleForFolder(folder);+ scan.printable_path = IsPrintableAscii(folder);+ const int root = platform::OpenDirectoryWalk(folder);+ if (root < 0) {+ scan.error = errno;+ scan.status = StatusForOpen(scan.error);+ return scan;+ }+ // The walk keeps one frame per level (at most kMaxProgramDepth): bounded descriptors and stack.+ std::vector<Frame> stack;+ stack.reserve(kMaxProgramDepth + 1);+ Frame first;+ if (!OpenFrame(root, {}, 0, &first)) {+ scan.error = errno;+ scan.status = StatusForOpen(scan.error);+ return scan;+ }+ stack.push_back(std::move(first));+ std::size_t entries = 0;+ ScanStatus status = ScanStatus::kOk;+ while (!stack.empty() && status == ScanStatus::kOk && !scan.limit_reached) {+ Frame& frame = stack.back();+ errno = 0;+ const auto* entry = ::readdir(frame.listing);+ if (!entry) {+ if (errno != 0) { scan.error = errno; status = ScanStatus::kFailed; break; }+ Close(frame);+ stack.pop_back();+ continue;+ }+ const std::string name = entry->d_name;+ if (name == "." || name == "..") continue;+ if (++entries > kMaxScanEntries) { scan.limit_reached = true; break; }+ if (name.front() == '.') continue;+ if (entry->d_type == DT_LNK) continue;+ const auto listed = static_cast<std::uint64_t>(entry->d_fileno);+ const int child = platform::OpenWalkChild(frame.fd, name.c_str(), kOpenAny, listed);+ if (child < 0) {+ const int error = errno;+ if (Unsupported(error)) { scan.error = error; status = ScanStatus::kUnsupported; break; }+ // ELOOP/EMLINK: a link whose listing said DT_UNKNOWN (exFAT does).+ if (error != ELOOP && error != EMLINK) ++scan.unreadable;+ continue;+ }+ struct stat info{};+ if (::fstat(child, &info) != 0) { ::close(child); ++scan.unreadable; continue; }+ const auto relative = frame.relative.empty() ? name : frame.relative + "/" + name;+ if (S_ISDIR(info.st_mode)) {+ if (frame.level + 1 >= kMaxProgramDepth) {+ // Its entries would be deeper than programs are looked for.+ ::close(child);+ scan.deeper_folders = true;+ continue;+ }+ Frame next;+ const int level = frame.level + 1;+ if (!OpenFrame(child, relative, level, &next)) { ++scan.unreadable; continue; }+ stack.push_back(std::move(next)); // `frame` is not used after this+ continue;+ }+ ::close(child);+ if (!S_ISREG(info.st_mode)) continue;+ const auto bytes = info.st_size > 0 ? static_cast<std::uint64_t>(info.st_size) : 0;+ scan.total_bytes += bytes;+ ++scan.file_count;+ if (!EndsWithExe(name)) continue;+ if (scan.programs.size() >= kMaxScanPrograms) { scan.limit_reached = true; break; }+ scan.programs.push_back(Program{relative, bytes, IsLikelyHelper(relative), IsPrintableAscii(relative)});+ }+ for (auto& frame : stack) Close(frame);+ scan.status = status;+ if (status != ScanStatus::kOk) {+ scan.programs.clear();+ return scan;+ }+ RankPrograms(scan.programs, scan.title);+ return scan;+}++ProgramCheck CheckProgram(const std::string& folder, const std::string& program) {+ if (!IsSafeRelativePath(program)) return ProgramCheck::kInvalidPath;+ int fd = platform::OpenDirectoryWalk(Trimmed(folder));+ if (fd < 0) {+ if (Unsupported(errno)) return ProgramCheck::kUnsupported;+ return errno == ENOENT || errno == ENOTDIR ? ProgramCheck::kMissing : ProgramCheck::kFailed;+ }+ const auto parts = Split(program);+ for (std::size_t i = 0; i < parts.size(); ++i) {+ const bool last = i + 1 == parts.size();+ const int child = platform::OpenWalkChild(fd, parts[i].c_str(), last ? kOpenAny : kOpenFolder);+ const int error = errno;+ ::close(fd);+ if (child < 0) {+ if (Unsupported(error)) return ProgramCheck::kUnsupported;+ if (error == ENOENT) return ProgramCheck::kMissing;+ // A link (ELOOP/EMLINK), or a file where a folder was expected.+ if (error == ELOOP || error == EMLINK || error == ENOTDIR) return ProgramCheck::kNotRegularFile;+ return ProgramCheck::kFailed;+ }+ fd = child;+ }+ struct stat info{};+ const bool regular = ::fstat(fd, &info) == 0 && S_ISREG(info.st_mode);+ ::close(fd);+ return regular ? ProgramCheck::kOk : ProgramCheck::kNotRegularFile;+}++const std::vector<ImportRoot>& ImportRoots() {+ static const std::vector<ImportRoot> roots = {+ {"internal", "/data", "playden-import"},+ {"m2", "/mnt/ext1", "playden/import"},+ {"extended", "/mnt/ext0", "playden/import"},+ {"usb0", "/mnt/usb0", "playden/import"},+ {"usb1", "/mnt/usb1", "playden/import"},+ };+ return roots;+}++CandidateScan ScanImportRoot(const std::string& root_path) {+ CandidateScan scan;+ const int root = platform::OpenDirectoryWalk(Trimmed(root_path));+ if (root < 0) {+ scan.error = errno;+ scan.status = StatusForOpen(scan.error);+ // No drive, or no import folder on it: nothing to offer.+ if (scan.status == ScanStatus::kMissing) { scan.status = ScanStatus::kOk; scan.error = 0; }+ return scan;+ }+ Frame frame;+ if (!OpenFrame(root, {}, 0, &frame)) {+ scan.error = errno;+ scan.status = StatusForOpen(scan.error);+ return scan;+ }+ std::size_t entries = 0;+ scan.status = ScanStatus::kOk;+ for (;;) {+ errno = 0;+ const auto* entry = ::readdir(frame.listing);+ if (!entry) {+ if (errno != 0) { scan.error = errno; scan.status = ScanStatus::kFailed; }+ break;+ }+ const std::string name = entry->d_name;+ if (name == "." || name == "..") continue;+ if (++entries > kMaxScanEntries) { scan.limit_reached = true; break; }+ if (name.front() == '.' || entry->d_type == DT_LNK) continue;+ if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) continue;+ const int child = platform::OpenWalkChild(frame.fd, name.c_str(), kOpenFolder,+ static_cast<std::uint64_t>(entry->d_fileno));+ if (child < 0) {+ if (Unsupported(errno)) { scan.error = errno; scan.status = ScanStatus::kUnsupported; break; }+ continue; // a link, a file, or gone+ }+ ::close(child);+ if (scan.folders.size() >= kMaxImportCandidates) { scan.limit_reached = true; break; }+ scan.folders.push_back(name);+ }+ Close(frame);+ if (scan.status != ScanStatus::kOk) scan.folders.clear();+ std::sort(scan.folders.begin(), scan.folders.end());+ return scan;+}++} // namespace playden::stores::localdiff --git a/src/stores/local/scanner.h b/src/stores/local/scanner.hnew file mode 100644index 0000000..57c04c3--- /dev/null+++ b/src/stores/local/scanner.h@@ -0,0 +1,94 @@+#pragma once+// Finds the programs in a Windows game folder the player copied to a drive (port of the macOS+// app's LocalWindowsGame). Every walk goes through platform/directory_walk.h: a PS5 title can't+// open relative to a descriptor, and a walk the platform can't do is refused, never redone with+// plain path operations.+#include <cstddef>+#include <cstdint>+#include <string>+#include <vector>++namespace playden::stores::local {++// Programs are looked for this many components deep: "a/b/c/game.exe" is the deepest.+inline constexpr int kMaxProgramDepth = 4;+// Entries listed (any kind, hidden ones included) and programs found before a scan stops.+inline constexpr std::size_t kMaxScanEntries = 20000;+inline constexpr std::size_t kMaxScanPrograms = 512;+// Folders an import root lists before its scan stops.+inline constexpr std::size_t kMaxImportCandidates = 512;++enum class ScanStatus {+ kOk,+ kMissing, // the folder (or the drive) isn't there+ kNotFolder,+ kUnsupported, // the platform refused a descriptor walk (ENOSYS/EINVAL): nothing was read+ kFailed, // a folder could not be listed, or a link sits in the folder's own path+};++struct Program {+ std::string path; // inside the game folder, '/' separators+ std::uint64_t bytes = 0;+ // Installers, uninstallers, redistributables, crash handlers and tools: shown as+ // "Installer or tool" and ranked last.+ bool helper = false;+ bool printable = true; // printable ASCII (in-place eligibility also needs this)+};++struct FolderScan {+ ScanStatus status = ScanStatus::kFailed;+ int error = 0; // errno of the failure+ std::string title;+ std::vector<Program> programs; // ranked: the likely game first+ // Regular files the walk saw (hidden files and links left out). Display estimates only: they+ // cover the first kMaxProgramDepth levels and stop at the limits; the copy plan has the real totals.+ std::uint64_t total_bytes = 0;+ std::uint64_t file_count = 0;+ bool deeper_folders = false; // folders below kMaxProgramDepth were not walked+ // The folder's console path is printable ASCII: only then can it play in place (the+ // descriptor rule); otherwise it can only be copied or moved into Playden.+ bool printable_path = false;+ bool limit_reached = false; // kMaxScanEntries or kMaxScanPrograms: the lists are partial+ std::size_t unreadable = 0; // entries that could not be opened and were skipped+};++// Scans an absolute console path such as /mnt/usb0/playden/import/Delores.+FolderScan ScanGameFolder(const std::string& folder);++// The folder's name, or its parent's when the name is generic ("game", "bin", "x64", ...), unless+// the parent is an import root (playden/import, playden-import) or the file system root.+std::string TitleForFolder(const std::string& folder);+bool IsLikelyHelper(const std::string& relative_path);+// Non-helpers first; then names matching the title (ASCII letters only, lower case, either+// contains the other); then shallower; then larger; then by path (byte order).+void RankPrograms(std::vector<Program>& programs, const std::string& title);+bool IsPrintableAscii(const std::string& text);++// A path inside a folder: relative, '/' separators, no empty, "." or ".." parts, no backslash or NUL.+bool IsSafeRelativePath(const std::string& path);++enum class ProgramCheck { kOk, kInvalidPath, kMissing, kNotRegularFile, kUnsupported, kFailed };+// Before saving or launching: the path is safe, and walking it from the folder without following+// links reaches a regular file.+ProgramCheck CheckProgram(const std::string& folder, const std::string& program);++// Where games to import are looked for. `folder` is relative to `mount`; entries in imports.json+// name a game by `volume` and its folder relative to the same mount (the drop folder:+// volume "internal", folder "playden-import/<name>"). Volume IDs are the install volumes'.+struct ImportRoot {+ const char* volume;+ const char* mount;+ const char* folder;+};+const std::vector<ImportRoot>& ImportRoots();++struct CandidateScan {+ ScanStatus status = ScanStatus::kFailed; // a missing root is kOk with no folders+ int error = 0;+ std::vector<std::string> folders; // names of the direct child folders, sorted+ bool limit_reached = false;+};+// Direct child folders of an import root; hidden entries, links and files are left out.+CandidateScan ScanImportRoot(const std::string& root);++} // namespace playden::stores::localdiff --git a/tests/homebrew_installed_locations_test.cpp b/tests/homebrew_installed_locations_test.cppindex 00d390e..9cc2bed 100644--- a/tests/homebrew_installed_locations_test.cpp+++ b/tests/homebrew_installed_locations_test.cpp@@ -2,10 +2,12 @@// Task 2.5) and never reaches Steam Cloud (Task 2.6), against a real RuntimeHost.#include "homebrew/controller.hpp"#include "homebrew/runtime_host.hpp"+#include "stores/local/library_source.h"#include "fake_library_source.h"#include "platform/managed_directory.h"#include "platform/storage.h"#include "runtime/layout_migration.h"+#include "runtime/settings_storage.h"#include "runtime/artifact_hash.h"#include <algorithm>@@ -41,6 +43,11 @@ void Write(const fs::path& path, const std::string& bytes, unsigned mode) {std::ofstream(path, std::ios::binary | std::ios::trunc) << bytes;CHECK(chmod(path.c_str(), mode) == 0);}+unsigned Mode(const fs::path& path) {+ struct stat info{};+ CHECK(lstat(path.c_str(), &info) == 0);+ return info.st_mode & 07777;+}std::string Read(const fs::path& path) {std::ifstream in(path, std::ios::binary);return {std::istreambuf_iterator<char>(in), {}};@@ -449,11 +456,360 @@ void ImportedGamePreparesWithoutSteam() {host.Shutdown();}+// The Local store end to end (import-games Task 3.3): a folder on a USB drive is found, added and+// listed with its title; Details offers Play, which prepares it from its folder with the program+// chosen at add (written into Game settings first) under a runtime number from 0xF0000000. An+// unplugged drive leaves it listed but unavailable; a folder name the PS5 can't open never plays in+// place. Removing it keeps its files and its bottle (kept data); adding it again brings back its ID+// and settings. Signed out of Steam, Playden opens on Home; signed in, the game is still there.+void ImportedGamePlaysInPlace() {+ using playden::GameId;+ namespace hb = playden::homebrew;+ namespace local = playden::stores::local;+ Fixture fixture;+ auto config = fixture.Config();+ config.host_namespace_prefix = fixture.root;+ config.package_paths.console_data_root = "/data";+ config.package_paths.host_data_root = fixture.root / "data";+ config.package_operations.hash_file = HashArtifactFile;+ config.package_operations.hash_bytes = HashArtifactBytes;+ config.package_operations.free_space_unverifiable = true;+ config.package_operations.validate_bottle = [](const fs::path& bottle) {+ return fs::is_regular_file(bottle / "system.reg") && fs::is_regular_file(bottle / "user.reg");+ };+ const auto kit = SyntheticKit(fixture.root / "kit");+ config.admit_kit = [&kit](std::string*) { return std::optional<RuntimeKit>(kit); };+ unsigned uuids = 0;+ config.uuid = [&uuids]() -> std::optional<std::string> {+ ++uuids; return std::string("00000000-0000-4000-8000-0000000000") + (uuids < 10 ? "0" : "") + std::to_string(uuids);+ };+ config.unix_time = [] { return std::int64_t{1760000000}; };+ // Game settings as the console keeps them: one file, read by the worker at each preparation.+ fs::create_directories(fixture.root / "user");+ std::string settings_reason;+ const auto prepared_settings = PrepareGameSettingsStorage(fixture.root / "user", &settings_reason);+ if (!prepared_settings) std::fprintf(stderr, "settings: %s\n", settings_reason.c_str());+ CHECK(prepared_settings);+ const auto settings_file = *prepared_settings;+ config.settings_record = [settings_file](const GameId& game) -> std::optional<GameSettingsRecord> {+ std::map<GameId, GameSettingsRecord> records;+ std::string reason;+ if (!LoadGameSettingsFile(settings_file.string(), &records, &reason)) return std::nullopt;+ const auto found = records.find(game);+ return found == records.end() ? std::nullopt : std::optional<GameSettingsRecord>(found->second);+ };+ // Play starts the prepared title (on the application thread, from the controller's Tick). Nothing+ // runs it here: without the wrapper's evidence the session soon ends, as a title closed at once would.+ config.launch_evidence_wait_ms = 300;+ // Removing the game removes its PS5 title through the title remover.+ config.unregister_title = [&fixture](const std::string&, const std::string&) {+ fixture.registered = false; fixture.submitted = false;+ return TitleRemoverResult{true, 0, "answered"};+ };+ std::vector<std::string> launched;+ config.launch_title = [&launched](const std::string& title, const std::vector<std::string>& arguments) {+ if (arguments.size() == 1 && arguments[0].rfind("session=", 0) == 0) launched.push_back(title);+ return TitleResult{true, 0, "launch"};+ };+ std::string pe(0x80 + 26, '\0');+ pe[0] = 'M'; pe[1] = 'Z'; pe[0x3c] = 0x40; pe[0x40] = 'P'; pe[0x41] = 'E';+ pe[0x44] = 0x64; pe[0x45] = static_cast<char>(0x86); pe[0x40 + 24] = 0x0b; pe[0x40 + 25] = 0x02;+ const auto usb = fixture.root / "mnt/usb0/playden/import";+ Write(usb / "Rally/Bin/Rally.exe", pe + std::string(4096, 'x'), 0644);+ Write(usb / "Rally/Redist/vcredist_x64.exe", pe, 0644);+ Write(usb / "Rally/data.pak", "data", 0644);+ Write(usb / "Caf\xc3\xa9/Game.exe", pe, 0644);+ CHECK(chmod((usb / "Rally").c_str(), 0755) == 0);+ const std::string console_root = "/mnt/usb0/playden/import/Rally";+ std::atomic<bool> usb_present{true};+ // The drop folder's games: FTP leaves them root-owned, simulated by telling the walk the app user+ // is someone else; the fake root raise runs the work and is counted.+ std::atomic<unsigned> drop_raises{0};+ // imports.json sits next to installs.json, in the folder the runtime host makes private at its start.+ playden::Storage import_storage((fixture.root / "data/runtime-state").string());+ std::string id;++ const auto run = [&](bool signed_in, const std::function<void(hb::Simulation&, hb::Controller&, RuntimeHost&,+ local::LocalLibrarySource&,+ const std::function<void(const std::function<bool()>&)>&)>& body) {+ hb::Simulation model({}, {}, hb::ModelMode::Live);+ model.SetGameSettingsFile(settings_file);+ MemorySessions sessions;+ if (signed_in) sessions.value = playden::StoredSession{12345, "Synthetic account", "refresh", "access", {}};+ RestoringBackend backend;+ local::ImportStore imports(import_storage);+ local::LocalLibraryConfig local_config;+ local_config.volumes = {{"internal", "/data", "playden-import", false}, {"usb0", "/mnt/usb0", "playden/import", true}};+ local_config.host_prefix = fixture.root.string();+ local_config.present = [&usb_present](const local::ImportVolume& volume) {+ return volume.volume != "usb0" || usb_present.load();+ };+ local::DropFolderAccessOptions drop_access;+ drop_access.namespace_root = fixture.root;+ drop_access.app_uid = ::geteuid() + 1;+ drop_access.as_root = [&drop_raises](const std::function<bool()>& work) { ++drop_raises; return work(); };+ local_config.open_drop_folder = local::MakeDropFolderOpener(drop_access);+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ playden::stores::Registry registry;+ registry.Add(std::make_unique<playden::stores::SteamAccount>(backend, sessions));+ registry.Add(std::make_unique<playden::test::FakeLibrarySource>(playden::Store::kSteam));+ auto source = std::make_unique<local::LocalLibrarySource>(imports, local_config);+ auto* local_library = source.get();+ registry.Add(std::move(source));+ hb::Controller controller(model, registry, nullptr, nullptr, nullptr, &host);+ controller.SetLocalLibrary(local_library);+ const auto tick_until = [&](const std::function<bool()>& done) {+ const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60);+ for (;;) {+ controller.Tick();+ if (done()) return;+ if (std::chrono::steady_clock::now() >= deadline) {+ const auto local_snapshot = local_library->Snapshot();+ std::fprintf(stderr, "runtime=%d scanning=%d candidates=%zu games=%zu imports=%zu external=%zu outcomes=%zu\n",+ (int)model.state().runtime, (int)model.state().import_scanning, model.state().import_candidates.size(),+ model.state().games.size(), local_snapshot->games.size(), model.state().external_games.size(),+ local_snapshot->outcomes.size());+ }+ CHECK(std::chrono::steady_clock::now() < deadline);+ std::this_thread::sleep_for(std::chrono::milliseconds(2));+ }+ };+ tick_until([&] {+ return host.Snapshot().status == RuntimeHostStatus::kReady && model.state().runtime == hb::RuntimeState::Ready &&+ !model.state().import_scanning && model.state().import_candidates.size() == 2;+ });+ body(model, controller, host, *local_library, tick_until);+ controller.Shutdown();+ host.Shutdown();+ };+ const auto game_of = [](const hb::Simulation& model, const GameId& game) -> const hb::Game* {+ for (const auto& value : model.state().games) if (value.id == game) return &value;+ return nullptr;+ };+ const auto external_of = [](const hb::Simulation& model, const GameId& game) -> const hb::LiveExternalGame* {+ for (const auto& value : model.state().external_games) if (value.game_id == game) return &value;+ return nullptr;+ };+ const auto row_of = [](const hb::Simulation& model, const std::string& key) -> const hb::ViewRow* {+ for (const auto& row : model.view().rows) if (row.key == key) return &row;+ return nullptr;+ };+ const auto open_details = [](hb::Simulation& model, const GameId& game) {+ for (int i = 0; i < 8 && model.state().navigation.route != hb::Route::Library; ++i) model.action("r1");+ CHECK(model.state().navigation.route == hb::Route::Library);+ CHECK(model.action("focus:game-" + playden::ToKey(game)) && model.action("cross"));+ CHECK(model.state().navigation.route == hb::Route::Details);+ };++ // Signed out of Steam.+ run(false, [&](hb::Simulation& model, hb::Controller&, RuntimeHost& host, local::LocalLibrarySource& source,+ const std::function<void(const std::function<bool()>&)>& tick_until) {+ // The two folders on the drive are candidates: Rally with its game first and the redistributable+ // flagged, and the one whose name the PS5 can't play from.+ const auto& candidates = model.state().import_candidates;+ const auto rally = std::find_if(candidates.begin(), candidates.end(), [](const hb::LiveImportCandidate& c) { return c.title == "Rally"; });+ CHECK(rally != candidates.end() && rally->volume_id == "usb0" && rally->folder == "playden/import/Rally");+ CHECK(rally->ascii_ok && !rally->imported.valid() && rally->programs.size() == 2);+ CHECK(rally->programs[0].path == "Bin/Rally.exe" && !rally->programs[0].helper && rally->programs[1].helper);+ CHECK(rally->total_bytes > 4096);+ const auto cafe = std::find_if(candidates.begin(), candidates.end(), [](const hb::LiveImportCandidate& c) { return !c.ascii_ok; });+ CHECK(cafe != candidates.end() && cafe->folder == "playden/import/Caf\xc3\xa9");++ // Add: the game is in the library with its folder's name, and plays in place.+ model.RequestImport({hb::ImportRequestKind::Add, {}, "usb0", "Rally", "Bin/Rally.exe"});+ tick_until([&] {+ const auto snapshot = source.Snapshot();+ if (snapshot->games.empty()) return false;+ id = snapshot->games.front().id;+ const auto* external = external_of(model, GameId::Local(id));+ return game_of(model, GameId::Local(id)) && external && external->available &&+ model.state().runtime_games.count(GameId::Local(id));+ });+ const auto game = GameId::Local(id);+ CHECK(game_of(model, game)->title == "Rally");+ CHECK(model.state().navigation.route == hb::Route::Home);+ // Never one of Playden's installs.+ CHECK(model.state().installations.empty());+ // A bad program or an unknown drive is refused, and nothing is added.+ const auto refused = source.Add("usb0", "Rally", "Bin/Missing.exe");+ const auto unknown = source.Add("usb9", "Rally", "Bin/Rally.exe");+ tick_until([&] {+ const auto snapshot = source.Snapshot();+ const auto outcome = [&](std::uint64_t request) {+ return std::any_of(snapshot->outcomes.begin(), snapshot->outcomes.end(), [&](const local::ImportOutcome& o) { return o.request == request && !o.ok; });+ };+ return outcome(refused) && outcome(unknown) && snapshot->games.size() == 1;+ });++ // Details offers Play; it prepares the game from its folder with the chosen program, then starts it.+ open_details(model, game);+ const auto* play = row_of(model, "details-play");+ CHECK(play && play->enabled && play->action == "runtime-play" && play->title == "Play");+ CHECK(model.action("focus:details-play") && model.action("cross"));+ tick_until([&] { return !launched.empty(); });+ CHECK(launched.front() == "FAKE78001");+ tick_until([&] {+ const auto s = host.Snapshot();+ return s.launch && s.launch->game == game && s.launch->state != LaunchState::kReadyToHandoff &&+ s.launch->state != LaunchState::kLaunching;+ });+ {+ const auto snapshot = host.Snapshot();+ const auto number = NumberOf(snapshot, game);+ CHECK(number >= 0xF0000000u && !snapshot.refusals.count(number));+ const auto view = std::find_if(snapshot.installs.begin(), snapshot.installs.end(), [&](const RuntimeInstallView& v) { return v.game == game; });+ CHECK(view->install_path == console_root);+ CHECK(model.state().game_settings.at(game).overrides.at("program") == "Bin/Rally.exe");+ std::map<GameId, GameSettingsRecord> saved;+ std::string reason;+ CHECK(LoadGameSettingsFile(settings_file.string(), &saved, &reason) && saved.at(game).overrides.at("program") == "Bin/Rally.exe");+ }+ });++ // Started again: drives come and go, and the game leaves the library.+ run(false, [&](hb::Simulation& model, hb::Controller&, RuntimeHost& host, local::LocalLibrarySource& source,+ const std::function<void(const std::function<bool()>&)>& tick_until) {+ const auto game = GameId::Local(id);+ tick_until([&] {+ const auto snapshot = host.Snapshot();+ return game_of(model, game) && model.state().runtime_games.count(game) &&+ (!snapshot.launch || (snapshot.launch->state != LaunchState::kLaunching &&+ snapshot.launch->state != LaunchState::kRunning));+ });+ open_details(model, game);++ // Unplugged: still listed, unavailable, and Details says to connect the drive.+ usb_present = false;+ source.CheckVolumes();+ tick_until([&] {+ const auto* external = external_of(model, game);+ return external && !external->available && !model.state().runtime_games.count(game) &&+ model.state().import_candidates.empty();+ });+ CHECK(game_of(model, game));+ CHECK(row_of(model, "details-installed") && row_of(model, "details-installed")->title == "Storage disconnected");+ CHECK(!row_of(model, "details-play"));+ usb_present = true;+ source.CheckVolumes();+ tick_until([&] {+ const auto* external = external_of(model, game);+ return external && external->available && model.state().runtime_games.count(game);+ });++ // The folder the PS5 can't open can be added, but never plays in place.+ model.RequestImport({hb::ImportRequestKind::Add, {}, "usb0", "playden/import/Caf\xc3\xa9", "Game.exe"});+ tick_until([&] { return source.Snapshot()->games.size() == 2 && model.state().external_games.size() == 2; });+ const auto cafe_game = GameId::Local(source.Snapshot()->games[1].id);+ CHECK(!external_of(model, cafe_game)->available && external_of(model, cafe_game)->reason == local::kNotAsciiReason);+ CHECK(!model.state().runtime_games.count(cafe_game));+ model.RequestImport({hb::ImportRequestKind::Remove, cafe_game, {}, {}, {}});+ tick_until([&] { return source.Snapshot()->games.size() == 1 && !game_of(model, cafe_game); });++ // Removed: out of the library, its files where they were, its bottle kept.+ for (int i = 0; i < 4 && model.state().navigation.route != hb::Route::Home; ++i) model.action("circle");+ model.RequestImport({hb::ImportRequestKind::Remove, game, {}, {}, {}});+ tick_until([&] {+ const auto snapshot = host.Snapshot();+ return !game_of(model, game) && source.Snapshot()->games.empty() &&+ std::any_of(snapshot.kept_bottles.begin(), snapshot.kept_bottles.end(), [&](const auto& kept) { return kept.game == game; });+ });+ CHECK(fs::is_regular_file(usb / "Rally/Bin/Rally.exe") && fs::is_regular_file(usb / "Rally/data.pak"));+ CHECK(model.state().external_games.empty());++ // Added again: the same ID, its settings and the program chosen before.+ model.RequestImport({hb::ImportRequestKind::Add, {}, "usb0", "Rally", "Bin/Rally.exe"});+ tick_until([&] { return game_of(model, game) && external_of(model, game) && external_of(model, game)->available; });+ CHECK(source.Snapshot()->games.front().id == id);+ CHECK(model.state().game_settings.at(game).overrides.at("program") == "Bin/Rally.exe");+ });++ // Signed in to Steam, after a restart: the imported game is listed from imports.json and Details offers Play.+ run(true, [&](hb::Simulation& model, hb::Controller&, RuntimeHost&, local::LocalLibrarySource&,+ const std::function<void(const std::function<bool()>&)>& tick_until) {+ const auto game = GameId::Local(id);+ tick_until([&] {+ return model.state().account().authentication == hb::AuthState::SignedIn && game_of(model, game) &&+ model.state().runtime_games.count(game);+ });+ const auto rally = std::find_if(model.state().import_candidates.begin(), model.state().import_candidates.end(),+ [](const hb::LiveImportCandidate& c) { return c.title == "Rally"; });+ CHECK(rally != model.state().import_candidates.end() && rally->imported == game);+ open_details(model, game);+ const auto* play = row_of(model, "details-play");+ CHECK(play && play->enabled);+ });++ // A game copied over FTP into the drop folder: its files are opened when it is added and again+ // before each Play; a link out of its folder stops Play with the folder named.+ const auto sky = fixture.root / "data/playden-import/Sky";+ run(false, [&](hb::Simulation& model, hb::Controller&, RuntimeHost& host, local::LocalLibrarySource& source,+ const std::function<void(const std::function<bool()>&)>& tick_until) {+ Write(sky / "Game.exe", pe + std::string(4096, 'x'), 0755);+ Write(sky / "Data/sky.ini", "[sky]", 0644);+ CHECK(chmod((sky / "Data").c_str(), 0755) == 0 && chmod(sky.c_str(), 0755) == 0);+ model.RequestImport({hb::ImportRequestKind::Add, {}, "internal", "Sky", "Game.exe"});+ std::string sky_id;+ tick_until([&] {+ for (const auto& game : source.Snapshot()->games)+ if (game.volume == "internal") sky_id = game.id;+ const auto* external = sky_id.empty() ? nullptr : external_of(model, GameId::Local(sky_id));+ return external && external->available && model.state().runtime_games.count(GameId::Local(sky_id)) &&+ game_of(model, GameId::Local(sky_id));+ });+ const auto game = GameId::Local(sky_id);+ CHECK(source.Snapshot()->games.size() == 2);+ CHECK(Mode(sky) == 0777 && Mode(sky / "Data") == 0777 && Mode(sky / "Data/sky.ini") == 0666 &&+ Mode(sky / "Game.exe") == 0777 && drop_raises > 0);++ // FTP replaced the .ini (root-owned 0644 again): Play opens it before the game starts.+ CHECK(chmod((sky / "Data/sky.ini").c_str(), 0644) == 0);+ const auto launches = launched.size();+ open_details(model, game);+ CHECK(model.action("focus:details-play") && model.action("cross"));+ tick_until([&] { return launched.size() > launches; });+ CHECK(Mode(sky / "Data/sky.ini") == 0666);+ {+ const auto snapshot = host.Snapshot();+ const auto view = std::find_if(snapshot.installs.begin(), snapshot.installs.end(), [&](const RuntimeInstallView& v) { return v.game == game; });+ CHECK(view != snapshot.installs.end() && view->install_path == "/data/playden-import/Sky");+ }+ tick_until([&] {+ const auto s = host.Snapshot();+ return !s.launch || (s.launch->state != LaunchState::kReadyToHandoff && s.launch->state != LaunchState::kLaunching &&+ s.launch->state != LaunchState::kRunning);+ });++ // A link out of the game's folder: Play stops and says which.+ fs::create_directories(fixture.root / "outside");+ CHECK(symlink("../../../outside", (sky / "Data/out").c_str()) == 0);+ const auto before = launched.size();+ // The synthetic title never starts, so that session ended with a "Can't play" of its own.+ tick_until([&] { return !model.state().navigation.modals.empty(); });+ CHECK(model.action("focus:alert-ok") && model.action("cross"));+ open_details(model, game);+ CHECK(model.action("focus:details-play") && model.action("cross"));+ tick_until([&] {+ const auto& modals = model.state().navigation.modals;+ if (modals.empty()) return false;+ if (modals.back().body.find("/data/playden-import/Sky/Data/out") != std::string::npos)+ return modals.back().title == "Can't play";+ // The earlier session's own "could not start" may still come up first.+ CHECK(model.action("focus:alert-ok") && model.action("cross"));+ return false;+ });+ CHECK(launched.size() == before);+ CHECK(unlink((sky / "Data/out").c_str()) == 0);+ });+}+int main(int argc, char** argv) {CHECK(argc == 2);g_runtime_dir = argv[1];ExternalGameRegistersAfterRestartWithoutCloud();ImportedGamePreparesWithoutSteam();+ ImportedGamePlaysInPlace();std::puts("installed locations tests passed");return 0;}diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 9064d1c..48b0a44 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -4141,6 +4141,308 @@ void MovedGameWithUnreadableOperationRepublishes() {host.Shutdown();}+// "Copy into Playden" and "Move into Playden" of an imported game already prepared to play from its+// folder (import-games 4.3, 4.4): RuntimeHost::Relocate.+constexpr const char* kImportedId = "0f8fad5b-d9cb-469f-a165-70867728950e";+constexpr const char* kImportedRoot = "/data/playden-games/local-0f8fad5b-d9cb-469f-a165-70867728950e";+constexpr const char* kImportedSave = "data/playden/games/local-0f8fad5b-d9cb-469f-a165-70867728950e/library/prefixes/game/drive_c/users/steamuser/save.dat";++struct Imported {+ std::uint32_t number = 0;+ std::string title_id;+};++void WriteImportedGame(const fs::path& folder) {+ std::string pe(0x80 + 26, '\0');+ pe[0] = 'M'; pe[1] = 'Z'; pe[0x3c] = 0x40; pe[0x40] = 'P'; pe[0x41] = 'E';+ pe[0x44] = 0x64; pe[0x45] = static_cast<char>(0x86); pe[0x40 + 24] = 0x0b; pe[0x40 + 25] = 0x02;+ Write(folder / "Bin/Game.exe", pe, 0644);+ Write(folder / "Data/game.pak", "pak", 0644);+ CHECK(chmod(folder.c_str(), 0777) == 0);+}++RuntimePrepareRequest ImportedRequest(std::uint32_t number, const std::string& folder) {+ RuntimePrepareRequest request{number, folder, folder.rfind("/data/", 0) == 0 ? "internal" : "usb0", "Imported"};+ request.launches = {{"", "", "Bin/Game.exe", "", ""}};+ request.game = playden::GameId::Local(kImportedId);+ return request;+}++// Prepares the imported game at `folder` (a console path) and leaves a save in its bottle.+Imported PrepareImported(Fixture& fixture, const RuntimeHostConfig& config, const std::string& folder) {+ WriteImportedGame(fixture.root / fs::path(folder).relative_path());+ Imported imported;+ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ imported.number = *host.RuntimeNumberFor(playden::GameId::Local(kImportedId));+ host.Prepare(ImportedRequest(imported.number, folder));+ const auto number = imported.number;+ const auto ready = WaitFor(host, [number](const RuntimeSnapshot& s) {+ return !s.preparing_app && (s.refusals.count(number) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));+ }, 60);+ if (ready.refusals.count(number)) std::fprintf(stderr, "Prepare refused: %s\n", ready.refusals.at(number).c_str());+ CHECK(!ready.refusals.count(number) && ready.installs.size() == 1 && ready.installs[0].install_path == folder);+ imported.title_id = ready.installs[0].title_id;+ host.Shutdown();+ Write(fixture.root / kImportedSave, "player-save", 0600);+ return imported;+}++// The relocated game: same title, number and bottle; its record and publication name the new folder.+void CheckRelocated(Fixture& fixture, RuntimeHost& host, const Imported& imported, const std::string& volume) {+ const auto snapshot = host.Snapshot();+ CHECK(snapshot.installs.size() == 1);+ const auto& view = snapshot.installs[0];+ CHECK(view.title_id == imported.title_id && view.runtime_number == imported.number && view.state == PreparationState::kReady);+ CHECK(view.install_path == kImportedRoot && view.admission == "ok" && view.settings_revision == 1);+ const auto records = Read(fixture.root / "data/runtime-state/installs.json");+ CHECK(records.find(std::string("\"install_path\":\"") + kImportedRoot + "\"") != std::string::npos);+ CHECK(records.find("\"volume_id\":\"" + volume + "\"") != std::string::npos && records.find("moved_from") == std::string::npos);+ const auto descriptor = Read(fixture.root / "data/homebrew" / imported.title_id / "game-launch.json");+ CHECK(descriptor.find(std::string("\"game_root\":\"") + kImportedRoot + "\"") != std::string::npos);+ CHECK(Read(fixture.root / kImportedSave) == "player-save");+ // Play passes the title's whole start check.+ host.Play(imported.number);+ const auto number = imported.number;+ const auto played = WaitFor(host, [number](const RuntimeSnapshot& s) { return s.handoff_ready || s.play_refusals.count(number); }, 30);+ if (played.play_refusals.count(number)) std::fprintf(stderr, "Play refused: %s\n", played.play_refusals.at(number).c_str());+ CHECK(played.handoff_ready && host.Handoff());+}++RuntimeRelocationResult RelocateNow(RuntimeHost& host, std::uint32_t number, std::function<std::string()> move_files = {}) {+ RuntimeRelocation request;+ request.runtime_number = number;+ request.to = kImportedRoot;+ request.volume_id = "internal";+ request.move_files = std::move(move_files);+ return host.Relocate(std::move(request), std::chrono::seconds(60));+}++// Copy later: a game prepared on a USB drive, copied into Playden. Its title follows the copy;+// the drive folder stays as it was.+void CopiedGameKeepsItsTitle() {+ Fixture fixture;+ GamesMove move;+ const auto kit = SyntheticKit(fixture.root / "kit");+ unsigned uuids = 0;+ const auto config = MoveConfig(fixture, kit, &uuids, move);+ const std::string drive = "/mnt/usb0/playden/import/Imported";+ const auto imported = PrepareImported(fixture, config, drive);+ const auto registrations = fixture.registry_mutations;+ fs::create_directories(fixture.root / "data/playden-games");+ fs::copy(fixture.root / fs::path(drive).relative_path(), fixture.root / fs::path(kImportedRoot).relative_path(),+ fs::copy_options::recursive);+ {+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ const auto result = RelocateNow(host, imported.number);+ if (!result.message.empty()) std::fprintf(stderr, "Relocate: %s\n", result.message.c_str());+ CHECK(result.outcome == RuntimeRelocationResult::Outcome::kDone && !result.prepare_again && result.message.empty());+ CHECK(move.Logged("relocate app=" + std::to_string(imported.number) + " republished"));+ CheckRelocated(fixture, host, imported, "internal");+ CHECK(fixture.registry_mutations == registrations); // the same title: nothing registered again+ CHECK(fs::is_regular_file(fixture.root / fs::path(drive).relative_path() / "Bin/Game.exe"));+ host.Shutdown();+ }+ // After a restart (the copy runner asks again when it stopped before its install record): done.+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ const auto again = RelocateNow(host, imported.number);+ CHECK(again.outcome == RuntimeRelocationResult::Outcome::kDone && again.message.empty());+ CHECK(host.Snapshot().installs[0].install_path == kImportedRoot);+ host.Shutdown();+}++// Move: the drop folder renamed under the title's publication lease; refused while the title runs.+void MovedGameKeepsItsTitle() {+ Fixture fixture;+ GamesMove move;+ const auto kit = SyntheticKit(fixture.root / "kit");+ unsigned uuids = 0;+ const auto config = MoveConfig(fixture, kit, &uuids, move);+ const std::string drop = "/data/playden-import/Imported";+ const auto imported = PrepareImported(fixture, config, drop);+ fs::create_directories(fixture.root / "data/playden-games");+ const auto from = fixture.root / fs::path(drop).relative_path();+ const auto to = fixture.root / fs::path(kImportedRoot).relative_path();+ const auto lock_path = fixture.root / "data/playden/games" / ("local-" + std::string(kImportedId)) / ".playden-control/publication.lock";+ const auto held = [&] {+ const int fd = ::open(lock_path.c_str(), O_RDONLY);+ CHECK(fd >= 0);+ const bool busy = ::flock(fd, LOCK_SH | LOCK_NB) != 0;+ ::close(fd);+ return busy;+ };+ unsigned renames = 0;+ const auto rename = [&]() -> std::string {+ ++renames;+ CHECK(held()); // the lease is held through the rename+ if (!fs::exists(from) && fs::is_directory(to)) return {};+ return ::rename(from.c_str(), to.c_str()) == 0 ? std::string() : std::string("rename failed");+ };+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ // Its title runs (or is suspended): nothing moves.+ fixture.activity->store(static_cast<int>(NativeActivity::kActive));+ auto refused = RelocateNow(host, imported.number, rename);+ CHECK(refused.outcome == RuntimeRelocationResult::Outcome::kBusy && refused.message == "Close the game first." && renames == 0);+ fixture.activity->store(static_cast<int>(NativeActivity::kUnknown));+ refused = RelocateNow(host, imported.number, rename);+ CHECK(refused.outcome == RuntimeRelocationResult::Outcome::kBusy && renames == 0);+ // A Home launch's reader lease refuses it too, whatever the activity says.+ fixture.activity->store(static_cast<int>(NativeActivity::kStopped));+ const int reader = ::open(lock_path.c_str(), O_RDONLY);+ CHECK(reader >= 0 && ::flock(reader, LOCK_SH | LOCK_NB) == 0);+ refused = RelocateNow(host, imported.number, rename);+ CHECK(refused.outcome == RuntimeRelocationResult::Outcome::kBusy && renames == 0);+ ::close(reader);+ CHECK(fs::exists(from / "Bin/Game.exe") && host.Snapshot().installs[0].install_path == drop);+ // Stopped: renamed, then republished.+ const auto result = RelocateNow(host, imported.number, rename);+ if (!result.message.empty()) std::fprintf(stderr, "Relocate: %s\n", result.message.c_str());+ CHECK(result.outcome == RuntimeRelocationResult::Outcome::kDone && !result.prepare_again && renames == 1);+ CHECK(!fs::exists(from) && fs::is_regular_file(to / "Bin/Game.exe") && !held());+ CheckRelocated(fixture, host, imported, "internal");+ // A game without a PS5 title: the files move at once.+ const auto other = fixture.root / "data/playden-import/Other";+ WriteImportedGame(other);+ unsigned plain = 0;+ RuntimeRelocation unprepared;+ unprepared.to = "/data/playden-games/local-other";+ unprepared.move_files = [&]() -> std::string {+ ++plain;+ return ::rename(other.c_str(), (fixture.root / "data/playden-games/local-other").c_str()) == 0 ? std::string() : "failed";+ };+ const auto moved = host.Relocate(std::move(unprepared), std::chrono::seconds(30));+ CHECK(moved.outcome == RuntimeRelocationResult::Outcome::kDone && plain == 1);+ CHECK(fs::is_regular_file(fixture.root / "data/playden-games/local-other/Bin/Game.exe"));+ host.Shutdown();+}++// Restarts: after the rename (record not written), after the record (republish not done), after+// the republish. Each start leaves the game consistent and the next Relocate finishes it.+void RelocationResumesAfterRestart() {+ const std::string drop = "/data/playden-import/Imported";+ for (const int step : {1, 2}) {+ Fixture fixture;+ GamesMove move;+ const auto kit = SyntheticKit(fixture.root / "kit");+ unsigned uuids = 0;+ const auto config = MoveConfig(fixture, kit, &uuids, move);+ const auto imported = PrepareImported(fixture, config, drop);+ fs::create_directories(fixture.root / "data/playden-games");+ const auto from = fixture.root / fs::path(drop).relative_path();+ const auto to = fixture.root / fs::path(kImportedRoot).relative_path();+ unsigned renames = 0;+ const auto rename = [&]() -> std::string {+ ++renames;+ if (!fs::exists(from) && fs::is_directory(to)) return {};+ return ::rename(from.c_str(), to.c_str()) == 0 ? std::string() : std::string("rename failed");+ };+ if (step == 2) {+ // The record written (moved_from), then Playden stopped before the republish.+ playden::Storage storage(config.state_root.string());+ RuntimeInstallRepository repository(storage, "installs.json", {});+ CHECK(repository.Load().ok());+ auto record = repository.Snapshot().front();+ record.moved_from = record.install_path;+ record.install_path = kImportedRoot;+ CHECK(repository.UpdateAll({record}).ok());+ }+ // Step 1: renamed, then Playden stopped; the record still names the drop folder.+ CHECK(::rename(from.c_str(), to.c_str()) == 0);+ {+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ const auto logged = move.Logged("startup moved app=" + std::to_string(imported.number) + " republished");+ if (step == 1) {+ // Nothing moves the record by itself: Relocate (the copy runner's resume) finishes it.+ CHECK(!logged && host.Snapshot().installs[0].install_path == drop);+ } else {+ // The next start republishes it like a game the 0.6 move left marked.+ CHECK(logged && host.Snapshot().installs[0].install_path == kImportedRoot);+ }+ const auto result = RelocateNow(host, imported.number, rename);+ if (!result.message.empty()) std::fprintf(stderr, "Relocate: %s\n", result.message.c_str());+ CHECK(result.outcome == RuntimeRelocationResult::Outcome::kDone && !result.prepare_again);+ CHECK(renames == (step == 1 ? 1u : 0u)); // after its record, nothing renames again+ CheckRelocated(fixture, host, imported, "internal");+ host.Shutdown();+ }+ // After the republish: a start changes nothing.+ std::size_t before = 0;+ {+ std::lock_guard<std::mutex> lock(move.mutex);+ before = move.lines.size();+ }+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ {+ std::lock_guard<std::mutex> lock(move.mutex);+ for (std::size_t index = before; index < move.lines.size(); ++index)+ CHECK(move.lines[index].rfind("startup moved app=", 0) != 0);+ }+ CHECK(host.Snapshot().installs[0].install_path == kImportedRoot);+ host.Shutdown();+ }+}++// The moved publication can't be verified (its descriptor changed): the old title goes, keeping its+// saves, and the game is prepared again at its new folder with them.+void RelocationFallsBackToANewTitle() {+ Fixture fixture;+ GamesMove move;+ const auto kit = SyntheticKit(fixture.root / "kit");+ unsigned uuids = 0;+ auto config = MoveConfig(fixture, kit, &uuids, move);+ config.removal_confirm_polls = 3;+ config.removal_poll_interval = std::chrono::milliseconds(1);+ config.unregister_title = [&fixture](const std::string&, const std::string&) {+ fixture.registered = false; fixture.submitted = false;+ return TitleRemoverResult{true, 0, "answered"};+ };+ const std::string drop = "/data/playden-import/Imported";+ const auto imported = PrepareImported(fixture, config, drop);+ fs::create_directories(fixture.root / "data/playden-games");+ const auto from = fixture.root / fs::path(drop).relative_path();+ const auto to = fixture.root / fs::path(kImportedRoot).relative_path();+ RuntimeHost host(config);+ CHECK(host.Start());+ WaitFor(host, Started);+ const auto descriptor = fixture.root / "data/homebrew" / imported.title_id / "game-launch.json";+ const auto bytes = Read(descriptor);+ CHECK(!bytes.empty());+ fs::permissions(descriptor, fs::perms::owner_read | fs::perms::owner_write);+ std::ofstream(descriptor, std::ios::binary | std::ios::trunc) << bytes << " ";+ const auto result = RelocateNow(host, imported.number, [&]() -> std::string {+ return ::rename(from.c_str(), to.c_str()) == 0 ? std::string() : std::string("rename failed");+ });+ if (!result.message.empty()) std::fprintf(stderr, "Relocate: %s\n", result.message.c_str());+ CHECK(result.outcome == RuntimeRelocationResult::Outcome::kDone && result.prepare_again && result.message.empty());+ const auto removed = host.Snapshot();+ CHECK(removed.installs.empty() && fs::is_regular_file(to / "Bin/Game.exe"));+ const auto kept = std::find_if(removed.kept_bottles.begin(), removed.kept_bottles.end(),+ [](const KeptBottleView& view) { return view.game == playden::GameId::Local(kImportedId); });+ CHECK(kept != removed.kept_bottles.end() && kept->runtime_number == imported.number);+ // Play prepares it again where it is now; the saves come back.+ host.Prepare(ImportedRequest(imported.number, kImportedRoot));+ const auto number = imported.number;+ const auto ready = WaitFor(host, [number](const RuntimeSnapshot& s) {+ return !s.preparing_app && (s.refusals.count(number) || (!s.installs.empty() && s.installs[0].state == PreparationState::kReady));+ }, 60);+ if (ready.refusals.count(number)) std::fprintf(stderr, "Prepare refused: %s\n", ready.refusals.at(number).c_str());+ CHECK(!ready.refusals.count(number) && ready.installs[0].install_path == kImportedRoot);+ CHECK(ready.installs[0].runtime_number == imported.number);+ CHECK(Read(fixture.root / kImportedSave) == "player-save");+ host.Shutdown();+}+int main(int argc, char** argv) {CHECK(argc == 2);g_runtime_dir = argv[1];@@ -4197,6 +4499,10 @@ int main(int argc, char** argv) {UnownedUpgradeSkippedWhenFolderMissing();for (int scenario = 0; scenario <= 9; ++scenario) UnownedUpgradeAfterCommittedSettingsUpdates(scenario);MovedGameWithUnreadableOperationRepublishes();+ CopiedGameKeepsItsTitle();+ MovedGameKeepsItsTitle();+ RelocationResumesAfterRestart();+ RelocationFallsBackToANewTitle();std::puts("runtime host tests passed");return 0;}diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cppindex 18697f6..e4f253b 100644--- a/tests/install_service_test.cpp+++ b/tests/install_service_test.cpp@@ -534,6 +534,34 @@ int main() {CHECK(requests[3].action == InstallRequest::Action::kInstall && requests[3].app == 108600);CHECK(ParseInstallRequests("0\n-1\n4294967296\n12x\nuninstall 12 extra\nunknown 12\n").empty());CHECK(ParseInstallRequests(" cancel 42 \r\n")[0].app == 42);+ CHECK(requests[0].game == playden::GameId::Steam(439490));+ {+ // Imported games: by their key, and `import` adds one in place (quoted words may hold spaces).+ const std::string uuid = "0f8fad5b-d9cb-469f-a165-70867728950e";+ const auto local = ParseInstallRequests("local-" + uuid + "\nuninstall local-" + uuid + "\n"+ "import usb0 Delores Delores.exe\n"+ "import\tusb0 \"Old School Rally\" \"bin/Old School Rally.exe\"\r\n"+ "import internal playden-import/Night-Sky NightSky.exe\n");+ CHECK(local.size() == 5);+ CHECK(local[0].action == InstallRequest::Action::kInstall && local[0].game == playden::GameId::Local(uuid) && local[0].app == 0);+ CHECK(local[1].action == InstallRequest::Action::kUninstall && local[1].game == playden::GameId::Local(uuid));+ CHECK(local[2].action == InstallRequest::Action::kImport && local[2].volume == "usb0" && local[2].folder == "Delores" &&+ local[2].program == "Delores.exe" && !local[2].game.valid());+ CHECK(local[3].folder == "Old School Rally" && local[3].program == "bin/Old School Rally.exe");+ CHECK(local[4].volume == "internal" && local[4].folder == "playden-import/Night-Sky");+ // `copy` copies an imported game into Playden, to the default volume or the one named.+ const auto copies = ParseInstallRequests("copy local-" + uuid + "\ncopy local-" + uuid + " usb0\ncopy 220\n"+ "copy local-" + uuid + " usb0 extra\ncopy\n");+ CHECK(copies.size() == 2);+ CHECK(copies[0].action == InstallRequest::Action::kCopy && copies[0].game == playden::GameId::Local(uuid) &&+ copies[0].volume.empty());+ CHECK(copies[1].action == InstallRequest::Action::kCopy && copies[1].volume == "usb0");+ // Refused: a cancel (imported games have no download), a malformed key, missing or extra words,+ // unclosed or empty quotes, a quote glued to the next word.+ CHECK(ParseInstallRequests("cancel local-" + uuid + "\nlocal-XYZ\nlocal-" + uuid + " extra\n"+ "import usb0 Delores\nimport usb0 Delores a.exe b.exe\nimport usb0 \"Delores a.exe\n"+ "import usb0 \"\" a.exe\nimport usb0 \"Del\"ores a.exe\nimport\n").empty());+ }std::string many_requests;for (int app = 1; app <= 70; ++app) many_requests += std::to_string(app) + "\n";CHECK(ParseInstallRequests(many_requests).size() == 64);diff --git a/tests/local_copy_test.cpp b/tests/local_copy_test.cppnew file mode 100644index 0000000..7d31ac5--- /dev/null+++ b/tests/local_copy_test.cpp@@ -0,0 +1,797 @@+// "Copy into Playden" and "Move into Playden" (import-games Tasks 4.1-4.4): the copy plan of an imported game's folder,+// and the install service's Local runner copying it into a managed install, with the Local+// source's request, the ImportStore states, resume, cancel, uninstall and the restart reconcile.+// Built twice: with the host's descriptor walk, and with the console's walk by path.+#include "installs/install_service.h"+#include "platform/storage.h"+#include "stores/local/copy_plan.h"+#include "stores/local/copy_runner.h"+#include "stores/local/import_store.h"+#include "stores/local/library_source.h"++#include <algorithm>+#include <atomic>+#include <chrono>+#include <cstdio>+#include <cstdlib>+#include <fcntl.h>+#include <filesystem>+#include <fstream>+#include <functional>+#include <iterator>+#include <memory>+#include <mutex>+#include <set>+#include <string>+#include <sys/stat.h>+#include <thread>+#include <unistd.h>+#include <vector>++using namespace playden;+using namespace playden::stores::local;+using installs::InstallJobState;+using installs::InstallService;+using installs::InstallVolume;+namespace fs = std::filesystem;++#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #x); std::exit(1); } } while (0)++namespace {++void Write(const fs::path& path, const std::string& bytes) {+ fs::create_directories(path.parent_path());+ std::ofstream(path, std::ios::binary | std::ios::trunc) << bytes;+}+std::string Read(const fs::path& path) {+ std::ifstream in(path, std::ios::binary);+ return {std::istreambuf_iterator<char>(in), {}};+}+bool Has(const std::string& text, const std::string& part) { return text.find(part) != std::string::npos; }++void WaitFor(const std::function<bool()>& done, const char* what) {+ const auto until = std::chrono::steady_clock::now() + std::chrono::seconds(20);+ while (!done()) {+ if (std::chrono::steady_clock::now() > until) {+ std::fprintf(stderr, "timed out waiting for %s\n", what);+ std::exit(1);+ }+ std::this_thread::sleep_for(std::chrono::milliseconds(5));+ }+}++// Every regular file under `root`, relative, with its bytes (links and the folders left out).+std::vector<std::pair<std::string, std::string>> Files(const fs::path& root) {+ std::vector<std::pair<std::string, std::string>> files;+ for (auto it = fs::recursive_directory_iterator(root); it != fs::recursive_directory_iterator(); ++it)+ if (it->is_regular_file() && !it->is_symlink()) files.emplace_back(fs::relative(it->path(), root).string(), Read(it->path()));+ std::sort(files.begin(), files.end());+ return files;+}++// A game with nested assets: deeper than the scanner looks, hidden files, an empty file and+// folder, a name JSON must escape, and files bigger than the copy buffer.+void MakeGame(const fs::path& game) {+ Write(game / "Bin/Rally.exe", std::string(70000, 'x'));+ std::string big(3 * 1024 * 1024 + 17, '\0');+ for (std::size_t i = 0; i < big.size(); ++i) big[i] = static_cast<char>(i * 31 + 7);+ Write(game / "data/big.pak", big);+ Write(game / "data/a/b/c/d/e/f/g/h/i/j/deep.pak", "deep");+ Write(game / ".hidden/settings.ini", "[video]\nfullscreen=1\n");+ Write(game / "Caf\xc3\xa9 \"quoted\" name.txt", "unicode");+ Write(game / "empty.txt", "");+ fs::create_directories(game / "saves");+}++constexpr const char* kId = "1b4e28ba-2fa1-41d2-883f-0016d3cca427";+constexpr std::uint32_t kNumber = 0xF0000001u;++struct Fixture {+ fs::path root;+ fs::path source; // the game on the drive+ fs::path games; // the internal volume's games folder+ std::unique_ptr<Storage> storage;+ std::unique_ptr<ImportStore> store;+ LocalCopyOptions options;+ std::unique_ptr<LocalCopyRunner> runner;+ std::unique_ptr<InstallService> service;+ std::unique_ptr<LocalLibrarySource> local;+ std::vector<std::string> log;+ std::mutex log_mutex;+ int next_id = 0;++ Fixture() {+ char name[] = "/tmp/playden-local-copy-XXXXXX";+ CHECK(mkdtemp(name));+ root = fs::canonical(name); // /tmp is a link on macOS; the walk never follows one+ source = root / "mnt/usb0/playden/import/Rally";+ games = root / "data/playden-games";+ fs::create_directories(games);+ fs::create_directories(root / "state");+ CHECK(::chmod((root / "state").c_str(), 0700) == 0); // private storage+ MakeGame(source);+ options.log = [this](const std::string& line) {+ std::lock_guard<std::mutex> lock(log_mutex);+ log.push_back(line);+ };+ }+ ~Fixture() {+ Stop();+ std::error_code ignored;+ fs::remove_all(root, ignored);+ }+ // Playden starting: imports.json, the runner, the install service and the Local source.+ void Start(bool with_local = true) {+ storage = std::make_unique<Storage>((root / "state").string());+ // kId first, then others (the store asks again for an ID in use).+ store = std::make_unique<ImportStore>(*storage, "imports.json", [this]() -> std::optional<std::string> {+ static const char* const kIds[] = {kId, "6fa459ea-ee8a-4ca4-894e-db77e160355e", "16fd2706-8baf-433b-82eb-8c7fada847da"};+ if (next_id >= 3) return std::nullopt;+ return std::string(kIds[next_id++]);+ }, [] { return std::uint64_t{1760000000}; });+ runner = std::make_unique<LocalCopyRunner>(*store, options);+ service = std::make_unique<InstallService>(InstallService::Runners{{Store::kLocal, runner.get()}},+ std::vector<InstallVolume>{{"internal", "Console storage", games.string(), true}});+ service->SetRetryDelays(std::chrono::milliseconds(10), std::chrono::milliseconds(20));+ if (!with_local) {+ CHECK(store->Load().ok());+ return;+ }+ LocalLibraryConfig config;+ config.volumes = {{"internal", "/data", "playden-import", false}, {"usb0", "/mnt/usb0", "playden/import", false}};+ config.host_prefix = root.string();+ config.present = [](const ImportVolume&) { return true; };+ config.log = options.log;+ config.copy_into_playden = [this](const ImportEntry& entry, const std::string& from, const std::string& volume,+ std::uint32_t number) { return runner->Enqueue(*service, entry, from, volume, number); };+ config.move_into_playden = [this](const ImportEntry& entry, const std::string& from, std::uint32_t number) {+ return runner->Move(*service, entry, from, number);+ };+ local = std::make_unique<LocalLibrarySource>(*store, std::move(config));+ runner->SetChanged([source = local.get()] { source->Reload(); });+ WaitFor([&] { return local->Snapshot()->loaded || !local->Snapshot()->load_error.empty(); }, "imports loaded");+ if (!local->Snapshot()->loaded) std::fprintf(stderr, "load: %s\n", local->Snapshot()->load_error.c_str());+ CHECK(local->Snapshot()->loaded);+ service->RetryWaiting(Store::kLocal);+ }+ // Playden closing (or stopping at once: the install service first, as the controller does).+ void Stop() {+ if (service) service->Shutdown();+ if (runner) runner->SetChanged({});+ local.reset();+ service.reset();+ runner.reset();+ store.reset();+ storage.reset();+ }+ fs::path Dest() const { return games / ("local-" + std::string(kId)); }+ GameId Game() const { return GameId::Local(kId); }+ ImportEntry Add() {+ const auto added = store->Add("usb0", "playden/import/Rally", "Rally", "Bin/Rally.exe");+ CHECK(added.ok());+ if (local) local->Reload();+ return added.value();+ }+ // A game copied over FTP into the drop folder (same file system as the games folder).+ fs::path Drop() const { return root / "data/playden-import/Rally"; }+ ImportEntry AddDrop() {+ fs::create_directories(Drop().parent_path());+ fs::rename(source, Drop());+ const auto added = store->Add("internal", "playden-import/Rally", "Rally", "Bin/Rally.exe");+ CHECK(added.ok());+ if (local) local->Reload();+ return added.value();+ }+ ImportOutcome Outcome(std::uint64_t request) {+ ImportOutcome found;+ WaitFor([&] {+ const auto snapshot = local->Snapshot();+ for (const auto& outcome : snapshot->outcomes)+ if (outcome.request == request) { found = outcome; return true; }+ return false;+ }, "an outcome");+ return found;+ }+ std::optional<installs::InstallJobView> Job() {+ const auto snapshot = service->Snapshot();+ for (const auto& job : snapshot.jobs)+ if (job.id == Game()) return job;+ return std::nullopt;+ }+ bool Installed() {+ const auto snapshot = service->Snapshot();+ return std::any_of(snapshot.installed.begin(), snapshot.installed.end(),+ [&](const installs::InstalledGame& game) { return game.id == Game() && game.available; });+ }+ ImportState State() {+ const auto entry = store->Find(kId);+ CHECK(entry.has_value());+ return entry->state;+ }+ bool Logged(const std::string& part) {+ std::lock_guard<std::mutex> lock(log_mutex);+ return std::any_of(log.begin(), log.end(), [&](const std::string& line) { return Has(line, part); });+ }+};++void PlanCoversTheTree() {+ Fixture fixture;+ const auto build = BuildCopyPlan(fixture.source.string());+ CHECK(build.ok);+ const auto& plan = build.plan;+ CHECK(plan.source == fixture.source.string());+ std::set<std::string> files, dirs(plan.dirs.begin(), plan.dirs.end());+ std::uint64_t total = 0;+ for (const auto& file : plan.files) {+ files.insert(file.path);+ total += file.bytes;+ }+ CHECK(files.size() == 6 && plan.files.size() == 6);+ CHECK(files.count("Bin/Rally.exe") && files.count("data/a/b/c/d/e/f/g/h/i/j/deep.pak") &&+ files.count(".hidden/settings.ini") && files.count("empty.txt") && files.count("Caf\xc3\xa9 \"quoted\" name.txt"));+ CHECK(dirs.count("saves") && dirs.count("data/a/b/c/d/e/f/g/h/i/j") && dirs.count(".hidden"));+ CHECK(plan.total == total && total == 70000 + 3 * 1024 * 1024 + 17 + 4 + 21 + 7);+ // Parents come before their children.+ for (std::size_t i = 0; i < plan.dirs.size(); ++i) {+ const auto slash = plan.dirs[i].find_last_of('/');+ if (slash == std::string::npos) continue;+ const auto parent = std::find(plan.dirs.begin(), plan.dirs.end(), plan.dirs[i].substr(0, slash));+ CHECK(parent != plan.dirs.end() && parent < plan.dirs.begin() + static_cast<long>(i));+ }++ // Saved before the job is queued, and read back the same; the file list names every path.+ auto saved = plan;+ saved.program = "Bin/Rally.exe";+ saved.runtime_number = kNumber;+ const auto dest = fixture.Dest().string();+ CHECK(SaveCopyPlan(saved, dest).empty());+ CHECK(fs::is_regular_file(fixture.Dest() / ".playden-copy/plan.json"));+ const auto text = Read(fixture.Dest() / ".playden-copy/plan.json");+ CHECK(text.rfind("{\"version\":1,\"source\":\"", 0) == 0 && Has(text, "\"runtime_number\":4026531841"));+ const auto loaded = LoadCopyPlan(dest);+ CHECK(loaded && loaded->source == saved.source && loaded->program == saved.program &&+ loaded->runtime_number == kNumber && loaded->total == saved.total && loaded->dirs == saved.dirs &&+ loaded->files.size() == saved.files.size());+ for (std::size_t i = 0; i < saved.files.size(); ++i)+ CHECK(loaded->files[i].path == saved.files[i].path && loaded->files[i].bytes == saved.files[i].bytes &&+ loaded->files[i].mtime == saved.files[i].mtime && loaded->files[i].mtime_nsec == saved.files[i].mtime_nsec);+ const auto list = Read(fixture.Dest() / ".playden-files");+ CHECK(Has(list, "f\tBin/Rally.exe\n") && Has(list, "d\tdata/a/b/c/d/e/f/g/h/i/j\n"));+ // A cut or edited plan doesn't load.+ Write(fixture.Dest() / ".playden-copy/plan.json", text.substr(0, text.size() - 4));+ CHECK(!LoadCopyPlan(dest));+ std::string edited = text;+ edited.replace(edited.find("\"bytes\":"), 9, "\"bytes\":9");+ Write(fixture.Dest() / ".playden-copy/plan.json", edited);+ CHECK(!LoadCopyPlan(dest));+ // RemoveManagedInstall knows the copy journal folder: a partial copy is Playden's own.+ CHECK(fs::exists(fixture.Dest()));++ // Limits: files and bytes.+ const auto few = BuildCopyPlan(fixture.source.string(), 3);+ CHECK(!few.ok && Has(few.failure, "more than 3 files"));+ const auto small = BuildCopyPlan(fixture.source.string(), kMaxCopyFiles, 1024);+ CHECK(!small.ok && Has(small.failure, "larger than"));+ const auto missing = BuildCopyPlan((fixture.root / "mnt/usb0/playden/import/None").string());+ CHECK(!missing.ok && Has(missing.failure, "not there"));+}++void LinksAreRefused() {+ Fixture fixture;+ // A link to a file inside, one to a folder inside, and one leaving: each fails, naming itself.+ fs::create_symlink("../Bin/Rally.exe", fixture.source / "data/game.lnk");+ auto build = BuildCopyPlan(fixture.source.string());+ CHECK(!build.ok && Has(build.failure, "\"data/game.lnk\"") && Has(build.failure, "link"));+ fs::remove(fixture.source / "data/game.lnk");+ fs::create_directory_symlink("a/b", fixture.source / "data/shortcut");+ build = BuildCopyPlan(fixture.source.string());+ CHECK(!build.ok && Has(build.failure, "\"data/shortcut\""));+ fs::remove(fixture.source / "data/shortcut");+ fs::create_directory_symlink("/etc", fixture.source / "escape");+ build = BuildCopyPlan(fixture.source.string());+ CHECK(!build.ok && Has(build.failure, "\"escape\""));+ fs::remove(fixture.source / "escape");+ CHECK(BuildCopyPlan(fixture.source.string()).ok);+ // The folder itself a link.+ fs::create_directory_symlink(fixture.source, fixture.root / "mnt/usb0/playden/import/Alias");+ CHECK(!BuildCopyPlan((fixture.root / "mnt/usb0/playden/import/Alias").string()).ok);++ // Through the Local source: refused, nothing queued, nothing left in Playden's games folder.+ fs::create_symlink("Bin/Rally.exe", fixture.source / "Rally.lnk");+ fixture.Start();+ fixture.Add();+ const auto outcome = fixture.Outcome(fixture.local->CopyIntoPlayden(kId));+ CHECK(!outcome.ok && Has(outcome.message, "\"Rally.lnk\""));+ CHECK(fixture.State() == ImportState::kInPlace && !fixture.Job() && !fs::exists(fixture.Dest()));+ // The move lists the files first too: refused before the rename.+ fs::remove(fixture.source / "Rally.lnk");+ fixture.AddDrop();+ const auto drop_id = fixture.store->FindByFolder("internal", "playden-import/Rally")->id;+ fs::create_symlink("Bin/Rally.exe", fixture.Drop() / "Rally.lnk");+ const auto moved = fixture.Outcome(fixture.local->MoveIntoPlayden(drop_id));+ CHECK(!moved.ok && Has(moved.message, "\"Rally.lnk\""));+ CHECK(fixture.store->Find(drop_id)->state == ImportState::kInPlace && fs::exists(fixture.Drop() / "Bin/Rally.exe"));+ CHECK(!fs::exists(fixture.games / ("local-" + drop_id)));+}++// A game set up to play from its drive folder: its PS5 title follows the verified copy before the+// install record is written; while the title runs the job waits, the drive folder still active.+void CopyRelocatesItsTitle() {+ Fixture fixture;+ std::mutex mutex;+ std::vector<LocalRelocation> asked;+ std::atomic<bool> running{true};+ fixture.options.relocate = [&](const LocalRelocation& relocation) {+ {+ std::lock_guard<std::mutex> lock(mutex);+ asked.push_back(relocation);+ }+ LocalRelocationResult result;+ // The copy is complete and verified, but no install record yet.+ CHECK(fs::is_regular_file(fs::path(relocation.root) / "data/big.pak"));+ CHECK(!fs::exists(fs::path(relocation.root) / ".playden-install.json"));+ if (running) {+ result.outcome = LocalRelocationResult::Outcome::kBusy;+ result.message = "Close the game first.";+ }+ return result;+ };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId, "", kNumber)).ok);+ WaitFor([&] { std::lock_guard<std::mutex> lock(mutex); return asked.size() >= 2; }, "the relocation asked again");+ WaitFor([&] {+ const auto job = fixture.Job();+ CHECK(job && job->state != InstallJobState::kFailed);+ return Has(job->failure, "Close the game first.");+ }, "the job waiting for the game to close");+ CHECK(!fixture.Installed() && fixture.State() == ImportState::kCopying);+ CHECK(fs::exists(fixture.Dest() / ".playden-copy/plan.json"));+ {+ std::lock_guard<std::mutex> lock(mutex);+ CHECK(asked[0].game == fixture.Game() && asked[0].runtime_number == kNumber &&+ asked[0].root == fixture.Dest().string() && !asked[0].move_files);+ }+ running = false;+ WaitFor([&] { return fixture.Installed() && fixture.State() == ImportState::kCopied; }, "the relocated copy");+ CHECK(fs::exists(fixture.Dest() / ".playden-install.json") && !fs::exists(fixture.Dest() / ".playden-copy"));+ CHECK(Has(Read(fixture.Dest() / ".playden-install.json"), "\"app_id\":4026531841"));+ CHECK(fs::exists(fixture.source / "Bin/Rally.exe")); // the drive folder is never touched++ // A relocation that fails (the record could not be written) fails the job; Resume tries again.+ Fixture failing;+ failing.options.relocate = [](const LocalRelocation&) {+ LocalRelocationResult result;+ result.outcome = LocalRelocationResult::Outcome::kFailed;+ result.message = "The game's record could not be saved. Try again.";+ return result;+ };+ failing.Start();+ failing.Add();+ CHECK(failing.Outcome(failing.local->CopyIntoPlayden(kId, "", kNumber)).ok);+ WaitFor([&] { const auto job = failing.Job(); return job && job->state == InstallJobState::kFailed; }, "the failed relocation");+ CHECK(Has(failing.Job()->failure, "record could not be saved") && failing.State() == ImportState::kCopying);+ CHECK(!fs::exists(failing.Dest() / ".playden-install.json") && fs::exists(failing.Dest() / ".playden-copy/plan.json"));+}++// "Move into Playden": one rename (as root) of the drop folder into the games folder, under the+// relocation; the moved folder is a managed install that uninstalls like a copy.+void MovesDropFolderGame() {+ Fixture fixture;+ std::atomic<unsigned> raises{0};+ fixture.options.as_root = [&raises](const std::function<bool()>& work) { ++raises; return work(); };+ std::atomic<unsigned> relocations{0};+ fixture.options.relocate = [&](const LocalRelocation& relocation) {+ ++relocations;+ CHECK(relocation.game == fixture.Game() && relocation.runtime_number == kNumber && relocation.volume_id == "internal" &&+ relocation.root == fixture.Dest().string() && relocation.move_files);+ LocalRelocationResult result;+ result.message = relocation.move_files();+ if (!result.message.empty()) result.outcome = LocalRelocationResult::Outcome::kFailed;+ // Renamed, both parents synced, before the record follows.+ CHECK(fs::exists(fixture.Dest() / "Bin/Rally.exe") && !fs::exists(fixture.Drop()));+ return result;+ };+ fixture.Start();+ const auto before = Files(fixture.source);+ fixture.AddDrop();+ WaitFor([&] { return fixture.local->Locations().size() == 1; }, "the in-place location");+ const auto moved = fixture.Outcome(fixture.local->MoveIntoPlayden(kId, kNumber));+ if (!moved.ok) std::fprintf(stderr, "move: %s\n", moved.message.c_str());+ CHECK(moved.ok && relocations == 1 && raises == 1);+ CHECK(fixture.State() == ImportState::kCopied && fixture.Installed() && !fixture.Job());+ CHECK(!fs::exists(fixture.Drop()) && fs::exists(fixture.games / ".." / "playden-import"));+ auto after = Files(fixture.Dest());+ after.erase(std::remove_if(after.begin(), after.end(), [](const auto& file) {+ return file.first == ".playden-install.json" || file.first == ".playden-files";+ }), after.end());+ CHECK(after == before);+ CHECK(Has(Read(fixture.Dest() / ".playden-install.json"), "\"app_id\":4026531841"));+ CHECK(Has(Read(fixture.Dest() / ".playden-files"), "f\tdata/a/b/c/d/e/f/g/h/i/j/deep.pak\n"));+ CHECK(!fs::exists(fixture.Dest() / ".playden-copy"));+ WaitFor([&] { return fixture.local->Locations().empty(); }, "the in-place location gone");+ // Asked again: already in Playden.+ CHECK(!fixture.Outcome(fixture.local->MoveIntoPlayden(kId, kNumber)).ok);+ // Listed after a restart (the service's registry), and uninstalled like a copy.+ fixture.Stop();+ fixture.Start();+ CHECK(fixture.Installed() && fixture.State() == ImportState::kCopied);+ fixture.service->Uninstall(fixture.Game());+ WaitFor([&] { return !fs::exists(fixture.Dest()); }, "the moved game uninstalled");+ WaitFor([&] { return fixture.store->Removed().size() == 1; }, "removed from the library");++ // A game on a drive is copied, not moved.+ Fixture drive;+ drive.Start();+ drive.Add();+ const auto refused = drive.Outcome(drive.local->MoveIntoPlayden(kId, kNumber));+ CHECK(!refused.ok && Has(refused.message, "drop folder") && drive.State() == ImportState::kInPlace);+ CHECK(fs::exists(drive.source / "Bin/Rally.exe") && !fs::exists(drive.Dest()));+}++// While the game's title runs the relocation refuses before the rename: it stays in the drop folder.+void MoveRefusedWhileTheTitleRuns() {+ Fixture fixture;+ std::atomic<bool> ran{false};+ fixture.options.relocate = [&](const LocalRelocation&) {+ LocalRelocationResult result;+ result.outcome = LocalRelocationResult::Outcome::kBusy;+ result.message = "Close the game first.";+ ran = true;+ return result;+ };+ fixture.Start();+ fixture.AddDrop();+ const auto moved = fixture.Outcome(fixture.local->MoveIntoPlayden(kId, kNumber));+ CHECK(ran && !moved.ok && moved.message == "Close the game first.");+ CHECK(fixture.State() == ImportState::kInPlace && fixture.store->Find(kId)->failure.empty());+ CHECK(fs::exists(fixture.Drop() / "Bin/Rally.exe") && !fs::exists(fixture.Dest()) && !fixture.Installed());+ // A folder in the way refuses the move before anything changes.+ fixture.options.relocate = {};+ fs::create_directories(fixture.Dest());+ Write(fixture.Dest() / "mine.txt", "player's");+ fixture.Stop();+ fixture.Start();+ const auto blocked = fixture.Outcome(fixture.local->MoveIntoPlayden(kId, kNumber));+ CHECK(!blocked.ok && Has(blocked.message, "in the way") && fixture.State() == ImportState::kInPlace);+ CHECK(Read(fixture.Dest() / "mine.txt") == "player's" && fs::exists(fixture.Drop() / "Bin/Rally.exe"));+}++// Restarts during a move: after the rename (the title's record not written), and after the install+// record (imports.json not updated). Each start finishes it; none puts it back "in place".+void MoveFinishesAfterRestart() {+ Fixture fixture;+ // The rename runs, then Playden stops before the title's record follows.+ fixture.options.relocate = [](const LocalRelocation& relocation) {+ LocalRelocationResult result;+ CHECK(relocation.move_files().empty());+ result.outcome = LocalRelocationResult::Outcome::kFailed;+ result.message = "stopped";+ return result;+ };+ fixture.Start();+ fixture.AddDrop();+ const auto first = fixture.Outcome(fixture.local->MoveIntoPlayden(kId, kNumber));+ CHECK(!first.ok && fixture.State() == ImportState::kCopying);+ CHECK(!fs::exists(fixture.Drop()) && fs::exists(fixture.Dest() / "Bin/Rally.exe") && !fixture.Installed());+ CHECK(!fs::exists(fixture.Dest() / ".playden-install.json"));+ fixture.Stop();+ // The next start: the relocation is asked again (the rename found done), then the markers.+ std::atomic<unsigned> asked{0};+ fixture.options.relocate = [&asked](const LocalRelocation& relocation) {+ ++asked;+ LocalRelocationResult result;+ result.message = relocation.move_files();+ if (!result.message.empty()) result.outcome = LocalRelocationResult::Outcome::kFailed;+ return result;+ };+ // ...and stops again after the install record, before imports.json says copied.+ fixture.options.before_finish = [](const GameId&, const std::string&) { return false; };+ fixture.Start();+ auto* service = fixture.service.get();+ std::vector<GameId> numbered;+ fixture.local->Reconcile([service] { return service->Snapshot(); }, [service](const GameId& id) { service->Cancel(id); },+ [&numbered](const GameId& id) { numbered.push_back(id); return kNumber; });+ WaitFor([&] { return fixture.Installed(); }, "the move finished as a managed install");+ WaitFor([&] { return fixture.Logged("import move reconciled id=" + std::string(kId)); }, "the move reconciled");+ CHECK(asked == 1 && numbered.size() == 1 && numbered[0] == fixture.Game());+ CHECK(fixture.State() == ImportState::kCopying && fs::exists(fixture.Dest() / ".playden-install.json"));+ CHECK(Has(Read(fixture.Dest() / ".playden-install.json"), "\"app_id\":4026531841"));+ CHECK(!Has(Read(fixture.Dest() / ".playden-files"), ".playden-install.json"));+ fixture.Stop();+ fixture.options.before_finish = {};+ fixture.Start();+ service = fixture.service.get();+ fixture.local->Reconcile([service] { return service->Snapshot(); }, [service](const GameId& id) { service->Cancel(id); });+ WaitFor([&] { return fixture.State() == ImportState::kCopied; }, "copied after the restart");+ CHECK(fixture.Installed() && asked == 1);++ // A move cut off before its rename goes back in place.+ Fixture early;+ early.Start();+ early.AddDrop();+ CHECK(early.store->QueueCopy(kId).ok() && early.store->StartCopy(kId).ok());+ auto* early_service = early.service.get();+ early.local->Reconcile([early_service] { return early_service->Snapshot(); },+ [early_service](const GameId& id) { early_service->Cancel(id); });+ WaitFor([&] { return early.State() == ImportState::kInPlace; }, "back in place");+ CHECK(fs::exists(early.Drop() / "Bin/Rally.exe") && !fs::exists(early.Dest()));+}++void CopiesIntoPlayden() {+ Fixture fixture;+ const auto before = Files(fixture.source);+ fixture.Start();+ fixture.Add();+ // Playing in place until it's copied: an external location.+ WaitFor([&] { return fixture.local->Locations().size() == 1; }, "the in-place location");+ CHECK(!fixture.local->Locations()[0].managed());+ std::vector<std::uint64_t> seen;+ const auto request = fixture.local->CopyIntoPlayden(kId, "", kNumber);+ const auto outcome = fixture.Outcome(request);+ CHECK(outcome.ok);+ WaitFor([&] {+ if (const auto job = fixture.Job(); job && job->total_bytes) seen.push_back(job->done_bytes);+ return fixture.Installed();+ }, "the copy");+ WaitFor([&] { return fixture.State() == ImportState::kCopied; }, "copied");+ // The copy is the folder, byte for byte; the source is untouched.+ auto copied = Files(fixture.Dest());+ copied.erase(std::remove_if(copied.begin(), copied.end(), [](const auto& file) {+ return file.first == ".playden-install.json" || file.first == ".playden-files";+ }), copied.end());+ CHECK(copied == before);+ CHECK(Files(fixture.source) == before);+ CHECK(fs::is_directory(fixture.Dest() / "saves"));+ CHECK(!fs::exists(fixture.Dest() / ".playden-copy"));+ for (const auto& file : Files(fixture.Dest())) CHECK(!Has(file.first, ".part"));+ // Recorded as the depot installer records another store's game, with the runtime number.+ const auto record = Read(fixture.Dest() / ".playden-install.json");+ CHECK(record.rfind("{\"version\":1,\"app_id\":4026531841,\"store\":\"local\",\"game\":\"" + std::string(kId) ++ "\",\"install_bytes\":" + std::to_string(70000 + 3 * 1024 * 1024 + 17 + 4 + 21 + 7) + ",",+ 0) == 0);+ const auto snapshot = fixture.service->Snapshot();+ CHECK(snapshot.installed.size() == 1 && snapshot.installed[0].install_bytes == 70000 + 3 * 1024 * 1024 + 17 + 4 + 21 + 7);+ CHECK(snapshot.installed[0].root == fixture.Dest().string() && snapshot.jobs.empty());+ // Progress went up in bytes.+ CHECK(std::is_sorted(seen.begin(), seen.end()));+ // The location is managed now: the Local source no longer offers the drive folder.+ WaitFor([&] { return fixture.local->Locations().empty(); }, "the external location gone");+ const auto projected = installs::ProjectInstalledLocations(snapshot.installed, fixture.local->Locations());+ CHECK(projected.size() == 1 && projected[0].managed() && projected[0].root == fixture.Dest().string());+ // A copied game isn't copied again.+ CHECK(!fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);++ // Uninstall: the files go, and the entry leaves the library for the removed list.+ fixture.service->Uninstall(fixture.Game());+ WaitFor([&] { return !fs::exists(fixture.Dest()); }, "the files removed");+ WaitFor([&] { return fixture.local->Snapshot()->games.empty(); }, "the game out of the library");+ CHECK(!fixture.store->Find(kId) && fixture.store->Removed().size() == 1 && fixture.store->Removed()[0].id == kId);+ CHECK(Files(fixture.source) == before);+}++void ResumesAfterPauseAndRestart() {+ Fixture fixture;+ std::atomic<int> copied{0};+ std::atomic<bool> pause{true};+ fixture.options.buffer_bytes = 4096;+ fixture.options.after_file = [&](const std::string&) {+ ++copied;+ if (pause.exchange(false)) fixture.service->Pause(GameId::Local(kId));+ };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kPaused; }, "paused");+ CHECK(fixture.State() == ImportState::kCopying);+ const int first = copied.load();+ CHECK(first >= 1 && first < 6);+ CHECK(fs::exists(fixture.Dest() / ".playden-copy/plan.json"));+ // Playden restarts: the paused copy comes back paused, and Resume copies only what is left.+ fixture.Stop();+ fixture.Start();+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kPaused; }, "restored paused");+ CHECK(fixture.State() == ImportState::kCopying);+ fixture.service->Resume(fixture.Game());+ WaitFor([&] { return fixture.Installed(); }, "the resumed copy");+ WaitFor([&] { return fixture.State() == ImportState::kCopied; }, "copied");+ CHECK(copied.load() == 6); // each file once+ CHECK(Read(fixture.Dest() / "data/big.pak") == Read(fixture.source / "data/big.pak"));+}++void CancelLeavesNothing() {+ Fixture fixture;+ std::atomic<bool> cancel{true};+ fixture.options.after_file = [&](const std::string&) {+ if (cancel.exchange(false)) fixture.service->Cancel(GameId::Local(kId));+ };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { return !fixture.Job() && !fs::exists(fixture.Dest()); }, "the cancelled copy removed");+ WaitFor([&] { return fixture.State() == ImportState::kInPlace; }, "back in place");+ CHECK(fixture.store->Find(kId)->failure.empty());+ CHECK(!fixture.Installed());+ // Copied again after the cancel: from the start, to the end.+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { return fixture.Installed() && fixture.State() == ImportState::kCopied; }, "the second copy");+}++void ChangedSourceFails() {+ Fixture fixture;+ std::atomic<bool> grow{true};+ std::string first;+ fixture.options.after_file = [&](const std::string& path) {+ if (!grow.exchange(false)) return;+ first = path;+ // Every other file grows (one is still to be copied).+ for (auto it = fs::recursive_directory_iterator(fixture.source); it != fs::recursive_directory_iterator(); ++it)+ if (it->is_regular_file() && fs::relative(it->path(), fixture.source).string() != path)+ std::ofstream(it->path(), std::ios::binary | std::ios::app) << "more";+ };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "the failure");+ CHECK(fixture.Job()->failure == kCopyChangedFailure);+ // The partial copy is gone, and the game plays in place again with the failure on its entry.+ CHECK(!fs::exists(fixture.Dest()));+ CHECK(fixture.State() == ImportState::kInPlace && fixture.store->Find(kId)->failure == kCopyChangedFailure);+ // Resume can't carry on; cancelling clears Downloads.+ fixture.service->Resume(fixture.Game());+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "failed again");+ fixture.service->Cancel(fixture.Game());+ WaitFor([&] { return !fixture.Job(); }, "the job gone");+ CHECK(fixture.State() == ImportState::kInPlace && !fs::exists(fixture.Dest()));++ // A file removed after the plan: the same failure.+ std::atomic<bool> remove{true};+ fixture.Stop();+ fixture.options.after_file = [&](const std::string& path) {+ if (!remove.exchange(false)) return;+ for (auto it = fs::recursive_directory_iterator(fixture.source); it != fs::recursive_directory_iterator(); ++it)+ if (it->is_regular_file() && fs::relative(it->path(), fixture.source).string() != path) {+ fs::remove(it->path());+ break;+ }+ };+ fixture.Start();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "the second failure");+ CHECK(fixture.Job()->failure == kCopyChangedFailure && !fs::exists(fixture.Dest()));+}++void ProgramMissingIsRefused() {+ Fixture fixture;+ fixture.Start(false);+ // The entry's program isn't in the folder: nothing is queued or written.+ const auto added = fixture.store->Add("usb0", "playden/import/Rally", "Rally", "Bin/Missing.exe");+ CHECK(added.ok());+ const auto failure = fixture.runner->Enqueue(*fixture.service, added.value(), fixture.source.string(), "", kNumber);+ CHECK(Has(failure, "program"));+ CHECK(fixture.State() == ImportState::kInPlace && !fixture.Job() && !fs::exists(fixture.Dest()));++ // The program in the plan but not in the copy (a plan that doesn't list it): verify refuses it.+ CHECK(fixture.store->SetProgram(kId, "Bin/Rally.exe").ok());+ auto plan = BuildCopyPlan(fixture.source.string());+ CHECK(plan.ok);+ plan.plan.program = "Bin/Rally.exe";+ plan.plan.files.erase(std::remove_if(plan.plan.files.begin(), plan.plan.files.end(), [](const CopyFile& file) {+ return file.path == "Bin/Rally.exe";+ }), plan.plan.files.end());+ plan.plan.total -= 70000;+ CHECK(SaveCopyPlan(plan.plan, fixture.Dest().string()).empty());+ CHECK(fixture.store->QueueCopy(kId).ok());+ fixture.service->Install(fixture.Game(), plan.plan.total);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "the verify failure");+ CHECK(Has(fixture.Job()->failure, "program is missing"));+ CHECK(fixture.State() == ImportState::kInPlace && !fs::exists(fixture.Dest()));+}++void CrashBetweenCommitSteps() {+ Fixture fixture;+ // Playden stops after the install record, before imports.json says copied.+ fixture.options.before_finish = [](const GameId&, const std::string&) { return false; };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { return fixture.Installed(); }, "the managed install");+ CHECK(fixture.State() == ImportState::kCopying);+ fixture.Stop();+ // The next start lists the managed install; the reconcile commits the entry.+ fixture.options.before_finish = {};+ fixture.Start();+ CHECK(fixture.Installed() && fixture.State() == ImportState::kCopying);+ auto* service = fixture.service.get();+ fixture.local->Reconcile([service] { return service->Snapshot(); }, [service](const GameId& id) { service->Cancel(id); });+ WaitFor([&] { return fixture.State() == ImportState::kCopied; }, "reconciled to copied");+ CHECK(fixture.Logged("import copy reconciled id=" + std::string(kId) + " copied"));+ WaitFor([&] { return fixture.local->Locations().empty(); }, "the external location gone");+ CHECK(fs::exists(fixture.Dest() / "Bin/Rally.exe"));+}++void ReconcileWithoutJob() {+ Fixture fixture;+ fixture.Start();+ fixture.Add();+ // imports.json says copying, but the service has neither a job nor an install (its registry lost).+ CHECK(fixture.store->QueueCopy(kId).ok() && fixture.store->StartCopy(kId).ok());+ auto* service = fixture.service.get();+ fixture.local->Reconcile([service] { return service->Snapshot(); }, [service](const GameId& id) { service->Cancel(id); });+ WaitFor([&] { return fixture.State() == ImportState::kInPlace; }, "back in place");+ CHECK(fixture.store->Find(kId)->failure == kCopyStoppedFailure);+ // A copy job whose game is back in place leaves Downloads.+ fixture.service->Install(fixture.Game(), 1);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "the stale job");+ fixture.local->Reconcile([service] { return service->Snapshot(); }, [service](const GameId& id) { service->Cancel(id); });+ WaitFor([&] { return !fixture.Job(); }, "the stale job cancelled");+}++void FreeSpaceRefused() {+ Fixture fixture;+ // At the start (and on resume): the runner checks what is left against the drive.+ std::atomic<std::uint64_t> free_bytes{1024};+ fixture.options.space = [&](const std::string&) {+ return std::optional<steam::VolumeSpace>(steam::VolumeSpace{1ull << 40, free_bytes.load()});+ };+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { const auto job = fixture.Job(); return job && job->state == InstallJobState::kFailed; }, "no space");+ CHECK(Has(fixture.Job()->failure, "Not enough free space"));+ // Not a lost copy: the plan stays and Resume carries on once there's room.+ CHECK(fixture.State() == ImportState::kCopying && fs::exists(fixture.Dest() / ".playden-copy/plan.json"));+ free_bytes = 1ull << 40;+ fixture.service->Resume(fixture.Game());+ WaitFor([&] { return fixture.Installed() && fixture.State() == ImportState::kCopied; }, "the copy with room");+ fixture.service->Uninstall(fixture.Game());+ WaitFor([&] { return !fs::exists(fixture.Dest()); }, "uninstalled");+ WaitFor([&] { return fixture.store->Removed().size() == 1; }, "removed");++ // At the enqueue: the install service's check of the plan's total. A sparse file larger than+ // the drive's free space (when that is under the plan limit) is refused before anything copies.+ const auto space = steam::QueryVolumeSpace(fixture.games.string());+ if (!space || space->free_bytes + (1ull << 30) >= kMaxCopyBytes) {+ std::fprintf(stderr, "skipped: enqueue free-space refusal (free space unknown or above 1 TiB)\n");+ return;+ }+ const auto huge = fixture.root / "mnt/usb0/playden/import/Huge";+ Write(huge / "Huge.exe", "x");+ {+ const int fd = ::open((huge / "huge.pak").c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);+ CHECK(fd >= 0 && ::ftruncate(fd, static_cast<off_t>(space->free_bytes + (1ull << 30))) == 0);+ ::close(fd);+ }+ const auto added = fixture.store->Add("usb0", "playden/import/Huge", "Huge", "Huge.exe");+ CHECK(added.ok());+ const auto failure = fixture.runner->Enqueue(*fixture.service, added.value(), huge.string(), "", 0);+ CHECK(Has(failure, "Not enough free space"));+ CHECK(fixture.store->Find(added.value().id)->state == ImportState::kInPlace);+ CHECK(fixture.store->Find(added.value().id)->failure == failure);+ const auto dest = fixture.games / ("local-" + added.value().id);+ WaitFor([&] {+ const auto snapshot = fixture.service->Snapshot();+ return snapshot.jobs.empty() && !fs::exists(dest);+ }, "the refused copy gone");+}++} // namespace++int main() {+ PlanCoversTheTree();+ LinksAreRefused();+ CopiesIntoPlayden();+ ResumesAfterPauseAndRestart();+ CancelLeavesNothing();+ ChangedSourceFails();+ ProgramMissingIsRefused();+ CrashBetweenCommitSteps();+ ReconcileWithoutJob();+ FreeSpaceRefused();+ CopyRelocatesItsTitle();+ MovesDropFolderGame();+ MoveRefusedWhileTheTitleRuns();+ MoveFinishesAfterRestart();+ std::puts("local copy tests passed");+ return 0;+}diff --git a/tests/local_drop_folder_access_test.cpp b/tests/local_drop_folder_access_test.cppnew file mode 100644index 0000000..93499e1--- /dev/null+++ b/tests/local_drop_folder_access_test.cpp@@ -0,0 +1,236 @@+// The drop-folder walk: a game FTP copied in (root-owned 0755 folders, 0644 files) is opened to+// 0777/0666 with x kept, never past a link, resumably. The host can't chown, so "root-owned" is+// simulated: the walk is told the app user is someone else, and a fake as_root runs the work.+#include "stores/local/drop_folder_access.h"++#include <cstdio>+#include <cstdlib>+#include <filesystem>+#include <fstream>+#include <string>+#include <sys/stat.h>+#include <unistd.h>+#include <vector>++using namespace playden::stores::local;+#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #x); std::exit(1); } } while (0)++namespace fs = std::filesystem;+namespace {+// The console's walk (by path) can't read a link, so every link fails it there.+#if defined(PLAYDEN_PLATFORM_PS5) && PLAYDEN_PLATFORM_PS5+constexpr bool kConsole = true;+#else+constexpr bool kConsole = false;+#endif+unsigned Mode(const fs::path& path) {+ struct stat info{};+ CHECK(lstat(path.c_str(), &info) == 0);+ return info.st_mode & 07777;+}+void Write(const fs::path& path, unsigned mode) {+ fs::create_directories(path.parent_path());+ std::ofstream(path, std::ios::binary | std::ios::trunc) << "data";+ CHECK(chmod(path.c_str(), mode) == 0);+}+void Folder(const fs::path& path, unsigned mode) {+ fs::create_directories(path);+ CHECK(chmod(path.c_str(), mode) == 0);+}+bool Has(const std::string& text, const std::string& part) { return text.find(part) != std::string::npos; }+} // namespace++int main() {+ char name[] = "/tmp/playden-drop-folder-XXXXXX";+ CHECK(mkdtemp(name));+ // /tmp is a link on macOS; the walk never follows one.+ const fs::path root = fs::canonical(name);+ const fs::path drop = root / "data/playden-import";+ fs::create_directories(drop);+ CHECK(chmod(drop.c_str(), 0777) == 0);++ std::vector<std::string> lines;+ unsigned raises = 0;+ int refuse_after = -1; // as_root refuses from this call on (an interrupted walk)+ DropFolderAccessOptions options;+ options.namespace_root = root;+ options.log = [&lines](const std::string& line) { lines.push_back(line); };+ options.as_root = [&](const std::function<bool()>& work) {+ if (refuse_after >= 0 && static_cast<int>(raises) >= refuse_after) { ++raises; return false; }+ ++raises;+ return work();+ };+ // Every entry is someone else's (root's, as FTP writes it): changes go through as_root.+ options.app_uid = ::geteuid() + 1;++ // Only a folder directly inside /data/playden-import is walked.+ for (const char* outside : {"/data/playden-import", "/data/playden-import/", "/data/playden-import/a/b",+ "/data/playden-import/../playden/x", "/data/playden-import/.", "/data/playden-import/..",+ "/data/playden/x", "/mnt/usb0/playden/import/x", "data/playden-import/x",+ "/data/playden-import/x/", "/data/playden-import//x", ""}) {+ const auto refused = OpenDropFolderEntry(outside, options);+ CHECK(!refused.ok && Has(refused.message, "directly inside /data/playden-import") && refused.raises == 0);+ }+ CHECK(raises == 0);+ CHECK(IsDropFolderEntry("/data/playden-import/Night Sky") && !IsDropFolderEntry("/data/playden-import/a/b"));+ // A missing folder fails, naming it.+ {+ const auto missing = OpenDropFolderEntry("/data/playden-import/Missing", options);+ CHECK(!missing.ok && Has(missing.message, "/data/playden-import/Missing"));+ }++ // A root-owned tree as FTP leaves it: folders 0755, files 0644, the programs 0755/0744.+ const auto game = drop / "Game";+ Folder(game / "Bin", 0755);+ Folder(game / "Deep/a/b", 0755);+ Write(game / "Bin/Game.exe", 0755);+ Write(game / "Bin/run.sh", 0744);+ Write(game / "settings.ini", 0644);+ Write(game / "Deep/a/b/save.dat", 0640);+ CHECK(chmod(game.c_str(), 0755) == 0);+ // A file with a second name is left alone; so is a link that stays inside.+ Write(game / "shared.dat", 0644);+ CHECK(link((game / "shared.dat").c_str(), (game / "Deep/shared-again.dat").c_str()) == 0);+ if (kConsole) {+ // On the console a link fails the walk even inside the folder, named; removed, it goes on.+ CHECK(symlink("Bin", (game / "Programs").c_str()) == 0);+ const auto refused = OpenDropFolderEntry("/data/playden-import/Game", options);+ CHECK(!refused.ok && Has(refused.message, "/data/playden-import/Game/Programs"));+ CHECK(unlink((game / "Programs").c_str()) == 0);+ for (const auto& folder : {game, game / "Bin", game / "Deep", game / "Deep/a", game / "Deep/a/b"}) CHECK(chmod(folder.c_str(), 0755) == 0);+ for (const auto& file : {"settings.ini", "shared.dat"}) CHECK(chmod((game / file).c_str(), 0644) == 0);+ CHECK(chmod((game / "Bin/Game.exe").c_str(), 0755) == 0 && chmod((game / "Bin/run.sh").c_str(), 0744) == 0);+ CHECK(chmod((game / "Deep/a/b/save.dat").c_str(), 0640) == 0);+ raises = 0;+ } else {+ CHECK(symlink("../settings.ini", (game / "Bin/settings-link.ini").c_str()) == 0);+ CHECK(symlink("Bin", (game / "Programs").c_str()) == 0);+ }+ lines.clear();+ auto opened = OpenDropFolderEntry("/data/playden-import/Game", options);+ CHECK(opened.ok && opened.message.empty());+ for (const auto& folder : {game, game / "Bin", game / "Deep", game / "Deep/a", game / "Deep/a/b"}) CHECK(Mode(folder) == 0777);+ CHECK(Mode(game / "settings.ini") == 0666 && Mode(game / "Deep/a/b/save.dat") == 0666);+ CHECK(Mode(game / "Bin/Game.exe") == 0777 && Mode(game / "Bin/run.sh") == 0766);+ CHECK(Mode(game / "shared.dat") == 0644 && Mode(game / "Deep/shared-again.dat") == 0644 && opened.hard_linked == 2);+ CHECK(kConsole ? opened.links == 0+ : fs::is_symlink(game / "Bin/settings-link.ini") && fs::is_symlink(game / "Programs") && opened.links == 2);+ CHECK(opened.folders == 5 && opened.files == 6 && opened.changed == 9);+ // Root in short raises: batches of root-owned entries, never one for the whole tree.+ CHECK(opened.raises >= 1 && opened.raises == raises);+ CHECK(Has(lines.back(), "drop folder opened entry=/data/playden-import/Game"));+ bool logged_link = false;+ for (const auto& line : lines) logged_link = logged_link || Has(line, "skips /data/playden-import/Game/shared.dat");+ CHECK(logged_link);++ // Again on the open tree: nothing changes and root is never raised.+ raises = 0;+ opened = OpenDropFolderEntry("/data/playden-import/Game", options);+ CHECK(opened.ok && opened.changed == 0 && opened.raises == 0 && raises == 0 && Mode(game / "settings.ini") == 0666);++ // FTP added a file since: the next walk opens just that one.+ Write(game / "Deep/a/new.cfg", 0644);+ opened = OpenDropFolderEntry("/data/playden-import/Game", options);+ CHECK(opened.ok && opened.changed == 1 && opened.raises == 1 && Mode(game / "Deep/a/new.cfg") == 0666);++ // Entries the app user owns are changed without root.+ {+ auto own = options;+ own.app_uid.reset();+ raises = 0;+ CHECK(chmod((game / "settings.ini").c_str(), 0644) == 0 && chmod((game / "Bin").c_str(), 0755) == 0);+ opened = OpenDropFolderEntry("/data/playden-import/Game", own);+ CHECK(opened.ok && opened.changed == 2 && raises == 0 && Mode(game / "settings.ini") == 0666 && Mode(game / "Bin") == 0777);+ }++ // Without root, root-owned entries can't be opened: the walk fails naming the path.+ {+ auto rootless = options;+ rootless.as_root = nullptr;+ CHECK(chmod((game / "settings.ini").c_str(), 0644) == 0);+ opened = OpenDropFolderEntry("/data/playden-import/Game", rootless);+ CHECK(!opened.ok && Has(opened.message, "/data/playden-import/Game/settings.ini") && Mode(game / "settings.ini") == 0644);+ CHECK(OpenDropFolderEntry("/data/playden-import/Game", options).ok && Mode(game / "settings.ini") == 0666);+ }++ // A link that leads outside the game's folder fails the walk, and what it points at is untouched.+ {+ const auto escaping = drop / "Escaping";+ const auto outside = root / "outside";+ Folder(outside, 0755);+ Write(outside / "secret.txt", 0644);+ Write(escaping / "Game.exe", 0644);+ Folder(escaping / "Sub", 0755);+ CHECK(symlink("../../../outside", (escaping / "Sub/out").c_str()) == 0);+ const auto failed = OpenDropFolderEntry("/data/playden-import/Escaping", options);+ CHECK(!failed.ok && Has(failed.message, "/data/playden-import/Escaping/Sub/out") &&+ Has(failed.message, kConsole ? "can't be checked" : "outside"));+ CHECK(Mode(outside) == 0755 && Mode(outside / "secret.txt") == 0644);+ // Absolute targets too, unless they name a place inside the folder.+ CHECK(unlink((escaping / "Sub/out").c_str()) == 0);+ CHECK(symlink(outside.c_str(), (escaping / "Sub/out").c_str()) == 0);+ CHECK(!OpenDropFolderEntry("/data/playden-import/Escaping", options).ok && Mode(outside) == 0755);+ CHECK(unlink((escaping / "Sub/out").c_str()) == 0);+ CHECK(symlink("/data/playden-import/Escaping/Game.exe", (escaping / "Sub/out").c_str()) == 0);+ CHECK(symlink("/data/playden-import/EscapingNot/x", (escaping / "near").c_str()) == 0);+ const auto near = OpenDropFolderEntry("/data/playden-import/Escaping", options);+ CHECK(!near.ok && Has(near.message, kConsole ? "/data/playden-import/Escaping/" : "/data/playden-import/Escaping/near"));+ CHECK(unlink((escaping / "near").c_str()) == 0);+ if (kConsole) CHECK(unlink((escaping / "Sub/out").c_str()) == 0);+ CHECK(OpenDropFolderEntry("/data/playden-import/Escaping", options).ok && Mode(escaping / "Game.exe") == 0666);+ CHECK(Mode(outside) == 0755 && Mode(outside / "secret.txt") == 0644);+ // The game's folder itself a link: refused.+ CHECK(symlink(outside.c_str(), (drop / "Linked").c_str()) == 0);+ const auto linked = OpenDropFolderEntry("/data/playden-import/Linked", options);+ CHECK(!linked.ok && Has(linked.message, "link") && Mode(outside) == 0755);+ }++ // Interrupted part way (root refused after the first raise), then run again: the whole tree opens.+ {+ const auto big = drop / "Big";+ for (int folder = 0; folder < 4; ++folder) {+ Folder(big / ("f" + std::to_string(folder)), 0755);+ for (int file = 0; file < 5; ++file) Write(big / ("f" + std::to_string(folder)) / ("x" + std::to_string(file)), 0644);+ }+ CHECK(chmod(big.c_str(), 0755) == 0);+ auto small = options;+ small.batch = 3;+ raises = 0;+ refuse_after = 1;+ const auto stopped = OpenDropFolderEntry("/data/playden-import/Big", small);+ CHECK(!stopped.ok && Has(stopped.message, "/data/playden-import/Big") && raises == 2);+ unsigned open_files = 0;+ for (int folder = 0; folder < 4; ++folder)+ for (int file = 0; file < 5; ++file)+ open_files += Mode(big / ("f" + std::to_string(folder)) / ("x" + std::to_string(file))) == 0666;+ CHECK(open_files < 20);+ refuse_after = -1;+ const auto redone = OpenDropFolderEntry("/data/playden-import/Big", small);+ CHECK(redone.ok && redone.changed > 0);+ for (int folder = 0; folder < 4; ++folder) {+ CHECK(Mode(big / ("f" + std::to_string(folder))) == 0777);+ for (int file = 0; file < 5; ++file) CHECK(Mode(big / ("f" + std::to_string(folder)) / ("x" + std::to_string(file))) == 0666);+ }+ CHECK(Mode(big) == 0777);+ CHECK(OpenDropFolderEntry("/data/playden-import/Big", small).changed == 0);+ }++ // A file nobody may read, not even its owner: named, nothing else hidden.+ if (::geteuid() != 0) {+ const auto locked = drop / "Locked";+ Write(locked / "Game.exe", 0644);+ Write(locked / "sealed.bin", 0000);+ const auto failed = OpenDropFolderEntry("/data/playden-import/Locked", options);+ CHECK(!failed.ok && Has(failed.message, "/data/playden-import/Locked/sealed.bin"));+ CHECK(chmod((locked / "sealed.bin").c_str(), 0644) == 0);+ }++ // The callback form: empty on success, the sentence otherwise.+ const auto opener = MakeDropFolderOpener(options);+ CHECK(opener("/data/playden-import/Game").empty() && !opener("/data/playden/Game").empty());++ std::error_code ignored;+ fs::remove_all(root, ignored);+ std::puts("drop folder access tests passed");+ return 0;+}diff --git a/tests/local_import_store_test.cpp b/tests/local_import_store_test.cppnew file mode 100644index 0000000..d3d28f8--- /dev/null+++ b/tests/local_import_store_test.cpp@@ -0,0 +1,274 @@+#include "stores/local/import_store.h"++#include <cstdio>+#include <cstdlib>+#include <filesystem>+#include <memory>+#include <string>+#include <sys/stat.h>+#include <unistd.h>+#include <vector>++using namespace playden::stores::local;+namespace fs = std::filesystem;++#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #x); std::exit(1); } } while (0)++namespace {++struct TemporaryDirectory {+ TemporaryDirectory() {+ auto pattern = (fs::temp_directory_path() / "playden-imports-XXXXXX").string();+ std::vector<char> writable(pattern.begin(), pattern.end());+ writable.push_back('\0');+ CHECK(::mkdtemp(writable.data()) != nullptr);+ path = fs::canonical(writable.data()).string();+ }+ ~TemporaryDirectory() {+ std::error_code ignored;+ fs::remove_all(path, ignored);+ }+ std::string path;+};++// Fixed IDs, then none.+ImportStore::IdSource Ids(std::vector<std::string> ids) {+ auto shared = std::make_shared<std::vector<std::string>>(std::move(ids));+ return [shared]() -> std::optional<std::string> {+ if (shared->empty()) return std::nullopt;+ auto id = shared->front();+ shared->erase(shared->begin());+ return id;+ };+}+ImportStore::Clock Clock() { return [] { return std::uint64_t{1760000000}; }; }++constexpr const char* kA = "1b4e28ba-2fa1-41d2-883f-0016d3cca427";+constexpr const char* kB = "6fa459ea-ee8a-4ca4-894e-db77e160355e";+constexpr const char* kC = "16fd2706-8baf-433b-82eb-8c7fada847da";++std::string Text(const playden::Storage& storage) {+ const auto read = storage.ReadDurable("imports.json");+ CHECK(read.ok());+ return std::string(read.value().begin(), read.value().end());+}++void Write(const playden::Storage& storage, const std::string& text) {+ CHECK(storage.WriteDurable("imports.json", playden::Bytes(text.begin(), text.end())).ok());+}++void RandomIdsAreLowerCaseVersionFour() {+ const auto first = RandomImportId(), second = RandomImportId();+ CHECK(first && second && *first != *second);+ CHECK(IsImportId(*first) && (*first)[14] == '4');+ CHECK(std::string("89ab").find((*first)[19]) != std::string::npos);+ CHECK(!IsImportId("1B4E28BA-2FA1-41D2-883F-0016D3CCA427") && !IsImportId("1b4e28ba2fa141d2883f0016d3cca427"));+}++void AddFindAndRoundTrip() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ ImportStore store(storage, "imports.json", Ids({kA, kB}), Clock());+ CHECK(store.Add("usb0", "playden/import/Delores", "Delores", "Delores.exe").error() == ImportError::kNotLoaded);+ CHECK(store.Load().ok() && store.Games().empty()); // a missing file loads empty+ const auto added = store.Add("usb0", "playden/import/Delores", "Delores", "Delores.exe");+ CHECK(added.ok() && added.value().id == kA && added.value().state == ImportState::kInPlace);+ CHECK(added.value().added_at == 1760000000);+ const auto drop = store.Add("internal", "playden-import/Night Sky", "Night \"Sky\"\n", "NightSky.exe");+ CHECK(drop.ok() && drop.value().id == kB);+ CHECK(store.Find(kA)->folder == "playden/import/Delores");+ CHECK(store.FindByFolder("internal", "playden-import/Night Sky")->id == kB);+ CHECK(!store.FindByFolder("usb1", "playden/import/Delores")); // slots, not drives+ CHECK(!store.Find(kC));+ const auto file = Text(storage);+ CHECK(file.rfind("{\"version\":1,\"games\":[{\"id\":\"1b4e28ba-2fa1-41d2-883f-0016d3cca427\",\"title\":\"Delores\","+ "\"volume\":\"usb0\",\"folder\":\"playden/import/Delores\",\"program\":\"Delores.exe\","+ "\"state\":\"in_place\",\"added_at\":1760000000}", 0) == 0);++ ImportStore reloaded(storage, "imports.json", Ids({}), Clock());+ CHECK(reloaded.Load().ok());+ const auto games = reloaded.Games();+ CHECK(games.size() == 2 && games[0].id == kA && games[1].id == kB && games[1].title == "Night \"Sky\"\n");+ CHECK(reloaded.Save().ok() && Text(storage) == file); // byte-stable++ // No random source: nothing is added.+ CHECK(reloaded.Add("usb0", "playden/import/Other", "Other", "Other.exe").error() == ImportError::kNoRandom);+ CHECK(reloaded.Games().size() == 2);+}++void DuplicatesAndInvalidFieldsAreRefused() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ ImportStore store(storage, "imports.json", Ids({kA, kA, kB, kC}), Clock());+ CHECK(store.Load().ok());+ CHECK(store.Add("usb0", "playden/import/Delores", "Delores", "Delores.exe").ok());+ CHECK(store.Add("usb0", "playden/import/Delores", "Again", "Other.exe").error() == ImportError::kDuplicate);+ // A generated ID already in use is never handed out twice.+ const auto other = store.Add("usb1", "playden/import/Delores", "Delores", "Delores.exe");+ CHECK(other.ok() && other.value().id == kB);+ for (const auto& bad : std::vector<std::vector<std::string>>{+ {"", "f", "t", "p.exe"}, {"USB0", "f", "t", "p.exe"}, {"usb0", "/abs", "t", "p.exe"},+ {"usb0", "a/../b", "t", "p.exe"}, {"usb0", "f", "", "p.exe"}, {"usb0", "f", "t", "../p.exe"},+ {"usb0", "f", "t", "a\\p.exe"}, {"usb0", "f/", "t", "p.exe"}})+ CHECK(store.Add(bad[0], bad[1], bad[2], bad[3]).error() == ImportError::kInvalid);+ CHECK(store.Games().size() == 2);+}++void InPlaceRemoveAndRestoreFromRemoved() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ ImportStore store(storage, "imports.json", Ids({kA, kB}), Clock());+ CHECK(store.Load().ok());+ CHECK(store.Add("usb0", "playden/import/Celeste", "Celeste", "Celeste.exe").ok());+ CHECK(store.Remove(kA).ok());+ CHECK(store.Games().empty() && store.Removed().size() == 1);+ const auto removed = store.FindRemoved("usb0", "playden/import/Celeste");+ CHECK(removed && removed->id == kA && removed->program == "Celeste.exe");+ CHECK(store.Remove(kA).error() == ImportError::kNotFound);++ ImportStore reloaded(storage, "imports.json", Ids({kB}), Clock());+ CHECK(reloaded.Load().ok() && reloaded.Removed().size() == 1);+ // Adding the removed folder again brings its ID (and through it, bottle and settings) back.+ const auto readded = reloaded.Add("usb0", "playden/import/Celeste", "Celeste", "bin/Celeste.exe");+ CHECK(readded.ok() && readded.value().id == kA && readded.value().program == "bin/Celeste.exe");+ CHECK(reloaded.Removed().empty());+ // Removed twice keeps one entry for the folder.+ CHECK(reloaded.Remove(kA).ok() && reloaded.Add("usb0", "playden/import/Celeste", "Celeste", "Celeste.exe").ok());+ CHECK(reloaded.Remove(kA).ok() && reloaded.Removed().size() == 1);+ // Another slot is another folder: a new ID.+ const auto other = reloaded.Add("usb1", "playden/import/Celeste", "Celeste", "Celeste.exe");+ CHECK(other.ok() && other.value().id == kB);+}++void CopyTransitionsFollowTheStateTable() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ ImportStore store(storage, "imports.json", Ids({kA, kB}), Clock());+ CHECK(store.Load().ok());+ CHECK(store.Add("usb0", "playden/import/Rally", "Old School Rally", "Old School Rally.exe").ok());++ // Moves the table doesn't have.+ CHECK(store.StartCopy(kA).error() == ImportError::kWrongState);+ CHECK(store.FinishCopy(kA).error() == ImportError::kWrongState);+ CHECK(store.CancelCopy(kA).error() == ImportError::kWrongState);+ CHECK(store.Uninstalled(kA).error() == ImportError::kWrongState);+ CHECK(store.QueueCopy(kC).error() == ImportError::kNotFound);++ // in_place -> copy_queued -> cancel -> in_place.+ CHECK(store.QueueCopy(kA).ok() && store.Find(kA)->state == ImportState::kCopyQueued);+ CHECK(store.QueueCopy(kA).error() == ImportError::kWrongState);+ CHECK(store.Remove(kA).error() == ImportError::kWrongState); // cancel first+ const auto cancelled = store.CancelCopy(kA);+ CHECK(cancelled.ok() && cancelled.value().state == ImportState::kInPlace);+ CHECK(cancelled.value().failure.empty());++ // copying -> failed: like cancel, with the failure kept (and saved).+ CHECK(store.QueueCopy(kA).ok() && store.StartCopy(kA).ok() && store.Find(kA)->state == ImportState::kCopying);+ CHECK(store.Remove(kA).error() == ImportError::kWrongState);+ CHECK(store.CancelCopy(kA, "The drive was disconnected.").ok());+ {+ ImportStore reloaded(storage, "imports.json", Ids({}), Clock());+ CHECK(reloaded.Load().ok());+ CHECK(reloaded.Find(kA)->state == ImportState::kInPlace && reloaded.Find(kA)->failure == "The drive was disconnected.");+ }+ // A new copy clears the old failure; copying -> copied.+ CHECK(store.QueueCopy(kA).ok() && store.Find(kA)->failure.empty());+ CHECK(store.StartCopy(kA).ok() && store.FinishCopy(kA).ok() && store.Find(kA)->state == ImportState::kCopied);+ CHECK(store.QueueCopy(kA).error() == ImportError::kWrongState);+ CHECK(store.CancelCopy(kA).error() == ImportError::kWrongState);+ // A copied game must be uninstalled before it can be removed.+ CHECK(store.Remove(kA).error() == ImportError::kWrongState);+ CHECK(store.Uninstalled(kA).ok());+ CHECK(store.Games().empty() && store.FindRemoved("usb0", "playden/import/Rally")->id == kA);+ const auto back = store.Add("usb0", "playden/import/Rally", "Old School Rally", "Old School Rally.exe");+ CHECK(back.ok() && back.value().id == kA && back.value().state == ImportState::kInPlace);++ // A drop-folder game plays in place too: a failed copy (or move) leaves it in place, with the failure.+ CHECK(store.Add("internal", "playden-import/Delores", "Delores", "Delores.exe").ok());+ CHECK(store.QueueCopy(kB).ok() && store.StartCopy(kB).ok());+ const auto dropped = store.CancelCopy(kB, "No space left.");+ CHECK(dropped.ok() && dropped.value().id == kB && dropped.value().state == ImportState::kInPlace);+ CHECK(store.Find(kB)->state == ImportState::kInPlace && store.Find(kB)->failure == "No space left.");+ {+ ImportStore reloaded(storage, "imports.json", Ids({}), Clock());+ CHECK(reloaded.Load().ok() && reloaded.Find(kB)->failure == "No space left.");+ }+ CHECK(store.QueueCopy(kB).ok() && store.CancelCopy(kB).ok() && store.Find(kB)->failure.empty());++ CHECK(store.SetProgram(kA, "bin/Rally.exe").ok() && store.Find(kA)->program == "bin/Rally.exe");+ CHECK(store.SetProgram(kA, "../x.exe").error() == ImportError::kInvalid);+}++void DamagedFilesAreNotReplaced() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ const auto expect = [&](const std::string& text, ImportError error) {+ Write(storage, text);+ ImportStore store(storage, "imports.json", Ids({kC}), Clock());+ CHECK(store.Load().error() == error);+ CHECK(!store.loaded() && store.Games().empty());+ CHECK(store.Add("usb0", "x", "X", "x.exe").error() == ImportError::kNotLoaded);+ CHECK(store.Save().error() == ImportError::kNotLoaded);+ CHECK(Text(storage) == text); // left as it was+ };+ const std::string game = std::string("{\"id\":\"") + kA ++ "\",\"title\":\"T\",\"volume\":\"usb0\",\"folder\":\"f\",\"program\":\"p.exe\","+ "\"state\":\"in_place\",\"added_at\":1}";+ expect("{broken", ImportError::kMalformed);+ expect("[]", ImportError::kMalformed);+ expect(R"({"version":2,"games":[],"removed":[]})", ImportError::kUnsupportedVersion);+ expect(R"({"version":"1","games":[],"removed":[]})", ImportError::kMalformed);+ expect(R"({"version":1,"games":[]})", ImportError::kMalformed);+ expect("{\"version\":1,\"games\":[" + game + "," + game + "],\"removed\":[]}", ImportError::kMalformed);+ expect("{\"version\":1,\"games\":[" + game + "],\"removed\":[{\"id\":\"" + kA ++ "\",\"volume\":\"usb0\",\"folder\":\"g\",\"program\":\"p.exe\"}]}",+ ImportError::kMalformed);+ for (const auto& [from, to] : std::vector<std::pair<std::string, std::string>>{+ {"in_place", "moved"}, {"\"f\"", "\"../f\""}, {"\"p.exe\"", "\"/p.exe\""}, {"usb0", "USB0"},+ {kA, "1B4E28BA-2FA1-41D2-883F-0016D3CCA427"}, {"\"added_at\":1", "\"added_at\":-1"},+ {"\"title\":\"T\"", "\"title\":\"\""}}) {+ auto damaged = game;+ damaged.replace(damaged.find(from), from.size(), to);+ expect("{\"version\":1,\"games\":[" + damaged + "],\"removed\":[]}", ImportError::kMalformed);+ }+ std::string oversized(300 * 1024, ' ');+ CHECK(storage.WriteDurable("imports.json", playden::Bytes(oversized.begin(), oversized.end())).ok());+ ImportStore big(storage, "imports.json", Ids({}), Clock());+ CHECK(!big.Load().ok() && !big.loaded());++ // A good file with a failure loads.+ auto failed = game;+ failed.replace(failed.find("\"added_at\""), 0, "\"failure\":\"Out of space\",");+ Write(storage, "{\"version\":1,\"games\":[" + failed + "],\"removed\":[]}");+ ImportStore good(storage, "imports.json", Ids({}), Clock());+ CHECK(good.Load().ok() && good.Find(kA)->failure == "Out of space");+}++void FileIsPrivate() {+ TemporaryDirectory directory;+ playden::Storage storage(directory.path);+ CHECK(storage.EnsureRootDirectory().ok());+ ImportStore store(storage, "imports.json", Ids({kA}), Clock());+ CHECK(store.Load().ok() && store.Add("usb0", "playden/import/A", "A", "A.exe").ok());+ struct stat info{};+ CHECK(::stat((directory.path + "/imports.json").c_str(), &info) == 0 && (info.st_mode & 0777) == 0600);+}++} // namespace++int main() {+ RandomIdsAreLowerCaseVersionFour();+ AddFindAndRoundTrip();+ DuplicatesAndInvalidFieldsAreRefused();+ InPlaceRemoveAndRestoreFromRemoved();+ CopyTransitionsFollowTheStateTable();+ DamagedFilesAreNotReplaced();+ FileIsPrivate();+ std::puts("local_import_store_test passed");+ return 0;+}diff --git a/tests/local_scanner_test.cpp b/tests/local_scanner_test.cppnew file mode 100644index 0000000..9f53417--- /dev/null+++ b/tests/local_scanner_test.cpp@@ -0,0 +1,285 @@+// Ports of the macOS LocalWindowsGameTests, plus the console's walk rules. Built twice: once with+// the host's descriptor walk, once with the PS5's walk by path (PLAYDEN_PLATFORM_PS5).+#include "stores/local/scanner.h"++#include <algorithm>+#include <cstdio>+#include <cstdlib>+#include <fcntl.h>+#include <filesystem>+#include <set>+#include <string>+#include <sys/stat.h>+#include <unistd.h>+#include <vector>++using namespace playden::stores::local;+namespace fs = std::filesystem;++#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #x); std::exit(1); } } while (0)++namespace {++struct TemporaryDirectory {+ TemporaryDirectory() {+ auto pattern = (fs::temp_directory_path() / "playden-local-scanner-XXXXXX").string();+ std::vector<char> writable(pattern.begin(), pattern.end());+ writable.push_back('\0');+ CHECK(::mkdtemp(writable.data()) != nullptr);+ path = fs::canonical(writable.data());+ }+ ~TemporaryDirectory() {+ std::error_code ignored;+ fs::remove_all(path, ignored);+ }+ fs::path path;+};++void File(const fs::path& path, std::uint64_t bytes = 4) {+ fs::create_directories(path.parent_path());+ const int fd = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);+ CHECK(fd >= 0);+ CHECK(::ftruncate(fd, static_cast<off_t>(bytes)) == 0);+ ::close(fd);+}++std::vector<std::string> Paths(const FolderScan& scan) {+ std::vector<std::string> paths;+ for (const auto& program : scan.programs) paths.push_back(program.path);+ return paths;+}++bool Has(const FolderScan& scan, const std::string& path) {+ const auto paths = Paths(scan);+ return std::find(paths.begin(), paths.end(), path) != paths.end();+}++void ExecutablesListTheGameFirstAndHelpersLast() {+ TemporaryDirectory root;+ File(root.path / "Blur/game/unins000.exe", 900);+ File(root.path / "Blur/game/Blur.exe", 100);+ File(root.path / "Blur/game/_Redist/dxsetup.exe", 2000);+ File(root.path / "Blur/game/_Redist/vcredist_x86.exe", 2000);+ File(root.path / "Blur/game/tools/Editor.exe", 5000);+ File(root.path / "Blur/game/UnityCrashHandler64.exe", 3000);+ const auto scan = ScanGameFolder((root.path / "Blur/game").string());+ CHECK(scan.status == ScanStatus::kOk);+ CHECK(scan.title == "Blur");+ const auto paths = Paths(scan);+ CHECK(paths.size() == 6);+ CHECK(paths[0] == "Blur.exe"); // named like the game+ CHECK(paths[1] == "tools/Editor.exe");+ CHECK((std::set<std::string>(paths.begin() + 2, paths.end()) ==+ std::set<std::string>{"unins000.exe", "_Redist/dxsetup.exe", "_Redist/vcredist_x86.exe", "UnityCrashHandler64.exe"}));+ CHECK(std::all_of(scan.programs.begin() + 2, scan.programs.end(), [](const Program& p) { return p.helper; }));+ CHECK(!scan.programs[0].helper && !scan.programs[1].helper);+ CHECK(scan.file_count == 6 && scan.total_bytes == 900 + 100 + 2000 + 2000 + 5000 + 3000);+ CHECK(!scan.limit_reached && !scan.deeper_folders && scan.printable_path);+}++void GenericFolderNamesUseTheParentAsTitle() {+ CHECK(TitleForFolder("/Games/Blur/game") == "Blur");+ CHECK(TitleForFolder("/Games/Hades") == "Hades");+ CHECK(TitleForFolder("/Games/Hades/") == "Hades");+ CHECK(TitleForFolder("/mnt/usb0/Some Game/Binaries") == "Some Game");+ CHECK(TitleForFolder("/mnt/usb0/Hades/X64") == "Hades");+ // A generic name right in an import root keeps its own name: the root isn't the game.+ CHECK(TitleForFolder("/mnt/usb0/playden/import/Game") == "Game");+ CHECK(TitleForFolder("/data/playden-import/bin") == "bin");+ CHECK(TitleForFolder("/game") == "game");+ CHECK(TitleForFolder("/mnt/usb0/playden/import/Hollow Knight") == "Hollow Knight");+}++void ProgramChecks() {+ TemporaryDirectory root;+ File(root.path / "Empty/readme.txt");+ File(root.path / "Hades/x64/Hades.exe");+ const auto empty = (root.path / "Empty").string(), hades = (root.path / "Hades").string();+ CHECK(CheckProgram(empty, "Game.exe") == ProgramCheck::kMissing);+ CHECK(CheckProgram(empty, "../readme.txt") == ProgramCheck::kInvalidPath);+ CHECK(CheckProgram(hades, "x64/Hades.exe") == ProgramCheck::kOk);+ CHECK(CheckProgram(hades + "/", "x64/Hades.exe") == ProgramCheck::kOk);+ CHECK(CheckProgram(hades, "x64") == ProgramCheck::kNotRegularFile);+ CHECK(CheckProgram(hades, "x64/Hades.exe/more") == ProgramCheck::kNotRegularFile);+ CHECK(CheckProgram((root.path / "Gone").string(), "Game.exe") == ProgramCheck::kMissing);+ for (const char* bad : {"", "/x64/Hades.exe", "x64//Hades.exe", "./x64/Hades.exe", "x64/./Hades.exe", "x64/",+ "x64\\Hades.exe", "x64/../x64/Hades.exe", ".."})+ CHECK(CheckProgram(hades, bad) == ProgramCheck::kInvalidPath);+ CHECK(IsSafeRelativePath("a b/c.exe") && !IsSafeRelativePath(std::string("a\0b", 3)));+}++void LinksAreNeverFollowed() {+ TemporaryDirectory root;+ File(root.path / "outside/Secret.exe", 10'000);+ File(root.path / "outside/dir/Other.exe", 10'000);+ File(root.path / "Game/Game.exe", 10);+ fs::create_symlink(root.path / "outside/Secret.exe", root.path / "Game/Linked.exe");+ fs::create_directory_symlink(root.path / "outside/dir", root.path / "Game/linked-dir");+ fs::create_symlink("../outside/Secret.exe", root.path / "Game/Relative.exe");+ const auto game = (root.path / "Game").string();+ const auto scan = ScanGameFolder(game);+ CHECK(scan.status == ScanStatus::kOk);+ CHECK(Paths(scan) == std::vector<std::string>{"Game.exe"});+ CHECK(scan.file_count == 1 && scan.total_bytes == 10);+ // The program check doesn't follow them either, wherever they point.+ CHECK(CheckProgram(game, "Linked.exe") == ProgramCheck::kNotRegularFile);+ CHECK(CheckProgram(game, "Relative.exe") == ProgramCheck::kNotRegularFile);+ CHECK(CheckProgram(game, "linked-dir/Other.exe") == ProgramCheck::kNotRegularFile);+ // A link in the folder's own path: refused, not followed.+ fs::create_directory_symlink(root.path / "Game", root.path / "Alias");+ const auto aliased = ScanGameFolder((root.path / "Alias").string());+ CHECK(aliased.status != ScanStatus::kOk && aliased.programs.empty());+ CHECK(CheckProgram((root.path / "Alias").string(), "Game.exe") != ProgramCheck::kOk);+}++void HiddenEntriesAreSkippedAndExtensionsIgnoreCase() {+ TemporaryDirectory root;+ File(root.path / "Game/.hidden/Game.exe");+ File(root.path / "Game/.Sneaky.exe");+ File(root.path / "Game/GAME.EXE");+ File(root.path / "Game/Launcher.Exe");+ File(root.path / "Game/notes.exe.txt");+ File(root.path / "Game/.exe");+ fs::create_directories(root.path / "Game/folder.exe");+ const auto scan = ScanGameFolder((root.path / "Game").string());+ CHECK(scan.status == ScanStatus::kOk);+ CHECK(Paths(scan) == (std::vector<std::string>{"GAME.EXE", "Launcher.Exe"}));+ CHECK(scan.file_count == 3); // the hidden ones aren't counted+}++void DepthIsLimited() {+ TemporaryDirectory root;+ File(root.path / "Game/a/b/c/Deep.exe", 1);+ File(root.path / "Game/a/b/c/d/Deeper.exe", 1'000'000);+ File(root.path / "Game/a/b/c/d/e/Deepest.exe", 1);+ const auto scan = ScanGameFolder((root.path / "Game").string());+ CHECK(scan.status == ScanStatus::kOk);+ CHECK(Paths(scan) == std::vector<std::string>{"a/b/c/Deep.exe"});+ CHECK(scan.deeper_folders && !scan.limit_reached);+ CHECK(scan.file_count == 1);+}++void LimitsStopTheScanAndSaySo() {+ {+ TemporaryDirectory root;+ for (std::size_t i = 0; i < kMaxScanPrograms + 1; ++i) File(root.path / "Game" / ("p" + std::to_string(i) + ".exe"), 1);+ const auto scan = ScanGameFolder((root.path / "Game").string());+ CHECK(scan.status == ScanStatus::kOk && scan.limit_reached);+ CHECK(scan.programs.size() == kMaxScanPrograms);+ }+ {+ TemporaryDirectory root;+ for (std::size_t i = 0; i < kMaxScanEntries + 1; ++i) {+ const auto path = root.path / "Game" / ("f" + std::to_string(i) + ".dat");+ if (i == 0) fs::create_directories(path.parent_path());+ const int fd = ::open(path.c_str(), O_WRONLY | O_CREAT, 0644);+ CHECK(fd >= 0);+ ::close(fd);+ }+ const auto scan = ScanGameFolder((root.path / "Game").string());+ CHECK(scan.status == ScanStatus::kOk && scan.limit_reached);+ CHECK(scan.file_count == kMaxScanEntries);+ }+}++void NonAsciiPathsAreFlagged() {+ TemporaryDirectory root;+ File(root.path / "Caf\xc3\xa9 Racer/Caf\xc3\xa9 Racer.exe");+ File(root.path / "Caf\xc3\xa9 Racer/Racer.exe");+ File(root.path / "Plain/\xe3\x82\xb2\xe3\x83\xbc\xe3\x83\xa0.exe");+ const auto accented = ScanGameFolder((root.path / "Caf\xc3\xa9 Racer").string());+ CHECK(accented.status == ScanStatus::kOk && !accented.printable_path);+ CHECK(accented.programs.size() == 2);+ const auto plain = ScanGameFolder((root.path / "Plain").string());+ CHECK(plain.status == ScanStatus::kOk && plain.printable_path);+ CHECK(plain.programs.size() == 1 && !plain.programs[0].printable);+ CHECK(IsPrintableAscii("/mnt/usb0/playden/import/Old School Rally") && !IsPrintableAscii("a\tb"));+}++void FailuresAreReported() {+ TemporaryDirectory root;+ File(root.path / "file.txt");+ CHECK(ScanGameFolder((root.path / "missing").string()).status == ScanStatus::kMissing);+ CHECK(ScanGameFolder((root.path / "file.txt").string()).status == ScanStatus::kNotFolder);+ CHECK(ScanGameFolder("relative/path").status == ScanStatus::kUnsupported);+}++// File lists of the three games the import spike played (sizes made up, shapes as shipped).+void SpikeGamesRankTheirProgramFirst() {+ TemporaryDirectory root;+ const auto import = root.path / "mnt/usb0/playden/import";+ File(import / "Delores/Delores.exe", 6'000'000);+ File(import / "Delores/SDL2.dll", 1'500'000);+ File(import / "Delores/Delores.ggpack1", 40'000'000);+ File(import / "Delores/crashpad_handler.exe", 9'000'000);+ File(import / "Night Sky/NightSky.exe", 2'000'000);+ File(import / "Night Sky/steam_api.dll", 200'000);+ File(import / "Night Sky/steam_appid.txt", 6);+ File(import / "Night Sky/setup/dxsetup.exe", 500'000);+ File(import / "Night Sky/setup/vcredist_x86.exe", 5'000'000);+ File(import / "Night Sky/setup/oalinst.exe", 800'000);+ File(import / "Old School Rally/Old School Rally.exe", 650'000);+ File(import / "Old School Rally/UnityCrashHandler64.exe", 1'100'000);+ File(import / "Old School Rally/UnityPlayer.dll", 30'000'000);+ File(import / "Old School Rally/Old School Rally_Data/Plugins/x86_64/steam_api64.dll", 300'000);+ File(import / "Old School Rally/MonoBleedingEdge/EmbedRuntime/mono-2.0-bdwgc.dll", 5'000'000);+ const auto candidates = ScanImportRoot(import.string());+ CHECK(candidates.status == ScanStatus::kOk);+ CHECK(candidates.folders == (std::vector<std::string>{"Delores", "Night Sky", "Old School Rally"}));+ const auto delores = ScanGameFolder((import / "Delores").string());+ CHECK(delores.title == "Delores" && Paths(delores).front() == "Delores.exe" && delores.programs.back().helper);+ CHECK(delores.file_count == 4);+ const auto night = ScanGameFolder((import / "Night Sky").string());+ CHECK(night.title == "Night Sky" && Paths(night).front() == "NightSky.exe");+ CHECK(std::all_of(night.programs.begin() + 1, night.programs.end(), [](const Program& p) { return p.helper; }));+ const auto rally = ScanGameFolder((import / "Old School Rally").string());+ CHECK(rally.status == ScanStatus::kOk && rally.printable_path);+ CHECK(Paths(rally) == (std::vector<std::string>{"Old School Rally.exe", "UnityCrashHandler64.exe"}));+ CHECK(!rally.programs[0].helper && rally.programs[1].helper);+ CHECK(rally.file_count == 5);+ CHECK(CheckProgram((import / "Old School Rally").string(), "Old School Rally.exe") == ProgramCheck::kOk);+}++void ImportRootListsChildFolders() {+ TemporaryDirectory root;+ CHECK(ScanImportRoot((root.path / "absent/playden/import").string()).status == ScanStatus::kOk);+ CHECK(ScanImportRoot((root.path / "absent/playden/import").string()).folders.empty());+ const auto import = root.path / "import";+ fs::create_directories(import / "B Game");+ fs::create_directories(import / "A Game/sub");+ fs::create_directories(import / ".Trashes");+ File(import / "loose.exe");+ fs::create_directory_symlink(root.path, import / "Link");+ const auto scan = ScanImportRoot(import.string());+ CHECK(scan.status == ScanStatus::kOk && !scan.limit_reached);+ CHECK(scan.folders == (std::vector<std::string>{"A Game", "B Game"}));+ CHECK(ScanImportRoot("relative").status == ScanStatus::kUnsupported);+ CHECK(ScanImportRoot((import / "loose.exe").string()).status == ScanStatus::kNotFolder);+ CHECK(ImportRoots().size() == 5 && std::string(ImportRoots()[0].volume) == "internal");+}++void HelperRules() {+ CHECK(IsLikelyHelper("Setup.exe") && IsLikelyHelper("UNINS000.EXE") && IsLikelyHelper("bin/UnityCrashHandler64.exe"));+ CHECK(IsLikelyHelper("_CommonRedist/DirectX/Jun2010/foo.exe") && IsLikelyHelper("EasyAntiCheat/Game.exe"));+ CHECK(!IsLikelyHelper("Game.exe") && !IsLikelyHelper("bin/Game.exe") && !IsLikelyHelper("redistx/Game.exe"));+ CHECK(!IsLikelyHelper("Setup/Game.exe")); // "setup" is a name prefix, not a helper folder+}++} // namespace++int main() {+ ExecutablesListTheGameFirstAndHelpersLast();+ GenericFolderNamesUseTheParentAsTitle();+ ProgramChecks();+ LinksAreNeverFollowed();+ HiddenEntriesAreSkippedAndExtensionsIgnoreCase();+ DepthIsLimited();+ LimitsStopTheScanAndSaySo();+ NonAsciiPathsAreFlagged();+ FailuresAreReported();+ SpikeGamesRankTheirProgramFirst();+ ImportRootListsChildFolders();+ HelperRules();+ std::puts("local_scanner_test passed");+ return 0;+}diff --git a/tests/runtime_writable_folders_test.cpp b/tests/runtime_writable_folders_test.cppindex ae1cac6..28072a7 100644--- a/tests/runtime_writable_folders_test.cpp+++ b/tests/runtime_writable_folders_test.cpp@@ -131,6 +131,32 @@ int main() {CHECK(EnsureWritableFolders({target}, runs, log, root).empty());CHECK(Mode(target) == 0777 && calls == before_calls);CHECK(chmod(target.c_str(), 0755) == 0);+ // The drop folder: made 0777 (not sticky) after the grant, an FTP-made 0755 one opened, a link refused.+ {+ const auto drop = root / "data/playden-import";+ const auto before = calls;+ CHECK(!PrepareDropFolder(root / "data/other", runs, log, root).empty() && calls == before);+ CHECK(PrepareDropFolder(drop, runs, log, root).empty());+ CHECK(fs::is_directory(drop) && Mode(drop) == 0777 && calls == before + 1);+ CHECK(chmod(drop.c_str(), 0755) == 0);+ CHECK(PrepareDropFolder(drop, runs, log, root).empty() && Mode(drop) == 0777);+ CHECK(PrepareDropFolder(drop, runs, log, root).empty() && Mode(drop) == 0777);+ CHECK(!PrepareDropFolder(drop, nullptr, log, root).empty());+ CHECK(!PrepareDropFolder(drop, refuses, log, root).empty());+ // An approved root: EnsureWritableFolders repairs it (and an import folder on a drive) to 0777.+ if (geteuid() != 0) {+ const auto drive_import = root / "mnt/usb0/playden/import";+ fs::create_directories(drive_import);+ CHECK(chmod(drop.c_str(), 0555) == 0 && chmod(drive_import.c_str(), 0555) == 0);+ CHECK(EnsureWritableFolders({drop, drive_import}, runs, log, root).empty());+ CHECK(Mode(drop) == 0777 && Mode(drive_import) == 0777);+ }+ CHECK(rmdir(drop.c_str()) == 0);+ CHECK(symlink(target.c_str(), drop.c_str()) == 0);+ CHECK(chmod(target.c_str(), 0755) == 0);+ CHECK(!PrepareDropFolder(drop, runs, log, root).empty() && Mode(target) == 0755 && fs::is_symlink(drop));+ CHECK(unlink(drop.c_str()) == 0);+ }// /data swapped for a link to an outside folder: neither repair follows it as root.{const auto outside = root / "outside-data";diff --git a/tools/import-scan/main.cpp b/tools/import-scan/main.cppnew file mode 100644index 0000000..0648787--- /dev/null+++ b/tools/import-scan/main.cpp@@ -0,0 +1,77 @@+// import-scan: what Playden's import scanner sees in a Windows game folder, on the host.+//+// import-scan <game folder> the folder's title, its programs ranked (helpers flagged), its+// size and whether it could play in place (printable-ASCII path)+// import-scan --root <folder> the same for every folder inside an import folder+//+// Sizes are the scanner's display estimates (scanner.h): the first levels only, up to its limits.+#include "stores/local/scanner.h"++#include <cstdio>+#include <cstring>+#include <string>++namespace {+using namespace playden::stores::local;++const char* StatusName(ScanStatus status) {+ switch (status) {+ case ScanStatus::kOk: return "ok";+ case ScanStatus::kMissing: return "missing";+ case ScanStatus::kNotFolder: return "not a folder";+ case ScanStatus::kUnsupported: return "unsupported";+ case ScanStatus::kFailed: return "failed";+ }+ return "unknown";+}++std::string Bytes(std::uint64_t bytes) {+ char text[32];+ if (bytes >= 1024ull * 1024 * 1024) std::snprintf(text, sizeof text, "%.1f GB", bytes / (1024.0 * 1024 * 1024));+ else if (bytes >= 1024ull * 1024) std::snprintf(text, sizeof text, "%.1f MB", bytes / (1024.0 * 1024));+ else std::snprintf(text, sizeof text, "%llu bytes", static_cast<unsigned long long>(bytes));+ return text;+}++int Report(const std::string& folder) {+ const auto scan = ScanGameFolder(folder);+ std::printf("%s\n", folder.c_str());+ if (scan.status != ScanStatus::kOk) {+ std::printf(" status: %s (%s)\n", StatusName(scan.status), std::strerror(scan.error));+ return 1;+ }+ std::printf(" title: %s\n", scan.title.c_str());+ std::printf(" size: %s in %llu files%s\n", Bytes(scan.total_bytes).c_str(),+ static_cast<unsigned long long>(scan.file_count),+ scan.limit_reached || scan.deeper_folders ? " (estimate: the scan stopped early)" : "");+ std::printf(" ascii_ok: %s\n", scan.printable_path ? "yes (can play in place)" : "no (copy or move only)");+ if (scan.unreadable) std::printf(" unreadable entries: %zu\n", scan.unreadable);+ std::printf(" programs: %zu\n", scan.programs.size());+ for (std::size_t index = 0; index < scan.programs.size(); ++index) {+ const auto& program = scan.programs[index];+ std::printf(" %2zu. %s %s%s%s\n", index + 1, program.path.c_str(), Bytes(program.bytes).c_str(),+ program.helper ? " [installer or tool]" : "", program.printable ? "" : " [not ASCII]");+ }+ return 0;+}+} // namespace++int main(int argc, char** argv) {+ if (argc == 3 && std::strcmp(argv[1], "--root") == 0) {+ const std::string root = argv[2];+ const auto listed = ScanImportRoot(root);+ if (listed.status != ScanStatus::kOk) {+ std::fprintf(stderr, "%s: %s (%s)\n", root.c_str(), StatusName(listed.status), std::strerror(listed.error));+ return 1;+ }+ int failed = 0;+ for (const auto& name : listed.folders) failed |= Report(root + "/" + name);+ if (listed.limit_reached) std::printf("(more folders than the scanner lists)\n");+ return failed;+ }+ if (argc != 2 || argv[1][0] == '-') {+ std::fprintf(stderr, "usage: import-scan <game folder>\n import-scan --root <import folder>\n");+ return 2;+ }+ return Report(argv[1]);+}