| 69 | } |
| 70 | |
| 71 | bool FileLock::TryLock() |
| 72 | { |
| 73 | if (fd == -1) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // Exclusive file locking is broken on WSL using fcntl (issue #18622) |
| 78 | // This workaround can be removed once the bug on WSL is fixed |
| 79 | static const bool is_wsl = IsWSL(); |
| 80 | if (is_wsl) { |
| 81 | if (flock(fd, LOCK_EX | LOCK_NB) == -1) { |
| 82 | reason = GetErrorReason(); |
| 83 | return false; |
| 84 | } |
| 85 | } else { |
| 86 | struct flock lock; |
| 87 | lock.l_type = F_WRLCK; |
| 88 | lock.l_whence = SEEK_SET; |
| 89 | lock.l_start = 0; |
| 90 | lock.l_len = 0; |
| 91 | if (fcntl(fd, F_SETLK, &lock) == -1) { |
| 92 | reason = GetErrorReason(); |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | #else |
| 100 | |
| 101 | static std::string GetErrorReason() { |
no test coverage detected