summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-08-06 16:37:00 +0200
committerDavid Robillard <d@drobilla.net>2020-08-06 21:35:16 +0200
commit64fbc5533bbb85ee9b35b44ac240d75be9f8e267 (patch)
tree34fa733d732c972436c5a164ae320e9ad60b0c65
parent1dd3439bb66eae7b61a3ce22b72ae8668fec1b58 (diff)
downloadlilv-64fbc5533bbb85ee9b35b44ac240d75be9f8e267.tar.gz
lilv-64fbc5533bbb85ee9b35b44ac240d75be9f8e267.tar.bz2
lilv-64fbc5533bbb85ee9b35b44ac240d75be9f8e267.zip
Implement file locking on Windows
-rw-r--r--src/filesystem.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/filesystem.c b/src/filesystem.c
index f521179..e610d07 100644
--- a/src/filesystem.c
+++ b/src/filesystem.c
@@ -305,7 +305,20 @@ lilv_symlink(const char* oldpath, const char* newpath)
int
lilv_flock(FILE* file, bool lock, bool block)
{
-#if defined(HAVE_FLOCK) && defined(HAVE_FILENO)
+#ifdef _WIN32
+ HANDLE handle = (HANDLE)_get_osfhandle(fileno(file));
+ OVERLAPPED overlapped = {0};
+
+ if (lock) {
+ const DWORD flags = (LOCKFILE_EXCLUSIVE_LOCK |
+ (block ? 0 : LOCKFILE_FAIL_IMMEDIATELY));
+
+ return !LockFileEx(
+ handle, flags, 0, UINT32_MAX, UINT32_MAX, &overlapped);
+ } else {
+ return !UnlockFileEx(handle, 0, UINT32_MAX, UINT32_MAX, &overlapped);
+ }
+#elif defined(HAVE_FLOCK) && defined(HAVE_FILENO)
return flock(fileno(file),
(lock ? LOCK_EX : LOCK_UN) | (block ? 0 : LOCK_NB));
#else