Exclusively lock save file

Prevents two instances of catgirl from using the same save file and
clobbering each other's data.
weechat-hashes
C. McEnroe 2021-06-11 21:09:46 -04:00
parent c6009cf13c
commit f6e8078c46
1 changed files with 6 additions and 0 deletions

6
ui.c
View File

@ -32,6 +32,7 @@
#include <curses.h> #include <curses.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <limits.h> #include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
@ -1183,6 +1184,11 @@ void uiLoad(const char *name) {
if (!saveFile) exit(EX_CANTCREAT); if (!saveFile) exit(EX_CANTCREAT);
rewind(saveFile); rewind(saveFile);
int error = flock(fileno(saveFile), LOCK_EX | LOCK_NB);
if (error && errno == EWOULDBLOCK) {
errx(EX_CANTCREAT, "%s: save file in use", name);
}
time_t signature; time_t signature;
fread(&signature, sizeof(signature), 1, saveFile); fread(&signature, sizeof(signature), 1, saveFile);
if (ferror(saveFile)) err(EX_IOERR, "fread"); if (ferror(saveFile)) err(EX_IOERR, "fread");