rpm  4.12.0.1
rpmdb.c
Go to the documentation of this file.
1 #include "system.h"
2 
3 #include <popt.h>
4 #include <rpm/rpmcli.h>
5 #include <rpm/rpmdb.h>
6 #include "cliutils.h"
7 #include "debug.h"
8 
9 #if !defined(__GLIBC__) && !defined(__APPLE__)
10 char ** environ = NULL;
11 #endif
12 
13 enum modes {
14  MODE_INITDB = (1 << 0),
15  MODE_REBUILDDB = (1 << 1),
16  MODE_VERIFYDB = (1 << 2),
17  MODE_EXPORTDB = (1 << 3),
18  MODE_IMPORTDB = (1 << 4),
19 };
20 
21 static int mode = 0;
22 
23 static struct poptOption dbOptsTable[] = {
24  { "initdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_INITDB,
25  N_("initialize database"), NULL},
26  { "rebuilddb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_REBUILDDB,
27  N_("rebuild database inverted lists from installed package headers"),
28  NULL},
29  { "verifydb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR|POPT_ARGFLAG_DOC_HIDDEN),
30  &mode, MODE_VERIFYDB, N_("verify database files"), NULL},
31  { "exportdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_EXPORTDB,
32  N_("export database to stdout header list"),
33  NULL},
34  { "importdb", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_OR), &mode, MODE_IMPORTDB,
35  N_("import database from stdin header list"),
36  NULL},
37  POPT_TABLEEND
38 };
39 
40 static struct poptOption optionsTable[] = {
41  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, dbOptsTable, 0,
42  N_("Database options:"), NULL },
43  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
44  N_("Common options for all rpm modes and executables:"), NULL },
45 
46  POPT_AUTOALIAS
47  POPT_AUTOHELP
48  POPT_TABLEEND
49 };
50 
51 static int exportDB(rpmts ts)
52 {
53  FD_t fd = fdDup(STDOUT_FILENO);
54  rpmtxn txn = rpmtxnBegin(ts, RPMTXN_READ);
55  int rc = 0;
56 
57  if (txn && fd) {
58  Header h;
60  while ((h = rpmdbNextIterator(mi))) {
61  rc += headerWrite(fd, h, HEADER_MAGIC_YES);
62  }
64  } else {
65  rc = -1;
66  }
67  Fclose(fd);
68  rpmtxnEnd(txn);
69  return rc;
70 }
71 
72 /* XXX: only allow this on empty db? */
73 static int importDB(rpmts ts)
74 {
75  FD_t fd = fdDup(STDIN_FILENO);
76  rpmtxn txn = rpmtxnBegin(ts, RPMTXN_WRITE);
77  int rc = 0;
78 
79  if (txn && fd) {
80  Header h;
81  while ((h = headerRead(fd, HEADER_MAGIC_YES))) {
82  rc += rpmtsImportHeader(txn, h, 0);
83  }
84  } else {
85  rc = -1;
86  }
87  rpmtxnEnd(txn);
88  Fclose(fd);
89  return rc;
90 }
91 
92 int main(int argc, char *argv[])
93 {
94  int ec = EXIT_FAILURE;
95  poptContext optCon = rpmcliInit(argc, argv, optionsTable);
96  rpmts ts = NULL;
97 
98  if (argc < 2 || poptPeekArg(optCon)) {
99  printUsage(optCon, stderr, 0);
100  goto exit;
101  }
102 
103  ts = rpmtsCreate();
105 
106  switch (mode) {
107  case MODE_INITDB:
108  ec = rpmtsInitDB(ts, 0644);
109  break;
110  case MODE_REBUILDDB:
111  { rpmVSFlags vsflags = rpmExpandNumeric("%{_vsflags_rebuilddb}");
112  rpmVSFlags ovsflags = rpmtsSetVSFlags(ts, vsflags);
113  ec = rpmtsRebuildDB(ts);
114  rpmtsSetVSFlags(ts, ovsflags);
115  } break;
116  case MODE_VERIFYDB:
117  ec = rpmtsVerifyDB(ts);
118  break;
119  case MODE_EXPORTDB:
120  ec = exportDB(ts);
121  break;
122  case MODE_IMPORTDB:
123  ec = importDB(ts);
124  break;
125  default:
126  argerror(_("only one major mode may be specified"));
127  }
128 
129 exit:
130  rpmtsFree(ts);
131  rpmcliFini(optCon);
132  return ec;
133 }
static struct poptOption dbOptsTable[]
Definition: rpmdb.c:23
struct rpmdbMatchIterator_s * rpmdbMatchIterator
Definition: rpmtypes.h:69
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
static struct poptOption optionsTable[]
Definition: rpmdb.c:40
rpmtxn rpmtxnEnd(rpmtxn txn)
Destroy transaction (lock) handle.
struct rpmtxn_s * rpmtxn
Definition: rpmtypes.h:72
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
int main(int argc, char *argv[])
Definition: rpmdb.c:92
rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
Destroy rpm database iterator.
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
int rpmtsRebuildDB(rpmts ts)
Rebuild the database used by the transaction.
struct rpmts_s * rpmts
The main types involved in transaction manipulation.
Definition: rpmtypes.h:63
#define _(Text)
Definition: system.h:110
rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmDbiTagVal rpmtag, const void *keyp, size_t keylen)
Return transaction database iterator.
rpmts rpmtsCreate(void)
Create an empty transaction set.
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
int rpmtsInitDB(rpmts ts, int dbmode)
Initialize the database used by the transaction.
void argerror(const char *desc)
Definition: cliutils.c:19
struct _FD_s * FD_t
RPM IO file descriptor type.
Definition: rpmtypes.h:98
int rpmtsVerifyDB(rpmts ts)
Verify the database used by the transaction.
modes
Definition: rpmbuild.c:204
struct headerToken_s * Header
RPM header and data retrieval types.
Definition: rpmtypes.h:24
int rpmExpandNumeric(const char *arg)
Return macro expansion as a numeric value.
rpmFlags rpmVSFlags
Definition: rpmts.h:110
char ** environ
Definition: rpmdb.c:10
const char * rpmcliRootDir
rpmRC rpmtsImportHeader(rpmtxn txn, Header h, rpmFlags flags)
Import a header into the rpmdb.
Header headerRead(FD_t fd, int magicp)
Read (and load) header from file handle.
static int exportDB(rpmts ts)
Definition: rpmdb.c:51
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
Set verify signatures flag(s).
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.
static int mode
Definition: rpmdb.c:21
FD_t fdDup(int fdno)
static int importDB(rpmts ts)
Definition: rpmdb.c:73
#define N_(Text)
Definition: system.h:113
Header rpmdbNextIterator(rpmdbMatchIterator mi)
Return next package header from iteration.
int headerWrite(FD_t fd, Header h, int magicp)
Write (with unload) header to file handle.
int rpmtsSetRootDir(rpmts ts, const char *rootDir)
Set transaction rootDir, i.e.
rpmtxn rpmtxnBegin(rpmts ts, rpmtxnFlags flags)
Create a transaction (lock) handle.
int Fclose(FD_t fd)
fclose(3) clone.