Skip to content

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

C++

Make the M.2's games folder as root (#57)

Leonardo Lobatoc73597fb61d0
diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cpp
index 64bd87a..35fc95f 100644
--- a/src/homebrew/controller.cpp
+++ b/src/homebrew/controller.cpp
@@ -1109,12 +1109,12 @@ void Controller::TickInstalls() {
if (snapshot.revision == install_revision_ && !account_changed && !space_due && !licenses_due) return;
{
std::vector<LiveVolume> volumes;
+ // Only attached drives are listed; one Playden can't write to is still attached, and its
+ // space is still readable (the query falls back from a missing games folder to the drive).
for (const auto& volume : snapshot.volumes) {
- LiveVolume live{volume.id, volume.label, volume.available, std::nullopt, std::nullopt};
- if (volume.available) {
- const auto space = steam::QueryVolumeSpace(volume.games_root);
- if (space) { live.total_bytes = space->total_bytes; live.free_bytes = space->free_bytes; }
- }
+ LiveVolume live{volume.id, volume.label, true, std::nullopt, std::nullopt, volume.available};
+ const auto space = steam::QueryVolumeSpace(volume.games_root);
+ if (space) { live.total_bytes = space->total_bytes; live.free_bytes = space->free_bytes; }
volumes.push_back(std::move(live));
}
model_.SetFreeSpaceSupported(steam::VolumeSpaceSupported());
diff --git a/src/homebrew/ps5.cpp b/src/homebrew/ps5.cpp
index b98c427..2aa209c 100644
--- a/src/homebrew/ps5.cpp
+++ b/src/homebrew/ps5.cpp
@@ -286,8 +286,14 @@ int main() {
playden::app::AppendLogLine(log_root, "install.log", line);
});
playden::LocalSessionRepository install_sessions(paths.steam_state_root);
- playden::steam::InstallService installs(install_runner, install_sessions,
- playden::steam::DiscoverInstallVolumes(playden::steam::ConsoleVolumeCandidates(paths.title_root)));
+ // A console-formatted M.2's root belongs to root: Playden's folder there is made as root.
+ auto install_volumes = playden::steam::DiscoverInstallVolumes(
+ playden::steam::ConsoleVolumeCandidates(paths.title_root),
+ [](const std::function<bool()>& work) { return playden::ps5::WithRoot(work); });
+ 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, install_sessions, std::move(install_volumes));
playden::steam::CmClient cloud_cm(paths.trust_store, library_storage);
cloud_cm.SetOwner("cloud");
cloud_cm.SetSharedSession(steam_cm);
diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cpp
index 5ea139f..08ad781 100644
--- a/src/homebrew/simulation.cpp
+++ b/src/homebrew/simulation.cpp
@@ -1212,7 +1212,7 @@ void Simulation::UpdateVolumes(const std::vector<LiveVolume>& volumes, const std
volume.id = live_volume.id;
volume.label = live_volume.label;
volume.available = live_volume.available;
- volume.writable = live_volume.available;
+ volume.writable = live_volume.available && live_volume.writable;
volume.capacity_known = live_volume.total_bytes.has_value() && live_volume.free_bytes.has_value();
if (volume.capacity_known) {
volume.capacity_bytes = *live_volume.total_bytes;
@@ -2866,7 +2866,7 @@ void Simulation::rebuild_view() {
item.value = format_bytes(free) + " free";
else
item.value = "Free space unknown";
- item.subtitle = !volume->available ? "Disconnected" : !volume->writable ? "Read-only" : "Available";
+ item.subtitle = !volume->available ? "Disconnected" : !volume->writable ? "No write access" : "Available";
item.enabled = volume->available && volume->writable;
item.selected = volume->id == (live() && picking_install_volume_ ? install_target() : selected_volume_id_);
}
diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hpp
index b3bfb12..33df41a 100644
--- a/src/homebrew/simulation.hpp
+++ b/src/homebrew/simulation.hpp
@@ -73,9 +73,10 @@ struct LiveCompletedDownload {
struct LiveVolume {
std::string id;
std::string label;
- bool available = true;
+ bool available = true; // the drive is attached
std::optional<std::uint64_t> total_bytes;
std::optional<std::uint64_t> free_bytes;
+ bool writable = true; // Playden can make its games folder there
};
// The compatibility kit in use, for Settings > About. Every field is empty when no kit is usable.
struct LiveKitInfo {
diff --git a/src/homebrew/view.cpp b/src/homebrew/view.cpp
index 92c4cfb..dd0f1fd 100644
--- a/src/homebrew/view.cpp
+++ b/src/homebrew/view.cpp
@@ -1934,7 +1934,7 @@ class Render::Impl
ui::text(scene_, fonts_.display, volume.label, 1200.0F, y, 30.0F, kText);
const bool is_default = !state.simulation_mode && volume.id == state.default_volume_id;
ui::text(scene_, fonts_.regular,
- volume.available ? (volume.writable ? (is_default ? "Default · Available" : "Available") : "Read only")
+ volume.available ? (volume.writable ? (is_default ? "Default · Available" : "Available") : "No write access")
: "Disconnected",
1784.0F, y, 22.0F, volume.available && volume.writable ? kSecondary : kAmber,
gfx::Align::right);
@@ -1963,8 +1963,9 @@ class Render::Impl
1200.0F, y + 76.0F, 22.0F, kSecondary);
ui::text(scene_, fonts_.regular, "Used " + short_size(volume.used_bytes),
1200.0F, y + 106.0F, 22.0F, kSecondary);
- ui::text(scene_, fonts_.regular, "Reserved " + short_size(volume.reserved_bytes),
- 1784.0F, y + 76.0F, 22.0F, kSecondary, gfx::Align::right);
+ if (volume.reserved_bytes > 0)
+ ui::text(scene_, fonts_.regular, "Reserved " + short_size(volume.reserved_bytes),
+ 1784.0F, y + 76.0F, 22.0F, kSecondary, gfx::Align::right);
ui::text(scene_, fonts_.semibold, "Free " + short_size(free),
1784.0F, y + 106.0F, 22.0F, volume.available ? kText : kMuted,
gfx::Align::right);
@@ -3142,9 +3143,10 @@ class Render::Impl
card.x + 22.0F, card.y + 98.0F, 17.0F, kSecondary);
ui::text(overlay_, fonts_.regular, "Used " + short_size(volume.used_bytes),
card.x + 22.0F, card.y + 128.0F, 17.0F, kSecondary);
- ui::text(overlay_, fonts_.regular, "Reserved " + short_size(volume.reserved_bytes),
- card.x + card.w - 22.0F, card.y + 98.0F, 17.0F, kSecondary,
- gfx::Align::right);
+ if (volume.reserved_bytes > 0)
+ ui::text(overlay_, fonts_.regular, "Reserved " + short_size(volume.reserved_bytes),
+ card.x + card.w - 22.0F, card.y + 98.0F, 17.0F, kSecondary,
+ gfx::Align::right);
ui::text(overlay_, fonts_.semibold, "Free " + short_size(free),
card.x + card.w - 22.0F, card.y + 128.0F, 17.0F, kText,
gfx::Align::right);
diff --git a/src/steam/install_volumes.cpp b/src/steam/install_volumes.cpp
index 9d81b73..cb38bd7 100644
--- a/src/steam/install_volumes.cpp
+++ b/src/steam/install_volumes.cpp
@@ -73,6 +73,20 @@ bool MakeDirectories(const std::string& path) {
return IsDirectory(path) || mkdir(path.c_str(), 0777) == 0 || IsDirectory(path);
}
+// Like MakeDirectories, but only below `mount`, and each folder it creates is made 0777 whatever
+// the umask, so the app user can write there after root made it.
+bool MakeSharedDirectories(const std::string& mount, const std::string& path) {
+ if (path.compare(0, mount.size() + 1, mount + "/") != 0) return false;
+ for (std::size_t slash = path.find('/', mount.size() + 1);; slash = path.find('/', slash + 1)) {
+ const auto prefix = slash == std::string::npos ? path : path.substr(0, slash);
+ if (!IsDirectory(prefix)) {
+ if (mkdir(prefix.c_str(), 0777) != 0 && !IsDirectory(prefix)) return false;
+ if (chmod(prefix.c_str(), 0777) != 0) return false;
+ }
+ if (slash == std::string::npos) return true;
+ }
+}
+
bool CanWrite(const std::string& directory) {
const auto probe = directory + "/.playden-probe";
FILE* file = std::fopen(probe.c_str(), "w");
@@ -122,7 +136,7 @@ bool detail::VolumeMountMatches(const std::string& path, const std::string& moun
return root.empty() || mount == root;
}
-std::vector<InstallVolume> DiscoverInstallVolumes(const std::vector<VolumeCandidate>& candidates) {
+std::vector<InstallVolume> DiscoverInstallVolumes(const std::vector<VolumeCandidate>& candidates, const AsRoot& as_root) {
std::vector<InstallVolume> volumes;
for (std::size_t index = 0; index < candidates.size(); ++index) {
const auto& candidate = candidates[index];
@@ -132,7 +146,10 @@ std::vector<InstallVolume> DiscoverInstallVolumes(const std::vector<VolumeCandid
}
if (!IsDirectory(candidate.mount)) continue;
if (candidate.require_mount_point && !IsMountPoint(candidate.mount)) continue;
- const bool available = MakeDirectories(candidate.games_root) && CanWrite(candidate.games_root);
+ bool available = MakeDirectories(candidate.games_root) && CanWrite(candidate.games_root);
+ if (!available && as_root &&
+ as_root([&] { return MakeSharedDirectories(candidate.mount, candidate.games_root); }))
+ available = CanWrite(candidate.games_root);
volumes.push_back({candidate.id, candidate.label, candidate.games_root, available});
}
return volumes;
diff --git a/src/steam/install_volumes.h b/src/steam/install_volumes.h
index 32b99cb..631d890 100644
--- a/src/steam/install_volumes.h
+++ b/src/steam/install_volumes.h
@@ -3,6 +3,7 @@
#include "steam/install_service.h"
#include <cstdint>
+#include <functional>
#include <optional>
#include <string>
#include <vector>
@@ -21,10 +22,17 @@ struct VolumeCandidate {
bool require_mount_point = false;
};
+// Runs `work` with root rights and reports whether both succeeded.
+using AsRoot = std::function<bool(const std::function<bool()>& work)>;
+
// Candidates whose mount exists (and is mounted, when required), with `available` set when Playden could create games_root and
// write a probe file there. The first candidate is the primary volume and is always returned,
-// marked available (its folder is created on first install).
-std::vector<InstallVolume> DiscoverInstallVolumes(const std::vector<VolumeCandidate>& candidates);
+// marked available (its folder is created on first install). A console-formatted drive's root
+// belongs to root (issue #57): when the app user cannot create games_root, `as_root` creates
+// the missing folders, open to everyone (0777) like an exFAT drive's, so games are still
+// written by the app user.
+std::vector<InstallVolume> DiscoverInstallVolumes(const std::vector<VolumeCandidate>& candidates,
+ const AsRoot& as_root = {});
// Candidate drives for the PS5 title: console storage under the verified title
// root, then extended storage and USB drives. Use the stable root after a grant.
diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cpp
index be299d0..eff86dd 100644
--- a/tests/homebrew_library_model_test.cpp
+++ b/tests/homebrew_library_model_test.cpp
@@ -344,18 +344,21 @@ void live_install_volumes() {
false, {}, false, false, {}}), "library loaded");
model.UpdateVolumes({{"internal", "Console storage", true, std::nullopt, std::nullopt},
{"m2", "M.2 SSD", true, 2'000, 500},
- {"usb0", "USB drive", false, std::nullopt, std::nullopt}}, "internal");
+ {"usb0", "USB drive", false, std::nullopt, std::nullopt},
+ {"ext0", "Extended storage", true, 4'000, 3'000, false}}, "internal");
model.UpdateAppDetails(43, 400, 800, true);
model.UpdateInstalls({{43, JobState::Downloading, 100, 400, {}, "m2"}},
{{42, 600, "internal", true}, {44, 0, "usb0", false}});
const auto& volumes = model.state().volumes;
- check(volumes.size() == 3 && model.state().default_volume_id == "internal", "volumes and default projected");
+ check(volumes.size() == 4 && model.state().default_volume_id == "internal", "volumes and default projected");
check(!volumes[0].capacity_known && volumes[0].games_bytes == 600 && volumes[0].used_bytes == 600
&& volumes[0].reserved_bytes == 0, "a drive without a space report counts finished installs");
check(volumes[1].capacity_known && volumes[1].capacity_bytes == 2'000 && volumes[1].used_bytes == 1'500
&& volumes[1].reserved_bytes == 800, "a reported drive uses its own figures plus full install reservations");
check(!volumes[2].available, "an absent drive stays listed as unavailable");
+ check(volumes[3].available && !volumes[3].writable && volumes[3].capacity_known && volumes[3].capacity_bytes == 4'000,
+ "an attached drive Playden can't write to keeps its space report");
// Settings shows the default and changes it through the install service.
model.action("l1");
@@ -365,6 +368,9 @@ void live_install_volumes() {
const auto* usb = find_row(model, "volume-usb0");
check(has_row(model, "volume-internal") && has_row(model, "volume-m2") && usb && !usb->enabled,
"the picker lists every drive and disables an absent one");
+ const auto* locked = find_row(model, "volume-ext0");
+ check(locked && !locked->enabled && locked->subtitle == "No write access" && usb->subtitle == "Disconnected",
+ "an attached drive Playden can't write to is told apart from an absent one");
const auto* m2 = find_row(model, "volume-m2");
check(m2 && m2->value.find("free") != std::string::npos, "a reported drive shows its free space");
activate(model, "volume-m2");
diff --git a/tests/install_volumes_test.cpp b/tests/install_volumes_test.cpp
index f453526..ddfaf78 100644
--- a/tests/install_volumes_test.cpp
+++ b/tests/install_volumes_test.cpp
@@ -41,6 +41,35 @@ int main() {
CHECK(volumes[2].id == "locked");
if (::geteuid() != 0) CHECK(!volumes[2].available); // root ignores the permission bits
+ // A drive whose root only root may write (a console-formatted M.2, issue #57): the folders are
+ // made with root rights and left open to the app user, which then writes there itself.
+ int root_calls = 0;
+ const auto locked_mode = std::filesystem::perms::owner_read | std::filesystem::perms::owner_exec;
+ const AsRoot as_root = [&](const std::function<bool()>& work) {
+ ++root_calls;
+ std::filesystem::permissions(base / "locked", std::filesystem::perms::owner_all);
+ const bool ok = work();
+ std::filesystem::permissions(base / "locked", locked_mode);
+ return ok;
+ };
+ const auto rooted = DiscoverInstallVolumes(candidates, as_root);
+ CHECK(rooted.size() == 3 && rooted[1].available && rooted[2].id == "locked" && rooted[2].available);
+ CHECK(root_calls == (::geteuid() != 0 ? 1 : 0)); // only the drive the app user could not write
+ if (::geteuid() != 0) {
+ for (const auto& folder : {base / "locked" / "playden", base / "locked" / "playden" / "games"})
+ CHECK((std::filesystem::status(folder).permissions() & std::filesystem::perms::all) == std::filesystem::perms::all);
+ CHECK(!std::filesystem::exists(base / "locked" / "playden" / "games" / ".playden-probe"));
+ }
+ // Without root rights the drive stays offered but unavailable.
+ std::filesystem::create_directories(base / "locked2");
+ std::filesystem::permissions(base / "locked2", locked_mode);
+ const auto refused = DiscoverInstallVolumes(
+ {candidates[0], {"locked2", "Locked", (base / "locked2").string(), (base / "locked2" / "playden" / "games").string()}},
+ [](const std::function<bool()>&) { return false; });
+ CHECK(refused.size() == 2);
+ if (::geteuid() != 0) CHECK(!refused[1].available && !std::filesystem::exists(base / "locked2" / "playden"));
+ std::filesystem::permissions(base / "locked2", std::filesystem::perms::owner_all);
+
// A folder on the same filesystem as its parent is an empty mount point, not a drive:
// it is skipped and nothing is created inside it.
std::filesystem::create_directories(base / "unmounted");