X-Git-Url: http://the.earth.li/gitweb/?a=blobdiff_plain;f=keydb_db3.c;h=de24f750d2656ad04c6c53fc7367fdbbbb601eb2;hb=76c5dd1081bc9db3bf446480457b52d4ae7b3891;hp=4d1bcca162acc14948e0e0e29be077170d76c2fb;hpb=87fbdd69b1885c6588ec924e37b734f2c3c5ed35;p=onak.git diff --git a/keydb_db3.c b/keydb_db3.c index 4d1bcca..de24f75 100644 --- a/keydb_db3.c +++ b/keydb_db3.c @@ -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. */