Skip to content

Commit e07443b

Browse files
DeusDatadLo999
andcommitted
Add configurable database directory via CBM_CACHE_DIR env var
All hardcoded ~/.cache/codebase-memory-mcp paths now route through cbm_resolve_cache_dir() in platform.c. Priority: CBM_CACHE_DIR env var > default ~/.cache/codebase-memory-mcp. Added db_path_for_project() helper in http_server.c to eliminate 5 copies of the same path construction pattern. Fixes #154 Co-Authored-By: dLo999 <dLo999@users.noreply.github.com>
1 parent 54c0325 commit e07443b

File tree

9 files changed

+109
-130
lines changed

9 files changed

+109
-130
lines changed

src/cli/cli.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,7 @@ static const char *get_cache_dir(const char *home_dir) {
20942094
if (!home_dir) {
20952095
return NULL;
20962096
}
2097-
snprintf(buf, sizeof(buf), "%s/.cache/codebase-memory-mcp", home_dir);
2097+
snprintf(buf, sizeof(buf), "%s", cbm_resolve_cache_dir());
20982098
return buf;
20992099
}
21002100

@@ -2320,7 +2320,7 @@ int cbm_cmd_config(int argc, char **argv) {
23202320
}
23212321

23222322
char cache_dir[CLI_BUF_1K];
2323-
snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
2323+
snprintf(cache_dir, sizeof(cache_dir), "%s", cbm_resolve_cache_dir());
23242324

23252325
cbm_config_t *cfg = cbm_config_open(cache_dir);
23262326
if (!cfg) {

src/foundation/platform.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,22 @@ const char *cbm_app_local_dir(void) {
354354
return cbm_app_config_dir();
355355
#endif
356356
}
357+
358+
/* ── Cache directory ─────────────────────────────────────────── */
359+
360+
const char *cbm_resolve_cache_dir(void) {
361+
static char buf[CBM_SZ_1K];
362+
char tmp[CBM_SZ_256] = "";
363+
cbm_safe_getenv("CBM_CACHE_DIR", tmp, sizeof(tmp), NULL);
364+
if (tmp[0]) {
365+
snprintf(buf, sizeof(buf), "%s", tmp);
366+
cbm_normalize_path_sep(buf);
367+
return buf;
368+
}
369+
const char *home = cbm_get_home_dir();
370+
if (!home) {
371+
return NULL;
372+
}
373+
snprintf(buf, sizeof(buf), "%s/.cache/codebase-memory-mcp", home);
374+
return buf;
375+
}

src/foundation/platform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ const char *cbm_app_config_dir(void);
9393
* macOS/Linux: same as cbm_app_config_dir(). */
9494
const char *cbm_app_local_dir(void);
9595

96+
/* ── Cache directory ────────────────────────────────────────────── */
97+
98+
/* Resolve the database cache directory. All project indexes are stored here.
99+
* Priority: CBM_CACHE_DIR env var > ~/.cache/codebase-memory-mcp (default).
100+
* Returns static buffer or NULL if home is unavailable. */
101+
const char *cbm_resolve_cache_dir(void);
102+
96103
/* ── File system ───────────────────────────────────────────────── */
97104

98105
/* Check if a path exists. */

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ int main(int argc, char **argv) {
294294
const char *cfg_home = cbm_get_home_dir();
295295
cbm_config_t *runtime_config = NULL;
296296
if (cfg_home) {
297-
snprintf(config_dir, sizeof(config_dir), "%s/.cache/codebase-memory-mcp", cfg_home);
297+
snprintf(config_dir, sizeof(config_dir), "%s", cbm_resolve_cache_dir());
298298
runtime_config = cbm_config_open(config_dir);
299299
}
300300

src/mcp/mcp.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,14 +646,13 @@ bool cbm_mcp_server_has_cached_store(cbm_mcp_server_t *srv) {
646646

647647
/* ── Cache dir + project DB path helpers ───────────────────────── */
648648

649-
/* Returns the platform cache directory: ~/.cache/codebase-memory-mcp
650-
* Writes to buf, returns buf for convenience. */
649+
/* Returns the cache directory. Writes to buf, returns buf for convenience. */
651650
static const char *cache_dir(char *buf, size_t bufsz) {
652-
const char *home = cbm_get_home_dir();
653-
if (!home) {
654-
home = cbm_tmpdir();
651+
const char *dir = cbm_resolve_cache_dir();
652+
if (!dir) {
653+
dir = cbm_tmpdir();
655654
}
656-
snprintf(buf, bufsz, "%s/.cache/codebase-memory-mcp", home);
655+
snprintf(buf, bufsz, "%s", dir);
657656
return buf;
658657
}
659658

@@ -3162,7 +3161,7 @@ static void maybe_auto_index(cbm_mcp_server_t *srv) {
31623161
const char *home = cbm_get_home_dir();
31633162
if (home) {
31643163
char db_check[CBM_SZ_1K];
3165-
snprintf(db_check, sizeof(db_check), "%s/.cache/codebase-memory-mcp/%s.db", home,
3164+
snprintf(db_check, sizeof(db_check), "%s/%s.db", cbm_resolve_cache_dir(),
31663165
srv->session_project);
31673166
struct stat st;
31683167
if (stat(db_check, &st) == 0) {

src/pipeline/pipeline.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@ static char *resolve_db_path(const cbm_pipeline_t *p) {
166166
if (p->db_path) {
167167
snprintf(path, 1024, "%s", p->db_path);
168168
} else {
169-
const char *home = cbm_get_home_dir();
170-
if (!home) {
171-
home = cbm_tmpdir();
172-
}
173-
snprintf(path, 1024, "%s/.cache/codebase-memory-mcp/%s.db", home, p->project_name);
169+
snprintf(path, 1024, "%s/%s.db", cbm_resolve_cache_dir(), p->project_name);
174170
}
175171
return path;
176172
}
@@ -595,16 +591,15 @@ static int64_t stat_mtime_ns(const struct stat *fst) {
595591
static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *files, int file_count,
596592
struct timespec *t) {
597593
cbm_clock_gettime(CLOCK_MONOTONIC, t);
598-
const char *home = cbm_get_home_dir();
599594
char db_path[CBM_SZ_1K];
600595
if (p->db_path) {
601596
snprintf(db_path, sizeof(db_path), "%s", p->db_path);
602597
} else {
603-
if (!home) {
604-
home = cbm_tmpdir();
598+
const char *cdir = cbm_resolve_cache_dir();
599+
if (!cdir) {
600+
cdir = cbm_tmpdir();
605601
}
606-
snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home,
607-
p->project_name);
602+
snprintf(db_path, sizeof(db_path), "%s/%s.db", cdir, p->project_name);
608603
}
609604
char db_dir[CBM_SZ_1K];
610605
snprintf(db_dir, sizeof(db_dir), "%s", db_path);

src/store/store.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,12 @@ cbm_store_t *cbm_store_open(const char *project) {
508508
if (!project) {
509509
return NULL;
510510
}
511-
/* Build path: ~/.cache/codebase-memory-mcp/<project>.db */
512-
const char *home = cbm_get_home_dir();
513-
if (!home) {
514-
home = cbm_tmpdir();
511+
const char *cdir = cbm_resolve_cache_dir();
512+
if (!cdir) {
513+
cdir = cbm_tmpdir();
515514
}
516515
char path[CBM_SZ_1K];
517-
snprintf(path, sizeof(path), "%s/.cache/codebase-memory-mcp/%s.db", home, project);
516+
snprintf(path, sizeof(path), "%s/%s.db", cdir, project);
518517
return store_open_internal(path, false);
519518
}
520519

@@ -2121,11 +2120,12 @@ int cbm_store_search(cbm_store_t *s, const cbm_search_params_t *params, cbm_sear
21212120
char count_sql[CBM_SZ_4K];
21222121
int bind_idx = 0;
21232122

2124-
const char *select_cols =
2125-
"SELECT n.id, n.project, n.label, n.name, n.qualified_name, "
2126-
"n.file_path, n.start_line, n.end_line, n.properties, "
2127-
"(SELECT COUNT(*) FROM edges e WHERE e.target_id = n.id AND e.type = 'CALLS') AS in_deg, "
2128-
"(SELECT COUNT(*) FROM edges e WHERE e.source_id = n.id AND e.type = 'CALLS') AS out_deg ";
2123+
const char *select_cols = "SELECT n.id, n.project, n.label, n.name, n.qualified_name, "
2124+
"n.file_path, n.start_line, n.end_line, n.properties, "
2125+
"(SELECT COUNT(*) FROM edges e WHERE e.target_id = n.id AND "
2126+
"e.type = 'CALLS') AS in_deg, "
2127+
"(SELECT COUNT(*) FROM edges e WHERE e.source_id = n.id AND "
2128+
"e.type = 'CALLS') AS out_deg ";
21292129

21302130
char where[CBM_SZ_2K] = "";
21312131
search_bind_t binds[ST_SEARCH_MAX_BINDS];
@@ -4324,8 +4324,8 @@ int cbm_store_adr_store(cbm_store_t *s, const char *project, const char *content
43244324
}
43254325

43264326
int cbm_store_adr_get(cbm_store_t *s, const char *project, cbm_adr_t *out) {
4327-
const char *sql =
4328-
"SELECT project, summary, created_at, updated_at FROM project_summaries WHERE project=?1";
4327+
const char *sql = "SELECT project, summary, created_at, updated_at FROM project_summaries "
4328+
"WHERE project=?1";
43294329
sqlite3_stmt *stmt = NULL;
43304330
if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
43314331
store_set_error_sqlite(s, "adr_get");

src/ui/config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
/* ── Path ────────────────────────────────────────────────────── */
2222

2323
void cbm_ui_config_path(char *buf, int bufsz) {
24-
const char *home = cbm_get_home_dir();
25-
if (!home) {
26-
home = cbm_tmpdir();
24+
const char *dir = cbm_resolve_cache_dir();
25+
if (!dir) {
26+
dir = cbm_tmpdir();
2727
}
28-
snprintf(buf, (size_t)bufsz, "%s/.cache/codebase-memory-mcp/config.json", home);
28+
snprintf(buf, (size_t)bufsz, "%s/config.json", dir);
2929
}
3030

3131
/* ── Load ────────────────────────────────────────────────────── */

0 commit comments

Comments
 (0)