cscvs to tla changeset 56
authorJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:26 +0000 (23:47 +0000)
committerJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:26 +0000 (23:47 +0000)
Author: noodles
Date: 2003/02/12 22:48:14
Add code to do a dump of the key database to a flat OpenPGP data file.

keydb.h
keydb_db3.c
onak.c

diff --git a/keydb.h b/keydb.h
index 647916c9d42521473d4d5ba72a6e9a2bd79c6334..7deb95a9e473490c3a773e1a41b15bbb22c26b12 100644 (file)
--- a/keydb.h
+++ b/keydb.h
@@ -133,4 +133,16 @@ struct ll *cached_getkeysigs(uint64_t keyid);
  */
 uint64_t getfullkeyid(uint64_t keyid);
 
+/**
+ *     dumpdb - dump the key database
+ *     @filenamebase: The base filename to use for the dump.
+ *
+ *     Dumps the database into one or more files, which contain pure OpenPGP
+ *     that can be reimported into onak or gpg. filenamebase provides a base
+ *     file name for the dump; several files may be created, all of which will
+ *     begin with this string and then have a unique number and a .pgp
+ *     extension.
+ */
+int dumpdb(char *filenamebase);
+
 #endif /* __KEYDB_H__ */
index 4d1bcca162acc14948e0e0e29be077170d76c2fb..de24f750d2656ad04c6c53fc7367fdbbbb601eb2 100644 (file)
@@ -107,7 +107,6 @@ struct ll *makewordlist(struct ll *wordlist, char *word)
  */
 void initdb(void)
 {
-       char buf[1024];
        int ret = 0;
 
        ret = db_env_create(&dbenv, 0);
@@ -198,6 +197,7 @@ bool starttrans(void)
 {
        int ret;
 
+       assert(dbenv != NULL);
        assert(txn == NULL);
 
        ret = txn_begin(dbenv,
@@ -221,6 +221,7 @@ void endtrans(void)
 {
        int ret;
 
+       assert(dbenv != NULL);
        assert(txn != NULL);
 
        ret = txn_commit(txn,
@@ -699,6 +700,52 @@ int delete_key(uint64_t keyid, bool intrans)
        return deadlock ? (-1) : (ret == DB_NOTFOUND);
 }
 
+/**
+ *     dumpdb - dump the key database
+ *     @filenamebase: The base filename to use for the dump.
+ *
+ *     Dumps the database into one or more files, which contain pure OpenPGP
+ *     that can be reimported into onak or gpg. filenamebase provides a base
+ *     file name for the dump; several files may be created, all of which will
+ *     begin with this string and then have a unique number and a .pgp
+ *     extension.
+ */
+int dumpdb(char *filenamebase)
+{
+       DBT key, data;
+       DBC *cursor = NULL;
+       int ret = 0;
+       int fd = -1;
+
+       starttrans();
+       
+       ret = dbconn->cursor(dbconn,
+               txn,
+               &cursor,
+               0);   /* flags */
+
+       fd = open(filenamebase, O_CREAT | O_WRONLY | O_TRUNC, 0640);
+       memset(&key, 0, sizeof(key));
+       memset(&data, 0, sizeof(data));
+       ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
+       while (ret == 0) {
+               write(fd, data.data, data.size);
+               memset(&key, 0, sizeof(key));
+               memset(&data, 0, sizeof(data));
+               ret = cursor->c_get(cursor, &key, &data, DB_NEXT);
+       }
+       dbconn->err(dbconn, ret, "Problem reading key");
+
+       close(fd);
+
+       ret = cursor->c_close(cursor);
+       cursor = NULL;
+       
+       endtrans();
+       
+       return 0;
+}
+
 /*
  * Include the basic keydb routines.
  */
diff --git a/onak.c b/onak.c
index 07e01a591db64a940497c1bde6e7e0dd3df31adf..f673bd38b5038dcb26042f6dcb6a92038c7bec39 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -120,6 +120,10 @@ int main(int argc, char *argv[])
 
        if ((argc - optind) < 1) {
                usage();
+       } else if (!strcmp("dump", argv[optind])) {
+               initdb();
+               dumpdb("keydump");
+               cleanupdb();
        } else if (!strcmp("add", argv[optind])) {
                if (binary) {
                        result = read_openpgp_stream(stdin_getchar, NULL,