mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
fix: games on an SD card install faster and Battle.net can read the card's volume (#2379)
A Battle.net library on another volume is reached through its own drive letter, so the Agent's volume query succeeds. The proot fast path keeps folder handles for FUSE-backed libraries. Supersedes #2377.
This commit is contained in:
1 parent
1d247d212b
commit
3b6d13a8d7
6 files changed
+883
-36
No files matched your search
@@ -41,6 +41,9 @@ FEX_WAIT = 120
|
||||
START_GRACE = 90
|
||||
POLL = 2
|
||||
STOP_WAIT = 5
|
||||
DRIVES = "defghijklmnopqruvwxy"
|
||||
PRODUCT_DB = ("ProgramData", "Battle.net", "Agent", "product.db")
|
||||
INSTALL_FOLDER = (1, 3, 1)
|
||||
CLOSE_GRACE = 15
|
||||
HANDOFF_WAIT = 120
|
||||
GAME_SETTLE = 20
|
||||
@@ -123,7 +126,7 @@ function install() {
|
||||
save(".form", form);
|
||||
if (form.indexOf('"' + lang + '"') < 0) lang = "enUS";
|
||||
var locale = lang.toLowerCase();
|
||||
call("POST", "/install/" + uid, '{"account_country":"' + country + '","finalized":true,"game_dir":"' + dir + '","geo_ip_country":"' +
|
||||
call("POST", "/install/" + uid, '{"account_country":"' + country + '","finalized":true,"game_dir":"' + dir.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '","geo_ip_country":"' +
|
||||
region + '","language":["' + locale + '"],"selected_asset_locale":"' + locale + '","selected_locale":"' + locale +
|
||||
'","shortcut":"none","tome_torrent":""}');
|
||||
watch("/install/" + uid);
|
||||
@@ -226,8 +229,72 @@ def installed(root):
|
||||
return os.path.isfile(launcher(root))
|
||||
|
||||
|
||||
def install_path(library):
|
||||
return "Z:" + library.rstrip("/")
|
||||
def dosdevices(root):
|
||||
return os.path.join(prefix(root), "pfx", "dosdevices")
|
||||
|
||||
|
||||
def volume_root(path):
|
||||
device = os.stat(path).st_dev
|
||||
while path != "/":
|
||||
parent = os.path.dirname(path)
|
||||
if os.stat(parent).st_dev != device:
|
||||
break
|
||||
path = parent
|
||||
return path
|
||||
|
||||
|
||||
def inside(path, folder):
|
||||
return path == folder or path.startswith(folder.rstrip("/") + "/")
|
||||
|
||||
|
||||
def mapped_drive(root, path, volume):
|
||||
folder = dosdevices(root)
|
||||
try:
|
||||
names = sorted(os.listdir(folder))
|
||||
except OSError:
|
||||
return None
|
||||
for name in names:
|
||||
link = os.path.join(folder, name)
|
||||
if len(name) == 2 and name[0].lower() in DRIVES and name[1] == ":" and os.path.islink(link):
|
||||
target = os.path.realpath(link)
|
||||
if inside(target, volume) and inside(path, target):
|
||||
return name[0].lower(), target
|
||||
return None
|
||||
|
||||
|
||||
def map_drive(root, volume):
|
||||
folder = dosdevices(root)
|
||||
for letter in DRIVES:
|
||||
link = os.path.join(folder, letter + ":")
|
||||
if not os.path.lexists(link):
|
||||
try:
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
os.symlink(volume, link)
|
||||
except OSError as error:
|
||||
say("%s not mapped to a Wine drive: %s" % (volume, error))
|
||||
return None
|
||||
say("Wine drive %s: is %s" % (letter.upper(), volume))
|
||||
return letter, volume
|
||||
say("%s not mapped to a Wine drive: every letter is taken" % volume)
|
||||
return None
|
||||
|
||||
|
||||
def install_path(root, library):
|
||||
library = library.rstrip("/") or "/"
|
||||
try:
|
||||
path = os.path.realpath(library)
|
||||
volume = volume_root(path)
|
||||
except OSError:
|
||||
return "Z:" + library
|
||||
if volume == "/":
|
||||
return "Z:" + library
|
||||
drive = mapped_drive(root, path, volume)
|
||||
if drive is None and not wine_processes(os.path.join(prefix(root), "pfx")):
|
||||
drive = map_drive(root, volume)
|
||||
if drive is None:
|
||||
return "Z:" + library
|
||||
letter, target = drive
|
||||
return letter.upper() + ":\\" + path[len(target):].strip("/").replace("/", "\\")
|
||||
|
||||
|
||||
def write_config(root, library=None):
|
||||
@@ -254,8 +321,8 @@ def write_config(root, library=None):
|
||||
client["HardwareAcceleration"] = "false"
|
||||
changes.append("acceleration")
|
||||
if library and isinstance(client.setdefault("Install", {}), dict) \
|
||||
and str(client["Install"].get("DefaultInstallPath", "")).rstrip("/\\") != install_path(library):
|
||||
client["Install"]["DefaultInstallPath"] = install_path(library)
|
||||
and str(client["Install"].get("DefaultInstallPath", "")).rstrip("/\\") != install_path(root, library).rstrip("\\"):
|
||||
client["Install"]["DefaultInstallPath"] = install_path(root, library)
|
||||
changes.append("library")
|
||||
games = config.setdefault("Games", {})
|
||||
for uid in CLIENT_32_BIT if isinstance(games, dict) else ():
|
||||
@@ -417,13 +484,106 @@ def wallpaper(root):
|
||||
|
||||
|
||||
def windows_path(root, path):
|
||||
found = re.match(r"(?i)^([cz]):[\\/](.*)$", path.strip())
|
||||
found = re.match(r"(?i)^([a-z]):[\\/](.*)$", path.strip())
|
||||
if not found:
|
||||
return None
|
||||
parts = [part for part in re.split(r"[\\/]+", found.group(2)) if part]
|
||||
if not parts or ".." in parts:
|
||||
if ".." in parts:
|
||||
return None
|
||||
return os.path.join(drive_c(root) if found.group(1).lower() == "c" else "/", *parts)
|
||||
letter = found.group(1).lower()
|
||||
link = os.path.join(dosdevices(root), letter + ":")
|
||||
if letter == "c":
|
||||
target = drive_c(root)
|
||||
elif os.path.islink(link):
|
||||
target = os.path.realpath(link)
|
||||
elif letter == "z":
|
||||
target = "/"
|
||||
else:
|
||||
return None
|
||||
return os.path.join(target, *parts)
|
||||
|
||||
|
||||
def varint(data, at):
|
||||
value = shift = 0
|
||||
while at < len(data) and shift < 64:
|
||||
byte = data[at]
|
||||
at += 1
|
||||
value |= (byte & 0x7F) << shift
|
||||
if not byte & 0x80:
|
||||
return value, at
|
||||
shift += 7
|
||||
raise ValueError("varint")
|
||||
|
||||
|
||||
def encode_varint(value):
|
||||
out = bytearray()
|
||||
while value > 0x7F:
|
||||
out.append(value & 0x7F | 0x80)
|
||||
value >>= 7
|
||||
out.append(value)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def rewrite_field(data, path, change):
|
||||
out, at = bytearray(), 0
|
||||
while at < len(data):
|
||||
start = at
|
||||
tag, at = varint(data, at)
|
||||
kind = tag & 7
|
||||
if kind == 0:
|
||||
at = varint(data, at)[1]
|
||||
elif kind == 1:
|
||||
at += 8
|
||||
elif kind == 5:
|
||||
at += 4
|
||||
elif kind == 2:
|
||||
size, body = varint(data, at)
|
||||
at = body + size
|
||||
else:
|
||||
raise ValueError("wire type")
|
||||
if at > len(data):
|
||||
raise ValueError("length")
|
||||
if kind == 2 and tag >> 3 == path[0]:
|
||||
value = data[body:at]
|
||||
value = change(value) if len(path) == 1 else rewrite_field(value, path[1:], change)
|
||||
out += encode_varint(tag) + encode_varint(len(value)) + value
|
||||
else:
|
||||
out += data[start:at]
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def drive_folder(root, value):
|
||||
try:
|
||||
text = value.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return value
|
||||
if not re.match(r"(?i)^z:[\\/]", text):
|
||||
return value
|
||||
folder = windows_path(root, text)
|
||||
if not folder or not os.path.isdir(folder):
|
||||
return value
|
||||
moved = install_path(root, folder)
|
||||
return value if moved.startswith("Z:") else moved.replace("\\", "/").encode("utf-8")
|
||||
|
||||
|
||||
def move_installs(root):
|
||||
path = os.path.join(drive_c(root), *PRODUCT_DB)
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
data = f.read()
|
||||
moved = rewrite_field(data, INSTALL_FOLDER, lambda value: drive_folder(root, value))
|
||||
if moved == data or wine_processes(os.path.join(prefix(root), "pfx")):
|
||||
return False
|
||||
with open(path, "rb") as f:
|
||||
if f.read() != data:
|
||||
return False
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
with open(path + ".tmp", "wb") as f:
|
||||
f.write(moved)
|
||||
shutil.copymode(path, path + ".tmp")
|
||||
os.replace(path + ".tmp", path)
|
||||
return True
|
||||
|
||||
|
||||
def wow_folders(root):
|
||||
@@ -533,6 +693,11 @@ def configure(root, library=None):
|
||||
except OSError as error:
|
||||
say("Battle.net's config not written: %s" % error)
|
||||
return
|
||||
try:
|
||||
if move_installs(root):
|
||||
say("Battle.net's installed games on a mapped volume now use its Wine drive")
|
||||
except OSError as error:
|
||||
say("Battle.net's installed games not moved to their Wine drive: %s" % error)
|
||||
try:
|
||||
wallpaper(root)
|
||||
except OSError as error:
|
||||
@@ -1227,7 +1392,6 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
say("%s not made: %s" % (folder, error))
|
||||
emit("ERROR", "folder")
|
||||
return 2
|
||||
windows = "Z:" + folder
|
||||
told = None
|
||||
while True:
|
||||
processes = wine_processes(pfx)
|
||||
@@ -1244,6 +1408,8 @@ def agent_job(root, action, uid, product="", folder="", locale="enUS", country="
|
||||
time.sleep(POLL if poll is None else poll)
|
||||
with lock:
|
||||
stop_leftovers(pfx)
|
||||
if action == "install":
|
||||
windows = install_path(root, folder)
|
||||
emit("STATE", "starting")
|
||||
base = os.path.join(CACHE, "agent-%s" % uid)
|
||||
for suffix in (".json", ".game", ".form", ".error"):
|
||||
|
||||
+241
-23
@@ -41,7 +41,7 @@ typedef long (*stub_fn)(long nr, long a, long b, long c, long d, long e, long f)
|
||||
static stub_fn fp_sys;
|
||||
static int fp_on;
|
||||
|
||||
struct bind { char *host; size_t hlen; char *guest; size_t glen; int canon; int dropped; };
|
||||
struct bind { char *host; size_t hlen; char *guest; size_t glen; int canon; int dropped; int fuse; };
|
||||
static struct bind binds[MAX_BINDS];
|
||||
static int nbinds;
|
||||
static char root[PATH_MAX];
|
||||
@@ -133,7 +133,8 @@ static long tracer;
|
||||
static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t bind_lock;
|
||||
static pthread_mutex_t long_lock;
|
||||
static void after_fork(void) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&bind_lock, NULL); pthread_mutex_init(&long_lock, NULL); }
|
||||
static pthread_mutex_t dir_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static void after_fork(void) { pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&bind_lock, NULL); pthread_mutex_init(&long_lock, NULL); pthread_mutex_init(&dir_lock, NULL); }
|
||||
|
||||
static int traced_by_match(const char *key) {
|
||||
static char buf[32768];
|
||||
@@ -414,6 +415,212 @@ static int host_bind(const char *h) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static unsigned hash_of(const char *s);
|
||||
static long long now_ns(void);
|
||||
#define DIRS 64
|
||||
#define DIR_FD_FLOOR 1000
|
||||
struct dirslot { unsigned hash, epoch; int fd, refs; dev_t dev; ino_t ino; long tag; long long stamp; 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) {
|
||||
int i = host_bind(h);
|
||||
if (i < 0) return -1;
|
||||
int f = __atomic_load_n(&binds[i].fuse, __ATOMIC_ACQUIRE);
|
||||
if (!f) {
|
||||
struct statfs fs;
|
||||
f = sc(SYS_statfs, (long)binds[i].host, (long)&fs, 0, 0, 0) == 0 && fs.f_type == 0x65735546 ? 1 : 2;
|
||||
__atomic_store_n(&binds[i].fuse, f, __ATOMIC_RELEASE);
|
||||
}
|
||||
return f == 1 ? i : -1;
|
||||
}
|
||||
|
||||
static long fd_path(long fd, char *out) {
|
||||
char link[40];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%ld", fd);
|
||||
long n = sc(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, PATH_MAX - 1, 0);
|
||||
if (n <= 0 || n >= PATH_MAX - 1) return -1;
|
||||
out[n] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fd_id(long fd, dev_t *dev, ino_t *ino) {
|
||||
struct stat st;
|
||||
if (sc(SYS_newfstatat, fd, (long)"", (long)&st, AT_EMPTY_PATH, 0) != 0) return -1;
|
||||
*dev = st.st_dev;
|
||||
*ino = st.st_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(struct dirslot *e, const char *dir) {
|
||||
long long t = now_ns();
|
||||
if (t - __atomic_load_n(&e->stamp, __ATOMIC_RELAXED) < ttl_ns) return 1;
|
||||
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;
|
||||
}
|
||||
__atomic_store_n(&e->stamp, t, __ATOMIC_RELAXED);
|
||||
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);
|
||||
n->fd = AT_FDCWD;
|
||||
n->slot = -1;
|
||||
n->own = 0;
|
||||
}
|
||||
|
||||
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
|
||||
&& 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 && dir_fresh(e, dir)) {
|
||||
n->fd = e->fd;
|
||||
n->slot = (int)(e - dirs);
|
||||
n->own = 0;
|
||||
return 1;
|
||||
}
|
||||
__atomic_sub_fetch(&e->refs, 1, __ATOMIC_RELEASE);
|
||||
if (pthread_mutex_trylock(&dir_lock) == 0) {
|
||||
if (e->hash == h && strcmp(e->path, dir) == 0) e->path[0] = 0;
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dir_keep(const char *dir, long fd, struct near *n) {
|
||||
long high = sc(SYS_fcntl, fd, F_DUPFD_CLOEXEC, DIR_FD_FLOOR, 0, 0);
|
||||
sc(SYS_close, fd, 0, 0, 0, 0);
|
||||
if (high < 0) return -1;
|
||||
n->fd = high;
|
||||
n->slot = -1;
|
||||
n->own = 1;
|
||||
char seen[PATH_MAX];
|
||||
dev_t dev;
|
||||
ino_t ino;
|
||||
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) {
|
||||
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->stamp = now_ns();
|
||||
e->fd = (int)high;
|
||||
e->dev = dev;
|
||||
e->ino = ino;
|
||||
e->refs = 1;
|
||||
strcpy(e->path, dir);
|
||||
strcpy(e->seen, seen);
|
||||
n->slot = (int)(e - dirs);
|
||||
n->own = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&dir_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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_RDONLY | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
if (fd < 0 || dir_keep(dir, fd, n) < 0) return -1;
|
||||
break;
|
||||
}
|
||||
char *slash = strrchr(dir, '/');
|
||||
if (!slash || (size_t)(slash - dir) < floor) return -1;
|
||||
*slash = 0;
|
||||
at = slash - dir;
|
||||
}
|
||||
while (at < len) {
|
||||
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_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;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static long near_get(const char *host, struct near *n) {
|
||||
n->fd = AT_FDCWD;
|
||||
n->slot = -1;
|
||||
n->own = 0;
|
||||
n->name = host;
|
||||
if (ttl_ns <= 0 || host[0] != '/') return n->fd;
|
||||
int i = fuse_bind(host);
|
||||
if (i < 0) return n->fd;
|
||||
const char *slash = strrchr(host, '/');
|
||||
size_t plen = slash - host;
|
||||
if (plen < binds[i].hlen || !slash[1] || plen >= PATH_MAX) return n->fd;
|
||||
char dir[PATH_MAX];
|
||||
memcpy(dir, host, plen);
|
||||
dir[plen] = 0;
|
||||
if (dir_get(dir, binds[i].hlen, n) < 0) {
|
||||
near_put(n);
|
||||
return n->fd;
|
||||
}
|
||||
n->name = slash + 1;
|
||||
return n->fd;
|
||||
}
|
||||
|
||||
static long hsc(long nr, const char *host, long a, long b, long c) {
|
||||
struct near n;
|
||||
long fd = near_get(host, &n);
|
||||
long rs = sc(nr, fd, (long)n.name, a, b, c);
|
||||
near_put(&n);
|
||||
return rs;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -556,7 +763,7 @@ static long proc_readlink(const struct res *r, char *text, size_t size, int *cha
|
||||
return name[0] == 'e' ? own_exe(text) : own_cwd(text);
|
||||
}
|
||||
char h[PATH_MAX];
|
||||
long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)h, sizeof h - 1, 0);
|
||||
long t = hsc(SYS_readlinkat, r->host, (long)h, sizeof h - 1, 0);
|
||||
if (t < 0) return t;
|
||||
if (t >= (long)sizeof h - 1) return FP_SLOW;
|
||||
if (size && (size_t)t > size) t = size;
|
||||
@@ -579,7 +786,7 @@ static long proc_link(struct res *r, char *text, int final) {
|
||||
if (!under(r->host, "/proc", 5)) return FP_SLOW;
|
||||
r->moving = 1;
|
||||
if (!strcmp(r->guest, "/proc/self") || !strcmp(r->guest, "/proc/thread-self")) {
|
||||
long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)text, PATH_MAX - 1, 0);
|
||||
long t = hsc(SYS_readlinkat, r->host, (long)text, PATH_MAX - 1, 0);
|
||||
if (t <= 0 || t >= PATH_MAX - 1) return FP_SLOW;
|
||||
text[t] = 0;
|
||||
return t;
|
||||
@@ -628,7 +835,7 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
if (reserved(r->guest)) return FP_SLOW;
|
||||
struct bind *through;
|
||||
if (to_host(r->guest, r->host, &through) < 0) return FP_SLOW;
|
||||
long s = sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0);
|
||||
long s = hsc(SYS_newfstatat, r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0);
|
||||
if (s < 0) {
|
||||
int glue = glue_above_binding(r->guest, glen + n);
|
||||
if (glue < 0) return FP_SLOW;
|
||||
@@ -648,13 +855,13 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
t = proc_link(r, target, final && last);
|
||||
if (t == FP_SLOW) return FP_SLOW;
|
||||
if (t == 0) {
|
||||
if (sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, 0, 0) < 0) return FP_SLOW;
|
||||
if (hsc(SYS_newfstatat, r->host, (long)&r->st, 0, 0) < 0) return FP_SLOW;
|
||||
r->magic = 1;
|
||||
r->exists = 1;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)target, sizeof target - 1, 0);
|
||||
t = hsc(SYS_readlinkat, r->host, (long)target, sizeof target - 1, 0);
|
||||
if (t <= 0 || t >= (long)sizeof target - 1) return FP_SLOW;
|
||||
target[t] = 0;
|
||||
if (detranslated(target, r->host) == FP_SLOW) return FP_SLOW;
|
||||
@@ -676,7 +883,7 @@ static long walk(struct res *r, const char *text, int follow, int last) {
|
||||
}
|
||||
if (!stated) {
|
||||
if (to_host(r->guest, r->host, NULL) < 0) return FP_SLOW;
|
||||
long s = sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0);
|
||||
long s = hsc(SYS_newfstatat, r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0);
|
||||
if (s < 0 && strcmp(r->guest, "/") != 0 && glue_above_binding(r->guest, strlen(r->guest))) return FP_SLOW;
|
||||
if (s < 0) return s == -ENOENT ? FP_SLOW : s;
|
||||
}
|
||||
@@ -699,7 +906,7 @@ static long bound_literal(const char *literal, struct res *r) {
|
||||
struct bind *bd;
|
||||
if (reserved(literal) || bind_of(literal, &bd) < 0 || !bd || bd->glen != strlen(literal) || bd->glen < 2) return FP_SLOW;
|
||||
if (snprintf(r->host, PATH_MAX, "%s", bd->host) >= PATH_MAX) return FP_SLOW;
|
||||
if (sc(SYS_newfstatat, AT_FDCWD, (long)r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0) < 0 || S_ISLNK(r->st.st_mode)) return FP_SLOW;
|
||||
if (hsc(SYS_newfstatat, r->host, (long)&r->st, AT_SYMLINK_NOFOLLOW, 0) < 0 || S_ISLNK(r->st.st_mode)) return FP_SLOW;
|
||||
snprintf(r->guest, PATH_MAX, "%s", literal);
|
||||
r->exists = 1;
|
||||
r->moving = 0;
|
||||
@@ -822,8 +1029,8 @@ static long fp_open(int dirfd, const char *path, int flags, mode_t mode) {
|
||||
long rs = resolve(dirfd, path, follow | MAY_BE_UNSEEN, &r);
|
||||
if (rs < 0) return rs;
|
||||
if (rs == 1 && !(flags & O_CREAT)) return -ENOENT;
|
||||
if (rs == UNSEEN) return sc(SYS_openat, AT_FDCWD, (long)r.host, flags, (flags & O_CREAT) ? mode : 0, 0);
|
||||
return sc(SYS_openat, AT_FDCWD, (long)r.host, flags | (follow && !r.magic ? O_NOFOLLOW : 0), (flags & O_CREAT) ? mode : 0, 0);
|
||||
if (rs == UNSEEN) return hsc(SYS_openat, r.host, flags, (flags & O_CREAT) ? mode : 0, 0);
|
||||
return hsc(SYS_openat, r.host, flags | (follow && !r.magic ? O_NOFOLLOW : 0), (flags & O_CREAT) ? mode : 0, 0);
|
||||
}
|
||||
|
||||
static int do_openat(int dirfd, const char *path, int flags, mode_t mode, int (*real)(int, const char *, int, ...)) {
|
||||
@@ -864,7 +1071,7 @@ static long fp_stat(int dirfd, const char *path, int flags, struct stat *st) {
|
||||
long rs = resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r);
|
||||
if (rs < 0) return rs;
|
||||
if (rs == 1) return -ENOENT;
|
||||
if (rs == UNSEEN) return sc(SYS_newfstatat, AT_FDCWD, (long)r.host, (long)st, flags, 0);
|
||||
if (rs == UNSEEN) return hsc(SYS_newfstatat, r.host, (long)st, flags, 0);
|
||||
*st = r.st;
|
||||
return 0;
|
||||
}
|
||||
@@ -1007,7 +1214,7 @@ static long fp_access(int dirfd, const char *path, int mode, int flags) {
|
||||
? resolve(dirfd, path, (flags & AT_SYMLINK_NOFOLLOW ? 0 : 1) | MAY_BE_UNSEEN, &r) : FP_SLOW;
|
||||
if (rs == 1) rs = -ENOENT;
|
||||
if (rs == 0 && S_ISLNK(r.st.st_mode)) rs = FP_SLOW;
|
||||
if (rs == 0 || rs == UNSEEN) rs = sc(SYS_faccessat, AT_FDCWD, (long)r.host, mode, 0, 0);
|
||||
if (rs == 0 || rs == UNSEEN) rs = hsc(SYS_faccessat, r.host, mode, 0, 0);
|
||||
return rs;
|
||||
}
|
||||
|
||||
@@ -1030,7 +1237,7 @@ static long link_text(const struct res *r, char *text, size_t size, int *changed
|
||||
*changed = 0;
|
||||
if (in_proc != under(r->host, "/proc", 5)) return FP_SLOW;
|
||||
if (in_proc) return proc_readlink(r, text, size, changed);
|
||||
long t = sc(SYS_readlinkat, AT_FDCWD, (long)r->host, (long)text, PATH_MAX - 1, 0);
|
||||
long t = hsc(SYS_readlinkat, r->host, (long)text, PATH_MAX - 1, 0);
|
||||
if (t < 0) return t;
|
||||
if (t >= PATH_MAX - 1) return FP_SLOW;
|
||||
if (size && (size_t)t > size) t = size;
|
||||
@@ -1135,7 +1342,7 @@ char *__getcwd_chk(char *buf, size_t size, size_t room) {
|
||||
static long fp_mkdir(int dirfd, const char *path, mode_t mode) {
|
||||
struct res r;
|
||||
long rs = resolve(dirfd, path, MAY_BE_UNSEEN, &r);
|
||||
if (rs == 0 || rs == 1 || rs == UNSEEN) rs = sc(SYS_mkdirat, AT_FDCWD, (long)r.host, mode, 0, 0);
|
||||
if (rs == 0 || rs == 1 || rs == UNSEEN) rs = hsc(SYS_mkdirat, r.host, mode, 0, 0);
|
||||
return rs;
|
||||
}
|
||||
int mkdirat(int dirfd, const char *path, mode_t mode) {
|
||||
@@ -1172,7 +1379,7 @@ static long fp_unlink(int dirfd, const char *path, int flags) {
|
||||
if (rs == 0 && (flags & AT_REMOVEDIR) && strcmp(r.guest, "/") == 0) rs = FP_SLOW;
|
||||
if (rs == 0 || rs == UNSEEN) {
|
||||
int changes_paths = (flags & AT_REMOVEDIR) || rs == UNSEEN || S_ISLNK(r.st.st_mode);
|
||||
rs = sc(SYS_unlinkat, AT_FDCWD, (long)r.host, flags, 0, 0);
|
||||
rs = hsc(SYS_unlinkat, r.host, flags, 0, 0);
|
||||
if (rs == 0 && changes_paths) forget();
|
||||
}
|
||||
return rs;
|
||||
@@ -1196,9 +1403,14 @@ static long fp_rename(int olddirfd, const char *old, int newdirfd, const char *n
|
||||
if (rt < 0) rs = rt;
|
||||
else if (rt == 0 && S_ISDIR(to.st.st_mode)) rs = FP_SLOW;
|
||||
else {
|
||||
rs = flags ? sc(SYS_renameat2, AT_FDCWD, (long)from.host, AT_FDCWD, (long)to.host, flags)
|
||||
: sc(SYS_renameat, AT_FDCWD, (long)from.host, AT_FDCWD, (long)to.host, 0);
|
||||
if (rs == 0) forget();
|
||||
struct near a, b;
|
||||
near_get(from.host, &a);
|
||||
near_get(to.host, &b);
|
||||
rs = flags ? sc(SYS_renameat2, a.fd, (long)a.name, b.fd, (long)b.name, flags)
|
||||
: sc(SYS_renameat, a.fd, (long)a.name, b.fd, (long)b.name, 0);
|
||||
near_put(&a);
|
||||
near_put(&b);
|
||||
if (rs == 0 && (S_ISLNK(from.st.st_mode) || (rt == 0 && S_ISLNK(to.st.st_mode)))) forget();
|
||||
}
|
||||
}
|
||||
return rs;
|
||||
@@ -1236,7 +1448,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 = sc(SYS_fchmodat, AT_FDCWD, (long)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) {
|
||||
@@ -1249,7 +1464,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 = sc(SYS_fchownat, AT_FDCWD, (long)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) {
|
||||
@@ -1267,7 +1485,7 @@ static long fp_utimens(int dirfd, const char *path, const struct timespec times[
|
||||
} else {
|
||||
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 = sc(SYS_utimensat, AT_FDCWD, (long)r.host, (long)times, flags, 0);
|
||||
if (rs == 0 || rs == UNSEEN) rs = hsc(SYS_utimensat, r.host, (long)times, flags, 0);
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
@@ -1509,7 +1727,7 @@ static long socket_address(const struct sockaddr *addr, socklen_t len, int follo
|
||||
char *slash = strrchr(r.host, '/');
|
||||
if (!slash || slash == r.host) return FP_SLOW;
|
||||
*slash = 0;
|
||||
*dir = sc(SYS_openat, AT_FDCWD, (long)r.host, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
*dir = hsc(SYS_openat, r.host, O_PATH | O_DIRECTORY | O_CLOEXEC, 0, 0);
|
||||
if (*dir < 0) return FP_SLOW;
|
||||
hlen = snprintf(out->sun_path, room, "/proc/self/fd/%ld/%s", *dir, slash + 1);
|
||||
if (hlen >= room) { sc(SYS_close, *dir, 0, 0, 0, 0); *dir = -1; return FP_SLOW; }
|
||||
|
||||
@@ -198,6 +198,155 @@ class ConfigAndMenuTest(Temp):
|
||||
self.assertEqual(path.stat().st_mtime_ns, mtime)
|
||||
|
||||
|
||||
class DriveTest(Temp):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.root = str(self.tmp / "Steam")
|
||||
self.devices = Path(BNET["dosdevices"](self.root))
|
||||
self.card = self.tmp / "card"
|
||||
self.library = self.card / "Games/Battle.net"
|
||||
self.library.mkdir(parents=True)
|
||||
self.running = []
|
||||
patcher = mock.patch.dict(BNET, {"say": lambda message: None, "wine_processes": lambda pfx, proc="/proc": list(self.running),
|
||||
"volume_root": lambda path: str(self.card) if BNET["inside"](path, str(self.card)) else "/"})
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_a_volume_ends_where_the_device_changes(self):
|
||||
devices = {"/": 1, "/mnt": 1, "/mnt/card": 7, "/mnt/card/Games": 7, "/mnt/card/Games/Battle.net": 7, "/root": 1, "/root/Games": 1}
|
||||
with mock.patch.object(os, "stat", lambda path: mock.Mock(st_dev=devices[path])):
|
||||
volume_root = runpy.run_path(str(BIN / "droiddeck-battlenet"))["volume_root"]
|
||||
self.assertEqual(volume_root("/mnt/card/Games/Battle.net"), "/mnt/card")
|
||||
self.assertEqual(volume_root("/mnt/card"), "/mnt/card")
|
||||
self.assertEqual(volume_root("/root/Games"), "/")
|
||||
self.assertEqual(volume_root("/"), "/")
|
||||
|
||||
def test_a_library_on_the_root_volume_keeps_its_path_and_gets_no_drive(self):
|
||||
library = self.tmp / "root/Games/Battle.net"
|
||||
library.mkdir(parents=True)
|
||||
self.assertEqual(BNET["install_path"](self.root, str(library) + "/"), "Z:" + str(library))
|
||||
self.assertFalse(self.devices.exists())
|
||||
|
||||
def test_a_library_on_another_volume_gets_a_drive_at_the_volume_root(self):
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), str(self.card))
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library / "World of Warcraft")), "D:\\Games\\Battle.net\\World of Warcraft")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.card)), "D:\\")
|
||||
self.assertEqual(sorted(os.listdir(self.devices)), ["d:"])
|
||||
|
||||
def test_drives_in_use_and_the_ones_proton_owns_are_left_alone(self):
|
||||
self.devices.mkdir(parents=True)
|
||||
os.symlink("/somewhere/else", self.devices / "d:")
|
||||
os.symlink(str(self.card), self.devices / "s:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "E:\\Games\\Battle.net")
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), "/somewhere/else")
|
||||
for letter in BNET["DRIVES"]:
|
||||
if not os.path.lexists(self.devices / (letter + ":")):
|
||||
os.symlink("/somewhere/else", self.devices / (letter + ":"))
|
||||
os.unlink(self.devices / "e:")
|
||||
os.symlink("/somewhere/else", self.devices / "e:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "Z:" + str(self.library))
|
||||
self.assertNotIn("s", BNET["DRIVES"])
|
||||
self.assertNotIn("t", BNET["DRIVES"])
|
||||
|
||||
def test_a_running_prefix_gets_no_new_drive_but_uses_the_ones_it_started_with(self):
|
||||
self.running = [(5, ["C:\\Program Files (x86)\\Battle.net\\Battle.net.exe"])]
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "Z:" + str(self.library))
|
||||
self.assertFalse(self.devices.exists())
|
||||
self.running = []
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
self.running = [(5, ["C:\\Program Files (x86)\\Battle.net\\Battle.net.exe"])]
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "D:\\Games\\Battle.net")
|
||||
|
||||
def test_a_drive_mapped_inside_the_volume_is_reused(self):
|
||||
self.devices.mkdir(parents=True)
|
||||
os.symlink(str(self.card / "Games"), self.devices / "g:")
|
||||
self.assertEqual(BNET["install_path"](self.root, str(self.library)), "G:\\Battle.net")
|
||||
self.assertEqual(sorted(os.listdir(self.devices)), ["g:"])
|
||||
|
||||
def test_the_config_stores_the_drive_path_once(self):
|
||||
path = self.tmp / ("Steam/steamapps/compatdata/%s/pfx/drive_c/users/steamuser/AppData/Roaming/Battle.net/Battle.net.config"
|
||||
% BNET["APP_ID"])
|
||||
BNET["write_config"](self.root)
|
||||
self.assertEqual(BNET["write_config"](self.root, str(self.library)), "library")
|
||||
self.assertEqual(json.loads(path.read_text())["Client"]["Install"]["DefaultInstallPath"], "D:\\Games\\Battle.net")
|
||||
self.assertIsNone(BNET["write_config"](self.root, str(self.library)))
|
||||
|
||||
def test_registered_paths_resolve_through_their_drive(self):
|
||||
BNET["install_path"](self.root, str(self.library))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "D:/Games/Battle.net/World of Warcraft"),
|
||||
str(self.library / "World of Warcraft"))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "Z:\\mnt\\card\\World of Warcraft"), "/mnt/card/World of Warcraft")
|
||||
self.assertEqual(BNET["windows_path"](self.root, "C:/Program Files (x86)/World of Warcraft"),
|
||||
os.path.join(BNET["drive_c"](self.root), "Program Files (x86)/World of Warcraft"))
|
||||
self.assertIsNone(BNET["windows_path"](self.root, "E:/Games/X"))
|
||||
self.assertIsNone(BNET["windows_path"](self.root, "D:/../etc"))
|
||||
self.assertEqual(BNET["windows_path"](self.root, "D:\\"), str(self.library.parent.parent))
|
||||
|
||||
def field(self, number, value):
|
||||
varint = BNET["encode_varint"]
|
||||
return varint(number << 3 | 2) + varint(len(value)) + value
|
||||
|
||||
def product(self, uid, folder, filler=b""):
|
||||
settings = self.field(1, folder.encode()) + self.field(2, b"us") + b"\x18\x02" + filler
|
||||
return self.field(1, self.field(1, uid.encode()) + self.field(2, uid.encode()) + self.field(3, settings)
|
||||
+ b"\x21" + bytes(8) + b"\x2d" + bytes(4))
|
||||
|
||||
def database(self, *products):
|
||||
path = Path(BNET["drive_c"](self.root)) / "ProgramData/Battle.net/Agent/product.db"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(b"".join(products) + self.field(7, b"tail"))
|
||||
path.chmod(0o600)
|
||||
return path
|
||||
|
||||
def test_games_registered_through_the_root_drive_move_to_their_volume_drive(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
other = self.tmp / "root/Games/Other"
|
||||
other.mkdir(parents=True)
|
||||
client = self.product("battle.net", "C:/Program Files (x86)/Battle.net")
|
||||
local = self.product("other", "Z:" + str(other))
|
||||
gone = self.product("gone", "Z:" + str(self.card / "Missing"))
|
||||
path = self.database(client, self.product("wow", "Z:" + str(game), bytes(200)), local, gone)
|
||||
self.assertTrue(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), client + self.product("wow", "D:/Games/Battle.net/World of Warcraft", bytes(200))
|
||||
+ local + gone + self.field(7, b"tail"))
|
||||
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(os.readlink(self.devices / "d:"), str(self.card))
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
|
||||
def test_registrations_stay_while_the_prefix_runs_or_the_database_is_unreadable(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
path = self.database(self.product("wow", "Z:" + str(game)))
|
||||
before = path.read_bytes()
|
||||
self.running = [(5, ["C:\\ProgramData\\Battle.net\\Agent\\Agent.exe"])]
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), before)
|
||||
self.running = []
|
||||
for broken in (before[:-3], b"\x0a\xff", b"\x0b\x01"):
|
||||
path.write_bytes(broken)
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), broken)
|
||||
os.unlink(path)
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
|
||||
def test_a_database_written_while_it_was_being_read_is_left_as_written(self):
|
||||
game = self.library / "World of Warcraft"
|
||||
game.mkdir()
|
||||
path = self.database(self.product("wow", "Z:" + str(game)))
|
||||
newer = path.read_bytes() + self.field(7, b"newer")
|
||||
|
||||
def processes(pfx, proc="/proc"):
|
||||
path.write_bytes(newer)
|
||||
return []
|
||||
|
||||
with mock.patch.dict(BNET, {"wine_processes": processes}):
|
||||
self.assertFalse(BNET["move_installs"](self.root))
|
||||
self.assertEqual(path.read_bytes(), newer)
|
||||
self.assertTrue(BNET["move_installs"](self.root))
|
||||
|
||||
|
||||
class GamepadTest(Temp):
|
||||
def registry(self, *entries):
|
||||
rows = ["WINE REGISTRY Version 2"]
|
||||
@@ -772,7 +921,7 @@ class Harness(Temp):
|
||||
"stop": lambda processes, wait=0: self.calls.append(("stop", sorted(pid for pid, _ in processes))),
|
||||
"start": lambda exe, verb=None: self.calls.append(("start", os.path.basename(exe), verb)) or self.Proton(),
|
||||
"spawn": lambda program, verb, args=(): self.calls.append(("spawn", os.path.basename(program), verb, list(args))),
|
||||
"acquire": lambda folder=None: self.lock,
|
||||
"acquire": lambda folder=None: self.lock, "volume_root": lambda path: "/",
|
||||
"POLL": 0, "READY_SETTLE": 0, "GAME_GRACE": 0, "CLIENT_WAIT": 0,
|
||||
})
|
||||
patcher.start()
|
||||
@@ -1170,6 +1319,7 @@ class AgentTest(Temp):
|
||||
script = BNET["AGENT_SCRIPT"]
|
||||
self.assertIn("port = a(0), action = a(1), uid = a(2), product = a(3), dir = a(4)", script)
|
||||
self.assertIn("lang = a(5), country = a(6), region = a(7), out = a(8)", script)
|
||||
self.assertIn(r'''"game_dir":"' + dir.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '","geo''', script)
|
||||
self.assertIn('"--internalclienttools"', (BIN / "droiddeck-battlenet").read_text())
|
||||
|
||||
def test_the_driver_asks_blizzards_chinese_repository_for_chinese_installs(self):
|
||||
@@ -1230,6 +1380,40 @@ class AgentJobTest(Harness):
|
||||
self.assertEqual([kind for kind, _ in self.lines], ["STATE", "PROGRESS", "DONE"])
|
||||
self.assertEqual(json.loads(self.lines[-1][1]), {"install_dir": "Z:" + folder, "exe": "WarcraftRumble.exe", "version": "16.1.2.119725"})
|
||||
|
||||
def test_an_install_on_another_volume_is_handed_over_by_its_drive_once_the_prefix_is_stopped(self):
|
||||
self.install()
|
||||
card = self.tmp / "card"
|
||||
folder = str(card / "Games/Battle.net/Warcraft Rumble")
|
||||
self.files = {".json": json.dumps({"progress": 1.0, "download_complete": True, "state": 1004}),
|
||||
".game": json.dumps({"installed": True, "install_dir": "D:/Games/Battle.net/Warcraft Rumble", "local_version": "1",
|
||||
"binaries": {"game": {"relative_path": "WarcraftRumble.exe"}}})}
|
||||
self.states = [[(7, self.AGENT)]]
|
||||
events = []
|
||||
real_map = BNET["map_drive"]
|
||||
|
||||
def acquire(folder=None):
|
||||
events.append("lock")
|
||||
return self.lock
|
||||
|
||||
def stop(pfx):
|
||||
events.append(("stop", not self.lock.closed))
|
||||
self.states = [[]]
|
||||
return True
|
||||
|
||||
def map_drive(root, volume):
|
||||
events.append(("map", not self.lock.closed))
|
||||
return real_map(root, volume)
|
||||
|
||||
with mock.patch.dict(BNET, {"volume_root": lambda path: str(card), "stop_leftovers": stop, "map_drive": map_drive,
|
||||
"acquire": acquire}):
|
||||
code = BNET["agent_job"](self.root, "install", "gryphon", "gryphon", folder, "enUS", "USA", "US", poll=0)
|
||||
self.assertEqual(code, 0)
|
||||
self.assertEqual(events, ["lock", ("stop", True), ("map", True)])
|
||||
self.assertTrue(self.lock.closed)
|
||||
cmd = self.started[0]
|
||||
self.assertEqual(cmd[cmd.index("--") + 6], "D:\\Games\\Battle.net\\Warcraft Rumble")
|
||||
self.assertEqual(os.readlink(os.path.join(BNET["dosdevices"](self.root), "d:")), str(card))
|
||||
|
||||
def test_an_agent_error_is_reported_with_its_code(self):
|
||||
self.install()
|
||||
self.files = {".json": json.dumps({"error": 2421, "progress": 0.0})}
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
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 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;
|
||||
}
|
||||
|
||||
static void show(long r) {
|
||||
if (r == FP_SLOW) puts("SLOW");
|
||||
else if (r < 0) printf("E%ld\n", -r);
|
||||
else puts("ok");
|
||||
}
|
||||
|
||||
static int high_fds(void) {
|
||||
int n = 0;
|
||||
for (int fd = DIR_FD_FLOOR; fd < DIR_FD_FLOOR + 4 * DIRS; fd++)
|
||||
if (fcntl(fd, F_GETFD) >= 0) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
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);
|
||||
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++) {
|
||||
const char *op = argv[i];
|
||||
if (!strcmp(op, "fuse")) {
|
||||
for (int b = 0; b < nbinds; b++) binds[b].fuse = 1;
|
||||
} else if (!strcmp(op, "touch")) {
|
||||
long fd = fp_open(AT_FDCWD, argv[++i], O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
|
||||
if (fd >= 0) close((int)fd);
|
||||
show(fd);
|
||||
} else if (!strcmp(op, "stat")) {
|
||||
struct stat st;
|
||||
show(fp_stat(AT_FDCWD, argv[++i], AT_SYMLINK_NOFOLLOW, &st));
|
||||
} else if (!strcmp(op, "mkdir")) {
|
||||
show(fp_mkdir(AT_FDCWD, argv[++i], 0755));
|
||||
} else if (!strcmp(op, "mv")) {
|
||||
show(fp_rename(AT_FDCWD, argv[i + 1], AT_FDCWD, argv[i + 2], 0));
|
||||
i += 2;
|
||||
} else if (!strcmp(op, "rm")) {
|
||||
show(fp_unlink(AT_FDCWD, argv[++i], 0));
|
||||
} 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, "host-rmdir")) {
|
||||
show(host_sys(SYS_unlinkat, AT_FDCWD, (long)argv[++i], AT_REMOVEDIR, 0, 0, 0));
|
||||
} else if (!strcmp(op, "host-mkdir")) {
|
||||
show(host_sys(SYS_mkdirat, AT_FDCWD, (long)argv[++i], 0755, 0, 0, 0));
|
||||
} else if (!strcmp(op, "clobber")) {
|
||||
long null = host_sys(SYS_openat, AT_FDCWD, (long)"/dev/null", O_RDONLY, 0, 0, 0);
|
||||
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, "first-fd")) {
|
||||
char link[40], out[PATH_MAX];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%d", DIR_FD_FLOOR);
|
||||
long n = host_sys(SYS_readlinkat, AT_FDCWD, (long)link, (long)out, sizeof out - 1, 0, 0);
|
||||
out[n > 0 ? n : 0] = 0;
|
||||
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-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;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class FastPathFuseDirsTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if shutil.which("gcc") is None:
|
||||
raise unittest.SkipTest("gcc is not available")
|
||||
cls.build = tempfile.TemporaryDirectory()
|
||||
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", "-Wall", "-Wextra", "-Werror", "-Wno-format-truncation", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.build.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
base = Path(self.temp.name)
|
||||
self.root = base / "root"
|
||||
(self.root / "mnt").mkdir(parents=True)
|
||||
self.card = base / "card"
|
||||
(self.card / "Games/steamapps/common/Game/Data").mkdir(parents=True)
|
||||
|
||||
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):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", f"{data}/a.tmp", "mv", f"{data}/a.tmp", f"{data}/a.idx",
|
||||
"stat", f"{data}/a.idx", "stat", f"{data}/a.tmp", "rm", f"{data}/a.idx",
|
||||
"stat", f"{data}/a.idx", "fds")
|
||||
self.assertEqual(["ok", "ok", "ok", "E2", "ok", "E2"], out[:-1])
|
||||
self.assertGreater(int(out[-1]), 0)
|
||||
self.assertEqual([], list((self.card / "Games/steamapps/common/Game/Data").iterdir()))
|
||||
|
||||
def test_a_folder_renamed_behind_the_cache_is_not_reused(self):
|
||||
game = self.card / "Games/steamapps/common/Game"
|
||||
(game / "Data/keep").write_text("x")
|
||||
out = self.ask("fuse", "stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"host-mv", str(game), str(game.parent / "Moved"),
|
||||
"stat", "/mnt/card/Games/steamapps/common/Game/Data/keep",
|
||||
"stat", "/mnt/card/Games/steamapps/common/Moved/Data/keep")
|
||||
self.assertEqual(["ok", "ok", "E2", "ok"], out)
|
||||
|
||||
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",
|
||||
"rm", "/mnt/card/Games/steamapps/common/Game/Data/old",
|
||||
"host-rmdir", str(data), "host-mkdir", str(data),
|
||||
"touch", "/mnt/card/Games/steamapps/common/Game/Data/new")
|
||||
self.assertEqual(["ok"] * 5, out)
|
||||
self.assertEqual(["new"], [p.name for p in data.iterdir()])
|
||||
|
||||
def test_folders_deleted_and_made_again_many_times_keep_descriptors_bounded(self):
|
||||
data = self.card / "Games/steamapps/common/Game/Data"
|
||||
ops = ["fuse"]
|
||||
for _ in range(200):
|
||||
ops += ["touch", "/mnt/card/Games/steamapps/common/Game/Data/x",
|
||||
"rm", "/mnt/card/Games/steamapps/common/Game/Data/x",
|
||||
"host-rmdir", str(data), "host-mkdir", str(data)]
|
||||
out = self.ask(*ops, "fds")
|
||||
self.assertEqual(["ok"] * 800, out[:-1])
|
||||
self.assertLessEqual(int(out[-1]), 64)
|
||||
|
||||
def test_descriptors_the_program_takes_over_are_left_alone(self):
|
||||
data = "/mnt/card/Games/steamapps/common/Game/Data"
|
||||
out = self.ask("fuse", "touch", f"{data}/a", "clobber", "touch", f"{data}/b",
|
||||
"stat", f"{data}/a", "first-fd")
|
||||
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.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", "1", "touch", "/mnt/card/Games/steamapps/common/Game/Data/under",
|
||||
"host-mount", str(data), "sleep", "20",
|
||||
"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_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")
|
||||
self.assertEqual(["ok", "ok", "ok", "0"], out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user