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) ? 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;
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 /* @todo Remove headerFormat() on a progress callback. */
153 if (flags & INSTALL_HASH) {
154 char *s = headerFormat(h, "%{NAME}", NULL);
155 if (isatty (STDOUT_FILENO))
156 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
158 fprintf(stdout, "%-28.28s", s);
159 (void) fflush(stdout);
162 char *s = headerFormat(h, "%{NAME}-%{VERSION}-%{RELEASE}", NULL);
163 fprintf(stdout, "%s\n", s);
164 (void) fflush(stdout);
169 case RPMCALLBACK_INST_STOP:
172 case RPMCALLBACK_TRANS_PROGRESS:
173 case RPMCALLBACK_INST_PROGRESS:
174 case RPMCALLBACK_UNINST_PROGRESS:
175 if (flags & INSTALL_PERCENT)
176 fprintf(stdout, "%%%% %f\n", (double) (total
177 ? ((((float) amount) / total) * 100)
179 else if (flags & INSTALL_HASH)
180 printHash(amount, total);
181 (void) fflush(stdout);
184 case RPMCALLBACK_TRANS_START:
185 rpmcliHashesCurrent = 0;
186 rpmcliProgressTotal = 1;
187 rpmcliProgressCurrent = 0;
188 rpmcliPackagesTotal = total;
190 if (!(flags & INSTALL_LABEL))
192 if (flags & INSTALL_HASH)
193 fprintf(stdout, "%-28s", _("Preparing..."));
195 fprintf(stdout, "%s\n", _("Preparing packages..."));
196 (void) fflush(stdout);
199 case RPMCALLBACK_TRANS_STOP:
200 if (flags & INSTALL_HASH)
201 printHash(1, 1); /* Fixes "preparing..." progress bar */
202 rpmcliProgressTotal = rpmcliPackagesTotal;
203 rpmcliProgressCurrent = 0;
206 case RPMCALLBACK_UNINST_STOP:
208 case RPMCALLBACK_UNPACK_ERROR:
210 case RPMCALLBACK_CPIO_ERROR:
212 case RPMCALLBACK_SCRIPT_ERROR:
214 case RPMCALLBACK_SCRIPT_START:
216 case RPMCALLBACK_SCRIPT_STOP:
218 case RPMCALLBACK_UNKNOWN:
226 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
231 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
232 rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
248 rpmRelocation * relocations;
252 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
260 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
262 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
264 if (rpmtsCheck(ts)) {
269 ps = rpmtsProblems(ts);
270 if (!stop && rpmpsNumProblems(ps) > 0) {
271 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
272 rpmpsPrint(NULL, ps);
279 if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
280 if (rpmtsOrder(ts)) {
286 if (numPackages && !stop) {
287 rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
288 "installing binary packages\n");
290 rc = rpmtsRun(ts, NULL, ia->probFilter);
292 ps = rpmtsProblems(ts);
294 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
295 rpmpsPrint((eflags? NULL : stderr), ps);
302 static int tryReadManifest(struct rpmEIU * eiu)
306 /* Try to read a package manifest. */
307 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
308 if (fd == NULL || Ferror(fd)) {
309 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
315 eiu->numFailed++; *eiu->fnp = NULL;
319 /* Read list of packages from manifest. */
320 rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
322 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
323 *eiu->fnp, Fstrerror(fd));
328 eiu->numFailed++; *eiu->fnp = NULL;
333 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, Header * hdrp)
335 /* Try to read the header from a package file. */
336 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
337 if (fd == NULL || Ferror(fd)) {
338 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
344 eiu->numFailed++; *eiu->fnp = NULL;
348 /* Read the header, verifying signatures (if present). */
349 eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, hdrp);
353 /* Honor --nomanifest */
354 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
355 eiu->rpmrc = RPMRC_FAIL;
357 if(eiu->rpmrc == RPMRC_FAIL) {
358 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
359 eiu->numFailed++; *eiu->fnp = NULL;
366 /* On --freshen, verify package is installed and newer */
367 static int checkFreshenStatus(rpmts ts, Header h)
369 rpmdbMatchIterator mi = NULL;
370 const char * name = headerGetString(h, RPMTAG_NAME);
371 const char *arch = headerGetString(h, RPMTAG_ARCH);
375 mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
376 if (rpmtsColor(ts) && arch)
377 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
379 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
380 /* Package is newer than those currently installed. */
381 if (rpmVersionCompare(oldH, h) < 0)
385 rpmdbFreeIterator(mi);
386 return (oldH != NULL);
389 /** @todo Generalize --freshen policies. */
390 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
392 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
393 rpmRelocation * relocations;
394 char * fileURL = NULL;
395 rpmVSFlags vsflags, ovsflags;
399 vsflags = setvsFlags(ia);
400 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
402 if (fileArgv == NULL) goto exit;
404 (void) rpmtsSetFlags(ts, ia->transFlags);
406 relocations = ia->relocations;
408 setNotifyFlag(ia, ts);
410 if ((eiu->relocations = relocations) != NULL) {
411 while (eiu->relocations->oldPath)
413 if (eiu->relocations->newPath == NULL)
414 eiu->relocations = NULL;
417 /* Build fully globbed list of arguments in argv[argc]. */
418 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
423 fn = rpmEscapeSpaces(*eiu->fnp);
424 rc = rpmGlob(fn, &ac, &av);
427 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
432 argvAppend(&(eiu->argv), av);
438 /* Allocate sufficient storage for next set of args. */
439 if (eiu->pkgx >= eiu->numPkgs) {
440 eiu->numPkgs = eiu->pkgx + eiu->argc;
441 eiu->pkgURL = xrealloc(eiu->pkgURL,
442 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
443 memset(eiu->pkgURL + eiu->pkgx, 0,
444 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
445 eiu->pkgState = xrealloc(eiu->pkgState,
446 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
447 memset(eiu->pkgState + eiu->pkgx, 0,
448 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
451 /* Retrieve next set of args, cache on local storage. */
452 for (i = 0; i < eiu->argc; i++) {
453 fileURL = _free(fileURL);
454 fileURL = eiu->argv[i];
457 switch (urlIsURL(fileURL)) {
465 fprintf(stdout, _("Retrieving %s\n"), fileURL);
467 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
470 rc = urlGetFile(fileURL, tfn);
477 _("skipping %s - transfer failed\n"), fileURL);
479 eiu->pkgURL[eiu->pkgx] = NULL;
483 eiu->pkgState[eiu->pkgx] = 1;
484 eiu->pkgURL[eiu->pkgx] = tfn;
488 case URL_IS_DASH: /* WRONG WRONG WRONG */
489 case URL_IS_HKP: /* WRONG WRONG WRONG */
491 eiu->pkgURL[eiu->pkgx] = fileURL;
497 fileURL = _free(fileURL);
499 if (eiu->numFailed) goto exit;
501 /* Continue processing file arguments, building transaction set. */
502 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
504 eiu->fnp++, eiu->prevx++)
507 const char * fileName;
509 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
510 (void) urlPath(*eiu->fnp, &fileName);
512 if (tryReadHeader(ts, eiu, &h) == RPMRC_FAIL)
515 if (eiu->rpmrc == RPMRC_NOTFOUND) {
516 rc = tryReadManifest(eiu);
517 if (rc == RPMRC_OK) {
523 if (headerIsSource(h)) {
524 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
526 eiu->sourceURL = xrealloc(eiu->sourceURL,
527 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
528 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
531 eiu->sourceURL[eiu->numSRPMS] = NULL;
535 if (eiu->relocations) {
536 struct rpmtd_s prefixes;
538 headerGet(h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
539 if (rpmtdCount(&prefixes) == 1) {
540 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
541 rpmtdFreeData(&prefixes);
543 rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
544 headerGetString(h, RPMTAG_NAME));
550 if (ia->installInterfaceFlags & INSTALL_FRESHEN)
551 if (checkFreshenStatus(ts, h) != 1) {
556 rc = rpmtsAddInstallElement(ts, h, (fnpyKey)fileName,
557 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
561 if (eiu->relocations)
562 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
566 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
571 _("error reading from file %s\n"), *eiu->fnp);
584 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
585 eiu->numSRPMS, eiu->numRPMS);
587 if (eiu->numFailed) goto exit;
590 int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
592 eiu->numFailed += eiu->numRPMS;
594 eiu->numFailed += rc;
597 if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
598 for (i = 0; i < eiu->numSRPMS; i++) {
600 if (eiu->sourceURL[i] != NULL) {
602 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
603 rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
611 if (eiu->pkgURL != NULL) {
612 for (i = 0; i < eiu->numPkgs; i++) {
613 if (eiu->pkgURL[i] == NULL) continue;
614 if (eiu->pkgState[i] == 1)
615 (void) unlink(eiu->pkgURL[i]);
616 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
619 eiu->pkgState = _free(eiu->pkgState);
620 eiu->pkgURL = _free(eiu->pkgURL);
621 eiu->argv = _free(eiu->argv);
626 rpmtsSetVSFlags(ts, ovsflags);
631 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
637 rpmVSFlags vsflags, ovsflags;
639 if (argv == NULL) return 0;
641 vsflags = setvsFlags(ia);
642 ovsflags = rpmtsSetVSFlags(ts, vsflags);
644 (void) rpmtsSetFlags(ts, ia->transFlags);
646 setNotifyFlag(ia, ts);
648 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
649 for (arg = argv; *arg; arg++) {
650 rpmdbMatchIterator mi;
654 /* Iterator count isn't reliable with labels, count manually... */
655 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
656 while (rpmdbNextIterator(mi) != NULL) {
659 rpmdbFreeIterator(mi);
662 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
665 Header h; /* XXX iterator owns the reference */
668 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
669 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
675 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
676 while ((h = rpmdbNextIterator(mi)) != NULL) {
678 (void) rpmtsAddEraseElement(ts, h, -1);
681 char *nevra = headerFormat(h, qfmt, NULL);
682 rpmlog(RPMLOG_NOTICE, " %s", nevra);
686 rpmdbFreeIterator(mi);
691 if (numFailed) goto exit;
692 numFailed = rpmcliTransaction(ts, ia, numPackages);
695 rpmtsSetVSFlags(ts, ovsflags);
700 int rpmInstallSource(rpmts ts, const char * arg,
701 char ** specFilePtr, char ** cookie)
707 fd = Fopen(arg, "r.ufdio");
708 if (fd == NULL || Ferror(fd)) {
709 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
710 if (fd != NULL) (void) Fclose(fd);
714 if (rpmIsVerbose() && specFilePtr != NULL)
715 fprintf(stdout, _("Installing %s\n"), arg);
718 rpmVSFlags ovsflags =
719 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
720 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
721 rc = (rpmrc == RPMRC_OK ? 0 : 1);
722 rpmtsSetVSFlags(ts, ovsflags);
725 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
726 if (specFilePtr && *specFilePtr)
727 *specFilePtr = _free(*specFilePtr);
728 if (cookie && *cookie)
729 *cookie = _free(*cookie);