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/rpmgi.h"
17 #include "lib/manifest.h"
20 int rpmcliPackagesTotal = 0;
21 int rpmcliHashesCurrent = 0;
22 int rpmcliHashesTotal = 0;
23 int rpmcliProgressCurrent = 0;
24 int rpmcliProgressTotal = 0;
27 * Print a CLI progress bar.
28 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
29 * @param amount current
32 static void printHash(const rpm_loff_t amount, const rpm_loff_t total)
36 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
38 if (rpmcliHashesCurrent != rpmcliHashesTotal) {
39 float pct = (total ? (((float) amount) / total) : 1.0);
40 hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
41 while (hashesNeeded > rpmcliHashesCurrent) {
42 if (isatty (STDOUT_FILENO)) {
44 for (i = 0; i < rpmcliHashesCurrent; i++)
46 for (; i < rpmcliHashesTotal; i++)
48 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
49 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
50 (void) putchar ('\b');
54 rpmcliHashesCurrent++;
56 (void) fflush(stdout);
58 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
60 rpmcliProgressCurrent++;
61 if (isatty(STDOUT_FILENO)) {
62 for (i = 1; i < rpmcliHashesCurrent; i++)
64 pct = (rpmcliProgressTotal
65 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
67 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
69 fprintf(stdout, "\n");
71 (void) fflush(stdout);
75 static rpmVSFlags setvsFlags(struct rpmInstallArguments_s * ia)
79 if (ia->installInterfaceFlags & (INSTALL_UPGRADE | INSTALL_ERASE))
80 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
82 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
84 if (rpmcliQueryFlags & VERIFY_DIGEST)
85 vsflags |= _RPMVSF_NODIGESTS;
86 if (rpmcliQueryFlags & VERIFY_SIGNATURE)
87 vsflags |= _RPMVSF_NOSIGNATURES;
88 if (rpmcliQueryFlags & VERIFY_HDRCHK)
89 vsflags |= RPMVSF_NOHDRCHK;
94 void * rpmShowProgress(const void * arg,
95 const rpmCallbackType what,
96 const rpm_loff_t amount,
97 const rpm_loff_t total,
101 Header h = (Header) arg;
103 int flags = (int) ((long)data);
105 const char * filename = (const char *)key;
106 static FD_t fd = NULL;
109 case RPMCALLBACK_INST_OPEN_FILE:
110 if (filename == NULL || filename[0] == '\0')
112 fd = Fopen(filename, "r.ufdio");
113 /* FIX: still necessary? */
114 if (fd == NULL || Ferror(fd)) {
115 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
126 case RPMCALLBACK_INST_CLOSE_FILE:
127 /* FIX: still necessary? */
135 case RPMCALLBACK_INST_START:
136 rpmcliHashesCurrent = 0;
137 if (h == NULL || !(flags & INSTALL_LABEL))
139 /* @todo Remove headerFormat() on a progress callback. */
140 if (flags & INSTALL_HASH) {
141 s = headerFormat(h, "%{NAME}", NULL);
142 if (isatty (STDOUT_FILENO))
143 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
145 fprintf(stdout, "%-28.28s", s);
146 (void) fflush(stdout);
149 s = headerFormat(h, "%{NAME}-%{VERSION}-%{RELEASE}", NULL);
150 fprintf(stdout, "%s\n", s);
151 (void) fflush(stdout);
156 case RPMCALLBACK_TRANS_PROGRESS:
157 case RPMCALLBACK_INST_PROGRESS:
158 if (flags & INSTALL_PERCENT)
159 fprintf(stdout, "%%%% %f\n", (double) (total
160 ? ((((float) amount) / total) * 100)
162 else if (flags & INSTALL_HASH)
163 printHash(amount, total);
164 (void) fflush(stdout);
167 case RPMCALLBACK_TRANS_START:
168 rpmcliHashesCurrent = 0;
169 rpmcliProgressTotal = 1;
170 rpmcliProgressCurrent = 0;
171 if (!(flags & INSTALL_LABEL))
173 if (flags & INSTALL_HASH)
174 fprintf(stdout, "%-28s", _("Preparing..."));
176 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
177 (void) fflush(stdout);
180 case RPMCALLBACK_TRANS_STOP:
181 if (flags & INSTALL_HASH)
182 printHash(1, 1); /* Fixes "preparing..." progress bar */
183 rpmcliProgressTotal = rpmcliPackagesTotal;
184 rpmcliProgressCurrent = 0;
187 case RPMCALLBACK_UNINST_PROGRESS:
189 case RPMCALLBACK_UNINST_START:
191 case RPMCALLBACK_UNINST_STOP:
193 case RPMCALLBACK_UNPACK_ERROR:
195 case RPMCALLBACK_CPIO_ERROR:
197 case RPMCALLBACK_SCRIPT_ERROR:
199 case RPMCALLBACK_UNKNOWN:
207 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
212 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
213 rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
231 rpmRelocation * relocations;
235 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
243 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
245 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
247 if (rpmtsCheck(ts)) {
252 ps = rpmtsProblems(ts);
253 if (!stop && rpmpsNumProblems(ps) > 0) {
254 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
255 rpmpsPrint(NULL, ps);
262 if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
263 if (rpmtsOrder(ts)) {
269 if (numPackages && !stop) {
270 rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
271 "installing binary packages\n");
273 rc = rpmtsRun(ts, NULL, ia->probFilter);
275 ps = rpmtsProblems(ts);
277 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
278 rpmpsPrint((eflags? NULL : stderr), ps);
285 static int tryReadManifest(struct rpmEIU * eiu)
289 /* Try to read a package manifest. */
290 FD_t fd = Fopen(*eiu->fnp, "r.fpio");
291 if (fd == NULL || Ferror(fd)) {
292 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
298 eiu->numFailed++; *eiu->fnp = NULL;
302 /* Read list of packages from manifest. */
303 rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
305 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
306 *eiu->fnp, Fstrerror(fd));
311 eiu->numFailed++; *eiu->fnp = NULL;
316 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, rpmVSFlags vsflags)
320 /* Try to read the header from a package file. */
321 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
322 if (fd == NULL || Ferror(fd)) {
323 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
329 eiu->numFailed++; *eiu->fnp = NULL;
333 /* Read the header, verifying signatures (if present). */
334 tvsflags = rpmtsSetVSFlags(ts, vsflags);
335 eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, &eiu->h);
336 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
340 /* Honor --nomanifest */
341 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
342 eiu->rpmrc = RPMRC_FAIL;
344 if(eiu->rpmrc == RPMRC_FAIL) {
345 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
346 eiu->numFailed++; *eiu->fnp = NULL;
353 /* On --freshen, verify package is installed and newer */
354 static int checkFreshenStatus(rpmts ts, struct rpmEIU * eiu)
356 rpmdbMatchIterator mi = NULL;
357 const char * name = headerGetString(eiu->h, RPMTAG_NAME);
358 const char *arch = headerGetString(eiu->h, RPMTAG_ARCH);
362 mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
363 if (rpmtsColor(ts) && arch)
364 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
366 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
367 /* Package is newer than those currently installed. */
368 if (rpmVersionCompare(oldH, eiu->h) < 0)
372 mi = rpmdbFreeIterator(mi);
374 eiu->h = headerFree(eiu->h);
376 return (oldH != NULL);
379 /** @todo Generalize --freshen policies. */
380 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
382 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
383 rpmRelocation * relocations;
384 char * fileURL = NULL;
385 rpmVSFlags vsflags, ovsflags;
389 if (fileArgv == NULL) goto exit;
391 rpmcliPackagesTotal = 0;
393 (void) rpmtsSetFlags(ts, ia->transFlags);
395 relocations = ia->relocations;
397 vsflags = setvsFlags(ia);
398 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
400 setNotifyFlag(ia, ts);
402 if ((eiu->relocations = relocations) != NULL) {
403 while (eiu->relocations->oldPath)
405 if (eiu->relocations->newPath == NULL)
406 eiu->relocations = NULL;
409 /* Build fully globbed list of arguments in argv[argc]. */
410 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
415 fn = rpmEscapeSpaces(*eiu->fnp);
416 rc = rpmGlob(fn, &ac, &av);
419 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
424 argvAppend(&(eiu->argv), av);
430 /* Allocate sufficient storage for next set of args. */
431 if (eiu->pkgx >= eiu->numPkgs) {
432 eiu->numPkgs = eiu->pkgx + eiu->argc;
433 eiu->pkgURL = xrealloc(eiu->pkgURL,
434 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
435 memset(eiu->pkgURL + eiu->pkgx, 0,
436 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
437 eiu->pkgState = xrealloc(eiu->pkgState,
438 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
439 memset(eiu->pkgState + eiu->pkgx, 0,
440 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
443 /* Retrieve next set of args, cache on local storage. */
444 for (i = 0; i < eiu->argc; i++) {
445 fileURL = _free(fileURL);
446 fileURL = eiu->argv[i];
449 switch (urlIsURL(fileURL)) {
457 fprintf(stdout, _("Retrieving %s\n"), fileURL);
459 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
462 rc = urlGetFile(fileURL, tfn);
469 _("skipping %s - transfer failed\n"), fileURL);
471 eiu->pkgURL[eiu->pkgx] = NULL;
475 eiu->pkgState[eiu->pkgx] = 1;
476 eiu->pkgURL[eiu->pkgx] = tfn;
480 case URL_IS_DASH: /* WRONG WRONG WRONG */
481 case URL_IS_HKP: /* WRONG WRONG WRONG */
483 eiu->pkgURL[eiu->pkgx] = fileURL;
489 fileURL = _free(fileURL);
491 if (eiu->numFailed) goto exit;
493 /* Continue processing file arguments, building transaction set. */
494 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
496 eiu->fnp++, eiu->prevx++)
498 const char * fileName;
500 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
501 (void) urlPath(*eiu->fnp, &fileName);
503 if (tryReadHeader(ts, eiu, vsflags) == RPMRC_FAIL)
506 if (eiu->rpmrc == RPMRC_NOTFOUND) {
507 rc = tryReadManifest(eiu);
508 if (rc == RPMRC_OK) {
514 eiu->isSource = headerIsSource(eiu->h);
517 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
519 eiu->sourceURL = xrealloc(eiu->sourceURL,
520 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
521 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
524 eiu->sourceURL[eiu->numSRPMS] = NULL;
528 if (eiu->relocations) {
529 struct rpmtd_s prefixes;
531 headerGet(eiu->h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
532 if (rpmtdCount(&prefixes) == 1) {
533 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
534 rpmtdFreeData(&prefixes);
536 rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
537 headerGetString(eiu->h, RPMTAG_NAME));
543 if (ia->installInterfaceFlags & INSTALL_FRESHEN)
544 if (checkFreshenStatus(ts, eiu) != 1)
547 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
548 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
551 /* XXX reference held by transaction set */
552 eiu->h = headerFree(eiu->h);
553 if (eiu->relocations)
554 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
558 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
563 _("error reading from file %s\n"), *eiu->fnp);
569 _("file %s requires a newer version of RPM\n"),
583 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
584 eiu->numSRPMS, eiu->numRPMS);
586 if (eiu->numFailed) goto exit;
589 int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
591 eiu->numFailed += eiu->numRPMS;
593 eiu->numFailed += rc;
596 if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
597 for (i = 0; i < eiu->numSRPMS; i++) {
599 if (eiu->sourceURL[i] != NULL) {
601 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
602 rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
610 if (eiu->pkgURL != NULL) {
611 for (i = 0; i < eiu->numPkgs; i++) {
612 if (eiu->pkgURL[i] == NULL) continue;
613 if (eiu->pkgState[i] == 1)
614 (void) unlink(eiu->pkgURL[i]);
615 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
618 eiu->pkgState = _free(eiu->pkgState);
619 eiu->pkgURL = _free(eiu->pkgURL);
620 eiu->argv = _free(eiu->argv);
629 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
635 rpmVSFlags vsflags, ovsflags;
637 if (argv == NULL) return 0;
639 vsflags = setvsFlags(ia);
640 ovsflags = rpmtsSetVSFlags(ts, vsflags);
642 /* XXX suggest mechanism only meaningful when installing */
643 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
645 (void) rpmtsSetFlags(ts, ia->transFlags);
647 #ifdef NOTYET /* XXX no callbacks on erase yet */
648 setNotifyFlag(ia, ts);
651 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
652 for (arg = argv; *arg; arg++) {
653 rpmdbMatchIterator mi;
657 /* Iterator count isn't reliable with labels, count manually... */
658 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
659 while (rpmdbNextIterator(mi) != NULL) {
662 rpmdbFreeIterator(mi);
665 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
668 Header h; /* XXX iterator owns the reference */
671 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
672 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
678 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
679 while ((h = rpmdbNextIterator(mi)) != NULL) {
681 (void) rpmtsAddEraseElement(ts, h, -1);
684 char *nevra = headerFormat(h, qfmt, NULL);
685 rpmlog(RPMLOG_NOTICE, " %s", nevra);
689 mi = rpmdbFreeIterator(mi);
694 if (numFailed) goto exit;
695 numFailed = rpmcliTransaction(ts, ia, numPackages);
702 int rpmInstallSource(rpmts ts, const char * arg,
703 char ** specFilePtr, char ** cookie)
709 fd = Fopen(arg, "r.ufdio");
710 if (fd == NULL || Ferror(fd)) {
711 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
712 if (fd != NULL) (void) Fclose(fd);
716 if (rpmIsVerbose() && specFilePtr != NULL)
717 fprintf(stdout, _("Installing %s\n"), arg);
720 rpmVSFlags ovsflags =
721 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
722 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
723 rc = (rpmrc == RPMRC_OK ? 0 : 1);
724 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
727 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
728 if (specFilePtr && *specFilePtr)
729 *specFilePtr = _free(*specFilePtr);
730 if (cookie && *cookie)
731 *cookie = _free(*cookie);