Skip to content

Commit 48acb55

Browse files
authored
Merge pull request DeusData#2405 from DeusData/fix/internal-db-enumeration
fix(cli): never treat internal cache stores as project indexes
2 parents 083bc99 + 4601532 commit 48acb55

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);
@@ -10437,7 +10435,7 @@ static int cbm_install_agent_configs_with_previous(const char *home, const char
1043710435
return result;
1043810436
}
1043910437

10440-
/* Count .db files in the cache directory. */
10438+
/* Count project index .db files in the cache directory (internal stores excluded). */
1044110439
static int count_db_indexes(const char *home) {
1044210440
const char *cache_dir = get_cache_dir(home);
1044310441
if (!cache_dir) {
@@ -10450,8 +10448,7 @@ static int count_db_indexes(const char *home) {
1045010448
int count = 0;
1045110449
cbm_dirent_t *ent;
1045210450
while ((ent = cbm_readdir(d)) != NULL) {
10453-
size_t len = strlen(ent->name);
10454-
if (len > DB_EXT_LEN && strcmp(ent->name + len - DB_EXT_LEN, ".db") == 0) {
10451+
if (cbm_is_project_index_db(ent->name)) {
1045510452
count++;
1045610453
}
1045710454
}

‎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
@@ -1170,15 +1170,13 @@ static int collect_all_projects(char ***out, cr_run_context_t *ctx) {
11701170
failed = true;
11711171
break;
11721172
}
1173-
size_t len = strlen(ent->name);
1174-
if (len < CR_COL_4 || strcmp(ent->name + len - CR_DB_EXT_LEN, ".db") != 0) {
1175-
continue;
1176-
}
1177-
/* Internal stores are exact filenames. Substring filtering would hide
1178-
* legitimate projects such as orders_config_service or api-wal. */
1179-
if (strcmp(ent->name, "_cross_repo.db") == 0 || strcmp(ent->name, "_config.db") == 0) {
1173+
/* Internal stores are exact filenames (cbm_is_internal_cache_db).
1174+
* Substring filtering would hide legitimate projects such as
1175+
* orders_config_service or api-wal. */
1176+
if (!cbm_is_project_index_db(ent->name)) {
11801177
continue;
11811178
}
1179+
size_t len = strlen(ent->name);
11821180
if (count >= CR_MAX_PROJECTS) {
11831181
failed = true;
11841182
break;

‎tests/test_cli.c‎

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

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

0 commit comments

Comments
 (0)