00001
00005 #include "system.h"
00006
00007 #include <rpmcli.h>
00008
00009 #include "rpmdb.h"
00010 #include "rpmds.h"
00011
00012 #include "rpmte.h"
00013 #define _RPMTS_INTERNAL
00014 #include "rpmts.h"
00015
00016 #include "manifest.h"
00017 #include "misc.h"
00018 #include "debug.h"
00019
00020
00021
00022
00023
00024
00025 int rpmcliPackagesTotal = 0;
00026
00027 int rpmcliHashesCurrent = 0;
00028
00029 int rpmcliHashesTotal = 0;
00030
00031 int rpmcliProgressCurrent = 0;
00032
00033 int rpmcliProgressTotal = 0;
00034
00041 static void printHash(const unsigned long amount, const unsigned long total)
00042
00043
00044
00045
00046 {
00047 int hashesNeeded;
00048
00049 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
00050
00051 if (rpmcliHashesCurrent != rpmcliHashesTotal) {
00052
00053 float pct = (total ? (((float) amount) / total) : 1.0);
00054 hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
00055
00056 while (hashesNeeded > rpmcliHashesCurrent) {
00057 if (isatty (STDOUT_FILENO)) {
00058 int i;
00059 for (i = 0; i < rpmcliHashesCurrent; i++)
00060 (void) putchar ('#');
00061 for (; i < rpmcliHashesTotal; i++)
00062 (void) putchar (' ');
00063 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
00064 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
00065 (void) putchar ('\b');
00066 } else
00067 fprintf(stdout, "#");
00068
00069 rpmcliHashesCurrent++;
00070 }
00071 (void) fflush(stdout);
00072
00073 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
00074 int i;
00075 rpmcliProgressCurrent++;
00076 if (isatty(STDOUT_FILENO)) {
00077 for (i = 1; i < rpmcliHashesCurrent; i++)
00078 (void) putchar ('#');
00079
00080 pct = (rpmcliProgressTotal
00081 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
00082 : 1);
00083
00084 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
00085 }
00086 fprintf(stdout, "\n");
00087 }
00088 (void) fflush(stdout);
00089 }
00090 }
00091
00092 void * rpmShowProgress( const void * arg,
00093 const rpmCallbackType what,
00094 const unsigned long amount,
00095 const unsigned long total,
00096 fnpyKey key,
00097 void * data)
00098
00099
00100
00101
00102 {
00103
00104 Header h = (Header) arg;
00105
00106 char * s;
00107 int flags = (int) ((long)data);
00108 void * rc = NULL;
00109
00110 const char * filename = (const char *)key;
00111
00112 static FD_t fd = NULL;
00113 int xx;
00114
00115 switch (what) {
00116 case RPMCALLBACK_INST_OPEN_FILE:
00117
00118 if (filename == NULL || filename[0] == '\0')
00119 return NULL;
00120
00121 fd = Fopen(filename, "r.ufdio");
00122
00123 if (fd == NULL || Ferror(fd)) {
00124 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), filename,
00125 Fstrerror(fd));
00126 if (fd != NULL) {
00127 xx = Fclose(fd);
00128 fd = NULL;
00129 }
00130 } else
00131 fd = fdLink(fd, "persist (showProgress)");
00132
00133
00134 return (void *)fd;
00135
00136 break;
00137
00138 case RPMCALLBACK_INST_CLOSE_FILE:
00139
00140 fd = fdFree(fd, "persist (showProgress)");
00141
00142 if (fd != NULL) {
00143 xx = Fclose(fd);
00144 fd = NULL;
00145 }
00146 break;
00147
00148 case RPMCALLBACK_INST_START:
00149 rpmcliHashesCurrent = 0;
00150 if (h == NULL || !(flags & INSTALL_LABEL))
00151 break;
00152
00153 if (flags & INSTALL_HASH) {
00154 s = headerSprintf(h, "%{NAME}",
00155 rpmTagTable, rpmHeaderFormats, NULL);
00156 if (isatty (STDOUT_FILENO))
00157 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
00158 else
00159 fprintf(stdout, "%-28.28s", s);
00160 (void) fflush(stdout);
00161 s = _free(s);
00162 } else {
00163 s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
00164 rpmTagTable, rpmHeaderFormats, NULL);
00165 fprintf(stdout, "%s\n", s);
00166 (void) fflush(stdout);
00167 s = _free(s);
00168 }
00169 break;
00170
00171 case RPMCALLBACK_TRANS_PROGRESS:
00172 case RPMCALLBACK_INST_PROGRESS:
00173
00174 if (flags & INSTALL_PERCENT)
00175 fprintf(stdout, "%%%% %f\n", (double) (total
00176 ? ((((float) amount) / total) * 100)
00177 : 100.0));
00178 else if (flags & INSTALL_HASH)
00179 printHash(amount, total);
00180
00181 (void) fflush(stdout);
00182 break;
00183
00184 case RPMCALLBACK_TRANS_START:
00185 rpmcliHashesCurrent = 0;
00186 rpmcliProgressTotal = 1;
00187 rpmcliProgressCurrent = 0;
00188 if (!(flags & INSTALL_LABEL))
00189 break;
00190 if (flags & INSTALL_HASH)
00191 fprintf(stdout, "%-28s", _("Preparing..."));
00192 else
00193 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
00194 (void) fflush(stdout);
00195 break;
00196
00197 case RPMCALLBACK_TRANS_STOP:
00198 if (flags & INSTALL_HASH)
00199 printHash(1, 1);
00200 rpmcliProgressTotal = rpmcliPackagesTotal;
00201 rpmcliProgressCurrent = 0;
00202 break;
00203
00204 case RPMCALLBACK_REPACKAGE_START:
00205 rpmcliHashesCurrent = 0;
00206 rpmcliProgressTotal = total;
00207 rpmcliProgressCurrent = 0;
00208 if (!(flags & INSTALL_LABEL))
00209 break;
00210 if (flags & INSTALL_HASH)
00211 fprintf(stdout, "%-28s\n", _("Repackaging..."));
00212 else
00213 fprintf(stdout, "%s\n", _("Repackaging erased files..."));
00214 (void) fflush(stdout);
00215 break;
00216
00217 case RPMCALLBACK_REPACKAGE_PROGRESS:
00218 if (amount && (flags & INSTALL_HASH))
00219 printHash(1, 1);
00220 break;
00221
00222 case RPMCALLBACK_REPACKAGE_STOP:
00223 rpmcliProgressTotal = total;
00224 rpmcliProgressCurrent = total;
00225 if (flags & INSTALL_HASH)
00226 printHash(1, 1);
00227 rpmcliProgressTotal = rpmcliPackagesTotal;
00228 rpmcliProgressCurrent = 0;
00229 if (!(flags & INSTALL_LABEL))
00230 break;
00231 if (flags & INSTALL_HASH)
00232 fprintf(stdout, "%-28s\n", _("Upgrading..."));
00233 else
00234 fprintf(stdout, "%s\n", _("Upgrading packages..."));
00235 (void) fflush(stdout);
00236 break;
00237
00238 case RPMCALLBACK_UNINST_PROGRESS:
00239 break;
00240 case RPMCALLBACK_UNINST_START:
00241 break;
00242 case RPMCALLBACK_UNINST_STOP:
00243 break;
00244 case RPMCALLBACK_UNPACK_ERROR:
00245 break;
00246 case RPMCALLBACK_CPIO_ERROR:
00247 break;
00248 case RPMCALLBACK_UNKNOWN:
00249 default:
00250 break;
00251 }
00252
00253 return rc;
00254 }
00255
00256 typedef const char * str_t;
00257
00258 struct rpmEIU {
00259 Header h;
00260 FD_t fd;
00261 int numFailed;
00262 int numPkgs;
00263
00264 str_t * pkgURL;
00265
00266 str_t * fnp;
00267
00268 char * pkgState;
00269 int prevx;
00270 int pkgx;
00271 int numRPMS;
00272 int numSRPMS;
00273
00274 str_t * sourceURL;
00275 int isSource;
00276 int argc;
00277
00278 str_t * argv;
00279
00280 rpmRelocation * relocations;
00281 rpmRC rpmrc;
00282 };
00283
00285
00286 int rpmInstall(rpmts ts,
00287 struct rpmInstallArguments_s * ia,
00288 const char ** fileArgv)
00289 {
00290 struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
00291 rpmps ps;
00292 rpmprobFilterFlags probFilter;
00293 rpmRelocation * relocations;
00294 const char * fileURL = NULL;
00295 int stopInstall = 0;
00296 const char ** av = NULL;
00297 rpmVSFlags vsflags, ovsflags, tvsflags;
00298 int ac = 0;
00299 int rc;
00300 int xx;
00301 int i;
00302 int secontexts = 0;
00303
00304 if (fileArgv == NULL) goto exit;
00305
00306 ts->goal = TSM_INSTALL;
00307 rpmcliPackagesTotal = 0;
00308
00309 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00310 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00311
00312
00313 if (rpmtsSELinuxEnabled(ts) &&
00314 !(ia->transFlags & RPMTRANS_FLAG_NOCONTEXTS)) {
00315 const char *fn = rpmGetPath("%{?_install_file_context_path}", NULL);
00316 if (fn != NULL && *fn != '\0') {
00317 secontexts = (matchpathcon_init(fn) == 0);
00318 }
00319 }
00320 (void) rpmtsSetFlags(ts, ia->transFlags);
00321
00322 probFilter = ia->probFilter;
00323 relocations = ia->relocations;
00324
00325 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
00326 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00327 else
00328 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
00329 if (ia->qva_flags & VERIFY_DIGEST)
00330 vsflags |= _RPMVSF_NODIGESTS;
00331 if (ia->qva_flags & VERIFY_SIGNATURE)
00332 vsflags |= _RPMVSF_NOSIGNATURES;
00333 if (ia->qva_flags & VERIFY_HDRCHK)
00334 vsflags |= RPMVSF_NOHDRCHK;
00335 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
00336
00337 { int notifyFlags;
00338 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00339 xx = rpmtsSetNotifyCallback(ts,
00340 rpmShowProgress, (void *) ((long)notifyFlags));
00341 }
00342
00343 if ((eiu->relocations = relocations) != NULL) {
00344 while (eiu->relocations->oldPath)
00345 eiu->relocations++;
00346 if (eiu->relocations->newPath == NULL)
00347 eiu->relocations = NULL;
00348 }
00349
00350
00351
00352
00353 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
00354
00355 char * fn;
00356 av = _free(av); ac = 0;
00357 fn = rpmEscapeSpaces(*eiu->fnp);
00358 rc = rpmGlob(fn, &ac, &av);
00359 fn = _free(fn);
00360 if (rc || ac == 0) {
00361 rpmError(RPMERR_OPEN, _("File not found by glob: %s\n"), *eiu->fnp);
00362 eiu->numFailed++;
00363 continue;
00364 }
00365
00366 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
00367 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
00368 eiu->argc += ac;
00369 eiu->argv[eiu->argc] = NULL;
00370 }
00371
00372 av = _free(av); ac = 0;
00373
00374 restart:
00375
00376 if (eiu->pkgx >= eiu->numPkgs) {
00377 eiu->numPkgs = eiu->pkgx + eiu->argc;
00378 eiu->pkgURL = xrealloc(eiu->pkgURL,
00379 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
00380 memset(eiu->pkgURL + eiu->pkgx, 0,
00381 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
00382 eiu->pkgState = xrealloc(eiu->pkgState,
00383 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
00384 memset(eiu->pkgState + eiu->pkgx, 0,
00385 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
00386 }
00387
00388
00389 for (i = 0; i < eiu->argc; i++) {
00390 fileURL = _free(fileURL);
00391 fileURL = eiu->argv[i];
00392 eiu->argv[i] = NULL;
00393
00394 #ifdef NOTYET
00395 if (fileURL[0] == '=') {
00396 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
00397
00398 xx = rpmtsSolve(ts, this, NULL);
00399 if (ts->suggests && ts->nsuggests > 0) {
00400 fileURL = _free(fileURL);
00401 fileURL = ts->suggests[0];
00402 ts->suggests[0] = NULL;
00403 while (ts->nsuggests-- > 0) {
00404 if (ts->suggests[ts->nsuggests] == NULL)
00405 continue;
00406 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
00407 }
00408 ts->suggests = _free(ts->suggests);
00409 rpmMessage(RPMMESS_DEBUG, _("Adding goal: %s\n"), fileURL);
00410 eiu->pkgURL[eiu->pkgx] = fileURL;
00411 fileURL = NULL;
00412 eiu->pkgx++;
00413 }
00414 this = rpmdsFree(this);
00415 } else
00416 #endif
00417
00418 switch (urlIsURL(fileURL)) {
00419 case URL_IS_HTTPS:
00420 case URL_IS_HTTP:
00421 case URL_IS_FTP:
00422 { const char *tfn;
00423
00424 if (rpmIsVerbose())
00425 fprintf(stdout, _("Retrieving %s\n"), fileURL);
00426
00427 { char tfnbuf[64];
00428 const char * rootDir = rpmtsRootDir(ts);
00429 if (!(rootDir && * rootDir))
00430 rootDir = "";
00431 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
00432 (void) mktemp(tfnbuf);
00433 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
00434 }
00435
00436
00437
00438 rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
00439 rc = urlGetFile(fileURL, tfn);
00440 if (rc < 0) {
00441 rpmMessage(RPMMESS_ERROR,
00442 _("skipping %s - transfer failed - %s\n"),
00443 fileURL, ftpStrerror(rc));
00444 eiu->numFailed++;
00445 eiu->pkgURL[eiu->pkgx] = NULL;
00446 tfn = _free(tfn);
00447 break;
00448 }
00449 eiu->pkgState[eiu->pkgx] = 1;
00450 eiu->pkgURL[eiu->pkgx] = tfn;
00451 eiu->pkgx++;
00452 } break;
00453 case URL_IS_PATH:
00454 case URL_IS_DASH:
00455 case URL_IS_HKP:
00456 default:
00457 eiu->pkgURL[eiu->pkgx] = fileURL;
00458 fileURL = NULL;
00459 eiu->pkgx++;
00460 break;
00461 }
00462 }
00463 fileURL = _free(fileURL);
00464
00465 if (eiu->numFailed) goto exit;
00466
00467
00468 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
00469 *eiu->fnp != NULL;
00470 eiu->fnp++, eiu->prevx++)
00471 {
00472 const char * fileName;
00473
00474 rpmMessage(RPMMESS_DEBUG, "============== %s\n", *eiu->fnp);
00475 (void) urlPath(*eiu->fnp, &fileName);
00476
00477
00478 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
00479 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00480 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
00481 Fstrerror(eiu->fd));
00482 if (eiu->fd != NULL) {
00483 xx = Fclose(eiu->fd);
00484 eiu->fd = NULL;
00485 }
00486 eiu->numFailed++; *eiu->fnp = NULL;
00487 continue;
00488 }
00489
00490
00491 tvsflags = rpmtsSetVSFlags(ts, vsflags);
00492 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
00493 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
00494 xx = Fclose(eiu->fd);
00495 eiu->fd = NULL;
00496
00497 switch (eiu->rpmrc) {
00498 case RPMRC_FAIL:
00499 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *eiu->fnp);
00500 eiu->numFailed++; *eiu->fnp = NULL;
00501 continue;
00502 break;
00503 case RPMRC_NOTFOUND:
00504 goto maybe_manifest;
00505 break;
00506 case RPMRC_NOTTRUSTED:
00507 case RPMRC_NOKEY:
00508 case RPMRC_OK:
00509 default:
00510 break;
00511 }
00512
00513 eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
00514
00515 if (eiu->isSource) {
00516 rpmMessage(RPMMESS_DEBUG, _("\tadded source package [%d]\n"),
00517 eiu->numSRPMS);
00518 eiu->sourceURL = xrealloc(eiu->sourceURL,
00519 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
00520 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
00521 *eiu->fnp = NULL;
00522 eiu->numSRPMS++;
00523 eiu->sourceURL[eiu->numSRPMS] = NULL;
00524 continue;
00525 }
00526
00527 if (eiu->relocations) {
00528 const char ** paths;
00529 int pft;
00530 int c;
00531
00532 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
00533 (void **) &paths, &c) && (c == 1))
00534 {
00535 eiu->relocations->oldPath = xstrdup(paths[0]);
00536 paths = headerFreeData(paths, pft);
00537 } else {
00538 const char * name;
00539 xx = headerNVR(eiu->h, &name, NULL, NULL);
00540 rpmMessage(RPMMESS_ERROR,
00541 _("package %s is not relocatable\n"), name);
00542 eiu->numFailed++;
00543 goto exit;
00544
00545 }
00546 }
00547
00548
00549 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
00550 rpmdbMatchIterator mi;
00551 const char * name;
00552 Header oldH;
00553 int count;
00554
00555 xx = headerNVR(eiu->h, &name, NULL, NULL);
00556 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
00557 count = rpmdbGetIteratorCount(mi);
00558 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
00559 if (rpmVersionCompare(oldH, eiu->h) < 0)
00560 continue;
00561
00562 count = 0;
00563 break;
00564 }
00565 mi = rpmdbFreeIterator(mi);
00566 if (count == 0) {
00567 eiu->h = headerFree(eiu->h);
00568 continue;
00569 }
00570
00571 }
00572
00573
00574 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
00575 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
00576 relocations);
00577
00578
00579
00580 eiu->h = headerFree(eiu->h);
00581 if (eiu->relocations)
00582 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
00583
00584 switch(rc) {
00585 case 0:
00586 rpmMessage(RPMMESS_DEBUG, _("\tadded binary package [%d]\n"),
00587 eiu->numRPMS);
00588 break;
00589 case 1:
00590 rpmMessage(RPMMESS_ERROR,
00591 _("error reading from file %s\n"), *eiu->fnp);
00592 eiu->numFailed++;
00593 goto exit;
00594 break;
00595 case 2:
00596 rpmMessage(RPMMESS_ERROR,
00597 _("file %s requires a newer version of RPM\n"),
00598 *eiu->fnp);
00599 eiu->numFailed++;
00600 goto exit;
00601 break;
00602 default:
00603 eiu->numFailed++;
00604 goto exit;
00605 break;
00606 }
00607
00608 eiu->numRPMS++;
00609 continue;
00610
00611 maybe_manifest:
00612
00613 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
00614 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00615 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
00616 Fstrerror(eiu->fd));
00617 if (eiu->fd != NULL) {
00618 xx = Fclose(eiu->fd);
00619 eiu->fd = NULL;
00620 }
00621 eiu->numFailed++; *eiu->fnp = NULL;
00622 break;
00623 }
00624
00625
00626
00627 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
00628
00629 if (rc != RPMRC_OK)
00630 rpmError(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
00631 *eiu->fnp, Fstrerror(eiu->fd));
00632 xx = Fclose(eiu->fd);
00633 eiu->fd = NULL;
00634
00635
00636 if (rc == RPMRC_OK) {
00637 eiu->prevx++;
00638 goto restart;
00639 }
00640
00641 eiu->numFailed++; *eiu->fnp = NULL;
00642 break;
00643 }
00644
00645 rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
00646 eiu->numSRPMS, eiu->numRPMS);
00647
00648 if (eiu->numFailed) goto exit;
00649
00650 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
00651
00652 if (rpmtsCheck(ts)) {
00653 eiu->numFailed = eiu->numPkgs;
00654 stopInstall = 1;
00655 }
00656
00657 ps = rpmtsProblems(ts);
00658 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
00659 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00660 rpmpsPrint(NULL, ps);
00661 eiu->numFailed = eiu->numPkgs;
00662 stopInstall = 1;
00663
00664
00665 if (ts->suggests != NULL && ts->nsuggests > 0) {
00666 rpmMessage(RPMMESS_NORMAL, _(" Suggested resolutions:\n"));
00667 for (i = 0; i < ts->nsuggests; i++) {
00668 const char * str = ts->suggests[i];
00669
00670 if (str == NULL)
00671 break;
00672
00673 rpmMessage(RPMMESS_NORMAL, "\t%s\n", str);
00674
00675 ts->suggests[i] = NULL;
00676 str = _free(str);
00677 }
00678 ts->suggests = _free(ts->suggests);
00679 }
00680
00681 }
00682 ps = rpmpsFree(ps);
00683 }
00684
00685 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00686 if (rpmtsOrder(ts)) {
00687 eiu->numFailed = eiu->numPkgs;
00688 stopInstall = 1;
00689 }
00690 }
00691
00692 if (eiu->numRPMS && !stopInstall) {
00693
00694 rpmcliPackagesTotal += eiu->numSRPMS;
00695
00696 rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
00697
00698
00699 rpmtsClean(ts);
00700
00701 rc = rpmtsRun(ts, NULL, probFilter);
00702 ps = rpmtsProblems(ts);
00703
00704 if (rc < 0) {
00705 eiu->numFailed += eiu->numRPMS;
00706 } else if (rc > 0) {
00707 eiu->numFailed += rc;
00708 if (rpmpsNumProblems(ps) > 0)
00709 rpmpsPrint(stderr, ps);
00710 }
00711 ps = rpmpsFree(ps);
00712 }
00713
00714 if (eiu->numSRPMS && !stopInstall) {
00715 if (eiu->sourceURL != NULL)
00716 for (i = 0; i < eiu->numSRPMS; i++) {
00717 rpmdbCheckSignals();
00718 if (eiu->sourceURL[i] == NULL) continue;
00719 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
00720 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00721 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
00722 eiu->sourceURL[i], Fstrerror(eiu->fd));
00723 if (eiu->fd != NULL) {
00724 xx = Fclose(eiu->fd);
00725 eiu->fd = NULL;
00726 }
00727 continue;
00728 }
00729
00730 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
00731 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
00732 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
00733 }
00734
00735 xx = Fclose(eiu->fd);
00736 eiu->fd = NULL;
00737 }
00738 }
00739
00740 exit:
00741 if (eiu->pkgURL != NULL)
00742 for (i = 0; i < eiu->numPkgs; i++) {
00743 if (eiu->pkgURL[i] == NULL) continue;
00744 if (eiu->pkgState[i] == 1)
00745 (void) Unlink(eiu->pkgURL[i]);
00746 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
00747 }
00748 eiu->pkgState = _free(eiu->pkgState);
00749 eiu->pkgURL = _free(eiu->pkgURL);
00750 eiu->argv = _free(eiu->argv);
00751
00752 if (secontexts) {
00753 matchpathcon_fini();
00754 }
00755
00756 rpmtsEmpty(ts);
00757
00758 return eiu->numFailed;
00759 }
00760
00761
00762 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
00763 const char ** argv)
00764 {
00765 int count;
00766 const char ** arg;
00767 int numFailed = 0;
00768 int stopUninstall = 0;
00769 int numPackages = 0;
00770 rpmVSFlags vsflags, ovsflags;
00771 rpmps ps;
00772
00773 if (argv == NULL) return 0;
00774
00775 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00776 if (ia->qva_flags & VERIFY_DIGEST)
00777 vsflags |= _RPMVSF_NODIGESTS;
00778 if (ia->qva_flags & VERIFY_SIGNATURE)
00779 vsflags |= _RPMVSF_NOSIGNATURES;
00780 if (ia->qva_flags & VERIFY_HDRCHK)
00781 vsflags |= RPMVSF_NOHDRCHK;
00782 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00783
00784 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00785 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00786
00787 (void) rpmtsSetFlags(ts, ia->transFlags);
00788
00789 #ifdef NOTYET
00790 { int notifyFlags;
00791 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00792 xx = rpmtsSetNotifyCallback(ts,
00793 rpmShowProgress, (void *) ((long)notifyFlags)
00794 }
00795 #endif
00796
00797 ts->goal = TSM_ERASE;
00798
00799 for (arg = argv; *arg; arg++) {
00800 rpmdbMatchIterator mi;
00801
00802
00803 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
00804 if (mi == NULL) {
00805 rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
00806 numFailed++;
00807 } else {
00808 Header h;
00809 count = 0;
00810 while ((h = rpmdbNextIterator(mi)) != NULL) {
00811 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
00812
00813 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
00814 rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
00815 *arg);
00816 numFailed++;
00817 break;
00818 }
00819 if (recOffset) {
00820 (void) rpmtsAddEraseElement(ts, h, recOffset);
00821 numPackages++;
00822 }
00823 }
00824 }
00825 mi = rpmdbFreeIterator(mi);
00826 }
00827
00828 if (numFailed) goto exit;
00829
00830 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
00831
00832 if (rpmtsCheck(ts)) {
00833 numFailed = numPackages;
00834 stopUninstall = 1;
00835 }
00836
00837 ps = rpmtsProblems(ts);
00838 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
00839 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00840 rpmpsPrint(NULL, ps);
00841 numFailed += numPackages;
00842 stopUninstall = 1;
00843 }
00844 ps = rpmpsFree(ps);
00845 }
00846
00847 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00848 if (rpmtsOrder(ts)) {
00849 numFailed += numPackages;
00850 stopUninstall = 1;
00851 }
00852 }
00853
00854 if (numPackages && !stopUninstall) {
00855 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
00856
00857
00858 rpmtsClean(ts);
00859
00860 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
00861 ps = rpmtsProblems(ts);
00862 if (rpmpsNumProblems(ps) > 0)
00863 rpmpsPrint(NULL, ps);
00864 numFailed += numPackages;
00865 stopUninstall = 1;
00866 ps = rpmpsFree(ps);
00867 }
00868
00869 exit:
00870 rpmtsEmpty(ts);
00871
00872 return numFailed;
00873 }
00874
00875 int rpmInstallSource(rpmts ts, const char * arg,
00876 const char ** specFilePtr, const char ** cookie)
00877 {
00878 FD_t fd;
00879 int rc;
00880
00881
00882 fd = Fopen(arg, "r.ufdio");
00883 if (fd == NULL || Ferror(fd)) {
00884 rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
00885 if (fd != NULL) (void) Fclose(fd);
00886 return 1;
00887 }
00888
00889 if (rpmIsVerbose())
00890 fprintf(stdout, _("Installing %s\n"), arg);
00891
00892 {
00893 rpmVSFlags ovsflags =
00894 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
00895 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
00896 rc = (rpmrc == RPMRC_OK ? 0 : 1);
00897 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
00898 }
00899 if (rc != 0) {
00900 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
00901
00902 if (specFilePtr && *specFilePtr)
00903 *specFilePtr = _free(*specFilePtr);
00904 if (cookie && *cookie)
00905 *cookie = _free(*cookie);
00906
00907 }
00908
00909 (void) Fclose(fd);
00910
00911 return rc;
00912 }
00913
00914
00915 static int reverse = -1;
00916
00919 static int IDTintcmp(const void * a, const void * b)
00920
00921 {
00922
00923 return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
00924
00925 }
00926
00927 IDTX IDTXfree(IDTX idtx)
00928 {
00929 if (idtx) {
00930 int i;
00931 if (idtx->idt)
00932 for (i = 0; i < idtx->nidt; i++) {
00933 IDT idt = idtx->idt + i;
00934 idt->h = headerFree(idt->h);
00935 idt->key = _free(idt->key);
00936 }
00937 idtx->idt = _free(idtx->idt);
00938 idtx = _free(idtx);
00939 }
00940 return NULL;
00941 }
00942
00943 IDTX IDTXnew(void)
00944 {
00945 IDTX idtx = xcalloc(1, sizeof(*idtx));
00946 idtx->delta = 10;
00947 idtx->size = sizeof(*((IDT)0));
00948 return idtx;
00949 }
00950
00951 IDTX IDTXgrow(IDTX idtx, int need)
00952 {
00953 if (need < 0) return NULL;
00954 if (idtx == NULL)
00955 idtx = IDTXnew();
00956 if (need == 0) return idtx;
00957
00958 if ((idtx->nidt + need) > idtx->alloced) {
00959 while (need > 0) {
00960 idtx->alloced += idtx->delta;
00961 need -= idtx->delta;
00962 }
00963 idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
00964 }
00965 return idtx;
00966 }
00967
00968 IDTX IDTXsort(IDTX idtx)
00969 {
00970 if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
00971 qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
00972 return idtx;
00973 }
00974
00975 IDTX IDTXload(rpmts ts, rpmTag tag)
00976 {
00977 IDTX idtx = NULL;
00978 rpmdbMatchIterator mi;
00979 HGE_t hge = (HGE_t) headerGetEntry;
00980 Header h;
00981
00982
00983 mi = rpmtsInitIterator(ts, tag, NULL, 0);
00984 #ifdef NOTYET
00985 (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey');
00986 #endif
00987 while ((h = rpmdbNextIterator(mi)) != NULL) {
00988 rpmTagType type = RPM_NULL_TYPE;
00989 int_32 count = 0;
00990 int_32 * tidp;
00991
00992 tidp = NULL;
00993 if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
00994 continue;
00995
00996 if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
00997 continue;
00998
00999 idtx = IDTXgrow(idtx, 1);
01000 if (idtx == NULL)
01001 continue;
01002 if (idtx->idt == NULL)
01003 continue;
01004
01005 { IDT idt;
01006
01007 idt = idtx->idt + idtx->nidt;
01008
01009 idt->h = headerLink(h);
01010 idt->key = NULL;
01011 idt->instance = rpmdbGetIteratorOffset(mi);
01012 idt->val.u32 = *tidp;
01013 }
01014 idtx->nidt++;
01015 }
01016 mi = rpmdbFreeIterator(mi);
01017
01018
01019 return IDTXsort(idtx);
01020 }
01021
01022 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
01023 {
01024 IDTX idtx = NULL;
01025 HGE_t hge = (HGE_t) headerGetEntry;
01026 Header h;
01027 int_32 * tidp;
01028 FD_t fd;
01029 const char ** av = NULL;
01030 int ac = 0;
01031 rpmRC rpmrc;
01032 int xx;
01033 int i;
01034
01035 av = NULL; ac = 0;
01036 xx = rpmGlob(globstr, &ac, &av);
01037
01038 if (xx == 0)
01039 for (i = 0; i < ac; i++) {
01040 rpmTagType type;
01041 int_32 count;
01042 int isSource;
01043
01044 fd = Fopen(av[i], "r.ufdio");
01045 if (fd == NULL || Ferror(fd)) {
01046 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
01047 Fstrerror(fd));
01048 if (fd != NULL) (void) Fclose(fd);
01049 continue;
01050 }
01051
01052 rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
01053 (void) Fclose(fd);
01054 switch (rpmrc) {
01055 default:
01056 goto bottom;
01057 break;
01058 case RPMRC_NOTTRUSTED:
01059 case RPMRC_NOKEY:
01060 case RPMRC_OK:
01061 isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
01062 if (isSource)
01063 goto bottom;
01064 break;
01065 }
01066
01067 tidp = NULL;
01068
01069 if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) {
01070
01071 idtx = IDTXgrow(idtx, 1);
01072 if (idtx == NULL || idtx->idt == NULL)
01073 goto bottom;
01074
01075 { IDT idt;
01076 idt = idtx->idt + idtx->nidt;
01077 idt->h = headerLink(h);
01078 idt->key = av[i];
01079 av[i] = NULL;
01080 idt->instance = 0;
01081 idt->val.u32 = *tidp;
01082 }
01083 idtx->nidt++;
01084 }
01085
01086 bottom:
01087 h = headerFree(h);
01088 }
01089
01090 for (i = 0; i < ac; i++)
01091 av[i] = _free(av[i]);
01092 av = _free(av); ac = 0;
01093
01094 return IDTXsort(idtx);
01095 }
01096
01098 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
01099 {
01100 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
01101 unsigned thistid = 0xffffffff;
01102 unsigned prevtid;
01103 time_t tid;
01104 IDTX itids = NULL;
01105 IDTX rtids = NULL;
01106 IDT rp;
01107 int nrids = 0;
01108 IDT ip;
01109 int niids = 0;
01110 int rc = 0;
01111 int vsflags, ovsflags;
01112 int numAdded;
01113 int numRemoved;
01114 rpmps ps;
01115 int _unsafe_rollbacks = 0;
01116 rpmtransFlags transFlags = ia->transFlags;
01117
01118 if (argv != NULL && *argv != NULL) {
01119 rc = -1;
01120 goto exit;
01121 }
01122
01123 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
01124
01125 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
01126 if (ia->qva_flags & VERIFY_DIGEST)
01127 vsflags |= _RPMVSF_NODIGESTS;
01128 if (ia->qva_flags & VERIFY_SIGNATURE)
01129 vsflags |= _RPMVSF_NOSIGNATURES;
01130 if (ia->qva_flags & VERIFY_HDRCHK)
01131 vsflags |= RPMVSF_NOHDRCHK;
01132 vsflags |= RPMVSF_NEEDPAYLOAD;
01133 ovsflags = rpmtsSetVSFlags(ts, vsflags);
01134
01135 (void) rpmtsSetFlags(ts, transFlags);
01136
01137
01138
01139
01140 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
01141
01142 itids = IDTXload(ts, RPMTAG_INSTALLTID);
01143 if (itids != NULL) {
01144 ip = itids->idt;
01145 niids = itids->nidt;
01146 } else {
01147 ip = NULL;
01148 niids = 0;
01149 }
01150
01151 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
01152 if (globstr == NULL || *globstr == '%') {
01153 globstr = _free(globstr);
01154 rc = -1;
01155 goto exit;
01156 }
01157 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
01158
01159 if (rtids != NULL) {
01160 rp = rtids->idt;
01161 nrids = rtids->nidt;
01162 } else {
01163 rp = NULL;
01164 nrids = 0;
01165 }
01166 globstr = _free(globstr);
01167 }
01168
01169 { int notifyFlags, xx;
01170 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
01171 xx = rpmtsSetNotifyCallback(ts,
01172 rpmShowProgress, (void *) ((long)notifyFlags));
01173 }
01174
01175
01176 do {
01177 prevtid = thistid;
01178 rc = 0;
01179 rpmcliPackagesTotal = 0;
01180 numAdded = 0;
01181 numRemoved = 0;
01182 ia->installInterfaceFlags &= ~ifmask;
01183
01184
01185 thistid = 0;
01186 if (ip != NULL && ip->val.u32 > thistid)
01187 thistid = ip->val.u32;
01188 if (rp != NULL && rp->val.u32 > thistid)
01189 thistid = rp->val.u32;
01190
01191
01192 if (thistid == 0 || thistid < ia->rbtid)
01193 break;
01194
01195
01196 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
01197 break;
01198
01199 rpmtsEmpty(ts);
01200 (void) rpmtsSetFlags(ts, transFlags);
01201
01202
01203 while (rp != NULL && rp->val.u32 == thistid) {
01204
01205 rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
01206 (rp->key ? rp->key : "???"));
01207
01208
01209 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
01210 0, ia->relocations);
01211
01212 if (rc != 0)
01213 goto exit;
01214
01215 numAdded++;
01216 rpmcliPackagesTotal++;
01217 if (!(ia->installInterfaceFlags & ifmask))
01218 ia->installInterfaceFlags |= INSTALL_UPGRADE;
01219
01220 #ifdef NOTYET
01221 rp->h = headerFree(rp->h);
01222 #endif
01223 nrids--;
01224 if (nrids > 0)
01225 rp++;
01226 else
01227 rp = NULL;
01228 }
01229
01230
01231 while (ip != NULL && ip->val.u32 == thistid) {
01232
01233 rpmMessage(RPMMESS_DEBUG,
01234 "\t--- erase h#%u\n", ip->instance);
01235
01236 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
01237 if (rc != 0)
01238 goto exit;
01239
01240 numRemoved++;
01241
01242 if (_unsafe_rollbacks)
01243 rpmcliPackagesTotal++;
01244
01245 if (!(ia->installInterfaceFlags & ifmask)) {
01246 ia->installInterfaceFlags |= INSTALL_ERASE;
01247 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
01248 }
01249
01250 #ifdef NOTYET
01251 ip->instance = 0;
01252 #endif
01253 niids--;
01254 if (niids > 0)
01255 ip++;
01256 else
01257 ip = NULL;
01258 }
01259
01260
01261 if (rpmcliPackagesTotal <= 0)
01262 break;
01263
01264 tid = (time_t)thistid;
01265 rpmMessage(RPMMESS_NORMAL,
01266 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
01267 numAdded, numRemoved, ctime(&tid), tid);
01268
01269 rc = rpmtsCheck(ts);
01270 ps = rpmtsProblems(ts);
01271 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
01272 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
01273 rpmpsPrint(NULL, ps);
01274 ps = rpmpsFree(ps);
01275 goto exit;
01276 }
01277 ps = rpmpsFree(ps);
01278
01279 rc = rpmtsOrder(ts);
01280 if (rc != 0)
01281 goto exit;
01282
01283
01284 rpmtsClean(ts);
01285
01286 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
01287 ps = rpmtsProblems(ts);
01288 if (rc > 0 && rpmpsNumProblems(ps) > 0)
01289 rpmpsPrint(stderr, ps);
01290 ps = rpmpsFree(ps);
01291 if (rc)
01292 goto exit;
01293
01294
01295 if (rtids && !rpmIsDebug()) {
01296 int i;
01297 rpmMessage(RPMMESS_NORMAL, _("Cleaning up repackaged packages:\n"));
01298 if (rtids->idt)
01299 for (i = 0; i < rtids->nidt; i++) {
01300 IDT rrp = rtids->idt + i;
01301 if (rrp->val.u32 != thistid)
01302 continue;
01303 if (rrp->key) {
01304 rpmMessage(RPMMESS_NORMAL, _("\tRemoving %s:\n"), rrp->key);
01305 (void) unlink(rrp->key);
01306 }
01307 }
01308 }
01309
01310
01311 } while (1);
01312
01313 exit:
01314 rtids = IDTXfree(rtids);
01315 itids = IDTXfree(itids);
01316
01317 rpmtsEmpty(ts);
01318 (void) rpmtsSetFlags(ts, transFlags);
01319
01320 return rc;
01321 }