rpm 4.11.90

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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;
59 rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
60 while ((h = rpmdbNextIterator(mi))) {
61 rc += headerWrite(fd, h, HEADER_MAGIC_YES);
62 }
63 rpmdbFreeIterator(mi);
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();
104 rpmtsSetRootDir(ts, rpmcliRootDir);
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 }
dbOptsTable
static struct poptOption dbOptsTable[]
Definition: rpmdb.c:23
rpmdbMatchIterator
struct rpmdbMatchIterator_s * rpmdbMatchIterator
Definition: rpmtypes.h:69
printUsage
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
MODE_VERIFYDB
Definition: rpmdb.c:16
optionsTable
static struct poptOption optionsTable[]
Definition: rpmdb.c:40
rpmtxnEnd
rpmtxn rpmtxnEnd(rpmtxn txn)
Destroy transaction (lock) handle.
rpmtxn
struct rpmtxn_s * rpmtxn
Definition: rpmtypes.h:72
debug.h
rpmtsFree
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
main
int main(int argc, char *argv[])
Definition: rpmdb.c:92
HEADER_MAGIC_YES
Definition: header.h:34
rpmdbFreeIterator
rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi)
Destroy rpm database iterator.
rpmcliInit
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
rpmtsRebuildDB
int rpmtsRebuildDB(rpmts ts)
Rebuild the database used by the transaction.
cliutils.h
rpmts
struct rpmts_s * rpmts
The main types involved in transaction manipulation.
Definition: rpmtypes.h:63
_
#define _(Text)
Definition: system.h:110
rpmtsInitIterator
rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmDbiTagVal rpmtag, const void *keyp, size_t keylen)
Return transaction database iterator.
rpmtsCreate
rpmts rpmtsCreate(void)
Create an empty transaction set.
rpmcliFini
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
rpmtsInitDB
int rpmtsInitDB(rpmts ts, int dbmode)
Initialize the database used by the transaction.
argerror
void argerror(const char *desc)
Definition: cliutils.c:19
FD_t
struct _FD_s * FD_t
RPM IO file descriptor type.
Definition: rpmtypes.h:98
rpmtsVerifyDB
int rpmtsVerifyDB(rpmts ts)
Verify the database used by the transaction.
modes
modes
Definition: rpmbuild.c:204
Header
struct headerToken_s * Header
RPM header and data retrieval types.
Definition: rpmtypes.h:24
rpmExpandNumeric
int rpmExpandNumeric(const char *arg)
Return macro expansion as a numeric value.
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
rpmtsImportHeader
rpmRC rpmtsImportHeader(rpmtxn txn, Header h, rpmFlags flags)
Import a header into the rpmdb.
headerRead
Header headerRead(FD_t fd, int magicp)
Read (and load) header from file handle.
exportDB
static int exportDB(rpmts ts)
Definition: rpmdb.c:51
rpmtsSetVSFlags
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
Set verify signatures flag(s).
rpmcliAllPoptTable
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.
mode
static int mode
Definition: rpmdb.c:21
MODE_REBUILDDB
Definition: rpmdb.c:15
fdDup
FD_t fdDup(int fdno)
system.h
MODE_EXPORTDB
Definition: rpmdb.c:17
importDB
static int importDB(rpmts ts)
Definition: rpmdb.c:73
N_
#define N_(Text)
Definition: system.h:113
RPMTXN_WRITE
Definition: rpmts.h:161
rpmdbNextIterator
Header rpmdbNextIterator(rpmdbMatchIterator mi)
Return next package header from iteration.
MODE_IMPORTDB
Definition: rpmdb.c:18
headerWrite
int headerWrite(FD_t fd, Header h, int magicp)
Write (with unload) header to file handle.
RPMDBI_PACKAGES
Definition: rpmtag.h:341
rpmtsSetRootDir
int rpmtsSetRootDir(rpmts ts, const char *rootDir)
Set transaction rootDir, i.e.
rpmtxnBegin
rpmtxn rpmtxnBegin(rpmts ts, rpmtxnFlags flags)
Create a transaction (lock) handle.
Fclose
int Fclose(FD_t fd)
fclose(3) clone.