2 * \file lib/rpminstall.c
12 #define _RPMTS_INTERNAL /* ts->goal, ts->dbmode, ts->suggests */
16 #include "misc.h" /* XXX for rpmGlob() */
19 /*@access rpmts @*/ /* XXX ts->goal, ts->dbmode */
20 /*@access rpmps @*/ /* XXX compared with NULL */
21 /*@access Header @*/ /* XXX compared with NULL */
22 /*@access rpmdb @*/ /* XXX compared with NULL */
23 /*@access FD_t @*/ /* XXX compared with NULL */
28 int rpmcliPackagesTotal = 0;
30 int rpmcliHashesCurrent = 0;
32 int rpmcliHashesTotal = 0;
34 int rpmcliProgressCurrent = 0;
36 int rpmcliProgressTotal = 0;
39 * Print a CLI progress bar.
40 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
41 * @param amount current
44 static void printHash(const unsigned long amount, const unsigned long total)
45 /*@globals rpmcliHashesCurrent, rpmcliHashesTotal,
46 rpmcliProgressCurrent, fileSystem @*/
47 /*@modifies rpmcliHashesCurrent, rpmcliHashesTotal,
48 rpmcliProgressCurrent, fileSystem @*/
52 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
54 if (rpmcliHashesCurrent != rpmcliHashesTotal) {
55 float pct = (total ? (((float) amount) / total) : 1.0);
56 hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
57 while (hashesNeeded > rpmcliHashesCurrent) {
58 if (isatty (STDOUT_FILENO)) {
60 for (i = 0; i < rpmcliHashesCurrent; i++)
62 for (; i < rpmcliHashesTotal; i++)
64 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
65 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
66 (void) putchar ('\b');
70 rpmcliHashesCurrent++;
72 (void) fflush(stdout);
74 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
76 rpmcliProgressCurrent++;
77 if (isatty(STDOUT_FILENO)) {
78 for (i = 1; i < rpmcliHashesCurrent; i++)
80 pct = (rpmcliProgressTotal
81 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
83 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
85 fprintf(stdout, "\n");
87 (void) fflush(stdout);
91 void * rpmShowProgress(/*@null@*/ const void * arg,
92 const rpmCallbackType what,
93 const unsigned long amount,
94 const unsigned long total,
95 /*@null@*/ fnpyKey key,
96 /*@null@*/ void * data)
97 /*@globals rpmcliHashesCurrent, rpmcliProgressCurrent, rpmcliProgressTotal,
99 /*@modifies rpmcliHashesCurrent, rpmcliProgressCurrent, rpmcliProgressTotal,
103 Header h = (Header) arg;
106 int flags = (int) ((long)data);
108 /*@-assignexpose -abstract @*/
109 const char * filename = (const char *)key;
110 /*@=assignexpose =abstract @*/
111 static FD_t fd = NULL;
115 case RPMCALLBACK_INST_OPEN_FILE:
117 if (filename == NULL || filename[0] == '\0')
120 fd = Fopen(filename, "r.ufdio");
121 /*@-type@*/ /* FIX: still necessary? */
122 if (fd == NULL || Ferror(fd)) {
123 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), filename,
130 fd = fdLink(fd, "persist (showProgress)");
133 /*@notreached@*/ break;
135 case RPMCALLBACK_INST_CLOSE_FILE:
136 /*@-type@*/ /* FIX: still necessary? */
137 fd = fdFree(fd, "persist (showProgress)");
145 case RPMCALLBACK_INST_START:
146 rpmcliHashesCurrent = 0;
147 if (h == NULL || !(flags & INSTALL_LABEL))
149 /* @todo Remove headerSprintf() on a progress callback. */
150 if (flags & INSTALL_HASH) {
151 s = headerSprintf(h, "%{NAME}",
152 rpmTagTable, rpmHeaderFormats, NULL);
153 if (isatty (STDOUT_FILENO))
154 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
156 fprintf(stdout, "%-28.28s", s);
157 (void) fflush(stdout);
160 s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
161 rpmTagTable, rpmHeaderFormats, NULL);
162 fprintf(stdout, "%s\n", s);
163 (void) fflush(stdout);
168 case RPMCALLBACK_TRANS_PROGRESS:
169 case RPMCALLBACK_INST_PROGRESS:
170 if (flags & INSTALL_PERCENT)
171 fprintf(stdout, "%%%% %f\n", (double) (total
172 ? ((((float) amount) / total) * 100)
174 else if (flags & INSTALL_HASH)
175 printHash(amount, total);
176 (void) fflush(stdout);
179 case RPMCALLBACK_TRANS_START:
180 rpmcliHashesCurrent = 0;
181 rpmcliProgressTotal = 1;
182 rpmcliProgressCurrent = 0;
183 if (!(flags & INSTALL_LABEL))
185 if (flags & INSTALL_HASH)
186 fprintf(stdout, "%-28s", _("Preparing..."));
188 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
189 (void) fflush(stdout);
192 case RPMCALLBACK_TRANS_STOP:
193 if (flags & INSTALL_HASH)
194 printHash(1, 1); /* Fixes "preparing..." progress bar */
195 rpmcliProgressTotal = rpmcliPackagesTotal;
196 rpmcliProgressCurrent = 0;
199 case RPMCALLBACK_REPACKAGE_START:
200 rpmcliHashesCurrent = 0;
201 rpmcliProgressTotal = total;
202 rpmcliProgressCurrent = 0;
203 if (!(flags & INSTALL_LABEL))
205 if (flags & INSTALL_HASH)
206 fprintf(stdout, "%-28s\n", _("Repackaging..."));
208 fprintf(stdout, "%s\n", _("Repackaging erased files..."));
209 (void) fflush(stdout);
212 case RPMCALLBACK_REPACKAGE_PROGRESS:
213 if (amount && (flags & INSTALL_HASH))
214 printHash(1, 1); /* Fixes "preparing..." progress bar */
217 case RPMCALLBACK_REPACKAGE_STOP:
218 rpmcliProgressTotal = total;
219 rpmcliProgressCurrent = total;
220 if (flags & INSTALL_HASH)
221 printHash(1, 1); /* Fixes "preparing..." progress bar */
222 rpmcliProgressTotal = rpmcliPackagesTotal;
223 rpmcliProgressCurrent = 0;
224 if (!(flags & INSTALL_LABEL))
226 if (flags & INSTALL_HASH)
227 fprintf(stdout, "%-28s\n", _("Upgrading..."));
229 fprintf(stdout, "%s\n", _("Upgrading packages..."));
230 (void) fflush(stdout);
233 case RPMCALLBACK_UNINST_PROGRESS:
235 case RPMCALLBACK_UNINST_START:
237 case RPMCALLBACK_UNINST_STOP:
239 case RPMCALLBACK_UNPACK_ERROR:
241 case RPMCALLBACK_CPIO_ERROR:
243 case RPMCALLBACK_UNKNOWN:
251 typedef /*@only@*/ /*@null@*/ const char * str_t;
260 /*@dependent@*/ /*@null@*/
268 /*@only@*/ /*@null@*/
272 /*@only@*/ /*@null@*/
275 rpmRelocation * relocations;
279 /** @todo Generalize --freshen policies. */
281 int rpmInstall(rpmts ts,
282 struct rpmInstallArguments_s * ia,
283 const char ** fileArgv)
285 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
287 rpmprobFilterFlags probFilter;
288 rpmRelocation * relocations;
289 /*@only@*/ /*@null@*/ const char * fileURL = NULL;
291 const char ** av = NULL;
292 rpmVSFlags vsflags, ovsflags, tvsflags;
298 if (fileArgv == NULL) goto exit;
300 ts->goal = TSM_INSTALL;
301 rpmcliPackagesTotal = 0;
303 (void) rpmtsSetFlags(ts, ia->transFlags);
304 probFilter = ia->probFilter;
305 relocations = ia->relocations;
307 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
308 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
310 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
311 if (ia->qva_flags & VERIFY_DIGEST)
312 vsflags |= _RPMVSF_NODIGESTS;
313 if (ia->qva_flags & VERIFY_SIGNATURE)
314 vsflags |= _RPMVSF_NOSIGNATURES;
315 if (ia->qva_flags & VERIFY_HDRCHK)
316 vsflags |= RPMVSF_NOHDRCHK;
317 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
320 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
321 xx = rpmtsSetNotifyCallback(ts,
322 rpmShowProgress, (void *) ((long)notifyFlags));
325 if ((eiu->relocations = relocations) != NULL) {
326 while (eiu->relocations->oldPath)
328 if (eiu->relocations->newPath == NULL)
329 eiu->relocations = NULL;
332 /* Build fully globbed list of arguments in argv[argc]. */
335 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
337 av = _free(av); ac = 0;
338 rc = rpmGlob(*eiu->fnp, &ac, &av);
339 if (rc || ac == 0) continue;
341 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
342 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
344 eiu->argv[eiu->argc] = NULL;
347 av = _free(av); ac = 0;
350 /* Allocate sufficient storage for next set of args. */
351 if (eiu->pkgx >= eiu->numPkgs) {
352 eiu->numPkgs = eiu->pkgx + eiu->argc;
353 eiu->pkgURL = xrealloc(eiu->pkgURL,
354 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
355 memset(eiu->pkgURL + eiu->pkgx, 0,
356 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
357 eiu->pkgState = xrealloc(eiu->pkgState,
358 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
359 memset(eiu->pkgState + eiu->pkgx, 0,
360 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
363 /* Retrieve next set of args, cache on local storage. */
364 for (i = 0; i < eiu->argc; i++) {
365 fileURL = _free(fileURL);
366 fileURL = eiu->argv[i];
370 if (fileURL[0] == '=') {
371 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
373 xx = rpmtsSolve(ts, this, NULL);
374 if (ts->suggests && ts->nsuggests > 0) {
375 fileURL = _free(fileURL);
376 fileURL = ts->suggests[0];
377 ts->suggests[0] = NULL;
378 while (ts->nsuggests-- > 0) {
379 if (ts->suggests[ts->nsuggests] == NULL)
381 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
383 ts->suggests = _free(ts->suggests);
384 rpmMessage(RPMMESS_DEBUG, _("Adding goal: %s\n"), fileURL);
385 eiu->pkgURL[eiu->pkgx] = fileURL;
389 this = rpmdsFree(this);
393 switch (urlIsURL(fileURL)) {
399 fprintf(stdout, _("Retrieving %s\n"), fileURL);
402 const char * rootDir = rpmtsRootDir(ts);
403 if (!(rootDir && * rootDir))
405 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
406 (void) mktemp(tfnbuf);
407 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
410 /* XXX undefined %{name}/%{version}/%{release} here */
411 /* XXX %{_tmpdir} does not exist */
412 rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
413 rc = urlGetFile(fileURL, tfn);
415 rpmMessage(RPMMESS_ERROR,
416 _("skipping %s - transfer failed - %s\n"),
417 fileURL, ftpStrerror(rc));
419 eiu->pkgURL[eiu->pkgx] = NULL;
421 /*@switchbreak@*/ break;
423 eiu->pkgState[eiu->pkgx] = 1;
424 eiu->pkgURL[eiu->pkgx] = tfn;
426 } /*@switchbreak@*/ break;
429 eiu->pkgURL[eiu->pkgx] = fileURL;
432 /*@switchbreak@*/ break;
435 fileURL = _free(fileURL);
437 if (eiu->numFailed) goto exit;
439 /* Continue processing file arguments, building transaction set. */
440 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
442 eiu->fnp++, eiu->prevx++)
444 const char * fileName;
446 rpmMessage(RPMMESS_DEBUG, "============== %s\n", *eiu->fnp);
447 (void) urlPath(*eiu->fnp, &fileName);
449 /* Try to read the header from a package file. */
450 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
451 if (eiu->fd == NULL || Ferror(eiu->fd)) {
452 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
455 xx = Fclose(eiu->fd);
458 eiu->numFailed++; *eiu->fnp = NULL;
462 /* Read the header, verifying signatures (if present). */
463 tvsflags = rpmtsSetVSFlags(ts, vsflags);
464 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
465 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
466 xx = Fclose(eiu->fd);
469 switch (eiu->rpmrc) {
471 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *eiu->fnp);
472 eiu->numFailed++; *eiu->fnp = NULL;
474 /*@notreached@*/ /*@switchbreak@*/ break;
477 /*@notreached@*/ /*@switchbreak@*/ break;
478 case RPMRC_NOTTRUSTED:
482 /*@switchbreak@*/ break;
485 eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
488 rpmMessage(RPMMESS_DEBUG, "\tadded source package [%d]\n",
490 eiu->sourceURL = xrealloc(eiu->sourceURL,
491 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
492 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
495 eiu->sourceURL[eiu->numSRPMS] = NULL;
499 if (eiu->relocations) {
504 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
505 (void **) &paths, &c) && (c == 1))
507 eiu->relocations->oldPath = xstrdup(paths[0]);
508 paths = headerFreeData(paths, pft);
511 xx = headerNVR(eiu->h, &name, NULL, NULL);
512 rpmMessage(RPMMESS_ERROR,
513 _("package %s is not relocateable\n"), name);
520 /* On --freshen, verify package is installed and newer */
521 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
522 rpmdbMatchIterator mi;
527 xx = headerNVR(eiu->h, &name, NULL, NULL);
528 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
529 count = rpmdbGetIteratorCount(mi);
530 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
531 if (rpmVersionCompare(oldH, eiu->h) < 0)
532 /*@innercontinue@*/ continue;
533 /* same or newer package already installed */
535 /*@innerbreak@*/ break;
537 mi = rpmdbFreeIterator(mi);
539 eiu->h = headerFree(eiu->h);
542 /* Package is newer than those currently installed. */
546 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
547 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
551 /* XXX reference held by transaction set */
552 eiu->h = headerFree(eiu->h);
553 if (eiu->relocations)
554 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
558 rpmMessage(RPMMESS_DEBUG, "\tadded binary package [%d]\n",
560 /*@switchbreak@*/ break;
562 rpmMessage(RPMMESS_ERROR,
563 _("error reading from file %s\n"), *eiu->fnp);
566 /*@notreached@*/ /*@switchbreak@*/ break;
568 rpmMessage(RPMMESS_ERROR,
569 _("file %s requires a newer version of RPM\n"),
573 /*@notreached@*/ /*@switchbreak@*/ break;
580 /* Try to read a package manifest. */
581 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
582 if (eiu->fd == NULL || Ferror(eiu->fd)) {
583 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
586 xx = Fclose(eiu->fd);
589 eiu->numFailed++; *eiu->fnp = NULL;
593 /* Read list of packages from manifest. */
594 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
596 rpmError(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
597 *eiu->fnp, Fstrerror(eiu->fd));
598 xx = Fclose(eiu->fd);
601 /* If successful, restart the query loop. */
602 if (rc == RPMRC_OK) {
607 eiu->numFailed++; *eiu->fnp = NULL;
611 rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
612 eiu->numSRPMS, eiu->numRPMS);
614 if (eiu->numFailed) goto exit;
616 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
618 if (rpmtsCheck(ts)) {
619 eiu->numFailed = eiu->numPkgs;
623 ps = rpmtsProblems(ts);
624 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
625 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
626 rpmpsPrint(NULL, ps);
627 eiu->numFailed = eiu->numPkgs;
631 if (ts->suggests != NULL && ts->nsuggests > 0) {
632 rpmMessage(RPMMESS_NORMAL, _(" Suggested resolutions:\n"));
633 for (i = 0; i < ts->nsuggests; i++) {
634 const char * str = ts->suggests[i];
639 rpmMessage(RPMMESS_NORMAL, "\t%s\n", str);
641 ts->suggests[i] = NULL;
644 ts->suggests = _free(ts->suggests);
651 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
652 if (rpmtsOrder(ts)) {
653 eiu->numFailed = eiu->numPkgs;
658 if (eiu->numRPMS && !stopInstall) {
660 rpmcliPackagesTotal += eiu->numSRPMS;
662 rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
664 /* Drop added/available package indices and dependency sets. */
667 rc = rpmtsRun(ts, NULL, probFilter);
668 ps = rpmtsProblems(ts);
671 eiu->numFailed += eiu->numRPMS;
673 eiu->numFailed += rc;
674 if (rpmpsNumProblems(ps) > 0)
675 rpmpsPrint(stderr, ps);
680 if (eiu->numSRPMS && !stopInstall) {
681 if (eiu->sourceURL != NULL)
682 for (i = 0; i < eiu->numSRPMS; i++) {
683 if (eiu->sourceURL[i] == NULL) continue;
684 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
685 if (eiu->fd == NULL || Ferror(eiu->fd)) {
686 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
687 eiu->sourceURL[i], Fstrerror(eiu->fd));
689 xx = Fclose(eiu->fd);
695 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
696 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
697 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
700 xx = Fclose(eiu->fd);
706 if (eiu->pkgURL != NULL)
707 for (i = 0; i < eiu->numPkgs; i++) {
708 if (eiu->pkgURL[i] == NULL) continue;
709 if (eiu->pkgState[i] == 1)
710 (void) Unlink(eiu->pkgURL[i]);
711 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
713 eiu->pkgState = _free(eiu->pkgState);
714 eiu->pkgURL = _free(eiu->pkgURL);
715 eiu->argv = _free(eiu->argv);
719 return eiu->numFailed;
723 int rpmErase(rpmts ts,
724 const struct rpmInstallArguments_s * ia,
730 int stopUninstall = 0;
732 rpmVSFlags vsflags, ovsflags;
735 if (argv == NULL) return 0;
737 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
738 if (ia->qva_flags & VERIFY_DIGEST)
739 vsflags |= _RPMVSF_NODIGESTS;
740 if (ia->qva_flags & VERIFY_SIGNATURE)
741 vsflags |= _RPMVSF_NOSIGNATURES;
742 if (ia->qva_flags & VERIFY_HDRCHK)
743 vsflags |= RPMVSF_NOHDRCHK;
744 ovsflags = rpmtsSetVSFlags(ts, vsflags);
746 (void) rpmtsSetFlags(ts, ia->transFlags);
748 #ifdef NOTYET /* XXX no callbacks on erase yet */
750 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
751 xx = rpmtsSetNotifyCallback(ts,
752 rpmShowProgress, (void *) ((long)notifyFlags)
756 ts->goal = TSM_ERASE;
758 for (arg = argv; *arg; arg++) {
759 rpmdbMatchIterator mi;
761 /* XXX HACK to get rpmdbFindByLabel out of the API */
762 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
763 count = rpmdbGetIteratorCount(mi);
765 rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
767 } else if (!(count == 1 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
768 rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
772 Header h; /* XXX iterator owns the reference */
773 while ((h = rpmdbNextIterator(mi)) != NULL) {
774 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
776 (void) rpmtsAddEraseElement(ts, h, recOffset);
781 mi = rpmdbFreeIterator(mi);
784 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
786 if (rpmtsCheck(ts)) {
787 numFailed = numPackages;
791 ps = rpmtsProblems(ts);
792 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
793 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
794 rpmpsPrint(NULL, ps);
795 numFailed += numPackages;
801 if (!stopUninstall) {
802 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
804 /* Drop added/available package indices and dependency sets. */
807 numPackages = rpmtsRun(ts, NULL, 0);
808 ps = rpmtsProblems(ts);
809 if (rpmpsNumProblems(ps) > 0)
810 rpmpsPrint(NULL, ps);
811 numFailed += numPackages;
821 int rpmInstallSource(rpmts ts, const char * arg,
822 const char ** specFilePtr, const char ** cookie)
828 fd = Fopen(arg, "r.ufdio");
829 if (fd == NULL || Ferror(fd)) {
830 rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
831 if (fd) (void) Fclose(fd);
836 fprintf(stdout, _("Installing %s\n"), arg);
839 rpmVSFlags ovsflags =
840 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
841 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
842 rc = (rpmrc == RPMRC_OK ? 0 : 1);
843 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
846 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
847 /*@-unqualifiedtrans@*/
848 if (specFilePtr && *specFilePtr)
849 *specFilePtr = _free(*specFilePtr);
850 if (cookie && *cookie)
851 *cookie = _free(*cookie);
852 /*@=unqualifiedtrans@*/
861 static int reverse = -1;
865 static int IDTintcmp(const void * a, const void * b)
869 return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
873 IDTX IDTXfree(IDTX idtx)
878 for (i = 0; i < idtx->nidt; i++) {
879 IDT idt = idtx->idt + i;
880 idt->h = headerFree(idt->h);
881 idt->key = _free(idt->key);
883 idtx->idt = _free(idtx->idt);
891 IDTX idtx = xcalloc(1, sizeof(*idtx));
893 idtx->size = sizeof(*((IDT)0));
897 IDTX IDTXgrow(IDTX idtx, int need)
899 if (need < 0) return NULL;
902 if (need == 0) return idtx;
904 if ((idtx->nidt + need) > idtx->alloced) {
906 idtx->alloced += idtx->delta;
909 idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
914 IDTX IDTXsort(IDTX idtx)
916 if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
917 qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
921 IDTX IDTXload(rpmts ts, rpmTag tag)
924 rpmdbMatchIterator mi;
925 HGE_t hge = (HGE_t) headerGetEntry;
929 mi = rpmtsInitIterator(ts, tag, NULL, 0);
930 while ((h = rpmdbNextIterator(mi)) != NULL) {
931 rpmTagType type = RPM_NULL_TYPE;
936 if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
939 if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
942 idtx = IDTXgrow(idtx, 1);
945 if (idtx->idt == NULL)
950 idt = idtx->idt + idtx->nidt;
952 idt->h = headerLink(h);
954 idt->instance = rpmdbGetIteratorOffset(mi);
955 idt->val.u32 = *tidp;
959 mi = rpmdbFreeIterator(mi);
962 return IDTXsort(idtx);
965 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
968 HGE_t hge = (HGE_t) headerGetEntry;
972 const char ** av = NULL;
979 xx = rpmGlob(globstr, &ac, &av);
982 for (i = 0; i < ac; i++) {
987 fd = Fopen(av[i], "r.ufdio");
988 if (fd == NULL || Ferror(fd)) {
989 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
991 if (fd) (void) Fclose(fd);
995 rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
1000 /*@notreached@*/ /*@switchbreak@*/ break;
1001 case RPMRC_NOTTRUSTED:
1004 isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
1007 /*@switchbreak@*/ break;
1012 if (hge(h, tag, &type, (void **) &tidp, &count) && tidp) {
1014 idtx = IDTXgrow(idtx, 1);
1015 if (idtx == NULL || idtx->idt == NULL)
1019 idt = idtx->idt + idtx->nidt;
1020 idt->h = headerLink(h);
1024 idt->val.u32 = *tidp;
1033 for (i = 0; i < ac; i++)
1034 av[i] = _free(av[i]);
1035 av = _free(av); ac = 0;
1040 /** @todo Transaction handling, more, needs work. */
1041 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
1043 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
1044 unsigned thistid = 0xffffffff;
1054 int vsflags, ovsflags;
1058 int _unsafe_rollbacks = 0;
1059 rpmtransFlags transFlags = ia->transFlags;
1061 if (argv != NULL && *argv != NULL) {
1066 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
1068 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
1069 if (ia->qva_flags & VERIFY_DIGEST)
1070 vsflags |= _RPMVSF_NODIGESTS;
1071 if (ia->qva_flags & VERIFY_SIGNATURE)
1072 vsflags |= _RPMVSF_NOSIGNATURES;
1073 if (ia->qva_flags & VERIFY_HDRCHK)
1074 vsflags |= RPMVSF_NOHDRCHK;
1075 vsflags |= RPMVSF_NEEDPAYLOAD; /* XXX no legacy signatures */
1076 ovsflags = rpmtsSetVSFlags(ts, vsflags);
1078 (void) rpmtsSetFlags(ts, transFlags);
1080 itids = IDTXload(ts, RPMTAG_INSTALLTID);
1081 if (itids != NULL) {
1083 niids = itids->nidt;
1089 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
1090 if (globstr == NULL || *globstr == '%') {
1091 globstr = _free(globstr);
1095 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
1096 if (rtids != NULL) {
1098 nrids = rtids->nidt;
1103 globstr = _free(globstr);
1106 { int notifyFlags, xx;
1107 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
1108 xx = rpmtsSetNotifyCallback(ts,
1109 rpmShowProgress, (void *) ((long)notifyFlags));
1112 /* Run transactions until rollback goal is achieved. */
1116 rpmcliPackagesTotal = 0;
1119 ia->installInterfaceFlags &= ~ifmask;
1121 /* Find larger of the remaining install/erase transaction id's. */
1123 if (ip != NULL && ip->val.u32 > thistid)
1124 thistid = ip->val.u32;
1125 if (rp != NULL && rp->val.u32 > thistid)
1126 thistid = rp->val.u32;
1128 /* If we've achieved the rollback goal, then we're done. */
1129 if (thistid == 0 || thistid < ia->rbtid)
1133 (void) rpmtsSetFlags(ts, transFlags);
1135 /* Install the previously erased packages for this transaction. */
1136 while (rp != NULL && rp->val.u32 == thistid) {
1138 rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
1139 (rp->key ? rp->key : "???"));
1142 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
1143 0, ia->relocations);
1149 rpmcliPackagesTotal++;
1150 if (!(ia->installInterfaceFlags & ifmask))
1151 ia->installInterfaceFlags |= INSTALL_UPGRADE;
1154 rp->h = headerFree(rp->h);
1163 /* Erase the previously installed packages for this transaction. */
1164 while (ip != NULL && ip->val.u32 == thistid) {
1166 rpmMessage(RPMMESS_DEBUG,
1167 "\t--- erase h#%u\n", ip->instance);
1169 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
1175 if (_unsafe_rollbacks)
1176 rpmcliPackagesTotal++;
1178 if (!(ia->installInterfaceFlags & ifmask)) {
1179 ia->installInterfaceFlags |= INSTALL_ERASE;
1180 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
1193 /* Anything to do? */
1194 if (rpmcliPackagesTotal <= 0)
1197 tid = (time_t)thistid;
1198 rpmMessage(RPMMESS_NORMAL,
1199 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1200 numAdded, numRemoved, ctime(&tid), tid);
1202 rc = rpmtsCheck(ts);
1203 ps = rpmtsProblems(ts);
1204 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1205 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
1206 rpmpsPrint(NULL, ps);
1212 rc = rpmtsOrder(ts);
1216 /* Drop added/available package indices and dependency sets. */
1219 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1220 ps = rpmtsProblems(ts);
1221 if (rc > 0 && rpmpsNumProblems(ps) > 0)
1222 rpmpsPrint(stderr, ps);
1227 /* Clean up after successful rollback. */
1228 if (rtids && !rpmIsDebug()) {
1231 for (i = 0; i < rtids->nidt; i++) {
1232 IDT rrp = rtids->idt + i;
1233 if (rrp->val.u32 != thistid)
1234 /*@innercontinue@*/ continue;
1235 if (rrp->key) /* XXX can't happen */
1236 (void) unlink(rrp->key);
1244 rtids = IDTXfree(rtids);
1245 itids = IDTXfree(itids);
1248 (void) rpmtsSetFlags(ts, transFlags);