File tree Expand file tree Collapse file tree 1 file changed +29
-8
lines changed Expand file tree Collapse file tree 1 file changed +29
-8
lines changed Original file line number Diff line number Diff line change 6
6
7
7
#ifndef WIN32
8
8
#include < fcntl.h>
9
+ #include < string>
10
+ #include < sys/file.h>
11
+ #include < sys/utsname.h>
9
12
#else
10
13
#ifndef NOMINMAX
11
14
#define NOMINMAX
@@ -47,20 +50,38 @@ FileLock::~FileLock()
47
50
}
48
51
}
49
52
53
+ static bool IsWSL ()
54
+ {
55
+ struct utsname uname_data;
56
+ return uname (&uname_data) == 0 && std::string (uname_data.version ).find (" Microsoft" ) != std::string::npos;
57
+ }
58
+
50
59
bool FileLock::TryLock ()
51
60
{
52
61
if (fd == -1 ) {
53
62
return false ;
54
63
}
55
- struct flock lock;
56
- lock.l_type = F_WRLCK;
57
- lock.l_whence = SEEK_SET;
58
- lock.l_start = 0 ;
59
- lock.l_len = 0 ;
60
- if (fcntl (fd, F_SETLK, &lock) == -1 ) {
61
- reason = GetErrorReason ();
62
- return false ;
64
+
65
+ // Exclusive file locking is broken on WSL using fcntl (issue #18622)
66
+ // This workaround can be removed once the bug on WSL is fixed
67
+ static const bool is_wsl = IsWSL ();
68
+ if (is_wsl) {
69
+ if (flock (fd, LOCK_EX | LOCK_NB) == -1 ) {
70
+ reason = GetErrorReason ();
71
+ return false ;
72
+ }
73
+ } else {
74
+ struct flock lock;
75
+ lock.l_type = F_WRLCK;
76
+ lock.l_whence = SEEK_SET;
77
+ lock.l_start = 0 ;
78
+ lock.l_len = 0 ;
79
+ if (fcntl (fd, F_SETLK, &lock) == -1 ) {
80
+ reason = GetErrorReason ();
81
+ return false ;
82
+ }
63
83
}
84
+
64
85
return true ;
65
86
}
66
87
#else
You can’t perform that action at this time.
0 commit comments