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_off_t amount, const rpm_off_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_off_t amount,
77 const rpm_off_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 headerSprintf() on a progress callback. */
121 if (flags & INSTALL_HASH) {
122 s = headerSprintf(h, "%{NAME}",
123 rpmTagTable, rpmHeaderFormats, NULL);
124 if (isatty (STDOUT_FILENO))
125 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
127 fprintf(stdout, "%-28.28s", s);
128 (void) fflush(stdout);
131 s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
132 rpmTagTable, rpmHeaderFormats, NULL);
133 fprintf(stdout, "%s\n", s);
134 (void) fflush(stdout);
139 case RPMCALLBACK_TRANS_PROGRESS:
140 case RPMCALLBACK_INST_PROGRESS:
141 if (flags & INSTALL_PERCENT)
142 fprintf(stdout, "%%%% %f\n", (double) (total
143 ? ((((float) amount) / total) * 100)
145 else if (flags & INSTALL_HASH)
146 printHash(amount, total);
147 (void) fflush(stdout);
150 case RPMCALLBACK_TRANS_START:
151 rpmcliHashesCurrent = 0;
152 rpmcliProgressTotal = 1;
153 rpmcliProgressCurrent = 0;
154 if (!(flags & INSTALL_LABEL))
156 if (flags & INSTALL_HASH)
157 fprintf(stdout, "%-28s", _("Preparing..."));
159 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
160 (void) fflush(stdout);
163 case RPMCALLBACK_TRANS_STOP:
164 if (flags & INSTALL_HASH)
165 printHash(1, 1); /* Fixes "preparing..." progress bar */
166 rpmcliProgressTotal = rpmcliPackagesTotal;
167 rpmcliProgressCurrent = 0;
170 case RPMCALLBACK_UNINST_PROGRESS:
172 case RPMCALLBACK_UNINST_START:
174 case RPMCALLBACK_UNINST_STOP:
176 case RPMCALLBACK_UNPACK_ERROR:
178 case RPMCALLBACK_CPIO_ERROR:
180 case RPMCALLBACK_SCRIPT_ERROR:
182 case RPMCALLBACK_UNKNOWN:
190 typedef const char * str_t;
208 rpmRelocation * relocations;
212 /** @todo Generalize --freshen policies. */
213 int rpmInstall(rpmts ts,
214 struct rpmInstallArguments_s * ia,
215 const char ** fileArgv)
217 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
219 rpmprobFilterFlags probFilter;
220 rpmRelocation * relocations;
221 char * fileURL = NULL;
224 rpmVSFlags vsflags, ovsflags, tvsflags;
230 if (fileArgv == NULL) goto exit;
232 rpmcliPackagesTotal = 0;
234 (void) rpmtsSetFlags(ts, ia->transFlags);
236 probFilter = ia->probFilter;
237 relocations = ia->relocations;
239 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
240 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
242 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
243 if (ia->qva_flags & VERIFY_DIGEST)
244 vsflags |= _RPMVSF_NODIGESTS;
245 if (ia->qva_flags & VERIFY_SIGNATURE)
246 vsflags |= _RPMVSF_NOSIGNATURES;
247 if (ia->qva_flags & VERIFY_HDRCHK)
248 vsflags |= RPMVSF_NOHDRCHK;
249 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
252 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
253 xx = rpmtsSetNotifyCallback(ts,
254 rpmShowProgress, (void *) ((long)notifyFlags));
257 if ((eiu->relocations = relocations) != NULL) {
258 while (eiu->relocations->oldPath)
260 if (eiu->relocations->newPath == NULL)
261 eiu->relocations = NULL;
264 /* Build fully globbed list of arguments in argv[argc]. */
265 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
267 av = _free(av); ac = 0;
268 fn = rpmEscapeSpaces(*eiu->fnp);
269 rc = rpmGlob(fn, &ac, &av);
272 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
276 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
277 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
279 eiu->argv[eiu->argc] = NULL;
281 av = _free(av); ac = 0;
284 /* Allocate sufficient storage for next set of args. */
285 if (eiu->pkgx >= eiu->numPkgs) {
286 eiu->numPkgs = eiu->pkgx + eiu->argc;
287 eiu->pkgURL = xrealloc(eiu->pkgURL,
288 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
289 memset(eiu->pkgURL + eiu->pkgx, 0,
290 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
291 eiu->pkgState = xrealloc(eiu->pkgState,
292 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
293 memset(eiu->pkgState + eiu->pkgx, 0,
294 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
297 /* Retrieve next set of args, cache on local storage. */
298 for (i = 0; i < eiu->argc; i++) {
299 fileURL = _free(fileURL);
300 fileURL = eiu->argv[i];
304 if (fileURL[0] == '=') {
305 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
307 xx = rpmtsSolve(ts, this, NULL);
308 if (ts->suggests && ts->nsuggests > 0) {
309 fileURL = _free(fileURL);
310 fileURL = ts->suggests[0];
311 ts->suggests[0] = NULL;
312 while (ts->nsuggests-- > 0) {
313 if (ts->suggests[ts->nsuggests] == NULL)
315 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
317 ts->suggests = _free(ts->suggests);
318 rpmlog(RPMLOG_DEBUG, "Adding goal: %s\n", fileURL);
319 eiu->pkgURL[eiu->pkgx] = fileURL;
323 this = rpmdsFree(this);
327 switch (urlIsURL(fileURL)) {
334 fprintf(stdout, _("Retrieving %s\n"), fileURL);
337 const char * rootDir = rpmtsRootDir(ts);
338 if (!(rootDir && * rootDir))
340 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
341 #if defined(HAVE_MKSTEMP)
342 (void) close(mkstemp(tfnbuf));
344 (void) mktemp(tfnbuf);
346 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
349 /* XXX undefined %{name}/%{version}/%{release} here */
350 /* XXX %{_tmpdir} does not exist */
351 rpmlog(RPMLOG_DEBUG, " ... as %s\n", tfn);
352 rc = urlGetFile(fileURL, tfn);
355 _("skipping %s - transfer failed - %s\n"),
356 fileURL, ftpStrerror(rc));
358 eiu->pkgURL[eiu->pkgx] = NULL;
362 eiu->pkgState[eiu->pkgx] = 1;
363 eiu->pkgURL[eiu->pkgx] = tfn;
367 case URL_IS_DASH: /* WRONG WRONG WRONG */
368 case URL_IS_HKP: /* WRONG WRONG WRONG */
370 eiu->pkgURL[eiu->pkgx] = fileURL;
376 fileURL = _free(fileURL);
378 if (eiu->numFailed) goto exit;
380 /* Continue processing file arguments, building transaction set. */
381 for (eiu->fnp = (const char**) eiu->pkgURL+eiu->prevx;
383 eiu->fnp++, eiu->prevx++)
385 const char * fileName;
387 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
388 (void) urlPath(*eiu->fnp, &fileName);
390 /* Try to read the header from a package file. */
391 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
392 if (eiu->fd == NULL || Ferror(eiu->fd)) {
393 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
395 if (eiu->fd != NULL) {
396 xx = Fclose(eiu->fd);
399 eiu->numFailed++; *eiu->fnp = NULL;
403 /* Read the header, verifying signatures (if present). */
404 tvsflags = rpmtsSetVSFlags(ts, vsflags);
405 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
406 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
407 xx = Fclose(eiu->fd);
410 switch (eiu->rpmrc) {
412 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
413 eiu->numFailed++; *eiu->fnp = NULL;
419 case RPMRC_NOTTRUSTED:
426 eiu->isSource = headerIsSource(eiu->h);
429 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
431 eiu->sourceURL = xrealloc(eiu->sourceURL,
432 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
433 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
436 eiu->sourceURL[eiu->numSRPMS] = NULL;
440 if (eiu->relocations) {
445 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
446 (rpm_data_t *) &paths, &c) && (c == 1))
448 eiu->relocations->oldPath = xstrdup(paths[0]);
449 paths = headerFreeData(paths, pft);
452 xx = headerNVR(eiu->h, &name, NULL, NULL);
454 _("package %s is not relocatable\n"), name);
460 /* On --freshen, verify package is installed and newer */
461 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
462 rpmdbMatchIterator mi;
467 xx = headerNVR(eiu->h, &name, NULL, NULL);
468 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
469 count = rpmdbGetIteratorCount(mi);
470 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
471 if (rpmVersionCompare(oldH, eiu->h) < 0)
473 /* same or newer package already installed */
477 mi = rpmdbFreeIterator(mi);
479 eiu->h = headerFree(eiu->h);
482 /* Package is newer than those currently installed. */
485 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
486 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
489 /* XXX reference held by transaction set */
490 eiu->h = headerFree(eiu->h);
491 if (eiu->relocations)
492 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
496 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
501 _("error reading from file %s\n"), *eiu->fnp);
507 _("file %s requires a newer version of RPM\n"),
522 /* Try to read a package manifest. */
523 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
524 if (eiu->fd == NULL || Ferror(eiu->fd)) {
525 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
527 if (eiu->fd != NULL) {
528 xx = Fclose(eiu->fd);
531 eiu->numFailed++; *eiu->fnp = NULL;
535 /* Read list of packages from manifest. */
536 /* FIX: *eiu->argv can be NULL */
537 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
539 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
540 *eiu->fnp, Fstrerror(eiu->fd));
541 xx = Fclose(eiu->fd);
544 /* If successful, restart the query loop. */
545 if (rc == RPMRC_OK) {
550 eiu->numFailed++; *eiu->fnp = NULL;
554 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
555 eiu->numSRPMS, eiu->numRPMS);
557 if (eiu->numFailed) goto exit;
559 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
561 if (rpmtsCheck(ts)) {
562 eiu->numFailed = eiu->numPkgs;
566 ps = rpmtsProblems(ts);
567 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
568 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
569 rpmpsPrint(NULL, ps);
570 eiu->numFailed = eiu->numPkgs;
573 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
574 rpmtsPrintSuggests(ts);
580 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
581 if (rpmtsOrder(ts)) {
582 eiu->numFailed = eiu->numPkgs;
587 if (eiu->numRPMS && !stopInstall) {
589 rpmcliPackagesTotal += eiu->numSRPMS;
591 rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
593 /* Drop added/available package indices and dependency sets. */
596 rc = rpmtsRun(ts, NULL, probFilter);
597 ps = rpmtsProblems(ts);
600 eiu->numFailed += eiu->numRPMS;
602 eiu->numFailed += rc;
603 if (rpmpsNumProblems(ps) > 0)
604 rpmpsPrint(stderr, ps);
609 if (eiu->numSRPMS && !stopInstall) {
610 if (eiu->sourceURL != NULL)
611 for (i = 0; i < eiu->numSRPMS; i++) {
613 if (eiu->sourceURL[i] == NULL) continue;
614 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
615 if (eiu->fd == NULL || Ferror(eiu->fd)) {
616 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
617 eiu->sourceURL[i], Fstrerror(eiu->fd));
618 if (eiu->fd != NULL) {
619 xx = Fclose(eiu->fd);
625 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
626 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
627 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
630 xx = Fclose(eiu->fd);
636 if (eiu->pkgURL != NULL)
637 for (i = 0; i < eiu->numPkgs; i++) {
638 if (eiu->pkgURL[i] == NULL) continue;
639 if (eiu->pkgState[i] == 1)
640 (void) unlink(eiu->pkgURL[i]);
641 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
643 eiu->pkgState = _free(eiu->pkgState);
644 eiu->pkgURL = _free(eiu->pkgURL);
645 eiu->argv = _free(eiu->argv);
649 return eiu->numFailed;
652 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
658 int stopUninstall = 0;
660 rpmVSFlags vsflags, ovsflags;
663 if (argv == NULL) return 0;
665 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
666 if (ia->qva_flags & VERIFY_DIGEST)
667 vsflags |= _RPMVSF_NODIGESTS;
668 if (ia->qva_flags & VERIFY_SIGNATURE)
669 vsflags |= _RPMVSF_NOSIGNATURES;
670 if (ia->qva_flags & VERIFY_HDRCHK)
671 vsflags |= RPMVSF_NOHDRCHK;
672 ovsflags = rpmtsSetVSFlags(ts, vsflags);
674 /* XXX suggest mechanism only meaningful when installing */
675 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
677 (void) rpmtsSetFlags(ts, ia->transFlags);
679 #ifdef NOTYET /* XXX no callbacks on erase yet */
680 { int notifyFlags, xx;
681 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
682 xx = rpmtsSetNotifyCallback(ts,
683 rpmShowProgress, (void *) ((long)notifyFlags));
687 for (arg = argv; *arg; arg++) {
688 rpmdbMatchIterator mi;
690 /* XXX HACK to get rpmdbFindByLabel out of the API */
691 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
693 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
696 Header h; /* XXX iterator owns the reference */
698 while ((h = rpmdbNextIterator(mi)) != NULL) {
699 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
701 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
702 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
708 (void) rpmtsAddEraseElement(ts, h, recOffset);
713 mi = rpmdbFreeIterator(mi);
716 if (numFailed) goto exit;
718 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
720 if (rpmtsCheck(ts)) {
721 numFailed = numPackages;
725 ps = rpmtsProblems(ts);
726 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
727 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
728 rpmpsPrint(NULL, ps);
729 numFailed += numPackages;
735 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
736 if (rpmtsOrder(ts)) {
737 numFailed += numPackages;
742 if (numPackages && !stopUninstall) {
743 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
745 /* Drop added/available package indices and dependency sets. */
748 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
749 ps = rpmtsProblems(ts);
750 if (rpmpsNumProblems(ps) > 0)
751 rpmpsPrint(NULL, ps);
752 numFailed += numPackages;
763 int rpmInstallSource(rpmts ts, const char * arg,
764 char ** specFilePtr, char ** cookie)
770 fd = Fopen(arg, "r.ufdio");
771 if (fd == NULL || Ferror(fd)) {
772 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
773 if (fd != NULL) (void) Fclose(fd);
778 fprintf(stdout, _("Installing %s\n"), arg);
781 rpmVSFlags ovsflags =
782 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
783 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
784 rc = (rpmrc == RPMRC_OK ? 0 : 1);
785 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
788 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
789 if (specFilePtr && *specFilePtr)
790 *specFilePtr = _free(*specFilePtr);
791 if (cookie && *cookie)
792 *cookie = _free(*cookie);