rpm  4.11.3
rpmqv.c
Go to the documentation of this file.
1 #include "system.h"
2 const char *__progname;
3 
4 #include <rpm/rpmcli.h>
5 #include <rpm/rpmlib.h> /* RPMSIGTAG, rpmReadPackageFile .. */
6 #include <rpm/rpmlog.h>
7 #include <rpm/rpmps.h>
8 #include <rpm/rpmts.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 
12 #include "cliutils.h"
13 
14 #include "debug.h"
15 
16 #if defined(IAM_RPMQ) || defined(IAM_RPMV)
17 #define IAM_RPMQV
18 #endif
19 
20 enum modes {
21 
22  MODE_QUERY = (1 << 0),
23  MODE_VERIFY = (1 << 3),
24 #define MODES_QV (MODE_QUERY | MODE_VERIFY)
25 
26  MODE_INSTALL = (1 << 1),
27  MODE_ERASE = (1 << 2),
28 #define MODES_IE (MODE_INSTALL | MODE_ERASE)
29 
31 };
32 
33 #define MODES_FOR_NODEPS (MODES_IE | MODE_VERIFY)
34 #define MODES_FOR_TEST (MODES_IE)
35 
36 static int quiet;
37 
38 /* the structure describing the options we take and the defaults */
39 static struct poptOption optionsTable[] = {
40 
41 #ifdef IAM_RPMQV
42  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmQVSourcePoptTable, 0,
43  N_("Query/Verify package selection options:"),
44  NULL },
45 #endif
46 #ifdef IAM_RPMQ
47  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmQueryPoptTable, 0,
48  N_("Query options (with -q or --query):"),
49  NULL },
50 #endif
51 #ifdef IAM_RPMV
52  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmVerifyPoptTable, 0,
53  N_("Verify options (with -V or --verify):"),
54  NULL },
55 #endif
56 
57 #ifdef IAM_RPMEIU
58  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmInstallPoptTable, 0,
59  N_("Install/Upgrade/Erase options:"),
60  NULL },
61 #endif /* IAM_RPMEIU */
62 
63  { "quiet", '\0', POPT_ARGFLAG_DOC_HIDDEN, &quiet, 0, NULL, NULL},
64 
65  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
66  N_("Common options for all rpm modes and executables:"),
67  NULL },
68 
69  POPT_AUTOALIAS
70  POPT_AUTOHELP
71  POPT_TABLEEND
72 };
73 
74 int main(int argc, char *argv[])
75 {
76  rpmts ts = NULL;
77  enum modes bigMode = MODE_UNKNOWN;
78 
79 #if defined(IAM_RPMQV)
80  QVA_t qva = &rpmQVKArgs;
81 #endif
82 
83 #ifdef IAM_RPMEIU
84  struct rpmInstallArguments_s * ia = &rpmIArgs;
85 #endif
86 
87  poptContext optCon;
88  int ec = 0;
89 #ifdef IAM_RPMEIU
90  int i;
91 #endif
92 
93  optCon = rpmcliInit(argc, argv, optionsTable);
94 
95  /* Set the major mode based on argv[0] */
96 #ifdef IAM_RPMQV
97  if (rstreq(__progname, "rpmquery")) bigMode = MODE_QUERY;
98  if (rstreq(__progname, "rpmverify")) bigMode = MODE_VERIFY;
99 #endif
100 
101 #if defined(IAM_RPMQV)
102  /* Jumpstart option from argv[0] if necessary. */
103  switch (bigMode) {
104  case MODE_QUERY: qva->qva_mode = 'q'; break;
105  case MODE_VERIFY: qva->qva_mode = 'V'; break;
106  case MODE_INSTALL:
107  case MODE_ERASE:
108  case MODE_UNKNOWN:
109  default:
110  break;
111  }
112 #endif
113 
114 #ifdef IAM_RPMQV
115  if (bigMode == MODE_UNKNOWN || (bigMode & MODES_QV)) {
116  switch (qva->qva_mode) {
117  case 'q': bigMode = MODE_QUERY; break;
118  case 'V': bigMode = MODE_VERIFY; break;
119  }
120 
121  if (qva->qva_sourceCount) {
122  if (qva->qva_sourceCount > 1)
123  argerror(_("one type of query/verify may be performed at a "
124  "time"));
125  }
126  if (qva->qva_flags && (bigMode & ~MODES_QV))
127  argerror(_("unexpected query flags"));
128 
129  if (qva->qva_queryFormat && (bigMode & ~MODES_QV))
130  argerror(_("unexpected query format"));
131 
132  if (qva->qva_source != RPMQV_PACKAGE && (bigMode & ~MODES_QV))
133  argerror(_("unexpected query source"));
134  }
135 #endif /* IAM_RPMQV */
136 
137 #ifdef IAM_RPMEIU
138  if (bigMode == MODE_UNKNOWN || (bigMode & MODES_IE))
139  { int iflags = (ia->installInterfaceFlags &
142  int eflags = (ia->installInterfaceFlags & INSTALL_ERASE);
143 
144  if (iflags & eflags)
145  argerror(_("only one major mode may be specified"));
146  else if (iflags)
147  bigMode = MODE_INSTALL;
148  else if (eflags)
149  bigMode = MODE_ERASE;
150  }
151 #endif /* IAM_RPMEIU */
152 
153 #if defined(IAM_RPMEIU)
154  if (!( bigMode == MODE_INSTALL ) &&
156  argerror(_("only installation and upgrading may be forced"));
157  if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_FORCERELOCATE))
158  argerror(_("files may only be relocated during package installation"));
159 
160  if (ia->relocations && ia->prefix)
161  argerror(_("cannot use --prefix with --relocate or --excludepath"));
162 
163  if (bigMode != MODE_INSTALL && ia->relocations)
164  argerror(_("--relocate and --excludepath may only be used when installing new packages"));
165 
166  if (bigMode != MODE_INSTALL && ia->prefix)
167  argerror(_("--prefix may only be used when installing new packages"));
168 
169  if (ia->prefix && ia->prefix[0] != '/')
170  argerror(_("arguments to --prefix must begin with a /"));
171 
172  if (!(bigMode & MODES_IE) && (ia->installInterfaceFlags & INSTALL_HASH))
173  argerror(_("--hash (-h) may only be specified during package "
174  "installation and erasure"));
175 
176  if (!(bigMode & MODES_IE) && (ia->installInterfaceFlags & INSTALL_PERCENT))
177  argerror(_("--percent may only be specified during package "
178  "installation and erasure"));
179 
180  if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_REPLACEPKG))
181  argerror(_("--replacepkgs may only be specified during package "
182  "installation"));
183 
184  if (bigMode != MODE_INSTALL && (ia->transFlags & RPMTRANS_FLAG_NODOCS))
185  argerror(_("--excludedocs may only be specified during package "
186  "installation"));
187 
188  if (bigMode != MODE_INSTALL && ia->incldocs)
189  argerror(_("--includedocs may only be specified during package "
190  "installation"));
191 
192  if (ia->incldocs && (ia->transFlags & RPMTRANS_FLAG_NODOCS))
193  argerror(_("only one of --excludedocs and --includedocs may be "
194  "specified"));
195 
196  if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_IGNOREARCH))
197  argerror(_("--ignorearch may only be specified during package "
198  "installation"));
199 
200  if (bigMode != MODE_INSTALL && (ia->probFilter & RPMPROB_FILTER_IGNOREOS))
201  argerror(_("--ignoreos may only be specified during package "
202  "installation"));
203 
204  if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE &&
206  argerror(_("--ignoresize may only be specified during package "
207  "installation"));
208 
209  if ((ia->installInterfaceFlags & UNINSTALL_ALLMATCHES) && bigMode != MODE_ERASE)
210  argerror(_("--allmatches may only be specified during package "
211  "erasure"));
212 
213  if ((ia->transFlags & RPMTRANS_FLAG_ALLFILES) && bigMode != MODE_INSTALL)
214  argerror(_("--allfiles may only be specified during package "
215  "installation"));
216 
217  if ((ia->transFlags & RPMTRANS_FLAG_JUSTDB) &&
218  bigMode != MODE_INSTALL && bigMode != MODE_ERASE)
219  argerror(_("--justdb may only be specified during package "
220  "installation and erasure"));
221 
222  if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE && bigMode != MODE_VERIFY &&
224  argerror(_("script disabling options may only be specified during "
225  "package installation and erasure"));
226 
227  if (bigMode != MODE_INSTALL && bigMode != MODE_ERASE && bigMode != MODE_VERIFY &&
229  argerror(_("trigger disabling options may only be specified during "
230  "package installation and erasure"));
231 
232  if (ia->noDeps & (bigMode & ~MODES_FOR_NODEPS))
233  argerror(_("--nodeps may only be specified during package "
234  "installation, erasure, and verification"));
235 
236  if ((ia->transFlags & RPMTRANS_FLAG_TEST) && (bigMode & ~MODES_FOR_TEST))
237  argerror(_("--test may only be specified during package installation "
238  "and erasure"));
239 #endif /* IAM_RPMEIU */
240 
241  if (rpmcliRootDir && rpmcliRootDir[0] != '/') {
242  argerror(_("arguments to --root (-r) must begin with a /"));
243  }
244 
245  if (quiet)
247 
248  if (rpmcliPipeOutput && initPipe())
249  exit(EXIT_FAILURE);
250 
251  ts = rpmtsCreate();
252  (void) rpmtsSetRootDir(ts, rpmcliRootDir);
253  switch (bigMode) {
254 #ifdef IAM_RPMEIU
255  uid_t uid;
256  case MODE_ERASE:
257  /*root to uninstall package */
258  uid=getuid();
259  if(0==uid){
260  }else{
261  printf("Use root to operate.\n");
262  break;
263  }
265 
266  if (!poptPeekArg(optCon)) {
267  argerror(_("no packages given for erase"));
268  } else {
269  ec += rpmErase(ts, ia, (ARGV_const_t) poptGetArgs(optCon));
270  }
271  break;
272 
273  case MODE_INSTALL:
274 
275  /* RPMTRANS_FLAG_KEEPOBSOLETE */
276  /*root to install package*/
277  uid=getuid();
278  if(0==uid){
279  }else{
280  printf("Use root to operate.\n");
281  break;
282  }
283  if (!ia->incldocs) {
284  if (ia->transFlags & RPMTRANS_FLAG_NODOCS) {
285  ;
286  } else if (rpmExpandNumeric("%{_excludedocs}"))
288  }
289 
291 
292  /* we've already ensured !(!ia->prefix && !ia->relocations) */
293  if (ia->prefix) {
294  ia->relocations = xmalloc(2 * sizeof(*ia->relocations));
295  ia->relocations[0].oldPath = NULL; /* special case magic */
296  ia->relocations[0].newPath = ia->prefix;
297  ia->relocations[1].oldPath = NULL;
298  ia->relocations[1].newPath = NULL;
299  } else if (ia->relocations) {
300  ia->relocations = xrealloc(ia->relocations,
301  sizeof(*ia->relocations) * (ia->numRelocations + 1));
302  ia->relocations[ia->numRelocations].oldPath = NULL;
303  ia->relocations[ia->numRelocations].newPath = NULL;
304  }
305 
306  if (!poptPeekArg(optCon)) {
307  argerror(_("no packages given for install"));
308  } else {
309  /* FIX: ia->relocations[0].newPath undefined */
310  ec += rpmInstall(ts, ia, (ARGV_t) poptGetArgs(optCon));
311  }
312  break;
313 
314 #endif /* IAM_RPMEIU */
315 
316 #ifdef IAM_RPMQV
317  case MODE_QUERY:
318  if (!poptPeekArg(optCon) && !(qva->qva_source == RPMQV_ALL))
319  argerror(_("no arguments given for query"));
320 
321  ec = rpmcliQuery(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
322  break;
323 
324  case MODE_VERIFY:
325  { rpmVerifyFlags verifyFlags = VERIFY_ALL;
326 
327  verifyFlags &= ~qva->qva_flags;
328  qva->qva_flags = (rpmQueryFlags) verifyFlags;
329 
330  if (!poptPeekArg(optCon) && !(qva->qva_source == RPMQV_ALL))
331  argerror(_("no arguments given for verify"));
332  ec = rpmcliVerify(ts, qva, (ARGV_const_t) poptGetArgs(optCon));
333  } break;
334 #endif /* IAM_RPMQV */
335 
336 #if !defined(IAM_RPMQV)
337  case MODE_QUERY:
338  case MODE_VERIFY:
339 #endif
340 #if !defined(IAM_RPMEIU)
341  case MODE_INSTALL:
342  case MODE_ERASE:
343 #endif
344  case MODE_UNKNOWN:
345  if (poptPeekArg(optCon) != NULL || argc <= 1 || rpmIsVerbose()) {
346  printUsage(optCon, stderr, 0);
347  ec = argc;
348  }
349  break;
350  }
351 
352  rpmtsFree(ts);
353  if (finishPipe())
354  ec = EXIT_FAILURE;
355 
356 #ifdef IAM_RPMQV
357  free(qva->qva_queryFormat);
358 #endif
359 
360 #ifdef IAM_RPMEIU
361  if (ia->relocations != NULL) {
362  for (i = 0; i < ia->numRelocations; i++)
363  free(ia->relocations[i].oldPath);
364  free(ia->relocations);
365  }
366 #endif
367 
368  rpmcliFini(optCon);
369 
370  return RETVAL(ec);
371 }
#define RETVAL(rc)
Definition: cliutils.h:9
void printUsage(poptContext con, FILE *fp, int flags)
Definition: cliutils.c:36
rpmFlags rpmVerifyFlags
Definition: rpmvf.h:77
int qva_sourceCount
Definition: rpmcli.h:170
#define xmalloc(_size)
Definition: system.h:108
char ** ARGV_t
Definition: argv.h:15
char * oldPath
Definition: rpmfi.h:95
rpmprobFilterFlags probFilter
Definition: rpmcli.h:348
static struct poptOption optionsTable[]
Definition: rpmqv.c:39
rpmts rpmtsFree(rpmts ts)
Destroy transaction set, closing the database as well.
int rpmErase(rpmts ts, struct rpmInstallArguments_s *ia, ARGV_const_t argv)
Erase binary rpm package.
rpmQueryFlags qva_flags
Definition: rpmcli.h:171
char * newPath
Definition: rpmfi.h:96
#define MODES_FOR_NODEPS
Definition: rpmqv.c:33
poptContext rpmcliInit(int argc, char *const argv[], struct poptOption *optionsTable)
Initialize most everything needed by an rpm CLI executable context.
int rpmcliQuery(rpmts ts, QVA_t qva, ARGV_const_t argv)
Display package information.
rpmRelocation * relocations
Definition: rpmcli.h:353
struct rpmts_s * rpmts
The main types involved in transaction manipulation.
Definition: rpmtypes.h:63
#define _noTransScripts
Definition: rpmts.h:59
int finishPipe(void)
Definition: cliutils.c:73
#define _(Text)
Definition: system.h:135
Describe query/verify/signature command line operation.
Definition: rpmcli.h:168
#define rpmIsVerbose()
Definition: rpmlog.h:272
#define MODES_QV
Definition: rpmqv.c:24
#define MODES_FOR_TEST
Definition: rpmqv.c:34
#define _noTransTriggers
Definition: rpmts.h:66
struct rpmQVKArguments_s rpmQVKArgs
rpmts rpmtsCreate(void)
Create an empty transaction set.
struct poptOption rpmInstallPoptTable[]
poptContext rpmcliFini(poptContext optCon)
Destroy most everything needed by an rpm CLI executable context.
const char * rpmcliPipeOutput
void argerror(const char *desc)
Definition: cliutils.c:19
struct poptOption rpmQueryPoptTable[]
rpmFlags rpmQueryFlags
Definition: rpmcli.h:130
int main(int argc, char *argv[])
Definition: rpm2cpio.c:15
static int rstreq(const char *s1, const char *s2)
Test for string equality.
Definition: rpmstring.h:113
rpmtransFlags transFlags
Definition: rpmcli.h:347
modes
Definition: rpmbuild.c:205
#define VERIFY_ALL
Definition: rpmvf.h:82
struct poptOption rpmQVSourcePoptTable[]
rpmQVSources qva_source
Definition: rpmcli.h:169
int rpmcliVerify(rpmts ts, QVA_t qva, ARGV_const_t argv)
Verify package install.
int initPipe(void)
Definition: cliutils.c:48
int rpmExpandNumeric(const char *arg)
Return macro expansion as a numeric value.
rpmInstallFlags installInterfaceFlags
Definition: rpmcli.h:349
struct rpmInstallArguments_s rpmIArgs
static int quiet
Definition: rpmqv.c:36
const char * rpmcliRootDir
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.
char * qva_queryFormat
Definition: rpmcli.h:176
char *const * ARGV_const_t
Definition: argv.h:16
#define UNINSTALL_ALLMATCHES
Definition: rpmcli.h:307
Describe database command line requests.
Definition: rpmcli.h:346
struct poptOption rpmVerifyPoptTable[]
#define __progname
Definition: system.h:121
int rpmInstall(rpmts ts, struct rpmInstallArguments_s *ia, ARGV_t fileArgv)
Install/upgrade/freshen/reinstall binary rpm package.
#define N_(Text)
Definition: system.h:138
#define rpmSetVerbosity(_lvl)
Definition: rpmlog.h:264
#define UNINSTALL_NODEPS
Definition: rpmcli.h:306
#define MODES_IE
Definition: rpmqv.c:28
int rpmtsSetRootDir(rpmts ts, const char *rootDir)
Set transaction rootDir, i.e.
#define xrealloc(_ptr, _size)
Definition: system.h:110