| 1335 | } |
| 1336 | |
| 1337 | int cbm_store_backup_path(const char *source_path, const char *staging_path) { |
| 1338 | if (!source_path || !staging_path) { |
| 1339 | return CBM_STORE_ERR; |
| 1340 | } |
| 1341 | if (cbm_remove_db_sidecars(staging_path) != 0) { |
| 1342 | return CBM_STORE_ERR; |
| 1343 | } |
| 1344 | |
| 1345 | char source_api[4096]; |
| 1346 | char staging_api[4096]; |
| 1347 | if (!cbm_path_for_file_api(source_path, source_api, sizeof(source_api)) || |
| 1348 | !cbm_path_for_file_api(staging_path, staging_api, sizeof(staging_api))) { |
| 1349 | return CBM_STORE_ERR; |
| 1350 | } |
| 1351 | sqlite3 *source = NULL; |
| 1352 | sqlite3 *dest = NULL; |
| 1353 | int rc = sqlite3_open_v2(source_api, &source, SQLITE_OPEN_READONLY, NULL); |
| 1354 | if (rc != SQLITE_OK) { |
| 1355 | sqlite3_close(source); |
| 1356 | return CBM_STORE_ERR; |
| 1357 | } |
| 1358 | sqlite3_busy_timeout(source, 10000); |
| 1359 | rc = sqlite3_open_v2(staging_api, &dest, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); |
| 1360 | if (rc != SQLITE_OK) { |
| 1361 | sqlite3_close(dest); |
| 1362 | sqlite3_close(source); |
| 1363 | return CBM_STORE_ERR; |
| 1364 | } |
| 1365 | sqlite3_busy_timeout(dest, 10000); |
| 1366 | |
| 1367 | sqlite3_backup *backup = sqlite3_backup_init(dest, "main", source, "main"); |
| 1368 | int result = CBM_STORE_ERR; |
| 1369 | if (backup) { |
| 1370 | int step_rc = sqlite3_backup_step(backup, CBM_NOT_FOUND); |
| 1371 | int finish_rc = sqlite3_backup_finish(backup); |
| 1372 | if (step_rc == SQLITE_DONE && finish_rc == SQLITE_OK) { |
| 1373 | result = CBM_STORE_OK; |
| 1374 | } |
| 1375 | } |
| 1376 | if (sqlite3_close(dest) != SQLITE_OK) { |
| 1377 | result = CBM_STORE_ERR; |
| 1378 | } |
| 1379 | if (sqlite3_close(source) != SQLITE_OK) { |
| 1380 | result = CBM_STORE_ERR; |
| 1381 | } |
| 1382 | return result; |
| 1383 | } |
| 1384 | |
| 1385 | /* #1083: the WAL size limit configured on this (write) connection, in bytes. |
| 1386 | * -1 means unlimited (SQLite's default — the pre-fix behavior). Per-connection |
no test coverage detected