added tdb_wipe_all() function
authorAndrew Tridgell <tridge@samba.org>
Sat, 5 Jan 2008 01:08:41 +0000 (12:08 +1100)
committerAndrew Tridgell <tridge@samba.org>
Sat, 5 Jan 2008 01:08:41 +0000 (12:08 +1100)
lib/tdb/common/tdb.c

index 5be6325d4952ff7f1a2a148496770754e22dd948..ad4feba51e12314108d1a722b69203129851c966 100644 (file)
@@ -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;
+}