summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-06-11 21:09:46 -0400
committerJune McEnroe <june@causal.agency>2021-06-11 21:15:34 -0400
commitf6e8078c4698d371359ff8827a017535e421496c (patch)
tree2dbe42ed831a8bcbc1a337fe75f2a5e4f764cd42
parentOpen save file with "a+" (diff)
downloadcatgirl-f6e8078c4698d371359ff8827a017535e421496c.tar.gz
catgirl-f6e8078c4698d371359ff8827a017535e421496c.zip
Exclusively lock save file
Prevents two instances of catgirl from using the same save file and
clobbering each other's data.
-rw-r--r--ui.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/ui.c b/ui.c
index 22b098e..2beba88 100644
--- a/ui.c
+++ b/ui.c
@@ -32,6 +32,7 @@
 #include <curses.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <signal.h>
 #include <stdarg.h>
@@ -1183,6 +1184,11 @@ void uiLoad(const char *name) {
 	if (!saveFile) exit(EX_CANTCREAT);
 	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;
 	fread(&signature, sizeof(signature), 1, saveFile);
 	if (ferror(saveFile)) err(EX_IOERR, "fread");