Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ if(CORTEXT_EMBED_OBJSTORE)
if(WIN32)
list(APPEND CORTEXT_OBJSTORE_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/third_party/sqlite-objstore/src/win_dirent.c
${CMAKE_CURRENT_SOURCE_DIR}/third_party/sqlite-objstore/src/win_posix.c
)
endif()
if(CORTEXT_TARGET_AARCH64)
Expand Down
2 changes: 1 addition & 1 deletion third_party/sqlite-objstore/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ if(OBJSTORE_BACKEND_VFS)
endif()

if(WIN32)
list(APPEND OBJSTORE_SOURCES win_dirent.c)
list(APPEND OBJSTORE_SOURCES win_dirent.c win_posix.c)
endif()

function(_objstore_configure_library target)
Expand Down
4 changes: 3 additions & 1 deletion third_party/sqlite-objstore/src/backend_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN32)
#include "win_posix.h"
#else
#include <unistd.h>
#if !defined(_WIN32)
#include <sys/file.h>
#endif
#if defined(__linux__)
Expand Down
4 changes: 4 additions & 0 deletions third_party/sqlite-objstore/src/backend_portable.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN32)
#include "win_posix.h"
#else
#include <unistd.h>
#endif

#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
Expand Down
63 changes: 63 additions & 0 deletions third_party/sqlite-objstore/src/win_posix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "win_posix.h"

#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>

int
flock (int fd, int how)
{
HANDLE handle;
BOOL exclusive = (how & LOCK_EX) != 0;

handle = (HANDLE) _get_osfhandle (fd);
if (handle == INVALID_HANDLE_VALUE)
{
errno = EBADF;
return -1;
}

if (how & LOCK_UN)
{
if (UnlockFileEx (handle, 0, 1, 0, (LPOVERLAPPED) 0) != 0)
{
return 0;
}
errno = (GetLastError () == ERROR_NOT_LOCKED) ? EINVAL : EIO;
return -1;
}
Comment on lines +19 to +27

/*
* LockFileEx is always non-blocking; the POSIX callers of this shim use
* both modes (LOCK_NB for the primary probe, blocking waits while the
* primary finishes), so emulate the blocking form with a short retry
* loop on the dedicated lock file.
*/
for (;;)
{
if (LockFileEx (handle, exclusive ? LOCKFILE_EXCLUSIVE_LOCK : 0,
0, 1, 0, (LPOVERLAPPED) 0) != 0)
{
return 0;
}
if (how & LOCK_NB)
{
errno = (GetLastError () == ERROR_LOCK_VIOLATION) ? EACCES : EIO;
return -1;
}
Sleep (10);
}
}

int
ftruncate (int fd, off_t length)
{
errno_t err;

err = _chsize_s (fd, length);
if (err != 0)
{
errno = (int) err;
return -1;
}
return 0;
}
66 changes: 66 additions & 0 deletions third_party/sqlite-objstore/src/win_posix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef OBJSTORE_WIN_POSIX_H
#define OBJSTORE_WIN_POSIX_H

/*
* Minimal POSIX unistd/fcntl/sys-file subset for Windows (MSVC and clang-cl).
*
* The objstore file backends use open/read/write/close, fsync, ftruncate,
* flock, the O_* open flags, and a few unistd functions. UCRT provides the
* underscore-prefixed primitives (_open, _read, _write, _close, _commit,
* _chsize_s, _get_osfhandle) and the prefixed _O_* flags, but not the bare
* POSIX names or the un-prefixed O_* aliases in plain C mode. On other
* platforms the real <unistd.h> and <sys/file.h> are used instead.
*
* Every definition is guarded so this header also compiles under MinGW,
* where the bare names already exist.
*/

#define WIN32_LEAN_AND_MEAN 1
#include <direct.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
Comment on lines +18 to +22
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifndef O_RDONLY
#define O_RDONLY _O_RDONLY
#define O_WRONLY _O_WRONLY
#define O_RDWR _O_RDWR
#define O_CREAT _O_CREAT
#define O_TRUNC _O_TRUNC
#define O_EXCL _O_EXCL
#define O_APPEND _O_APPEND
#endif
#ifndef O_CLOEXEC
#define O_CLOEXEC _O_NOINHERIT
#endif
#ifndef open
#define open _open
#define read _read
#define write _write
#define close _close
#endif
#ifndef fsync
#define fsync(fd) _commit(fd)
#endif
#ifndef LOCK_SH
#define LOCK_SH 1
#define LOCK_EX 2
#define LOCK_NB 4
#define LOCK_UN 8
#endif

#ifdef __cplusplus
extern "C" {
#endif

int flock (int fd, int how);
int ftruncate (int fd, off_t length);

#ifdef __cplusplus
}
#endif

#endif /* OBJSTORE_WIN_POSIX_H */
Loading