Skip to content

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

C++

Retry transient package staging rename failures

Leonardo Lobato92c224c3aecc
diff --git a/src/runtime/package_installer.cpp b/src/runtime/package_installer.cpp
index 4ede095..ac69f92 100644
--- a/src/runtime/package_installer.cpp
+++ b/src/runtime/package_installer.cpp
@@ -17,6 +17,7 @@
#include <set>
#include <vector>
#include <system_error>
+#include <thread>
#include <utility>
#include <sys/stat.h>
@@ -1293,10 +1294,36 @@ PackageActionResult FilesystemPackageInstaller::CopyMappings(
return Failed("destination-collision", "final package path already exists");
if (!SyncAncestors(final.parent_path()))
return Failed("sync-failed", "destination parent was not durably synced before rename");
- fs::rename(staging, final, error);
- if (error)
- return Failed("atomic-rename-failed",
- "package staging and destination must support one atomic rename");
+ // Retry brief filesystem contention without recopying the durable staging tree.
+ // Cross-volume moves must still fail: copying into the watched title is not atomic.
+ constexpr int kRenameAttempts = 4;
+ for (int attempt = 1; attempt <= kRenameAttempts; ++attempt) {
+ if (operations_.canceled && operations_.canceled()) return Canceled();
+ if (attempt > 1) {
+ if (!PathComponentsHaveNoSymlink(paths_.host_data_root, final_parent_relative, true))
+ return Failed("destination-invalid", "final destination contains a symbolic link");
+ if (ExistsNoThrow(final, &final_known) || !final_known)
+ return Failed("destination-collision", "final package path appeared or became unavailable during rename retry");
+ }
+ error.clear();
+ if (operations_.rename_path) operations_.rename_path(staging, final, error);
+ else fs::rename(staging, final, error);
+ if (!error) break;
+ const std::string detail = "errno=" + std::to_string(error.value()) + " category=" + error.category().name() +
+ " message=" + error.message();
+ if (operations_.log)
+ operations_.log("package rename stage=" + stage_kind + " install=" + record.install_id +
+ " attempt=" + std::to_string(attempt) + " from=" + staging.string() +
+ " to=" + final.string() + " " + detail);
+ const bool transient = error == std::errc::device_or_resource_busy || error == std::errc::interrupted ||
+ error == std::errc::resource_unavailable_try_again;
+ if (!transient || attempt == kRenameAttempts)
+ return Failed("atomic-rename-failed",
+ std::string(error == std::errc::cross_device_link
+ ? "package staging and destination must be on the same filesystem"
+ : "package staging rename failed") + " (" + detail + ")");
+ std::this_thread::sleep_for(std::chrono::milliseconds(100 * attempt));
+ }
if (!SyncTree(final, false) || !SyncAncestors(final.parent_path()) ||
!SyncAncestors(staging.parent_path()))
return Failed("sync-failed", "atomic package rename was not durably synced");
diff --git a/src/runtime/package_installer.h b/src/runtime/package_installer.h
index 2475eb5..10c31ff 100644
--- a/src/runtime/package_installer.h
+++ b/src/runtime/package_installer.h
@@ -136,6 +136,8 @@ struct PackageInstallerOperations {
// Optional test/platform seam. With no override, the implementation uses
// open(2)+fsync(2). `directory` distinguishes directory durability barriers.
std::function<bool(const std::filesystem::path&, bool directory)> sync_path;
+ // Optional test/platform seam for the atomic staging rename; defaults to filesystem::rename.
+ std::function<void(const std::filesystem::path&, const std::filesystem::path&, std::error_code&)> rename_path;
// Optional: one line per package copy and title check with its file count and the
// time spent in each part, for measuring preparation on the console.
std::function<void(const std::string&)> log;
diff --git a/tests/runtime_package_installer_test.cpp b/tests/runtime_package_installer_test.cpp
index 5169689..84c1cdb 100644
--- a/tests/runtime_package_installer_test.cpp
+++ b/tests/runtime_package_installer_test.cpp
@@ -178,6 +178,8 @@ struct Fixture {
bool publish_public_profile = false;
bool fail_publication = false;
bool legacy_descriptor = false;
+ std::function<void(const std::filesystem::path&, const std::filesystem::path&, std::error_code&)> rename_path;
+ std::vector<std::string> logs;
Fixture() {
Write(kit / "titles/variant/eboot.bin", "native", 0755);
@@ -248,6 +250,8 @@ struct Fixture {
FilesystemPackageInstaller Installer(bool inject_sync = true) {
PackageInstallerOperations operations;
+ operations.rename_path = rename_path;
+ operations.log = [&](const std::string& message) { logs.push_back(message); };
operations.hash_file = TestHash;
operations.hash_bytes = TestHashBytes;
operations.free_bytes = [&](const std::filesystem::path&) {
@@ -300,6 +304,76 @@ struct Fixture {
}
};
+void TransientRenameFailuresKeepTheStagedPackage() {
+ for (const auto failure : {std::errc::device_or_resource_busy, std::errc::interrupted,
+ std::errc::resource_unavailable_try_again}) {
+ for (const bool bottle : {false, true}) {
+ Fixture fixture;
+ int attempts = 0;
+ std::uintmax_t stage_inode = 0;
+ fixture.rename_path = [&](const auto& source, const auto& destination, std::error_code& error) {
+ struct stat info{};
+ CHECK(::stat(source.c_str(), &info) == 0);
+ if (++attempts == 1) stage_inode = info.st_ino;
+ CHECK(stage_inode == info.st_ino);
+ if (attempts < 3) error = std::make_error_code(failure);
+ else std::filesystem::rename(source, destination, error);
+ };
+ auto installer = fixture.Installer();
+ const auto result = bottle ? installer.CopyTemplate(Record()) : installer.StageTitle(Record());
+ CHECK(result.state == PackageActionState::kComplete);
+ const auto final = fixture.host_data / (bottle ? "playden/games/install-hl2/library/prefixes/game" : "homebrew/FAKE78001");
+ CHECK(std::filesystem::is_regular_file(final / (bottle ? "drive_c/windows/system.reg" : "eboot.bin")));
+ CHECK(attempts == 3);
+ CHECK(std::any_of(fixture.logs.begin(), fixture.logs.end(), [&](const auto& line) {
+ return line.find("errno=" + std::to_string(static_cast<int>(failure))) != std::string::npos;
+ }));
+ }
+ }
+}
+
+void RenameFailuresReportTheActualCondition() {
+ for (const auto failure : {std::errc::cross_device_link, std::errc::no_space_on_device,
+ std::errc::permission_denied, std::errc::file_exists,
+ std::errc::no_such_file_or_directory, std::errc::device_or_resource_busy}) {
+ Fixture fixture;
+ int attempts = 0;
+ fixture.rename_path = [&](const auto&, const auto&, std::error_code& error) {
+ ++attempts;
+ error = std::make_error_code(failure);
+ };
+ auto installer = fixture.Installer();
+ const auto result = installer.StageTitle(Record());
+ CHECK(result.state == PackageActionState::kFailed);
+ CHECK(result.code == "atomic-rename-failed");
+ CHECK(result.message.find("errno=" + std::to_string(static_cast<int>(failure))) != std::string::npos);
+ CHECK(result.message.find(std::make_error_code(failure).message()) != std::string::npos);
+ CHECK((result.message.find("same filesystem") != std::string::npos) == (failure == std::errc::cross_device_link));
+ CHECK(attempts == (failure == std::errc::device_or_resource_busy ? 4 : 1));
+ CHECK(!std::filesystem::exists(fixture.host_data / "homebrew/FAKE78001"));
+ CHECK(std::filesystem::is_regular_file(fixture.host_data / "playden/staging/title-install-hl2/eboot.bin"));
+ }
+}
+
+void RenameRetryRespectsCancellationAndCollisions() {
+ for (const bool cancel : {false, true}) {
+ Fixture fixture;
+ int attempts = 0;
+ fixture.rename_path = [&](const auto&, const auto& destination, std::error_code& error) {
+ ++attempts;
+ if (cancel) fixture.canceled = true;
+ else Write(destination / "foreign", "keep me");
+ error = std::make_error_code(std::errc::device_or_resource_busy);
+ };
+ auto installer = fixture.Installer();
+ const auto result = installer.StageTitle(Record());
+ CHECK(result.state == (cancel ? PackageActionState::kCanceled : PackageActionState::kFailed));
+ CHECK(result.code == (cancel ? "canceled" : "destination-collision"));
+ CHECK(attempts == 1);
+ if (!cancel) CHECK(Read(fixture.host_data / "homebrew/FAKE78001/foreign") == "keep me");
+ }
+}
+
void LegacyDescriptorOnlyBindsHistoricalRoot() {
for (const auto* root : {"/mnt/games/220", "/data/homebrew/FAKE78001/games/220"}) {
Fixture fixture; auto record = Record(); record.install_path = root;
@@ -1118,6 +1192,9 @@ int main() {
UnreadableFreeSpaceRequiresExplicitMode();
SyncFailureNeverPublishesWatchedTitle();
NativeFsyncFallbackPublishesAtomically();
+ TransientRenameFailuresKeepTheStagedPackage();
+ RenameFailuresReportTheActualCondition();
+ RenameRetryRespectsCancellationAndCollisions();
LateSyncFailureIsReestablishedOnRetry();
PrivateTitleFoldersAreRepublished();
AnyTitleVariantServesEveryTitle();