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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
/** \ingroup rpmcli
* \file lib/rpminstall.c
*/
#include "system.h"
#include <rpmlib.h>
#include <rpmmacro.h>
#include <rpmurl.h>
#include "manifest.h"
#include "misc.h"
#include "debug.h"
/*@access rpmTransactionSet@*/ /* XXX compared with NULL */
/*@access Header@*/ /* XXX compared with NULL */
/*@access FD_t@*/ /* XXX compared with NULL */
/**
* Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
* @param this memory to free
* @retval NULL always
*/
static /*@null@*/ void * _free(/*@only@*/ /*@null@*/ const void * this) {
if (this) free((void *)this);
return NULL;
}
/* Define if you want percentage progress in the hash bars when
* writing to a tty (ordinary hash bars otherwise) --claudio
*/
#define FANCY_HASH
static int hashesPrinted = 0;
#ifdef FANCY_HASH
static int packagesTotal = 0;
static int progressTotal = 0;
static int progressCurrent = 0;
#endif
/**
*/
static void printHash(const unsigned long amount, const unsigned long total)
{
int hashesNeeded;
int hashesTotal = 50;
#ifdef FANCY_HASH
if (isatty (STDOUT_FILENO))
hashesTotal = 44;
#endif
if (hashesPrinted != hashesTotal) {
hashesNeeded = hashesTotal * (total ? (((float) amount) / total) : 1);
while (hashesNeeded > hashesPrinted) {
#ifdef FANCY_HASH
if (isatty (STDOUT_FILENO)) {
int i;
for (i = 0; i < hashesPrinted; i++) putchar ('#');
for (; i < hashesTotal; i++) putchar (' ');
printf ("(%3d%%)",
(int)(100 * (total ? (((float) amount) / total) : 1)));
for (i = 0; i < (hashesTotal + 6); i++) putchar ('\b');
} else
#endif
fprintf(stdout, "#");
hashesPrinted++;
}
fflush(stdout);
hashesPrinted = hashesNeeded;
if (hashesPrinted == hashesTotal) {
#ifdef FANCY_HASH
int i;
progressCurrent++;
for (i = 1; i < hashesPrinted; i++) putchar ('#');
printf (" [%3d%%]\n", (int)(100 * (progressTotal ?
(((float) progressCurrent) / progressTotal) : 1)));
#else
fprintf (stdout, "\n");
#endif
}
fflush(stdout);
}
}
/**
*/
static void * showProgress(const void * arg, const rpmCallbackType what,
const unsigned long amount,
const unsigned long total,
const void * pkgKey, void * data)
{
Header h = (Header) arg;
char * s;
int flags = (int) ((long)data);
void * rc = NULL;
const char * filename = pkgKey;
static FD_t fd;
switch (what) {
case RPMCALLBACK_INST_OPEN_FILE:
fd = Fopen(filename, "r.ufdio");
if (fd)
fd = fdLink(fd, "persist (showProgress)");
return fd;
/*@notreached@*/ break;
case RPMCALLBACK_INST_CLOSE_FILE:
fd = fdFree(fd, "persist (showProgress)");
if (fd) {
Fclose(fd);
fd = NULL;
}
break;
case RPMCALLBACK_INST_START:
hashesPrinted = 0;
if (!(flags & INSTALL_LABEL))
break;
if (flags & INSTALL_HASH) {
s = headerSprintf(h, "%{NAME}", rpmTagTable, rpmHeaderFormats,NULL);
#ifdef FANCY_HASH
if (isatty (STDOUT_FILENO))
fprintf(stdout, "%4d:%-23.23s", progressCurrent + 1, s);
else
#else
fprintf(stdout, "%-28s", s);
#endif
fflush(stdout);
} else {
s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
rpmTagTable, rpmHeaderFormats, NULL);
fprintf(stdout, "%s\n", s);
fflush(stdout);
}
s = _free(s);
break;
case RPMCALLBACK_TRANS_PROGRESS:
case RPMCALLBACK_INST_PROGRESS:
if (flags & INSTALL_PERCENT)
fprintf(stdout, "%%%% %f\n", (total
? ((float) ((((float) amount) / total) * 100))
: 100.0));
else if (flags & INSTALL_HASH)
printHash(amount, total);
fflush(stdout);
break;
case RPMCALLBACK_TRANS_START:
hashesPrinted = 0;
#ifdef FANCY_HASH
progressTotal = 1;
progressCurrent = 0;
#endif
if (!(flags & INSTALL_LABEL))
break;
if (flags & INSTALL_HASH)
fprintf(stdout, "%-28s", _("Preparing..."));
else
printf("%s\n", _("Preparing packages for installation..."));
fflush(stdout);
break;
case RPMCALLBACK_TRANS_STOP:
if (flags & INSTALL_HASH)
printHash(1, 1); /* Fixes "preparing..." progress bar */
#ifdef FANCY_HASH
progressTotal = packagesTotal;
progressCurrent = 0;
#endif
break;
case RPMCALLBACK_UNINST_PROGRESS:
case RPMCALLBACK_UNINST_START:
case RPMCALLBACK_UNINST_STOP:
/* ignore */
break;
}
return rc;
}
/** @todo Generalize --freshen policies. */
int rpmInstall(const char * rootdir, const char ** fileArgv,
rpmtransFlags transFlags,
rpmInstallInterfaceFlags interfaceFlags,
rpmprobFilterFlags probFilter,
rpmRelocation * relocations)
{
rpmTransactionSet ts = NULL;
int notifyFlags = interfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
rpmdb db = NULL;
const char ** pkgURL = NULL;
char * pkgState = NULL;
const char ** fnp;
const char * fileURL = NULL;
int numPkgs = 0;
int numRPMS = 0;
int numSRPMS = 0;
int numFailed = 0;
int stopInstall = 0;
int dbIsOpen = 0;
rpmRelocation * defaultReloc = relocations;
const char ** sourceURL = NULL;
int prevx;
int pkgx;
const char ** argv = NULL;
int argc = 0;
const char ** av = NULL;
int ac = 0;
Header h;
FD_t fd;
int rc;
int i;
while (defaultReloc && defaultReloc->oldPath)
defaultReloc++;
if (defaultReloc && !defaultReloc->newPath) defaultReloc = NULL;
/* Build fully globbed list of arguments in argv[argc]. */
for (fnp = fileArgv; *fnp; fnp++) {
av = _free(av);
ac = 0;
rc = rpmGlob(*fnp, &ac, &av);
if (rc || ac == 0) continue;
if (argc == 0)
argv = xmalloc((argc+2) * sizeof(*argv));
else
argv = xrealloc(argv, (argc+2) * sizeof(*argv));
memcpy(argv+argc, av, ac * sizeof(*av));
argc += ac;
argv[argc] = NULL;
}
av = _free(av);
numPkgs = 0;
prevx = 0;
pkgx = 0;
restart:
/* Allocate sufficient storage for next set of args. */
if (pkgx >= numPkgs) {
numPkgs = pkgx + argc;
pkgURL = (pkgURL == NULL)
? xmalloc( (numPkgs + 1) * sizeof(*pkgURL))
: xrealloc(pkgURL, (numPkgs + 1) * sizeof(*pkgURL));
memset(pkgURL + pkgx, 0, ((argc + 1) * sizeof(*pkgURL)));
pkgState = (pkgState == NULL)
? xmalloc( (numPkgs + 1) * sizeof(*pkgState))
: xrealloc(pkgState, (numPkgs + 1) * sizeof(*pkgState));
memset(pkgState + pkgx, 0, ((argc + 1) * sizeof(*pkgState)));
}
/* Retrieve next set of args, cache on local storage. */
for (i = 0; i < argc; i++) {
fileURL = _free(fileURL);
fileURL = argv[i];
argv[i] = NULL;
switch (urlIsURL(fileURL)) {
case URL_IS_FTP:
case URL_IS_HTTP:
{ const char *tfn;
if (rpmIsVerbose())
fprintf(stdout, _("Retrieving %s\n"), fileURL);
{ char tfnbuf[64];
strcpy(tfnbuf, "rpm-xfer.XXXXXX");
/*@-unrecog@*/ mktemp(tfnbuf) /*@=unrecog@*/;
tfn = rpmGenPath(rootdir, "%{_tmppath}/", tfnbuf);
}
/* XXX undefined %{name}/%{version}/%{release} here */
/* XXX %{_tmpdir} does not exist */
rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
rc = urlGetFile(fileURL, tfn);
if (rc < 0) {
rpmMessage(RPMMESS_ERROR,
_("skipping %s - transfer failed - %s\n"),
fileURL, ftpStrerror(rc));
numFailed++;
pkgURL[pkgx] = NULL;
tfn = _free(tfn);
break;
}
pkgState[pkgx] = 1;
pkgURL[pkgx] = tfn;
pkgx++;
} break;
case URL_IS_PATH:
default:
pkgURL[pkgx] = fileURL;
fileURL = NULL;
pkgx++;
break;
}
}
fileURL = _free(fileURL);
if (numFailed) goto exit;
/* Continue processing file arguments, building transaction set. */
for (fnp = pkgURL+prevx; *fnp; fnp++, prevx++) {
const char * fileName;
rpmRC rpmrc;
int isSource;
rpmMessage(RPMMESS_DEBUG, "============== %s\n", *fnp);
(void) urlPath(*fnp, &fileName);
/* Try to read the header from a package file. */
fd = Fopen(*fnp, "r.ufdio");
if (fd == NULL || Ferror(fd)) {
rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *fnp,
Fstrerror(fd));
if (fd) Fclose(fd);
numFailed++; *fnp = NULL;
continue;
}
rpmrc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
Fclose(fd);
if (rpmrc == RPMRC_FAIL || rpmrc == RPMRC_SHORTREAD) {
numFailed++; *fnp = NULL;
continue;
}
if ((rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE) && isSource) {
rpmMessage(RPMMESS_DEBUG, "\tadded source rpm[%d]\n", numSRPMS);
sourceURL = (sourceURL == NULL)
? xmalloc( (numSRPMS + 2) * sizeof(*sourceURL))
: xrealloc(sourceURL, (numSRPMS + 2) * sizeof(*sourceURL));
sourceURL[numSRPMS++] = *fnp;
*fnp = NULL;
continue;
}
if (rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE) {
if (!dbIsOpen) {
int mode = (transFlags & RPMTRANS_FLAG_TEST)
? O_RDONLY : (O_RDWR | O_CREAT);
if (rpmdbOpen(rootdir, &db, mode, 0644)) {
const char *dn;
dn = rpmGetPath( (rootdir ? rootdir : ""),
"%{_dbpath}", NULL);
rpmMessage(RPMMESS_ERROR,
_("cannot open Packages database in %s\n"), dn);
dn = _free(dn);
numFailed++; *fnp = NULL;
break;
}
ts = rpmtransCreateSet(db, rootdir);
dbIsOpen = 1;
}
if (defaultReloc) {
const char ** paths;
int pft;
int c;
if (headerGetEntry(h, RPMTAG_PREFIXES, &pft,
(void **) &paths, &c) && (c == 1)) {
defaultReloc->oldPath = xstrdup(paths[0]);
paths = headerFreeData(paths, pft);
} else {
const char * name;
headerNVR(h, &name, NULL, NULL);
rpmMessage(RPMMESS_ERROR,
_("package %s is not relocateable\n"), name);
numFailed++;
goto exit;
/*@notreached@*/
}
}
/* On --freshen, verify package is installed and newer */
if (interfaceFlags & INSTALL_FRESHEN) {
rpmdbMatchIterator mi;
const char * name;
Header oldH;
int count;
headerNVR(h, &name, NULL, NULL);
mi = rpmdbInitIterator(db, RPMTAG_NAME, name, 0);
count = rpmdbGetIteratorCount(mi);
while ((oldH = rpmdbNextIterator(mi)) != NULL) {
if (rpmVersionCompare(oldH, h) < 0)
continue;
/* same or newer package already installed */
count = 0;
break;
}
rpmdbFreeIterator(mi);
if (count == 0) {
headerFree(h);
continue;
break; /* XXX out of switch */
}
/* Package is newer than those currently installed. */
}
rc = rpmtransAddPackage(ts, h, NULL, fileName,
(interfaceFlags & INSTALL_UPGRADE) != 0,
relocations);
headerFree(h); /* XXX reference held by transaction set */
if (defaultReloc)
defaultReloc->oldPath = _free(defaultReloc->oldPath);
switch(rc) {
case 0:
rpmMessage(RPMMESS_DEBUG, "\tadded binary rpm[%d]\n", numRPMS);
break;
case 1:
rpmMessage(RPMMESS_ERROR,
_("error reading from file %s\n"), *fnp);
numFailed++;
goto exit;
/*@notreached@*/ break;
case 2:
rpmMessage(RPMMESS_ERROR,
_("file %s requires a newer version of RPM\n"),
*fnp);
numFailed++;
goto exit;
/*@notreached@*/ break;
}
numRPMS++;
continue;
}
if (rpmrc != RPMRC_BADMAGIC) {
rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *fnp);
numFailed++; *fnp = NULL;
break;
}
/* Try to read a package manifest. */
fd = Fopen(*fnp, "r.fpio");
if (fd == NULL || Ferror(fd)) {
rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *fnp,
Fstrerror(fd));
if (fd) Fclose(fd);
numFailed++; *fnp = NULL;
break;
}
/* Read list of packages from manifest. */
rc = rpmReadPackageManifest(fd, &argc, &argv);
if (rc)
rpmError(RPMERR_MANIFEST, _("%s: read manifest failed: %s\n"),
fileURL, Fstrerror(fd));
Fclose(fd);
/* If successful, restart the query loop. */
if (rc == 0) {
prevx++;
goto restart;
}
numFailed++; *fnp = NULL;
break;
}
rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
numSRPMS, numRPMS);
if (numFailed) goto exit;
if (numRPMS && !(interfaceFlags & INSTALL_NODEPS)) {
struct rpmDependencyConflict * conflicts;
int numConflicts;
if (rpmdepCheck(ts, &conflicts, &numConflicts)) {
numFailed = numPkgs;
stopInstall = 1;
}
if (!stopInstall && conflicts) {
rpmMessage(RPMMESS_ERROR, _("failed dependencies:\n"));
printDepProblems(stderr, conflicts, numConflicts);
rpmdepFreeConflicts(conflicts, numConflicts);
numFailed = numPkgs;
stopInstall = 1;
}
}
if (numRPMS && !(interfaceFlags & INSTALL_NOORDER)) {
if (rpmdepOrder(ts)) {
numFailed = numPkgs;
stopInstall = 1;
}
}
if (numRPMS && !stopInstall) {
rpmProblemSet probs = NULL;
#ifdef FANCY_HASH
packagesTotal = numRPMS;
#endif
rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
rc = rpmRunTransactions(ts, showProgress, (void *) ((long)notifyFlags),
NULL, &probs, transFlags, probFilter);
if (rc < 0) {
numFailed += numRPMS;
} else if (rc > 0) {
numFailed += rc;
rpmProblemSetPrint(stderr, probs);
}
if (probs) rpmProblemSetFree(probs);
}
if (numSRPMS && !stopInstall) {
for (i = 0; i < numSRPMS; i++) {
fd = Fopen(sourceURL[i], "r.ufdio");
if (fd == NULL || Ferror(fd)) {
rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
sourceURL[i], Fstrerror(fd));
if (fd) Fclose(fd);
continue;
}
if (!(transFlags & RPMTRANS_FLAG_TEST)) {
rpmRC rpmrc = rpmInstallSourcePackage(rootdir, fd, NULL,
showProgress, (void *) ((long)notifyFlags), NULL);
if (rpmrc != RPMRC_OK) numFailed++;
}
Fclose(fd);
}
}
exit:
if (ts) rpmtransFree(ts);
for (i = 0; i < numPkgs; i++) {
if (pkgState[i] == 1)
Unlink(pkgURL[i]);
pkgURL[i] = _free(pkgURL[i]);
}
pkgState = _free(pkgState);
pkgURL = _free(pkgURL);
if (dbIsOpen) rpmdbClose(db);
return numFailed;
}
int rpmErase(const char * rootdir, const char ** argv,
rpmtransFlags transFlags,
rpmEraseInterfaceFlags interfaceFlags)
{
rpmdb db;
int mode;
int count;
const char ** arg;
int numFailed = 0;
rpmTransactionSet ts;
struct rpmDependencyConflict * conflicts;
int numConflicts;
int stopUninstall = 0;
int numPackages = 0;
rpmProblemSet probs;
if (transFlags & RPMTRANS_FLAG_TEST)
mode = O_RDONLY;
else
mode = O_RDWR | O_EXCL;
if (rpmdbOpen(rootdir, &db, mode, 0644)) {
const char *dn;
dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
rpmMessage(RPMMESS_ERROR, _("cannot open %s/packages.rpm\n"), dn);
dn = _free(dn);
return -1;
}
ts = rpmtransCreateSet(db, rootdir);
for (arg = argv; *arg; arg++) {
rpmdbMatchIterator mi;
/* XXX HACK to get rpmdbFindByLabel out of the API */
mi = rpmdbInitIterator(db, RPMDBI_LABEL, *arg, 0);
count = rpmdbGetIteratorCount(mi);
if (count <= 0) {
rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
numFailed++;
} else if (!(count == 1 || (interfaceFlags & UNINSTALL_ALLMATCHES))) {
rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
*arg);
numFailed++;
} else {
Header h; /* XXX iterator owns the reference */
while ((h = rpmdbNextIterator(mi)) != NULL) {
unsigned int recOffset = rpmdbGetIteratorOffset(mi);
if (recOffset) {
rpmtransRemovePackage(ts, recOffset);
numPackages++;
}
}
}
rpmdbFreeIterator(mi);
}
if (!(interfaceFlags & UNINSTALL_NODEPS)) {
if (rpmdepCheck(ts, &conflicts, &numConflicts)) {
numFailed = numPackages;
stopUninstall = 1;
}
if (!stopUninstall && conflicts) {
rpmMessage(RPMMESS_ERROR, _("removing these packages would break "
"dependencies:\n"));
printDepProblems(stderr, conflicts, numConflicts);
rpmdepFreeConflicts(conflicts, numConflicts);
numFailed += numPackages;
stopUninstall = 1;
}
}
if (!stopUninstall) {
transFlags |= RPMTRANS_FLAG_REVERSE;
numFailed += rpmRunTransactions(ts, NULL, NULL, NULL, &probs,
transFlags, 0);
}
rpmtransFree(ts);
rpmdbClose(db);
return numFailed;
}
int rpmInstallSource(const char * rootdir, const char * arg,
const char ** specFile, char ** cookie)
{
FD_t fd;
int rc;
fd = Fopen(arg, "r.ufdio");
if (fd == NULL || Ferror(fd)) {
rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
if (fd) Fclose(fd);
return 1;
}
if (rpmIsVerbose())
fprintf(stdout, _("Installing %s\n"), arg);
{ rpmRC rpmrc = rpmInstallSourcePackage(rootdir, fd, specFile, NULL, NULL,
cookie);
rc = (rpmrc == RPMRC_OK ? 0 : 1);
}
if (rc != 0) {
rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
if (specFile && *specFile)
*specFile = _free(*specFile);
if (cookie && *cookie)
*cookie = _free(*cookie);
}
Fclose(fd);
return rc;
}
|