Pacify file locks on Windows

This change gets redbean SQLite working in write mode on Windows.
Warnings have been added to the appropriate and responsible places.
Hacking proprietary PC systems into production-worthy servers isn't
terribly high on the list of priorities. Consider BSD or Linux when
building online systems that service requests from multiple people.

Fixes #193
This commit is contained in:
Justine Tunney 2021-06-12 00:01:55 -07:00
parent 9504ebaf7e
commit 8d7d00af3a
6 changed files with 146 additions and 17 deletions

View file

@ -19,42 +19,47 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/flock.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filelockflags.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/nt/enum/formatmessageflags.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/byhandlefileinformation.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/synchronization.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
static textwindows int sys_fcntl_nt_lock(struct Fd *f, int cmd, uintptr_t arg) {
uint32_t flags;
struct flock *l;
uint32_t flags, err;
struct NtOverlapped ov;
int64_t pos, off, len, size;
struct NtByHandleFileInformation info;
if (!GetFileInformationByHandle(f->handle, &info)) return __winerr();
if (!SetFilePointerEx(f->handle, 0, &pos, SEEK_CUR)) return __winerr();
l = (struct flock *)arg;
len = l->l_len;
off = l->l_start;
if (!len || l->l_whence == SEEK_END) {
if (!GetFileInformationByHandle(f->handle, &info)) return __winerr();
size = (uint64_t)info.nFileSizeHigh << 32 | info.nFileSizeLow;
} else {
size = 0;
}
if (l->l_whence != SEEK_SET) {
if (l->l_whence == SEEK_CUR) {
if (!SetFilePointerEx(f->handle, 0, &pos, SEEK_CUR)) return __winerr();
size = (uint64_t)info.nFileSizeHigh << 32 | info.nFileSizeLow;
switch (l->l_whence) {
case SEEK_SET:
break;
case SEEK_CUR:
off = pos + off;
} else if (l->l_whence == SEEK_END) {
break;
case SEEK_END:
off = size - off;
} else {
break;
default:
return einval();
}
}
if (!len) len = size - off;
if (off < 0 || len < 0) return einval();
@ -62,17 +67,27 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int cmd, uintptr_t arg) {
if (l->l_type == F_RDLCK || l->l_type == F_WRLCK) {
flags = 0;
if (cmd == F_SETLK) flags |= kNtLockfileFailImmediately;
if (l->l_type == F_WRLCK) flags |= kNtLockfileExclusiveLock;
/* TODO: How can we make SQLite locks on Windows to work? */
/* if (l->l_type == F_WRLCK) flags |= kNtLockfileExclusiveLock; */
if (LockFileEx(f->handle, flags, 0, len, len >> 32, &ov)) {
return 0;
} else {
return __winerr();
err = GetLastError();
if (err == kNtErrorLockViolation) err = EAGAIN;
errno = err;
return -1;
}
} else if (l->l_type == F_UNLCK) {
if (UnlockFileEx(f->handle, 0, len, len >> 32, &ov)) {
return 0;
} else {
return __winerr();
err = GetLastError();
if (err == kNtErrorNotLocked) {
return 0;
} else {
errno = err;
return -1;
}
}
} else {
return einval();

View file

@ -31,6 +31,9 @@
* // ...
* CHECK_NE(-1, fcntl(zfd, F_SETLK, &(struct flock){F_UNLCK}));
*
* Please be warned that locks currently do nothing on Windows since
* figuring out how to polyfill them correctly is a work in progress.
*
* @param cmd can be F_{GET,SET}{FD,FL}, etc.
* @param arg can be FD_CLOEXEC, etc. depending
* @return 0 on success, or -1 w/ errno

View file

@ -0,0 +1,41 @@
/*-*- 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 2021 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/testlib/testlib.h"
#include "third_party/sqlite3/sqlite3.h"
char testlib_enable_tmp_setup_teardown;
void SetUp(void) {
sqlite3_initialize();
}
TEST(sqlite, test) {
sqlite3 *db;
sqlite3_stmt *stmt;
ASSERT_EQ(SQLITE_OK, sqlite3_open("foo.sqlite", &db));
ASSERT_EQ(SQLITE_OK,
sqlite3_prepare_v2(db, "PRAGMA synchronous=0", -1, &stmt, 0));
ASSERT_EQ(SQLITE_DONE, sqlite3_step(stmt));
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt));
ASSERT_EQ(SQLITE_OK,
sqlite3_prepare_v2(db, "CREATE TABLE t (x INTEGER)", -1, &stmt, 0));
ASSERT_EQ(SQLITE_DONE, sqlite3_step(stmt));
ASSERT_EQ(SQLITE_OK, sqlite3_finalize(stmt));
ASSERT_EQ(SQLITE_OK, sqlite3_close(db));
}

View file

@ -43,7 +43,8 @@ TEST_TOOL_NET_DIRECTDEPS = \
LIBC_TESTLIB \
LIBC_X \
LIBC_ZIPOS \
THIRD_PARTY_REGEX
THIRD_PARTY_REGEX \
THIRD_PARTY_SQLITE3
TEST_TOOL_NET_DEPS := \
$(call uniq,$(foreach x,$(TEST_TOOL_NET_DIRECTDEPS),$($(x))))

View file

@ -1,5 +1,17 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
#
# OVERVIEW
#
# SQLite Embedded Database
#
# NOTES
#
# Please be warned that locks currently do nothing on Windows since
# figuring out how to polyfill them correctly is a work in progress
# Further note we currently don't do that thing SQLite does for Mac
# file locks so your dbase will only be as reliable as Apple wanted
# it to be when they wrote their POSIX file locking implementation.
PKGS += THIRD_PARTY_SQLITE3
@ -57,6 +69,7 @@ o/$(MODE)/third_party/sqlite3/sqlite3.com.dbg: \
$(THIRD_PARTY_SQLITE3_A_DEPS) \
$(THIRD_PARTY_SQLITE3_SHELL_OBJS) \
o/$(MODE)/third_party/sqlite3/shell.shell.o \
o/$(MODE)/third_party/sqlite3/shell.pkg \
$(CRT) \
$(APE)
-@$(APELINK)
@ -70,6 +83,10 @@ $(THIRD_PARTY_SQLITE3_A).pkg: \
$(THIRD_PARTY_SQLITE3_A_OBJS) \
$(foreach x,$(THIRD_PARTY_SQLITE3_A_DIRECTDEPS),$($(x)_A).pkg)
o/$(MODE)/third_party/sqlite3/shell.pkg: \
$(THIRD_PARTY_SQLITE3_SHELL_OBJS) \
$(foreach x,$(THIRD_PARTY_SQLITE3_A_DIRECTDEPS),$($(x)_A).pkg)
# https://www.sqlite.org/compile.html
THIRD_PARTY_SQLITE3_FLAGS = \
-DNDEBUG \
@ -87,6 +104,7 @@ THIRD_PARTY_SQLITE3_FLAGS = \
-DSQLITE_DEFAULT_MEMSTATUS=0 \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
-DSQLITE_OMIT_UTF16 \
-DSQLITE_OMIT_TCL_VARIABLE \
-DSQLITE_OMIT_LOAD_EXTENSION \
-DSQLITE_OMIT_DEPRECATED \
@ -143,6 +161,7 @@ o/$(MODE)/%.shell.o: %.c
THIRD_PARTY_SQLITE3_LIBS = $(foreach x,$(THIRD_PARTY_SQLITE3_ARTIFACTS),$($(x)))
THIRD_PARTY_SQLITE3_SRCS = $(foreach x,$(THIRD_PARTY_SQLITE3_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_SQLITE3_HDRS = $(foreach x,$(THIRD_PARTY_SQLITE3_ARTIFACTS),$($(x)_HDRS))
THIRD_PARTY_SQLITE3_CHECKS = $(foreach x,$(THIRD_PARTY_SQLITE3_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_SQLITE3_OBJS = $(foreach x,$(THIRD_PARTY_SQLITE3_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_SQLITE3_OBJS): third_party/sqlite3/sqlite3.mk

50
tool/viz/float2bin.c Normal file
View 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 2021 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/stdio/stdio.h"
#include "third_party/gdtoa/gdtoa.h"
const char kDoubleBits[] = "\
sign\n\
exponent\n\
fraction\n\
\n\
\n";
const char kLongDoubleBits[] = "\
sign\n\
exponent\n\
intpart\n\
fraction\n\
\n\
\n";
int main(int argc, char *argv[]) {
int i;
union {
double f;
uint64_t i;
} u;
if (argc <= 1) return 1;
fputs(kDoubleBits, stdout);
for (i = 1; i < argc; ++i) {
u.f = strtod(argv[i], 0);
printf("%064lb %.15g\n", u.i, u.f);
}
return 0;
}