2 * \file lib/rpminstall.c
7 #include <rpm/rpmcli.h>
10 #include <rpm/rpmds.h>
11 #include <rpm/rpmts.h>
12 #include <rpm/rpmlog.h>
13 #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 unsigned long amount, const unsigned long 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 unsigned long amount,
77 const unsigned long 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_REPACKAGE_START:
171 rpmcliHashesCurrent = 0;
172 rpmcliProgressTotal = total;
173 rpmcliProgressCurrent = 0;
174 if (!(flags & INSTALL_LABEL))
176 if (flags & INSTALL_HASH)
177 fprintf(stdout, "%-28s\n", _("Repackaging..."));
179 fprintf(stdout, "%s\n", _("Repackaging erased files..."));
180 (void) fflush(stdout);
183 case RPMCALLBACK_REPACKAGE_PROGRESS:
184 if (amount && (flags & INSTALL_HASH))
185 printHash(1, 1); /* Fixes "preparing..." progress bar */
188 case RPMCALLBACK_REPACKAGE_STOP:
189 rpmcliProgressTotal = total;
190 rpmcliProgressCurrent = total;
191 if (flags & INSTALL_HASH)
192 printHash(1, 1); /* Fixes "preparing..." progress bar */
193 rpmcliProgressTotal = rpmcliPackagesTotal;
194 rpmcliProgressCurrent = 0;
195 if (!(flags & INSTALL_LABEL))
197 if (flags & INSTALL_HASH)
198 fprintf(stdout, "%-28s\n", _("Upgrading..."));
200 fprintf(stdout, "%s\n", _("Upgrading packages..."));
201 (void) fflush(stdout);
204 case RPMCALLBACK_UNINST_PROGRESS:
206 case RPMCALLBACK_UNINST_START:
208 case RPMCALLBACK_UNINST_STOP:
210 case RPMCALLBACK_UNPACK_ERROR:
212 case RPMCALLBACK_CPIO_ERROR:
214 case RPMCALLBACK_SCRIPT_ERROR:
216 case RPMCALLBACK_UNKNOWN:
224 typedef const char * str_t;
242 rpmRelocation * relocations;
246 /** @todo Generalize --freshen policies. */
247 int rpmInstall(rpmts ts,
248 struct rpmInstallArguments_s * ia,
249 const char ** fileArgv)
251 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
253 rpmprobFilterFlags probFilter;
254 rpmRelocation * relocations;
255 const char * fileURL = NULL;
258 rpmVSFlags vsflags, ovsflags, tvsflags;
264 if (fileArgv == NULL) goto exit;
266 rpmcliPackagesTotal = 0;
268 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
269 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
271 (void) rpmtsSetFlags(ts, ia->transFlags);
273 probFilter = ia->probFilter;
274 relocations = ia->relocations;
276 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
277 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
279 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
280 if (ia->qva_flags & VERIFY_DIGEST)
281 vsflags |= _RPMVSF_NODIGESTS;
282 if (ia->qva_flags & VERIFY_SIGNATURE)
283 vsflags |= _RPMVSF_NOSIGNATURES;
284 if (ia->qva_flags & VERIFY_HDRCHK)
285 vsflags |= RPMVSF_NOHDRCHK;
286 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
289 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
290 xx = rpmtsSetNotifyCallback(ts,
291 rpmShowProgress, (void *) ((long)notifyFlags));
294 if ((eiu->relocations = relocations) != NULL) {
295 while (eiu->relocations->oldPath)
297 if (eiu->relocations->newPath == NULL)
298 eiu->relocations = NULL;
301 /* Build fully globbed list of arguments in argv[argc]. */
302 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
303 av = _free(av); ac = 0;
304 rc = rpmGlob(*eiu->fnp, &ac, &av);
306 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
310 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
311 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
313 eiu->argv[eiu->argc] = NULL;
315 av = _free(av); ac = 0;
318 /* Allocate sufficient storage for next set of args. */
319 if (eiu->pkgx >= eiu->numPkgs) {
320 eiu->numPkgs = eiu->pkgx + eiu->argc;
321 eiu->pkgURL = xrealloc(eiu->pkgURL,
322 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
323 memset(eiu->pkgURL + eiu->pkgx, 0,
324 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
325 eiu->pkgState = xrealloc(eiu->pkgState,
326 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
327 memset(eiu->pkgState + eiu->pkgx, 0,
328 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
331 /* Retrieve next set of args, cache on local storage. */
332 for (i = 0; i < eiu->argc; i++) {
333 fileURL = _free(fileURL);
334 fileURL = eiu->argv[i];
338 if (fileURL[0] == '=') {
339 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
341 xx = rpmtsSolve(ts, this, NULL);
342 if (ts->suggests && ts->nsuggests > 0) {
343 fileURL = _free(fileURL);
344 fileURL = ts->suggests[0];
345 ts->suggests[0] = NULL;
346 while (ts->nsuggests-- > 0) {
347 if (ts->suggests[ts->nsuggests] == NULL)
349 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
351 ts->suggests = _free(ts->suggests);
352 rpmlog(RPMLOG_DEBUG, _("Adding goal: %s\n"), fileURL);
353 eiu->pkgURL[eiu->pkgx] = fileURL;
357 this = rpmdsFree(this);
361 switch (urlIsURL(fileURL)) {
368 fprintf(stdout, _("Retrieving %s\n"), fileURL);
371 const char * rootDir = rpmtsRootDir(ts);
372 if (!(rootDir && * rootDir))
374 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
375 #if defined(HAVE_MKSTEMP)
376 (void) close(mkstemp(tfnbuf));
378 (void) mktemp(tfnbuf);
380 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
383 /* XXX undefined %{name}/%{version}/%{release} here */
384 /* XXX %{_tmpdir} does not exist */
385 rpmlog(RPMLOG_DEBUG, _(" ... as %s\n"), tfn);
386 rc = urlGetFile(fileURL, tfn);
389 _("skipping %s - transfer failed - %s\n"),
390 fileURL, ftpStrerror(rc));
392 eiu->pkgURL[eiu->pkgx] = NULL;
396 eiu->pkgState[eiu->pkgx] = 1;
397 eiu->pkgURL[eiu->pkgx] = tfn;
401 case URL_IS_DASH: /* WRONG WRONG WRONG */
402 case URL_IS_HKP: /* WRONG WRONG WRONG */
404 eiu->pkgURL[eiu->pkgx] = fileURL;
410 fileURL = _free(fileURL);
412 if (eiu->numFailed) goto exit;
414 /* Continue processing file arguments, building transaction set. */
415 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
417 eiu->fnp++, eiu->prevx++)
419 const char * fileName;
421 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
422 (void) urlPath(*eiu->fnp, &fileName);
424 /* Try to read the header from a package file. */
425 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
426 if (eiu->fd == NULL || Ferror(eiu->fd)) {
427 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
429 if (eiu->fd != NULL) {
430 xx = Fclose(eiu->fd);
433 eiu->numFailed++; *eiu->fnp = NULL;
437 /* Read the header, verifying signatures (if present). */
438 tvsflags = rpmtsSetVSFlags(ts, vsflags);
439 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
440 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
441 xx = Fclose(eiu->fd);
444 switch (eiu->rpmrc) {
446 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
447 eiu->numFailed++; *eiu->fnp = NULL;
453 case RPMRC_NOTTRUSTED:
460 eiu->isSource = headerIsSource(eiu->h);
463 rpmlog(RPMLOG_DEBUG, _("\tadded source package [%d]\n"),
465 eiu->sourceURL = xrealloc(eiu->sourceURL,
466 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
467 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
470 eiu->sourceURL[eiu->numSRPMS] = NULL;
474 if (eiu->relocations) {
479 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
480 (void **) &paths, &c) && (c == 1))
482 eiu->relocations->oldPath = xstrdup(paths[0]);
483 paths = headerFreeData(paths, pft);
486 xx = headerNVR(eiu->h, &name, NULL, NULL);
488 _("package %s is not relocatable\n"), name);
494 /* On --freshen, verify package is installed and newer */
495 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
496 rpmdbMatchIterator mi;
501 xx = headerNVR(eiu->h, &name, NULL, NULL);
502 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
503 count = rpmdbGetIteratorCount(mi);
504 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
505 if (rpmVersionCompare(oldH, eiu->h) < 0)
507 /* same or newer package already installed */
511 mi = rpmdbFreeIterator(mi);
513 eiu->h = headerFree(eiu->h);
516 /* Package is newer than those currently installed. */
519 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
520 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
523 /* XXX reference held by transaction set */
524 eiu->h = headerFree(eiu->h);
525 if (eiu->relocations)
526 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
530 rpmlog(RPMLOG_DEBUG, _("\tadded binary package [%d]\n"),
535 _("error reading from file %s\n"), *eiu->fnp);
541 _("file %s requires a newer version of RPM\n"),
556 /* Try to read a package manifest. */
557 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
558 if (eiu->fd == NULL || Ferror(eiu->fd)) {
559 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
561 if (eiu->fd != NULL) {
562 xx = Fclose(eiu->fd);
565 eiu->numFailed++; *eiu->fnp = NULL;
569 /* Read list of packages from manifest. */
570 /* FIX: *eiu->argv can be NULL */
571 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
573 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
574 *eiu->fnp, Fstrerror(eiu->fd));
575 xx = Fclose(eiu->fd);
578 /* If successful, restart the query loop. */
579 if (rc == RPMRC_OK) {
584 eiu->numFailed++; *eiu->fnp = NULL;
588 rpmlog(RPMLOG_DEBUG, _("found %d source and %d binary packages\n"),
589 eiu->numSRPMS, eiu->numRPMS);
591 if (eiu->numFailed) goto exit;
593 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
595 if (rpmtsCheck(ts)) {
596 eiu->numFailed = eiu->numPkgs;
600 ps = rpmtsProblems(ts);
601 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
602 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
603 rpmpsPrint(NULL, ps);
604 eiu->numFailed = eiu->numPkgs;
607 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
608 rpmtsPrintSuggests(ts);
614 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
615 if (rpmtsOrder(ts)) {
616 eiu->numFailed = eiu->numPkgs;
621 if (eiu->numRPMS && !stopInstall) {
623 rpmcliPackagesTotal += eiu->numSRPMS;
625 rpmlog(RPMLOG_DEBUG, _("installing binary packages\n"));
627 /* Drop added/available package indices and dependency sets. */
630 rc = rpmtsRun(ts, NULL, probFilter);
631 ps = rpmtsProblems(ts);
634 eiu->numFailed += eiu->numRPMS;
636 eiu->numFailed += rc;
637 if (rpmpsNumProblems(ps) > 0)
638 rpmpsPrint(stderr, ps);
643 if (eiu->numSRPMS && !stopInstall) {
644 if (eiu->sourceURL != NULL)
645 for (i = 0; i < eiu->numSRPMS; i++) {
647 if (eiu->sourceURL[i] == NULL) continue;
648 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
649 if (eiu->fd == NULL || Ferror(eiu->fd)) {
650 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
651 eiu->sourceURL[i], Fstrerror(eiu->fd));
652 if (eiu->fd != NULL) {
653 xx = Fclose(eiu->fd);
659 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
660 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
661 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
664 xx = Fclose(eiu->fd);
670 if (eiu->pkgURL != NULL)
671 for (i = 0; i < eiu->numPkgs; i++) {
672 if (eiu->pkgURL[i] == NULL) continue;
673 if (eiu->pkgState[i] == 1)
674 (void) unlink(eiu->pkgURL[i]);
675 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
677 eiu->pkgState = _free(eiu->pkgState);
678 eiu->pkgURL = _free(eiu->pkgURL);
679 eiu->argv = _free(eiu->argv);
683 return eiu->numFailed;
686 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
692 int stopUninstall = 0;
694 rpmVSFlags vsflags, ovsflags;
697 if (argv == NULL) return 0;
699 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
700 if (ia->qva_flags & VERIFY_DIGEST)
701 vsflags |= _RPMVSF_NODIGESTS;
702 if (ia->qva_flags & VERIFY_SIGNATURE)
703 vsflags |= _RPMVSF_NOSIGNATURES;
704 if (ia->qva_flags & VERIFY_HDRCHK)
705 vsflags |= RPMVSF_NOHDRCHK;
706 ovsflags = rpmtsSetVSFlags(ts, vsflags);
708 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
709 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
711 /* XXX suggest mechanism only meaningful when installing */
712 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
714 (void) rpmtsSetFlags(ts, ia->transFlags);
716 #ifdef NOTYET /* XXX no callbacks on erase yet */
717 { int notifyFlags, xx;
718 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
719 xx = rpmtsSetNotifyCallback(ts,
720 rpmShowProgress, (void *) ((long)notifyFlags));
724 for (arg = argv; *arg; arg++) {
725 rpmdbMatchIterator mi;
727 /* XXX HACK to get rpmdbFindByLabel out of the API */
728 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
730 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
733 Header h; /* XXX iterator owns the reference */
735 while ((h = rpmdbNextIterator(mi)) != NULL) {
736 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
738 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
739 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
745 (void) rpmtsAddEraseElement(ts, h, recOffset);
750 mi = rpmdbFreeIterator(mi);
753 if (numFailed) goto exit;
755 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
757 if (rpmtsCheck(ts)) {
758 numFailed = numPackages;
762 ps = rpmtsProblems(ts);
763 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
764 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
765 rpmpsPrint(NULL, ps);
766 numFailed += numPackages;
772 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
773 if (rpmtsOrder(ts)) {
774 numFailed += numPackages;
779 if (numPackages && !stopUninstall) {
780 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
782 /* Drop added/available package indices and dependency sets. */
785 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
786 ps = rpmtsProblems(ts);
787 if (rpmpsNumProblems(ps) > 0)
788 rpmpsPrint(NULL, ps);
789 numFailed += numPackages;
800 int rpmInstallSource(rpmts ts, const char * arg,
801 char ** specFilePtr, char ** cookie)
807 fd = Fopen(arg, "r.ufdio");
808 if (fd == NULL || Ferror(fd)) {
809 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
810 if (fd != NULL) (void) Fclose(fd);
815 fprintf(stdout, _("Installing %s\n"), arg);
818 rpmVSFlags ovsflags =
819 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
820 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
821 rc = (rpmrc == RPMRC_OK ? 0 : 1);
822 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
825 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
826 if (specFilePtr && *specFilePtr)
827 *specFilePtr = _free(*specFilePtr);
828 if (cookie && *cookie)
829 *cookie = _free(*cookie);
837 /** @todo Transaction handling, more, needs work. */
838 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
840 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
841 unsigned thistid = 0xffffffff;
851 int vsflags, ovsflags;
855 int _unsafe_rollbacks = 0;
856 rpmtransFlags transFlags = ia->transFlags;
858 if (argv != NULL && *argv != NULL) {
863 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
865 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
866 if (ia->qva_flags & VERIFY_DIGEST)
867 vsflags |= _RPMVSF_NODIGESTS;
868 if (ia->qva_flags & VERIFY_SIGNATURE)
869 vsflags |= _RPMVSF_NOSIGNATURES;
870 if (ia->qva_flags & VERIFY_HDRCHK)
871 vsflags |= RPMVSF_NOHDRCHK;
872 vsflags |= RPMVSF_NEEDPAYLOAD; /* XXX no legacy signatures */
873 ovsflags = rpmtsSetVSFlags(ts, vsflags);
875 (void) rpmtsSetFlags(ts, transFlags);
877 /* Make the transaction a rollback transaction. In a rollback
878 * a best effort is what we want
880 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
882 itids = IDTXload(ts, RPMTAG_INSTALLTID);
891 { char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
892 if (globstr == NULL || *globstr == '%') {
893 globstr = _free(globstr);
897 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
906 globstr = _free(globstr);
909 { int notifyFlags, xx;
910 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
911 xx = rpmtsSetNotifyCallback(ts,
912 rpmShowProgress, (void *) ((long)notifyFlags));
915 /* Run transactions until rollback goal is achieved. */
919 rpmcliPackagesTotal = 0;
922 ia->installInterfaceFlags &= ~ifmask;
924 /* Find larger of the remaining install/erase transaction id's. */
926 if (ip != NULL && ip->val.u32 > thistid)
927 thistid = ip->val.u32;
928 if (rp != NULL && rp->val.u32 > thistid)
929 thistid = rp->val.u32;
931 /* If we've achieved the rollback goal, then we're done. */
932 if (thistid == 0 || thistid < ia->rbtid)
935 /* If we've reached the (configured) rollback goal, then we're done. */
936 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
940 (void) rpmtsSetFlags(ts, transFlags);
942 /* Install the previously erased packages for this transaction. */
943 while (rp != NULL && rp->val.u32 == thistid) {
945 rpmlog(RPMLOG_DEBUG, "\t+++ install %s\n",
946 (rp->key ? rp->key : "???"));
948 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
954 rpmcliPackagesTotal++;
955 if (!(ia->installInterfaceFlags & ifmask))
956 ia->installInterfaceFlags |= INSTALL_UPGRADE;
959 rp->h = headerFree(rp->h);
968 /* Erase the previously installed packages for this transaction. */
969 while (ip != NULL && ip->val.u32 == thistid) {
972 "\t--- erase h#%u\n", ip->instance);
974 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
980 if (_unsafe_rollbacks)
981 rpmcliPackagesTotal++;
983 if (!(ia->installInterfaceFlags & ifmask)) {
984 ia->installInterfaceFlags |= INSTALL_ERASE;
985 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
998 /* Anything to do? */
999 if (rpmcliPackagesTotal <= 0)
1002 tid = (time_t)thistid;
1003 rpmlog(RPMLOG_NOTICE,
1004 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1005 numAdded, numRemoved, ctime(&tid), tid);
1007 rc = rpmtsCheck(ts);
1008 ps = rpmtsProblems(ts);
1009 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1010 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
1011 rpmpsPrint(NULL, ps);
1017 rc = rpmtsOrder(ts);
1021 /* Drop added/available package indices and dependency sets. */
1024 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1025 ps = rpmtsProblems(ts);
1026 if (rc > 0 && rpmpsNumProblems(ps) > 0)
1027 rpmpsPrint(stderr, ps);
1032 /* Clean up after successful rollback. */
1033 if (rtids && !rpmIsDebug()) {
1035 rpmlog(RPMLOG_NOTICE, _("Cleaning up repackaged packages:\n"));
1037 for (i = 0; i < rtids->nidt; i++) {
1038 IDT rrp = rtids->idt + i;
1039 if (rrp->val.u32 != thistid)
1041 if (rrp->key) { /* XXX can't happen */
1042 rpmlog(RPMLOG_NOTICE, _("\tRemoving %s:\n"), rrp->key);
1043 (void) unlink(rrp->key); /* XXX: Should check rc??? */
1052 rtids = IDTXfree(rtids);
1053 itids = IDTXfree(itids);
1056 (void) rpmtsSetFlags(ts, transFlags);