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
|
/**
* \file tools/rpmcache.c
*/
#include "system.h"
const char *__progname;
#include <fnmatch.h>
#include <rpm/rpmtypes.h>
#include <rpm/rpmlib.h> /* RPM_MACHTABLE, rpmReadPackageFile */
#include <rpm/rpmcli.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmps.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmds.h>
#include <rpm/rpmts.h>
#include "rpmio/fts.h"
#include "lib/misc.h" /* for rpmMkdirPath */
#include "debug.h"
static int _debug = 0;
/* XXX should be flag in ts */
static int noCache = 0;
static char ** ftsSet;
const char * bhpath;
int bhpathlen = 0;
int bhlvl = -1;
struct ftsglob_s {
const char ** patterns;
int fnflags;
};
static struct ftsglob_s * bhglobs;
static int nbhglobs = 5;
static int indent = 2;
typedef struct Item_s {
char * path;
rpm_off_t size;
rpm_time_t mtime;
rpmds this;
Header h;
} * Item;
static Item * items = NULL;
static int nitems = 0;
static inline Item freeItem(Item item) {
if (item != NULL) {
item->path = _free(item->path);
item->this = rpmdsFree(item->this);
item->h = headerFree(item->h);
item = _free(item);
}
return NULL;
}
static inline Item newItem(void) {
Item item = xcalloc(1, sizeof(*item));
return item;
}
static int cmpItem(const void * a, const void * b) {
Item aitem = *(Item *)a;
Item bitem = *(Item *)b;
int rc = strcmp(rpmdsN(aitem->this), rpmdsN(bitem->this));
return rc;
}
static void freeItems(void) {
int i;
for (i = 0; i < nitems; i++)
items[i] = freeItem(items[i]);
items = _free(items);
nitems = 0;
}
static int ftsCachePrint(rpmts ts, FILE * fp)
{
int rc = 0;
int i;
if (fp == NULL) fp = stdout;
for (i = 0; i < nitems; i++) {
Item ip;
ip = items[i];
if (ip == NULL) {
rc = 1;
break;
}
fprintf(fp, "%s\n", ip->path);
}
return rc;
}
static int ftsCacheUpdate(rpmts ts)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
rpm_tid_t tid = rpmtsGetTid(ts);
rpmdbMatchIterator mi;
unsigned char * md5;
int rc = 0;
int i;
rc = rpmtsCloseDB(ts);
rc = rpmDefineMacro(NULL, "_dbpath %{_cache_dbpath}", RMIL_CMDLINE);
rc = rpmtsOpenDB(ts, O_RDWR);
if (rc != 0)
return rc;
for (i = 0; i < nitems; i++) {
Item ip;
ip = items[i];
if (ip == NULL) {
rc = 1;
break;
}
/* --- Check that identical package is not already cached. */
if (!hge(ip->h, RPMTAG_SIGMD5, NULL, (rpm_data_t *) &md5, NULL)
|| md5 == NULL)
{
rc = 1;
break;
}
mi = rpmtsInitIterator(ts, RPMTAG_SIGMD5, md5, 16);
rc = rpmdbGetIteratorCount(mi);
mi = rpmdbFreeIterator(mi);
if (rc) {
rc = 0;
continue;
}
/* --- Add cache tags to new cache header. */
rc = headerAddOrAppendEntry(ip->h, RPMTAG_CACHECTIME,
RPM_INT32_TYPE, &tid, 1);
if (rc != 1) break;
rc = headerAddOrAppendEntry(ip->h, RPMTAG_CACHEPKGPATH,
RPM_STRING_ARRAY_TYPE, &ip->path, 1);
if (rc != 1) break;
rc = headerAddOrAppendEntry(ip->h, RPMTAG_CACHEPKGSIZE,
RPM_INT32_TYPE, &ip->size, 1);
if (rc != 1) break;
rc = headerAddOrAppendEntry(ip->h, RPMTAG_CACHEPKGMTIME,
RPM_INT32_TYPE, &ip->mtime, 1);
if (rc != 1) break;
/* --- Add new cache header to database. */
rc = rpmdbAdd(rpmtsGetRdb(ts), tid, ip->h, NULL, NULL);
if (rc) break;
}
return rc;
}
/**
*/
static int archOkay(const char * pkgArch)
{
if (pkgArch == NULL) return 0;
return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
}
/**
*/
static int osOkay(const char * pkgOs)
{
if (pkgOs == NULL) return 0;
return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
}
static int ftsStashLatest(FTSENT * fts, rpmts ts)
{
Header h = NULL;
rpmds add = NULL;
const char * arch;
const char * os;
struct stat sb, * st;
int ec = -1; /* assume not found */
int i = 0;
rpmlog(RPMLOG_DEBUG, "============== %s\n", fts->fts_accpath);
/* Read header from file. */
{ FD_t fd = Fopen(fts->fts_accpath, "r");
rpmRC rpmrc;
int xx;
if (fd == NULL || Ferror(fd)) {
if (fd) xx = Fclose(fd);
goto exit;
}
rpmrc = rpmReadPackageFile(ts, fd, fts->fts_path, &h);
xx = Fclose(fd);
if (rpmrc != RPMRC_OK || h == NULL)
goto exit;
}
if (!headerGetEntry(h, RPMTAG_ARCH, NULL, (rpm_data_t *) &arch, NULL)
|| !headerGetEntry(h, RPMTAG_OS, NULL, (rpm_data_t *) &os, NULL))
goto exit;
/* Make sure arch and os match this platform. */
if (!archOkay(arch) || !osOkay(os)) {
ec = 0;
goto exit;
}
add = rpmdsThis(h, RPMTAG_REQUIRENAME, (RPMSENSE_EQUAL|RPMSENSE_LESS));
if (items != NULL && nitems > 0) {
Item needle = xcalloc(1, sizeof(*needle));
Item * found, * fneedle = &needle;
needle->this = add;
found = bsearch(fneedle, items, nitems, sizeof(*found), cmpItem);
/* Rewind to the first item with same name. */
while (found > items && cmpItem(found-1, fneedle) == 0)
found--;
/* Check that all saved items are newer than this item. */
if (found != NULL)
while (found < (items + nitems) && cmpItem(found, fneedle) == 0) {
ec = rpmdsCompare(needle->this, (*found)->this);
if (ec == 0) {
found++;
continue;
}
i = found - items;
break;
}
free(needle);
}
/*
* At this point, ec is
* -1 no item with the same name has been seen.
* 0 item exists, but already saved item EVR is newer.
* 1 item exists, but already saved item EVR is same/older.
*/
if (ec == 0) {
goto exit;
} else if (ec == 1) {
items[i] = freeItem(items[i]);
} else {
i = nitems++;
items = xrealloc(items, nitems * sizeof(*items));
}
items[i] = newItem();
items[i]->path = xstrdup(fts->fts_path);
st = fts->fts_statp;
if (st == NULL && stat(fts->fts_accpath, &sb) == 0)
st = &sb;
if (st != NULL) {
items[i]->size = st->st_size;
items[i]->mtime = st->st_mtime;
}
st = NULL;
items[i]->this = rpmdsThis(h, RPMTAG_PROVIDENAME, RPMSENSE_EQUAL);
items[i]->h = headerLink(h);
if (nitems > 1)
qsort(items, nitems, sizeof(*items), cmpItem);
#if 0
fprintf(stderr, "\t%*s [%d] %s\n",
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
i, fts->fts_name);
#endif
exit:
h = headerFree(h);
add = rpmdsFree(add);
return ec;
}
static const char * ftsInfoStrings[] = {
"UNKNOWN",
"D",
"DC",
"DEFAULT",
"DNR",
"DOT",
"DP",
"ERR",
"F",
"INIT",
"NS",
"NSOK",
"SL",
"SLNONE",
"W",
};
static const char * ftsInfoStr(int fts_info) {
if (!(fts_info >= 1 && fts_info <= 14))
fts_info = 0;
return ftsInfoStrings[ fts_info ];
}
static int ftsPrint(FTS * ftsp, FTSENT * fts, rpmts ts)
{
struct ftsglob_s * bhg;
const char ** patterns;
const char * pattern;
const char * s;
int lvl;
int xx;
switch (fts->fts_info) {
case FTS_D: /* preorder directory */
if (fts->fts_pathlen < bhpathlen)
break;
/* Grab the level of the beehive top directory. */
if (bhlvl < 0) {
if (fts->fts_pathlen == bhpathlen && !strcmp(fts->fts_path, bhpath))
bhlvl = fts->fts_level;
else
break;
}
lvl = fts->fts_level - bhlvl;
if (lvl < 0)
break;
#if 0
if (_debug)
fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
fts->fts_name);
#endif
/* Full path glob expression check. */
bhg = bhglobs;
if ((patterns = bhg->patterns) != NULL)
while ((pattern = *patterns++) != NULL) {
if (*pattern == '/')
xx = fnmatch(pattern, fts->fts_path, bhg->fnflags);
else
xx = fnmatch(pattern, fts->fts_name, bhg->fnflags);
if (xx == 0)
break;
}
/* Level specific glob expression check(s). */
if (lvl == 0 || lvl >= nbhglobs)
break;
bhg += lvl;
if ((patterns = bhg->patterns) != NULL)
while ((pattern = *patterns++) != NULL) {
if (*pattern == '/')
xx = fnmatch(pattern, fts->fts_path, bhg->fnflags);
else
xx = fnmatch(pattern, fts->fts_name, bhg->fnflags);
if (xx == 0)
break;
else
xx = Fts_set(ftsp, fts, FTS_SKIP);
}
break;
case FTS_DP: /* postorder directory */
#if 0
if (_debug)
fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
fts->fts_name);
#endif
break;
case FTS_F: /* regular file */
#if 0
if (_debug)
fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
fts->fts_name);
#endif
if (fts->fts_level >= 0) {
/* Ignore source packages. */
if (!strcmp(fts->fts_parent->fts_name, "SRPMS")) {
xx = Fts_set(ftsp, fts->fts_parent, FTS_SKIP);
break;
}
}
/* Ignore all but *.rpm files. */
s = fts->fts_name + fts->fts_namelen + 1 - sizeof(".rpm");
if (strcmp(s, ".rpm"))
break;
xx = ftsStashLatest(fts, ts);
break;
case FTS_NS: /* stat(2) failed */
case FTS_DNR: /* unreadable directory */
case FTS_ERR: /* error; errno is set */
if (_debug)
fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
fts->fts_name);
break;
case FTS_DC: /* directory that causes cycles */
case FTS_DEFAULT: /* none of the above */
case FTS_DOT: /* dot or dot-dot */
case FTS_INIT: /* initialized only */
case FTS_NSOK: /* no stat(2) requested */
case FTS_SL: /* symbolic link */
case FTS_SLNONE: /* symbolic link without target */
case FTS_W: /* whiteout object */
default:
if (_debug)
fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
fts->fts_name);
break;
}
return 0;
}
/**
* Initialize fts and glob structures.
* @param ts transaction set
* @param argv package names to match
*/
static void initGlobs(rpmts ts, const char ** argv)
{
char *buf = NULL;
int i;
if (argv != NULL && * argv != NULL) {
const char * arg;
int single = (glob_pattern_p(argv[0], 0) && argv[1] == NULL);
if (!single) {
rstrcat(&buf, "@(");
}
while ((arg = *argv++) != NULL) {
rstrscat(&buf, arg, "|", NULL);
}
buf[strlen(buf)-1] = single ? '\0' : ')';
}
bhpath = rpmExpand("%{_bhpath}", NULL);
bhpathlen = strlen(bhpath);
ftsSet = xcalloc(2, sizeof(*ftsSet));
ftsSet[0] = rpmExpand("%{_bhpath}", NULL);
nbhglobs = 5;
bhglobs = xcalloc(nbhglobs, sizeof(*bhglobs));
for (i = 0; i < nbhglobs; i++) {
char * pattern;
const char * macro;
switch (i) {
case 0:
macro = "%{_bhpath}";
break;
case 1:
macro = "%{_bhcoll}";
break;
case 2:
macro = (buf[0] == '\0' ? "%{_bhN}" : buf);
break;
case 3:
macro = "%{_bhVR}";
break;
case 4:
macro = "%{_bhA}";
break;
default:
macro = NULL;
break;
}
bhglobs[i].patterns = xcalloc(2, sizeof(*bhglobs[i].patterns));
if (macro == NULL)
continue;
pattern = rpmExpand(macro, NULL);
if (pattern == NULL || *pattern == '\0') {
pattern = _free(pattern);
continue;
}
bhglobs[i].patterns[0] = pattern;
bhglobs[i].fnflags = (FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH);
if (bhglobs[i].patterns[0] != NULL)
rpmlog(RPMLOG_DEBUG, "\t%d \"%s\"\n",
i, bhglobs[i].patterns[0]);
}
free(buf);
}
static rpmVSFlags vsflags = 0;
static struct poptOption optionsTable[] = {
{ "nolegacy", '\0', POPT_BIT_SET, &vsflags, RPMVSF_NEEDPAYLOAD,
N_("don't verify header+payload signature"), NULL },
{ "nocache", '\0', POPT_ARG_VAL, &noCache, -1,
N_("don't update cache database, only print package paths"), NULL },
{ NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliFtsPoptTable, 0,
N_("File tree walk 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
};
int
main(int argc, char *argv[])
{
rpmts ts = NULL;
poptContext optCon;
char * s;
FTS * ftsp;
FTSENT * fts;
int ec = 1;
rpmRC rpmrc;
int xx;
optCon = rpmcliInit(argc, argv, optionsTable);
if (optCon == NULL)
exit(EXIT_FAILURE);
/* Configure the path to cache database, creating if necessary. */
s = rpmExpand("%{?_cache_dbpath}", NULL);
if (!(s && *s))
rpmrc = RPMRC_FAIL;
else
rpmrc = rpmMkdirPath(s, "_cache_dbpath");
s = _free(s);
if (rpmrc != RPMRC_OK) {
fprintf(stderr, _("%s: %%{_cache_dbpath} macro is mis-configured.\n"),
__progname);
exit(EXIT_FAILURE);
}
ts = rpmtsCreate();
if (rpmcliQueryFlags & VERIFY_DIGEST)
vsflags |= _RPMVSF_NODIGESTS;
if (rpmcliQueryFlags & VERIFY_SIGNATURE)
vsflags |= _RPMVSF_NOSIGNATURES;
if (rpmcliQueryFlags & VERIFY_HDRCHK)
vsflags |= RPMVSF_NOHDRCHK;
(void) rpmtsSetVSFlags(ts, vsflags);
{ rpm_tid_t tid = (rpm_tid_t) time(NULL);
(void) rpmtsSetTid(ts, tid);
}
initGlobs(ts, poptGetArgs(optCon));
if (ftsOpts == 0)
ftsOpts = (FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOSTAT);
if (noCache)
ftsOpts |= FTS_NOSTAT;
else
ftsOpts &= ~FTS_NOSTAT;
/* Walk file tree, filter paths, save matched items. */
ftsp = Fts_open(ftsSet, ftsOpts, NULL);
while((fts = Fts_read(ftsp)) != NULL) {
xx = ftsPrint(ftsp, fts, ts);
}
xx = Fts_close(ftsp);
if (noCache)
ec = ftsCachePrint(ts, stdout);
else
ec = ftsCacheUpdate(ts);
if (ec) {
fprintf(stderr, _("%s: cache operation failed: ec %d.\n"),
__progname, ec);
}
freeItems();
ts = rpmtsFree(ts);
optCon = rpmcliFini(optCon);
return ec;
}
|