mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
perf: SD card library files resolve through the fast path instead of proot (#1331)
perf: files in an SD card library resolve through the fast path instead of proot
A library bound at several guest names (droiddeck-sd, bannerlator-sd and
case variants) made every symlink and /proc/self/fd readlink under it fall
back to proot. The fast path now orders its binds the way proot does and
gives each host path the name proot would, and lstat("link/.") follows
the link as the kernel does.
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
1 parent
f61525162d
commit
006db14561
3 files changed
+199
-20
No files matched your search
@@ -337,34 +337,73 @@ static int to_guest(const char *h, char *g, size_t size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int guest_text(const char *h, char *g) {
|
||||
int in = h[0] == '/' && under(h, root, rootlen);
|
||||
for (int i = 0; h[0] == '/' && !in && i < nbinds; i++) {
|
||||
int live = held(&binds[i]);
|
||||
if (live < 0) return -1;
|
||||
in = live && under(h, binds[i].host, binds[i].hlen);
|
||||
static int host_order[MAX_BINDS + 1], norder, ordered;
|
||||
|
||||
static int contains(const char *a, const char *b) {
|
||||
size_t la = strlen(a);
|
||||
return strlen(b) > la && under(b, a, la);
|
||||
}
|
||||
|
||||
static const char *side_of(int i, int host) {
|
||||
return host ? (i < 0 ? root : binds[i].host) : (i < 0 ? "/" : binds[i].guest);
|
||||
}
|
||||
|
||||
static int insort(int *list, int m, int i, int host) {
|
||||
const char *p = side_of(i, host);
|
||||
int prev = -1;
|
||||
for (int j = 0; j < m; j++) {
|
||||
const char *q = side_of(list[j], host);
|
||||
if (!strcmp(p, q) && !host) {
|
||||
list[j] = i;
|
||||
return m;
|
||||
}
|
||||
if (!strcmp(p, q) || contains(p, q)) prev = j;
|
||||
}
|
||||
if (in) return to_guest(h, g, PATH_MAX);
|
||||
memcpy(g, h, strlen(h) + 1);
|
||||
memmove(list + prev + 2, list + prev + 1, (m - prev - 1) * sizeof *list);
|
||||
list[prev + 1] = i;
|
||||
return m + 1;
|
||||
}
|
||||
|
||||
static int order_binds(void) {
|
||||
if (__atomic_load_n(&ordered, __ATOMIC_ACQUIRE)) return 0;
|
||||
for (int i = 0; i < nbinds; i++)
|
||||
if (held(&binds[i]) < 0) return -1;
|
||||
if (pthread_mutex_trylock(&bind_lock) != 0) return -1;
|
||||
if (!ordered) {
|
||||
int pending[MAX_BINDS + 1], m = 1;
|
||||
pending[0] = -1;
|
||||
for (int i = 0; i < nbinds; i++)
|
||||
if (!binds[i].dropped) m = insort(pending, m, i, 0);
|
||||
norder = 0;
|
||||
for (int j = m - 1; j >= 0; j--) norder = insort(host_order, norder, pending[j], 1);
|
||||
__atomic_store_n(&ordered, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
pthread_mutex_unlock(&bind_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int host_bind(const char *h) {
|
||||
int found = -1;
|
||||
for (int i = 0; i < nbinds; i++) {
|
||||
int live = held(&binds[i]);
|
||||
if (live < 0) return -2;
|
||||
if (!live || !under(h, binds[i].host, binds[i].hlen)) continue;
|
||||
if (found < 0 || binds[i].hlen > binds[found].hlen) found = i;
|
||||
else if (binds[i].hlen == binds[found].hlen && strcmp(binds[i].guest, binds[found].guest) != 0) found = -3;
|
||||
}
|
||||
return found == -3 ? -2 : found;
|
||||
if (order_binds() < 0) return -2;
|
||||
for (int j = 0; j < norder; j++)
|
||||
if (host_order[j] >= 0 && under(h, binds[host_order[j]].host, binds[host_order[j]].hlen)) return host_order[j];
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int guest_text(const char *h, char *g) {
|
||||
if (h[0] == '/' && under(h, root, rootlen)) return to_guest(h, g, PATH_MAX);
|
||||
int i = h[0] == '/' ? host_bind(h) : -1;
|
||||
if (i == -2) return -1;
|
||||
if (i >= 0)
|
||||
return snprintf(g, PATH_MAX, "%s%s", binds[i].guest, h + binds[i].hlen) >= PATH_MAX || reserved(g) ? -1 : 0;
|
||||
memcpy(g, h, strlen(h) + 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long detranslated(char *text, const char *at) {
|
||||
if (text[0] != '/') return 0;
|
||||
if (!under(at, root, rootlen) && !under(text, root, rootlen)) {
|
||||
int own = host_bind(at), to = host_bind(text);
|
||||
if (to == -1) return 0;
|
||||
if (own == -2 || to == -2) return FP_SLOW;
|
||||
if (own >= 0 && to >= 0 && strcmp(binds[own].host, binds[to].host) == 0) {
|
||||
const struct bind *bd = &binds[to];
|
||||
@@ -635,8 +674,9 @@ static long resolve_in(const char *base, const char *path, int follow, struct re
|
||||
char norm[PATH_MAX];
|
||||
size_t o = 0;
|
||||
int dotdot = 0;
|
||||
size_t plen = strlen(path);
|
||||
int trailing = plen > 1 && path[plen - 1] == '/';
|
||||
size_t plen = strlen(path), end = plen;
|
||||
while (end > 1 && path[end - 1] == '/') end--;
|
||||
int trailing = (plen > 1 && path[plen - 1] == '/') || (end > 0 && path[end - 1] == '.' && (end == 1 || path[end - 2] == '/'));
|
||||
for (int part = base ? 0 : 1; part < 2; part++) {
|
||||
for (const char *c = part ? path : base; *c;) {
|
||||
while (*c == '/') c++;
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
FASTPATH = Path(__file__).resolve().parents[1] / "proot/fastpath/fastpath.c"
|
||||
|
||||
PROGRAM = r"""
|
||||
#include "FASTPATH"
|
||||
|
||||
static long host_sys(long nr, long a, long b, long c, long d, long e, long f) {
|
||||
long r = syscall(nr, a, b, c, d, e, f);
|
||||
return r < 0 ? -errno : r;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
static char hosts[MAX_BINDS][PATH_MAX], guests[MAX_BINDS][512];
|
||||
fp_sys = host_sys;
|
||||
snprintf(root, sizeof root, "%s", argv[1]);
|
||||
rootlen = strlen(root);
|
||||
int i = 2;
|
||||
for (; i + 1 < argc && strcmp(argv[i], "--"); i += 2) {
|
||||
struct bind *bd = &binds[nbinds];
|
||||
bd->host = hosts[nbinds];
|
||||
bd->guest = guests[nbinds];
|
||||
snprintf(bd->host, PATH_MAX, "%s", argv[i]);
|
||||
snprintf(bd->guest, sizeof guests[0], "%s", argv[i + 1]);
|
||||
bd->hlen = strlen(bd->host);
|
||||
bd->glen = strlen(bd->guest);
|
||||
nbinds++;
|
||||
}
|
||||
pthread_once(&init_once, fp_init);
|
||||
fp_on = 1;
|
||||
for (i++; i < argc; i++) {
|
||||
char text[PATH_MAX];
|
||||
if (!strcmp(argv[i], "name")) {
|
||||
if (guest_text(argv[++i], text) < 0) strcpy(text, "SLOW");
|
||||
puts(text);
|
||||
} else if (!strcmp(argv[i], "link")) {
|
||||
snprintf(text, sizeof text, "%s", argv[i + 2]);
|
||||
if (detranslated(text, argv[i + 1]) == FP_SLOW) strcpy(text, "SLOW");
|
||||
i += 2;
|
||||
puts(text);
|
||||
} else if (!strcmp(argv[i], "lstat")) {
|
||||
struct stat st;
|
||||
long r = fstatat(AT_FDCWD, argv[++i], &st, AT_SYMLINK_NOFOLLOW);
|
||||
puts(r < 0 ? "error" : S_ISDIR(st.st_mode) ? "dir" : S_ISLNK(st.st_mode) ? "link" : "other");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class FastPathBindNamesTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if shutil.which("gcc") is None:
|
||||
raise unittest.SkipTest("gcc is not available")
|
||||
cls.work = Path(tempfile.mkdtemp())
|
||||
source = cls.work / "names.c"
|
||||
source.write_text(PROGRAM.replace("FASTPATH", str(FASTPATH)))
|
||||
cls.program = cls.work / "names"
|
||||
subprocess.run(["gcc", "-O1", "-w", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
t = cls.work / "tree"
|
||||
for d in ["root/etc", "root/mnt", "root/opt", "root/d/real", "lib/common/game", "fonts", "a", "b"]:
|
||||
(t / d).mkdir(parents=True, exist_ok=True)
|
||||
for f in ["root/etc/x", "lib/f", "lib/common/game/f", "fonts/f", "a/f", "b/f"]:
|
||||
(t / f).touch()
|
||||
os.symlink(t / "lib/f", t / "lib/l2")
|
||||
os.symlink(t / "lib/common", t / "lib/l")
|
||||
os.symlink("real", t / "root/d/link")
|
||||
cls.tree = t
|
||||
cls.binds = [
|
||||
(t / "lib", "/mnt/droiddeck-sd"),
|
||||
(t / "lib", "/mnt/bannerlator-sd"),
|
||||
(t / "lib", "/mnt/DroidDeck-SD"),
|
||||
(t / "lib/common", "/opt/common"),
|
||||
(t / "fonts", "/usr/share/fonts/android"),
|
||||
(t / "a", "/opt/g"),
|
||||
(t / "b", "/opt/g"),
|
||||
(t / "fonts", "/mnt/x"),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.work, ignore_errors=True)
|
||||
|
||||
def ask(self, *queries):
|
||||
argv = [str(self.program), str(self.tree / "root")]
|
||||
for host, guest in self.binds:
|
||||
argv += [str(host), guest]
|
||||
argv.append("--")
|
||||
for q in queries:
|
||||
argv += [str(x) for x in q]
|
||||
out = subprocess.run(argv, capture_output=True, text=True, timeout=20, check=True).stdout
|
||||
return out.splitlines()
|
||||
|
||||
def test_a_host_file_takes_the_name_proot_gives_it(self):
|
||||
t = self.tree
|
||||
self.assertEqual(self.ask(
|
||||
("name", t / "lib/f"),
|
||||
("name", t / "lib/common/game/f"),
|
||||
("name", t / "fonts/f"),
|
||||
("name", t / "b/f"),
|
||||
("name", t / "a/f"),
|
||||
("name", t / "root/etc/x"),
|
||||
), [
|
||||
"/mnt/droiddeck-sd/f",
|
||||
"/opt/common/game/f",
|
||||
"/usr/share/fonts/android/f",
|
||||
"/opt/g/f",
|
||||
str(t / "a/f"),
|
||||
"/etc/x",
|
||||
])
|
||||
|
||||
def test_a_link_inside_a_library_reads_back_like_proot(self):
|
||||
t = self.tree
|
||||
self.assertEqual(self.ask(
|
||||
("link", t / "lib/l2", t / "lib/f"),
|
||||
("link", t / "lib/l", t / "lib/common"),
|
||||
), [
|
||||
"/mnt/droiddeck-sd/f",
|
||||
str(t / "lib/common"),
|
||||
])
|
||||
|
||||
def test_a_final_dot_follows_the_link(self):
|
||||
self.assertEqual(self.ask(
|
||||
("lstat", "/d/link"),
|
||||
("lstat", "/d/link/."),
|
||||
("lstat", "/d/link/./"),
|
||||
("lstat", "/d/link/"),
|
||||
), ["link", "dir", "dir", "dir"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user