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_loff_t amount, const rpm_loff_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_loff_t amount,
77 const rpm_loff_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 headerFormat() on a progress callback. */
121 if (flags & INSTALL_HASH) {
122 s = headerFormat(h, "%{NAME}", NULL);
123 if (isatty (STDOUT_FILENO))
124 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
126 fprintf(stdout, "%-28.28s", s);
127 (void) fflush(stdout);
130 s = headerFormat(h, "%{NAME}-%{VERSION}-%{RELEASE}", NULL);
131 fprintf(stdout, "%s\n", s);
132 (void) fflush(stdout);
137 case RPMCALLBACK_TRANS_PROGRESS:
138 case RPMCALLBACK_INST_PROGRESS:
139 if (flags & INSTALL_PERCENT)
140 fprintf(stdout, "%%%% %f\n", (double) (total
141 ? ((((float) amount) / total) * 100)
143 else if (flags & INSTALL_HASH)
144 printHash(amount, total);
145 (void) fflush(stdout);
148 case RPMCALLBACK_TRANS_START:
149 rpmcliHashesCurrent = 0;
150 rpmcliProgressTotal = 1;
151 rpmcliProgressCurrent = 0;
152 if (!(flags & INSTALL_LABEL))
154 if (flags & INSTALL_HASH)
155 fprintf(stdout, "%-28s", _("Preparing..."));
157 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
158 (void) fflush(stdout);
161 case RPMCALLBACK_TRANS_STOP:
162 if (flags & INSTALL_HASH)
163 printHash(1, 1); /* Fixes "preparing..." progress bar */
164 rpmcliProgressTotal = rpmcliPackagesTotal;
165 rpmcliProgressCurrent = 0;
168 case RPMCALLBACK_UNINST_PROGRESS:
170 case RPMCALLBACK_UNINST_START:
172 case RPMCALLBACK_UNINST_STOP:
174 case RPMCALLBACK_UNPACK_ERROR:
176 case RPMCALLBACK_CPIO_ERROR:
178 case RPMCALLBACK_SCRIPT_ERROR:
180 case RPMCALLBACK_UNKNOWN:
204 rpmRelocation * relocations;
208 /** @todo Generalize --freshen policies. */
209 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
211 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
213 rpmprobFilterFlags probFilter;
214 rpmRelocation * relocations;
215 char * fileURL = NULL;
217 rpmVSFlags vsflags, ovsflags, tvsflags;
222 if (fileArgv == NULL) goto exit;
224 rpmcliPackagesTotal = 0;
226 (void) rpmtsSetFlags(ts, ia->transFlags);
228 probFilter = ia->probFilter;
229 relocations = ia->relocations;
231 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
232 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
234 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
235 if (ia->qva_flags & VERIFY_DIGEST)
236 vsflags |= _RPMVSF_NODIGESTS;
237 if (ia->qva_flags & VERIFY_SIGNATURE)
238 vsflags |= _RPMVSF_NOSIGNATURES;
239 if (ia->qva_flags & VERIFY_HDRCHK)
240 vsflags |= RPMVSF_NOHDRCHK;
241 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
244 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
245 xx = rpmtsSetNotifyCallback(ts,
246 rpmShowProgress, (void *) ((long)notifyFlags));
249 if ((eiu->relocations = relocations) != NULL) {
250 while (eiu->relocations->oldPath)
252 if (eiu->relocations->newPath == NULL)
253 eiu->relocations = NULL;
256 /* Build fully globbed list of arguments in argv[argc]. */
257 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
262 fn = rpmEscapeSpaces(*eiu->fnp);
263 rc = rpmGlob(fn, &ac, &av);
266 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
270 argvAppend(&(eiu->argv), av);
276 /* Allocate sufficient storage for next set of args. */
277 if (eiu->pkgx >= eiu->numPkgs) {
278 eiu->numPkgs = eiu->pkgx + eiu->argc;
279 eiu->pkgURL = xrealloc(eiu->pkgURL,
280 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
281 memset(eiu->pkgURL + eiu->pkgx, 0,
282 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
283 eiu->pkgState = xrealloc(eiu->pkgState,
284 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
285 memset(eiu->pkgState + eiu->pkgx, 0,
286 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
289 /* Retrieve next set of args, cache on local storage. */
290 for (i = 0; i < eiu->argc; i++) {
291 fileURL = _free(fileURL);
292 fileURL = eiu->argv[i];
295 switch (urlIsURL(fileURL)) {
303 fprintf(stdout, _("Retrieving %s\n"), fileURL);
305 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
308 rc = urlGetFile(fileURL, tfn);
315 _("skipping %s - transfer failed\n"), fileURL);
317 eiu->pkgURL[eiu->pkgx] = NULL;
321 eiu->pkgState[eiu->pkgx] = 1;
322 eiu->pkgURL[eiu->pkgx] = tfn;
326 case URL_IS_DASH: /* WRONG WRONG WRONG */
327 case URL_IS_HKP: /* WRONG WRONG WRONG */
329 eiu->pkgURL[eiu->pkgx] = fileURL;
335 fileURL = _free(fileURL);
337 if (eiu->numFailed) goto exit;
339 /* Continue processing file arguments, building transaction set. */
340 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
342 eiu->fnp++, eiu->prevx++)
344 const char * fileName;
346 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
347 (void) urlPath(*eiu->fnp, &fileName);
349 /* Try to read the header from a package file. */
350 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
351 if (eiu->fd == NULL || Ferror(eiu->fd)) {
352 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
354 if (eiu->fd != NULL) {
355 xx = Fclose(eiu->fd);
358 eiu->numFailed++; *eiu->fnp = NULL;
362 /* Read the header, verifying signatures (if present). */
363 tvsflags = rpmtsSetVSFlags(ts, vsflags);
364 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
365 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
366 xx = Fclose(eiu->fd);
369 switch (eiu->rpmrc) {
371 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
372 eiu->numFailed++; *eiu->fnp = NULL;
378 case RPMRC_NOTTRUSTED:
385 eiu->isSource = headerIsSource(eiu->h);
388 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
390 eiu->sourceURL = xrealloc(eiu->sourceURL,
391 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
392 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
395 eiu->sourceURL[eiu->numSRPMS] = NULL;
399 if (eiu->relocations) {
400 struct rpmtd_s prefixes;
402 headerGet(eiu->h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
403 if (rpmtdCount(&prefixes) == 1) {
404 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
405 rpmtdFreeData(&prefixes);
408 xx = headerNVR(eiu->h, &name, NULL, NULL);
410 _("package %s is not relocatable\n"), name);
416 /* On --freshen, verify package is installed and newer */
417 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
418 rpmdbMatchIterator mi;
423 xx = headerNVR(eiu->h, &name, NULL, NULL);
424 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
425 count = rpmdbGetIteratorCount(mi);
426 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
427 if (rpmVersionCompare(oldH, eiu->h) < 0)
429 /* same or newer package already installed */
433 mi = rpmdbFreeIterator(mi);
435 eiu->h = headerFree(eiu->h);
438 /* Package is newer than those currently installed. */
441 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
442 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
445 /* XXX reference held by transaction set */
446 eiu->h = headerFree(eiu->h);
447 if (eiu->relocations)
448 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
452 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
457 _("error reading from file %s\n"), *eiu->fnp);
463 _("file %s requires a newer version of RPM\n"),
478 /* Try to read a package manifest. */
479 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
480 if (eiu->fd == NULL || Ferror(eiu->fd)) {
481 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
483 if (eiu->fd != NULL) {
484 xx = Fclose(eiu->fd);
487 eiu->numFailed++; *eiu->fnp = NULL;
491 /* Read list of packages from manifest. */
492 /* FIX: *eiu->argv can be NULL */
493 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
495 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
496 *eiu->fnp, Fstrerror(eiu->fd));
497 xx = Fclose(eiu->fd);
500 /* If successful, restart the query loop. */
501 if (rc == RPMRC_OK) {
506 eiu->numFailed++; *eiu->fnp = NULL;
510 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
511 eiu->numSRPMS, eiu->numRPMS);
513 if (eiu->numFailed) goto exit;
515 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
517 if (rpmtsCheck(ts)) {
518 eiu->numFailed = eiu->numPkgs;
522 ps = rpmtsProblems(ts);
523 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
524 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
525 rpmpsPrint(NULL, ps);
526 eiu->numFailed = eiu->numPkgs;
532 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
533 if (rpmtsOrder(ts)) {
534 eiu->numFailed = eiu->numPkgs;
539 if (eiu->numRPMS && !stopInstall) {
541 rpmcliPackagesTotal += eiu->numSRPMS;
543 rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
545 /* Drop added/available package indices and dependency sets. */
548 rc = rpmtsRun(ts, NULL, probFilter);
549 ps = rpmtsProblems(ts);
552 eiu->numFailed += eiu->numRPMS;
554 eiu->numFailed += rc;
555 if (rpmpsNumProblems(ps) > 0)
556 rpmpsPrint(stderr, ps);
561 if (eiu->numSRPMS && !stopInstall) {
562 if (eiu->sourceURL != NULL)
563 for (i = 0; i < eiu->numSRPMS; i++) {
565 if (eiu->sourceURL[i] == NULL) continue;
566 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
567 if (eiu->fd == NULL || Ferror(eiu->fd)) {
568 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
569 eiu->sourceURL[i], Fstrerror(eiu->fd));
570 if (eiu->fd != NULL) {
571 xx = Fclose(eiu->fd);
577 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
578 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
579 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
582 xx = Fclose(eiu->fd);
588 if (eiu->pkgURL != NULL)
589 for (i = 0; i < eiu->numPkgs; i++) {
590 if (eiu->pkgURL[i] == NULL) continue;
591 if (eiu->pkgState[i] == 1)
592 (void) unlink(eiu->pkgURL[i]);
593 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
595 eiu->pkgState = _free(eiu->pkgState);
596 eiu->pkgURL = _free(eiu->pkgURL);
597 eiu->argv = _free(eiu->argv);
606 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
611 int stopUninstall = 0;
613 rpmVSFlags vsflags, ovsflags;
616 if (argv == NULL) return 0;
618 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
619 if (ia->qva_flags & VERIFY_DIGEST)
620 vsflags |= _RPMVSF_NODIGESTS;
621 if (ia->qva_flags & VERIFY_SIGNATURE)
622 vsflags |= _RPMVSF_NOSIGNATURES;
623 if (ia->qva_flags & VERIFY_HDRCHK)
624 vsflags |= RPMVSF_NOHDRCHK;
625 ovsflags = rpmtsSetVSFlags(ts, vsflags);
627 /* XXX suggest mechanism only meaningful when installing */
628 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
630 (void) rpmtsSetFlags(ts, ia->transFlags);
632 #ifdef NOTYET /* XXX no callbacks on erase yet */
633 { int notifyFlags, xx;
634 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
635 xx = rpmtsSetNotifyCallback(ts,
636 rpmShowProgress, (void *) ((long)notifyFlags));
640 for (arg = argv; *arg; arg++) {
641 rpmdbMatchIterator mi;
643 /* XXX HACK to get rpmdbFindByLabel out of the API */
644 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
646 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
649 Header h; /* XXX iterator owns the reference */
651 while ((h = rpmdbNextIterator(mi)) != NULL) {
652 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
654 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
655 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
661 (void) rpmtsAddEraseElement(ts, h, recOffset);
666 mi = rpmdbFreeIterator(mi);
669 if (numFailed) goto exit;
671 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
673 if (rpmtsCheck(ts)) {
674 numFailed = numPackages;
678 ps = rpmtsProblems(ts);
679 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
680 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
681 rpmpsPrint(NULL, ps);
682 numFailed += numPackages;
688 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
689 if (rpmtsOrder(ts)) {
690 numFailed += numPackages;
695 if (numPackages && !stopUninstall) {
696 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
698 /* Drop added/available package indices and dependency sets. */
701 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
702 ps = rpmtsProblems(ts);
703 if (rpmpsNumProblems(ps) > 0)
704 rpmpsPrint(NULL, ps);
705 numFailed += numPackages;
716 int rpmInstallSource(rpmts ts, const char * arg,
717 char ** specFilePtr, char ** cookie)
723 fd = Fopen(arg, "r.ufdio");
724 if (fd == NULL || Ferror(fd)) {
725 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
726 if (fd != NULL) (void) Fclose(fd);
731 fprintf(stdout, _("Installing %s\n"), arg);
734 rpmVSFlags ovsflags =
735 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
736 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
737 rc = (rpmrc == RPMRC_OK ? 0 : 1);
738 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
741 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
742 if (specFilePtr && *specFilePtr)
743 *specFilePtr = _free(*specFilePtr);
744 if (cookie && *cookie)
745 *cookie = _free(*cookie);