diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 8fc619cc..cd3e095b 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -626,6 +626,12 @@ static void storage_init(void *param) { uint64_t uid[2]; char p[PATH_CAP]; make_dirs(profile); + // One game per profile: a second one's start would take the first one's commit in progress for an interrupted + // one and delete it, and both would write into one working copy (save-tx/work), losing or mixing their saves. + snprintf(p, sizeof p, "%s/instance.lock", profile); + int held = plat_lock_file(p); + if (!held) rt_fatal("fs: another copy of the game is running with the profile %s; close it first", profile); + if (held < 0) rt_log("fs: cannot make %s; a second copy of the game would not be stopped", p); account_uid(uid); snprintf(p, sizeof p, "%s/save", profile); make_dirs(p); @@ -1810,6 +1816,9 @@ static void probe_storage(void) { CHECK(RES(S_DELETE_CACHE, 3) == 0, "DeleteCacheStorage"); CALL(S_UNMOUNT, U("save")); + char lock[PLAT_PATH_MAX * 3 + 96]; + snprintf(lock, sizeof lock, "%s/instance.lock", dir); + plat_unlock_file(lock); // storage_init's one-game-per-profile lock, held open until given back int removed = !remove_tree(dir) && !exists(dir); CHECK(removed, "the temporary profile was not deleted"); printf("storage: save create/write/commit/reopen, directories, rename/delete, temporary and cache storage in a " diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index ecf68ffe..67a866e3 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -731,6 +732,49 @@ int plat_file_replace(const char *from, const char *to) { return 1; } +static char lock_held[4][1100]; +static int lock_fd[4]; +static int lock_n; +static PlatMutex lock_lock = PLAT_MUTEX_INIT; + +int plat_lock_file(const char *path) { + plat_mutex_lock(&lock_lock); + int r = 1, nheld = lock_n; + for (int i = 0; i < nheld; i++) + if (!strcmp(lock_held[i], path)) goto done; + int fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0644); // open until plat_unlock_file or the process ends + if (fd < 0) { + r = -1; + goto done; + } + if (flock(fd, LOCK_EX | LOCK_NB)) { + r = errno == EWOULDBLOCK ? 0 : -1; + close(fd); + goto done; + } + if (nheld < 4) { + snprintf(lock_held[nheld], sizeof lock_held[0], "%s", path); + lock_fd[nheld] = fd; + lock_n = nheld + 1; + } +done: + plat_mutex_unlock(&lock_lock); + return r; +} + +void plat_unlock_file(const char *path) { + plat_mutex_lock(&lock_lock); + for (int i = 0; i < lock_n; i++) + if (!strcmp(lock_held[i], path)) { + close(lock_fd[i]); + lock_n--; + memmove(&lock_held[i], &lock_held[i + 1], sizeof lock_held[0] * (size_t)(lock_n - i)); + memmove(&lock_fd[i], &lock_fd[i + 1], sizeof lock_fd[0] * (size_t)(lock_n - i)); + break; + } + plat_mutex_unlock(&lock_lock); +} + int plat_file_write_atomic(const char *path, const void *data, size_t n) { // A rename replaces a read-only file here (only the folder's permission counts) but not on Windows: a file the // player made read-only stays as it is on both. diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 33c5bd56..590d4486 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -288,6 +288,10 @@ int plat_file_replace(const char *from, const char *to); // rename, replacing t // Writes path...tmp, flushes it to the disk and renames it over path: a crash leaves the old file or the new // one, never a torn or zero-filled one. 1 on success. int plat_file_write_atomic(const char *path, const void *data, size_t n); +// Takes the file at path (made when missing) as a lock no other process can take, held until this process ends: +// 1 held (again, when this process holds it already), 0 another process holds it, -1 the file cannot be made. +int plat_lock_file(const char *path); +void plat_unlock_file(const char *path); // gives a lock plat_lock_file took back (probes, before deleting it) int plat_file_flush(const char *path); // flushes a written file's data to the disk; 1 on success int plat_file_sync(FILE *f); // fflush, then the file's data to the disk; 1 on success // fseek and ftell with 64-bit offsets (a long is 32 bits on Windows). diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 2db33470..8fd2da87 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -527,6 +527,49 @@ int plat_file_replace(const char *from, const char *to) { return MoveFileExW(a, b, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0; } +static char lock_held[4][1100]; +static HANDLE lock_handle[4]; +static int lock_n; +static PlatMutex lock_lock = PLAT_MUTEX_INIT; + +int plat_lock_file(const char *path) { + plat_mutex_lock(&lock_lock); + int r = 1, nheld = lock_n; + for (int i = 0; i < nheld; i++) + if (!strcmp(lock_held[i], path)) goto done; + wchar_t w[WPATH]; + // no sharing at all: a second process's open fails with a sharing violation; the handle stays open until + // plat_unlock_file or the process ends + HANDLE h = widen(path, w, WPATH) ? CreateFileW(w, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, NULL) + : INVALID_HANDLE_VALUE; + if (h == INVALID_HANDLE_VALUE) { + r = GetLastError() == ERROR_SHARING_VIOLATION ? 0 : -1; + goto done; + } + if (nheld < 4) { + snprintf(lock_held[nheld], sizeof lock_held[0], "%s", path); + lock_handle[nheld] = h; + lock_n = nheld + 1; + } +done: + plat_mutex_unlock(&lock_lock); + return r; +} + +void plat_unlock_file(const char *path) { + plat_mutex_lock(&lock_lock); + for (int i = 0; i < lock_n; i++) + if (!strcmp(lock_held[i], path)) { + CloseHandle(lock_handle[i]); + lock_n--; + memmove(&lock_held[i], &lock_held[i + 1], sizeof lock_held[0] * (size_t)(lock_n - i)); + memmove(&lock_handle[i], &lock_handle[i + 1], sizeof lock_handle[0] * (size_t)(lock_n - i)); + break; + } + plat_mutex_unlock(&lock_lock); +} + int plat_file_write_atomic(const char *path, const void *data, size_t n) { char tmp[WPATH + 48]; wchar_t a[WPATH], b[WPATH];