Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Reclaim staging left by an earlier install of a game

Leonardo Lobato2bd52910407a
diff --git a/src/runtime/package_installer.cpp b/src/runtime/package_installer.cpp
index e2bc480..e49b645 100644
--- a/src/runtime/package_installer.cpp
+++ b/src/runtime/package_installer.cpp
@@ -340,24 +340,35 @@ bool MarkerEquals(const fs::path& path, const std::string& expected) {
return bytes && *bytes == expected;
}
-// Whether a marker names this install: its schema, kind, install id and title id.
-// The kit, runtime and recipe lines say which kit made the folder; they decide
-// whether the title needs Update (TitleCurrent), not whose folder it is.
-bool MarkerIdentifies(const fs::path& path, const RuntimeInstall& record, const char* kind) {
+// Whether a marker holds every wanted line.
+bool MarkerHasLines(const fs::path& path, const std::vector<std::string>& wanted) {
const auto bytes = ReadBounded(path, 4096);
if (!bytes) return false;
- const std::string wanted[] = {"schema=1", std::string("kind=") + kind, "install_id=" + record.install_id,
- "title_id=" + record.title_id};
- bool seen[4] = {};
+ std::vector<bool> seen(wanted.size());
std::size_t start = 0;
while (start < bytes->size()) {
auto end = bytes->find('\n', start);
if (end == std::string::npos) end = bytes->size();
const auto line = bytes->substr(start, end - start);
- for (std::size_t i = 0; i < 4; ++i) seen[i] = seen[i] || line == wanted[i];
+ for (std::size_t i = 0; i < wanted.size(); ++i) seen[i] = seen[i] || line == wanted[i];
start = end + 1;
}
- return seen[0] && seen[1] && seen[2] && seen[3];
+ return std::find(seen.begin(), seen.end(), false) == seen.end();
+}
+
+// Whether a marker names this install: its schema, kind, install id and title id.
+// The kit, runtime and recipe lines say which kit made the folder; they decide
+// whether the title needs Update (TitleCurrent), not whose folder it is.
+bool MarkerIdentifies(const fs::path& path, const RuntimeInstall& record, const char* kind) {
+ return MarkerHasLines(path, {"schema=1", std::string("kind=") + kind, "install_id=" + record.install_id,
+ "title_id=" + record.title_id});
+}
+
+// Whether a staging folder's marker was written by an earlier install of this game.
+// The folder is named after the install id; the title id, kit and recipe can change
+// between installs (a reinstall can get another title), so they are not compared.
+bool StageMarkerNamesInstall(const fs::path& path, const RuntimeInstall& record, const std::string& kind) {
+ return MarkerHasLines(path, {"schema=1", "kind=" + kind, "install_id=" + record.install_id});
}
const playden::steam::json::Value* Required(
@@ -1118,8 +1129,13 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(
if (staging_exists) {
if (stage_kind == "template" && !IsPrivateDirectory(staging))
return Failed("staging-collision", "existing bottle staging has wrong owner or permissions");
- if (!MarkerEquals(staging / kStageOwner, expected_stage))
+ // An earlier install of this game (another kit, title or recipe) can leave its
+ // incomplete staging behind; it is ours and holds nothing published.
+ const bool current = MarkerEquals(staging / kStageOwner, expected_stage);
+ if (!current && !StageMarkerNamesInstall(staging / kStageOwner, record, stage_kind))
return Failed("staging-collision", "staging path is not owned by this install");
+ if (!current && operations_.log)
+ operations_.log("package stage=" + stage_kind + " reclaims staging left by an earlier install");
// RuntimeInstallService has already established stopped activity. Only our
// identity-marked incomplete staging directory is eligible for cleanup.
if (!RemoveTreeNoFollow(staging.string()))
diff --git a/tests/runtime_package_installer_test.cpp b/tests/runtime_package_installer_test.cpp
index ff5084b..ca86d10 100644
--- a/tests/runtime_package_installer_test.cpp
+++ b/tests/runtime_package_installer_test.cpp
@@ -741,6 +741,39 @@ void EmptyUnmarkedStagingIsReclaimed() {
CHECK(std::filesystem::exists(stray_staging / "unknown"));
}
+// Staging left by an earlier install of the same game (another kit and title) is
+// reclaimed; staging marked for another install is still refused.
+void EarlierInstallStagingIsReclaimed() {
+ Fixture fixture;
+ auto installer = fixture.Installer();
+ const auto staging = fixture.host_data / ("playden/staging/title-" + Record().install_id);
+ Write(staging / ".playden-stage-owner",
+ "schema=1\nkind=title\ninstall_id=" + Record().install_id + "\ntitle_id=FAKE78002\nkit_id=old-kit\n", 0600);
+ Write(staging / "eboot.bin", "old shell", 0600);
+ CHECK(installer.StageTitle(Record()).state == PackageActionState::kComplete);
+ CHECK(!std::filesystem::exists(staging));
+ CHECK(Read(fixture.host_data / "homebrew/FAKE78001/eboot.bin") != "old shell");
+
+ Fixture other;
+ auto refusing = other.Installer();
+ const auto other_staging = other.host_data / ("playden/staging/title-" + Record().install_id);
+ Write(other_staging / ".playden-stage-owner",
+ "schema=1\nkind=title\ninstall_id=install-other\ntitle_id=FAKE78001\n", 0600);
+ Write(other_staging / "eboot.bin", "keep", 0600);
+ CHECK(refusing.StageTitle(Record()).code == "staging-collision");
+ CHECK(Read(other_staging / "eboot.bin") == "keep");
+
+ Fixture bottle;
+ auto copier = bottle.Installer();
+ const auto bottle_staging = bottle.host_data / ("playden/staging/bottle-" + Record().install_id);
+ Write(bottle_staging / ".playden-stage-owner",
+ "schema=1\nkind=template\ninstall_id=" + Record().install_id + "\nkit_id=old-kit\n", 0600);
+ CHECK(::chmod(bottle_staging.c_str(), 0700) == 0);
+ CHECK(copier.StageTitle(Record()).state == PackageActionState::kComplete);
+ CHECK(copier.CopyTemplate(Record()).state == PackageActionState::kComplete);
+ CHECK(!std::filesystem::exists(bottle_staging));
+}
+
void LateSyncFailureIsReestablishedOnRetry() {
Fixture fixture;
fixture.fail_title_post_rename_once = true;
@@ -1081,6 +1114,7 @@ int main() {
FixedTitleRefreshesToAnyTitleShell();
CreatedFoldersIgnoreUmask();
EmptyUnmarkedStagingIsReclaimed();
+ EarlierInstallStagingIsReclaimed();
CollisionsTraversalSymlinksAndModesFailClosed();
RestartDoesNotRecopyOrDestroyMutableFiles();
CorruptPublicationAndInvalidDriveTableAreRejected();