mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(proot): revalidate cached directory paths before every reuse
This commit is contained in:
1 parent
b676448e0a
commit
dff9e6a640
2 files changed
+251
-17
No files matched your search
@@ -59,10 +59,10 @@ static int optional_syscall(long nr) {
|
||||
nr == SYS_openat2 || nr == SYS_renameat2;
|
||||
}
|
||||
static long sc(long nr, long a, long b, long c, long d, long e) {
|
||||
if (fp_refused) return -ENOSYS;
|
||||
if (fp_refused && nr != SYS_close) return -ENOSYS;
|
||||
long r = fp_sys(nr, a, b, c, d, e, 0);
|
||||
if (r == -ENOSYS && !optional_syscall(nr)) {
|
||||
__atomic_store_n(&fp_on, 0, __ATOMIC_RELAXED);
|
||||
if (fp_sys(nr, -1, -1, -1, -1, -1, 0) == -ENOSYS) __atomic_store_n(&fp_on, 0, __ATOMIC_RELAXED);
|
||||
fp_refused = 1;
|
||||
}
|
||||
return r;
|
||||
@@ -418,8 +418,9 @@ static int host_bind(const char *h) {
|
||||
static unsigned hash_of(const char *s);
|
||||
#define DIRS 64
|
||||
#define DIR_FD_FLOOR 1000
|
||||
struct dirslot { unsigned hash; int fd, refs; dev_t dev; ino_t ino; char path[PATH_MAX]; char seen[PATH_MAX]; };
|
||||
struct dirslot { unsigned hash, epoch; int fd, refs; dev_t dev; ino_t ino; long tag; char path[PATH_MAX]; char seen[PATH_MAX]; };
|
||||
static struct dirslot dirs[DIRS];
|
||||
static unsigned dir_epoch;
|
||||
struct near { long fd; int slot, own; const char *name; };
|
||||
|
||||
static int fuse_bind(const char *h) {
|
||||
@@ -451,6 +452,44 @@ static int fd_id(long fd, dev_t *dev, ino_t *ino) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long dir_tag(void) {
|
||||
static uint64_t state;
|
||||
uint64_t z = __atomic_add_fetch(&state, 0x9e3779b97f4a7c15ULL, __ATOMIC_RELAXED), seed = 0;
|
||||
const void *random = (const void *)getauxval(AT_RANDOM);
|
||||
if (random) memcpy(&seed, random, sizeof seed);
|
||||
z += seed;
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
|
||||
return (long)((z ^ (z >> 31)) & 0xffffffffffULL) + 1;
|
||||
}
|
||||
|
||||
static int dir_owned(const struct dirslot *e) {
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
return e->fd > 0 && sc(SYS_lseek, e->fd, 0, SEEK_CUR, 0, 0) == e->tag
|
||||
&& fd_id(e->fd, &dev, &ino) == 0 && dev == e->dev && ino == e->ino;
|
||||
}
|
||||
|
||||
static int dir_fresh(const struct dirslot *e, const char *dir) {
|
||||
struct stat st;
|
||||
if (sc(SYS_newfstatat, AT_FDCWD, (long)dir, (long)&st, 0, 0) != 0 || st.st_dev != e->dev || st.st_ino != e->ino) {
|
||||
__atomic_add_fetch(&dir_epoch, 1, __ATOMIC_RELEASE);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void dirs_drop(const char *host) {
|
||||
size_t len = strlen(host);
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) {
|
||||
__atomic_add_fetch(&dir_epoch, 1, __ATOMIC_RELEASE);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < DIRS; i++)
|
||||
if (dirs[i].path[0] && under(dirs[i].path, host, len)) dirs[i].path[0] = 0;
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
}
|
||||
|
||||
static void near_put(struct near *n) {
|
||||
if (n->own) sc(SYS_close, n->fd, 0, 0, 0, 0);
|
||||
else if (n->slot >= 0) __atomic_sub_fetch(&dirs[n->slot].refs, 1, __ATOMIC_RELEASE);
|
||||
@@ -463,12 +502,13 @@ static int dir_cached(const char *dir, struct near *n) {
|
||||
unsigned h = hash_of(dir);
|
||||
struct dirslot *e = &dirs[h % DIRS];
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) return 0;
|
||||
int hit = e->fd >= 0 && e->hash == h && e->path[0] && strcmp(e->path, dir) == 0;
|
||||
int hit = e->fd >= 0 && e->hash == h && e->path[0] && strcmp(e->path, dir) == 0
|
||||
&& e->epoch == __atomic_load_n(&dir_epoch, __ATOMIC_ACQUIRE);
|
||||
if (hit) __atomic_add_fetch(&e->refs, 1, __ATOMIC_ACQUIRE);
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
if (!hit) return 0;
|
||||
char now[PATH_MAX];
|
||||
if (fd_path(e->fd, now) == 0 && strcmp(now, e->seen) == 0) {
|
||||
if (fd_path(e->fd, now) == 0 && strcmp(now, e->seen) == 0 && dir_fresh(e, dir)) {
|
||||
n->fd = e->fd;
|
||||
n->slot = (int)(e - dirs);
|
||||
n->own = 0;
|
||||
@@ -492,15 +532,22 @@ static int dir_keep(const char *dir, long fd, struct near *n) {
|
||||
char seen[PATH_MAX];
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
if (fd_path(high, seen) < 0 || fd_id(high, &dev, &ino) < 0 || strlen(dir) >= PATH_MAX) return 0;
|
||||
long tag = dir_tag();
|
||||
struct stat st;
|
||||
if (fd_id(high, &dev, &ino) < 0 || strlen(dir) >= PATH_MAX
|
||||
|| sc(SYS_newfstatat, AT_FDCWD, (long)dir, (long)&st, 0, 0) != 0 || st.st_dev != dev || st.st_ino != ino) {
|
||||
near_put(n);
|
||||
return -1;
|
||||
}
|
||||
if (fd_path(high, seen) < 0 || sc(SYS_lseek, high, tag, SEEK_SET, 0, 0) != tag) return 0;
|
||||
unsigned h = hash_of(dir);
|
||||
struct dirslot *e = &dirs[h % DIRS];
|
||||
if (pthread_mutex_trylock(&dir_lock) != 0) return 0;
|
||||
if (__atomic_load_n(&e->refs, __ATOMIC_ACQUIRE) == 0) {
|
||||
dev_t odev;
|
||||
ino_t oino;
|
||||
if (e->fd > 0 && fd_id(e->fd, &odev, &oino) == 0 && odev == e->dev && oino == e->ino) sc(SYS_close, e->fd, 0, 0, 0, 0);
|
||||
if (dir_owned(e)) sc(SYS_close, e->fd, 0, 0, 0, 0);
|
||||
e->hash = h;
|
||||
e->epoch = __atomic_load_n(&dir_epoch, __ATOMIC_ACQUIRE);
|
||||
e->tag = tag;
|
||||
e->fd = (int)high;
|
||||
e->dev = dev;
|
||||
e->ino = ino;
|
||||
@@ -518,7 +565,7 @@ static int dir_get(char *dir, size_t floor, struct near *n) {
|
||||
size_t len = strlen(dir), at = len;
|
||||
while (!dir_cached(dir, n)) {
|
||||
if (at <= floor) {
|
||||
long fd = sc(SYS_openat, AT_FDCWD, (long)dir, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
long fd = sc(SYS_openat, AT_FDCWD, (long)dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
if (fd < 0 || dir_keep(dir, fd, n) < 0) return -1;
|
||||
break;
|
||||
}
|
||||
@@ -531,7 +578,7 @@ static int dir_get(char *dir, size_t floor, struct near *n) {
|
||||
dir[at] = '/';
|
||||
const char *name = dir + at + 1;
|
||||
size_t next = at + 1 + strlen(name);
|
||||
long fd = sc(SYS_openat, n->fd, (long)name, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC, 0, 0);
|
||||
long fd = sc(SYS_openat, n->fd, (long)name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC, 0, 0);
|
||||
near_put(n);
|
||||
if (fd < 0 || dir_keep(dir, fd, n) < 0) return -1;
|
||||
at = next;
|
||||
@@ -1396,7 +1443,10 @@ static long fp_chmod(int dirfd, const char *path, mode_t mode, int flags) {
|
||||
struct res r;
|
||||
long rs = flags ? FP_SLOW : resolve(dirfd, path, 1 | MAY_BE_UNSEEN, &r);
|
||||
if (rs == 1) rs = -ENOENT;
|
||||
if (rs == 0 || rs == UNSEEN) rs = hsc(SYS_fchmodat, r.host, mode, 0, 0);
|
||||
if (rs == 0 || rs == UNSEEN) {
|
||||
rs = hsc(SYS_fchmodat, r.host, mode, 0, 0);
|
||||
if (rs == 0) dirs_drop(r.host);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
int fchmodat(int dirfd, const char *path, mode_t mode, int flags) {
|
||||
@@ -1409,7 +1459,10 @@ static long fp_chown(int dirfd, const char *path, uid_t uid, gid_t gid, int flag
|
||||
struct res r;
|
||||
long rs = flags & ~AT_SYMLINK_NOFOLLOW ? FP_SLOW : resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r);
|
||||
if (rs == 1) rs = -ENOENT;
|
||||
if (rs == 0 || rs == UNSEEN) rs = hsc(SYS_fchownat, r.host, (long)uid, (long)gid, flags);
|
||||
if (rs == 0 || rs == UNSEEN) {
|
||||
rs = hsc(SYS_fchownat, r.host, (long)uid, (long)gid, flags);
|
||||
if (rs == 0) dirs_drop(r.host);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
int fchownat(int dirfd, const char *path, uid_t uid, gid_t gid, int flags) {
|
||||
|
||||
@@ -5,13 +5,29 @@ import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
USERNS = shutil.which("unshare") is not None and subprocess.run(["unshare", "-Urm", "true"], capture_output=True).returncode == 0
|
||||
|
||||
FASTPATH = Path(__file__).resolve().parents[1] / "proot/fastpath/fastpath.c"
|
||||
|
||||
PROGRAM = r"""
|
||||
#include "FASTPATH"
|
||||
#include <sys/mount.h>
|
||||
|
||||
static const char *race_from, *race_to;
|
||||
static dev_t race_dev;
|
||||
static ino_t race_ino;
|
||||
|
||||
static long trapped_nr = -1, unsupported_nr = -1;
|
||||
|
||||
static long host_sys(long nr, long a, long b, long c, long d, long e, long f) {
|
||||
if (nr == trapped_nr || (nr == unsupported_nr && a != -1)) return -ENOSYS;
|
||||
long r = syscall(nr, a, b, c, d, e, f);
|
||||
struct stat fs;
|
||||
if (r >= 0 && nr == SYS_fcntl && b == F_DUPFD_CLOEXEC && c == DIR_FD_FLOOR && race_from
|
||||
&& syscall(SYS_fstat, a, &fs) == 0 && fs.st_dev == race_dev && fs.st_ino == race_ino) {
|
||||
syscall(SYS_renameat, AT_FDCWD, race_from, AT_FDCWD, race_to);
|
||||
race_from = NULL;
|
||||
}
|
||||
return r < 0 ? -errno : r;
|
||||
}
|
||||
|
||||
@@ -30,6 +46,8 @@ static int high_fds(void) {
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
static char hosts[MAX_BINDS][PATH_MAX], guests[MAX_BINDS][512];
|
||||
static int taken[DIRS];
|
||||
int ntaken = 0;
|
||||
fp_sys = host_sys;
|
||||
snprintf(root, sizeof root, "%s", argv[1]);
|
||||
rootlen = strlen(root);
|
||||
@@ -57,8 +75,24 @@ int main(int argc, char **argv) {
|
||||
} else if (!strcmp(op, "stat")) {
|
||||
struct stat st;
|
||||
show(fp_stat(AT_FDCWD, argv[++i], AT_SYMLINK_NOFOLLOW, &st));
|
||||
} else if (!strcmp(op, "symlink")) {
|
||||
long r = fp_symlink("target", AT_FDCWD, argv[++i]);
|
||||
if (refused()) puts("REFUSED");
|
||||
else show(r);
|
||||
} else if (!strcmp(op, "trap-symlink")) {
|
||||
trapped_nr = SYS_symlinkat;
|
||||
} else if (!strcmp(op, "no-symlinks")) {
|
||||
unsupported_nr = SYS_symlinkat;
|
||||
} else if (!strcmp(op, "on")) {
|
||||
printf("%d\n", fp_ready());
|
||||
} else if (!strcmp(op, "no-folders")) {
|
||||
unsupported_nr = SYS_mkdirat;
|
||||
} else if (!strcmp(op, "busy-cache")) {
|
||||
pthread_mutex_lock(&dir_lock);
|
||||
} else if (!strcmp(op, "mkdir")) {
|
||||
show(fp_mkdir(AT_FDCWD, argv[++i], 0755));
|
||||
long r = fp_mkdir(AT_FDCWD, argv[++i], 0755);
|
||||
if (refused()) puts("REFUSED");
|
||||
else show(r);
|
||||
} else if (!strcmp(op, "mv")) {
|
||||
show(fp_rename(AT_FDCWD, argv[i + 1], AT_FDCWD, argv[i + 2], 0));
|
||||
i += 2;
|
||||
@@ -67,6 +101,14 @@ int main(int argc, char **argv) {
|
||||
} else if (!strcmp(op, "host-mv")) {
|
||||
show(host_sys(SYS_renameat, AT_FDCWD, (long)argv[i + 1], AT_FDCWD, (long)argv[i + 2], 0, 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "race-mv")) {
|
||||
struct stat rs;
|
||||
race_from = argv[i + 1];
|
||||
race_to = argv[i + 2];
|
||||
i += 2;
|
||||
show(host_sys(SYS_newfstatat, AT_FDCWD, (long)race_from, (long)&rs, 0, 0, 0));
|
||||
race_dev = rs.st_dev;
|
||||
race_ino = rs.st_ino;
|
||||
} else if (!strcmp(op, "host-rmdir")) {
|
||||
show(host_sys(SYS_unlinkat, AT_FDCWD, (long)argv[++i], AT_REMOVEDIR, 0, 0, 0));
|
||||
} else if (!strcmp(op, "host-mkdir")) {
|
||||
@@ -76,6 +118,20 @@ int main(int argc, char **argv) {
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + DIRS; fd++) host_sys(SYS_dup3, null, fd, 0, 0, 0, 0);
|
||||
host_sys(SYS_close, null, 0, 0, 0, 0, 0);
|
||||
puts("ok");
|
||||
} else if (!strcmp(op, "same-dir-takeover")) {
|
||||
const char *dir = argv[++i];
|
||||
struct near first, second;
|
||||
long fd = host_sys(SYS_openat, AT_FDCWD, (long)dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0, 0);
|
||||
if (fd < 0 || dir_keep(dir, fd, &first) < 0 || first.slot < 0) return 2;
|
||||
int target = (int)first.fd;
|
||||
near_put(&first);
|
||||
fd = host_sys(SYS_openat, AT_FDCWD, (long)dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0, 0);
|
||||
if (fd < 0 || host_sys(SYS_dup3, fd, target, O_CLOEXEC, 0, 0, 0) < 0) return 3;
|
||||
host_sys(SYS_close, fd, 0, 0, 0, 0, 0);
|
||||
fd = host_sys(SYS_openat, AT_FDCWD, (long)dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0, 0);
|
||||
if (fd < 0 || dir_keep(dir, fd, &second) < 0) return 4;
|
||||
puts(fcntl(target, F_GETFD) < 0 ? "closed" : "open");
|
||||
near_put(&second);
|
||||
} else if (!strcmp(op, "first-fd")) {
|
||||
char link[40], out[PATH_MAX];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%d", DIR_FD_FLOOR);
|
||||
@@ -84,6 +140,36 @@ int main(int argc, char **argv) {
|
||||
puts(out);
|
||||
} else if (!strcmp(op, "fds")) {
|
||||
printf("%d\n", high_fds());
|
||||
} else if (!strcmp(op, "ttl")) {
|
||||
ttl_ns = atoll(argv[++i]) * 1000000LL;
|
||||
} else if (!strcmp(op, "sleep")) {
|
||||
usleep(atoi(argv[++i]) * 1000);
|
||||
} else if (!strcmp(op, "chmod")) {
|
||||
show(fp_chmod(AT_FDCWD, argv[i + 1], strtol(argv[i + 2], NULL, 8), 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "host-chmod")) {
|
||||
show(host_sys(SYS_fchmodat, AT_FDCWD, (long)argv[i + 1], strtol(argv[i + 2], NULL, 8), 0, 0, 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "host-mount")) {
|
||||
show(mount("tmpfs", argv[++i], "tmpfs", 0, NULL) == 0 ? 0 : -errno);
|
||||
} else if (!strcmp(op, "take-over")) {
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + DIRS; fd++) {
|
||||
char link[40], out[PATH_MAX];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%d", fd);
|
||||
long n = host_sys(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0, 0);
|
||||
if (n <= 0) continue;
|
||||
out[n] = 0;
|
||||
long mine = host_sys(SYS_openat, AT_FDCWD, (long)out, O_RDONLY | O_DIRECTORY, 0, 0, 0);
|
||||
host_sys(SYS_dup3, mine, fd, 0, 0, 0, 0);
|
||||
host_sys(SYS_close, mine, 0, 0, 0, 0, 0);
|
||||
taken[ntaken++] = fd;
|
||||
}
|
||||
puts("ok");
|
||||
} else if (!strcmp(op, "taken")) {
|
||||
int open_fds = 0;
|
||||
for (int t = 0; t < ntaken; t++)
|
||||
if (lseek(taken[t], 0, SEEK_CUR) == 0) open_fds++;
|
||||
printf("%d/%d\n", open_fds, ntaken);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
@@ -100,7 +186,7 @@ class FastPathFuseDirsTest(unittest.TestCase):
|
||||
source = Path(cls.build.name) / "fuse_dirs.c"
|
||||
source.write_text(PROGRAM.replace("FASTPATH", str(FASTPATH)))
|
||||
cls.program = Path(cls.build.name) / "fuse_dirs"
|
||||
subprocess.run(["gcc", "-O1", "-w", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
subprocess.run(["gcc", "-O1", "-Wall", "-Wextra", "-Werror", "-Wno-format-truncation", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
@@ -115,8 +201,8 @@ class FastPathFuseDirsTest(unittest.TestCase):
|
||||
self.card = base / "card"
|
||||
(self.card / "Games/steamapps/common/Game/Data").mkdir(parents=True)
|
||||
|
||||
def ask(self, *ops):
|
||||
argv = [str(self.program), str(self.root), str(self.card), "/mnt/card", "--", *ops]
|
||||
def ask(self, *ops, wrap=()):
|
||||
argv = [*wrap, str(self.program), str(self.root), str(self.card), "/mnt/card", "--", *ops]
|
||||
return subprocess.run(argv, capture_output=True, text=True, timeout=20, check=True).stdout.split()
|
||||
|
||||
def test_files_deep_in_a_fuse_library_are_created_renamed_and_removed(self):
|
||||
@@ -137,6 +223,16 @@ class FastPathFuseDirsTest(unittest.TestCase):
|
||||
"stat", "/mnt/card/Games/steamapps/common/Moved/Data/keep")
|
||||
self.assertEqual(["ok", "ok", "E2", "ok"], out)
|
||||
|
||||
def test_a_folder_renamed_while_it_is_opened_is_not_used_under_its_old_name(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
out = self.ask("fuse", "race-mv", str(game / "Data"), str(game / "Moved"),
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new", "fds")
|
||||
self.assertEqual(["ok", "E2", "E2"], out[:3])
|
||||
self.assertEqual(["keep"], [p.name for p in (game / "Moved").iterdir()])
|
||||
self.assertLessEqual(int(out[-1]), 64)
|
||||
|
||||
def test_a_folder_deleted_and_made_again_is_looked_up_fresh(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", "/mnt/card/Games/steamapps/common/Game/Data/old",
|
||||
@@ -164,6 +260,91 @@ class FastPathFuseDirsTest(unittest.TestCase):
|
||||
self.assertEqual(["ok", "ok", "ok", "ok", "/dev/null"], out)
|
||||
self.assertEqual({"a", "b"}, {p.name for p in (self.card / "Games/steamapps/common/Game/Data").iterdir()})
|
||||
|
||||
def test_a_folder_the_program_opened_itself_stays_open_when_the_cache_moves_on(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
ops = ["fuse", "touch", "/mnt/card/Games/steamapps/common/Game/Data/a", "take-over"]
|
||||
for n in range(200):
|
||||
(data / f"d{n}").mkdir()
|
||||
ops += ["touch", f"/mnt/card/Games/steamapps/common/Game/Data/d{n}/x"]
|
||||
out = self.ask(*ops, "taken")
|
||||
self.assertEqual(["ok"] * 202, out[:-1])
|
||||
opened, total = map(int, out[-1].split("/"))
|
||||
self.assertGreater(total, 0)
|
||||
self.assertEqual(total, opened)
|
||||
self.assertEqual(200, len(list(data.glob("d*/x"))))
|
||||
|
||||
@unittest.skipIf(os.geteuid() == 0, "root passes folder permissions")
|
||||
def test_a_parent_folder_closed_by_the_program_stops_cached_access_at_once(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
self.addCleanup(os.chmod, game, 0o755)
|
||||
out = self.ask("fuse", "stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"chmod", "/mnt/card/Games/steamapps/common/Game", "000",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new")
|
||||
self.assertEqual(["ok", "ok"], out[:2])
|
||||
self.assertNotIn("ok", out[2:])
|
||||
os.chmod(game, 0o755)
|
||||
self.assertEqual(["keep"], [p.name for p in (game / "Data").iterdir()])
|
||||
|
||||
@unittest.skipIf(os.geteuid() == 0, "root passes folder permissions")
|
||||
def test_external_permission_change_stops_access_before_ttl_expires(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
self.addCleanup(os.chmod, game, 0o755)
|
||||
out = self.ask("fuse", "ttl", "60000", "stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"host-chmod", str(game), "000",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new",
|
||||
"host-chmod", str(game), "755",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new")
|
||||
self.assertEqual(["ok", "ok", "E13", "E13", "ok", "ok", "ok"], out)
|
||||
self.assertEqual({"keep", "new"}, {p.name for p in (game / "Data").iterdir()})
|
||||
|
||||
@unittest.skipUnless(USERNS, "mount namespaces are not available")
|
||||
def test_a_mount_placed_over_a_cached_folder_receives_later_writes(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "ttl", "60000", "touch", "/mnt/card/Games/steamapps/common/Game/Data/under",
|
||||
"host-mount", str(data),
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/over",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/under", wrap=("unshare", "-Urm"))
|
||||
self.assertEqual(["ok", "ok", "ok", "E2"], out)
|
||||
self.assertEqual(["under"], [p.name for p in data.iterdir()])
|
||||
|
||||
def test_a_call_the_filesystem_does_not_implement_leaves_the_fast_path_on(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "no-symlinks", "touch", f"{data}/a", "symlink", f"{data}/link", "on",
|
||||
"stat", f"{data}/a", "symlink", f"{data}/link", "on", "touch", f"{data}/b")
|
||||
self.assertEqual(["ok", "REFUSED", "1", "ok", "REFUSED", "1", "ok"], out)
|
||||
self.assertEqual(["a", "b"], sorted(p.name for p in (self.card / "Games/steamapps/common/Game/Data").iterdir()))
|
||||
|
||||
def test_repeated_unsupported_calls_close_the_folders_they_opened(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
ops = [op for n in range(40) for op in ("mkdir", f"{data}/new{n}")]
|
||||
out = self.ask("fuse", "no-folders", "busy-cache", *ops, "on", "fds")
|
||||
self.assertEqual(["REFUSED"] * 40 + ["1", "0"], out)
|
||||
|
||||
def test_a_trapped_call_still_stops_the_fast_path_for_the_process(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "trap-symlink", "symlink", f"{data}/link", "on")
|
||||
self.assertEqual(["REFUSED", "0"], out)
|
||||
|
||||
def test_a_symlink_is_made_where_the_filesystem_allows_it(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "symlink", f"{data}/link", "on")
|
||||
self.assertEqual(["ok", "1"], out)
|
||||
self.assertEqual("target", os.readlink(self.card / "Games/steamapps/common/Game/Data/link"))
|
||||
|
||||
def test_same_directory_takeover_survives_slot_reuse(self):
|
||||
self.assertEqual(["open"], self.ask("same-dir-takeover", str(self.card)))
|
||||
|
||||
def test_disabling_the_cache_keeps_absolute_paths_and_opens_no_folders(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "ttl", "0", "touch", f"{data}/a", "mv", f"{data}/a", f"{data}/b",
|
||||
"stat", f"{data}/b", "rm", f"{data}/b", "fds")
|
||||
self.assertEqual(["ok", "ok", "ok", "ok", "0"], out)
|
||||
|
||||
def test_other_filesystems_keep_absolute_paths_and_open_no_folders(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("touch", f"{data}/a", "mv", f"{data}/a", f"{data}/b", "rm", f"{data}/b", "fds")
|
||||
|
||||
Reference in new issue
Block a user