fix(sqlite-objstore): provide win32 unistd/fcntl shim for MSVC builds - #23
Merged
Conversation
There was a problem hiding this comment.
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) providingO_*aliases,open/read/write/close,fsync, plusflock/ftruncateimplementations for MSVC/UCRT environments. - Swap
<unistd.h>/<sys/file.h>includes towin_posix.hunder_WIN32in the affected objstore backends. - Include
win_posix.cin 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_NBpath is not actually non-blocking:LockFileExblocks unlessLOCKFILE_FAIL_IMMEDIATELYis set. Also, likeUnlockFileEx, it requires a non-nullOVERLAPPED*. This means a caller usingLOCK_NBcould 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.