2 * \file lib/rpminstall.c
13 #include <rpmfileutil.h>
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(RPMLOG_ERR, _("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(RPMLOG_ERR, _("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 #if defined(HAVE_MKSTEMP)
373 (void) close(mkstemp(tfnbuf));
375 (void) mktemp(tfnbuf);
377 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
380 /* XXX undefined %{name}/%{version}/%{release} here */
381 /* XXX %{_tmpdir} does not exist */
382 rpmlog(RPMLOG_DEBUG, _(" ... as %s\n"), tfn);
383 rc = urlGetFile(fileURL, tfn);
386 _("skipping %s - transfer failed - %s\n"),
387 fileURL, ftpStrerror(rc));
389 eiu->pkgURL[eiu->pkgx] = NULL;
393 eiu->pkgState[eiu->pkgx] = 1;
394 eiu->pkgURL[eiu->pkgx] = tfn;
398 case URL_IS_DASH: /* WRONG WRONG WRONG */
399 case URL_IS_HKP: /* WRONG WRONG WRONG */
401 eiu->pkgURL[eiu->pkgx] = fileURL;
407 fileURL = _free(fileURL);
409 if (eiu->numFailed) goto exit;
411 /* Continue processing file arguments, building transaction set. */
412 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
414 eiu->fnp++, eiu->prevx++)
416 const char * fileName;
418 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
419 (void) urlPath(*eiu->fnp, &fileName);
421 /* Try to read the header from a package file. */
422 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
423 if (eiu->fd == NULL || Ferror(eiu->fd)) {
424 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
426 if (eiu->fd != NULL) {
427 xx = Fclose(eiu->fd);
430 eiu->numFailed++; *eiu->fnp = NULL;
434 /* Read the header, verifying signatures (if present). */
435 tvsflags = rpmtsSetVSFlags(ts, vsflags);
436 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
437 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
438 xx = Fclose(eiu->fd);
441 switch (eiu->rpmrc) {
443 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
444 eiu->numFailed++; *eiu->fnp = NULL;
450 case RPMRC_NOTTRUSTED:
457 eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
460 rpmlog(RPMLOG_DEBUG, _("\tadded source package [%d]\n"),
462 eiu->sourceURL = xrealloc(eiu->sourceURL,
463 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
464 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
467 eiu->sourceURL[eiu->numSRPMS] = NULL;
471 if (eiu->relocations) {
476 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
477 (void **) &paths, &c) && (c == 1))
479 eiu->relocations->oldPath = xstrdup(paths[0]);
480 paths = headerFreeData(paths, pft);
483 xx = headerNVR(eiu->h, &name, NULL, NULL);
485 _("package %s is not relocatable\n"), name);
491 /* On --freshen, verify package is installed and newer */
492 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
493 rpmdbMatchIterator mi;
498 xx = headerNVR(eiu->h, &name, NULL, NULL);
499 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
500 count = rpmdbGetIteratorCount(mi);
501 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
502 if (rpmVersionCompare(oldH, eiu->h) < 0)
504 /* same or newer package already installed */
508 mi = rpmdbFreeIterator(mi);
510 eiu->h = headerFree(eiu->h);
513 /* Package is newer than those currently installed. */
516 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
517 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
520 /* XXX reference held by transaction set */
521 eiu->h = headerFree(eiu->h);
522 if (eiu->relocations)
523 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
527 rpmlog(RPMLOG_DEBUG, _("\tadded binary package [%d]\n"),
532 _("error reading from file %s\n"), *eiu->fnp);
538 _("file %s requires a newer version of RPM\n"),
553 /* Try to read a package manifest. */
554 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
555 if (eiu->fd == NULL || Ferror(eiu->fd)) {
556 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
558 if (eiu->fd != NULL) {
559 xx = Fclose(eiu->fd);
562 eiu->numFailed++; *eiu->fnp = NULL;
566 /* Read list of packages from manifest. */
567 /* FIX: *eiu->argv can be NULL */
568 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
570 rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
571 *eiu->fnp, Fstrerror(eiu->fd));
572 xx = Fclose(eiu->fd);
575 /* If successful, restart the query loop. */
576 if (rc == RPMRC_OK) {
581 eiu->numFailed++; *eiu->fnp = NULL;
585 rpmlog(RPMLOG_DEBUG, _("found %d source and %d binary packages\n"),
586 eiu->numSRPMS, eiu->numRPMS);
588 if (eiu->numFailed) goto exit;
590 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
592 if (rpmtsCheck(ts)) {
593 eiu->numFailed = eiu->numPkgs;
597 ps = rpmtsProblems(ts);
598 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
599 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
600 rpmpsPrint(NULL, ps);
601 eiu->numFailed = eiu->numPkgs;
604 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
605 rpmtsPrintSuggests(ts);
611 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
612 if (rpmtsOrder(ts)) {
613 eiu->numFailed = eiu->numPkgs;
618 if (eiu->numRPMS && !stopInstall) {
620 rpmcliPackagesTotal += eiu->numSRPMS;
622 rpmlog(RPMLOG_DEBUG, _("installing binary packages\n"));
624 /* Drop added/available package indices and dependency sets. */
627 rc = rpmtsRun(ts, NULL, probFilter);
628 ps = rpmtsProblems(ts);
631 eiu->numFailed += eiu->numRPMS;
633 eiu->numFailed += rc;
634 if (rpmpsNumProblems(ps) > 0)
635 rpmpsPrint(stderr, ps);
640 if (eiu->numSRPMS && !stopInstall) {
641 if (eiu->sourceURL != NULL)
642 for (i = 0; i < eiu->numSRPMS; i++) {
644 if (eiu->sourceURL[i] == NULL) continue;
645 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
646 if (eiu->fd == NULL || Ferror(eiu->fd)) {
647 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
648 eiu->sourceURL[i], Fstrerror(eiu->fd));
649 if (eiu->fd != NULL) {
650 xx = Fclose(eiu->fd);
656 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
657 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
658 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
661 xx = Fclose(eiu->fd);
667 if (eiu->pkgURL != NULL)
668 for (i = 0; i < eiu->numPkgs; i++) {
669 if (eiu->pkgURL[i] == NULL) continue;
670 if (eiu->pkgState[i] == 1)
671 (void) Unlink(eiu->pkgURL[i]);
672 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
674 eiu->pkgState = _free(eiu->pkgState);
675 eiu->pkgURL = _free(eiu->pkgURL);
676 eiu->argv = _free(eiu->argv);
680 return eiu->numFailed;
683 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
689 int stopUninstall = 0;
691 rpmVSFlags vsflags, ovsflags;
694 if (argv == NULL) return 0;
696 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
697 if (ia->qva_flags & VERIFY_DIGEST)
698 vsflags |= _RPMVSF_NODIGESTS;
699 if (ia->qva_flags & VERIFY_SIGNATURE)
700 vsflags |= _RPMVSF_NOSIGNATURES;
701 if (ia->qva_flags & VERIFY_HDRCHK)
702 vsflags |= RPMVSF_NOHDRCHK;
703 ovsflags = rpmtsSetVSFlags(ts, vsflags);
705 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
706 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
708 /* XXX suggest mechanism only meaningful when installing */
709 ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
711 (void) rpmtsSetFlags(ts, ia->transFlags);
713 #ifdef NOTYET /* XXX no callbacks on erase yet */
714 { int notifyFlags, xx;
715 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
716 xx = rpmtsSetNotifyCallback(ts,
717 rpmShowProgress, (void *) ((long)notifyFlags));
721 for (arg = argv; *arg; arg++) {
722 rpmdbMatchIterator mi;
724 /* XXX HACK to get rpmdbFindByLabel out of the API */
725 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
727 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
730 Header h; /* XXX iterator owns the reference */
732 while ((h = rpmdbNextIterator(mi)) != NULL) {
733 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
735 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
736 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
742 (void) rpmtsAddEraseElement(ts, h, recOffset);
747 mi = rpmdbFreeIterator(mi);
750 if (numFailed) goto exit;
752 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
754 if (rpmtsCheck(ts)) {
755 numFailed = numPackages;
759 ps = rpmtsProblems(ts);
760 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
761 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
762 rpmpsPrint(NULL, ps);
763 numFailed += numPackages;
769 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
770 if (rpmtsOrder(ts)) {
771 numFailed += numPackages;
776 if (numPackages && !stopUninstall) {
777 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
779 /* Drop added/available package indices and dependency sets. */
782 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
783 ps = rpmtsProblems(ts);
784 if (rpmpsNumProblems(ps) > 0)
785 rpmpsPrint(NULL, ps);
786 numFailed += numPackages;
797 int rpmInstallSource(rpmts ts, const char * arg,
798 const char ** specFilePtr, const char ** cookie)
804 fd = Fopen(arg, "r.ufdio");
805 if (fd == NULL || Ferror(fd)) {
806 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
807 if (fd != NULL) (void) Fclose(fd);
812 fprintf(stdout, _("Installing %s\n"), arg);
815 rpmVSFlags ovsflags =
816 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
817 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
818 rc = (rpmrc == RPMRC_OK ? 0 : 1);
819 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
822 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
823 if (specFilePtr && *specFilePtr)
824 *specFilePtr = _free(*specFilePtr);
825 if (cookie && *cookie)
826 *cookie = _free(*cookie);
834 /** @todo Transaction handling, more, needs work. */
835 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
837 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
838 unsigned thistid = 0xffffffff;
848 int vsflags, ovsflags;
852 int _unsafe_rollbacks = 0;
853 rpmtransFlags transFlags = ia->transFlags;
855 if (argv != NULL && *argv != NULL) {
860 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
862 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
863 if (ia->qva_flags & VERIFY_DIGEST)
864 vsflags |= _RPMVSF_NODIGESTS;
865 if (ia->qva_flags & VERIFY_SIGNATURE)
866 vsflags |= _RPMVSF_NOSIGNATURES;
867 if (ia->qva_flags & VERIFY_HDRCHK)
868 vsflags |= RPMVSF_NOHDRCHK;
869 vsflags |= RPMVSF_NEEDPAYLOAD; /* XXX no legacy signatures */
870 ovsflags = rpmtsSetVSFlags(ts, vsflags);
872 (void) rpmtsSetFlags(ts, transFlags);
874 /* Make the transaction a rollback transaction. In a rollback
875 * a best effort is what we want
877 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
879 itids = IDTXload(ts, RPMTAG_INSTALLTID);
888 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
889 if (globstr == NULL || *globstr == '%') {
890 globstr = _free(globstr);
894 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
903 globstr = _free(globstr);
906 { int notifyFlags, xx;
907 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
908 xx = rpmtsSetNotifyCallback(ts,
909 rpmShowProgress, (void *) ((long)notifyFlags));
912 /* Run transactions until rollback goal is achieved. */
916 rpmcliPackagesTotal = 0;
919 ia->installInterfaceFlags &= ~ifmask;
921 /* Find larger of the remaining install/erase transaction id's. */
923 if (ip != NULL && ip->val.u32 > thistid)
924 thistid = ip->val.u32;
925 if (rp != NULL && rp->val.u32 > thistid)
926 thistid = rp->val.u32;
928 /* If we've achieved the rollback goal, then we're done. */
929 if (thistid == 0 || thistid < ia->rbtid)
932 /* If we've reached the (configured) rollback goal, then we're done. */
933 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
937 (void) rpmtsSetFlags(ts, transFlags);
939 /* Install the previously erased packages for this transaction. */
940 while (rp != NULL && rp->val.u32 == thistid) {
942 rpmlog(RPMLOG_DEBUG, "\t+++ install %s\n",
943 (rp->key ? rp->key : "???"));
945 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
951 rpmcliPackagesTotal++;
952 if (!(ia->installInterfaceFlags & ifmask))
953 ia->installInterfaceFlags |= INSTALL_UPGRADE;
956 rp->h = headerFree(rp->h);
965 /* Erase the previously installed packages for this transaction. */
966 while (ip != NULL && ip->val.u32 == thistid) {
969 "\t--- erase h#%u\n", ip->instance);
971 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
977 if (_unsafe_rollbacks)
978 rpmcliPackagesTotal++;
980 if (!(ia->installInterfaceFlags & ifmask)) {
981 ia->installInterfaceFlags |= INSTALL_ERASE;
982 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
995 /* Anything to do? */
996 if (rpmcliPackagesTotal <= 0)
999 tid = (time_t)thistid;
1000 rpmlog(RPMLOG_NOTICE,
1001 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1002 numAdded, numRemoved, ctime(&tid), tid);
1004 rc = rpmtsCheck(ts);
1005 ps = rpmtsProblems(ts);
1006 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1007 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
1008 rpmpsPrint(NULL, ps);
1014 rc = rpmtsOrder(ts);
1018 /* Drop added/available package indices and dependency sets. */
1021 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1022 ps = rpmtsProblems(ts);
1023 if (rc > 0 && rpmpsNumProblems(ps) > 0)
1024 rpmpsPrint(stderr, ps);
1029 /* Clean up after successful rollback. */
1030 if (rtids && !rpmIsDebug()) {
1032 rpmlog(RPMLOG_NOTICE, _("Cleaning up repackaged packages:\n"));
1034 for (i = 0; i < rtids->nidt; i++) {
1035 IDT rrp = rtids->idt + i;
1036 if (rrp->val.u32 != thistid)
1038 if (rrp->key) { /* XXX can't happen */
1039 rpmlog(RPMLOG_NOTICE, _("\tRemoving %s:\n"), rrp->key);
1040 (void) unlink(rrp->key); /* XXX: Should check rc??? */
1049 rtids = IDTXfree(rtids);
1050 itids = IDTXfree(itids);
1053 (void) rpmtsSetFlags(ts, transFlags);