summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/syscalls.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2010-09-06 02:38:25 +0200
committerAdam Borowski <kilobyte@angband.pl>2010-09-06 02:38:25 +0200
commitb57f86755026f0eea077c763259e4a16c2ae0925 (patch)
tree1b852325d092658ae1fdf55d1a02ae791057840a /crawl-ref/source/syscalls.cc
parenta16ef2ae22a9124bbe19495e301b86525946a7b3 (diff)
downloadcrawl-ref-b57f86755026f0eea077c763259e4a16c2ae0925.tar.gz
crawl-ref-b57f86755026f0eea077c763259e4a16c2ae0925.zip
Use file locking on the save file to prevent corruption from running Crawl twice.
This fixes Mantis 2061. I did not reuse the existing lk_open() because it's non-portable, waits for the lock to become available and works using FILE* not a descriptor (bad for random access).
Diffstat (limited to 'crawl-ref/source/syscalls.cc')
-rw-r--r--crawl-ref/source/syscalls.cc32
1 files changed, 32 insertions, 0 deletions
diff --git a/crawl-ref/source/syscalls.cc b/crawl-ref/source/syscalls.cc
new file mode 100644
index 0000000000..c223b7db98
--- /dev/null
+++ b/crawl-ref/source/syscalls.cc
@@ -0,0 +1,32 @@
+/*
+ * File: syscalls.cc
+ * Summary: Wrappers for sys/libc calls, mostly for charset purposes.
+ * Written by: Adam Borowski
+ */
+
+#include "AppHdr.h"
+#include "syscalls.h"
+
+#ifdef TARGET_OS_WINDOWS
+
+// FIXME! This file doesn't yet use rename() (but we'll redefine it anyway once
+// charset handling comes).
+#undef rename
+
+#include <io.h>
+#endif
+
+bool lock_file(int fd)
+{
+#ifdef TARGET_OS_WINDOWS
+ return !!LockFile((HANDLE)_get_osfhandle(fd), 0, 0, -1, -1);
+#else
+ struct flock fl;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+
+ return !fcntl(fd, F_SETLK, &fl);
+#endif
+}