From 7b85a2681a7be370d7cb0a9481b31f2727857032 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 26 Oct 2026 09:23:55 +0300 Subject: [PATCH] Keep free_cores=0 as gate on, refuse to rewrite totk.ini after a failed read, drop empty-valued lines on Reset, and add plat_file_set_readonly and plat_file_size Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 30 +++++++++++++++++++++--------- runtime/cfg_probe.c | 20 +++++++++++++------- runtime/kernel.c | 3 ++- runtime/kernel_probe.c | 3 ++- runtime/platform/platform.h | 2 ++ runtime/platform/win32.c | 15 +++++++++++++++ 6 files changed, 55 insertions(+), 18 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 6d6ba2ba..9ecf7059 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -223,15 +223,20 @@ int cfg_reset(const char *const *keys, int n) { const char *name = norm_key(key); snprintf(var, sizeof var, PREFIX "%s", name); if (getenv(var)) continue; // the environment wins; the file line stays - for (int i = 0; i < n_entries; i++) - if (!strcmp(entries[i].key, name)) { - if (!entries[i].value) break; - __atomic_store_n(&entries[i].value, NULL, __ATOMIC_RELEASE); // the old string stays for readers - entries[i].dirty = 0; - entries[i].removed = 1; // cfg_save_to deletes the key's lines - done++; - break; - } + int i = 0; + while (i < n_entries && strcmp(entries[i].key, name)) i++; + if (i == n_entries) { // no value in memory ("key =" in the file): the line still has to go + if (i == CFG_MAX_KEYS) continue; + entries[i] = (CfgEntry){_strdup(name), NULL, 0, 1}; + __atomic_store_n(&n_entries, i + 1, __ATOMIC_RELEASE); + } else if (entries[i].value) { + __atomic_store_n(&entries[i].value, NULL, __ATOMIC_RELEASE); // the old string stays for readers + entries[i].dirty = 0; + entries[i].removed = 1; // cfg_save_to deletes the key's lines + done++; + } else { + entries[i].removed = 1; + } } if (done) __atomic_add_fetch(&version, 1, __ATOMIC_RELEASE); plat_mutex_unlock(&set_lock); @@ -349,6 +354,13 @@ int cfg_save_to(const char *path) { } uint64_t size = 0; uint8_t *data = plat_file_read(path, &size); + if (!data && plat_file_exists(path) && plat_file_size(path) != 0) { + // the file is there but unreadable (locked, transient error): rebuilding it from nothing would lose its lines + rt_log("cfg: cannot read %s, nothing written", path); + __atomic_store_n(&save_ok, 0, __ATOMIC_RELEASE); + plat_mutex_unlock(&set_lock); + return 0; + } const char *text = data ? (const char *)data : ""; int nl = data ? line_count(text, size) : 0; Line *lines = calloc((size_t)nl + 1, sizeof *lines); diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index e5613d8a..21361e67 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -9,9 +9,6 @@ #include "input.h" #include "input_map.h" #include "platform/platform.h" -#ifdef _WIN32 -#include -#endif static int fails; #define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL cfg: %s\n", what); } } while (0) @@ -176,6 +173,17 @@ int probe_cfg(void) { CHECK(got && strstr((const char *)got, "\nvolume = 60\n"), "reset then set: the new line is written"); free(got); + // an active line with an empty value has no entry in memory; Reset still removes it from the file + static const char rempty[] = "[audio]\n# volume = 100\nvolume =\n[input]\nmotion = 0\n"; + CHECK(plat_file_write(path, rempty, sizeof rempty - 1) && cfg_load(path) == 1 && cfg_get("TOTK_VOLUME") == NULL, + "reset: load an ini with an empty value"); + CHECK(cfg_reset(vol, 1) == 0 && cfg_save_to(path) == 1, "reset: an empty-valued key saves"); + got = plat_file_read(path, &got_n); + CHECK(got && got_n == sizeof rbase - 1 && !memcmp(got, rbase, got_n), "reset: the empty-valued line is gone"); + free(got); + cfg_set("TOTK_VOLUME", "60"); + CHECK(cfg_save_to(path) == 1, "reset: volume 60 again for the environment case"); + // a key the environment sets is skipped and keeps its file line CHECK(cfg_load(path) == 1 && EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: reload"); _putenv_s("TOTK_VOLUME", "70"); @@ -196,13 +204,11 @@ int probe_cfg(void) { CHECK(cfg_save_to(bad) == 0 && cfg_last_save_ok() == 0 && EQ(cfg_get("TOTK_VOLUME"), "20"), "save status: an unwritable path fails, the value stays"); cfg_set("TOTK_VOLUME", "21"); -#ifdef _WIN32 CHECK(plat_file_write(path, rbase, sizeof rbase - 1), "write the read-only ini"); - SetFileAttributesA(path, FILE_ATTRIBUTE_READONLY); + CHECK(plat_file_set_readonly(path, 1), "set the file read-only"); CHECK(cfg_save_to(path) == 0 && cfg_last_save_ok() == 0 && EQ(cfg_get("TOTK_VOLUME"), "21"), "save status: a read-only file fails, cfg_get returns the new value"); - SetFileAttributesA(path, FILE_ATTRIBUTE_NORMAL); -#endif + plat_file_set_readonly(path, 0); CHECK(cfg_save_to(path) == 1 && cfg_last_save_ok() == 1, "save status: the retry after the failure works"); got = plat_file_read(path, &got_n); CHECK(got && strstr((const char *)got, "\nvolume = 21\n"), "save status: the retried save wrote the value"); diff --git a/runtime/kernel.c b/runtime/kernel.c index aab2a986..c897f750 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -266,7 +266,8 @@ static void ap_init(void); static int cores_enabled(void) { if (g_cores_on < 0) { - g_cores_on = cfg_get("TOTK_FREE_CORES") ? 0 : 1; + const char *fc = cfg_get("TOTK_FREE_CORES"); + g_cores_on = fc && strcmp(fc, "0") ? 0 : 1; // "0" keeps the gate, like unset diag_init(); ap_init(); } diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index 6cfacac3..f72f6176 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -417,7 +417,8 @@ static void test_cores(Cpu *c) { int now, held_max; kern_core_stats(&now, &held_max); DWORD cpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); - int gate = cfg_get("TOTK_FREE_CORES") == NULL; // the kernel's own switch + const char *fc = cfg_get("TOTK_FREE_CORES"); + int gate = !fc || !strcmp(fc, "0"); // the kernel's own switch int ok = g_run_max <= 3 && held_max <= 3 && !g_core_clash && (cpus < 4 || g_run_max == 3); printf("virtual cores: %d threads x %d runs on %lu host CPUs: at most %ld running guest code at once, at most %d " "core holders (kernel count), %ld core-number clashes: %s\n", CORE_WORKERS, CORE_RUNS, cpus, g_run_max, diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 81d7113b..9c97fe2b 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -58,6 +58,8 @@ int plat_file_write_atomic(const char *path, const void *data, size_t n); int plat_file_flush(const char *path); // flushes a written file's data to the disk; 1 on success void plat_file_delete(const char *path); int plat_file_truncate(const char *path, uint64_t size); // 1 on success +int64_t plat_file_size(const char *path); // bytes of a file, -1 when missing or unreadable +int plat_file_set_readonly(const char *path, int on); // sets or clears the read-only attribute; 1 on success // Calls cb(name, ctx) for each file in dir whose name ends in ext (".in"); name has no directory part. cb returns 0 to // stop. Returns 0 when dir cannot be listed. int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index a9bfb17f..119cebc6 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -236,6 +236,21 @@ int plat_file_truncate(const char *path, uint64_t size) { return ok; } +int64_t plat_file_size(const char *path) { + wchar_t w[WPATH]; + WIN32_FILE_ATTRIBUTE_DATA d; + if (!widen(path, w, WPATH) || !GetFileAttributesExW(w, GetFileExInfoStandard, &d)) return -1; + return (int64_t)(((uint64_t)d.nFileSizeHigh << 32) | d.nFileSizeLow); +} + +int plat_file_set_readonly(const char *path, int on) { + wchar_t w[WPATH]; + if (!widen(path, w, WPATH)) return 0; + DWORD a = GetFileAttributesW(w); + if (a == INVALID_FILE_ATTRIBUTES) return 0; + return SetFileAttributesW(w, on ? a | FILE_ATTRIBUTE_READONLY : a & ~(DWORD)FILE_ATTRIBUTE_READONLY) != 0; +} + int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx) { wchar_t wd[WPATH], we[64], pat[WPATH + 64]; if (!widen(dir, wd, WPATH) || !widen(ext, we, 64)) return 0;