diff --git a/CMakeLists.txt b/CMakeLists.txt index 6199c156..a7272ee7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -718,6 +718,11 @@ if(CORTEXT_EMBED_OBJSTORE) ${CMAKE_CURRENT_SOURCE_DIR}/third_party/sqlite-objstore/src/backend_sqlite.c ) endif() + if(WIN32) + list(APPEND CORTEXT_OBJSTORE_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/third_party/sqlite-objstore/src/win_dirent.c + ) + endif() if(CORTEXT_TARGET_AARCH64) list(APPEND CORTEXT_OBJSTORE_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/sqlite-objstore/third_party/blake3/blake3_neon.c diff --git a/third_party/sqlite-objstore/src/CMakeLists.txt b/third_party/sqlite-objstore/src/CMakeLists.txt index 12b126e3..fb72fb55 100644 --- a/third_party/sqlite-objstore/src/CMakeLists.txt +++ b/third_party/sqlite-objstore/src/CMakeLists.txt @@ -79,6 +79,10 @@ if(OBJSTORE_BACKEND_VFS) list(APPEND OBJSTORE_SOURCES backend_vfs.c) endif() +if(WIN32) + list(APPEND OBJSTORE_SOURCES win_dirent.c) +endif() + function(_objstore_configure_library target) target_include_directories(${target} PUBLIC diff --git a/third_party/sqlite-objstore/src/backend_file.c b/third_party/sqlite-objstore/src/backend_file.c index fa6da85a..f1fefd05 100644 --- a/third_party/sqlite-objstore/src/backend_file.c +++ b/third_party/sqlite-objstore/src/backend_file.c @@ -13,7 +13,11 @@ #include "backend_fs_common.h" +#if defined(_WIN32) +#include "win_dirent.h" +#else #include +#endif #include #include #include diff --git a/third_party/sqlite-objstore/src/backend_fs_common.c b/third_party/sqlite-objstore/src/backend_fs_common.c index a4260032..06eb0c75 100644 --- a/third_party/sqlite-objstore/src/backend_fs_common.c +++ b/third_party/sqlite-objstore/src/backend_fs_common.c @@ -1,6 +1,10 @@ #include "backend_fs_common.h" +#if defined(_WIN32) +#include "win_dirent.h" +#else #include +#endif #include #include #include diff --git a/third_party/sqlite-objstore/src/backend_portable.c b/third_party/sqlite-objstore/src/backend_portable.c index 254fa888..754bb13f 100644 --- a/third_party/sqlite-objstore/src/backend_portable.c +++ b/third_party/sqlite-objstore/src/backend_portable.c @@ -1,6 +1,10 @@ #include "backend_portable.h" +#if defined(_WIN32) +#include "win_dirent.h" +#else #include +#endif #include #include #include diff --git a/third_party/sqlite-objstore/src/win_dirent.c b/third_party/sqlite-objstore/src/win_dirent.c new file mode 100644 index 00000000..00873752 --- /dev/null +++ b/third_party/sqlite-objstore/src/win_dirent.c @@ -0,0 +1,85 @@ +#include "win_dirent.h" + +#include +#include +#include + +#define WIN32_LEAN_AND_MEAN 1 +#include + +DIR * +opendir (const char *path) +{ + DIR *dir; + WIN32_FIND_DATAA find_data; + HANDLE handle; + + if (path == NULL) + { + errno = ENOENT; + return NULL; + } + + dir = (DIR *) malloc (sizeof (DIR)); + if (dir == NULL) + { + errno = ENOMEM; + return NULL; + } + + handle = FindFirstFileA (path, &find_data); + if (handle == INVALID_HANDLE_VALUE) + { + free (dir); + errno = ENOENT; + return NULL; + } + + dir->handle = (void *) handle; + memcpy (dir->entry.d_name, find_data.cFileName, + sizeof (find_data.cFileName)); + dir->entry_valid = 1; + return dir; +} + +struct dirent * +readdir (DIR *dir) +{ + WIN32_FIND_DATAA find_data; + + if (dir == NULL) + { + errno = EBADF; + return NULL; + } + + if (dir->entry_valid) + { + dir->entry_valid = 0; + return &dir->entry; + } + + if (!FindNextFileA ((HANDLE) dir->handle, &find_data)) + { + return NULL; + } + + memcpy (dir->entry.d_name, find_data.cFileName, + sizeof (find_data.cFileName)); + dir->entry_valid = 1; + return &dir->entry; +} + +int +closedir (DIR *dir) +{ + if (dir == NULL) + { + errno = EBADF; + return -1; + } + + FindClose ((HANDLE) dir->handle); + free (dir); + return 0; +} diff --git a/third_party/sqlite-objstore/src/win_dirent.h b/third_party/sqlite-objstore/src/win_dirent.h new file mode 100644 index 00000000..252d4dfc --- /dev/null +++ b/third_party/sqlite-objstore/src/win_dirent.h @@ -0,0 +1,36 @@ +#ifndef OBJSTORE_WIN_DIRENT_H +#define OBJSTORE_WIN_DIRENT_H + +/* + * Minimal POSIX dirent subset for Windows (MSVC and clang-cl). + * + * sqlite-objstore only needs opendir(), readdir(), closedir(), and + * d_name; the system does not exist on Windows. On other + * platforms the real is used instead. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +struct dirent +{ + char d_name[260]; +}; + +typedef struct +{ + void *handle; + int entry_valid; + struct dirent entry; +} DIR; + +DIR *opendir (const char *path); +struct dirent *readdir (DIR *dir); +int closedir (DIR *dir); + +#ifdef __cplusplus +} +#endif + +#endif /* OBJSTORE_WIN_DIRENT_H */