mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
fix(desktop): let desktop apps save read-only game files, and make game libraries writable (#1781)
This commit is contained in:
1 parent
5786e89ff4
commit
22d39998f0
8 files changed
+366
-4
No files matched your search
@@ -60,7 +60,7 @@ class BinfmtPreloadTest(unittest.TestCase):
|
||||
cls.library = cls.tmp / "libbinfmt.so"
|
||||
subprocess.run(["gcc", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror", "-DBINFMT_TEST",
|
||||
'-DBINFMT_HANDLER="%s"' % cls.handler, "-o", str(cls.library),
|
||||
"-pthread"] + [str(PRELOAD / name) for name in ("binfmt.c", "net.c", "udevmon.c", "pathcache.c", "webhelper.c")] + ["-ldl"],
|
||||
"-pthread"] + [str(PRELOAD / name) for name in ("binfmt.c", "net.c", "udevmon.c", "pathcache.c", "perms.c", "webhelper.c")] + ["-ldl"],
|
||||
check=True)
|
||||
source = cls.tmp / "program.c"
|
||||
source.write_text(PROGRAM)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-library-perms"
|
||||
|
||||
|
||||
class LibraryPermsTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.restore)
|
||||
self.root = Path(self.temp.name)
|
||||
self.home = self.root / "home"
|
||||
self.steam = self.home / ".local/share/Steam"
|
||||
self.card = self.root / "card"
|
||||
self.stores = self.root / "stores"
|
||||
(self.steam / "steamapps").mkdir(parents=True)
|
||||
(self.steam / "steamapps/libraryfolders.vdf").write_text(
|
||||
'"libraryfolders"\n{\n\t"0"\n\t{\n\t\t"path"\t\t"%s"\n\t}\n\t"1"\n\t{\n\t\t"path"\t\t"%s"\n\t}\n}\n'
|
||||
% (self.steam, self.card))
|
||||
self.locked = []
|
||||
|
||||
def restore(self):
|
||||
for path in self.locked:
|
||||
if path.exists() and not path.is_symlink():
|
||||
path.chmod(0o700 if path.is_dir() else 0o600)
|
||||
for path in sorted(self.root.rglob("*"), key=lambda p: len(p.parts), reverse=True):
|
||||
if path.is_dir() and not path.is_symlink():
|
||||
path.chmod(0o700)
|
||||
self.temp.cleanup()
|
||||
|
||||
def file(self, path, mode=0o400):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("x")
|
||||
path.chmod(mode)
|
||||
self.locked.append(path)
|
||||
return path
|
||||
|
||||
def folder(self, path, mode=0o500):
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
self.locked.append(path)
|
||||
return path
|
||||
|
||||
def run_script(self):
|
||||
env = dict(os.environ, HOME=str(self.home), DROIDDECK_STORES_ROOT=str(self.stores))
|
||||
for path in reversed(self.locked):
|
||||
if path.is_dir():
|
||||
path.chmod(0o500)
|
||||
return subprocess.run([sys.executable, str(SCRIPT)], env=env, check=True, capture_output=True, text=True).stdout
|
||||
|
||||
def mode(self, path):
|
||||
return stat.S_IMODE(os.lstat(path).st_mode)
|
||||
|
||||
def test_read_only_game_files_and_folders_become_writable_in_every_library(self):
|
||||
ini = self.file(self.steam / "steamapps/compatdata/3712566526/pfx/drive_c/users/steamuser/Documents/My Games/FalloutNV/FALLOUT.INI")
|
||||
data = self.folder(self.steam / "steamapps/common/Game/Data")
|
||||
game = self.file(data / "game.esm", 0o444)
|
||||
card = self.file(self.card / "steamapps/common/Other/settings.ini")
|
||||
gog = self.file(self.home / "Games/Stores/GOG/Fallout New Vegas/Fallout_default.ini")
|
||||
store = self.file(self.stores / "abc/Game/config.cfg")
|
||||
out = self.run_script()
|
||||
self.assertEqual(self.mode(ini) & 0o600, 0o600)
|
||||
self.assertEqual(self.mode(game), 0o644)
|
||||
self.assertEqual(self.mode(data) & 0o700, 0o700)
|
||||
for path in (card, gog, store):
|
||||
self.assertEqual(self.mode(path) & 0o600, 0o600, path)
|
||||
self.assertIn("made writable", out)
|
||||
|
||||
def test_proton_and_steam_runtimes_are_left_as_valve_ships_them(self):
|
||||
proton = self.file(self.steam / "steamapps/common/Proton Experimental (ARM64)/files/lib/wine/a.so", 0o444)
|
||||
runtime = self.file(self.card / "steamapps/common/SteamLinuxRuntime_sniper/run", 0o555)
|
||||
nested = self.file(self.steam / "steamapps/common/Game/Proton notes.txt")
|
||||
self.run_script()
|
||||
self.assertEqual(self.mode(proton), 0o444)
|
||||
self.assertEqual(self.mode(runtime), 0o555)
|
||||
self.assertEqual(self.mode(nested) & 0o600, 0o600)
|
||||
|
||||
def test_symlinks_are_not_followed(self):
|
||||
outside = self.file(self.root / "outside/keep.ini")
|
||||
link = self.steam / "steamapps/compatdata/1/pfx/dosdevices/z:"
|
||||
link.parent.mkdir(parents=True)
|
||||
link.symlink_to(self.root / "outside")
|
||||
self.run_script()
|
||||
self.assertEqual(self.mode(outside), 0o400)
|
||||
|
||||
def test_writable_files_are_untouched_and_missing_libraries_are_fine(self):
|
||||
fine = self.file(self.steam / "steamapps/common/Game/ok.ini", 0o640)
|
||||
out = self.run_script()
|
||||
self.assertEqual(self.mode(fine), 0o640)
|
||||
self.assertIn("0 files and folders made writable", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,109 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import stat
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
PROGRAM = r"""
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 1; i + 1 < argc; i += 2) {
|
||||
const char *op = argv[i], *path = argv[i + 1];
|
||||
int ok;
|
||||
if (strcmp(op, "access") == 0) ok = access(path, W_OK) == 0;
|
||||
else if (strcmp(op, "access-rw") == 0) ok = access(path, R_OK | W_OK) == 0;
|
||||
else if (strcmp(op, "faccessat") == 0) {
|
||||
int dir = open(".", O_RDONLY | O_DIRECTORY);
|
||||
ok = faccessat(dir, path, W_OK, 0) == 0;
|
||||
close(dir);
|
||||
} else if (strcmp(op, "write") == 0) {
|
||||
int fd = open(path, O_WRONLY | O_TRUNC);
|
||||
ok = fd >= 0 && write(fd, "saved", 5) == 5;
|
||||
if (fd >= 0) close(fd);
|
||||
} else if (strcmp(op, "create") == 0) {
|
||||
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
||||
ok = fd >= 0;
|
||||
if (fd >= 0) close(fd);
|
||||
} else ok = 0;
|
||||
printf("%s %d\n", op, ok);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("cc") and os.geteuid() != 0, "needs a C compiler and a non-root user")
|
||||
class WritablePreloadTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.dir = Path(tempfile.mkdtemp())
|
||||
build = cls.dir / "build"
|
||||
build.mkdir()
|
||||
(build / "program.c").write_text(PROGRAM)
|
||||
cls.preload = build / "libblsession.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.preload),
|
||||
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
|
||||
cls.editor = build / "kate"
|
||||
subprocess.run(["cc", "-O2", "-o", str(cls.editor), str(build / "program.c")], check=True)
|
||||
cls.wine = build / "wine64-preloader"
|
||||
shutil.copy(cls.editor, cls.wine)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.dir, True)
|
||||
|
||||
def setUp(self):
|
||||
self.work = Path(tempfile.mkdtemp(dir=self.dir))
|
||||
self.addCleanup(self.unlock)
|
||||
self.ini = self.work / "My Games/FalloutNV/FALLOUT.INI"
|
||||
self.ini.parent.mkdir(parents=True)
|
||||
self.ini.write_text("bUseThreadedAI=0\n")
|
||||
self.ini.chmod(0o400)
|
||||
self.locked = self.work / "Locked"
|
||||
self.locked.mkdir()
|
||||
self.locked.chmod(0o500)
|
||||
|
||||
def unlock(self):
|
||||
for path in (self.ini, self.locked):
|
||||
path.chmod(0o700)
|
||||
|
||||
def run_as(self, program, *args, preload=True):
|
||||
env = dict(os.environ)
|
||||
env.pop("LD_PRELOAD", None)
|
||||
if preload:
|
||||
env["LD_PRELOAD"] = str(self.preload)
|
||||
out = subprocess.run([str(program), *map(str, args)], env=env, cwd=self.ini.parent,
|
||||
capture_output=True, text=True, check=True, timeout=60).stdout
|
||||
return dict(line.split() for line in out.splitlines())
|
||||
|
||||
def test_without_the_preload_a_read_only_file_cannot_be_saved(self):
|
||||
out = self.run_as(self.editor, "access", self.ini, "write", self.ini, preload=False)
|
||||
self.assertEqual(out, {"access": "0", "write": "0"})
|
||||
|
||||
def test_a_desktop_app_can_save_a_read_only_file_like_root(self):
|
||||
out = self.run_as(self.editor, "access", self.ini, "access-rw", self.ini, "faccessat", self.ini.name,
|
||||
"write", self.ini, "create", self.locked / "new.tmp")
|
||||
self.assertEqual(out, {"access": "1", "access-rw": "1", "faccessat": "1", "write": "1", "create": "1"})
|
||||
self.assertEqual(self.ini.read_text(), "saved")
|
||||
|
||||
def test_checking_access_alone_leaves_the_mode_alone(self):
|
||||
self.run_as(self.editor, "access", self.ini)
|
||||
self.assertEqual(stat.S_IMODE(self.ini.stat().st_mode), 0o400)
|
||||
|
||||
def test_wine_keeps_windows_read_only_files_read_only(self):
|
||||
out = self.run_as(self.wine, "access", self.ini, "write", self.ini, "create", self.locked / "new.tmp")
|
||||
self.assertEqual(out, {"access": "0", "write": "0", "create": "0"})
|
||||
self.assertEqual(self.ini.read_text(), "bUseThreadedAI=0\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user