-struct traverse_state {
- enum TDB_ERROR error;
- struct tdb_context *dest_db;
-};
-
-/*
- traverse function for repacking
- */
-static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
-{
- struct traverse_state *state = (struct traverse_state *)private_data;
- if (tdb1_store(state->dest_db, key, data, TDB_INSERT) != 0) {
- state->error = state->dest_db->last_error;
- return -1;
- }
- return 0;
-}
-
-/*
- repack a tdb
- */
-int tdb1_repack(struct tdb_context *tdb)
-{
- struct tdb_context *tmp_db;
- struct traverse_state state;
-
- if (tdb1_transaction_start(tdb) != 0) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- __location__ " Failed to start transaction");
- return -1;
- }
-
- tmp_db = tdb1_open("tmpdb", tdb1_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0, NULL);
- if (tmp_db == NULL) {
- tdb->last_error = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
- __location__ " Failed to create tmp_db");
- tdb1_transaction_cancel(tdb);
- return -1;
- }
-
- state.error = TDB_SUCCESS;
- state.dest_db = tmp_db;
-
- if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- __location__ " Failed to traverse copying out");
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (state.error != TDB_SUCCESS) {
- tdb->last_error = tdb_logerr(tdb, state.error, TDB_LOG_ERROR,
- __location__ " Error during traversal");
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (tdb1_wipe_all(tdb) != 0) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- __location__ " Failed to wipe database\n");
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- state.error = TDB_SUCCESS;
- state.dest_db = tdb;
-
- if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- __location__ " Failed to traverse copying back");
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (state.error) {
- tdb->last_error = tdb_logerr(tdb, state.error, TDB_LOG_ERROR,
- __location__ " Error during second traversal");
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- tdb1_close(tmp_db);
-
- if (tdb1_transaction_commit(tdb) != 0) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- __location__ " Failed to commit");
- return -1;
- }
-
- return 0;
-}
-