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 = xcalloc(1, sizeof(*eiu));
215 rpmprobFilterFlags probFilter;
216 rpmRelocation * relocations;
217 char * fileURL = NULL;
219 rpmVSFlags vsflags, ovsflags, tvsflags;
224 if (fileArgv == NULL) goto exit;
226 rpmcliPackagesTotal = 0;
228 (void) rpmtsSetFlags(ts, ia->transFlags);
230 probFilter = ia->probFilter;
231 relocations = ia->relocations;
233 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
234 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
236 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
237 if (ia->qva_flags & VERIFY_DIGEST)
238 vsflags |= _RPMVSF_NODIGESTS;
239 if (ia->qva_flags & VERIFY_SIGNATURE)
240 vsflags |= _RPMVSF_NOSIGNATURES;
241 if (ia->qva_flags & VERIFY_HDRCHK)
242 vsflags |= RPMVSF_NOHDRCHK;
243 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
246 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
247 xx = rpmtsSetNotifyCallback(ts,
248 rpmShowProgress, (void *) ((long)notifyFlags));
251 if ((eiu->relocations = relocations) != NULL) {
252 while (eiu->relocations->oldPath)
254 if (eiu->relocations->newPath == NULL)
255 eiu->relocations = NULL;
258 /* Build fully globbed list of arguments in argv[argc]. */
259 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
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 argvAppend(&(eiu->argv), av);
278 /* Allocate sufficient storage for next set of args. */
279 if (eiu->pkgx >= eiu->numPkgs) {
280 eiu->numPkgs = eiu->pkgx + eiu->argc;
281 eiu->pkgURL = xrealloc(eiu->pkgURL,
282 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
283 memset(eiu->pkgURL + eiu->pkgx, 0,
284 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
285 eiu->pkgState = xrealloc(eiu->pkgState,
286 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
287 memset(eiu->pkgState + eiu->pkgx, 0,
288 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
291 /* Retrieve next set of args, cache on local storage. */
292 for (i = 0; i < eiu->argc; i++) {
293 fileURL = _free(fileURL);
294 fileURL = eiu->argv[i];
297 switch (urlIsURL(fileURL)) {
305 fprintf(stdout, _("Retrieving %s\n"), fileURL);
307 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
310 rc = urlGetFile(fileURL, tfn);
317 _("skipping %s - transfer failed - %s\n"),
318 fileURL, ftpStrerror(rc));
320 eiu->pkgURL[eiu->pkgx] = NULL;
324 eiu->pkgState[eiu->pkgx] = 1;
325 eiu->pkgURL[eiu->pkgx] = tfn;
329 case URL_IS_DASH: /* WRONG WRONG WRONG */
330 case URL_IS_HKP: /* WRONG WRONG WRONG */
332 eiu->pkgURL[eiu->pkgx] = fileURL;
338 fileURL = _free(fileURL);
340 if (eiu->numFailed) goto exit;
342 /* Continue processing file arguments, building transaction set. */
343 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
345 eiu->fnp++, eiu->prevx++)
347 const char * fileName;
349 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
350 (void) urlPath(*eiu->fnp, &fileName);
352 /* Try to read the header from a package file. */
353 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
354 if (eiu->fd == NULL || Ferror(eiu->fd)) {
355 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
357 if (eiu->fd != NULL) {
358 xx = Fclose(eiu->fd);
361 eiu->numFailed++; *eiu->fnp = NULL;
365 /* Read the header, verifying signatures (if present). */
366 tvsflags = rpmtsSetVSFlags(ts, vsflags);
367 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
368 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
369 xx = Fclose(eiu->fd);
372 switch (eiu->rpmrc) {
374 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
375 eiu->numFailed++; *eiu->fnp = NULL;
381 case RPMRC_NOTTRUSTED:
388 eiu->isSource = headerIsSource(eiu->h);
391 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
393 eiu->sourceURL = xrealloc(eiu->sourceURL,
394 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
395 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
398 eiu->sourceURL[eiu->numSRPMS] = NULL;
402 if (eiu->relocations) {
407 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
408 (rpm_data_t *) &paths, &c) && (c == 1))
410 eiu->relocations->oldPath = xstrdup(paths[0]);
411 paths = headerFreeData(paths, pft);
414 xx = headerNVR(eiu->h, &name, NULL, NULL);
416 _("package %s is not relocatable\n"), name);
422 /* On --freshen, verify package is installed and newer */
423 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
424 rpmdbMatchIterator mi;
429 xx = headerNVR(eiu->h, &name, NULL, NULL);
430 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
431 count = rpmdbGetIteratorCount(mi);
432 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
433 if (rpmVersionCompare(oldH, eiu->h) < 0)
435 /* same or newer package already installed */
439 mi = rpmdbFreeIterator(mi);
441 eiu->h = headerFree(eiu->h);
444 /* Package is newer than those currently installed. */
447 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
448 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
451 /* XXX reference held by transaction set */
452 eiu->h = headerFree(eiu->h);
453 if (eiu->relocations)
454 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
458 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
463 _("error reading from file %s\n"), *eiu->fnp);
469 _("file %s requires a newer version of RPM\n"),
484 /* Try to read a package manifest. */
485 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
486 if (eiu->fd == NULL || Ferror(eiu->fd)) {
487 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
489 if (eiu->fd != NULL) {
490 xx = Fclose(eiu->fd);
493 eiu->numFailed++; *eiu->fnp = NULL;
497 /* Read list of packages from manifest. */
498 /* FIX: *eiu->argv can be NULL */
499 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
501 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
502 *eiu->fnp, Fstrerror(eiu->fd));
503 xx = Fclose(eiu->fd);
506 /* If successful, restart the query loop. */
507 if (rc == RPMRC_OK) {
512 eiu->numFailed++; *eiu->fnp = NULL;
516 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
517 eiu->numSRPMS, eiu->numRPMS);
519 if (eiu->numFailed) goto exit;
521 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
523 if (rpmtsCheck(ts)) {
524 eiu->numFailed = eiu->numPkgs;
528 ps = rpmtsProblems(ts);
529 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
530 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
531 rpmpsPrint(NULL, ps);
532 eiu->numFailed = eiu->numPkgs;
535 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
536 rpmtsPrintSuggests(ts);
542 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
543 if (rpmtsOrder(ts)) {
544 eiu->numFailed = eiu->numPkgs;
549 if (eiu->numRPMS && !stopInstall) {
551 rpmcliPackagesTotal += eiu->numSRPMS;
553 rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
555 /* Drop added/available package indices and dependency sets. */
558 rc = rpmtsRun(ts, NULL, probFilter);
559 ps = rpmtsProblems(ts);
562 eiu->numFailed += eiu->numRPMS;
564 eiu->numFailed += rc;
565 if (rpmpsNumProblems(ps) > 0)
566 rpmpsPrint(stderr, ps);
571 if (eiu->numSRPMS && !stopInstall) {
572 if (eiu->sourceURL != NULL)
573 for (i = 0; i < eiu->numSRPMS; i++) {
575 if (eiu->sourceURL[i] == NULL) continue;
576 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
577 if (eiu->fd == NULL || Ferror(eiu->fd)) {
578 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
579 eiu->sourceURL[i], Fstrerror(eiu->fd));
580 if (eiu->fd != NULL) {
581 xx = Fclose(eiu->fd);
587 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
588 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
589 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
592 xx = Fclose(eiu->fd);
598 if (eiu->pkgURL != NULL)
599 for (i = 0; i < eiu->numPkgs; i++) {
600 if (eiu->pkgURL[i] == NULL) continue;
601 if (eiu->pkgState[i] == 1)
602 (void) unlink(eiu->pkgURL[i]);
603 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
605 eiu->pkgState = _free(eiu->pkgState);
606 eiu->pkgURL = _free(eiu->pkgURL);
607 eiu->argv = _free(eiu->argv);
616 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
621 int stopUninstall = 0;
623 rpmVSFlags vsflags, ovsflags;
626 if (argv == NULL) return 0;
628 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
629 if (ia->qva_flags & VERIFY_DIGEST)
630 vsflags |= _RPMVSF_NODIGESTS;
631 if (ia->qva_flags & VERIFY_SIGNATURE)
632 vsflags |= _RPMVSF_NOSIGNATURES;
633 if (ia->qva_flags & VERIFY_HDRCHK)
634 vsflags |= RPMVSF_NOHDRCHK;
635 ovsflags = rpmtsSetVSFlags(ts, vsflags);
637 /* XXX suggest mechanism only meaningful when installing */
638 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
640 (void) rpmtsSetFlags(ts, ia->transFlags);
642 #ifdef NOTYET /* XXX no callbacks on erase yet */
643 { int notifyFlags, xx;
644 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
645 xx = rpmtsSetNotifyCallback(ts,
646 rpmShowProgress, (void *) ((long)notifyFlags));
650 for (arg = argv; *arg; arg++) {
651 rpmdbMatchIterator mi;
653 /* XXX HACK to get rpmdbFindByLabel out of the API */
654 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
656 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
659 Header h; /* XXX iterator owns the reference */
661 while ((h = rpmdbNextIterator(mi)) != NULL) {
662 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
664 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
665 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
671 (void) rpmtsAddEraseElement(ts, h, recOffset);
676 mi = rpmdbFreeIterator(mi);
679 if (numFailed) goto exit;
681 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
683 if (rpmtsCheck(ts)) {
684 numFailed = numPackages;
688 ps = rpmtsProblems(ts);
689 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
690 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
691 rpmpsPrint(NULL, ps);
692 numFailed += numPackages;
698 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
699 if (rpmtsOrder(ts)) {
700 numFailed += numPackages;
705 if (numPackages && !stopUninstall) {
706 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
708 /* Drop added/available package indices and dependency sets. */
711 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
712 ps = rpmtsProblems(ts);
713 if (rpmpsNumProblems(ps) > 0)
714 rpmpsPrint(NULL, ps);
715 numFailed += numPackages;
726 int rpmInstallSource(rpmts ts, const char * arg,
727 char ** specFilePtr, char ** cookie)
733 fd = Fopen(arg, "r.ufdio");
734 if (fd == NULL || Ferror(fd)) {
735 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
736 if (fd != NULL) (void) Fclose(fd);
741 fprintf(stdout, _("Installing %s\n"), arg);
744 rpmVSFlags ovsflags =
745 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
746 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
747 rc = (rpmrc == RPMRC_OK ? 0 : 1);
748 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
751 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
752 if (specFilePtr && *specFilePtr)
753 *specFilePtr = _free(*specFilePtr);
754 if (cookie && *cookie)
755 *cookie = _free(*cookie);