aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/flock.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/flock.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/flock.cpp')
-rw-r--r--util/system/flock.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/util/system/flock.cpp b/util/system/flock.cpp
new file mode 100644
index 0000000000..fe88fecaff
--- /dev/null
+++ b/util/system/flock.cpp
@@ -0,0 +1,71 @@
+#include "flock.h"
+
+#ifndef _unix_
+
+ #include <util/generic/utility.h>
+
+ #include "winint.h"
+ #include <io.h>
+ #include <errno.h>
+
+ #ifdef __cplusplus
+extern "C" {
+ #endif
+
+ int flock(int fd, int op) {
+ return Flock((HANDLE)_get_osfhandle(fd), op);
+ }
+
+ int Flock(void* hdl, int op) {
+ errno = 0;
+
+ if (hdl == INVALID_HANDLE_VALUE) {
+ errno = EBADF;
+ return -1;
+ }
+
+ DWORD low = 1, high = 0;
+ OVERLAPPED io;
+
+ Zero(io);
+
+ UnlockFileEx(hdl, 0, low, high, &io);
+
+ switch (op & ~LOCK_NB) {
+ case LOCK_EX:
+ case LOCK_SH: {
+ auto mode = ((op & ~LOCK_NB) == LOCK_EX) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
+ if (op & LOCK_NB) {
+ if (LockFileEx(hdl, mode | LOCKFILE_FAIL_IMMEDIATELY, 0, low, high, &io)) {
+ return 0;
+ } else if (GetLastError() == ERROR_LOCK_VIOLATION) {
+ ClearLastSystemError();
+ errno = EWOULDBLOCK;
+ return -1;
+ }
+ } else {
+ if (LockFileEx(hdl, mode, 0, low, high, &io)) {
+ return 0;
+ }
+ }
+ break;
+ }
+ case LOCK_UN:
+ return 0;
+ break;
+ default:
+ break;
+ }
+ errno = EINVAL;
+ return -1;
+ }
+
+ int fsync(int fd) {
+ return _commit(fd);
+ }
+
+ #ifdef __cplusplus
+}
+ #endif
+
+#endif