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>
15 #include <rpm/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 (ia->qva_flags & VERIFY_DIGEST)
85 vsflags |= _RPMVSF_NODIGESTS;
86 if (ia->qva_flags & VERIFY_SIGNATURE)
87 vsflags |= _RPMVSF_NOSIGNATURES;
88 if (ia->qva_flags & 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;
110 case RPMCALLBACK_INST_OPEN_FILE:
111 if (filename == NULL || filename[0] == '\0')
113 fd = Fopen(filename, "r.ufdio");
114 /* FIX: still necessary? */
115 if (fd == NULL || Ferror(fd)) {
116 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
123 fd = fdLink(fd, RPMDBG_M("persist (showProgress)"));
127 case RPMCALLBACK_INST_CLOSE_FILE:
128 /* FIX: still necessary? */
129 fd = fdFree(fd, RPMDBG_M("persist (showProgress)"));
136 case RPMCALLBACK_INST_START:
137 rpmcliHashesCurrent = 0;
138 if (h == NULL || !(flags & INSTALL_LABEL))
140 /* @todo Remove headerFormat() on a progress callback. */
141 if (flags & INSTALL_HASH) {
142 s = headerFormat(h, "%{NAME}", NULL);
143 if (isatty (STDOUT_FILENO))
144 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
146 fprintf(stdout, "%-28.28s", s);
147 (void) fflush(stdout);
150 s = headerFormat(h, "%{NAME}-%{VERSION}-%{RELEASE}", NULL);
151 fprintf(stdout, "%s\n", s);
152 (void) fflush(stdout);
157 case RPMCALLBACK_TRANS_PROGRESS:
158 case RPMCALLBACK_INST_PROGRESS:
159 if (flags & INSTALL_PERCENT)
160 fprintf(stdout, "%%%% %f\n", (double) (total
161 ? ((((float) amount) / total) * 100)
163 else if (flags & INSTALL_HASH)
164 printHash(amount, total);
165 (void) fflush(stdout);
168 case RPMCALLBACK_TRANS_START:
169 rpmcliHashesCurrent = 0;
170 rpmcliProgressTotal = 1;
171 rpmcliProgressCurrent = 0;
172 if (!(flags & INSTALL_LABEL))
174 if (flags & INSTALL_HASH)
175 fprintf(stdout, "%-28s", _("Preparing..."));
177 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
178 (void) fflush(stdout);
181 case RPMCALLBACK_TRANS_STOP:
182 if (flags & INSTALL_HASH)
183 printHash(1, 1); /* Fixes "preparing..." progress bar */
184 rpmcliProgressTotal = rpmcliPackagesTotal;
185 rpmcliProgressCurrent = 0;
188 case RPMCALLBACK_UNINST_PROGRESS:
190 case RPMCALLBACK_UNINST_START:
192 case RPMCALLBACK_UNINST_STOP:
194 case RPMCALLBACK_UNPACK_ERROR:
196 case RPMCALLBACK_CPIO_ERROR:
198 case RPMCALLBACK_SCRIPT_ERROR:
200 case RPMCALLBACK_UNKNOWN:
208 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
213 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
214 xx = rpmtsSetNotifyCallback(ts,
215 rpmShowProgress, (void *) ((long)notifyFlags));
234 rpmRelocation * relocations;
238 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
246 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
248 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
250 if (rpmtsCheck(ts)) {
255 ps = rpmtsProblems(ts);
256 if (!stop && rpmpsNumProblems(ps) > 0) {
257 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
258 rpmpsPrint(NULL, ps);
265 if ((eflags? 1 : (!stop)) && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
266 if (rpmtsOrder(ts)) {
272 if (numPackages && !stop) {
275 rpmlog(RPMLOG_DEBUG, "erasing packages\n");
277 rc = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
279 rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
281 rc = rpmtsRun(ts, NULL, ia->probFilter);
284 ps = rpmtsProblems(ts);
286 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
287 rpmpsPrint((eflags? NULL : stderr), ps);
294 static int tryReadManifest(struct rpmEIU * eiu)
298 /* Try to read a package manifest. */
299 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
300 if (eiu->fd == NULL || Ferror(eiu->fd)) {
301 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
303 if (eiu->fd != NULL) {
304 xx = Fclose(eiu->fd);
307 eiu->numFailed++; *eiu->fnp = NULL;
311 /* Read list of packages from manifest. */
312 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
314 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
315 *eiu->fnp, Fstrerror(eiu->fd));
316 xx = Fclose(eiu->fd);
320 eiu->numFailed++; *eiu->fnp = NULL;
325 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, rpmVSFlags vsflags)
329 /* Try to read the header from a package file. */
330 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
331 if (eiu->fd == NULL || Ferror(eiu->fd)) {
332 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
334 if (eiu->fd != NULL) {
338 eiu->numFailed++; *eiu->fnp = NULL;
342 /* Read the header, verifying signatures (if present). */
343 tvsflags = rpmtsSetVSFlags(ts, vsflags);
344 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
345 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
349 /* Honor --nomanifest */
350 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
351 eiu->rpmrc = RPMRC_FAIL;
353 if(eiu->rpmrc == RPMRC_FAIL) {
354 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
355 eiu->numFailed++; *eiu->fnp = NULL;
361 /** @todo Generalize --freshen policies. */
362 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
364 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
365 rpmRelocation * relocations;
366 char * fileURL = NULL;
367 rpmVSFlags vsflags, ovsflags;
372 if (fileArgv == NULL) goto exit;
374 rpmcliPackagesTotal = 0;
376 (void) rpmtsSetFlags(ts, ia->transFlags);
378 relocations = ia->relocations;
380 vsflags = setvsFlags(ia);
381 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
383 setNotifyFlag(ia, ts);
385 if ((eiu->relocations = relocations) != NULL) {
386 while (eiu->relocations->oldPath)
388 if (eiu->relocations->newPath == NULL)
389 eiu->relocations = NULL;
392 /* Build fully globbed list of arguments in argv[argc]. */
393 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
398 fn = rpmEscapeSpaces(*eiu->fnp);
399 rc = rpmGlob(fn, &ac, &av);
402 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
407 argvAppend(&(eiu->argv), av);
413 /* Allocate sufficient storage for next set of args. */
414 if (eiu->pkgx >= eiu->numPkgs) {
415 eiu->numPkgs = eiu->pkgx + eiu->argc;
416 eiu->pkgURL = xrealloc(eiu->pkgURL,
417 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
418 memset(eiu->pkgURL + eiu->pkgx, 0,
419 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
420 eiu->pkgState = xrealloc(eiu->pkgState,
421 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
422 memset(eiu->pkgState + eiu->pkgx, 0,
423 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
426 /* Retrieve next set of args, cache on local storage. */
427 for (i = 0; i < eiu->argc; i++) {
428 fileURL = _free(fileURL);
429 fileURL = eiu->argv[i];
432 switch (urlIsURL(fileURL)) {
440 fprintf(stdout, _("Retrieving %s\n"), fileURL);
442 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
445 rc = urlGetFile(fileURL, tfn);
452 _("skipping %s - transfer failed\n"), fileURL);
454 eiu->pkgURL[eiu->pkgx] = NULL;
458 eiu->pkgState[eiu->pkgx] = 1;
459 eiu->pkgURL[eiu->pkgx] = tfn;
463 case URL_IS_DASH: /* WRONG WRONG WRONG */
464 case URL_IS_HKP: /* WRONG WRONG WRONG */
466 eiu->pkgURL[eiu->pkgx] = fileURL;
472 fileURL = _free(fileURL);
474 if (eiu->numFailed) goto exit;
476 /* Continue processing file arguments, building transaction set. */
477 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
479 eiu->fnp++, eiu->prevx++)
481 const char * fileName;
483 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
484 (void) urlPath(*eiu->fnp, &fileName);
486 if (tryReadHeader(ts, eiu, vsflags) == RPMRC_FAIL)
489 if (eiu->rpmrc == RPMRC_NOTFOUND) {
490 rc = tryReadManifest(eiu);
491 if (rc == RPMRC_OK) {
497 eiu->isSource = headerIsSource(eiu->h);
500 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
502 eiu->sourceURL = xrealloc(eiu->sourceURL,
503 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
504 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
507 eiu->sourceURL[eiu->numSRPMS] = NULL;
511 if (eiu->relocations) {
512 struct rpmtd_s prefixes;
514 headerGet(eiu->h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
515 if (rpmtdCount(&prefixes) == 1) {
516 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
517 rpmtdFreeData(&prefixes);
520 xx = headerNVR(eiu->h, &name, NULL, NULL);
522 _("package %s is not relocatable\n"), name);
528 /* On --freshen, verify package is installed and newer */
529 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
530 rpmdbMatchIterator mi;
535 xx = headerNVR(eiu->h, &name, NULL, NULL);
536 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
537 count = rpmdbGetIteratorCount(mi);
538 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
539 if (rpmVersionCompare(oldH, eiu->h) < 0)
541 /* same or newer package already installed */
545 mi = rpmdbFreeIterator(mi);
547 eiu->h = headerFree(eiu->h);
550 /* Package is newer than those currently installed. */
553 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
554 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
557 /* XXX reference held by transaction set */
558 eiu->h = headerFree(eiu->h);
559 if (eiu->relocations)
560 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
564 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
569 _("error reading from file %s\n"), *eiu->fnp);
575 _("file %s requires a newer version of RPM\n"),
589 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
590 eiu->numSRPMS, eiu->numRPMS);
592 if (eiu->numFailed) goto exit;
595 int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
597 eiu->numFailed += eiu->numRPMS;
599 eiu->numFailed += rc;
602 if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
603 for (i = 0; i < eiu->numSRPMS; i++) {
605 if (eiu->sourceURL[i] != NULL) {
607 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
608 rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
616 if (eiu->pkgURL != NULL) {
617 for (i = 0; i < eiu->numPkgs; i++) {
618 if (eiu->pkgURL[i] == NULL) continue;
619 if (eiu->pkgState[i] == 1)
620 (void) unlink(eiu->pkgURL[i]);
621 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
624 eiu->pkgState = _free(eiu->pkgState);
625 eiu->pkgURL = _free(eiu->pkgURL);
626 eiu->argv = _free(eiu->argv);
635 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
641 rpmVSFlags vsflags, ovsflags;
643 if (argv == NULL) return 0;
645 vsflags = setvsFlags(ia);
646 ovsflags = rpmtsSetVSFlags(ts, vsflags);
648 /* XXX suggest mechanism only meaningful when installing */
649 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
651 (void) rpmtsSetFlags(ts, ia->transFlags);
653 #ifdef NOTYET /* XXX no callbacks on erase yet */
654 setNotifyFlag(ia, ts);
657 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
658 for (arg = argv; *arg; arg++) {
659 rpmdbMatchIterator mi;
663 /* Iterator count isn't reliable with labels, count manually... */
664 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
665 while (rpmdbNextIterator(mi) != NULL) {
668 rpmdbFreeIterator(mi);
671 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
674 Header h; /* XXX iterator owns the reference */
677 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
678 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
684 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
685 while ((h = rpmdbNextIterator(mi)) != NULL) {
687 (void) rpmtsAddEraseElement(ts, h, -1);
690 char *nevra = headerFormat(h, qfmt, NULL);
691 rpmlog(RPMLOG_NOTICE, " %s", nevra);
695 mi = rpmdbFreeIterator(mi);
700 if (numFailed) goto exit;
701 numFailed = rpmcliTransaction(ts, ia, numPackages);
708 int rpmInstallSource(rpmts ts, const char * arg,
709 char ** specFilePtr, char ** cookie)
715 fd = Fopen(arg, "r.ufdio");
716 if (fd == NULL || Ferror(fd)) {
717 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
718 if (fd != NULL) (void) Fclose(fd);
722 if (rpmIsVerbose() && specFilePtr != NULL)
723 fprintf(stdout, _("Installing %s\n"), arg);
726 rpmVSFlags ovsflags =
727 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
728 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
729 rc = (rpmrc == RPMRC_OK ? 0 : 1);
730 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
733 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
734 if (specFilePtr && *specFilePtr)
735 *specFilePtr = _free(*specFilePtr);
736 if (cookie && *cookie)
737 *cookie = _free(*cookie);