From 64fbc5533bbb85ee9b35b44ac240d75be9f8e267 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 6 Aug 2020 16:37:00 +0200 Subject: Implement file locking on Windows --- src/filesystem.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src') 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 -- cgit v1.2.1