mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-02 07:50:31 +00:00
Fix POSIX advisory locks on Windows
This commit is contained in:
parent
050062bcbb
commit
a5fa90a21f
14 changed files with 472 additions and 39 deletions
|
@ -30,6 +30,7 @@ const char *DescribeNtFileFlagAttr(char[256], uint32_t);
|
|||
const char *DescribeNtFileMapFlags(char[64], uint32_t);
|
||||
const char *DescribeNtFileShareFlags(char[64], uint32_t);
|
||||
const char *DescribeNtFiletypeFlags(char[64], uint32_t);
|
||||
const char *DescribeNtLockFileFlags(char[64], uint32_t);
|
||||
const char *DescribeNtMovFileInpFlags(char[256], uint32_t);
|
||||
const char *DescribeNtPageFlags(char[64], uint32_t);
|
||||
const char *DescribeNtPipeModeFlags(char[64], uint32_t);
|
||||
|
@ -72,6 +73,7 @@ const char *DescribeWhence(char[12], int);
|
|||
#define DescribeNtFileMapFlags(x) DescribeNtFileMapFlags(alloca(64), x)
|
||||
#define DescribeNtFileShareFlags(x) DescribeNtFileShareFlags(alloca(64), x)
|
||||
#define DescribeNtFiletypeFlags(x) DescribeNtFiletypeFlags(alloca(64), x)
|
||||
#define DescribeNtLockFileFlags(x) DescribeNtLockFileFlags(alloca(64), x)
|
||||
#define DescribeNtMovFileInpFlags(x) DescribeNtMovFileInpFlags(alloca(256), x)
|
||||
#define DescribeNtPageFlags(x) DescribeNtPageFlags(alloca(64), x)
|
||||
#define DescribeNtPipeModeFlags(x) DescribeNtPipeModeFlags(alloca(64), x)
|
||||
|
|
31
libc/intrin/describentlockfileflags.c
Normal file
31
libc/intrin/describentlockfileflags.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/enum/filelockflags.h"
|
||||
|
||||
static const struct DescribeFlags kNtLockFileFlags[] = {
|
||||
{kNtLockfileFailImmediately, "FailImmediately"}, //
|
||||
{kNtLockfileExclusiveLock, "ExclusiveLock"}, //
|
||||
};
|
||||
|
||||
const char *(DescribeNtLockFileFlags)(char buf[64], uint32_t x) {
|
||||
return DescribeFlags(buf, 64, kNtLockFileFlags, ARRAYLEN(kNtLockFileFlags),
|
||||
"kNtLockfile", x);
|
||||
}
|
50
libc/intrin/describentoverlapped.c
Normal file
50
libc/intrin/describentoverlapped.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/describentoverlapped.internal.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
|
||||
const char *(DescribeNtOverlapped)(char b[128], struct NtOverlapped *o) {
|
||||
int i = 0, n = 128;
|
||||
bool gotsome = false;
|
||||
if (!o) return "NULL";
|
||||
i += ksnprintf(b + i, MAX(0, n - i), "{");
|
||||
|
||||
if (o->hEvent) {
|
||||
if (gotsome) {
|
||||
i += ksnprintf(b + i, MAX(0, n - i), ", ");
|
||||
} else {
|
||||
gotsome = true;
|
||||
}
|
||||
i += ksnprintf(b + i, MAX(0, n - i), ".hEvent = %ld", o->hEvent);
|
||||
}
|
||||
|
||||
if (o->Pointer) {
|
||||
if (gotsome) {
|
||||
i += ksnprintf(b + i, MAX(0, n - i), ", ");
|
||||
} else {
|
||||
gotsome = true;
|
||||
}
|
||||
i += ksnprintf(b + i, MAX(0, n - i), ".Pointer = (void *)%p", o->Pointer);
|
||||
}
|
||||
|
||||
i += ksnprintf(b + i, MAX(0, n - i), "}");
|
||||
return b;
|
||||
}
|
13
libc/intrin/describentoverlapped.internal.h
Executable file
13
libc/intrin/describentoverlapped.internal.h
Executable file
|
@ -0,0 +1,13 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBENTOVERLAPPED_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBENTOVERLAPPED_INTERNAL_H_
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
const char *DescribeNtOverlapped(char[128], struct NtOverlapped *);
|
||||
#define DescribeNtOverlapped(x) DescribeNtOverlapped(alloca(128), x)
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBENTOVERLAPPED_INTERNAL_H_ */
|
50
libc/intrin/lockfileex.c
Normal file
50
libc/intrin/lockfileex.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/intrin/describentoverlapped.internal.h"
|
||||
#include "libc/nt/files.h"
|
||||
|
||||
__msabi extern typeof(LockFileEx) *const __imp_LockFileEx;
|
||||
|
||||
/**
|
||||
* Locks file on the New Technology.
|
||||
*
|
||||
* @return handle, or -1 on failure
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
bool32 LockFileEx(int64_t hFile, uint32_t dwFlags, uint32_t dwReserved,
|
||||
uint32_t nNumberOfBytesToLockLow,
|
||||
uint32_t nNumberOfBytesToLockHigh,
|
||||
struct NtOverlapped *lpOverlapped) {
|
||||
bool32 ok;
|
||||
STRACE("LockFileEx(%ld, %s, %#x, %'zu, %s) → ...", hFile,
|
||||
DescribeNtLockFileFlags(dwFlags), dwReserved,
|
||||
(uint64_t)nNumberOfBytesToLockHigh << 32 | nNumberOfBytesToLockLow,
|
||||
DescribeNtOverlapped(lpOverlapped));
|
||||
ok = __imp_LockFileEx(hFile, dwFlags, dwReserved, nNumberOfBytesToLockLow,
|
||||
nNumberOfBytesToLockHigh, lpOverlapped);
|
||||
if (!ok) __winerr();
|
||||
STRACE("LockFileEx(%ld, %s, %#x, %'zu, [%s]) → %hhhd% m", hFile,
|
||||
DescribeNtLockFileFlags(dwFlags), dwReserved,
|
||||
(uint64_t)nNumberOfBytesToLockHigh << 32 | nNumberOfBytesToLockLow,
|
||||
DescribeNtOverlapped(lpOverlapped), ok);
|
||||
return ok;
|
||||
}
|
45
libc/intrin/unlockfileex.c
Normal file
45
libc/intrin/unlockfileex.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/strace.internal.h"
|
||||
#include "libc/calls/syscall_support-nt.internal.h"
|
||||
#include "libc/intrin/describentoverlapped.internal.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/struct/overlapped.h"
|
||||
|
||||
__msabi extern typeof(UnlockFileEx) *const __imp_UnlockFileEx;
|
||||
|
||||
/**
|
||||
* Unlocks file on the New Technology.
|
||||
*
|
||||
* @return handle, or -1 on failure
|
||||
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
|
||||
*/
|
||||
bool32 UnlockFileEx(int64_t hFile, uint32_t dwReserved,
|
||||
uint32_t nNumberOfBytesToUnlockLow,
|
||||
uint32_t nNumberOfBytesToUnlockHigh,
|
||||
struct NtOverlapped *lpOverlapped) {
|
||||
bool32 ok;
|
||||
ok = __imp_UnlockFileEx(hFile, dwReserved, nNumberOfBytesToUnlockLow,
|
||||
nNumberOfBytesToUnlockHigh, lpOverlapped);
|
||||
if (!ok) __winerr();
|
||||
STRACE("UnlockFileEx(%ld, %#x, %'zu, %s) → %hhhd% m", hFile, dwReserved,
|
||||
(uint64_t)nNumberOfBytesToUnlockHigh << 32 | nNumberOfBytesToUnlockLow,
|
||||
DescribeNtOverlapped(lpOverlapped), ok);
|
||||
return ok;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue