From 7da472bbb18db154bd949184bb3fe3b0994f13b6 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Sun, 8 Nov 2026 09:19:14 -0500 Subject: [PATCH] Keep the profile working when it is linked elsewhere or account.uid is lost, and check save write sizes safely. - A profile, save or account folder that is a junction or symbolic link (a profile moved to another drive and linked back) stopped the start at "cannot create directory"; it is now taken as the folder. A save folder linked to another drive than save-tx cannot take commits (a folder rename cannot cross drives); the log now says so instead of blaming a file in use. - A missing account.uid made a new account, and the save under the old account's folder was no longer found (a profile copied without the file, or only its save folder put in place). When save/ holds exactly one account folder, its account is taken. A file that is there but cannot be read now stops the start instead of being replaced. - WriteFile's check of offset plus size against the file size could wrap for a size near 2^64. - The first save mount deletes the last session's working copy with the retries the start uses, so a scanner holding a file there no longer stops the game. Co-Authored-By: Claude Opus 5.5 --- runtime/fs_host.c | 21 +++++++++++++++++---- runtime/ov_system.c | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 28c1011d..680324dd 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -105,9 +105,14 @@ int fs_save_busy(void) { } // ---- host directories +// A junction or symbolic link to a folder counts as the folder: a player who moved the profile to another drive links +// it back, and the start stopped at "cannot create directory" for it. static int make_dir(const char *p) { int e = plat_fs_mkdir(p); - return !e || (e == PLAT_FS_EXISTS && plat_fs_kind(p) == PLAT_FS_DIR); + if (!e) return 1; + if (e != PLAT_FS_EXISTS) return 0; + int k = plat_fs_kind(p); + return k == PLAT_FS_DIR || (k == PLAT_FS_LINK && plat_dir_exists(p)); } // p and every missing parent. @@ -227,7 +232,13 @@ static int move_dir(const char *from, const char *to, int tries) { for (int i = 0; i < tries; i++) { int e = plat_fs_rename(from, to, 1); if (!e) return 0; - if (e != PLAT_FS_BUSY) return -1; + if (e != PLAT_FS_BUSY) { + // a folder rename cannot cross drives: save/ linked to another drive than save-tx/ (link the whole + // profile folder instead) fails every commit here + rt_log("fs: cannot move %s to %s (error %d)%s", from, to, plat_fs_os_error(), + plat_fs_os_error() == 17 ? ": they are on different drives; keep save and save-tx on one drive" : ""); + return -1; + } plat_sleep_ms(50); } return -1; @@ -490,7 +501,8 @@ static void save_recover(void) { static void work_create(void) { int n = 0, linked = 0; uint64_t bytes = 0; - if (remove_tree(work_root) || copy_tree(newest_save(), work_root, NULL, 0, &n, &linked, &bytes)) + // remove_tree_retry: the last unmount's delete may have been held up by a scanner, as at the start + if (remove_tree_retry(work_root) || copy_tree(newest_save(), work_root, NULL, 0, &n, &linked, &bytes)) rt_fatal("fs: cannot make the save working copy"); rt_log("host save working copy: %d files, %.1f MiB", n, bytes / 1048576.0); } @@ -953,7 +965,8 @@ static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint6 if (!(mode & FS_OPEN_WRITE)) return FS_R_OPEN_MODE; PlatFileId id; if (!plat_file_id(f, &id)) rt_fatal("fs: host save size"); - if ((uint64_t)offset + size > id.size && !(mode & FS_OPEN_APPEND)) return FS_R_APPEND_REQUIRED; + // offset + size without the sum: a size near 2^64 would wrap it below the file's size + if ((size > id.size || (uint64_t)offset > id.size - size) && !(mode & FS_OPEN_APPEND)) return FS_R_APPEND_REQUIRED; if (file->save) save_touched(); for (uint64_t done = 0; done < size;) { uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done); diff --git a/runtime/ov_system.c b/runtime/ov_system.c index 028358cd..31608994 100644 --- a/runtime/ov_system.c +++ b/runtime/ov_system.c @@ -500,6 +500,34 @@ static void make_dirs(const char *dir) { if (!plat_dir_exists(dir)) rt_fatal("account: cannot create %s", dir); } +// Holding account_lock. 1 when /save holds exactly one folder named as fs_host.c names an account's +// (, 32 hex digits); local is then that account. +static int local_adopted; +typedef struct { int n; uint64_t hi, lo; } AdoptScan; +static int adopt_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + AdoptScan *a = ctx; + if (kind != PLAT_FS_DIR && kind != PLAT_FS_LINK) return 1; + if (strlen(name) != 32 || strspn(name, "0123456789abcdefABCDEF") != 32) return 1; + char half[17]; + memcpy(half, name, 16), half[16] = 0; + a->hi = strtoull(half, NULL, 16); + a->lo = strtoull(name + 16, NULL, 16); + a->n++; + return 1; +} +static int adopt_save_uid(const char *profile) { + char save[1100]; + AdoptScan a = {0}; + if (snprintf(save, sizeof save, "%s/save", profile) >= (int)sizeof save || !plat_dir_exists(save)) return 0; + plat_fs_list(save, adopt_cb, &a); + if (a.n != 1 || (!a.hi && !a.lo)) return 0; + local.lo = a.lo; + local.hi = a.hi; + local_adopted = 1; + return 1; +} + // Holding account_lock. static void load_uid(void) { if (local.lo || local.hi) return; @@ -508,11 +536,17 @@ static void load_uid(void) { make_dirs(dir); need(snprintf(path, sizeof path, "%s/account.uid", dir) < (int)sizeof path, "account: path too long"); int64_t size = plat_file_size(path); + // a file that is there but cannot be asked for its size is no reason for a new account: its saves would vanish + if (size < 0 && plat_fs_kind(path) != PLAT_FS_MISSING) rt_fatal("account: cannot read %s", path); if (size <= 0) { - do need(plat_random(&local, sizeof local), "account: random uid"); - while (!local.lo && !local.hi); + // account.uid lost (a profile copied without it, or the save folder alone put in place): the one account + // folder in save/ is the player's, and taking its name keeps that save; otherwise a new account + if (!adopt_save_uid(dir)) { + do need(plat_random(&local, sizeof local), "account: random uid"); + while (!local.lo && !local.hi); + } need(plat_file_write_atomic(path, &local, sizeof local), "account: persist the local account identity"); - rt_log("account: new local account in %s", path); + rt_log("account: %s local account in %s", local_adopted ? "the save folder's" : "new", path); } else { uint64_t got = 0; uint8_t *b = plat_file_read(path, &got);