Skip to content
Open
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
2 changes: 1 addition & 1 deletion scripts/memory-core-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ src/daemon/application.c 107
src/daemon/bootstrap.c 13
src/daemon/daemon.c 31
src/daemon/frontend.c 12
src/daemon/ipc.c 146
src/daemon/ipc.c 144
src/daemon/project_lock.c 6
src/daemon/runtime.c 45
src/daemon/service.c 6
Expand Down
10 changes: 7 additions & 3 deletions src/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -7902,14 +7902,18 @@ cbm_config_t *cbm_config_open(const char *cache_dir) {
return NULL;
}

char dbpath[CLI_BUF_1K];
snprintf(dbpath, sizeof(dbpath), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);
char dbpath[4096];
int dbpath_length = snprintf(dbpath, sizeof(dbpath), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);
if (dbpath_length < 0 || (size_t)dbpath_length >= sizeof(dbpath)) {
return NULL;
}

/* Ensure directory exists */
mkdirp(cache_dir, DIR_PERMS);

sqlite3 *db = NULL;
if (sqlite3_open(dbpath, &db) != SQLITE_OK) {
if (cbm_runtime_settings_open_db(dbpath, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) !=
SQLITE_OK) {
if (db) {
sqlite3_close(db);
}
Expand Down
28 changes: 25 additions & 3 deletions src/cli/runtime_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "foundation/platform.h"
#include "foundation/log.h"
#include "foundation/compat_fs.h"
#ifdef _WIN32
#include "foundation/win_utf8.h"
#endif
#include "daemon/ipc.h"
#include "ui/config.h"
#include <sqlite3.h>
Expand Down Expand Up @@ -241,6 +244,24 @@ static bool canonical_cache_path(const char *cache, char out[SETTINGS_PATH_CAP],
return cbm_daemon_ipc_private_directory_secure(out);
}

int cbm_runtime_settings_open_db(const char *path, sqlite3 **db, int flags) {
#ifdef _WIN32
*db = NULL;
wchar_t *wide = cbm_path_to_wide_in(CBM_MEM_CLASS_OTHER, path);
if (!wide)
return SQLITE_NOMEM;
char extended[SETTINGS_PATH_CAP + 8];
int converted = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide, -1, extended,
sizeof(extended), NULL, NULL);
cbm_free(CBM_MEM_CLASS_OTHER, wide);
if (!converted)
return SQLITE_CANTOPEN;
return sqlite3_open_v2(extended, db, flags, "win32-longpath");
#else
return sqlite3_open_v2(path, db, flags, NULL);
#endif
}

static sqlite3 *settings_open(const char *cache) {
if (!cache || !cache[0] || strlen(cache) + 12 >= SETTINGS_PATH_CAP)
return NULL;
Expand All @@ -255,7 +276,8 @@ static sqlite3 *settings_open(const char *cache) {
if (snprintf(path, sizeof(path), "%s/_config.db", canonical) >= (int)sizeof(path))
return NULL;
sqlite3 *db = NULL;
if (sqlite3_open(path, &db) != SQLITE_OK) {
if (cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) !=
SQLITE_OK) {
if (db)
sqlite3_close(db);
return NULL;
Expand Down Expand Up @@ -477,7 +499,7 @@ void cbm_runtime_settings_reset_for_tests(void) {
* progress; a live writer takes the busy path instead. */
static bool settings_recover_hot_journal(const char *path) {
sqlite3 *db = NULL;
bool recovered = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK;
bool recovered = cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READWRITE) == SQLITE_OK;
if (recovered) {
cbm_runtime_settings_wait_for_writers(db);
recovered =
Expand Down Expand Up @@ -505,7 +527,7 @@ static bool settings_recover_hot_journal(const char *path) {
static sqlite3 *settings_open_snapshot(const char *path, bool *hot_journal) {
*hot_journal = false;
sqlite3 *db = NULL;
if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
if (cbm_runtime_settings_open_db(path, &db, SQLITE_OPEN_READONLY) != SQLITE_OK) {
if (db)
sqlite3_close(db);
return NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/cli/runtime_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void cbm_runtime_settings_note_http_port(int port);
* failing on its lock. Logs config_db.busy_wait once per busy wait. */
struct sqlite3;
void cbm_runtime_settings_wait_for_writers(struct sqlite3 *db);
/* Open the configuration database with a Windows long-path capable VFS. */
int cbm_runtime_settings_open_db(const char *path, struct sqlite3 **db, int flags);

#ifdef CBM_ENABLE_TEST_SEAMS
/* Single-threaded test teardown only; never exposed in production builds. */
Expand Down
1 change: 1 addition & 0 deletions src/daemon/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) {
cbm_daemon_runtime_service_config_t runtime_config = {
.endpoint = config->endpoint,
.identity = config->identity,
.cache_directory = cbm_resolve_cache_dir(),
.conflict_log_path = conflict_log,
.conflict_log_cap_bytes = HOST_CONFLICT_LOG_CAP,
.max_clients = HOST_MAX_CLIENTS,
Expand Down
25 changes: 19 additions & 6 deletions src/daemon/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
#include "foundation/compat_thread.h"
#include "foundation/log.h"
#include "foundation/macos_acl.h"
#include "foundation/mem_core.h"
#include "foundation/platform.h"
#include "foundation/private_file_lock_internal.h"
#include "foundation/sha256.h"
#include "foundation/secure_random.h"
#ifdef _WIN32
#include "foundation/win_utf8.h"
#endif

#include <errno.h>
#include <limits.h>
Expand Down Expand Up @@ -5425,22 +5429,31 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) {
* absolute drive path. */
return false;
}
wchar_t *path = wide_copy(directory_path);
/* Prefix the already validated drive-absolute spelling without resolving
* dot components: the component walk must still reject those itself. */
bool extended = length >= 240U;
size_t prefix_length = extended ? 4U : 0U;
wchar_t *path = cbm_calloc(CBM_MEM_CLASS_OTHER, (length + prefix_length + 1U) * sizeof(*path));
if (!path) {
return false;
}
if (extended) {
wmemcpy(path, L"\\\\?\\", prefix_length);
}
wmemcpy(path + prefix_length, directory_path, length + 1U);
length = wcslen(path);
for (size_t i = 0; i < length; i++) {
if (path[i] == L'/') {
path[i] = L'\\';
}
}
win_security_t security;
if (!win_security_init(&security)) {
free(path);
cbm_free(CBM_MEM_CLASS_OTHER, path);
return false;
}
bool ok = true;
size_t component_start = 3;
size_t component_start = 3U + prefix_length;
for (size_t i = component_start; ok && i <= length; i++) {
if (i < length && path[i] != L'\\') {
continue;
Expand Down Expand Up @@ -5509,7 +5522,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) {
if (ok) {
ok = win_runtime_directory_secure(path);
}
free(path);
cbm_free(CBM_MEM_CLASS_OTHER, path);
return ok;
}

Expand Down Expand Up @@ -5577,8 +5590,8 @@ FILE *cbm_daemon_ipc_private_log_open(const char *directory_path, const char *ba
wchar_t *wide_directory = utf8_to_wide(directory_path);
char *path = string_format("%s/%s", directory_path, base_name);
char *rotated_path = path ? string_format("%s.1", path) : NULL;
wchar_t *wide_path = path ? utf8_to_wide(path) : NULL;
wchar_t *wide_rotated = rotated_path ? utf8_to_wide(rotated_path) : NULL;
wchar_t *wide_path = path ? cbm_path_to_wide(path) : NULL;
wchar_t *wide_rotated = rotated_path ? cbm_path_to_wide(rotated_path) : NULL;
if (!wide_directory || !path || !rotated_path || !wide_path || !wide_rotated ||
!win_private_directory_tree_secure(wide_directory)) {
free(wide_directory);
Expand Down
Loading
Loading