2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "tdb1_private.h"
33 non-blocking increment of the tdb sequence number if the tdb has been opened using
36 void tdb1_increment_seqnum_nonblock(struct tdb1_context *tdb)
40 if (!(tdb->flags & TDB1_SEQNUM)) {
44 /* we ignore errors from this, as we have no sane way of
47 tdb1_ofs_read(tdb, TDB1_SEQNUM_OFS, &seqnum);
49 tdb1_ofs_write(tdb, TDB1_SEQNUM_OFS, &seqnum);
53 increment the tdb sequence number if the tdb has been opened using
56 static void tdb1_increment_seqnum(struct tdb1_context *tdb)
58 if (!(tdb->flags & TDB1_SEQNUM)) {
62 if (tdb1_nest_lock(tdb, TDB1_SEQNUM_OFS, F_WRLCK,
63 TDB1_LOCK_WAIT|TDB1_LOCK_PROBE) != 0) {
67 tdb1_increment_seqnum_nonblock(tdb);
69 tdb1_nest_unlock(tdb, TDB1_SEQNUM_OFS, F_WRLCK, false);
72 static int tdb1_key_compare(TDB1_DATA key, TDB1_DATA data, void *private_data)
74 return memcmp(data.dptr, key.dptr, data.dsize);
77 /* Returns 0 on fail. On success, return offset of record, and fills
79 static tdb1_off_t tdb1_find(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash,
80 struct tdb1_record *r)
84 /* read in the hash top */
85 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1)
88 /* keep looking until we find the right record */
90 if (tdb1_rec_read(tdb, rec_ptr, r) == -1)
93 if (!TDB1_DEAD(r) && hash==r->full_hash
94 && key.dsize==r->key_len
95 && tdb1_parse_data(tdb, key, rec_ptr + sizeof(*r),
96 r->key_len, tdb1_key_compare,
100 /* detect tight infinite loop */
101 if (rec_ptr == r->next) {
102 tdb->ecode = TDB1_ERR_CORRUPT;
103 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_find: loop detected.\n"));
108 tdb->ecode = TDB1_ERR_NOEXIST;
112 /* As tdb1_find, but if you succeed, keep the lock */
113 tdb1_off_t tdb1_find_lock_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, int locktype,
114 struct tdb1_record *rec)
118 if (tdb1_lock(tdb, TDB1_BUCKET(hash), locktype) == -1)
120 if (!(rec_ptr = tdb1_find(tdb, key, hash, rec)))
121 tdb1_unlock(tdb, TDB1_BUCKET(hash), locktype);
125 static TDB1_DATA _tdb1_fetch(struct tdb1_context *tdb, TDB1_DATA key);
127 /* update an entry in place - this only works if the new data size
128 is <= the old data size and the key exists.
129 on failure return -1.
131 static int tdb1_update_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash, TDB1_DATA dbuf)
133 struct tdb1_record rec;
137 if (!(rec_ptr = tdb1_find(tdb, key, hash, &rec)))
140 /* it could be an exact duplicate of what is there - this is
141 * surprisingly common (eg. with a ldb re-index). */
142 if (rec.key_len == key.dsize &&
143 rec.data_len == dbuf.dsize &&
144 rec.full_hash == hash) {
145 TDB1_DATA data = _tdb1_fetch(tdb, key);
146 if (data.dsize == dbuf.dsize &&
147 memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
158 /* must be long enough key, data and tailer */
159 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb1_off_t)) {
160 tdb->ecode = TDB1_SUCCESS; /* Not really an error */
164 if (tdb->methods->tdb1_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
165 dbuf.dptr, dbuf.dsize) == -1)
168 if (dbuf.dsize != rec.data_len) {
170 rec.data_len = dbuf.dsize;
171 return tdb1_rec_write(tdb, rec_ptr, &rec);
177 /* find an entry in the database given a key */
178 /* If an entry doesn't exist tdb1_err will be set to
179 * TDB1_ERR_NOEXIST. If a key has no data attached
180 * then the TDB1_DATA will have zero length but
183 static TDB1_DATA _tdb1_fetch(struct tdb1_context *tdb, TDB1_DATA key)
186 struct tdb1_record rec;
190 /* find which hash bucket it is in */
191 hash = tdb->hash_fn(&key);
192 if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
195 ret.dptr = tdb1_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
197 ret.dsize = rec.data_len;
198 tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
202 TDB1_DATA tdb1_fetch(struct tdb1_context *tdb, TDB1_DATA key)
204 TDB1_DATA ret = _tdb1_fetch(tdb, key);
210 * Find an entry in the database and hand the record's data to a parsing
211 * function. The parsing function is executed under the chain read lock, so it
212 * should be fast and should not block on other syscalls.
214 * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
216 * For mmapped tdb's that do not have a transaction open it points the parsing
217 * function directly at the mmap area, it avoids the malloc/memcpy in this
218 * case. If a transaction is open or no mmap is available, it has to do
219 * malloc/read/parse/free.
221 * This is interesting for all readers of potentially large data structures in
222 * the tdb records, ldb indexes being one example.
224 * Return -1 if the record was not found.
227 int tdb1_parse_record(struct tdb1_context *tdb, TDB1_DATA key,
228 int (*parser)(TDB1_DATA key, TDB1_DATA data,
233 struct tdb1_record rec;
237 /* find which hash bucket it is in */
238 hash = tdb->hash_fn(&key);
240 if (!(rec_ptr = tdb1_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
241 /* record not found */
242 tdb->ecode = TDB1_ERR_NOEXIST;
246 ret = tdb1_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
247 rec.data_len, parser, private_data);
249 tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
254 /* check if an entry in the database exists
256 note that 1 is returned if the key is found and 0 is returned if not found
257 this doesn't match the conventions in the rest of this module, but is
260 static int tdb1_exists_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash)
262 struct tdb1_record rec;
264 if (tdb1_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
266 tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_RDLCK);
270 int tdb1_exists(struct tdb1_context *tdb, TDB1_DATA key)
272 uint32_t hash = tdb->hash_fn(&key);
275 ret = tdb1_exists_hash(tdb, key, hash);
279 /* actually delete an entry in the database given the offset */
280 int tdb1_do_delete(struct tdb1_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec)
282 tdb1_off_t last_ptr, i;
283 struct tdb1_record lastrec;
285 if (tdb->read_only || tdb->traverse_read) return -1;
287 if (((tdb->traverse_write != 0) && (!TDB1_DEAD(rec))) ||
288 tdb1_write_lock_record(tdb, rec_ptr) == -1) {
289 /* Someone traversing here: mark it as dead */
290 rec->magic = TDB1_DEAD_MAGIC;
291 return tdb1_rec_write(tdb, rec_ptr, rec);
293 if (tdb1_write_unlock_record(tdb, rec_ptr) != 0)
296 /* find previous record in hash chain */
297 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(rec->full_hash), &i) == -1)
299 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
300 if (tdb1_rec_read(tdb, i, &lastrec) == -1)
303 /* unlink it: next ptr is at start of record. */
305 last_ptr = TDB1_HASH_TOP(rec->full_hash);
306 if (tdb1_ofs_write(tdb, last_ptr, &rec->next) == -1)
309 /* recover the space */
310 if (tdb1_free(tdb, rec_ptr, rec) == -1)
315 static int tdb1_count_dead(struct tdb1_context *tdb, uint32_t hash)
319 struct tdb1_record rec;
321 /* read in the hash top */
322 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1)
326 if (tdb1_rec_read(tdb, rec_ptr, &rec) == -1)
329 if (rec.magic == TDB1_DEAD_MAGIC) {
338 * Purge all DEAD records from a hash chain
340 static int tdb1_purge_dead(struct tdb1_context *tdb, uint32_t hash)
343 struct tdb1_record rec;
346 if (tdb1_lock(tdb, -1, F_WRLCK) == -1) {
350 /* read in the hash top */
351 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1)
357 if (tdb1_rec_read(tdb, rec_ptr, &rec) == -1) {
363 if (rec.magic == TDB1_DEAD_MAGIC
364 && tdb1_do_delete(tdb, rec_ptr, &rec) == -1) {
371 tdb1_unlock(tdb, -1, F_WRLCK);
375 /* delete an entry in the database given a key */
376 static int tdb1_delete_hash(struct tdb1_context *tdb, TDB1_DATA key, uint32_t hash)
379 struct tdb1_record rec;
382 if (tdb->max_dead_records != 0) {
385 * Allow for some dead records per hash chain, mainly for
386 * tdb's with a very high create/delete rate like locking.tdb.
389 if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
392 if (tdb1_count_dead(tdb, hash) >= tdb->max_dead_records) {
394 * Don't let the per-chain freelist grow too large,
395 * delete all existing dead records
397 tdb1_purge_dead(tdb, hash);
400 if (!(rec_ptr = tdb1_find(tdb, key, hash, &rec))) {
401 tdb1_unlock(tdb, TDB1_BUCKET(hash), F_WRLCK);
406 * Just mark the record as dead.
408 rec.magic = TDB1_DEAD_MAGIC;
409 ret = tdb1_rec_write(tdb, rec_ptr, &rec);
412 if (!(rec_ptr = tdb1_find_lock_hash(tdb, key, hash, F_WRLCK,
416 ret = tdb1_do_delete(tdb, rec_ptr, &rec);
420 tdb1_increment_seqnum(tdb);
423 if (tdb1_unlock(tdb, TDB1_BUCKET(rec.full_hash), F_WRLCK) != 0)
424 TDB1_LOG((tdb, TDB1_DEBUG_WARNING, "tdb1_delete: WARNING tdb1_unlock failed!\n"));
428 int tdb1_delete(struct tdb1_context *tdb, TDB1_DATA key)
430 uint32_t hash = tdb->hash_fn(&key);
433 ret = tdb1_delete_hash(tdb, key, hash);
438 * See if we have a dead record around with enough space
440 static tdb1_off_t tdb1_find_dead(struct tdb1_context *tdb, uint32_t hash,
441 struct tdb1_record *r, tdb1_len_t length)
445 /* read in the hash top */
446 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1)
449 /* keep looking until we find the right record */
451 if (tdb1_rec_read(tdb, rec_ptr, r) == -1)
454 if (TDB1_DEAD(r) && r->rec_len >= length) {
456 * First fit for simple coding, TODO: change to best
466 static int _tdb1_store(struct tdb1_context *tdb, TDB1_DATA key,
467 TDB1_DATA dbuf, int flag, uint32_t hash)
469 struct tdb1_record rec;
474 /* check for it existing, on insert. */
475 if (flag == TDB1_INSERT) {
476 if (tdb1_exists_hash(tdb, key, hash)) {
477 tdb->ecode = TDB1_ERR_EXISTS;
481 /* first try in-place update, on modify or replace. */
482 if (tdb1_update_hash(tdb, key, hash, dbuf) == 0) {
485 if (tdb->ecode == TDB1_ERR_NOEXIST &&
486 flag == TDB1_MODIFY) {
487 /* if the record doesn't exist and we are in TDB1_MODIFY mode then
488 we should fail the store */
492 /* reset the error code potentially set by the tdb1_update() */
493 tdb->ecode = TDB1_SUCCESS;
495 /* delete any existing record - if it doesn't exist we don't
496 care. Doing this first reduces fragmentation, and avoids
497 coalescing with `allocated' block before it's updated. */
498 if (flag != TDB1_INSERT)
499 tdb1_delete_hash(tdb, key, hash);
501 /* Copy key+value *before* allocating free space in case malloc
502 fails and we are left with a dead spot in the tdb. */
504 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
505 tdb->ecode = TDB1_ERR_OOM;
509 memcpy(p, key.dptr, key.dsize);
511 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
513 if (tdb->max_dead_records != 0) {
515 * Allow for some dead records per hash chain, look if we can
516 * find one that can hold the new record. We need enough space
517 * for key, data and tailer. If we find one, we don't have to
518 * consult the central freelist.
520 rec_ptr = tdb1_find_dead(
522 key.dsize + dbuf.dsize + sizeof(tdb1_off_t));
525 rec.key_len = key.dsize;
526 rec.data_len = dbuf.dsize;
527 rec.full_hash = hash;
528 rec.magic = TDB1_MAGIC;
529 if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
530 || tdb->methods->tdb1_write(
531 tdb, rec_ptr + sizeof(rec),
532 p, key.dsize + dbuf.dsize) == -1) {
540 * We have to allocate some space from the freelist, so this means we
541 * have to lock it. Use the chance to purge all the DEAD records from
542 * the hash chain under the freelist lock.
545 if (tdb1_lock(tdb, -1, F_WRLCK) == -1) {
549 if ((tdb->max_dead_records != 0)
550 && (tdb1_purge_dead(tdb, hash) == -1)) {
551 tdb1_unlock(tdb, -1, F_WRLCK);
555 /* we have to allocate some space */
556 rec_ptr = tdb1_allocate(tdb, key.dsize + dbuf.dsize, &rec);
558 tdb1_unlock(tdb, -1, F_WRLCK);
564 /* Read hash top into next ptr */
565 if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(hash), &rec.next) == -1)
568 rec.key_len = key.dsize;
569 rec.data_len = dbuf.dsize;
570 rec.full_hash = hash;
571 rec.magic = TDB1_MAGIC;
573 /* write out and point the top of the hash chain at it */
574 if (tdb1_rec_write(tdb, rec_ptr, &rec) == -1
575 || tdb->methods->tdb1_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
576 || tdb1_ofs_write(tdb, TDB1_HASH_TOP(hash), &rec_ptr) == -1) {
577 /* Need to tdb1_unallocate() here */
585 tdb1_increment_seqnum(tdb);
592 /* store an element in the database, replacing any existing element
595 return 0 on success, -1 on failure
597 int tdb1_store(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA dbuf, int flag)
602 if (tdb->read_only || tdb->traverse_read) {
603 tdb->ecode = TDB1_ERR_RDONLY;
607 /* find which hash bucket it is in */
608 hash = tdb->hash_fn(&key);
609 if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
612 ret = _tdb1_store(tdb, key, dbuf, flag, hash);
613 tdb1_unlock(tdb, TDB1_BUCKET(hash), F_WRLCK);
617 /* Append to an entry. Create if not exist. */
618 int tdb1_append(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA new_dbuf)
624 /* find which hash bucket it is in */
625 hash = tdb->hash_fn(&key);
626 if (tdb1_lock(tdb, TDB1_BUCKET(hash), F_WRLCK) == -1)
629 dbuf = _tdb1_fetch(tdb, key);
631 if (dbuf.dptr == NULL) {
632 dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
634 unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
635 unsigned char *new_dptr;
637 /* realloc '0' is special: don't do that. */
640 new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
641 if (new_dptr == NULL) {
644 dbuf.dptr = new_dptr;
647 if (dbuf.dptr == NULL) {
648 tdb->ecode = TDB1_ERR_OOM;
652 memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
653 dbuf.dsize += new_dbuf.dsize;
655 ret = _tdb1_store(tdb, key, dbuf, 0, hash);
658 tdb1_unlock(tdb, TDB1_BUCKET(hash), F_WRLCK);
659 SAFE_FREE(dbuf.dptr);
665 return the current logging function
666 useful for external tdb routines that wish to log tdb errors
668 tdb1_log_func tdb1_log_fn(struct tdb1_context *tdb)
670 return tdb->log.log_fn;
675 get the tdb sequence number. Only makes sense if the writers opened
676 with TDB1_SEQNUM set. Note that this sequence number will wrap quite
677 quickly, so it should only be used for a 'has something changed'
678 test, not for code that relies on the count of the number of changes
679 made. If you want a counter then use a tdb record.
681 The aim of this sequence number is to allow for a very lightweight
682 test of a possible tdb change.
684 int tdb1_get_seqnum(struct tdb1_context *tdb)
688 tdb1_ofs_read(tdb, TDB1_SEQNUM_OFS, &seqnum);
692 int tdb1_hash_size(struct tdb1_context *tdb)
694 return tdb->header.hash_size;
699 add a region of the file to the freelist. Length is the size of the region in bytes,
700 which includes the free list header that needs to be added
702 static int tdb1_free_region(struct tdb1_context *tdb, tdb1_off_t offset, ssize_t length)
704 struct tdb1_record rec;
705 if (length <= sizeof(rec)) {
706 /* the region is not worth adding */
709 if (length + offset > tdb->map_size) {
710 TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"tdb1_free_region: adding region beyond end of file\n"));
713 memset(&rec,'\0',sizeof(rec));
714 rec.rec_len = length - sizeof(rec);
715 if (tdb1_free(tdb, offset, &rec) == -1) {
716 TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"tdb1_free_region: failed to add free record\n"));
723 wipe the entire database, deleting all records. This can be done
724 very fast by using a allrecord lock. The entire data portion of the
725 file becomes a single entry in the freelist.
727 This code carefully steps around the recovery area, leaving it alone
729 int tdb1_wipe_all(struct tdb1_context *tdb)
732 tdb1_off_t offset = 0;
734 tdb1_off_t recovery_head;
735 tdb1_len_t recovery_size = 0;
737 if (tdb1_lockall(tdb) != 0) {
742 /* see if the tdb has a recovery area, and remember its size
743 if so. We don't want to lose this as otherwise each
744 tdb1_wipe_all() in a transaction will increase the size of
745 the tdb by the size of the recovery area */
746 if (tdb1_ofs_read(tdb, TDB1_RECOVERY_HEAD, &recovery_head) == -1) {
747 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_wipe_all: failed to read recovery head\n"));
751 if (recovery_head != 0) {
752 struct tdb1_record rec;
753 if (tdb->methods->tdb1_read(tdb, recovery_head, &rec, sizeof(rec), TDB1_DOCONV()) == -1) {
754 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_wipe_all: failed to read recovery record\n"));
757 recovery_size = rec.rec_len + sizeof(rec);
760 /* wipe the hashes */
761 for (i=0;i<tdb->header.hash_size;i++) {
762 if (tdb1_ofs_write(tdb, TDB1_HASH_TOP(i), &offset) == -1) {
763 TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"tdb1_wipe_all: failed to write hash %d\n", i));
768 /* wipe the freelist */
769 if (tdb1_ofs_write(tdb, TDB1_FREELIST_TOP, &offset) == -1) {
770 TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"tdb1_wipe_all: failed to write freelist\n"));
774 /* add all the rest of the file to the freelist, possibly leaving a gap
775 for the recovery area */
776 if (recovery_size == 0) {
777 /* the simple case - the whole file can be used as a freelist */
778 data_len = (tdb->map_size - TDB1_DATA_START(tdb->header.hash_size));
779 if (tdb1_free_region(tdb, TDB1_DATA_START(tdb->header.hash_size), data_len) != 0) {
783 /* we need to add two freelist entries - one on either
784 side of the recovery area
786 Note that we cannot shift the recovery area during
787 this operation. Only the transaction.c code may
788 move the recovery area or we risk subtle data
791 data_len = (recovery_head - TDB1_DATA_START(tdb->header.hash_size));
792 if (tdb1_free_region(tdb, TDB1_DATA_START(tdb->header.hash_size), data_len) != 0) {
795 /* and the 2nd free list entry after the recovery area - if any */
796 data_len = tdb->map_size - (recovery_head+recovery_size);
797 if (tdb1_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
802 if (tdb1_unlockall(tdb) != 0) {
803 TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"tdb1_wipe_all: failed to unlock\n"));
814 struct traverse_state {
816 struct tdb1_context *dest_db;
820 traverse function for repacking
822 static int repack_traverse(struct tdb1_context *tdb, TDB1_DATA key, TDB1_DATA data, void *private_data)
824 struct traverse_state *state = (struct traverse_state *)private_data;
825 if (tdb1_store(state->dest_db, key, data, TDB1_INSERT) != 0) {
835 int tdb1_repack(struct tdb1_context *tdb)
837 struct tdb1_context *tmp_db;
838 struct traverse_state state;
840 if (tdb1_transaction_start(tdb) != 0) {
841 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
845 tmp_db = tdb1_open("tmpdb", tdb1_hash_size(tdb), TDB1_INTERNAL, O_RDWR|O_CREAT, 0);
846 if (tmp_db == NULL) {
847 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
848 tdb1_transaction_cancel(tdb);
853 state.dest_db = tmp_db;
855 if (tdb1_traverse_read(tdb, repack_traverse, &state) == -1) {
856 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
857 tdb1_transaction_cancel(tdb);
863 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Error during traversal\n"));
864 tdb1_transaction_cancel(tdb);
869 if (tdb1_wipe_all(tdb) != 0) {
870 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
871 tdb1_transaction_cancel(tdb);
879 if (tdb1_traverse_read(tmp_db, repack_traverse, &state) == -1) {
880 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
881 tdb1_transaction_cancel(tdb);
887 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Error during second traversal\n"));
888 tdb1_transaction_cancel(tdb);
895 if (tdb1_transaction_commit(tdb) != 0) {
896 TDB1_LOG((tdb, TDB1_DEBUG_FATAL, __location__ " Failed to commit\n"));
903 /* Even on files, we can get partial writes due to signals. */
904 bool tdb1_write_all(int fd, const void *buf, size_t count)
908 ret = write(fd, buf, count);
911 buf = (const char *)buf + ret;