Skip to content

fix(sqlite-objstore): provide win32 unistd/fcntl shim for MSVC builds - #23

Merged
gabewillen merged 1 commit into
mainfrom
fix/sqlite-objstore-win32-posix
Aug 18, 2026
Merged

fix(sqlite-objstore): provide win32 unistd/fcntl shim for MSVC builds#23
gabewillen merged 1 commit into
mainfrom
fix/sqlite-objstore-win32-posix

Conversation

@gabewillen

Copy link
Copy Markdown
Contributor

Round 2 of the Windows port for the npm release of @augmem/cortext v1.3.3
(follow-up to PR #22, the dirent shim). After the dirent fix, both Windows
matrix jobs failed with:

backend_portable.c(17,10): error C1083: Cannot open include file: 'unistd.h'
backend_file.c(31,10): error C1083: Cannot open include file: 'unistd.h'

Audit of the real UCRT headers (not MinGW) shows MSVC/UCRT in plain C mode
provides _open/_read/_write/_close/_commit/_chsize_s/get_osfhandle,
unlink/rename/stat, and the prefixed O* flags - but not the bare POSIX
names, the un-prefixed O
* aliases (gated on _CRT_DECLARE_NONSTDC_NAMES,
which is 0 in C), O_CLOEXEC, fsync, ftruncate, or flock.

This adds win_posix.h/.c (guarded so MinGW also compiles): O_* aliases,
O_CLOEXEC -> _O_NOINHERIT, open/read/write/close -> UCRT underscore
primitives, fsync -> _commit, ftruncate via _chsize_s, and flock emulated
with LockFileEx/UnlockFileEx on the 1-byte range at offset 0 (LOCK_NB ->
non-blocking with EACCES on ERROR_LOCK_VIOLATION; blocking form via a 10 ms
retry loop). The three affected files swap unistd.h / sys/file.h for the
shim under a _WIN32 include guard. Note: the flock-based recovery protocol
in backend_file.c is already #if !defined(_WIN32) (Windows runs recovery
unconditionally, as in the published v1.2.4 win32 addons), so flock is
defensive here.

Verified: 10/10 object cross-compiles (win_posix, win_dirent,
backend_fs_common, backend_portable, backend_file) for both
x86_64-windows-gnu and aarch64-windows-gnu via zig, -pedantic clean, shim
exports flock/ftruncate; native macOS arm64 objstore suite 4/4 ctest,
80/80 unit tests, warnings-as-errors clean.

Copilot AI lite review requested due to automatic review settings August 17, 2026 23:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the Windows (MSVC/clang-cl) portability work for the embedded sqlite-objstore backend by replacing missing POSIX headers (unistd.h, sys/file.h) with a Win32/UCRT-compatible shim and wiring that shim into the build so the Windows npm release matrix can compile.

Changes:

  • Add a Win32 “POSIX shim” (win_posix.h/.c) providing O_* aliases, open/read/write/close, fsync, plus flock/ftruncate implementations for MSVC/UCRT environments.
  • Swap <unistd.h> / <sys/file.h> includes to win_posix.h under _WIN32 in the affected objstore backends.
  • Include win_posix.c in both the vendored objstore sub-build and the top-level embedded source list on Windows.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
third_party/sqlite-objstore/src/win_posix.h Introduces the Windows/UCRT POSIX-compatibility header for objstore backends.
third_party/sqlite-objstore/src/win_posix.c Implements flock() (Win32 locking) and ftruncate() (via _chsize_s) for Windows builds.
third_party/sqlite-objstore/src/CMakeLists.txt Adds win_posix.c to objstore sources when building on Windows.
third_party/sqlite-objstore/src/backend_portable.c Uses win_posix.h instead of <unistd.h> on _WIN32.
third_party/sqlite-objstore/src/backend_file.c Uses win_posix.h instead of <unistd.h>/<sys/file.h> on _WIN32.
CMakeLists.txt Adds win_posix.c to the embedded objstore source list for Windows builds.
Suppressed comments (1)

third_party/sqlite-objstore/src/win_posix.c:41

  • The LOCK_NB path is not actually non-blocking: LockFileEx blocks unless LOCKFILE_FAIL_IMMEDIATELY is set. Also, like UnlockFileEx, it requires a non-null OVERLAPPED*. This means a caller using LOCK_NB could hang instead of returning EWOULDBLOCK/EAGAIN.
      if (LockFileEx (handle, exclusive ? LOCKFILE_EXCLUSIVE_LOCK : 0,
                      0, 1, 0, (LPOVERLAPPED) 0) != 0)
        {
          return 0;
        }

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +19 to +27
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 +18 to +22
#define WIN32_LEAN_AND_MEAN 1
#include <direct.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
@gabewillen
gabewillen merged commit c40ebcb into main Aug 18, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants