1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/** \ingroup rpmcli
* \file lib/query.c
* Display tag values from package metadata.
*/
#include "system.h"
#include <inttypes.h>
#include <rpm/rpmcli.h>
#include <rpm/header.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmfi.h>
#include <rpm/rpmgi.h>
#include <rpm/rpmts.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmfileutil.h> /* rpmCleanPath */
#include "lib/manifest.h"
#include "debug.h"
/**
*/
static void printFileInfo(const char * name,
rpm_loff_t size, unsigned short mode,
unsigned int mtime,
unsigned short rdev, unsigned int nlink,
const char * owner, const char * group,
const char * linkto)
{
char sizefield[21];
char ownerfield[8+1], groupfield[8+1];
char timefield[100];
time_t when = mtime; /* important if sizeof(int32_t) ! sizeof(time_t) */
struct tm * tm;
static time_t now;
static struct tm nowtm;
char * perms = rpmPermsString(mode);
char *link = NULL;
/* On first call, grab snapshot of now */
if (now == 0) {
now = time(NULL);
tm = localtime(&now);
if (tm) nowtm = *tm; /* structure assignment */
}
rstrlcpy(ownerfield, owner, sizeof(ownerfield));
rstrlcpy(groupfield, group, sizeof(groupfield));
/* this is normally right */
snprintf(sizefield, sizeof(sizefield), "%20" PRIu64, size);
/* this knows too much about dev_t */
if (S_ISLNK(mode)) {
rasprintf(&link, "%s -> %s", name, linkto);
} else if (S_ISCHR(mode)) {
perms[0] = 'c';
snprintf(sizefield, sizeof(sizefield), "%3u, %3u", ((unsigned)(rdev >> 8) & 0xff),
((unsigned)rdev & 0xff));
} else if (S_ISBLK(mode)) {
perms[0] = 'b';
snprintf(sizefield, sizeof(sizefield), "%3u, %3u", ((unsigned)(rdev >> 8) & 0xff),
((unsigned)rdev & 0xff));
}
/* Convert file mtime to display format */
tm = localtime(&when);
timefield[0] = '\0';
if (tm != NULL)
{ const char *fmt;
if (now > when + 6L * 30L * 24L * 60L * 60L || /* Old. */
now < when - 60L * 60L) /* In the future. */
{
/* The file is fairly old or in the future.
* POSIX says the cutoff is 6 months old;
* approximate this by 6*30 days.
* Allow a 1 hour slop factor for what is considered "the future",
* to allow for NFS server/client clock disagreement.
* Show the year instead of the time of day.
*/
fmt = "%b %e %Y";
} else {
fmt = "%b %e %H:%M";
}
(void)strftime(timefield, sizeof(timefield) - 1, fmt, tm);
}
rpmlog(RPMLOG_NOTICE, "%s %4d %-8s%-8s %10s %s %s\n", perms,
(int)nlink, ownerfield, groupfield, sizefield, timefield,
link ? link : name);
free(perms);
free(link);
}
int showQueryPackage(QVA_t qva, rpmts ts, Header h)
{
rpmfi fi = NULL;
rpmfiFlags fiflags = (RPMFI_NOHEADER | RPMFI_FLAGS_QUERY);
int rc = 0; /* XXX FIXME: need real return code */
if (qva->qva_queryFormat != NULL) {
const char *errstr;
char *str = headerFormat(h, qva->qva_queryFormat, &errstr);
if ( str != NULL ) {
rpmlog(RPMLOG_NOTICE, "%s", str);
free(str);
} else {
rpmlog(RPMLOG_ERR, _("incorrect format: %s\n"), errstr);
}
}
if (!(qva->qva_flags & QUERY_FOR_LIST))
goto exit;
if (!(qva->qva_flags & QUERY_FOR_DUMPFILES))
fiflags |= RPMFI_NOFILEDIGESTS;
fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, fiflags);
if (rpmfiFC(fi) <= 0) {
rpmlog(RPMLOG_NOTICE, _("(contains no files)\n"));
goto exit;
}
fi = rpmfiInit(fi, 0);
while (rpmfiNext(fi) >= 0) {
rpmfileAttrs fflags = rpmfiFFlags(fi);
rpm_mode_t fmode = rpmfiFMode(fi);
rpm_rdev_t frdev = rpmfiFRdev(fi);
rpm_time_t fmtime = rpmfiFMtime(fi);
rpmfileState fstate = rpmfiFState(fi);
rpm_loff_t fsize = rpmfiFSize(fi);
const char *fn = rpmfiFN(fi);
const char *fuser = rpmfiFUser(fi);
const char *fgroup = rpmfiFGroup(fi);
const char *flink = rpmfiFLink(fi);
char *buf = NULL;
/* If querying only docs, skip non-doc files. */
if ((qva->qva_flags & QUERY_FOR_DOCS) && !(fflags & RPMFILE_DOC))
continue;
/* If querying only configs, skip non-config files. */
if ((qva->qva_flags & QUERY_FOR_CONFIG) && !(fflags & RPMFILE_CONFIG))
continue;
/* If not querying %ghost, skip ghost files. */
if ((qva->qva_fflags & RPMFILE_GHOST) && (fflags & RPMFILE_GHOST))
continue;
if (qva->qva_flags & QUERY_FOR_STATE) {
switch (fstate) {
case RPMFILE_STATE_NORMAL:
rstrcat(&buf, _("normal "));
break;
case RPMFILE_STATE_REPLACED:
rstrcat(&buf, _("replaced "));
break;
case RPMFILE_STATE_NOTINSTALLED:
rstrcat(&buf, _("not installed "));
break;
case RPMFILE_STATE_NETSHARED:
rstrcat(&buf, _("net shared "));
break;
case RPMFILE_STATE_WRONGCOLOR:
rstrcat(&buf, _("wrong color "));
break;
case RPMFILE_STATE_MISSING:
rstrcat(&buf, _("(no state) "));
break;
default:
rasprintf(&buf, _("(unknown %3d) "), fstate);
break;
}
}
if (qva->qva_flags & QUERY_FOR_DUMPFILES) {
char *add, *fdigest;
fdigest = rpmfiFDigestHex(fi, NULL);
rasprintf(&add, "%s %" PRIu64 " %d %s 0%o ",
fn, fsize, fmtime, fdigest ? fdigest : "", fmode);
rstrcat(&buf, add);
free(add);
free(fdigest);
if (fuser && fgroup) {
rasprintf(&add, "%s %s", fuser, fgroup);
rstrcat(&buf, add);
free(add);
} else {
rpmlog(RPMLOG_ERR,
_("package has not file owner/group lists\n"));
}
rasprintf(&add, " %s %s %u %s",
fflags & RPMFILE_CONFIG ? "1" : "0",
fflags & RPMFILE_DOC ? "1" : "0",
frdev,
(flink && *flink ? flink : "X"));
rpmlog(RPMLOG_NOTICE, "%s%s\n", buf, add);
free(add);
} else
if (!rpmIsVerbose()) {
rpmlog(RPMLOG_NOTICE, "%s%s\n", buf ? buf : "", fn);
}
else {
uint32_t fnlink = rpmfiFNlink(fi);
/* XXX Adjust directory link count and size for display output. */
if (S_ISDIR(fmode)) {
fnlink++;
fsize = 0;
}
if (fuser && fgroup) {
if (buf) {
rpmlog(RPMLOG_NOTICE, "%s", buf);
}
printFileInfo(fn, fsize, fmode, fmtime, frdev, fnlink,
fuser, fgroup, flink);
} else {
rpmlog(RPMLOG_ERR,
_("package has neither file owner or id lists\n"));
}
}
free(buf);
}
rc = 0;
exit:
fi = rpmfiFree(fi);
return rc;
}
void rpmDisplayQueryTags(FILE * fp)
{
static const char * const tagTypeNames[] = {
"", "char", "int8", "int16", "int32", "int64",
"string", "blob", "argv", "i18nstring"
};
const char *tname, *sname;
rpmtd names = rpmtdNew();
(void) rpmTagGetNames(names, 1);
while ((tname = rpmtdNextString(names))) {
sname = tname + strlen("RPMTAG_");
if (rpmIsVerbose()) {
rpmTag tag = rpmTagGetValue(sname);
rpmTagType type = rpmTagGetType(tag) & RPM_MASK_TYPE;
fprintf(fp, "%-20s %6d", sname, tag);
if (type > RPM_NULL_TYPE && type <= RPM_MAX_TYPE)
fprintf(fp, " %s", tagTypeNames[type]);
} else {
fprintf(fp, "%s", sname);
}
fprintf(fp, "\n");
}
rpmtdFreeData(names);
rpmtdFree(names);
}
static int rpmgiShowMatches(QVA_t qva, rpmts ts)
{
rpmgi gi = qva->qva_gi;
int ec = 0;
while (rpmgiNext(gi) == RPMRC_OK) {
Header h;
int rc;
rpmdbCheckSignals();
h = rpmgiHeader(gi);
if (h == NULL) /* XXX perhaps stricter break instead? */
continue;
if ((rc = qva->qva_showPackage(qva, ts, h)) != 0)
ec = rc;
}
return ec + rpmgiNumErrors(gi);
}
int rpmcliShowMatches(QVA_t qva, rpmts ts)
{
Header h;
int ec = 0;
while ((h = rpmdbNextIterator(qva->qva_mi)) != NULL) {
int rc;
rpmdbCheckSignals();
if ((rc = qva->qva_showPackage(qva, ts, h)) != 0)
ec = rc;
}
qva->qva_mi = rpmdbFreeIterator(qva->qva_mi);
return ec;
}
int rpmQueryVerify(QVA_t qva, rpmts ts, const char * arg)
{
int res = 0;
const char * s;
int i;
int provides_checked = 0;
(void) rpmdbCheckSignals();
if (qva->qva_showPackage == NULL)
return 1;
switch (qva->qva_source) {
case RPMQV_RPM:
case RPMQV_ALL:
case RPMQV_HDLIST:
case RPMQV_FTSWALK:
res = rpmgiShowMatches(qva, ts);
break;
case RPMQV_SPECFILE:
res = ((qva->qva_specQuery != NULL)
? qva->qva_specQuery(ts, qva, arg) : 1);
break;
case RPMQV_GROUP:
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_GROUP, arg, 0);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE,
_("group %s does not contain any packages\n"), arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
break;
case RPMQV_TRIGGEREDBY:
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, arg, 0);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package triggers %s\n"), arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
break;
case RPMQV_PKGID:
{ unsigned char MD5[16];
unsigned char * t;
for (i = 0, s = arg; *s && isxdigit(*s); s++, i++)
{};
if (i != 32) {
rpmlog(RPMLOG_ERR, _("malformed %s: %s\n"), "pkgid", arg);
return 1;
}
MD5[0] = '\0';
for (i = 0, t = MD5, s = arg; i < 16; i++, t++, s += 2)
*t = (rnibble(s[0]) << 4) | rnibble(s[1]);
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_SIGMD5, MD5, sizeof(MD5));
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package matches %s: %s\n"),
"pkgid", arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
} break;
case RPMQV_HDRID:
for (i = 0, s = arg; *s && isxdigit(*s); s++, i++)
{};
if (i != 40) {
rpmlog(RPMLOG_ERR, _("malformed %s: %s\n"), "hdrid", arg);
return 1;
}
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_SHA1HEADER, arg, 0);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package matches %s: %s\n"),
"hdrid", arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
break;
case RPMQV_FILEID:
{ unsigned char *digest, *t;
size_t diglen;
for (i = 0, s = arg; *s && isxdigit(*s); s++, i++)
{};
/* XXX dunno the algorithm yet, just check we're in the ballpark */
if (i % 32 != 0 || i < 32 || i > 512) {
rpmlog(RPMLOG_ERR, _("malformed %s: %s\n"), "fileid", arg);
return 1;
}
diglen = i / 2;
digest = t = xcalloc(diglen, sizeof(*digest));
for (i = 0, s = arg; i < diglen; i++, t++, s += 2)
*t = (rnibble(s[0]) << 4) | rnibble(s[1]);
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_FILEDIGESTS, digest, diglen);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package matches %s: %s\n"),
"fileid", arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
free(digest);
} break;
case RPMQV_TID:
{ char * end = NULL;
rpm_tid_t iid = strtoul(arg, &end, 0);
if ((*end) || (end == arg) || (iid == UINT_MAX)) {
rpmlog(RPMLOG_ERR, _("malformed %s: %s\n"), "tid", arg);
return 1;
}
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_INSTALLTID, &iid, sizeof(iid));
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package matches %s: %s\n"),
"tid", arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
} break;
case RPMQV_WHATREQUIRES:
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_REQUIRENAME, arg, 0);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package requires %s\n"), arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
break;
case RPMQV_WHATPROVIDES:
if (arg[0] != '/') {
provides_checked = 1;
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_PROVIDENAME, arg, 0);
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_NOTICE, _("no package provides %s\n"), arg);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
break;
}
case RPMQV_PATH:
{ char * fn;
for (s = arg; *s != '\0'; s++)
if (!(*s == '.' || *s == '/'))
break;
if (*s == '\0') {
char fnbuf[PATH_MAX];
fn = realpath(arg, fnbuf);
fn = xstrdup( (fn != NULL ? fn : arg) );
} else if (*arg != '/') {
char *curDir = rpmGetCwd();
fn = (char *) rpmGetPath(curDir, "/", arg, NULL);
curDir = _free(curDir);
} else
fn = xstrdup(arg);
(void) rpmCleanPath(fn);
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_BASENAMES, fn, 0);
if (qva->qva_mi == NULL && !provides_checked)
qva->qva_mi = rpmtsInitIterator(ts, RPMTAG_PROVIDENAME, fn, 0);
if (qva->qva_mi == NULL) {
struct stat sb;
if (lstat(fn, &sb) != 0)
rpmlog(RPMLOG_ERR, _("file %s: %s\n"), fn, strerror(errno));
else
rpmlog(RPMLOG_NOTICE,
_("file %s is not owned by any package\n"), fn);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
fn = _free(fn);
} break;
case RPMQV_DBOFFSET:
{ char * end = NULL;
unsigned int recOffset = strtoul(arg, &end, 0);
if ((*end) || (end == arg) || (recOffset == UINT_MAX)) {
rpmlog(RPMLOG_ERR, _("invalid package number: %s\n"), arg);
return 1;
}
rpmlog(RPMLOG_DEBUG, "package record number: %u\n", recOffset);
/* RPMDBI_PACKAGES */
qva->qva_mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, &recOffset, sizeof(recOffset));
if (qva->qva_mi == NULL) {
rpmlog(RPMLOG_ERR,
_("record %u could not be read\n"), recOffset);
res = 1;
} else
res = rpmcliShowMatches(qva, ts);
} break;
case RPMQV_PACKAGE:
{
int matches = 0;
rpmdbMatchIterator mi;
mi = rpmtsInitIterator(ts, RPMDBI_LABEL, arg, 0);
while (rpmdbNextIterator(mi) != NULL) {
matches++;
}
rpmdbFreeIterator(mi);
if (! matches) {
rpmlog(RPMLOG_NOTICE, _("package %s is not installed\n"), arg);
res = 1;
} else {
qva->qva_mi = rpmtsInitIterator(ts, RPMDBI_LABEL, arg, 0);
res = rpmcliShowMatches(qva, ts);
}
break;
}
}
return res;
}
static int rpmcliArgIterHelper(rpmts ts, QVA_t qva, rpmTag tag, ARGV_const_t argv, rpmgiFlags gFlgs)
{
rpmRC rpmrc = RPMRC_NOTFOUND;
int ec = 0;
qva->qva_gi = rpmgiNew(ts, tag, NULL, 0);
qva->qva_rc = rpmgiSetArgs(qva->qva_gi, argv, ftsOpts, gFlgs);
if (qva->qva_gi != NULL && (rpmgiGetFlags(qva->qva_gi) & RPMGI_TSADD)) /* Load the ts with headers. */
while ((rpmrc = rpmgiNext(qva->qva_gi)) == RPMRC_OK)
{};
if (rpmrc != RPMRC_NOTFOUND) {
qva->qva_gi = rpmgiFree(qva->qva_gi);
return 1; /* XXX should be no. of failures. */
}
/* FIX: argv can be NULL, cast to pass argv array */
ec = rpmQueryVerify(qva, ts, (tag == RPMDBI_PACKAGES)? (const char *) argv : NULL);
rpmtsEmpty(ts);
qva->qva_gi = rpmgiFree(qva->qva_gi);
return ec;
}
int rpmcliArgIter(rpmts ts, QVA_t qva, ARGV_const_t argv)
{
int ec = 0;
switch (qva->qva_source) {
case RPMQV_ALL:
ec = rpmcliArgIterHelper(ts, qva, RPMDBI_PACKAGES, argv, RPMGI_NONE);
break;
case RPMQV_RPM:
ec = rpmcliArgIterHelper(ts, qva, RPMDBI_ARGLIST, argv, giFlags);
break;
case RPMQV_HDLIST:
ec = rpmcliArgIterHelper(ts, qva, RPMDBI_HDLIST, argv, giFlags);
break;
case RPMQV_FTSWALK:
if (ftsOpts == 0)
ftsOpts = (RPMGI_COMFOLLOW | RPMGI_LOGICAL | RPMGI_NOSTAT);
ec = rpmcliArgIterHelper(ts, qva, RPMDBI_FTSWALK, argv, giFlags);
break;
default:
qva->qva_gi = rpmgiNew(ts, RPMDBI_ARGLIST, NULL, 0);
qva->qva_rc = rpmgiSetArgs(qva->qva_gi, argv, ftsOpts,
(giFlags | (RPMGI_NOGLOB|RPMGI_NOHEADER)));
while (rpmgiNext(qva->qva_gi) == RPMRC_OK) {
ec += rpmQueryVerify(qva, ts, rpmgiHdrPath(qva->qva_gi));
rpmtsEmpty(ts);
}
qva->qva_gi = rpmgiFree(qva->qva_gi);
break;
}
return ec;
}
int rpmcliQuery(rpmts ts, QVA_t qva, char * const * argv)
{
rpmVSFlags vsflags, ovsflags;
int ec = 0;
if (qva->qva_showPackage == NULL)
qva->qva_showPackage = showQueryPackage;
/* If --queryformat unspecified, then set default now. */
if (!(qva->qva_flags & _QUERY_FOR_BITS) && qva->qva_queryFormat == NULL) {
char * fmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
if (fmt == NULL || strlen(fmt) <= 1) {
fmt = _free(fmt);
fmt = xstrdup("%{nvra}\n");
}
qva->qva_queryFormat = fmt;
}
vsflags = rpmExpandNumeric("%{?_vsflags_query}");
if (qva->qva_flags & VERIFY_DIGEST)
vsflags |= _RPMVSF_NODIGESTS;
if (qva->qva_flags & VERIFY_SIGNATURE)
vsflags |= _RPMVSF_NOSIGNATURES;
if (qva->qva_flags & VERIFY_HDRCHK)
vsflags |= RPMVSF_NOHDRCHK;
ovsflags = rpmtsSetVSFlags(ts, vsflags);
ec = rpmcliArgIter(ts, qva, argv);
vsflags = rpmtsSetVSFlags(ts, ovsflags);
if (qva->qva_showPackage == showQueryPackage)
qva->qva_showPackage = NULL;
return ec;
}
|