2 * \file lib/rpminstall.c
18 int rpmcliPackagesTotal = 0;
19 int rpmcliHashesCurrent = 0;
20 int rpmcliHashesTotal = 0;
21 int rpmcliProgressCurrent = 0;
22 int rpmcliProgressTotal = 0;
25 * Print a CLI progress bar.
26 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
27 * @param amount current
30 static void printHash(const unsigned long amount, const unsigned long total)
34 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
36 if (rpmcliHashesCurrent != rpmcliHashesTotal) {
37 float pct = (total ? (((float) amount) / total) : 1.0);
38 hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
39 while (hashesNeeded > rpmcliHashesCurrent) {
40 if (isatty (STDOUT_FILENO)) {
42 for (i = 0; i < rpmcliHashesCurrent; i++)
44 for (; i < rpmcliHashesTotal; i++)
46 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
47 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
48 (void) putchar ('\b');
52 rpmcliHashesCurrent++;
54 (void) fflush(stdout);
56 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
58 rpmcliProgressCurrent++;
59 if (isatty(STDOUT_FILENO)) {
60 for (i = 1; i < rpmcliHashesCurrent; i++)
62 pct = (rpmcliProgressTotal
63 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
65 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
67 fprintf(stdout, "\n");
69 (void) fflush(stdout);
73 void * rpmShowProgress(const void * arg,
74 const rpmCallbackType what,
75 const unsigned long amount,
76 const unsigned long total,
80 Header h = (Header) arg;
82 int flags = (int) ((long)data);
84 const char * filename = (const char *)key;
85 static FD_t fd = NULL;
89 case RPMCALLBACK_INST_OPEN_FILE:
90 if (filename == NULL || filename[0] == '\0')
92 fd = Fopen(filename, "r.ufdio");
93 /* FIX: still necessary? */
94 if (fd == NULL || Ferror(fd)) {
95 rpmlog(RPMERR_OPEN, _("open of %s failed: %s\n"), filename,
102 fd = fdLink(fd, "persist (showProgress)");
106 case RPMCALLBACK_INST_CLOSE_FILE:
107 /* FIX: still necessary? */
108 fd = fdFree(fd, "persist (showProgress)");
115 case RPMCALLBACK_INST_START:
116 rpmcliHashesCurrent = 0;
117 if (h == NULL || !(flags & INSTALL_LABEL))
119 /* @todo Remove headerSprintf() on a progress callback. */
120 if (flags & INSTALL_HASH) {
121 s = headerSprintf(h, "%{NAME}",
122 rpmTagTable, rpmHeaderFormats, 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 = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
131 rpmTagTable, rpmHeaderFormats, NULL);
132 fprintf(stdout, "%s\n", s);
133 (void) fflush(stdout);
138 case RPMCALLBACK_TRANS_PROGRESS:
139 case RPMCALLBACK_INST_PROGRESS:
140 if (flags & INSTALL_PERCENT)
141 fprintf(stdout, "%%%% %f\n", (double) (total
142 ? ((((float) amount) / total) * 100)
144 else if (flags & INSTALL_HASH)
145 printHash(amount, total);
146 (void) fflush(stdout);
149 case RPMCALLBACK_TRANS_START:
150 rpmcliHashesCurrent = 0;
151 rpmcliProgressTotal = 1;
152 rpmcliProgressCurrent = 0;
153 if (!(flags & INSTALL_LABEL))
155 if (flags & INSTALL_HASH)
156 fprintf(stdout, "%-28s", _("Preparing..."));
158 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
159 (void) fflush(stdout);
162 case RPMCALLBACK_TRANS_STOP:
163 if (flags & INSTALL_HASH)
164 printHash(1, 1); /* Fixes "preparing..." progress bar */
165 rpmcliProgressTotal = rpmcliPackagesTotal;
166 rpmcliProgressCurrent = 0;
169 case RPMCALLBACK_REPACKAGE_START:
170 rpmcliHashesCurrent = 0;
171 rpmcliProgressTotal = total;
172 rpmcliProgressCurrent = 0;
173 if (!(flags & INSTALL_LABEL))
175 if (flags & INSTALL_HASH)
176 fprintf(stdout, "%-28s\n", _("Repackaging..."));
178 fprintf(stdout, "%s\n", _("Repackaging erased files..."));
179 (void) fflush(stdout);
182 case RPMCALLBACK_REPACKAGE_PROGRESS:
183 if (amount && (flags & INSTALL_HASH))
184 printHash(1, 1); /* Fixes "preparing..." progress bar */
187 case RPMCALLBACK_REPACKAGE_STOP:
188 rpmcliProgressTotal = total;
189 rpmcliProgressCurrent = total;
190 if (flags & INSTALL_HASH)
191 printHash(1, 1); /* Fixes "preparing..." progress bar */
192 rpmcliProgressTotal = rpmcliPackagesTotal;
193 rpmcliProgressCurrent = 0;
194 if (!(flags & INSTALL_LABEL))
196 if (flags & INSTALL_HASH)
197 fprintf(stdout, "%-28s\n", _("Upgrading..."));
199 fprintf(stdout, "%s\n", _("Upgrading packages..."));
200 (void) fflush(stdout);
203 case RPMCALLBACK_UNINST_PROGRESS:
205 case RPMCALLBACK_UNINST_START:
207 case RPMCALLBACK_UNINST_STOP:
209 case RPMCALLBACK_UNPACK_ERROR:
211 case RPMCALLBACK_CPIO_ERROR:
213 case RPMCALLBACK_UNKNOWN:
221 typedef const char * str_t;
239 rpmRelocation * relocations;
243 /** @todo Generalize --freshen policies. */
244 int rpmInstall(rpmts ts,
245 struct rpmInstallArguments_s * ia,
246 const char ** fileArgv)
248 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
250 rpmprobFilterFlags probFilter;
251 rpmRelocation * relocations;
252 const char * fileURL = NULL;
254 const char ** av = NULL;
255 rpmVSFlags vsflags, ovsflags, tvsflags;
261 if (fileArgv == NULL) goto exit;
263 rpmcliPackagesTotal = 0;
265 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
266 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
268 (void) rpmtsSetFlags(ts, ia->transFlags);
270 probFilter = ia->probFilter;
271 relocations = ia->relocations;
273 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
274 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
276 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
277 if (ia->qva_flags & VERIFY_DIGEST)
278 vsflags |= _RPMVSF_NODIGESTS;
279 if (ia->qva_flags & VERIFY_SIGNATURE)
280 vsflags |= _RPMVSF_NOSIGNATURES;
281 if (ia->qva_flags & VERIFY_HDRCHK)
282 vsflags |= RPMVSF_NOHDRCHK;
283 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
286 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
287 xx = rpmtsSetNotifyCallback(ts,
288 rpmShowProgress, (void *) ((long)notifyFlags));
291 if ((eiu->relocations = relocations) != NULL) {
292 while (eiu->relocations->oldPath)
294 if (eiu->relocations->newPath == NULL)
295 eiu->relocations = NULL;
298 /* Build fully globbed list of arguments in argv[argc]. */
299 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
300 av = _free(av); ac = 0;
301 rc = rpmGlob(*eiu->fnp, &ac, &av);
303 rpmlog(RPMERR_OPEN, _("File not found by glob: %s\n"), *eiu->fnp);
307 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
308 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
310 eiu->argv[eiu->argc] = NULL;
312 av = _free(av); ac = 0;
315 /* Allocate sufficient storage for next set of args. */
316 if (eiu->pkgx >= eiu->numPkgs) {
317 eiu->numPkgs = eiu->pkgx + eiu->argc;
318 eiu->pkgURL = xrealloc(eiu->pkgURL,
319 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
320 memset(eiu->pkgURL + eiu->pkgx, 0,
321 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
322 eiu->pkgState = xrealloc(eiu->pkgState,
323 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
324 memset(eiu->pkgState + eiu->pkgx, 0,
325 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
328 /* Retrieve next set of args, cache on local storage. */
329 for (i = 0; i < eiu->argc; i++) {
330 fileURL = _free(fileURL);
331 fileURL = eiu->argv[i];
335 if (fileURL[0] == '=') {
336 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
338 xx = rpmtsSolve(ts, this, NULL);
339 if (ts->suggests && ts->nsuggests > 0) {
340 fileURL = _free(fileURL);
341 fileURL = ts->suggests[0];
342 ts->suggests[0] = NULL;
343 while (ts->nsuggests-- > 0) {
344 if (ts->suggests[ts->nsuggests] == NULL)
346 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
348 ts->suggests = _free(ts->suggests);
349 rpmlog(RPMLOG_DEBUG, _("Adding goal: %s\n"), fileURL);
350 eiu->pkgURL[eiu->pkgx] = fileURL;
354 this = rpmdsFree(this);
358 switch (urlIsURL(fileURL)) {
365 fprintf(stdout, _("Retrieving %s\n"), fileURL);
368 const char * rootDir = rpmtsRootDir(ts);
369 if (!(rootDir && * rootDir))
371 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
372 (void) mktemp(tfnbuf);
373 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
376 /* XXX undefined %{name}/%{version}/%{release} here */
377 /* XXX %{_tmpdir} does not exist */
378 rpmlog(RPMLOG_DEBUG, _(" ... as %s\n"), tfn);
379 rc = urlGetFile(fileURL, tfn);
382 _("skipping %s - transfer failed - %s\n"),
383 fileURL, ftpStrerror(rc));
385 eiu->pkgURL[eiu->pkgx] = NULL;
389 eiu->pkgState[eiu->pkgx] = 1;
390 eiu->pkgURL[eiu->pkgx] = tfn;
394 case URL_IS_DASH: /* WRONG WRONG WRONG */
395 case URL_IS_HKP: /* WRONG WRONG WRONG */
397 eiu->pkgURL[eiu->pkgx] = fileURL;
403 fileURL = _free(fileURL);
405 if (eiu->numFailed) goto exit;
407 /* Continue processing file arguments, building transaction set. */
408 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
410 eiu->fnp++, eiu->prevx++)
412 const char * fileName;
414 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
415 (void) urlPath(*eiu->fnp, &fileName);
417 /* Try to read the header from a package file. */
418 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
419 if (eiu->fd == NULL || Ferror(eiu->fd)) {
420 rpmlog(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
422 if (eiu->fd != NULL) {
423 xx = Fclose(eiu->fd);
426 eiu->numFailed++; *eiu->fnp = NULL;
430 /* Read the header, verifying signatures (if present). */
431 tvsflags = rpmtsSetVSFlags(ts, vsflags);
432 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
433 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
434 xx = Fclose(eiu->fd);
437 switch (eiu->rpmrc) {
439 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
440 eiu->numFailed++; *eiu->fnp = NULL;
446 case RPMRC_NOTTRUSTED:
453 eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
456 rpmlog(RPMLOG_DEBUG, _("\tadded source package [%d]\n"),
458 eiu->sourceURL = xrealloc(eiu->sourceURL,
459 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
460 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
463 eiu->sourceURL[eiu->numSRPMS] = NULL;
467 if (eiu->relocations) {
472 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
473 (void **) &paths, &c) && (c == 1))
475 eiu->relocations->oldPath = xstrdup(paths[0]);
476 paths = headerFreeData(paths, pft);
479 xx = headerNVR(eiu->h, &name, NULL, NULL);
481 _("package %s is not relocatable\n"), name);
487 /* On --freshen, verify package is installed and newer */
488 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
489 rpmdbMatchIterator mi;
494 xx = headerNVR(eiu->h, &name, NULL, NULL);
495 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
496 count = rpmdbGetIteratorCount(mi);
497 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
498 if (rpmVersionCompare(oldH, eiu->h) < 0)
500 /* same or newer package already installed */
504 mi = rpmdbFreeIterator(mi);
506 eiu->h = headerFree(eiu->h);
509 /* Package is newer than those currently installed. */
512 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
513 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
516 /* XXX reference held by transaction set */
517 eiu->h = headerFree(eiu->h);
518 if (eiu->relocations)
519 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
523 rpmlog(RPMLOG_DEBUG, _("\tadded binary package [%d]\n"),
528 _("error reading from file %s\n"), *eiu->fnp);
534 _("file %s requires a newer version of RPM\n"),
549 /* Try to read a package manifest. */
550 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
551 if (eiu->fd == NULL || Ferror(eiu->fd)) {
552 rpmlog(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
554 if (eiu->fd != NULL) {
555 xx = Fclose(eiu->fd);
558 eiu->numFailed++; *eiu->fnp = NULL;
562 /* Read list of packages from manifest. */
563 /* FIX: *eiu->argv can be NULL */
564 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
566 rpmlog(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
567 *eiu->fnp, Fstrerror(eiu->fd));
568 xx = Fclose(eiu->fd);
571 /* If successful, restart the query loop. */
572 if (rc == RPMRC_OK) {
577 eiu->numFailed++; *eiu->fnp = NULL;
581 rpmlog(RPMLOG_DEBUG, _("found %d source and %d binary packages\n"),
582 eiu->numSRPMS, eiu->numRPMS);
584 if (eiu->numFailed) goto exit;
586 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
588 if (rpmtsCheck(ts)) {
589 eiu->numFailed = eiu->numPkgs;
593 ps = rpmtsProblems(ts);
594 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
595 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
596 rpmpsPrint(NULL, ps);
597 eiu->numFailed = eiu->numPkgs;
600 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
601 rpmtsPrintSuggests(ts);
607 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
608 if (rpmtsOrder(ts)) {
609 eiu->numFailed = eiu->numPkgs;
614 if (eiu->numRPMS && !stopInstall) {
616 rpmcliPackagesTotal += eiu->numSRPMS;
618 rpmlog(RPMLOG_DEBUG, _("installing binary packages\n"));
620 /* Drop added/available package indices and dependency sets. */
623 rc = rpmtsRun(ts, NULL, probFilter);
624 ps = rpmtsProblems(ts);
627 eiu->numFailed += eiu->numRPMS;
629 eiu->numFailed += rc;
630 if (rpmpsNumProblems(ps) > 0)
631 rpmpsPrint(stderr, ps);
636 if (eiu->numSRPMS && !stopInstall) {
637 if (eiu->sourceURL != NULL)
638 for (i = 0; i < eiu->numSRPMS; i++) {
640 if (eiu->sourceURL[i] == NULL) continue;
641 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
642 if (eiu->fd == NULL || Ferror(eiu->fd)) {
643 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
644 eiu->sourceURL[i], Fstrerror(eiu->fd));
645 if (eiu->fd != NULL) {
646 xx = Fclose(eiu->fd);
652 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
653 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
654 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
657 xx = Fclose(eiu->fd);
663 if (eiu->pkgURL != NULL)
664 for (i = 0; i < eiu->numPkgs; i++) {
665 if (eiu->pkgURL[i] == NULL) continue;
666 if (eiu->pkgState[i] == 1)
667 (void) Unlink(eiu->pkgURL[i]);
668 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
670 eiu->pkgState = _free(eiu->pkgState);
671 eiu->pkgURL = _free(eiu->pkgURL);
672 eiu->argv = _free(eiu->argv);
676 return eiu->numFailed;
679 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
685 int stopUninstall = 0;
687 rpmVSFlags vsflags, ovsflags;
690 if (argv == NULL) return 0;
692 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
693 if (ia->qva_flags & VERIFY_DIGEST)
694 vsflags |= _RPMVSF_NODIGESTS;
695 if (ia->qva_flags & VERIFY_SIGNATURE)
696 vsflags |= _RPMVSF_NOSIGNATURES;
697 if (ia->qva_flags & VERIFY_HDRCHK)
698 vsflags |= RPMVSF_NOHDRCHK;
699 ovsflags = rpmtsSetVSFlags(ts, vsflags);
701 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
702 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
704 /* XXX suggest mechanism only meaningful when installing */
705 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
707 (void) rpmtsSetFlags(ts, ia->transFlags);
709 #ifdef NOTYET /* XXX no callbacks on erase yet */
710 { int notifyFlags, xx;
711 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
712 xx = rpmtsSetNotifyCallback(ts,
713 rpmShowProgress, (void *) ((long)notifyFlags));
717 for (arg = argv; *arg; arg++) {
718 rpmdbMatchIterator mi;
720 /* XXX HACK to get rpmdbFindByLabel out of the API */
721 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
723 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
726 Header h; /* XXX iterator owns the reference */
728 while ((h = rpmdbNextIterator(mi)) != NULL) {
729 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
731 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
732 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
738 (void) rpmtsAddEraseElement(ts, h, recOffset);
743 mi = rpmdbFreeIterator(mi);
746 if (numFailed) goto exit;
748 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
750 if (rpmtsCheck(ts)) {
751 numFailed = numPackages;
755 ps = rpmtsProblems(ts);
756 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
757 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
758 rpmpsPrint(NULL, ps);
759 numFailed += numPackages;
765 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
766 if (rpmtsOrder(ts)) {
767 numFailed += numPackages;
772 if (numPackages && !stopUninstall) {
773 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
775 /* Drop added/available package indices and dependency sets. */
778 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
779 ps = rpmtsProblems(ts);
780 if (rpmpsNumProblems(ps) > 0)
781 rpmpsPrint(NULL, ps);
782 numFailed += numPackages;
793 int rpmInstallSource(rpmts ts, const char * arg,
794 const char ** specFilePtr, const char ** cookie)
800 fd = Fopen(arg, "r.ufdio");
801 if (fd == NULL || Ferror(fd)) {
802 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
803 if (fd != NULL) (void) Fclose(fd);
808 fprintf(stdout, _("Installing %s\n"), arg);
811 rpmVSFlags ovsflags =
812 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
813 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
814 rc = (rpmrc == RPMRC_OK ? 0 : 1);
815 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
818 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
819 if (specFilePtr && *specFilePtr)
820 *specFilePtr = _free(*specFilePtr);
821 if (cookie && *cookie)
822 *cookie = _free(*cookie);
830 /** @todo Transaction handling, more, needs work. */
831 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
833 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
834 unsigned thistid = 0xffffffff;
844 int vsflags, ovsflags;
848 int _unsafe_rollbacks = 0;
849 rpmtransFlags transFlags = ia->transFlags;
851 if (argv != NULL && *argv != NULL) {
856 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
858 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
859 if (ia->qva_flags & VERIFY_DIGEST)
860 vsflags |= _RPMVSF_NODIGESTS;
861 if (ia->qva_flags & VERIFY_SIGNATURE)
862 vsflags |= _RPMVSF_NOSIGNATURES;
863 if (ia->qva_flags & VERIFY_HDRCHK)
864 vsflags |= RPMVSF_NOHDRCHK;
865 vsflags |= RPMVSF_NEEDPAYLOAD; /* XXX no legacy signatures */
866 ovsflags = rpmtsSetVSFlags(ts, vsflags);
868 (void) rpmtsSetFlags(ts, transFlags);
870 /* Make the transaction a rollback transaction. In a rollback
871 * a best effort is what we want
873 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
875 itids = IDTXload(ts, RPMTAG_INSTALLTID);
884 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
885 if (globstr == NULL || *globstr == '%') {
886 globstr = _free(globstr);
890 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
899 globstr = _free(globstr);
902 { int notifyFlags, xx;
903 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
904 xx = rpmtsSetNotifyCallback(ts,
905 rpmShowProgress, (void *) ((long)notifyFlags));
908 /* Run transactions until rollback goal is achieved. */
912 rpmcliPackagesTotal = 0;
915 ia->installInterfaceFlags &= ~ifmask;
917 /* Find larger of the remaining install/erase transaction id's. */
919 if (ip != NULL && ip->val.u32 > thistid)
920 thistid = ip->val.u32;
921 if (rp != NULL && rp->val.u32 > thistid)
922 thistid = rp->val.u32;
924 /* If we've achieved the rollback goal, then we're done. */
925 if (thistid == 0 || thistid < ia->rbtid)
928 /* If we've reached the (configured) rollback goal, then we're done. */
929 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
933 (void) rpmtsSetFlags(ts, transFlags);
935 /* Install the previously erased packages for this transaction. */
936 while (rp != NULL && rp->val.u32 == thistid) {
938 rpmlog(RPMLOG_DEBUG, "\t+++ install %s\n",
939 (rp->key ? rp->key : "???"));
941 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
947 rpmcliPackagesTotal++;
948 if (!(ia->installInterfaceFlags & ifmask))
949 ia->installInterfaceFlags |= INSTALL_UPGRADE;
952 rp->h = headerFree(rp->h);
961 /* Erase the previously installed packages for this transaction. */
962 while (ip != NULL && ip->val.u32 == thistid) {
965 "\t--- erase h#%u\n", ip->instance);
967 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
973 if (_unsafe_rollbacks)
974 rpmcliPackagesTotal++;
976 if (!(ia->installInterfaceFlags & ifmask)) {
977 ia->installInterfaceFlags |= INSTALL_ERASE;
978 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
991 /* Anything to do? */
992 if (rpmcliPackagesTotal <= 0)
995 tid = (time_t)thistid;
996 rpmlog(RPMLOG_NOTICE,
997 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
998 numAdded, numRemoved, ctime(&tid), tid);
1000 rc = rpmtsCheck(ts);
1001 ps = rpmtsProblems(ts);
1002 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1003 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
1004 rpmpsPrint(NULL, ps);
1010 rc = rpmtsOrder(ts);
1014 /* Drop added/available package indices and dependency sets. */
1017 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1018 ps = rpmtsProblems(ts);
1019 if (rc > 0 && rpmpsNumProblems(ps) > 0)
1020 rpmpsPrint(stderr, ps);
1025 /* Clean up after successful rollback. */
1026 if (rtids && !rpmIsDebug()) {
1028 rpmlog(RPMLOG_NOTICE, _("Cleaning up repackaged packages:\n"));
1030 for (i = 0; i < rtids->nidt; i++) {
1031 IDT rrp = rtids->idt + i;
1032 if (rrp->val.u32 != thistid)
1034 if (rrp->key) { /* XXX can't happen */
1035 rpmlog(RPMLOG_NOTICE, _("\tRemoving %s:\n"), rrp->key);
1036 (void) unlink(rrp->key); /* XXX: Should check rc??? */
1045 rtids = IDTXfree(rtids);
1046 itids = IDTXfree(itids);
1049 (void) rpmtsSetFlags(ts, transFlags);