rpmdb.c
Go to the documentation of this file.
#include "system.h"
#include <popt.h>
#include <rpm/rpmcli.h>
#include <rpm/rpmdb.h>
#include "cliutils.h"
#include "debug.h"
#if !defined(__GLIBC__) && !defined(__APPLE__)
char ** environ = NULL;
#endif
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;
static struct poptOption dbOptsTable[] = {
{ "initdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_INITDB,
N_("initialize database"), NULL},
{ "rebuilddb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_REBUILDDB,
N_("rebuild database inverted lists from installed package headers"),
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
};
static struct poptOption optionsTable[] = {
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, dbOptsTable, 0,
N_("Database options:"), NULL },
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
N_("Common options for all rpm modes and executables:"), NULL },
POPT_AUTOALIAS
POPT_AUTOHELP
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;
poptContext optCon = rpmcliInit(argc, argv, optionsTable);
rpmts ts = NULL;
if (argc < 2 || poptPeekArg(optCon)) {
printUsage(optCon, stderr, 0);
goto exit;
}
ts = rpmtsCreate();
rpmtsSetRootDir(ts, rpmcliRootDir);
switch (mode) {
case MODE_INITDB:
ec = rpmtsInitDB(ts, 0644);
break;
case MODE_REBUILDDB:
{ rpmVSFlags vsflags = rpmExpandNumeric("%{_vsflags_rebuilddb}");
rpmVSFlags ovsflags = rpmtsSetVSFlags(ts, vsflags);
ec = rpmtsRebuildDB(ts);
rpmtsSetVSFlags(ts, ovsflags);
} break;
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"));
}
exit:
rpmtsFree(ts);
rpmcliFini(optCon);
return ec;
}
dbOptsTable
static struct poptOption dbOptsTable[]
Definition: rpmdb.c:23
rpmtsSetRootDir
int rpmtsSetRootDir(rpmts ts, const char *rootDir)
Set transaction rootDir, i.e.
printUsage
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
MODE_VERIFYDB
Definition: rpmdb.c:16
rpmtsInitDB
int rpmtsInitDB(rpmts ts, int dbmode)
Initialize the database used by the transaction.
rpmtsInitIterator
rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmDbiTagVal rpmtag, const void *keyp, size_t keylen)
Return transaction database iterator.
optionsTable
static struct poptOption optionsTable[]
Definition: rpmdb.c:40
RPMDBI_PACKAGES
Definition: rpmtag.h:377
FD_t
struct _FD_s * FD_t
RPM IO file descriptor type.
Definition: rpmtypes.h:98
rpmtxnEnd
rpmtxn rpmtxnEnd(rpmtxn txn)
Destroy transaction (lock) handle.
debug.h
rpmtsImportHeader
rpmRC rpmtsImportHeader(rpmtxn txn, Header h, rpmFlags flags)
Import a header into the rpmdb.
main
int main(int argc, char *argv[])
Definition: rpmdb.c:92
rpmcliInit
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
rpmcliFini
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
rpmtxnBegin
rpmtxn rpmtxnBegin(rpmts ts, rpmtxnFlags flags)
Create a transaction (lock) handle.
cliutils.h
Misc helpers for RPM CLI tools.
rpmdbNextIterator
Header rpmdbNextIterator(rpmdbMatchIterator mi)
Return next package header from iteration.
HEADER_MAGIC_YES
Definition: header.h:34
_
#define _(Text)
Definition: system.h:112
rpmtxn
struct rpmtxn_s * rpmtxn
Definition: rpmtypes.h:72
fdDup
FD_t fdDup(int fdno)
rpmdbFreeIterator
rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
Destroy rpm database iterator.
rpmdbMatchIterator
struct rpmdbMatchIterator_s * rpmdbMatchIterator
Definition: rpmtypes.h:69
argerror
void argerror(const char *desc)
Definition: cliutils.c:19
headerWrite
int headerWrite(FD_t fd, Header h, int magicp)
Write (with unload) header to file handle.
rpmtsVerifyDB
int rpmtsVerifyDB(rpmts ts)
Verify the database used by the transaction.
modes
modes
Definition: rpmbuild.c:250
rpmExpandNumeric
int rpmExpandNumeric(const char *arg)
Return macro expansion as a numeric value.
Fclose
int Fclose(FD_t fd)
fclose(3) clone.
RPMTXN_READ
Definition: rpmts.h:160
rpmVSFlags
rpmFlags rpmVSFlags
Definition: rpmts.h:110
environ
char ** environ
Definition: rpmdb.c:10
MODE_INITDB
Definition: rpmdb.c:14
rpmcliRootDir
const char * rpmcliRootDir
exportDB
static int exportDB(rpmts ts)
Definition: rpmdb.c:51
mode
static int mode
Definition: rpmdb.c:21
MODE_REBUILDDB
Definition: rpmdb.c:15
rpmtsFree
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
rpmtsSetVSFlags
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
Set verify signatures flag(s).
rpmts
struct rpmts_s * rpmts
The main types involved in transaction manipulation.
Definition: rpmtypes.h:63
system.h
Some misc low-level API.
MODE_EXPORTDB
Definition: rpmdb.c:17
headerRead
Header headerRead(FD_t fd, int magicp)
Read (and load) header from file handle.
importDB
static int importDB(rpmts ts)
Definition: rpmdb.c:73
N_
#define N_(Text)
Definition: system.h:115
rpmtsRebuildDB
int rpmtsRebuildDB(rpmts ts)
Rebuild the database used by the transaction.
RPMTXN_WRITE
Definition: rpmts.h:161
rpmtsCreate
rpmts rpmtsCreate(void)
Create an empty transaction set.
MODE_IMPORTDB
Definition: rpmdb.c:18
rpmcliAllPoptTable
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.
Header
struct headerToken_s * Header
RPM header and data retrieval types.
Definition: rpmtypes.h:24