-struct traverse_state {
- bool error;
- struct tdb1_context *dest_db;
-};
-
-/*
- traverse function for repacking
- */
-static int repack_traverse(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA data, void *private_data)
-{
- struct traverse_state *state = (struct traverse_state *)private_data;
- if (tdb1_store(state->dest_db, key, data, TDB1_INSERT) != 0) {
- state->error = true;
- return -1;
- }
- return 0;
-}
-
-/*
- repack a tdb
- */
-_PUBLIC_ int tdb1_repack(struct tdb1_context *tdb)
-{
- struct tdb1_context *tmp_db;
- struct traverse_state state;
-
- if (tdb1_transaction_start(tdb) != 0) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
- return -1;
- }
-
- tmp_db = tdb1_open("tmpdb", tdb1_hash_size(tdb), TDB1_INTERNAL, O_RDWR|O_CREAT, 0);
- if (tmp_db == NULL) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
- tdb1_transaction_cancel(tdb);
- return -1;
- }
-
- state.error = false;
- state.dest_db = tmp_db;
-
- if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (state.error) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Error during traversal\n"));
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (tdb1_wipe_all(tdb) != 0) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- state.error = false;
- state.dest_db = tdb;
-
- if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- if (state.error) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Error during second traversal\n"));
- tdb1_transaction_cancel(tdb);
- tdb1_close(tmp_db);
- return -1;
- }
-
- tdb1_close(tmp_db);
-
- if (tdb1_transaction_commit(tdb) != 0) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to commit\n"));
- return -1;
- }
-
- return 0;
-}
-