Keep a save commit that cannot replace the committed save instead of dropping it

Windows refuses to rename a directory while any file under it is open, so a
virus scanner, Explorer's preview or a sync tool holding a file of
save/<uid> made every Commit of a session fail after its 2 s of retries.
The game had its Commit return 0 and showed the new slots from the working
copy, and the next start deleted that copy: hours of play were gone (#260,
#263, log line "cannot move the committed save (error 5)").

A Commit now renames its finished staging copy to save-tx/ready-<n> before it
touches the committed save. That rename is the commit point. When the swap
into save/<uid> fails, ready-<n> stays the newest save: later working copies
and the next start are made from it, and the next Commit or start retries the
swap. The failure log names the programs holding files in the committed save
(Restart Manager) or says the folder refuses the rename (no DELETE access).
--probe-fs holds a committed file open across two Commits and restarts.

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:20:50 +03:00
1 parent 8a4ab178ec
commit 65fd631fea
4 files changed
+221 -40

No files matched your search

+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;