mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-19 13:27:35 +02:00
Fix WSL file locking by using flock instead of fcntl
Co-authored-by: sipa <pieter@wuille.net>
This commit is contained in:
parent
6ae99aab5d
commit
e8fa0a3d20
1 changed files with 29 additions and 8 deletions
37
src/fs.cpp
37
src/fs.cpp
|
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <string>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/utsname.h>
|
||||||
#else
|
#else
|
||||||
#ifndef NOMINMAX
|
#ifndef NOMINMAX
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
|
|
@ -47,20 +50,38 @@ FileLock::~FileLock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool IsWSL()
|
||||||
|
{
|
||||||
|
struct utsname uname_data;
|
||||||
|
return uname(&uname_data) == 0 && std::string(uname_data.version).find("Microsoft") != std::string::npos;
|
||||||
|
}
|
||||||
|
|
||||||
bool FileLock::TryLock()
|
bool FileLock::TryLock()
|
||||||
{
|
{
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
struct flock lock;
|
|
||||||
lock.l_type = F_WRLCK;
|
// Exclusive file locking is broken on WSL using fcntl (issue #18622)
|
||||||
lock.l_whence = SEEK_SET;
|
// This workaround can be removed once the bug on WSL is fixed
|
||||||
lock.l_start = 0;
|
static const bool is_wsl = IsWSL();
|
||||||
lock.l_len = 0;
|
if (is_wsl) {
|
||||||
if (fcntl(fd, F_SETLK, &lock) == -1) {
|
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
|
||||||
reason = GetErrorReason();
|
reason = GetErrorReason();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
struct flock lock;
|
||||||
|
lock.l_type = F_WRLCK;
|
||||||
|
lock.l_whence = SEEK_SET;
|
||||||
|
lock.l_start = 0;
|
||||||
|
lock.l_len = 0;
|
||||||
|
if (fcntl(fd, F_SETLK, &lock) == -1) {
|
||||||
|
reason = GetErrorReason();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue