Play your Steam library's Windows games on a jailbroken PS5.
C++
Restore kept saves past a file that cannot be read
Leonardo Lobato1c134066c4b9
diff --git a/src/homebrew/runtime_host.cpp b/src/homebrew/runtime_host.cppindex 32c29d4..e371ac8 100644--- a/src/homebrew/runtime_host.cpp+++ b/src/homebrew/runtime_host.cpp@@ -3725,6 +3725,9 @@ constexpr char kKeptMarker[] = "playden-kept.json";constexpr char kKeptSuffix[] = ".kept";// In a bottle once its kept saves were brought back: a kept copy still beside it is then older.constexpr char kRestoredMarker[] = ".playden-saves-restored";+// In a bottle whose kept saves came back but for files that could not be read: the kept copy stays beside it, as it+// still holds those files, but is not brought back again.+constexpr char kPartlyRestoredMarker[] = ".playden-saves-partly-restored";// Save backups: <games>/<install id>.saves/<Unix time>/users, the newest few kept.constexpr char kSaveBackupSuffix[] = ".saves";constexpr std::size_t kSaveBackupsKept = 3;@@ -3872,22 +3875,34 @@ std::optional<KeptBottleView> ReadKeptMarker(const std::filesystem::path& kept,// owner bits, links are skipped (Wine links point outside the bottle).// Files with another link are skipped (logged): their content may be another file's (a private// one linked into the profile), which the copy would make the profile's own.+// With `unreadable`, a file or folder that cannot be read is skipped (logged, its path relative to the+// first `from` added) and the rest is copied; without it, the copy stops there. Writing stops it either way.bool CopySavesTree(const std::filesystem::path& from, const std::filesystem::path& to, std::string* error,- const std::function<void(const std::string&)>& log) {+ const std::function<void(const std::string&)>& log,+ std::vector<std::string>* unreadable = nullptr, const std::string& relative = {}) {struct stat info{};if (lstat(to.c_str(), &info) != 0) {if (mkdir(to.c_str(), 0700) != 0) { *error = "cannot create " + to.filename().string(); return false; }} else if (!S_ISDIR(info.st_mode)) {*error = to.filename().string() + " is not a folder"; return false;}+ const auto skip = [&](const std::string& path, int code) {+ log("saves copy skips " + path + ": cannot read (errno " + std::to_string(code) + ")");+ unreadable->push_back(path);+ };std::error_code iteration;for (std::filesystem::directory_iterator it(from, iteration), end; !iteration && it != end; it.increment(iteration)) {const auto source = it->path();const auto target = to / source.filename();- if (lstat(source.c_str(), &info) != 0) continue;+ const auto name = relative.empty() ? source.filename().string() : relative + "/" + source.filename().string();+ if (lstat(source.c_str(), &info) != 0) {+ // A folder that can be listed but not searched: its entries cannot be looked at, so they count as unread.+ if (unreadable && errno != ENOENT) skip(name, errno);+ continue;+ }if (S_ISLNK(info.st_mode)) continue;if (S_ISDIR(info.st_mode)) {- if (!CopySavesTree(source, target, error, log)) return false;+ if (!CopySavesTree(source, target, error, log, unreadable, name)) return false;continue;}if (!S_ISREG(info.st_mode)) continue;@@ -3896,6 +3911,10 @@ bool CopySavesTree(const std::filesystem::path& from, const std::filesystem::patcontinue;}const int input = open(source.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC);+ if (input < 0 && unreadable) {+ skip(name, errno);+ continue;+ }struct stat opened{};if (input >= 0 && (fstat(input, &opened) != 0 || opened.st_ino != info.st_ino || opened.st_dev != info.st_dev ||opened.st_nlink != 1)) {@@ -3908,10 +3927,12 @@ bool CopySavesTree(const std::filesystem::path& from, const std::filesystem::patconst int output = open(temporary.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,(info.st_mode & 0700) | 0600);bool ok = input >= 0 && output >= 0;+ int read_error = 0;std::vector<char> buffer(64 * 1024); // heap: console worker stacks are smallwhile (ok) {const auto count = read(input, buffer.data(), buffer.size());if (count < 0 && errno == EINTR) continue;+ if (count < 0) read_error = errno;if (count <= 0) { ok = count == 0; break; }for (ssize_t offset = 0; ok && offset < count;) {const auto written = write(output, buffer.data() + offset, static_cast<std::size_t>(count - offset));@@ -3922,12 +3943,21 @@ bool CopySavesTree(const std::filesystem::path& from, const std::filesystem::patok = ok && fsync(output) == 0;if (input >= 0) close(input);if (output >= 0) ok = close(output) == 0 && ok;+ if (!ok && read_error != 0 && unreadable) {+ unlink(temporary.c_str());+ skip(name, read_error);+ continue;+ }if (!ok || rename(temporary.c_str(), target.c_str()) != 0) {unlink(temporary.c_str());*error = "cannot copy " + source.filename().string();return false;}}+ if (iteration && unreadable) {+ skip(relative.empty() ? from.filename().string() : relative, iteration.value());+ return true;+ }if (iteration) { *error = "cannot list " + from.filename().string(); return false; }return true;}@@ -4421,7 +4451,18 @@ std::optional<RuntimeHost::RemovalFailure> RuntimeHost::KeepBottle(if (fd >= 0) close(fd);if (!written) return RemovalFailure{"bottle-keep", "The game's saves could not be marked as kept."};const bool restored = lstat((bottle / kRestoredMarker).c_str(), &info) == 0;- if (lstat(kept.c_str(), &info) == 0 && !restored) {+ const bool partly_restored = lstat((bottle / kPartlyRestoredMarker).c_str(), &info) == 0;+ if (lstat(kept.c_str(), &info) == 0 && partly_restored) {+ // The kept copy came back but for files that could not be read: this bottle has the newer saves and becomes the+ // kept copy; the older one, which still holds those files, is set aside beside it, never deleted.+ const auto aside = bottle.parent_path() / (install_id + ".set-aside-" + operation_id);+ Log("remove sets the partly restored kept bottle aside install=" + install_id);+ if (rename(kept.c_str(), aside.c_str()) != 0)+ return RemovalFailure{"bottle-keep", "An older copy of the game's saves could not be set aside (error " ++ std::to_string(errno) + ")."};+ if (rename(bottle.c_str(), kept.c_str()) != 0)+ return RemovalFailure{"bottle-keep", "The game's saves could not be kept (error " + std::to_string(errno) + ")."};+ } else if (lstat(kept.c_str(), &info) == 0 && !restored) {// The kept copy's saves were never brought back into this bottle (a preparation after a move or// reinstall stopped before that): the kept copy holds them and stays. The newer bottle is set// aside beside it, never deleted.@@ -4706,13 +4747,20 @@ std::string RuntimeHost::RestoreKeptSaves(Composition& c, std::uint32_t app) {const auto kept = bottle.parent_path() / (record->install_id + kKeptSuffix);struct stat info{};if (lstat(kept.c_str(), &info) != 0 || !S_ISDIR(info.st_mode)) return {};+ // This bottle already has the kept saves (some files of them, for a partial restore): the kept copy is older than+ // what the game saved since, and is not copied over it.+ if (lstat((bottle / kRestoredMarker).c_str(), &info) == 0 || lstat((bottle / kPartlyRestoredMarker).c_str(), &info) == 0)+ return {};const auto from = kept / "library/prefixes/game/drive_c/users";const auto to = bottle / "library/prefixes/game/drive_c/users";std::string reason;+ // Files that cannot be read (left by FTP with another owner, or a mode that shuts Playden out) are skipped: the+ // rest comes back, and the kept copy, which still holds them, stays.+ std::vector<std::string> unreadable;if (lstat(from.c_str(), &info) == 0 && S_ISDIR(info.st_mode)) {if (lstat(to.parent_path().c_str(), &info) != 0 || !S_ISDIR(info.st_mode))return "The game's new bottle has no drive C for its kept saves.";- if (!CopySavesTree(from, to, &reason, [this](const std::string& line) { Log(line); })) {+ if (!CopySavesTree(from, to, &reason, [this](const std::string& line) { Log(line); }, &unreadable)) {Log("restore kept saves failed app=" + std::to_string(app) + " " + reason);return "The saves kept from the previous install could not be brought back: " + reason;}@@ -4721,11 +4769,22 @@ std::string RuntimeHost::RestoreKeptSaves(Composition& c, std::uint32_t app) {SettleBottle(c, *record, "restore");}{- const int fd = open((bottle / kRestoredMarker).c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);- const bool marked = fd >= 0 && fsync(fd) == 0;+ const auto marker = bottle / (unreadable.empty() ? kRestoredMarker : kPartlyRestoredMarker);+ std::string skipped;+ for (const auto& path : unreadable) skipped += path + "\n";+ const int fd = open(marker.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);+ const bool marked = fd >= 0 &&+ (skipped.empty() || write(fd, skipped.data(), skipped.size()) == static_cast<ssize_t>(skipped.size())) &&+ fsync(fd) == 0;if (fd >= 0) close(fd);if (!marked) return "The saves were brought back, but that could not be recorded.";}+ if (!unreadable.empty()) {+ Log("restore kept saves app=" + std::to_string(app) + " skipped=" + std::to_string(unreadable.size()) ++ " kept_removed=0");+ PublishKeptBottles(c);+ return {};+ }std::error_code error;RemoveTreeNoFollow(kept, error);Log("restore kept saves app=" + std::to_string(app) + " kept_removed=" + std::to_string(!error));diff --git a/tests/homebrew_runtime_host_test.cpp b/tests/homebrew_runtime_host_test.cppindex 735fdc3..bdd43a6 100644--- a/tests/homebrew_runtime_host_test.cpp+++ b/tests/homebrew_runtime_host_test.cpp@@ -3396,6 +3396,116 @@ void LocalGameKeepsItsBottle() {}}+// One unreadable file in the kept saves (put there over FTP, #569): Prepare brings back the rest and logs the one+// skipped. The kept copy stays, as it still holds that file; a later Prepare does not copy it over newer saves, and the+// next uninstall keeps the newer bottle and sets the older copy aside. Nothing is deleted.+void RestoresReadableKeptSaves() {+ if (geteuid() == 0) return; // root reads a file of mode 000+ 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}; };+ 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"};+ };+ auto log_mutex = std::make_shared<std::mutex>();+ auto lines = std::make_shared<std::vector<std::string>>();+ config.log = [log_mutex, lines](const std::string& line) {+ std::lock_guard<std::mutex> lock(*log_mutex);+ lines->push_back(line);+ };+ const auto logged = [log_mutex, lines](const std::string& part) {+ std::lock_guard<std::mutex> lock(*log_mutex);+ return std::any_of(lines->begin(), lines->end(), [&](const std::string& line) { return line.find(part) != std::string::npos; });+ };+ Write(fixture.root / "data/homebrew/FAKE77031/games/220/hl2.exe", "existing-game", 0644);+ const auto games = fixture.root / "data/playden/games";+ const auto bottle = games / "steam-220";+ const auto kept = games / "steam-220.kept";+ const auto users = fs::path("library/prefixes/game/drive_c/users");+ const auto folder = users / "steamuser/Documents/Game/savegames";+ const auto prepare = [](RuntimeHost& host) {+ const auto before = host.Snapshot().prepares_handled;+ host.Prepare(Hl2());+ return WaitFor(host, [before](const RuntimeSnapshot& s) { return !s.preparing_app && s.prepares_handled > before; }, 60);+ };+ const auto remove = [](RuntimeHost& host) {+ const auto before = host.Snapshot().removals_handled;+ host.Remove(220, false);+ return WaitFor(host, [before](const RuntimeSnapshot& s) { return s.removals_handled > before; }, 30);+ };+ RuntimeHost host(config);+ CHECK(host.Start());+ CHECK(WaitFor(host, Started).kit_available);+ CHECK(!prepare(host).refusals.count(220));+ host.SetTitleNames({{220, "Half-Life 2"}});+ for (const char* name : {"SGTA400", "SGTA401", "SGTA402"}) Write(bottle / folder / name, std::string("save ") + name, 0600);+ auto removed = remove(host);+ CHECK(!removed.removal_failures.count(220) && removed.installs.empty() && fs::is_directory(kept));+ CHECK(chmod((kept / folder / "SGTA401").c_str(), 0) == 0);+ // A folder that can be listed but not searched: its files cannot be looked at either.+ const auto profiles = users / "steamuser/Documents/Game/Profiles";+ Write(kept / profiles / "profile.dat", "profile", 0600);+ CHECK(chmod((kept / profiles).c_str(), 0444) == 0);++ // Prepared again: the readable saves come back, the unreadable one is named in the log, Prepare succeeds.+ auto ready = prepare(host);+ if (ready.refusals.count(220)) std::fprintf(stderr, "Prepare refused: %s\n", ready.refusals.at(220).c_str());+ CHECK(!ready.refusals.count(220) && ready.installs.size() == 1 && ready.installs[0].state == PreparationState::kReady);+ CHECK(Read(bottle / folder / "SGTA400") == "save SGTA400" && Read(bottle / folder / "SGTA402") == "save SGTA402");+ CHECK(!fs::exists(bottle / folder / "SGTA401"));+ CHECK(logged("saves copy skips steamuser/Documents/Game/savegames/SGTA401: cannot read (errno " ++ std::to_string(EACCES) + ")"));+ CHECK(logged("saves copy skips steamuser/Documents/Game/Profiles/profile.dat: cannot read (errno " ++ std::to_string(EACCES) + ")"));+ CHECK(logged("restore kept saves app=220 skipped=2 kept_removed=0"));+ CHECK(!fs::exists(bottle / profiles / "profile.dat"));+ CHECK(fs::is_directory(kept) && Read(kept / folder / "SGTA400") == "save SGTA400");++ // The game is played, then updated: Update's Prepare does not bring the older saves back over the newer ones.+ Write(bottle / folder / "SGTA400", "newer save", 0600);+ Write(fixture.root / "data/homebrew/FAKE77031/games/220/hl2.exe", "updated-game", 0644);+ Write(fixture.root / "data/homebrew/FAKE77031/games/220" / playden::installs::kFilesChangedName, "v1\n", 0644);+ host.RecheckInstalls();+ WaitFor(host, [](const RuntimeSnapshot& s) { return !s.installs.empty() && s.installs[0].files_changed; });+ ready = prepare(host);+ if (ready.refusals.count(220)) std::fprintf(stderr, "Prepare refused: %s\n", ready.refusals.at(220).c_str());+ CHECK(!ready.refusals.count(220));+ CHECK(Read(bottle / folder / "SGTA400") == "newer save");++ // Uninstalled again keeping saves: the newer bottle becomes the kept copy; the older one, with the file that+ // could not be read, is set aside.+ CHECK(chmod((kept / folder / "SGTA401").c_str(), 0600) == 0);+ CHECK(chmod((kept / profiles).c_str(), 0700) == 0);+ removed = remove(host);+ CHECK(!removed.removal_failures.count(220) && removed.installs.empty());+ CHECK(Read(kept / folder / "SGTA400") == "newer save" && !fs::exists(bottle));+ bool aside = false;+ for (const auto& entry : fs::directory_iterator(games))+ aside |= entry.path().filename().string().rfind("steam-220.set-aside-", 0) == 0 &&+ Read(entry.path() / folder / "SGTA401") == "save SGTA401" &&+ Read(entry.path() / profiles / "profile.dat") == "profile";+ CHECK(aside);+ host.Shutdown();+}+void RemovesTitleKeepingSaves() {Fixture fixture;auto config = fixture.Config();@@ -6997,6 +7107,7 @@ int main(int argc, char** argv) {UpdateWithoutSavedSettingsKeepsThePublication();StaleTitleKeeps120HzChoicesForUpdate();RemovesTitleKeepingSaves();+ RestoresReadableKeptSaves();PreparesGameInstalledAgainElsewhere();SharedProfileKitOpensSaves();ExistingBottleOpensWithSharedKit();