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 static int rpmcliPackagesTotal = 0;
21 static int rpmcliHashesCurrent = 0;
22 static int rpmcliHashesTotal = 0;
23 static int rpmcliProgressCurrent = 0;
24 static 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) ? 34 : 40);
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;
102 int flags = (int) ((long)data);
104 const char * filename = (const char *)key;
105 static FD_t fd = NULL;
106 static int state = -1;
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 case RPMCALLBACK_UNINST_START:
139 if (flags & INSTALL_HASH) {
140 if (what == RPMCALLBACK_INST_START) {
141 fprintf(stdout, _("Updating / installing...\n"));
143 fprintf(stdout, _("Cleaning up / removing...\n"));
149 rpmcliHashesCurrent = 0;
150 if (h == NULL || !(flags & INSTALL_LABEL))
152 if (flags & INSTALL_HASH) {
153 char *s = headerGetAsString(h, RPMTAG_NEVR);
154 if (isatty (STDOUT_FILENO))
155 fprintf(stdout, "%4d:%-33.33s", rpmcliProgressCurrent + 1, s);
157 fprintf(stdout, "%-38.38s", s);
158 (void) fflush(stdout);
161 char *s = headerGetAsString(h, RPMTAG_NEVRA);
162 fprintf(stdout, "%s\n", s);
163 (void) fflush(stdout);
168 case RPMCALLBACK_INST_STOP:
171 case RPMCALLBACK_TRANS_PROGRESS:
172 case RPMCALLBACK_INST_PROGRESS:
173 case RPMCALLBACK_UNINST_PROGRESS:
174 if (flags & INSTALL_PERCENT)
175 fprintf(stdout, "%%%% %f\n", (double) (total
176 ? ((((float) amount) / total) * 100)
178 else if (flags & INSTALL_HASH)
179 printHash(amount, total);
180 (void) fflush(stdout);
183 case RPMCALLBACK_TRANS_START:
184 rpmcliHashesCurrent = 0;
185 rpmcliProgressTotal = 1;
186 rpmcliProgressCurrent = 0;
187 rpmcliPackagesTotal = total;
189 if (!(flags & INSTALL_LABEL))
191 if (flags & INSTALL_HASH)
192 fprintf(stdout, "%-38s", _("Preparing..."));
194 fprintf(stdout, "%s\n", _("Preparing packages..."));
195 (void) fflush(stdout);
198 case RPMCALLBACK_TRANS_STOP:
199 if (flags & INSTALL_HASH)
200 printHash(1, 1); /* Fixes "preparing..." progress bar */
201 rpmcliProgressTotal = rpmcliPackagesTotal;
202 rpmcliProgressCurrent = 0;
205 case RPMCALLBACK_UNINST_STOP:
207 case RPMCALLBACK_UNPACK_ERROR:
209 case RPMCALLBACK_CPIO_ERROR:
211 case RPMCALLBACK_SCRIPT_ERROR:
213 case RPMCALLBACK_SCRIPT_START:
215 case RPMCALLBACK_SCRIPT_STOP:
217 case RPMCALLBACK_UNKNOWN:
225 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
230 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
231 rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
247 rpmRelocation * relocations;
251 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
259 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
261 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
263 if (rpmtsCheck(ts)) {
268 ps = rpmtsProblems(ts);
269 if (!stop && rpmpsNumProblems(ps) > 0) {
270 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
271 rpmpsPrint(NULL, ps);
278 if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
279 if (rpmtsOrder(ts)) {
285 if (numPackages && !stop) {
286 rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
287 "installing binary packages\n");
289 rc = rpmtsRun(ts, NULL, ia->probFilter);
291 ps = rpmtsProblems(ts);
293 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
294 rpmpsPrint((eflags? NULL : stderr), ps);
301 static int tryReadManifest(struct rpmEIU * eiu)
305 /* Try to read a package manifest. */
306 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
307 if (fd == NULL || Ferror(fd)) {
308 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
314 eiu->numFailed++; *eiu->fnp = NULL;
318 /* Read list of packages from manifest. */
319 rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
321 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
322 *eiu->fnp, Fstrerror(fd));
327 eiu->numFailed++; *eiu->fnp = NULL;
332 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, Header * hdrp)
334 /* Try to read the header from a package file. */
335 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
336 if (fd == NULL || Ferror(fd)) {
337 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
343 eiu->numFailed++; *eiu->fnp = NULL;
347 /* Read the header, verifying signatures (if present). */
348 eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, hdrp);
352 /* Honor --nomanifest */
353 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
354 eiu->rpmrc = RPMRC_FAIL;
356 if(eiu->rpmrc == RPMRC_FAIL) {
357 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
358 eiu->numFailed++; *eiu->fnp = NULL;
365 /* On --freshen, verify package is installed and newer */
366 static int checkFreshenStatus(rpmts ts, Header h)
368 rpmdbMatchIterator mi = NULL;
369 const char * name = headerGetString(h, RPMTAG_NAME);
370 const char *arch = headerGetString(h, RPMTAG_ARCH);
374 mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
375 if (rpmtsColor(ts) && arch)
376 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
378 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
379 /* Package is newer than those currently installed. */
380 if (rpmVersionCompare(oldH, h) < 0)
384 rpmdbFreeIterator(mi);
385 return (oldH != NULL);
388 /** @todo Generalize --freshen policies. */
389 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
391 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
392 rpmRelocation * relocations;
393 char * fileURL = NULL;
394 rpmVSFlags vsflags, ovsflags;
398 vsflags = setvsFlags(ia);
399 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
401 if (fileArgv == NULL) goto exit;
403 (void) rpmtsSetFlags(ts, ia->transFlags);
405 relocations = ia->relocations;
407 setNotifyFlag(ia, ts);
409 if ((eiu->relocations = relocations) != NULL) {
410 while (eiu->relocations->oldPath)
412 if (eiu->relocations->newPath == NULL)
413 eiu->relocations = NULL;
416 /* Build fully globbed list of arguments in argv[argc]. */
417 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
422 fn = rpmEscapeSpaces(*eiu->fnp);
423 rc = rpmGlob(fn, &ac, &av);
426 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
431 argvAppend(&(eiu->argv), av);
437 /* Allocate sufficient storage for next set of args. */
438 if (eiu->pkgx >= eiu->numPkgs) {
439 eiu->numPkgs = eiu->pkgx + eiu->argc;
440 eiu->pkgURL = xrealloc(eiu->pkgURL,
441 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
442 memset(eiu->pkgURL + eiu->pkgx, 0,
443 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
444 eiu->pkgState = xrealloc(eiu->pkgState,
445 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
446 memset(eiu->pkgState + eiu->pkgx, 0,
447 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
450 /* Retrieve next set of args, cache on local storage. */
451 for (i = 0; i < eiu->argc; i++) {
452 fileURL = _free(fileURL);
453 fileURL = eiu->argv[i];
456 switch (urlIsURL(fileURL)) {
464 fprintf(stdout, _("Retrieving %s\n"), fileURL);
466 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
469 rc = urlGetFile(fileURL, tfn);
476 _("skipping %s - transfer failed\n"), fileURL);
478 eiu->pkgURL[eiu->pkgx] = NULL;
482 eiu->pkgState[eiu->pkgx] = 1;
483 eiu->pkgURL[eiu->pkgx] = tfn;
487 case URL_IS_DASH: /* WRONG WRONG WRONG */
488 case URL_IS_HKP: /* WRONG WRONG WRONG */
490 eiu->pkgURL[eiu->pkgx] = fileURL;
496 fileURL = _free(fileURL);
498 if (eiu->numFailed) goto exit;
500 /* Continue processing file arguments, building transaction set. */
501 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
503 eiu->fnp++, eiu->prevx++)
506 const char * fileName;
508 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
509 (void) urlPath(*eiu->fnp, &fileName);
511 if (tryReadHeader(ts, eiu, &h) == RPMRC_FAIL)
514 if (eiu->rpmrc == RPMRC_NOTFOUND) {
515 rc = tryReadManifest(eiu);
516 if (rc == RPMRC_OK) {
522 if (headerIsSource(h)) {
523 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
525 eiu->sourceURL = xrealloc(eiu->sourceURL,
526 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
527 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
530 eiu->sourceURL[eiu->numSRPMS] = NULL;
534 if (eiu->relocations) {
535 struct rpmtd_s prefixes;
537 headerGet(h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
538 if (rpmtdCount(&prefixes) == 1) {
539 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
540 rpmtdFreeData(&prefixes);
542 rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
543 headerGetString(h, RPMTAG_NAME));
549 if (ia->installInterfaceFlags & INSTALL_FRESHEN)
550 if (checkFreshenStatus(ts, h) != 1) {
555 rc = rpmtsAddInstallElement(ts, h, (fnpyKey)fileName,
556 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
560 if (eiu->relocations)
561 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
565 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
570 _("error reading from file %s\n"), *eiu->fnp);
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);
625 rpmtsSetVSFlags(ts, ovsflags);
630 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
636 rpmVSFlags vsflags, ovsflags;
638 if (argv == NULL) return 0;
640 vsflags = setvsFlags(ia);
641 ovsflags = rpmtsSetVSFlags(ts, vsflags);
643 (void) rpmtsSetFlags(ts, ia->transFlags);
645 setNotifyFlag(ia, ts);
647 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
648 for (arg = argv; *arg; arg++) {
649 rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
650 int matches = rpmdbGetIteratorCount(mi);
654 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
657 Header h; /* XXX iterator owns the reference */
660 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
661 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
667 while ((h = rpmdbNextIterator(mi)) != NULL) {
669 (void) rpmtsAddEraseElement(ts, h, -1);
672 char *nevra = headerFormat(h, qfmt, NULL);
673 rpmlog(RPMLOG_NOTICE, " %s", nevra);
678 rpmdbFreeIterator(mi);
682 if (numFailed) goto exit;
683 numFailed = rpmcliTransaction(ts, ia, numPackages);
686 rpmtsSetVSFlags(ts, ovsflags);
691 int rpmInstallSource(rpmts ts, const char * arg,
692 char ** specFilePtr, char ** cookie)
698 fd = Fopen(arg, "r.ufdio");
699 if (fd == NULL || Ferror(fd)) {
700 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
701 if (fd != NULL) (void) Fclose(fd);
705 if (rpmIsVerbose() && specFilePtr != NULL)
706 fprintf(stdout, _("Installing %s\n"), arg);
709 rpmVSFlags ovsflags =
710 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
711 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
712 rc = (rpmrc == RPMRC_OK ? 0 : 1);
713 rpmtsSetVSFlags(ts, ovsflags);
716 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
717 if (specFilePtr && *specFilePtr)
718 *specFilePtr = _free(*specFilePtr);
719 if (cookie && *cookie)
720 *cookie = _free(*cookie);