Merge task/savecommit: a save commit that cannot replace the committed save waits in save-tx instead of being lost (#260, #263)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-31 20:35:26 +03:00
commit 1ba0424774
4 files changed
+221 -40

No files matched your search

+122 -40
View File
@@ -146,31 +146,36 @@ static int remove_tree(const char *path) {
}
// ---- save transactions
// The save mount's root is a working copy, made from the committed save when the first save mount appears. Commit
// copies it to a staging directory and swaps that in for the committed save:
// The save mount's root is a working copy, made from the newest save when the first save mount appears. Commit copies
// it to a staging directory, which a rename makes a finished commit, and swaps that in for the committed save:
//
// <profile>/save/<uid>/ committed save: what the next start loads
// <profile>/save/<uid>/ committed save: what the next start loads when no finished commit waits
// <profile>/save-tx/work/ working copy, the save mount's root; deleted at the last Unmount and at start
// <profile>/save-tx/new/ Commit's staging copy of the working copy
// <profile>/save-tx/ready-<n>/ a finished commit not swapped in yet: the newest save
// <profile>/save-tx/old/ the committed save a Commit replaced, on its way to save-backup
//
// Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the committed
// one as a hard link to it; (2) rename save/<uid> to old;
// (3) rename new to save/<uid>, the commit point; (4) move old to save-backup/<UTC time> and keep the newest three.
// Both renames are durable (plat_fs_rename). The game's Commit returns 0 once steps (1) to (4) are
// queued; a thread runs them (95-135 ms on the game thread before, in the 1.4.3 fight save) while every change to the
// working copy waits in save_touched, and Unmount and the next Commit wait too. A crash in that time loses this Commit
// only, and a failed one is logged instead of returned. save/<uid> is renamed away only after new is complete
// on disk, so the recovery rule at start (save_recover) is:
// save/<uid> present it is the committed save (the old one if the crash came before (3), the new one after);
// new, if present, is an unfinished copy and is deleted (rollback);
// save/<uid> absent the crash came between (2) and (3) and new is complete: rename it to save/<uid> (roll
// forward); with no new either, old goes back to save/<uid>.
// Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the newest
// save's as a hard link to it; (2) rename new to ready-<n>, the commit point, and delete the ready-<n> it supersedes;
// (3) rename save/<uid> to old; (4) rename ready-<n> to save/<uid>; (5) move old to save-backup/<UTC time> and keep
// the newest three. Every rename is durable (plat_fs_rename). Windows refuses (3) while any file under save/<uid> is
// open, in this or another process (a virus scanner, Explorer's preview, a sync tool), and a folder an elevated run
// made can refuse it too. ready-<n> then stays the newest save: later working copies are made from it, and the next
// Commit and the next start try (3) to (5) again. The game's Commit returns 0 once the steps are queued; a thread runs
// them (95-135 ms on the game thread before, in the 1.4.3 fight save) while every change to the working copy waits in
// save_touched, and Unmount and the next Commit wait too. A crash in that time loses this Commit only, and a failed
// one is logged instead of returned. The recovery rule at start (save_recover):
// new present an unfinished copy: deleted (rollback). An older build renamed save/<uid> away before its
// finished copy: with save/<uid> and ready-<n> absent, new is that copy and becomes save/<uid>;
// ready-<n> present the highest n is the newest save and (3) to (5) run (roll forward); the others are deleted;
// save/<uid> absent with no ready-<n> either, old goes back to save/<uid>.
// Then old, if present, goes to save-backup, and work (changes no Commit took) is deleted.
#define SAVE_BACKUPS 3
#define COPY_CHUNK (1u << 20)
static char tx_root[1100], backup_root[1100], new_root[1100], old_root[1100];
static char ready_root[1100]; // the newest finished commit not swapped in, empty when none
static unsigned ready_seq; // the highest ready-<n> this run made or found
static PlatMutex tx_lock = PLAT_MUTEX_INIT; // taken before lock, never inside it
static int save_mounts; // M_SAVE mounts: the working copy exists while this is above 0
@@ -325,22 +330,80 @@ static void rotate_backup(void) {
}
}
// The newest save: the finished commit waiting to be swapped in, else the committed save.
static const char *newest_save(void) { return *ready_root ? ready_root : save_root; }
// Steps (3) to (5): swap the finished commit ready_root in for the committed save. 0 on success; on failure
// ready_root stays the newest save.
static int swap_in(void) {
if (!*ready_root) return 0;
rotate_backup(); // an old left by a failed (5): (3) needs its name
if (exists(save_root) && move_dir(save_root, old_root)) {
char why[512];
plat_fs_busy_reason(save_root, why, sizeof why);
rt_log("fs: save commit: the committed save cannot be replaced (%s); the commit waits in %s and the next "
"Commit or start retries", *why ? why : "a file in it is in use", ready_root);
return -1;
}
if (crashed()) return -1;
if (move_dir(ready_root, save_root)) {
rt_log("fs: save commit: cannot move the finished commit to the committed save; it waits in %s", ready_root);
move_dir(old_root, save_root); // else save/<uid> stays absent and the next start rolls the commit forward
return -1;
}
*ready_root = 0;
if (crashed()) return -1;
rotate_backup();
return 0;
}
// The number n of a "ready-<n>" name, 0 for any other name.
static unsigned ready_number(const char *name) {
char *end;
if (strncmp(name, "ready-", 6) || name[6] < '0' || name[6] > '9') return 0;
unsigned long n = strtoul(name + 6, &end, 10);
return *end || n > 0xffffffffu ? 0 : (unsigned)n;
}
// Startup: the newest finished commit into ready_root; the older ones, which it supersedes, are deleted.
static void ready_scan(void) {
Listing l;
list_dir(tx_root, &l);
*ready_root = 0, ready_seq = 0;
for (int i = 0; i < l.n; i++)
if (l.e[i].kind == PLAT_FS_DIR && ready_number(l.e[i].name) > ready_seq) ready_seq = ready_number(l.e[i].name);
for (int i = 0; i < l.n; i++) {
unsigned n = l.e[i].kind == PLAT_FS_DIR ? ready_number(l.e[i].name) : 0;
char *p = n ? join(tx_root, l.e[i].name) : NULL;
if (n == ready_seq && p) snprintf(ready_root, sizeof ready_root, "%s", p);
else if (p) remove_tree(p);
free(p);
}
listing_free(l.e, l.n);
}
// Startup: finish or roll back an interrupted Commit and drop uncommitted changes (the rule above).
static void save_recover(void) {
const char *did = "nothing to recover";
ready_scan();
int has_save = exists(save_root);
if (!has_save && exists(new_root)) {
if (exists(new_root) && !has_save && !*ready_root) {
if (move_dir(new_root, save_root)) rt_fatal("fs: cannot complete an interrupted save commit");
did = "completed an interrupted commit";
} else if (!has_save && exists(old_root)) {
if (move_dir(old_root, save_root)) rt_fatal("fs: cannot roll back an interrupted save commit");
did = "rolled back an interrupted commit";
has_save = 1;
} else if (exists(new_root)) {
if (remove_tree(new_root)) rt_fatal("fs: cannot delete an unfinished save commit");
did = "rolled back an unfinished commit";
}
if (!has_save && !*ready_root && exists(old_root)) {
if (move_dir(old_root, save_root)) rt_fatal("fs: cannot roll back an interrupted save commit");
did = "rolled back an interrupted commit";
} else if (exists(old_root))
did = "moved the save an interrupted commit replaced to save-backup";
rotate_backup();
if (*ready_root)
did = swap_in() ? "a finished commit still waits in save-tx; the game loads it from there"
: "completed a finished commit";
if (exists(work_root)) {
if (remove_tree(work_root)) rt_fatal("fs: cannot delete the old save working copy");
if (!strcmp(did, "nothing to recover"))
@@ -349,11 +412,11 @@ static void save_recover(void) {
rt_log("host save recovery: %s", did);
}
// The first save mount: a fresh working copy of the committed save.
// The first save mount: a fresh working copy of the newest save.
static void work_create(void) {
int n = 0, linked = 0;
uint64_t bytes = 0;
if (remove_tree(work_root) || copy_tree(save_root, work_root, NULL, 0, &n, &linked, &bytes))
if (remove_tree(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);
}
@@ -364,38 +427,36 @@ static void work_discard(void) {
if (remove_tree(work_root)) rt_log("fs: save working copy not fully deleted (a file still open?)");
}
// Commit of the save mount, tx_lock held. Steps (1) to (4) above; every failure before (3) leaves the committed save.
// Commit of the save mount, tx_lock held. Steps (1) to (5) above; a failure before (2) leaves the newest save as it
// was, one after it leaves the commit waiting in ready_root.
static uint32_t save_commit_locked(void) {
uint64_t t0 = plat_now_ns(), bytes = 0;
int nfiles = 0, linked = 0;
tx_step = tx_dead = 0;
rotate_backup(); // an old left by a failed (4)
const char *base = exists(save_root) ? save_root : NULL;
const char *base = exists(newest_save()) ? newest_save() : NULL;
if (remove_tree(new_root) || copy_tree(work_root, new_root, base, 1, &nfiles, &linked, &bytes)) {
if (tx_dead) return FS_R_INVALID_ARGUMENT;
rt_log("fs: save commit: copying the working copy failed; committed save unchanged");
rt_log("fs: save commit: copying the working copy failed; the newest save is unchanged");
remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
if (exists(save_root) && move_dir(save_root, old_root)) {
rt_log("fs: save commit: cannot move the committed save; committed save unchanged");
char ready[1100];
snprintf(ready, sizeof ready, "%s/ready-%010u", tx_root, ready_seq + 1);
if (move_dir(new_root, ready)) {
rt_log("fs: save commit: cannot finish the staging copy; the newest save is unchanged");
remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
if (move_dir(new_root, save_root)) {
if (move_dir(old_root, save_root))
rt_fatal("fs: save commit: cannot swap in the new save; the next start completes it");
rt_log("fs: save commit: cannot swap in the new save; committed save unchanged");
remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
rotate_backup();
ready_seq++;
if (*ready_root) remove_tree(ready_root); // superseded: the working copy it was made from held all of it
snprintf(ready_root, sizeof ready_root, "%s", ready);
plat_xchg32(&save_dirty, 0);
rt_log("fs commit save: %d files, %d changed (%.1f MiB written), %d unchanged linked, in %.0f ms", nfiles,
nfiles - linked, bytes / 1048576.0, linked, (plat_now_ns() - t0) / 1e6);
if (crashed()) return FS_R_INVALID_ARGUMENT;
int waits = swap_in();
if (tx_dead) return FS_R_INVALID_ARGUMENT;
rt_log("fs commit save: %d files, %d changed (%.1f MiB written), %d unchanged linked, in %.0f ms%s", nfiles,
nfiles - linked, bytes / 1048576.0, linked, (plat_now_ns() - t0) / 1e6, waits ? "; waits in save-tx" : "");
return 0;
}
@@ -1401,7 +1462,8 @@ static void probe_save_tx(const uint64_t *uid) {
else mixed++;
CHECK(ok && (va == vb) && (va == committed), "crash at commit step %d: tx_a %d, tx_b %d (old %d, new %d)", k,
va, vb, committed, next);
CHECK(!exists(new_root) && !exists(old_root), "crash at commit step %d: staging left after recovery", k);
CHECK(!exists(new_root) && !exists(old_root) && !*ready_root, "crash at commit step %d: staging left after "
"recovery", k);
}
CHECK(steps >= 4 && olds && news && !mixed, "crash points: %d (%d old, %d new, %d mixed)", steps, olds, news,
mixed);
@@ -1428,6 +1490,26 @@ static void probe_save_tx(const uint64_t *uid) {
"an Unmount right after Commit lost it");
committed = gen;
// a file of the committed save held open by another program (a virus scanner, Explorer's preview, a sync tool):
// Windows refuses to rename a directory with an open file under it, so the committed save cannot be swapped out.
// The game and a restart still see every Commit, and the first start after the file is closed swaps them in.
char *held_path = join(save_root, "tx_b.sav");
PlatFile *held = plat_fs_open(held_path, 0, NULL);
free(held_path);
int pending = ++gen;
CHECK(held && tx_write(a, pending) && tx_commit() == 0, "Commit with a committed file held open");
probe_restart(uid);
CHECK(tx_read(a) == pending, "a Commit made while a committed file was held open, after a restart: tx_a %d, "
"expected %d", tx_read(a), pending);
pending = ++gen;
CHECK(tx_write(a, pending) && tx_commit() == 0 && tx_read(a) == pending, "a second Commit while held open");
if (held) plat_file_close(held);
probe_restart(uid);
CHECK(tx_read(a) == pending && tx_read_committed("tx_a.sav") == pending && tx_read_committed("tx_b.sav") == kept,
"the held-open Commits after the file was closed and a restart: tx_a %d, on disk %d, expected %d",
tx_read(a), tx_read_committed("tx_a.sav"), pending);
committed = pending;
// the read-only mount shows the committed save and refuses changes
CHECK(tx_write(a, 99) && RES(S_MOUNT_SAVE_RO, U("ro"), TOTK_APPLICATION_ID, U(uid)) == 0 &&
tx_read("ro:/tx_a.sav") == committed && RES(S_OPEN_FILE, U(&h), U("ro:/tx_a.sav"), FS_OPEN_WRITE) ==
+6
View File
@@ -1094,6 +1094,12 @@ void plat_fs_sync_dir(const char *path) {
}
}
// Open files never stop a rename here.
void plat_fs_busy_reason(const char *dir, char *out, size_t cap) {
(void)dir;
if (cap) *out = 0;
}
int plat_fs_list(const char *dir, int (*cb)(const char *name, int kind, uint64_t size, void *ctx), void *ctx) {
DIR *d = opendir(dir);
if (!d) return fs_err(errno);
+4
View File
@@ -307,6 +307,10 @@ int plat_fs_rename(const char *from, const char *to, int durable);
int plat_fs_link(const char *existing, const char *path); // a hard link
int plat_fs_links(const char *path); // the file's hard link count, 0 when unknown
void plat_fs_sync_dir(const char *path); // a directory's entries to the disk, as far as the FS allows
// Why dir could not be renamed, for the log: into out "no permission to rename it" when this process may not delete
// it, else the programs holding a file under it ("explorer.exe 1234, ..."), else an empty string. Windows refuses to
// rename a directory with any file open under it; elsewhere out is always empty.
void plat_fs_busy_reason(const char *dir, char *out, size_t cap);
// cb(name, kind, size, ctx) for each entry of dir but "." and ".."; cb returns 0 to stop. The listing's error.
int plat_fs_list(const char *dir, int (*cb)(const char *name, int kind, uint64_t size, void *ctx), void *ctx);
// An existing file for reading and, with write, writing; *err (may be NULL) gets the reason on failure.
+89
View File
@@ -7,6 +7,7 @@
#include <pdh.h>
#include <pdhmsg.h>
#include <psapi.h>
#include <restartmanager.h>
#include <shellapi.h>
#include <timeapi.h>
#include <signal.h>
@@ -875,6 +876,94 @@ void plat_fs_sync_dir(const char *path) {
}
}
// Restart Manager (rstrtmgr.dll, loaded on first use) names the processes that hold a file open.
typedef DWORD(WINAPI *RmStartSession_t)(DWORD *, DWORD, WCHAR *);
typedef DWORD(WINAPI *RmRegisterResources_t)(DWORD, UINT, LPCWSTR *, UINT, RM_UNIQUE_PROCESS *, UINT, LPCWSTR *);
typedef DWORD(WINAPI *RmGetList_t)(DWORD, UINT *, UINT *, RM_PROCESS_INFO *, LPDWORD);
typedef DWORD(WINAPI *RmEndSession_t)(DWORD);
static struct {
INIT_ONCE once;
RmStartSession_t start;
RmRegisterResources_t add;
RmGetList_t list;
RmEndSession_t end;
} rm = {INIT_ONCE_STATIC_INIT};
static BOOL CALLBACK rm_load(INIT_ONCE *o, void *p, void **c) {
(void)o, (void)p, (void)c;
HMODULE l = plat_lib_open("rstrtmgr.dll");
if (!l) return TRUE;
rm.add = (RmRegisterResources_t)(void *)GetProcAddress(l, "RmRegisterResources");
rm.list = (RmGetList_t)(void *)GetProcAddress(l, "RmGetList");
rm.end = (RmEndSession_t)(void *)GetProcAddress(l, "RmEndSession");
RmStartSession_t start = (RmStartSession_t)(void *)GetProcAddress(l, "RmStartSession");
if (rm.add && rm.list && rm.end) rm.start = start; // start set: all of them are there
return TRUE;
}
// The files under dir (links not followed), at most BUSY_FILES of them, each a malloc'd path.
#define BUSY_FILES 256
typedef struct { LPCWSTR p[BUSY_FILES]; UINT n; } BusyFiles;
static void busy_files(const wchar_t *dir, BusyFiles *f) {
wchar_t pat[WPATH + 4];
swprintf(pat, WPATH + 4, L"%ls\\*", dir);
WIN32_FIND_DATAW fd;
HANDLE h = FindFirstFileW(pat, &fd);
if (h == INVALID_HANDLE_VALUE) return;
do {
DWORD a = fd.dwFileAttributes;
if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..") || (a & FILE_ATTRIBUTE_REPARSE_POINT)) continue;
size_t n = wcslen(dir) + wcslen(fd.cFileName) + 2;
wchar_t *p = n <= WPATH ? malloc(n * sizeof *p) : NULL;
if (!p) continue;
swprintf(p, n, L"%ls\\%ls", dir, fd.cFileName);
if (a & FILE_ATTRIBUTE_DIRECTORY) busy_files(p, f), free(p);
else f->p[f->n++] = p;
} while (f->n < BUSY_FILES && FindNextFileW(h, &fd));
FindClose(h);
}
void plat_fs_busy_reason(const char *dir, char *out, size_t cap) {
if (!cap) return;
*out = 0;
wchar_t w[WPATH];
if (!widen(dir, w, WPATH)) return;
for (wchar_t *q = w; *q; q++)
if (*q == L'/') *q = L'\\';
// a rename needs DELETE access to the directory: a folder made by an elevated run can refuse it
HANDLE h = CreateFileW(w, DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_ACCESS_DENIED) snprintf(out, cap, "no permission to rename it");
return;
}
CloseHandle(h);
InitOnceExecuteOnce(&rm.once, rm_load, NULL, NULL);
DWORD session;
WCHAR key[CCH_RM_SESSION_KEY + 1] = {0};
if (!rm.start || rm.start(&session, 0, key) != ERROR_SUCCESS) return;
BusyFiles f = {.n = 0};
busy_files(w, &f);
RM_PROCESS_INFO info[8];
UINT need = 0, n = 8;
DWORD reasons = 0, e = f.n ? rm.add(session, f.n, f.p, 0, NULL, 0, NULL) : ERROR_SUCCESS;
if (f.n && e == ERROR_SUCCESS) e = rm.list(session, &need, &n, info, &reasons);
else n = 0;
rm.end(session);
for (UINT i = 0; i < f.n; i++) free((void *)f.p[i]);
if (e == ERROR_MORE_DATA) snprintf(out, cap, "%u programs hold files in it", need); // info is not filled then
if (e != ERROR_SUCCESS) return;
size_t at = 0;
for (UINT i = 0; i < n && at < cap; i++) { // the program's display name ("Windows Explorer") and process id
char name[CCH_RM_MAX_APP_NAME + 1];
if (!WideCharToMultiByte(CP_UTF8, 0, info[i].strAppName, -1, name, sizeof name, NULL, NULL)) strcpy(name, "?");
int k = snprintf(out + at, cap - at, "%s%s %lu", i ? ", " : "", name, info[i].Process.dwProcessId);
if (k < 0) break;
at += (size_t)k;
}
}
int plat_fs_list(const char *dir, int (*cb)(const char *name, int kind, uint64_t size, void *ctx), void *ctx) {
wchar_t wd[WPATH], pat[WPATH + 4];
if (!widen(dir, wd, WPATH)) return PLAT_FS_ERROR;