csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
#include "system.h"
2ff057
2ff057
#include <popt.h>
2ff057
#include <rpm/rpmcli.h>
2ff057
#include <rpm/rpmdb.h>
2ff057
#include "cliutils.h"
2ff057
#include "debug.h"
2ff057
2ff057
enum modes {
2ff057
    MODE_INITDB		= (1 << 0),
2ff057
    MODE_REBUILDDB	= (1 << 1),
2ff057
    MODE_VERIFYDB	= (1 << 2),
2ff057
    MODE_EXPORTDB	= (1 << 3),
2ff057
    MODE_IMPORTDB	= (1 << 4),
2ff057
};
2ff057
2ff057
static int mode = 0;
2ff057
2ff057
static struct poptOption dbOptsTable[] = {
2ff057
    { "initdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_INITDB,
2ff057
	N_("initialize database"), NULL},
2ff057
    { "rebuilddb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_REBUILDDB,
2ff057
	N_("rebuild database inverted lists from installed package headers"),
2ff057
	NULL},
2ff057
    { "verifydb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR|POPT_ARGFLAG_DOC_HIDDEN),
2ff057
	&mode, MODE_VERIFYDB, N_("verify database files"), NULL},
2ff057
    { "exportdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_EXPORTDB,
2ff057
	N_("export database to stdout header list"),
2ff057
	NULL},
2ff057
    { "importdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_IMPORTDB,
2ff057
	N_("import database from stdin header list"),
2ff057
	NULL},
2ff057
    POPT_TABLEEND
2ff057
};
2ff057
2ff057
static struct poptOption optionsTable[] = {
2ff057
    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, dbOptsTable, 0,
2ff057
	N_("Database options:"), NULL },
2ff057
    { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
2ff057
	N_("Common options for all rpm modes and executables:"), NULL },
2ff057
2ff057
    POPT_AUTOALIAS
2ff057
    POPT_AUTOHELP
2ff057
    POPT_TABLEEND
2ff057
};
2ff057
2ff057
static int exportDB(rpmts ts)
2ff057
{
2ff057
    FD_t fd = fdDup(STDOUT_FILENO);
2ff057
    rpmtxn txn = rpmtxnBegin(ts, RPMTXN_READ);
2ff057
    int rc = 0;
2ff057
2ff057
    if (txn && fd) {
2ff057
	Header h;
2ff057
	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
2ff057
	while ((h = rpmdbNextIterator(mi))) {
2ff057
	    rc += headerWrite(fd, h, HEADER_MAGIC_YES);
2ff057
	}
2ff057
	rpmdbFreeIterator(mi);
2ff057
    } else {
2ff057
	rc = -1;
2ff057
    }
2ff057
    Fclose(fd);
2ff057
    rpmtxnEnd(txn);
2ff057
    return rc;
2ff057
}
2ff057
2ff057
/* XXX: only allow this on empty db? */
2ff057
static int importDB(rpmts ts)
2ff057
{
2ff057
    FD_t fd = fdDup(STDIN_FILENO);
2ff057
    rpmtxn txn = rpmtxnBegin(ts, RPMTXN_WRITE);
2ff057
    int rc = 0;
2ff057
2ff057
    if (txn && fd) {
2ff057
	Header h;
2ff057
	while ((h = headerRead(fd, HEADER_MAGIC_YES))) {
2ff057
	    rc += rpmtsImportHeader(txn, h, 0);
2ff057
	}
2ff057
    } else {
2ff057
	rc = -1;
2ff057
    }
2ff057
    rpmtxnEnd(txn);
2ff057
    Fclose(fd);
2ff057
    return rc;
2ff057
}
2ff057
2ff057
int main(int argc, char *argv[])
2ff057
{
2ff057
    int ec = EXIT_FAILURE;
2ff057
    poptContext optCon = NULL;
2ff057
    rpmts ts = NULL;
2ff057
2ff057
    xsetprogname(argv[0]); /* Portability call -- see system.h */
2ff057
2ff057
    optCon = rpmcliInit(argc, argv, optionsTable);
2ff057
2ff057
    if (argc < 2 || poptPeekArg(optCon)) {
2ff057
	printUsage(optCon, stderr, 0);
2ff057
	goto exit;
2ff057
    }
2ff057
2ff057
    ts = rpmtsCreate();
2ff057
    rpmtsSetRootDir(ts, rpmcliRootDir);
2ff057
2ff057
    switch (mode) {
2ff057
    case MODE_INITDB:
2ff057
	ec = rpmtsInitDB(ts, 0644);
2ff057
	break;
2ff057
    case MODE_REBUILDDB:
2ff057
    {   rpmVSFlags vsflags = rpmExpandNumeric("%{_vsflags_rebuilddb}");
2ff057
	rpmVSFlags ovsflags = rpmtsSetVSFlags(ts, vsflags);
2ff057
	ec = rpmtsRebuildDB(ts);
2ff057
	rpmtsSetVSFlags(ts, ovsflags);
2ff057
    }	break;
2ff057
    case MODE_VERIFYDB:
2ff057
	ec = rpmtsVerifyDB(ts);
2ff057
	break;
2ff057
    case MODE_EXPORTDB:
2ff057
	ec = exportDB(ts);
2ff057
	break;
2ff057
    case MODE_IMPORTDB:
2ff057
	ec = importDB(ts);
2ff057
	break;
2ff057
    default:
2ff057
	argerror(_("only one major mode may be specified"));
2ff057
    }
2ff057
2ff057
exit:
2ff057
    rpmtsFree(ts);
2ff057
    rpmcliFini(optCon);
2ff057
    return ec;
2ff057
}