mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 04:46:08 +00:00
419 lines
17 KiB
C
419 lines
17 KiB
C
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
|
// parts share their static functions and are built with the same command as before).
|
|
//
|
|
// Path translation: Ubuntu paths (/usr, /etc, ...) mapped into the rootfs, Android's own mounts
|
|
// left alone, and the library directories of the rootfs.
|
|
|
|
// ---- Path translation -------------------------------------------------------------------
|
|
// Pure string functions (no syscalls), so tests/native can check them directly. They run on
|
|
// every file-system call, so they avoid snprintf and compute the rootfs prefix length once.
|
|
|
|
static char g_ddnatv_root[PATH_MAX] = {0};
|
|
static size_t g_ddnatv_root_len = 0;
|
|
|
|
static void ddnatv_set_root(const char *root, size_t len) {
|
|
if (!root) len = 0;
|
|
if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1;
|
|
if (len > 0) memcpy(g_ddnatv_root, root, len);
|
|
g_ddnatv_root[len] = '\0';
|
|
g_ddnatv_root_len = len;
|
|
}
|
|
|
|
// Mounts that proot used to provide: "src:dst" pairs from DDNATV_BINDS ('|' separated), longest dst first.
|
|
// A path under dst is read from src; src equal to dst keeps a host path that would otherwise be
|
|
// looked up in the rootfs.
|
|
#define DDNATV_MAX_BINDS 128
|
|
struct ddnatv_bind {
|
|
char *src, *dst;
|
|
size_t src_len, dst_len;
|
|
int identity;
|
|
};
|
|
static struct ddnatv_bind g_binds[DDNATV_MAX_BINDS];
|
|
static int g_nbinds = 0;
|
|
static char *g_binds_raw = NULL;
|
|
|
|
static int ddnatv_bind_longer_first(const void *a, const void *b) {
|
|
const struct ddnatv_bind *x = a, *y = b;
|
|
return (y->dst_len > x->dst_len) - (y->dst_len < x->dst_len);
|
|
}
|
|
|
|
static void ddnatv_load_binds(const char *spec) {
|
|
if (!spec || !spec[0]) return;
|
|
char *raw = strdup(spec);
|
|
char *copy = strdup(spec);
|
|
if (!raw || !copy) { free(raw); free(copy); return; }
|
|
char *save = NULL;
|
|
for (char *entry = strtok_r(copy, "|", &save); entry && g_nbinds < DDNATV_MAX_BINDS; entry = strtok_r(NULL, "|", &save)) {
|
|
char *colon = strchr(entry, ':');
|
|
const char *src = entry;
|
|
const char *dst = colon ? colon + 1 : entry;
|
|
if (colon) *colon = ' |