Added kdb_db_destroy() function
authorTheodore Tso <tytso@mit.edu>
Sat, 2 Feb 1991 01:34:20 +0000 (01:34 +0000)
committerTheodore Tso <tytso@mit.edu>
Sat, 2 Feb 1991 01:34:20 +0000 (01:34 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1647 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/kdb/kdb_dbm.c

index ff448b3c37c246059870c586fe290267a4666a71..291670220b06d8205d1944403d7f6d0ec6294628 100644 (file)
@@ -20,6 +20,7 @@ static char rcsid_krb_dbm_c[] =
 #include <krb5/kdb_dbm.h>
 #include <krb5/ext-proto.h>
 #include <krb5/sysincl.h>
+#include <stdio.h>
 
 #define KRB5_DBM_MAX_RETRY 5
 
@@ -644,6 +645,66 @@ char *db_name;
     return retval;
 }
 
+/*
+ * Destroy the database.  Zero's out all of the files, just to be sure.
+ */
+krb5_error_code
+destroy_file_suffix(dbname, suffix)
+       char    *dbname;
+       char    *suffix;
+{
+       char    *filename;
+       struct stat     statb;
+       int             nb,fd,i;
+       char            buf[BUFSIZ];
+
+       filename = gen_dbsuffix(dbname, suffix);
+       if (stat(filename, &statb)) {
+               free(filename);
+               if (errno == ENOENT)
+                       return(0);
+               else
+                       return(errno);
+       }
+       if ((fd = open(filename, O_RDWR, 0)) < 0) {
+               free(filename);
+               return(errno);
+       }
+       i = 0;
+       while (i < statb.st_size) {
+               nb = write(fd, buf, BUFSIZ);
+               if (nb < 0) {
+                       free(filename);
+                       return(errno);
+               }
+               i += nb;
+       }
+       fsync(fd);
+       close(fd);
+
+       if (unlink(filename)) {
+               free(filename);
+               return(errno);
+       }
+       free(filename);
+       return(0);
+}
+
+krb5_error_code
+krb5_dbm_db_destroy(dbname)
+       char    *dbname;
+{
+       krb5_error_code retval;
+
+       if (retval = destroy_file_suffix(dbname, ".pag"))
+               return(retval);
+       if (retval = destroy_file_suffix(dbname, ".dir"))
+               return(retval);
+       if (retval = destroy_file_suffix(dbname, ".ok"))
+               return(retval);
+       return(0);
+}
+
 /*
  * "Atomically" rename the database in a way that locks out read
  * access in the middle of the rename.