Skip to content

Commit 4601532

Browse files
committed
fix(cli): never treat internal cache stores as project indexes
The cache directory holds two internal SQLite stores next to the project indexes: the user's settings (_config.db) and the cross-repo store (_cross_repo.db). cbm_list_indexes, cbm_remove_indexes and count_db_indexes matched every "*.db" there. As a result: - install --reset-indexes and uninstall (when index deletion is confirmed) deleted the user's config (every `config set` value silently reverted to its default) and the cross-repo store along with the indexes; - the "Found N index(es)" listing and count over-reported by up to two. pass_cross_repo.c already excluded both stores by exact filename, but in its own copy of the check. Centralise the rule in foundation/str_util: cbm_is_internal_cache_db() (exact names, so a real project whose name starts with "_" or contains "config" is still an index) and cbm_is_project_index_db(). Every enumeration of the cache directory's .db files (list, count, remove, cross-repo target scan) now calls it, and cbm_config_open() builds its path from the same CBM_CONFIG_DB_FILENAME constant. Proof on a real isolated cache (one indexed Go repo, `config set auto_index true`, a _cross_repo.db present): before: "Found 3 index(es)", "Removed 3 index(es)", cache left empty, `config get auto_index` -> false after: "Found 1 index(es)", "Removed 1 index(es)", _config.db and _cross_repo.db kept, `config get auto_index` -> true Test: cli::cli_index_enumeration_skips_internal_cache_dbs (RED on main, green with the fix, RED again on revert). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9d65f4c commit 4601532

6 files changed

Lines changed: 113 additions & 17 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "foundation/constants.h"
2424
#include "foundation/log.h"
2525
#include "foundation/sha256.h"
26+
#include "foundation/str_util.h"
2627
#include "cli/client_adapter.h"
2728
#include "mcp/mcp.h" // cbm_mcp_tool_input_schema — CLI flag parser + per-tool --help
2829
#include "mcp/index_supervisor.h"
@@ -62,7 +63,6 @@ enum {
6263
SQL_PARAM_1 = 1, /* sqlite3_bind parameter index 1 */
6364
SQL_PARAM_2 = 2,
6465
SEMVER_PARTS = 3, /* major.minor.patch */
65-
DB_EXT_LEN = 3, /* strlen(".db") */
6666
MIN_ARGC_CMD = 3,
6767
/* minimum argc for subcommand with arg */ /* sqlite3_bind parameter index 2 */ /* 10 MB cap
6868
factor */
@@ -7207,8 +7207,7 @@ int cbm_list_indexes(const char *home_dir) {
72077207
int count = 0;
72087208
cbm_dirent_t *ent;
72097209
while ((ent = cbm_readdir(d)) != NULL) {
7210-
size_t len = strlen(ent->name);
7211-
if (len > DB_EXT_LEN && strcmp(ent->name + len - DB_EXT_LEN, ".db") == 0) {
7210+
if (cbm_is_project_index_db(ent->name)) {
72127211
printf(" %s/%s\n", cache_dir, ent->name);
72137212
count++;
72147213
}
@@ -7231,8 +7230,7 @@ int cbm_remove_indexes(const char *home_dir) {
72317230
int count = 0;
72327231
cbm_dirent_t *ent;
72337232
while ((ent = cbm_readdir(d)) != NULL) {
7234-
size_t len = strlen(ent->name);
7235-
if (len > DB_EXT_LEN && strcmp(ent->name + len - DB_EXT_LEN, ".db") == 0) {
7233+
if (cbm_is_project_index_db(ent->name)) {
72367234
char path[CLI_BUF_1K];
72377235
snprintf(path, sizeof(path), "%s/%s", cache_dir, ent->name);
72387236
/* Also remove .db.tmp if present */
@@ -7268,7 +7266,7 @@ cbm_config_t *cbm_config_open(const char *cache_dir) {
72687266
}
72697267

72707268
char dbpath[CLI_BUF_1K];
7271-
snprintf(dbpath, sizeof(dbpath), "%s/_config.db", cache_dir);
7269+
snprintf(dbpath, sizeof(dbpath), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);
72727270

72737271
/* Ensure directory exists */
72747272
mkdirp(cache_dir, DIR_PERMS);
@@ -10423,7 +10421,7 @@ static int cbm_install_agent_configs_with_previous(const char *home, const char
1042310421
return result;
1042410422
}
1042510423

10426-
/* Count .db files in the cache directory. */
10424+
/* Count project index .db files in the cache directory (internal stores excluded). */
1042710425
static int count_db_indexes(const char *home) {
1042810426
const char *cache_dir = get_cache_dir(home);
1042910427
if (!cache_dir) {
@@ -10436,8 +10434,7 @@ static int count_db_indexes(const char *home) {
1043610434
int count = 0;
1043710435
cbm_dirent_t *ent;
1043810436
while ((ent = cbm_readdir(d)) != NULL) {
10439-
size_t len = strlen(ent->name);
10440-
if (len > DB_EXT_LEN && strcmp(ent->name + len - DB_EXT_LEN, ".db") == 0) {
10437+
if (cbm_is_project_index_db(ent->name)) {
1044110438
count++;
1044210439
}
1044310440
}

‎src/cli/cli.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ unsigned char *cbm_extract_binary_from_zip(const unsigned char *data, int data_l
404404
* Prints each file path to stdout. Returns count of .db files found. */
405405
int cbm_list_indexes(const char *home_dir);
406406

407-
/* Remove all .db files in the cache directory. Returns count removed. */
407+
/* Remove every project index .db (and its sidecars) in the cache directory.
408+
* Internal stores (_config.db, _cross_repo.db) are kept. Returns count removed. */
408409
int cbm_remove_indexes(const char *home_dir);
409410

410411
/* ── Config store (persistent key-value, backed by _config.db) ── */

‎src/foundation/str_util.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,24 @@ bool cbm_validate_project_name(const char *name) {
310310
return true;
311311
}
312312

313+
bool cbm_is_internal_cache_db(const char *filename) {
314+
return filename && (strcmp(filename, CBM_CONFIG_DB_FILENAME) == 0 ||
315+
strcmp(filename, CBM_CROSS_REPO_DB_FILENAME) == 0);
316+
}
317+
318+
bool cbm_is_project_index_db(const char *filename) {
319+
static const char db_ext[] = ".db";
320+
const size_t ext_len = sizeof(db_ext) - 1;
321+
if (!filename) {
322+
return false;
323+
}
324+
size_t len = strlen(filename);
325+
if (len <= ext_len || strcmp(filename + len - ext_len, db_ext) != 0) {
326+
return false;
327+
}
328+
return !cbm_is_internal_cache_db(filename);
329+
}
330+
313331
int cbm_utf8_trim_partial(char *buf) {
314332
if (!buf) {
315333
return 0;

‎src/foundation/str_util.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ bool cbm_validate_shell_path_arg(const char *path);
7373
* Returns true if safe, false if the name could escape the cache directory. */
7474
bool cbm_validate_project_name(const char *name);
7575

76+
/* Internal stores that live next to the project indexes in the cache
77+
* directory. They are exact filenames: a project name may itself begin with
78+
* "_" or contain "config", so prefix/substring filters would hide real
79+
* projects. */
80+
#define CBM_CONFIG_DB_FILENAME "_config.db"
81+
#define CBM_CROSS_REPO_DB_FILENAME "_cross_repo.db"
82+
83+
/* True when a cache-directory entry is one of the internal stores above. */
84+
bool cbm_is_internal_cache_db(const char *filename);
85+
86+
/* True when a cache-directory entry is a project index: "<name>.db" with a
87+
* non-empty stem and not an internal store. The one predicate every
88+
* enumeration of the cache directory's .db files uses (list, count,
89+
* remove, cross-repo). */
90+
bool cbm_is_project_index_db(const char *filename);
91+
7692
/* Safe snprintf append: clamps offset to prevent buffer overflow on truncation.
7793
* When snprintf truncates, it returns what it WOULD have written, which can make
7894
* offset > bufsize. Next call: bufsize - offset wraps unsigned → huge → overflow.

‎src/pipeline/pass_cross_repo.c‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,15 +1094,13 @@ static int collect_all_projects(char ***out, cr_run_context_t *ctx) {
10941094
failed = true;
10951095
break;
10961096
}
1097-
size_t len = strlen(ent->name);
1098-
if (len < CR_COL_4 || strcmp(ent->name + len - CR_DB_EXT_LEN, ".db") != 0) {
1099-
continue;
1100-
}
1101-
/* Internal stores are exact filenames. Substring filtering would hide
1102-
* legitimate projects such as orders_config_service or api-wal. */
1103-
if (strcmp(ent->name, "_cross_repo.db") == 0 || strcmp(ent->name, "_config.db") == 0) {
1097+
/* Internal stores are exact filenames (cbm_is_internal_cache_db).
1098+
* Substring filtering would hide legitimate projects such as
1099+
* orders_config_service or api-wal. */
1100+
if (!cbm_is_project_index_db(ent->name)) {
11041101
continue;
11051102
}
1103+
size_t len = strlen(ent->name);
11061104
if (count >= CR_MAX_PROJECTS) {
11071105
failed = true;
11081106
break;

‎tests/test_cli.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,71 @@ TEST(cli_remove_indexes_deletes_orphan_sqlite_sidecars_issue2054) {
22852285
PASS();
22862286
}
22872287

2288+
/* The cache directory also holds internal stores next to the project
2289+
* indexes: the user's settings (_config.db) and the cross-repo store
2290+
* (_cross_repo.db). Index enumeration treated every *.db as a project, so
2291+
* install --reset-indexes / uninstall deleted the user's config along with
2292+
* the indexes, and list/count over-reported. Internal stores are exact
2293+
* filenames: a real project whose name starts with "_" is still an index. */
2294+
TEST(cli_index_enumeration_skips_internal_cache_dbs) {
2295+
char tmpdir[256];
2296+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-internal-dbs-XXXXXX");
2297+
if (!cbm_mkdtemp(tmpdir)) {
2298+
FAIL("cbm_mkdtemp failed");
2299+
}
2300+
char *old_home = NULL;
2301+
char *old_cache = NULL;
2302+
cli_activation_save_env(&old_home, &old_cache);
2303+
cbm_setenv("HOME", tmpdir, 1);
2304+
char cache_dir[512];
2305+
snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
2306+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
2307+
test_mkdirp(cache_dir);
2308+
2309+
cbm_config_t *cfg = cbm_config_open(cache_dir);
2310+
int set_rc = cfg ? cbm_config_set(cfg, "auto_index", "true") : -1;
2311+
cbm_config_close(cfg);
2312+
2313+
char config_path[640];
2314+
char cross_path[640];
2315+
char proj_path[640];
2316+
char underscore_proj_path[640];
2317+
snprintf(config_path, sizeof(config_path), "%s/_config.db", cache_dir);
2318+
snprintf(cross_path, sizeof(cross_path), "%s/_cross_repo.db", cache_dir);
2319+
snprintf(proj_path, sizeof(proj_path), "%s/proj.db", cache_dir);
2320+
snprintf(underscore_proj_path, sizeof(underscore_proj_path), "%s/_work-api.db", cache_dir);
2321+
write_test_file(cross_path, "cross");
2322+
write_test_file(proj_path, "db");
2323+
write_test_file(underscore_proj_path, "db");
2324+
2325+
int listed = cbm_list_indexes(tmpdir);
2326+
int removed = cbm_remove_indexes(tmpdir);
2327+
2328+
struct stat st;
2329+
bool config_kept = stat(config_path, &st) == 0;
2330+
bool cross_kept = stat(cross_path, &st) == 0;
2331+
bool proj_absent = stat(proj_path, &st) != 0;
2332+
bool underscore_proj_absent = stat(underscore_proj_path, &st) != 0;
2333+
char value[32] = "";
2334+
cfg = cbm_config_open(cache_dir);
2335+
if (cfg) {
2336+
snprintf(value, sizeof(value), "%s", cbm_config_get(cfg, "auto_index", ""));
2337+
cbm_config_close(cfg);
2338+
}
2339+
cli_activation_restore_env(old_home, old_cache);
2340+
test_rmdir_r(tmpdir);
2341+
2342+
ASSERT_EQ(set_rc, 0);
2343+
ASSERT_EQ(listed, 2);
2344+
ASSERT_EQ(removed, 2);
2345+
ASSERT_TRUE(config_kept);
2346+
ASSERT_TRUE(cross_kept);
2347+
ASSERT_TRUE(proj_absent);
2348+
ASSERT_TRUE(underscore_proj_absent);
2349+
ASSERT_STR_EQ(value, "true");
2350+
PASS();
2351+
}
2352+
22882353
TEST(cli_install_config_only_waits_for_cohort_drain) {
22892354
char tmpdir[256];
22902355
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-daemon-install-config-race-XXXXXX");
@@ -16264,6 +16329,7 @@ SUITE(cli) {
1626416329
RUN_TEST(cli_activation_commands_reject_malformed_and_unknown_flags);
1626516330
RUN_TEST(cli_install_reset_deletion_waits_for_final_activation_guard);
1626616331
RUN_TEST(cli_remove_indexes_deletes_orphan_sqlite_sidecars_issue2054);
16332+
RUN_TEST(cli_index_enumeration_skips_internal_cache_dbs);
1626716333
RUN_TEST(cli_install_config_only_waits_for_cohort_drain);
1626816334
RUN_TEST(cli_install_config_and_path_finish_before_guard_release);
1626916335
RUN_TEST(cli_install_config_failure_keeps_published_binary);

0 commit comments

Comments
 (0)