summaryrefslogtreecommitdiffstats
path: root/ctdb/lib/tdb/common
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2008-01-05 12:08:41 +1100
committerAndrew Tridgell <tridge@samba.org>2008-01-05 12:08:41 +1100
commita21afe88bcfd12b5af4f8312b551afc3fc7a1c7c (patch)
tree0b2cedb2bb88edce2c9f7ea08c2d99c30e2aa79a /ctdb/lib/tdb/common
parent9bd69e75c873d51abea6813f3e8325e796e7ca7d (diff)
downloadsamba-a21afe88bcfd12b5af4f8312b551afc3fc7a1c7c.tar.gz
samba-a21afe88bcfd12b5af4f8312b551afc3fc7a1c7c.tar.xz
samba-a21afe88bcfd12b5af4f8312b551afc3fc7a1c7c.zip
added tdb_wipe_all() function
(This used to be ctdb commit 8e2d81cf54630970d66af92de2c0333acd2e1d22)
Diffstat (limited to 'ctdb/lib/tdb/common')
-rw-r--r--ctdb/lib/tdb/common/tdb.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/ctdb/lib/tdb/common/tdb.c b/ctdb/lib/tdb/common/tdb.c
index 5be6325d495..ad4feba51e1 100644
--- a/ctdb/lib/tdb/common/tdb.c
+++ b/ctdb/lib/tdb/common/tdb.c
@@ -236,7 +236,7 @@ int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
}
/* actually delete an entry in the database given the offset */
-int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct*rec)
+int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
{
tdb_off_t last_ptr, i;
struct list_struct lastrec;
@@ -670,3 +670,57 @@ void tdb_enable_seqnum(struct tdb_context *tdb)
{
tdb->flags |= TDB_SEQNUM;
}
+
+
+/*
+ wipe the entire database, deleting all records. This can be done
+ very fast by using a global lock. The entire data portion of the
+ file becomes a single entry in the freelist.
+ */
+int tdb_wipe_all(struct tdb_context *tdb)
+{
+ int i;
+ tdb_off_t offset = 0;
+ ssize_t data_len;
+
+ if (tdb_lockall(tdb) != 0) {
+ return -1;
+ }
+
+ /* wipe the hashes */
+ for (i=0;i<tdb->header.hash_size;i++) {
+ if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
+ goto failed;
+ }
+ }
+
+ /* wipe the freelist */
+ if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
+ goto failed;
+ }
+
+ /* add all the rest of the file to the freelist */
+ data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size)) - sizeof(struct list_struct);
+ if (data_len > 0) {
+ struct list_struct rec;
+ memset(&rec,'\0',sizeof(rec));
+ rec.rec_len = data_len;
+ if (tdb_free(tdb, TDB_DATA_START(tdb->header.hash_size), &rec) == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to add free record\n"));
+ goto failed;
+ }
+ }
+
+ if (tdb_unlockall(tdb) != 0) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
+ goto failed;
+ }
+
+ return 0;
+
+failed:
+ tdb_unlockall(tdb);
+ return -1;
+}