Add crude --exportdb and --importdb operations to rpmdb utility

- Exportdb simply dumps all the headers from rpmdb into simple header
  list which is about as rpm-native and backwards and forwards compatible
  format as they come. Importdb obviously, well, imports such a
  list into rpmdb.
- Both require transaction handle: we dont want anybody running
  transactions while exporting, and importing obviously requires
  write-locking.
This commit is contained in:
Panu Matilainen 2013-03-11 15:37:56 +02:00
parent c6e84b8981
commit 656459ba44
1 changed files with 56 additions and 0 deletions

56
rpmdb.c
View File

@ -2,6 +2,7 @@
#include <popt.h>
#include <rpm/rpmcli.h>
#include <rpm/rpmdb.h>
#include "cliutils.h"
#include "debug.h"
@ -13,6 +14,8 @@ enum modes {
MODE_INITDB = (1 << 0),
MODE_REBUILDDB = (1 << 1),
MODE_VERIFYDB = (1 << 2),
MODE_EXPORTDB = (1 << 3),
MODE_IMPORTDB = (1 << 4),
};
static int mode = 0;
@ -25,6 +28,12 @@ static struct poptOption dbOptsTable[] = {
NULL},
{ "verifydb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR|POPT_ARGFLAG_DOC_HIDDEN),
&mode, MODE_VERIFYDB, N_("verify database files"), NULL},
{ "exportdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_EXPORTDB,
N_("export database to stdout header list"),
NULL},
{ "importdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_IMPORTDB,
N_("import database from stdin header list"),
NULL},
POPT_TABLEEND
};
@ -39,6 +48,47 @@ static struct poptOption optionsTable[] = {
POPT_TABLEEND
};
static int exportDB(rpmts ts)
{
FD_t fd = fdDup(STDOUT_FILENO);
rpmtxn txn = rpmtxnBegin(ts, RPMTXN_READ);
int rc = 0;
if (txn && fd) {
Header h;
rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
while ((h = rpmdbNextIterator(mi))) {
rc += headerWrite(fd, h, HEADER_MAGIC_YES);
}
rpmdbFreeIterator(mi);
} else {
rc = -1;
}
Fclose(fd);
rpmtxnEnd(txn);
return rc;
}
/* XXX: only allow this on empty db? */
static int importDB(rpmts ts)
{
FD_t fd = fdDup(STDIN_FILENO);
rpmtxn txn = rpmtxnBegin(ts, RPMTXN_WRITE);
int rc = 0;
if (txn && fd) {
Header h;
while ((h = headerRead(fd, HEADER_MAGIC_YES))) {
rc += rpmtsImportHeader(txn, h, 0);
}
} else {
rc = -1;
}
rpmtxnEnd(txn);
Fclose(fd);
return rc;
}
int main(int argc, char *argv[])
{
int ec = EXIT_FAILURE;
@ -66,6 +116,12 @@ int main(int argc, char *argv[])
case MODE_VERIFYDB:
ec = rpmtsVerifyDB(ts);
break;
case MODE_EXPORTDB:
ec = exportDB(ts);
break;
case MODE_IMPORTDB:
ec = importDB(ts);
break;
default:
argerror(_("only one major mode may be specified"));
}