2 * \file lib/rpminstall.c
7 #include <rpm/rpmcli.h>
8 #include <rpm/rpmtag.h>
9 #include <rpm/rpmlib.h> /* rpmReadPackageFile, vercmp etc */
10 #include <rpm/rpmdb.h>
11 #include <rpm/rpmds.h>
12 #include <rpm/rpmts.h>
13 #include <rpm/rpmlog.h>
14 #include <rpm/rpmfileutil.h>
16 #include "lib/manifest.h"
19 int rpmcliPackagesTotal = 0;
20 int rpmcliHashesCurrent = 0;
21 int rpmcliHashesTotal = 0;
22 int rpmcliProgressCurrent = 0;
23 int rpmcliProgressTotal = 0;
26 * Print a CLI progress bar.
27 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
28 * @param amount current
31 static void printHash(const rpm_off_t amount, const rpm_off_t total)
35 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
37 if (rpmcliHashesCurrent != rpmcliHashesTotal) {
38 float pct = (total ? (((float) amount) / total) : 1.0);
39 hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
40 while (hashesNeeded > rpmcliHashesCurrent) {
41 if (isatty (STDOUT_FILENO)) {
43 for (i = 0; i < rpmcliHashesCurrent; i++)
45 for (; i < rpmcliHashesTotal; i++)
47 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
48 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
49 (void) putchar ('\b');
53 rpmcliHashesCurrent++;
55 (void) fflush(stdout);
57 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
59 rpmcliProgressCurrent++;
60 if (isatty(STDOUT_FILENO)) {
61 for (i = 1; i < rpmcliHashesCurrent; i++)
63 pct = (rpmcliProgressTotal
64 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
66 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
68 fprintf(stdout, "\n");
70 (void) fflush(stdout);
74 void * rpmShowProgress(const void * arg,
75 const rpmCallbackType what,
76 const rpm_off_t amount,
77 const rpm_off_t total,
81 Header h = (Header) arg;
83 int flags = (int) ((long)data);
85 const char * filename = (const char *)key;
86 static FD_t fd = NULL;
90 case RPMCALLBACK_INST_OPEN_FILE:
91 if (filename == NULL || filename[0] == '\0')
93 fd = Fopen(filename, "r.ufdio");
94 /* FIX: still necessary? */
95 if (fd == NULL || Ferror(fd)) {
96 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
103 fd = fdLink(fd, RPMDBG_M("persist (showProgress)"));
107 case RPMCALLBACK_INST_CLOSE_FILE:
108 /* FIX: still necessary? */
109 fd = fdFree(fd, RPMDBG_M("persist (showProgress)"));
116 case RPMCALLBACK_INST_START:
117 rpmcliHashesCurrent = 0;
118 if (h == NULL || !(flags & INSTALL_LABEL))
120 /* @todo Remove headerSprintf() on a progress callback. */
121 if (flags & INSTALL_HASH) {
122 s = headerSprintf(h, "%{NAME}",
123 rpmTagTable, rpmHeaderFormats, NULL);
124 if (isatty (STDOUT_FILENO))
125 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
127 fprintf(stdout, "%-28.28s", s);
128 (void) fflush(stdout);
131 s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
132 rpmTagTable, rpmHeaderFormats, NULL);
133 fprintf(stdout, "%s\n", s);
134 (void) fflush(stdout);
139 case RPMCALLBACK_TRANS_PROGRESS:
140 case RPMCALLBACK_INST_PROGRESS:
141 if (flags & INSTALL_PERCENT)
142 fprintf(stdout, "%%%% %f\n", (double) (total
143 ? ((((float) amount) / total) * 100)
145 else if (flags & INSTALL_HASH)
146 printHash(amount, total);
147 (void) fflush(stdout);
150 case RPMCALLBACK_TRANS_START:
151 rpmcliHashesCurrent = 0;
152 rpmcliProgressTotal = 1;
153 rpmcliProgressCurrent = 0;
154 if (!(flags & INSTALL_LABEL))
156 if (flags & INSTALL_HASH)
157 fprintf(stdout, "%-28s", _("Preparing..."));
159 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
160 (void) fflush(stdout);
163 case RPMCALLBACK_TRANS_STOP:
164 if (flags & INSTALL_HASH)
165 printHash(1, 1); /* Fixes "preparing..." progress bar */
166 rpmcliProgressTotal = rpmcliPackagesTotal;
167 rpmcliProgressCurrent = 0;
170 case RPMCALLBACK_UNINST_PROGRESS:
172 case RPMCALLBACK_UNINST_START:
174 case RPMCALLBACK_UNINST_STOP:
176 case RPMCALLBACK_UNPACK_ERROR:
178 case RPMCALLBACK_CPIO_ERROR:
180 case RPMCALLBACK_SCRIPT_ERROR:
182 case RPMCALLBACK_UNKNOWN:
206 rpmRelocation * relocations;
210 /** @todo Generalize --freshen policies. */
211 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
213 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
215 rpmprobFilterFlags probFilter;
216 rpmRelocation * relocations;
217 char * fileURL = NULL;
220 rpmVSFlags vsflags, ovsflags, tvsflags;
226 if (fileArgv == NULL) goto exit;
228 rpmcliPackagesTotal = 0;
230 (void) rpmtsSetFlags(ts, ia->transFlags);
232 probFilter = ia->probFilter;
233 relocations = ia->relocations;
235 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
236 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
238 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
239 if (ia->qva_flags & VERIFY_DIGEST)
240 vsflags |= _RPMVSF_NODIGESTS;
241 if (ia->qva_flags & VERIFY_SIGNATURE)
242 vsflags |= _RPMVSF_NOSIGNATURES;
243 if (ia->qva_flags & VERIFY_HDRCHK)
244 vsflags |= RPMVSF_NOHDRCHK;
245 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
248 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
249 xx = rpmtsSetNotifyCallback(ts,
250 rpmShowProgress, (void *) ((long)notifyFlags));
253 if ((eiu->relocations = relocations) != NULL) {
254 while (eiu->relocations->oldPath)
256 if (eiu->relocations->newPath == NULL)
257 eiu->relocations = NULL;
260 /* Build fully globbed list of arguments in argv[argc]. */
261 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
263 av = _free(av); ac = 0;
264 fn = rpmEscapeSpaces(*eiu->fnp);
265 rc = rpmGlob(fn, &ac, &av);
268 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
272 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
273 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
275 eiu->argv[eiu->argc] = NULL;
277 av = _free(av); ac = 0;
280 /* Allocate sufficient storage for next set of args. */
281 if (eiu->pkgx >= eiu->numPkgs) {
282 eiu->numPkgs = eiu->pkgx + eiu->argc;
283 eiu->pkgURL = xrealloc(eiu->pkgURL,
284 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
285 memset(eiu->pkgURL + eiu->pkgx, 0,
286 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
287 eiu->pkgState = xrealloc(eiu->pkgState,
288 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
289 memset(eiu->pkgState + eiu->pkgx, 0,
290 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
293 /* Retrieve next set of args, cache on local storage. */
294 for (i = 0; i < eiu->argc; i++) {
295 fileURL = _free(fileURL);
296 fileURL = eiu->argv[i];
300 if (fileURL[0] == '=') {
301 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
303 xx = rpmtsSolve(ts, this, NULL);
304 if (ts->suggests && ts->nsuggests > 0) {
305 fileURL = _free(fileURL);
306 fileURL = ts->suggests[0];
307 ts->suggests[0] = NULL;
308 while (ts->nsuggests-- > 0) {
309 if (ts->suggests[ts->nsuggests] == NULL)
311 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
313 ts->suggests = _free(ts->suggests);
314 rpmlog(RPMLOG_DEBUG, "Adding goal: %s\n", fileURL);
315 eiu->pkgURL[eiu->pkgx] = fileURL;
319 this = rpmdsFree(this);
323 switch (urlIsURL(fileURL)) {
330 fprintf(stdout, _("Retrieving %s\n"), fileURL);
333 const char * rootDir = rpmtsRootDir(ts);
334 if (!(rootDir && * rootDir))
336 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
337 #if defined(HAVE_MKSTEMP)
338 (void) close(mkstemp(tfnbuf));
340 (void) mktemp(tfnbuf);
342 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
345 /* XXX undefined %{name}/%{version}/%{release} here */
346 /* XXX %{_tmpdir} does not exist */
347 rpmlog(RPMLOG_DEBUG, " ... as %s\n", tfn);
348 rc = urlGetFile(fileURL, tfn);
351 _("skipping %s - transfer failed - %s\n"),
352 fileURL, ftpStrerror(rc));
354 eiu->pkgURL[eiu->pkgx] = NULL;
358 eiu->pkgState[eiu->pkgx] = 1;
359 eiu->pkgURL[eiu->pkgx] = tfn;
363 case URL_IS_DASH: /* WRONG WRONG WRONG */
364 case URL_IS_HKP: /* WRONG WRONG WRONG */
366 eiu->pkgURL[eiu->pkgx] = fileURL;
372 fileURL = _free(fileURL);
374 if (eiu->numFailed) goto exit;
376 /* Continue processing file arguments, building transaction set. */
377 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
379 eiu->fnp++, eiu->prevx++)
381 const char * fileName;
383 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
384 (void) urlPath(*eiu->fnp, &fileName);
386 /* Try to read the header from a package file. */
387 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
388 if (eiu->fd == NULL || Ferror(eiu->fd)) {
389 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
391 if (eiu->fd != NULL) {
392 xx = Fclose(eiu->fd);
395 eiu->numFailed++; *eiu->fnp = NULL;
399 /* Read the header, verifying signatures (if present). */
400 tvsflags = rpmtsSetVSFlags(ts, vsflags);
401 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
402 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
403 xx = Fclose(eiu->fd);
406 switch (eiu->rpmrc) {
408 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
409 eiu->numFailed++; *eiu->fnp = NULL;
415 case RPMRC_NOTTRUSTED:
422 eiu->isSource = headerIsSource(eiu->h);
425 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
427 eiu->sourceURL = xrealloc(eiu->sourceURL,
428 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
429 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
432 eiu->sourceURL[eiu->numSRPMS] = NULL;
436 if (eiu->relocations) {
441 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
442 (rpm_data_t *) &paths, &c) && (c == 1))
444 eiu->relocations->oldPath = xstrdup(paths[0]);
445 paths = headerFreeData(paths, pft);
448 xx = headerNVR(eiu->h, &name, NULL, NULL);
450 _("package %s is not relocatable\n"), name);
456 /* On --freshen, verify package is installed and newer */
457 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
458 rpmdbMatchIterator mi;
463 xx = headerNVR(eiu->h, &name, NULL, NULL);
464 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
465 count = rpmdbGetIteratorCount(mi);
466 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
467 if (rpmVersionCompare(oldH, eiu->h) < 0)
469 /* same or newer package already installed */
473 mi = rpmdbFreeIterator(mi);
475 eiu->h = headerFree(eiu->h);
478 /* Package is newer than those currently installed. */
481 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
482 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
485 /* XXX reference held by transaction set */
486 eiu->h = headerFree(eiu->h);
487 if (eiu->relocations)
488 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
492 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
497 _("error reading from file %s\n"), *eiu->fnp);
503 _("file %s requires a newer version of RPM\n"),
518 /* Try to read a package manifest. */
519 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
520 if (eiu->fd == NULL || Ferror(eiu->fd)) {
521 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
523 if (eiu->fd != NULL) {
524 xx = Fclose(eiu->fd);
527 eiu->numFailed++; *eiu->fnp = NULL;
531 /* Read list of packages from manifest. */
532 /* FIX: *eiu->argv can be NULL */
533 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
535 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
536 *eiu->fnp, Fstrerror(eiu->fd));
537 xx = Fclose(eiu->fd);
540 /* If successful, restart the query loop. */
541 if (rc == RPMRC_OK) {
546 eiu->numFailed++; *eiu->fnp = NULL;
550 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
551 eiu->numSRPMS, eiu->numRPMS);
553 if (eiu->numFailed) goto exit;
555 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
557 if (rpmtsCheck(ts)) {
558 eiu->numFailed = eiu->numPkgs;
562 ps = rpmtsProblems(ts);
563 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
564 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
565 rpmpsPrint(NULL, ps);
566 eiu->numFailed = eiu->numPkgs;
569 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
570 rpmtsPrintSuggests(ts);
576 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
577 if (rpmtsOrder(ts)) {
578 eiu->numFailed = eiu->numPkgs;
583 if (eiu->numRPMS && !stopInstall) {
585 rpmcliPackagesTotal += eiu->numSRPMS;
587 rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
589 /* Drop added/available package indices and dependency sets. */
592 rc = rpmtsRun(ts, NULL, probFilter);
593 ps = rpmtsProblems(ts);
596 eiu->numFailed += eiu->numRPMS;
598 eiu->numFailed += rc;
599 if (rpmpsNumProblems(ps) > 0)
600 rpmpsPrint(stderr, ps);
605 if (eiu->numSRPMS && !stopInstall) {
606 if (eiu->sourceURL != NULL)
607 for (i = 0; i < eiu->numSRPMS; i++) {
609 if (eiu->sourceURL[i] == NULL) continue;
610 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
611 if (eiu->fd == NULL || Ferror(eiu->fd)) {
612 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
613 eiu->sourceURL[i], Fstrerror(eiu->fd));
614 if (eiu->fd != NULL) {
615 xx = Fclose(eiu->fd);
621 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
622 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
623 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
626 xx = Fclose(eiu->fd);
632 if (eiu->pkgURL != NULL)
633 for (i = 0; i < eiu->numPkgs; i++) {
634 if (eiu->pkgURL[i] == NULL) continue;
635 if (eiu->pkgState[i] == 1)
636 (void) unlink(eiu->pkgURL[i]);
637 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
639 eiu->pkgState = _free(eiu->pkgState);
640 eiu->pkgURL = _free(eiu->pkgURL);
641 eiu->argv = _free(eiu->argv);
645 return eiu->numFailed;
648 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
653 int stopUninstall = 0;
655 rpmVSFlags vsflags, ovsflags;
658 if (argv == NULL) return 0;
660 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
661 if (ia->qva_flags & VERIFY_DIGEST)
662 vsflags |= _RPMVSF_NODIGESTS;
663 if (ia->qva_flags & VERIFY_SIGNATURE)
664 vsflags |= _RPMVSF_NOSIGNATURES;
665 if (ia->qva_flags & VERIFY_HDRCHK)
666 vsflags |= RPMVSF_NOHDRCHK;
667 ovsflags = rpmtsSetVSFlags(ts, vsflags);
669 /* XXX suggest mechanism only meaningful when installing */
670 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
672 (void) rpmtsSetFlags(ts, ia->transFlags);
674 #ifdef NOTYET /* XXX no callbacks on erase yet */
675 { int notifyFlags, xx;
676 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
677 xx = rpmtsSetNotifyCallback(ts,
678 rpmShowProgress, (void *) ((long)notifyFlags));
682 for (arg = argv; *arg; arg++) {
683 rpmdbMatchIterator mi;
685 /* XXX HACK to get rpmdbFindByLabel out of the API */
686 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
688 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
691 Header h; /* XXX iterator owns the reference */
693 while ((h = rpmdbNextIterator(mi)) != NULL) {
694 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
696 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
697 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
703 (void) rpmtsAddEraseElement(ts, h, recOffset);
708 mi = rpmdbFreeIterator(mi);
711 if (numFailed) goto exit;
713 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
715 if (rpmtsCheck(ts)) {
716 numFailed = numPackages;
720 ps = rpmtsProblems(ts);
721 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
722 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
723 rpmpsPrint(NULL, ps);
724 numFailed += numPackages;
730 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
731 if (rpmtsOrder(ts)) {
732 numFailed += numPackages;
737 if (numPackages && !stopUninstall) {
738 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
740 /* Drop added/available package indices and dependency sets. */
743 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
744 ps = rpmtsProblems(ts);
745 if (rpmpsNumProblems(ps) > 0)
746 rpmpsPrint(NULL, ps);
747 numFailed += numPackages;
758 int rpmInstallSource(rpmts ts, const char * arg,
759 char ** specFilePtr, char ** cookie)
765 fd = Fopen(arg, "r.ufdio");
766 if (fd == NULL || Ferror(fd)) {
767 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
768 if (fd != NULL) (void) Fclose(fd);
773 fprintf(stdout, _("Installing %s\n"), arg);
776 rpmVSFlags ovsflags =
777 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
778 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
779 rc = (rpmrc == RPMRC_OK ? 0 : 1);
780 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
783 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
784 if (specFilePtr && *specFilePtr)
785 *specFilePtr = _free(*specFilePtr);
786 if (cookie && *cookie)
787 *cookie = _free(*cookie);