Skip to content

Commit a0dc418

Browse files
authored
Merge pull request DeusData#2137 from DeusData/fix/2054-orphan-sidecars
fix(cli): remove orphan SQLite sidecars on index deletion (DeusData#2054)
2 parents 9b85dd3 + ca36316 commit a0dc418

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7099,6 +7099,12 @@ int cbm_remove_indexes(const char *home_dir) {
70997099
if (cbm_unlink(path) == 0) {
71007100
count++;
71017101
}
7102+
/* Remove the SQLite sidecars (-wal/-shm/-journal) for both the
7103+
* live and staged DBs. Idempotent and ENOENT-tolerant, so it runs
7104+
* even when the .db unlink failed -- an orphan -wal can outlive
7105+
* its .db. Sidecars are not indexes, so count is unchanged. */
7106+
cbm_remove_db_sidecars(path);
7107+
cbm_remove_db_sidecars(tmp_path);
71027108
}
71037109
}
71047110
cbm_closedir(d);

‎tests/test_cli.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,53 @@ TEST(cli_install_reset_deletion_waits_for_final_activation_guard) {
16701670
PASS();
16711671
}
16721672

1673+
/* Removing an index must also delete its SQLite sidecars (-wal/-shm/-journal),
1674+
* not just the .db. cbm_remove_indexes is the single deletion chokepoint for
1675+
* both explicit deleters (install --reset-indexes, uninstall), so an orphan
1676+
* -wal/-shm left behind here outlives the index everywhere. Reporter ask (c)
1677+
* on issue #2054. */
1678+
TEST(cli_remove_indexes_deletes_orphan_sqlite_sidecars_issue2054) {
1679+
char tmpdir[256];
1680+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-remove-indexes-sidecars-XXXXXX");
1681+
if (!cbm_mkdtemp(tmpdir)) {
1682+
FAIL("cbm_mkdtemp failed");
1683+
}
1684+
char *old_home = NULL;
1685+
char *old_cache = NULL;
1686+
cli_activation_save_env(&old_home, &old_cache);
1687+
cbm_setenv("HOME", tmpdir, 1);
1688+
char cache_dir[512];
1689+
snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
1690+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
1691+
test_mkdirp(cache_dir);
1692+
1693+
char db_path[640];
1694+
char wal_path[640];
1695+
char shm_path[640];
1696+
snprintf(db_path, sizeof(db_path), "%s/proj.db", cache_dir);
1697+
snprintf(wal_path, sizeof(wal_path), "%s/proj.db-wal", cache_dir);
1698+
snprintf(shm_path, sizeof(shm_path), "%s/proj.db-shm", cache_dir);
1699+
write_test_file(db_path, "db");
1700+
write_test_file(wal_path, "wal");
1701+
write_test_file(shm_path, "shm");
1702+
1703+
int rc = cbm_remove_indexes(tmpdir);
1704+
1705+
struct stat st;
1706+
bool db_absent = stat(db_path, &st) != 0;
1707+
bool wal_absent = stat(wal_path, &st) != 0;
1708+
bool shm_absent = stat(shm_path, &st) != 0;
1709+
cli_activation_restore_env(old_home, old_cache);
1710+
test_rmdir_r(tmpdir);
1711+
1712+
/* One .db removed; sidecars are not indexes, so the count is unchanged. */
1713+
ASSERT_EQ(rc, 1);
1714+
ASSERT_TRUE(db_absent);
1715+
ASSERT_TRUE(wal_absent);
1716+
ASSERT_TRUE(shm_absent);
1717+
PASS();
1718+
}
1719+
16731720
TEST(cli_install_config_only_waits_for_cohort_drain) {
16741721
char tmpdir[256];
16751722
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-daemon-install-config-race-XXXXXX");
@@ -15182,6 +15229,7 @@ SUITE(cli) {
1518215229
RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely);
1518315230
RUN_TEST(cli_activation_commands_reject_malformed_and_unknown_flags);
1518415231
RUN_TEST(cli_install_reset_deletion_waits_for_final_activation_guard);
15232+
RUN_TEST(cli_remove_indexes_deletes_orphan_sqlite_sidecars_issue2054);
1518515233
RUN_TEST(cli_install_config_only_waits_for_cohort_drain);
1518615234
RUN_TEST(cli_install_config_and_path_finish_before_guard_release);
1518715235
RUN_TEST(cli_install_config_failure_keeps_published_binary);

0 commit comments

Comments
 (0)