Play your Steam library's Windows games on a jailbroken PS5.
C++
Delete uninstalled games' files without waiting
Leonardo Lobato7c611bca92e3
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex 1dda380..4d5ba22 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -1656,6 +1656,9 @@ void Controller::TickInstalls() {case InstallRequestKind::ClearCompleted: installs_->ClearCompleted(); break;}}+ // An uninstall whose files could not be deleted lists the game again and says why.+ for (const auto& failure : installs_->TakeRemovalFailures())+ StopUninstall(failure.id, failure.reason + " Uninstall the game again to try again.");const auto snapshot = installs_->Snapshot();ProjectLocations(snapshot.installed);// A download that finished in this session is prepared next, so the game is ready to play.diff --git a/src/homebrew/host.cpp b/src/homebrew/host.cppindex 5780139..9144f9c 100644--- a/src/homebrew/host.cpp+++ b/src/homebrew/host.cpp@@ -586,7 +586,7 @@ int main(int argc, char** argv) {std::uint32_t runtime_number) {return runner->Move(*service, entry, source, runtime_number);};- import_config.remove_copy_folder = [runner = copy_runner.get()](const std::string& root) { return runner->Remove(root); };+ import_config.remove_copy_folder = [runner = copy_runner.get()](const std::string& root) { return runner->Remove(root).empty(); };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(); });diff --git a/src/homebrew/ps5.cpp b/src/homebrew/ps5.cppindex d2ce7ab..0f0d05e 100644--- a/src/homebrew/ps5.cpp+++ b/src/homebrew/ps5.cpp@@ -647,7 +647,7 @@ int main() {std::uint32_t runtime_number) {return runner->Move(installs, entry, source, runtime_number);};- import_config.remove_copy_folder = [runner = copy_runner.get()](const std::string& root) { return runner->Remove(root); };+ import_config.remove_copy_folder = [runner = copy_runner.get()](const std::string& root) { return runner->Remove(root).empty(); };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(); });diff --git a/src/installs/install_service.cpp b/src/installs/install_service.cppindex 692e83b..20ce8ee 100644--- a/src/installs/install_service.cpp+++ b/src/installs/install_service.cpp@@ -182,11 +182,13 @@ struct RegistryEntry {std::uint64_t install_bytes = 0;bool queued = false; // written for a job in Downloads: "<id>\t<volume>\t0"bool paused = false; // "<id>\t<volume>\t0\tpaused": the player paused it, or it failed+ bool removing = false; // "<id>\t<volume>\t<install bytes>\tremoving": uninstalled, folder not deleted yet};// One line per game: "<key>" (legacy, primary volume) or "<key>\t<volume id>\t<install bytes>",-// followed by "\tpaused" for a job that should wait for Resume after a restart. The key is ToKey:-// a Steam game's app ID, "local-<uuid>" for an imported game.+// followed by "\tpaused" for a job that should wait for Resume after a restart, or "\tremoving" for+// an uninstall whose folder is not deleted yet. The key is ToKey: a Steam game's app ID,+// "local-<uuid>" for an imported game.std::optional<RegistryEntry> ParseRegistryLine(std::string_view line) {RegistryEntry entry;const auto first_tab = line.find('\t');@@ -205,7 +207,9 @@ std::optional<RegistryEntry> ParseRegistryLine(std::string_view line) {const auto bytes = fields.substr(0, third_tab);std::from_chars(bytes.data(), bytes.data() + bytes.size(), entry.install_bytes);entry.queued = entry.install_bytes == 0;- entry.paused = third_tab != std::string_view::npos && fields.substr(third_tab + 1) == "paused";+ const auto flag = third_tab == std::string_view::npos ? std::string_view{} : fields.substr(third_tab + 1);+ entry.paused = flag == "paused";+ entry.removing = flag == "removing";}return entry;}@@ -312,6 +316,15 @@ void InstallService::Scan() {}for (const auto& entry : entries) {const auto* volume = FindVolume(entry.volume_id);+ if (entry.removing) {+ // An uninstall Playden stopped before the folder was gone carries on; one on an absent+ // drive waits for the drive.+ InstalledGame game{entry.id, entry.install_bytes, RootOn(entry.volume_id, entry.id), entry.volume_id,+ volume && volume->available};+ uninstalling_.push_back(game);+ if (game.available) removals_.push_back({entry.id.store, game.root, std::nullopt, game});+ continue;+ }if (!volume || !volume->available) {// Keep games on an absent drive listed so they come back when it does.installed_.push_back({entry.id, entry.install_bytes, RootOn(entry.volume_id, entry.id), entry.volume_id, false});@@ -379,6 +392,9 @@ void InstallService::SaveRegistry() {const bool waits = job.view.state == InstallJobState::kPaused || job.view.state == InstallJobState::kFailed;std::fprintf(registry, "%s\t%s\t0%s\n", ToKey(job.view.id).c_str(), job.view.volume_id.c_str(), waits ? "\tpaused" : "");}+ for (const auto& game : uninstalling_)+ std::fprintf(registry, "%s\t%s\t%llu\tremoving\n", ToKey(game.id).c_str(), game.volume_id.c_str(),+ static_cast<unsigned long long>(game.install_bytes));if (std::fclose(registry) == 0) std::rename(temporary.c_str(), path.c_str());}@@ -455,6 +471,8 @@ void InstallService::Install(GameId id, std::optional<std::uint64_t> estimated_b// Only a store with a runner installs.if (stopping_ || !id.valid() || !RunnerFor(id.store) || Find(id) || jobs_.size() >= kMaxJobs) return;if (std::any_of(installed_.begin(), installed_.end(), [id](const InstalledGame& game) { return game.id == id; })) return;+ // A game whose files are still being deleted can be installed again once they are gone.+ if (std::any_of(uninstalling_.begin(), uninstalling_.end(), [id](const InstalledGame& game) { return game.id == id; })) return;Job job;job.view.id = id;const auto& target = volume_id.empty() ? default_volume_id_ : volume_id;@@ -570,7 +588,7 @@ void InstallService::Cancel(GameId id) {const auto root = volume && volume->available ? RootOn(job->view.volume_id, id) : std::string();jobs_.erase(std::remove_if(jobs_.begin(), jobs_.end(), [id](const Job& entry) { return entry.view.id == id; }),jobs_.end());- if (!root.empty()) removals_.push_back({id.store, root, std::nullopt});+ if (!root.empty()) QueueRemoval({id.store, root, std::nullopt, std::nullopt});}Publish();wake_.notify_all();@@ -584,11 +602,11 @@ void InstallService::Uninstall(GameId id) {// drive is only forgotten: whatever its path names now isn't this install, so nothing is removed.const auto* volume = FindVolume(found->volume_id);if (!found->available && (!volume || !volume->available)) return;- const auto root = found->available ? RootOn(found->volume_id, id) : std::string();+ const auto game = *found;installed_.erase(found);- // Its runner still hears of it, to forget what it keeps of the game.- if (!root.empty()) removals_.push_back({id.store, root, std::nullopt});- else removals_.push_back({id.store, {}, id});+ // Its runner still hears of a game whose folder is missing, to forget what it keeps of the game.+ if (game.available) QueueRemoval({id.store, RootOn(game.volume_id, id), std::nullopt, game});+ else QueueRemoval({id.store, {}, id, std::nullopt});Publish();wake_.notify_all();}@@ -599,6 +617,8 @@ bool InstallService::Adopt(GameId id, const std::string& volume_id, std::uint64_return true;const auto* volume = FindVolume(volume_id);if (stopping_ || !id.valid() || !RunnerFor(id.store) || Find(id) || !volume || !volume->available) return false;+ if (std::any_of(uninstalling_.begin(), uninstalling_.end(), [id](const InstalledGame& game) { return game.id == id; }))+ 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)});@@ -608,6 +628,26 @@ bool InstallService::Adopt(GameId id, const std::string& volume_id, std::uint64_return true;}+// Callers hold the lock. An uninstall does not wait for the running download, which can take+// hours: the download steps back into the queue, keeping its place, and carries on afterwards.+// Other removals (a canceled download's folder, a game to forget) wait for it.+void InstallService::QueueRemoval(Removal removal) {+ const bool uninstall = removal.game.has_value();+ if (uninstall) uninstalling_.push_back(*removal.game);+ removals_.push_back(std::move(removal));+ if (!uninstall || !active_) return;+ if (auto* running = Find(*active_); running && running->view.state == InstallJobState::kRunning && !running->cancel_requested) {+ running->view.state = InstallJobState::kQueued;+ running->rate.Reset(); running->view.bytes_per_second.reset(); running->view.eta_seconds.reset();+ active_cancel_.store(true);+ }+}++std::vector<RemovalFailure> InstallService::TakeRemovalFailures() {+ std::lock_guard<std::mutex> lock(mutex_);+ return std::exchange(removal_failures_, {});+}+void InstallService::SetDefaultVolume(const std::string& volume_id) {std::lock_guard<std::mutex> lock(mutex_);const auto* volume = FindVolume(volume_id);@@ -633,6 +673,7 @@ InstallSnapshot InstallService::Snapshot() {snapshot.revision = revision_;for (const auto& job : jobs_) snapshot.jobs.push_back(job.view);snapshot.installed = installed_;+ for (const auto& game : uninstalling_) snapshot.uninstalling.push_back(game.id);snapshot.completed = CompletedDownloads(installed_, dismissed_, WallNow());snapshot.volumes = volumes_;snapshot.default_volume_id = default_volume_id_;@@ -718,11 +759,29 @@ void InstallService::Run() {}}if (removal) {+ std::string failure;if (auto* remover = RunnerFor(removal->store)) {if (removal->forget) remover->Forget(*removal->forget);- else remover->Remove(removal->root);+ else failure = remover->Remove(removal->root);+ } else if (removal->game) {+ failure = "Playden can't delete this game's files.";}std::lock_guard<std::mutex> lock(mutex_);+ if (const auto& game = removal->game) {+ uninstalling_.erase(std::remove_if(uninstalling_.begin(), uninstalling_.end(),+ [&](const InstalledGame& entry) { return entry.id == game->id; }),+ uninstalling_.end());+ if (!failure.empty()) {+ // The folder keeps its install record until everything else is gone, so the game is+ // listed again and uninstalling it again tries again.+ if (!Find(game->id)) {+ installed_.push_back(*game);+ std::sort(installed_.begin(), installed_.end(),+ [](const InstalledGame& left, const InstalledGame& right) { return left.id < right.id; });+ }+ removal_failures_.push_back({game->id, failure});+ }+ }Publish();continue;}diff --git a/src/installs/install_service.h b/src/installs/install_service.hindex b7f9826..fca3e5e 100644--- a/src/installs/install_service.h+++ b/src/installs/install_service.h@@ -99,10 +99,17 @@ std::vector<CompletedDownload> CompletedDownloads(const std::vector<InstalledGamconst std::vector<std::pair<GameId, std::int64_t>>& dismissed,std::int64_t now);+// An uninstall whose files could not be deleted: the game is listed as installed again.+struct RemovalFailure {+ GameId id;+ std::string reason; // short, user-facing+};+struct InstallSnapshot {std::uint64_t revision = 0;std::vector<InstallJobView> jobs;std::vector<InstalledGame> installed;+ std::vector<GameId> uninstalling; // games whose files are being deletedstd::vector<CompletedDownload> completed;std::vector<InstallVolume> volumes;std::string default_volume_id;@@ -112,7 +119,8 @@ struct InstallSnapshot {// One game's install, run synchronously on the service worker, for the store it is registered// for. Implementations report progress and honor cancel. A runner that can't start yet (no// sign-in, the store out of reach) returns `retry_later`: the job keeps its place, waits and is-// tried again by itself while other jobs run.+// tried again by itself while other jobs run. Remove deletes a game's folder and returns an empty+// string on success or a short failure text.class InstallRunner {public:virtual ~InstallRunner() = default;@@ -127,7 +135,7 @@ class InstallRunner {virtual 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(InstallStep)>& step) = 0;- virtual bool Remove(const std::string& root) = 0;+ virtual std::string Remove(const std::string& root) = 0;// An uninstalled game whose folder is missing: forgotten with nothing to delete (on the worker).virtual void Forget(const GameId& id) { (void)id; }};@@ -135,7 +143,10 @@ class InstallRunner {// Install queue for real games under <volume games_root>/<key>. One install runs at a time; jobs// go to the runner of their game's store, and a store without one can't install.// Pause keeps the partial download for Resume; Cancel and Uninstall delete only Playden-managed-// folders. The first volume is primary: it holds the registry of every managed game (with the+// folders. A deletion never waits for a download: the running download steps back into the queue+// and carries on after it. An uninstall is in the registry until its folder is gone, so one that+// Playden stopped before it finished carries on at the next start; one that fails lists the game+// again and is reported by TakeRemovalFailures. The first volume is primary: it holds the registry of every managed game (with the// volume each is on) and the default-volume choice. At start the registry is read: complete// installs are listed and interrupted downloads come back paused. New installs go to the default// volume. A job whose runner asks to retry later waits (5 s, doubling up to a minute) while the@@ -166,9 +177,12 @@ class InstallService {void MoveQueued(GameId id, bool up);void Cancel(GameId id);void Uninstall(GameId id);+ // Uninstalls whose files could not be deleted since the last call.+ std::vector<RemovalFailure> TakeRemovalFailures();// 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.+ // (or was already); false when the game has a job or files being deleted, 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);@@ -206,9 +220,11 @@ class InstallService {Store store = Store::kSteam;std::string root;std::optional<GameId> forget; // set: InstallRunner::Forget instead of deleting `root`+ std::optional<InstalledGame> game; // an uninstall's folder; none for a canceled download};void Run();void Scan();+ void QueueRemoval(Removal removal);Job* Find(GameId id);void Publish();void SaveRegistry();@@ -235,6 +251,8 @@ class InstallService {std::vector<InstalledGame> installed_;std::vector<std::pair<GameId, std::int64_t>> dismissed_; // finished downloads taken off Completedstd::deque<Removal> removals_; // folders to delete, by the runner of their store+ std::vector<InstalledGame> uninstalling_; // uninstalls whose folder is not deleted yet+ std::vector<RemovalFailure> removal_failures_;std::optional<GameId> active_;std::atomic<bool> active_cancel_{false};std::uint64_t revision_ = 0;diff --git a/src/steam/backend/depot_installer.cpp b/src/steam/backend/depot_installer.cppindex c30f497..12e12c2 100644--- a/src/steam/backend/depot_installer.cpp+++ b/src/steam/backend/depot_installer.cpp@@ -11,6 +11,7 @@#include <algorithm>#include <cctype>+#include <cerrno>#include <chrono>#include <cstdio>#include <cstring>@@ -122,20 +123,33 @@ std::uint64_t DefaultFreeBytes(const std::string& path) {return VolumeSpaceSupported() ? 0 : std::numeric_limits<std::uint64_t>::max();}-bool RemoveTree(const std::string& path) {- struct stat info {};- if (lstat(path.c_str(), &info) != 0) return true;- if (!S_ISDIR(info.st_mode)) return unlink(path.c_str()) == 0;+// Deletes a tree without following links. `error` keeps the first failure's errno.+// A folder's entries other than "." and "..", read before any is deleted: deleting while reading+// can make readdir skip entries. False when the folder cannot be opened.+bool ListEntries(const std::string& path, std::vector<std::string>& names) {DIR* dir = opendir(path.c_str());if (!dir) return false;- bool ok = true;while (dirent* entry = readdir(dir)) {- const std::string name = entry->d_name;- if (name == "." || name == "..") continue;- ok = RemoveTree(path + "/" + name) && ok;+ std::string name = entry->d_name;+ if (name != "." && name != "..") names.push_back(std::move(name));}closedir(dir);- return rmdir(path.c_str()) == 0 && ok;+ return true;+}++bool RemoveTree(const std::string& path, int* error = nullptr) {+ const auto failed = [error] {+ if (error && *error == 0) *error = errno;+ return false;+ };+ struct stat info {};+ if (lstat(path.c_str(), &info) != 0) return errno == ENOENT || failed();+ if (!S_ISDIR(info.st_mode)) return unlink(path.c_str()) == 0 || failed();+ std::vector<std::string> names;+ if (!ListEntries(path, names)) return failed();+ bool ok = true;+ for (const auto& name : names) ok = RemoveTree(path + "/" + name, error) && ok;+ return (rmdir(path.c_str()) == 0 || failed()) && ok;}// Bytes the filesystem has allocated to a file, at most its size; 0 when absent.@@ -838,14 +852,19 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTareturn outcome;}-bool RemoveManagedInstall(const std::string& install_root) {- if (install_root.size() < 2 || install_root[0] != '/' || install_root.find("/..") != std::string::npos) return false;+bool RemoveManagedInstall(const std::string& install_root, std::string* failure) {+ const auto fail = [failure](std::string reason) {+ if (failure) *failure = std::move(reason);+ return false;+ };+ if (install_root.size() < 2 || install_root[0] != '/' || install_root.find("/..") != std::string::npos)+ return fail("The game's folder path is not valid.");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(copy_dir))- return false; // not a Playden-managed folder+ return fail("The game's folder holds no Playden install record, so it was left as it is.");// Remove exactly the paths the install recorded, deepest first, then Playden's own files.std::vector<std::string> files;std::vector<std::string> directories;@@ -872,17 +891,30 @@ bool RemoveManagedInstall(const std::string& install_root) {std::sort(parents.begin(), parents.end(),[](const std::string& left, const std::string& right) { return left.size() > right.size(); });for (const auto& parent : parents) rmdir(parent.c_str());- 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;- // Files the list did not cover (for example written by the game itself): fall back to a walk.- return RemoveTree(install_root);+ // Files the list did not cover (Playden's journals, files the game or other tools wrote) go in a walk.+ // The install record goes last: a folder that could not be emptied stays Playden's, so+ // uninstalling it again tries again.+ int error = 0;+ bool emptied = true;+ std::vector<std::string> names;+ if (ListEntries(install_root, names)) {+ for (const auto& name : names)+ if (name != DepotInstaller::kInstallRecord) emptied = RemoveTree(install_root + "/" + name, &error) && emptied;+ } else {+ emptied = false;+ error = errno;+ }+ if (emptied) {+ if (std::remove((install_root + "/" + DepotInstaller::kInstallRecord).c_str()) != 0 && errno != ENOENT) {+ emptied = false;+ error = errno;+ } else if (rmdir(install_root.c_str()) != 0) {+ emptied = false;+ error = errno;+ }+ }+ if (emptied) return true;+ return fail("Some of the game's files could not be deleted (" + std::string(std::strerror(error)) + ").");}} // namespace playden::steamdiff --git a/src/steam/backend/depot_installer.h b/src/steam/backend/depot_installer.hindex 6e10009..28d0aea 100644--- a/src/steam/backend/depot_installer.h+++ b/src/steam/backend/depot_installer.h@@ -133,6 +133,8 @@ class DepotInstaller {// Removes a managed install directory: only paths under install_root, and only when it holds// Playden's install record, download journal or an imported game's copy journal (.playden-copy).-bool RemoveManagedInstall(const std::string& install_root);+// The record is deleted last, so a folder that could not be emptied is still recognized next time.+// `failure`, when given, says why it failed.+bool RemoveManagedInstall(const std::string& install_root, std::string* failure = nullptr);} // namespace playden::steamdiff --git a/src/steam/backend/steam_install_runner.cpp b/src/steam/backend/steam_install_runner.cppindex 9fc9267..801dc23 100644--- a/src/steam/backend/steam_install_runner.cpp+++ b/src/steam/backend/steam_install_runner.cpp@@ -43,15 +43,16 @@ std::string FormatInstallStats(const char* phase, const InstallStats& stats) {return detail;}-bool SteamInstallRunner::Remove(const std::string& root) {- const bool ok = RemoveManagedInstall(root);+std::string SteamInstallRunner::Remove(const std::string& root) {+ std::string failure;+ const bool ok = RemoveManagedInstall(root, &failure);const auto slash = root.rfind('/');const auto name = root.substr(slash == std::string::npos ? 0 : slash + 1);std::uint32_t app = 0;const auto parsed = std::from_chars(name.data(), name.data() + name.size(), app);if (log_ && app != 0 && parsed.ec == std::errc{} && parsed.ptr == name.data() + name.size())- log_(app, "install-removal", "done", ok ? "ok=1" : "ok=0");- return ok;+ log_(app, "install-removal", "done", ok ? "ok=1" : ("ok=0 " + failure).c_str());+ return ok ? std::string() : failure;}const char* InstallFailureText(InstallError error) {diff --git a/src/steam/backend/steam_install_runner.h b/src/steam/backend/steam_install_runner.hindex 40b5f91..85e344c 100644--- a/src/steam/backend/steam_install_runner.h+++ b/src/steam/backend/steam_install_runner.h@@ -21,7 +21,7 @@ class SteamInstallRunner final : public SessionInstallRunner {const std::atomic<bool>& cancel,const std::function<void(std::uint64_t, std::uint64_t, std::uint64_t)>& progress,const std::function<void(InstallStep)>& step) override;- bool Remove(const std::string& root) override;+ std::string Remove(const std::string& root) override;void SetTuning(std::function<DownloadTuning()> tuning) { tuning_ = std::move(tuning); }// Optional sanitized log: the Steam app ID, step, error category and a short detail; never// tokens or account identifiers. Called on the install worker.diff --git a/src/stores/local/copy_runner.cpp b/src/stores/local/copy_runner.cppindex d6dec15..f942992 100644--- a/src/stores/local/copy_runner.cpp+++ b/src/stores/local/copy_runner.cpp@@ -807,30 +807,46 @@ std::string LocalCopyRunner::Move(installs::InstallService& service, const Imporreturn {};}-bool LocalCopyRunner::Remove(const std::string& root) {- RemoveParts(root);- const bool removed = steam::RemoveManagedInstall(root);+bool LocalCopyRunner::StoreLoaded() {+ if (!store_.loaded()) {+ const auto loaded = store_.Load();+ Log("import store read for a removal" + (loaded.ok() ? std::string() : ": " + loaded.message()));+ }+ return store_.loaded();+}++std::string LocalCopyRunner::Remove(const std::string& root) {const auto game = ParseKey(Basename(root));- if (!game || game->store != Store::kLocal || !store_.loaded()) return removed;+ const bool local = game && game->store == Store::kLocal;+ // An uninstall Playden stopped carries on at the next start, before the library has read the+ // imports. They are read now, so the game leaves the library with its folder; while they can't+ // be read, nothing is deleted.+ if (local && !StoreLoaded()) return "Playden's list of imported games can't be read.";+ RemoveParts(root);+ std::string failure;+ const bool removed = steam::RemoveManagedInstall(root, &failure);+ if (!local) return failure;const auto entry = store_.Find(game->value);- if (!entry) return removed;+ if (!entry) return failure;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.+ } else if (entry->state == ImportState::kCopied && removed) {+ // Uninstalled: the game leaves the library (its bottle stays, like any kept data).store_.Uninstalled(entry->id);- Log("import copy uninstalled id=" + entry->id + (removed ? "" : " (folder not removed)"));+ Log("import copy uninstalled id=" + entry->id);Changed();+ } else if (entry->state == ImportState::kCopied) {+ // The service lists the game again, so it stays in the library and uninstalling it again tries again.+ Log("import copy uninstall stopped id=" + entry->id + ": " + failure);}- return removed;+ return failure;}void LocalCopyRunner::Forget(const GameId& id) {// As Remove, with no folder to delete (its folder is missing).- if (id.store != Store::kLocal || !store_.loaded()) return;+ if (id.store != Store::kLocal || !StoreLoaded()) return;const auto entry = store_.Find(id.value);if (!entry) return;if (entry->state == ImportState::kCopyQueued || entry->state == ImportState::kCopying) {diff --git a/src/stores/local/copy_runner.h b/src/stores/local/copy_runner.hindex 16a4a59..0445e39 100644--- a/src/stores/local/copy_runner.h+++ b/src/stores/local/copy_runner.h@@ -112,10 +112,13 @@ class LocalCopyRunner final : public installs::InstallRunner {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;+ std::string Remove(const std::string& root) override;void Forget(const GameId& id) override;private:+ // Reads the imports if nobody has yet (removals run from the install worker, which can start+ // first). False while they can't be read.+ bool StoreLoaded();LocalRelocationResult Relocate(const LocalRelocation& relocation);// Before the title follows the copy (LocalRelocation::move_files): every planned file the source// has changed since it was copied is planned again as it is now (the plan saved) and copied again.diff --git a/tests/depot_installer_test.cpp b/tests/depot_installer_test.cppindex d7d6056..f8a9303 100644--- a/tests/depot_installer_test.cpp+++ b/tests/depot_installer_test.cpp@@ -18,6 +18,7 @@#include <iterator>#include <map>#include <string>+#include <sys/stat.h>#include <unistd.h>using namespace playden;@@ -618,14 +619,14 @@ void StatsAndRemovalLogs(const std::filesystem::path& root) {CHECK(std::string(detail) == expected_ok);++removals;});- CHECK(runner.Remove((root / "440").string()));+ CHECK(runner.Remove((root / "440").string()).empty());CHECK(removals == 1);- expected_app = 441; expected_ok = "ok=0";+ expected_app = 441; expected_ok = "ok=0 The game's folder holds no Playden install record, so it was left as it is.";std::filesystem::create_directories(root / "441");std::ofstream(root / "441" / "keep.txt") << "unmanaged";- CHECK(!runner.Remove((root / "441").string()));+ CHECK(runner.Remove((root / "441").string()).find("no Playden install record") != std::string::npos);CHECK(removals == 2);- CHECK(!runner.Remove((root / "account-name").string() + "/.."));+ CHECK(!runner.Remove((root / "account-name").string() + "/..").empty());CHECK(removals == 2); // no unvalidated path/name is logged}@@ -1012,6 +1013,29 @@ void RemovesOnlyManagedFolders(const std::filesystem::path& base) {CHECK(RemoveManagedInstall(managed.string()));CHECK(!std::filesystem::exists(managed));CHECK(!RemoveManagedInstall("relative/path"));++ // Files the install did not record (written by the game or other tools, in open folders) go too.+ InstallsMergedDepotsAndWritesRecord(managed);+ std::filesystem::create_directories(managed / "bottle/drive_c");+ std::filesystem::permissions(managed / "bottle", std::filesystem::perms::all);+ std::ofstream(managed / "bottle/drive_c/user.reg") << "written later";+ std::ofstream(managed / "config.ini") << "written later";+ std::string failure;+ CHECK(RemoveManagedInstall(managed.string(), &failure) && failure.empty());+ CHECK(!std::filesystem::exists(managed));++ // A folder that cannot be emptied says why and keeps its install record, so a retry is+ // still recognized as Playden's and finishes the job.+ InstallsMergedDepotsAndWritesRecord(managed);+ std::filesystem::create_directories(managed / "locked");+ std::ofstream(managed / "locked/file.bin") << "stuck";+ CHECK(chmod((managed / "locked").c_str(), 0500) == 0);+ CHECK(!RemoveManagedInstall(managed.string(), &failure));+ CHECK(failure.find("could not be deleted") != std::string::npos);+ CHECK(std::filesystem::exists(managed / DepotInstaller::kInstallRecord));+ CHECK(chmod((managed / "locked").c_str(), 0700) == 0);+ CHECK(RemoveManagedInstall(managed.string()));+ CHECK(!std::filesystem::exists(managed));}} // namespacediff --git a/tests/homebrew_account_test.cpp b/tests/homebrew_account_test.cppindex 7158530..1e1df4e 100644--- a/tests/homebrew_account_test.cpp+++ b/tests/homebrew_account_test.cpp@@ -133,7 +133,7 @@ class LicenseCheckRunner final : public SessionInstallRunner {while (!release && !cancel) std::this_thread::sleep_for(1ms);return {false,true,{},0};}- bool Remove(const std::string&) override { return true; }+ std::string Remove(const std::string&) override { return {}; }};void Wait(Controller& controller,const std::function<bool()>& predicate) {const auto deadline = std::chrono::steady_clock::now()+4s;diff --git a/tests/homebrew_installed_locations_test.cpp b/tests/homebrew_installed_locations_test.cppindex 4f28e02..f1b898f 100644--- a/tests/homebrew_installed_locations_test.cpp+++ b/tests/homebrew_installed_locations_test.cpp@@ -79,7 +79,7 @@ class NoDownloads final : public playden::steam::SessionInstallRunner {const std::function<void(playden::steam::InstallStep)>&) override {return {false, false, "no downloads here", 0};}- bool Remove(const std::string&) override { return true; }+ std::string Remove(const std::string&) override { return {}; }};// Records every game the Cloud is asked about.class RecordingCloud final : public playden::steam::CloudRunner {diff --git a/tests/identity_fixtures_test.cpp b/tests/identity_fixtures_test.cppindex dc05ab0..5b6732d 100644--- a/tests/identity_fixtures_test.cpp+++ b/tests/identity_fixtures_test.cpp@@ -137,7 +137,7 @@ class Runner final : public playden::steam::SessionInstallRunner {++installs;return {false, false, "unexpected", 0};}- bool Remove(const std::string&) override { return true; }+ std::string Remove(const std::string&) override { return {}; }};template <typename Ready>diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cppindex 64a58c8..f6e34a5 100644--- a/tests/install_service_test.cpp+++ b/tests/install_service_test.cpp@@ -84,11 +84,13 @@ class Runner final : public SessionInstallRunner {progress(100, 100, held);return {true, false, {}, 4096};}- bool Remove(const std::string& root) override {+ std::string remove_failure; // the next removals fail with this, leaving the folder+ std::string Remove(const std::string& root) override {std::lock_guard<std::mutex> lock(mutex);removed.push_back(root);+ if (!remove_failure.empty()) return remove_failure;std::filesystem::remove_all(root);- return true;+ return {};}};@@ -341,11 +343,11 @@ class LocalRunner final : public InstallRunner {installed.push_back(id);return {true, false, {}, 7};}- bool Remove(const std::string& root) override {+ std::string Remove(const std::string& root) override {std::lock_guard<std::mutex> lock(mutex);removed.push_back(root);std::filesystem::remove_all(root);- return true;+ return {};}void Forget(const GameId& id) override {std::lock_guard<std::mutex> lock(mutex);@@ -572,8 +574,109 @@ void FolderCheckGuardsDownloadStart(const std::filesystem::path& root) {std::filesystem::remove_all(root);}+bool Uninstalling(const InstallSnapshot& snapshot, GameId id) {+ return std::find(snapshot.uninstalling.begin(), snapshot.uninstalling.end(), id) != snapshot.uninstalling.end();+}++void InstalledGameFolder(const std::filesystem::path& root) {+ std::filesystem::create_directories(root / "bin");+ std::ofstream(root / ".playden-install.json") << "{\"install_bytes\":4096}";+ std::ofstream(root / "bin/game.exe") << "game";+}++// Uninstalling deletes the game's folder on any drive, prepared or not, even while a long+// download runs; an uninstall Playden stopped carries on at the next start, and one whose files+// could not be deleted lists the game again and says why.+void UninstallDeletesFilesWhileDownloading(const std::filesystem::path& base) {+ std::filesystem::remove_all(base);+ const auto primary = base / "data";+ const auto m2 = base / "ext1";+ for (const auto id : {11, 12, 13}) InstalledGameFolder(primary / std::to_string(id));+ InstalledGameFolder(m2 / "21");+ std::filesystem::create_directories(primary);+ std::ofstream(primary / ".playden-games") << "11\tinternal\t4096\n12\tinternal\t4096\n13\tinternal\t4096\n21\tm2\t4096\n";+ const std::vector<InstallVolume> volumes{{"internal", "Console storage", primary.string(), true},+ {"m2", "M.2 SSD", m2.string(), true}};+ Sessions sessions;+ {+ Runner runner(sessions);+ runner.block = true;+ InstallService service(runner, volumes);+ service.Install(SteamId(30));+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) {+ const auto* job = JobOf(s, SteamId(30));+ return job && job->state == InstallJobState::kRunning;+ });+ // The download is still running: the folders go now, on console storage and on the M.2 drive.+ service.Uninstall(SteamId(11));+ service.Uninstall(SteamId(21));+ CHECK(!Installed(service.Snapshot(), SteamId(11)) && !Installed(service.Snapshot(), SteamId(21)));+ AwaitAt(__LINE__, service, [&](const InstallSnapshot& s) {+ return !Uninstalling(s, SteamId(11)) && !Uninstalling(s, SteamId(21)) && !std::filesystem::exists(primary / "11") &&+ !std::filesystem::exists(m2 / "21");+ });+ CHECK(runner.block);+ // The download stepped back into the queue and carried on.+ AwaitAt(__LINE__, service, [&](const InstallSnapshot& s) {+ const auto* job = JobOf(s, SteamId(30));+ return runner.installs >= 2 && job && job->state == InstallJobState::kRunning;+ });++ // Files that cannot be deleted: the game is listed again and the reason is reported once.+ {+ std::lock_guard<std::mutex> lock(runner.mutex);+ runner.remove_failure = "Some of the game's files could not be deleted (Permission denied).";+ }+ service.Uninstall(SteamId(12));+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) { return Installed(s, SteamId(12)) && !Uninstalling(s, SteamId(12)); });+ auto failures = service.TakeRemovalFailures();+ CHECK(failures.size() == 1 && failures[0].id == SteamId(12) && failures[0].reason.find("Permission denied") != std::string::npos);+ CHECK(service.TakeRemovalFailures().empty());+ CHECK(std::filesystem::exists(primary / "12/.playden-install.json"));+ // Uninstalling it again tries again.+ {+ std::lock_guard<std::mutex> lock(runner.mutex);+ runner.remove_failure.clear();+ }+ service.Uninstall(SteamId(12));+ AwaitAt(__LINE__, service, [&](const InstallSnapshot& s) {+ return !Installed(s, SteamId(12)) && !Uninstalling(s, SteamId(12)) && !std::filesystem::exists(primary / "12");+ });+ CHECK(service.TakeRemovalFailures().empty());+ runner.block = false;+ AwaitAt(__LINE__, service, [](const InstallSnapshot& s) { return Installed(s, SteamId(30)); });+ }+ {+ // Playden stopped before the folder was gone: the registry still names the uninstall, so the+ // next start deletes the folder instead of forgetting it.+ std::ofstream(primary / ".playden-games") << "13\tinternal\t4096\tremoving\n30\tinternal\t4096\n22\tusb0\t4096\tremoving\n"+ << "local-3bb51844-7741-4ff2-a345-cdcc1e62a872\tusb0\t4096\tremoving\n";+ Runner runner(sessions);+ InstallService service(runner, volumes);+ AwaitAt(__LINE__, service, [&](const InstallSnapshot& s) {+ return !Uninstalling(s, SteamId(13)) && !std::filesystem::exists(primary / "13");+ });+ const auto snapshot = service.Snapshot();+ CHECK(!Installed(snapshot, SteamId(13)) && Installed(snapshot, SteamId(30)));+ // One on a drive that is not connected waits for the drive, and the game cannot be installed meanwhile.+ CHECK(Uninstalling(snapshot, SteamId(22)) && !Installed(snapshot, SteamId(22)));+ service.Install(SteamId(22));+ CHECK(!JobOf(service.Snapshot(), SteamId(22)));+ }+ {+ std::ifstream registry(primary / ".playden-games");+ const std::string text((std::istreambuf_iterator<char>(registry)), std::istreambuf_iterator<char>());+ CHECK(text.find("13\t") == std::string::npos && text.find("22\tusb0\t4096\tremoving") != std::string::npos);+ // An imported game's key is kept as it was.+ CHECK(text.find("local-3bb51844-7741-4ff2-a345-cdcc1e62a872\tusb0\t4096\tremoving") != std::string::npos);+ }+ std::filesystem::remove_all(base);+}+int main() {FolderCheckGuardsDownloadStart(std::filesystem::temp_directory_path() / ("playden-foldercheck-service-" + std::to_string(::getpid())));+ UninstallDeletesFilesWhileDownloading(std::filesystem::temp_directory_path() /+ ("playden-uninstall-service-" + std::to_string(::getpid())));RunningJobReportsItsStep(std::filesystem::temp_directory_path() / ("playden-step-service-" + std::to_string(::getpid())));SpaceAdmission(std::filesystem::temp_directory_path() / ("playden-space-service-" + std::to_string(::getpid())));LiveTelemetryDecaysWithoutCallbacks(std::filesystem::temp_directory_path() / ("playden-rate-service-" + std::to_string(::getpid())));diff --git a/tests/local_copy_test.cpp b/tests/local_copy_test.cppindex 13b91d1..f3d6c9e 100644--- a/tests/local_copy_test.cpp+++ b/tests/local_copy_test.cpp@@ -150,7 +150,7 @@ struct Fixture {config.move_into_playden = [this](const ImportEntry& entry, const std::string& from, std::uint32_t number) {return runner->Move(*service, entry, from, number);};- config.remove_copy_folder = [this](const std::string& folder) { return runner->Remove(folder); };+ config.remove_copy_folder = [this](const std::string& folder) { return runner->Remove(folder).empty(); };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");@@ -924,6 +924,31 @@ void UninstallMissingCopy() {CHECK(fs::exists(fixture.source / "Bin/Rally.exe"));}+// Playden stopped while a copied game's folder was being deleted: at the next start the uninstall+// carries on before the library has read the imports, and the game still leaves the library.+void UninstallResumesAfterRestart() {+ Fixture fixture;+ fixture.Start();+ fixture.Add();+ CHECK(fixture.Outcome(fixture.local->CopyIntoPlayden(kId)).ok);+ WaitFor([&] { return fixture.Installed() && fixture.State() == ImportState::kCopied; }, "copied");+ fixture.Stop();+ // The registry as an uninstall leaves it until the folder is gone.+ const auto registry = fixture.games / ".playden-games";+ auto text = Read(registry);+ const auto line = text.find("local-" + std::string(kId));+ CHECK(line != std::string::npos);+ text.insert(text.find('\n', line), "\tremoving");+ Write(registry, text);+ fixture.Start();+ WaitFor([&] { return !fs::exists(fixture.Dest()) && !fixture.store->Find(kId).has_value(); }, "the uninstall finished");+ CHECK(fixture.store->Removed().size() == 1 && fixture.store->Removed()[0].id == kId);+ const auto snapshot = fixture.service->Snapshot();+ CHECK(snapshot.installed.empty() && snapshot.uninstalling.empty());+ CHECK(!Has(Read(registry), "local-" + std::string(kId)));+ WaitFor([&] { return fixture.local->Snapshot()->games.empty(); }, "the game out of the library");+}+} // namespace// A drop-folder game copied to another drive: once the copy is committed its drop-folder files go@@ -1133,6 +1158,7 @@ int main() {LinkInTheCopyRefused();ReconcileWaitsForTheDrive();UninstallMissingCopy();+ UninstallResumesAfterRestart();std::puts("local copy tests passed");return 0;}