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
00303 if (fileArgv == NULL) goto exit;
00304
00305 ts->goal = TSM_INSTALL;
00306 rpmcliPackagesTotal = 0;
00307
00308 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00309 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00310
00311
00312 if (!(ia->transFlags & RPMTRANS_FLAG_NOCONTEXTS)) {
00313 rpmsx sx = rpmtsREContext(ts);
00314 if (sx == NULL) {
00315 const char *fn = rpmGetPath("%{?_install_file_context_path}", NULL);
00316 if (fn != NULL && *fn != '\0') {
00317 sx = rpmsxNew(fn);
00318 (void) rpmtsSetREContext(ts, sx);
00319 }
00320 fn = _free(fn);
00321 }
00322 sx = rpmsxFree(sx);
00323 }
00324 (void) rpmtsSetFlags(ts, ia->transFlags);
00325
00326 probFilter = ia->probFilter;
00327 relocations = ia->relocations;
00328
00329 if (ia->installInterfaceFlags & INSTALL_UPGRADE)
00330 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00331 else
00332 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
00333 if (ia->qva_flags & VERIFY_DIGEST)
00334 vsflags |= _RPMVSF_NODIGESTS;
00335 if (ia->qva_flags & VERIFY_SIGNATURE)
00336 vsflags |= _RPMVSF_NOSIGNATURES;
00337 if (ia->qva_flags & VERIFY_HDRCHK)
00338 vsflags |= RPMVSF_NOHDRCHK;
00339 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
00340
00341 { int notifyFlags;
00342 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00343 xx = rpmtsSetNotifyCallback(ts,
00344 rpmShowProgress, (void *) ((long)notifyFlags));
00345 }
00346
00347 if ((eiu->relocations = relocations) != NULL) {
00348 while (eiu->relocations->oldPath)
00349 eiu->relocations++;
00350 if (eiu->relocations->newPath == NULL)
00351 eiu->relocations = NULL;
00352 }
00353
00354
00355
00356
00357 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
00358
00359 char * fn;
00360 av = _free(av); ac = 0;
00361 fn = rpmEscapeSpaces(*eiu->fnp);
00362 rc = rpmGlob(fn, &ac, &av);
00363 fn = _free(fn);
00364 if (rc || ac == 0) {
00365 rpmError(RPMERR_OPEN, _("File not found by glob: %s\n"), *eiu->fnp);
00366 continue;
00367 }
00368
00369 eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
00370 memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
00371 eiu->argc += ac;
00372 eiu->argv[eiu->argc] = NULL;
00373 }
00374
00375 av = _free(av); ac = 0;
00376
00377 restart:
00378
00379 if (eiu->pkgx >= eiu->numPkgs) {
00380 eiu->numPkgs = eiu->pkgx + eiu->argc;
00381 eiu->pkgURL = xrealloc(eiu->pkgURL,
00382 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
00383 memset(eiu->pkgURL + eiu->pkgx, 0,
00384 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
00385 eiu->pkgState = xrealloc(eiu->pkgState,
00386 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
00387 memset(eiu->pkgState + eiu->pkgx, 0,
00388 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
00389 }
00390
00391
00392 for (i = 0; i < eiu->argc; i++) {
00393 fileURL = _free(fileURL);
00394 fileURL = eiu->argv[i];
00395 eiu->argv[i] = NULL;
00396
00397 #ifdef NOTYET
00398 if (fileURL[0] == '=') {
00399 rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
00400
00401 xx = rpmtsSolve(ts, this, NULL);
00402 if (ts->suggests && ts->nsuggests > 0) {
00403 fileURL = _free(fileURL);
00404 fileURL = ts->suggests[0];
00405 ts->suggests[0] = NULL;
00406 while (ts->nsuggests-- > 0) {
00407 if (ts->suggests[ts->nsuggests] == NULL)
00408 continue;
00409 ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
00410 }
00411 ts->suggests = _free(ts->suggests);
00412 rpmMessage(RPMMESS_DEBUG, _("Adding goal: %s\n"), fileURL);
00413 eiu->pkgURL[eiu->pkgx] = fileURL;
00414 fileURL = NULL;
00415 eiu->pkgx++;
00416 }
00417 this = rpmdsFree(this);
00418 } else
00419 #endif
00420
00421 switch (urlIsURL(fileURL)) {
00422 case URL_IS_HTTPS:
00423 case URL_IS_HTTP:
00424 case URL_IS_FTP:
00425 { const char *tfn;
00426
00427 if (rpmIsVerbose())
00428 fprintf(stdout, _("Retrieving %s\n"), fileURL);
00429
00430 { char tfnbuf[64];
00431 const char * rootDir = rpmtsRootDir(ts);
00432 if (!(rootDir && * rootDir))
00433 rootDir = "";
00434 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
00435 (void) mktemp(tfnbuf);
00436 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
00437 }
00438
00439
00440
00441 rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
00442 rc = urlGetFile(fileURL, tfn);
00443 if (rc < 0) {
00444 rpmMessage(RPMMESS_ERROR,
00445 _("skipping %s - transfer failed - %s\n"),
00446 fileURL, ftpStrerror(rc));
00447 eiu->numFailed++;
00448 eiu->pkgURL[eiu->pkgx] = NULL;
00449 tfn = _free(tfn);
00450 break;
00451 }
00452 eiu->pkgState[eiu->pkgx] = 1;
00453 eiu->pkgURL[eiu->pkgx] = tfn;
00454 eiu->pkgx++;
00455 } break;
00456 case URL_IS_PATH:
00457 case URL_IS_DASH:
00458 case URL_IS_HKP:
00459 default:
00460 eiu->pkgURL[eiu->pkgx] = fileURL;
00461 fileURL = NULL;
00462 eiu->pkgx++;
00463 break;
00464 }
00465 }
00466 fileURL = _free(fileURL);
00467
00468 if (eiu->numFailed) goto exit;
00469
00470
00471 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
00472 *eiu->fnp != NULL;
00473 eiu->fnp++, eiu->prevx++)
00474 {
00475 const char * fileName;
00476
00477 rpmMessage(RPMMESS_DEBUG, "============== %s\n", *eiu->fnp);
00478 (void) urlPath(*eiu->fnp, &fileName);
00479
00480
00481 eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
00482 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00483 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
00484 Fstrerror(eiu->fd));
00485 if (eiu->fd != NULL) {
00486 xx = Fclose(eiu->fd);
00487 eiu->fd = NULL;
00488 }
00489 eiu->numFailed++; *eiu->fnp = NULL;
00490 continue;
00491 }
00492
00493
00494 tvsflags = rpmtsSetVSFlags(ts, vsflags);
00495 eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
00496 tvsflags = rpmtsSetVSFlags(ts, tvsflags);
00497 xx = Fclose(eiu->fd);
00498 eiu->fd = NULL;
00499
00500 switch (eiu->rpmrc) {
00501 case RPMRC_FAIL:
00502 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *eiu->fnp);
00503 eiu->numFailed++; *eiu->fnp = NULL;
00504 continue;
00505 break;
00506 case RPMRC_NOTFOUND:
00507 goto maybe_manifest;
00508 break;
00509 case RPMRC_NOTTRUSTED:
00510 case RPMRC_NOKEY:
00511 case RPMRC_OK:
00512 default:
00513 break;
00514 }
00515
00516 eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
00517
00518 if (eiu->isSource) {
00519 rpmMessage(RPMMESS_DEBUG, _("\tadded source package [%d]\n"),
00520 eiu->numSRPMS);
00521 eiu->sourceURL = xrealloc(eiu->sourceURL,
00522 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
00523 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
00524 *eiu->fnp = NULL;
00525 eiu->numSRPMS++;
00526 eiu->sourceURL[eiu->numSRPMS] = NULL;
00527 continue;
00528 }
00529
00530 if (eiu->relocations) {
00531 const char ** paths;
00532 int pft;
00533 int c;
00534
00535 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
00536 (void **) &paths, &c) && (c == 1))
00537 {
00538 eiu->relocations->oldPath = xstrdup(paths[0]);
00539 paths = headerFreeData(paths, pft);
00540 } else {
00541 const char * name;
00542 xx = headerNVR(eiu->h, &name, NULL, NULL);
00543 rpmMessage(RPMMESS_ERROR,
00544 _("package %s is not relocatable\n"), name);
00545 eiu->numFailed++;
00546 goto exit;
00547
00548 }
00549 }
00550
00551
00552 if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
00553 rpmdbMatchIterator mi;
00554 const char * name;
00555 Header oldH;
00556 int count;
00557
00558 xx = headerNVR(eiu->h, &name, NULL, NULL);
00559 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
00560 count = rpmdbGetIteratorCount(mi);
00561 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
00562 if (rpmVersionCompare(oldH, eiu->h) < 0)
00563 continue;
00564
00565 count = 0;
00566 break;
00567 }
00568 mi = rpmdbFreeIterator(mi);
00569 if (count == 0) {
00570 eiu->h = headerFree(eiu->h);
00571 continue;
00572 }
00573
00574 }
00575
00576
00577 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
00578 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
00579 relocations);
00580
00581
00582
00583 eiu->h = headerFree(eiu->h);
00584 if (eiu->relocations)
00585 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
00586
00587 switch(rc) {
00588 case 0:
00589 rpmMessage(RPMMESS_DEBUG, _("\tadded binary package [%d]\n"),
00590 eiu->numRPMS);
00591 break;
00592 case 1:
00593 rpmMessage(RPMMESS_ERROR,
00594 _("error reading from file %s\n"), *eiu->fnp);
00595 eiu->numFailed++;
00596 goto exit;
00597 break;
00598 case 2:
00599 rpmMessage(RPMMESS_ERROR,
00600 _("file %s requires a newer version of RPM\n"),
00601 *eiu->fnp);
00602 eiu->numFailed++;
00603 goto exit;
00604 break;
00605 default:
00606 eiu->numFailed++;
00607 goto exit;
00608 break;
00609 }
00610
00611 eiu->numRPMS++;
00612 continue;
00613
00614 maybe_manifest:
00615
00616 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
00617 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00618 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
00619 Fstrerror(eiu->fd));
00620 if (eiu->fd != NULL) {
00621 xx = Fclose(eiu->fd);
00622 eiu->fd = NULL;
00623 }
00624 eiu->numFailed++; *eiu->fnp = NULL;
00625 break;
00626 }
00627
00628
00629
00630 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
00631
00632 if (rc != RPMRC_OK)
00633 rpmError(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
00634 *eiu->fnp, Fstrerror(eiu->fd));
00635 xx = Fclose(eiu->fd);
00636 eiu->fd = NULL;
00637
00638
00639 if (rc == RPMRC_OK) {
00640 eiu->prevx++;
00641 goto restart;
00642 }
00643
00644 eiu->numFailed++; *eiu->fnp = NULL;
00645 break;
00646 }
00647
00648 rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
00649 eiu->numSRPMS, eiu->numRPMS);
00650
00651 if (eiu->numFailed) goto exit;
00652
00653 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
00654
00655 if (rpmtsCheck(ts)) {
00656 eiu->numFailed = eiu->numPkgs;
00657 stopInstall = 1;
00658 }
00659
00660 ps = rpmtsProblems(ts);
00661 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
00662 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00663 rpmpsPrint(NULL, ps);
00664 eiu->numFailed = eiu->numPkgs;
00665 stopInstall = 1;
00666
00667
00668 if (ts->suggests != NULL && ts->nsuggests > 0) {
00669 rpmMessage(RPMMESS_NORMAL, _(" Suggested resolutions:\n"));
00670 for (i = 0; i < ts->nsuggests; i++) {
00671 const char * str = ts->suggests[i];
00672
00673 if (str == NULL)
00674 break;
00675
00676 rpmMessage(RPMMESS_NORMAL, "\t%s\n", str);
00677
00678 ts->suggests[i] = NULL;
00679 str = _free(str);
00680 }
00681 ts->suggests = _free(ts->suggests);
00682 }
00683
00684 }
00685 ps = rpmpsFree(ps);
00686 }
00687
00688 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00689 if (rpmtsOrder(ts)) {
00690 eiu->numFailed = eiu->numPkgs;
00691 stopInstall = 1;
00692 }
00693 }
00694
00695 if (eiu->numRPMS && !stopInstall) {
00696
00697 rpmcliPackagesTotal += eiu->numSRPMS;
00698
00699 rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
00700
00701
00702 rpmtsClean(ts);
00703
00704 rc = rpmtsRun(ts, NULL, probFilter);
00705 ps = rpmtsProblems(ts);
00706
00707 if (rc < 0) {
00708 eiu->numFailed += eiu->numRPMS;
00709 } else if (rc > 0) {
00710 eiu->numFailed += rc;
00711 if (rpmpsNumProblems(ps) > 0)
00712 rpmpsPrint(stderr, ps);
00713 }
00714 ps = rpmpsFree(ps);
00715 }
00716
00717 if (eiu->numSRPMS && !stopInstall) {
00718 if (eiu->sourceURL != NULL)
00719 for (i = 0; i < eiu->numSRPMS; i++) {
00720 rpmdbCheckSignals();
00721 if (eiu->sourceURL[i] == NULL) continue;
00722 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
00723 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00724 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
00725 eiu->sourceURL[i], Fstrerror(eiu->fd));
00726 if (eiu->fd != NULL) {
00727 xx = Fclose(eiu->fd);
00728 eiu->fd = NULL;
00729 }
00730 continue;
00731 }
00732
00733 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
00734 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
00735 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
00736 }
00737
00738 xx = Fclose(eiu->fd);
00739 eiu->fd = NULL;
00740 }
00741 }
00742
00743 exit:
00744 if (eiu->pkgURL != NULL)
00745 for (i = 0; i < eiu->numPkgs; i++) {
00746 if (eiu->pkgURL[i] == NULL) continue;
00747 if (eiu->pkgState[i] == 1)
00748 (void) Unlink(eiu->pkgURL[i]);
00749 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
00750 }
00751 eiu->pkgState = _free(eiu->pkgState);
00752 eiu->pkgURL = _free(eiu->pkgURL);
00753 eiu->argv = _free(eiu->argv);
00754
00755 rpmtsEmpty(ts);
00756
00757 return eiu->numFailed;
00758 }
00759
00760
00761 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
00762 const char ** argv)
00763 {
00764 int count;
00765 const char ** arg;
00766 int numFailed = 0;
00767 int stopUninstall = 0;
00768 int numPackages = 0;
00769 rpmVSFlags vsflags, ovsflags;
00770 rpmps ps;
00771
00772 if (argv == NULL) return 0;
00773
00774 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00775 if (ia->qva_flags & VERIFY_DIGEST)
00776 vsflags |= _RPMVSF_NODIGESTS;
00777 if (ia->qva_flags & VERIFY_SIGNATURE)
00778 vsflags |= _RPMVSF_NOSIGNATURES;
00779 if (ia->qva_flags & VERIFY_HDRCHK)
00780 vsflags |= RPMVSF_NOHDRCHK;
00781 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00782
00783 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00784 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00785
00786 (void) rpmtsSetFlags(ts, ia->transFlags);
00787
00788 #ifdef NOTYET
00789 { int notifyFlags;
00790 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00791 xx = rpmtsSetNotifyCallback(ts,
00792 rpmShowProgress, (void *) ((long)notifyFlags)
00793 }
00794 #endif
00795
00796 ts->goal = TSM_ERASE;
00797
00798 for (arg = argv; *arg; arg++) {
00799 rpmdbMatchIterator mi;
00800
00801
00802 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
00803 if (mi == NULL) {
00804 rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
00805 numFailed++;
00806 } else {
00807 Header h;
00808 count = 0;
00809 while ((h = rpmdbNextIterator(mi)) != NULL) {
00810 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
00811
00812 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
00813 rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
00814 *arg);
00815 numFailed++;
00816 break;
00817 }
00818 if (recOffset) {
00819 (void) rpmtsAddEraseElement(ts, h, recOffset);
00820 numPackages++;
00821 }
00822 }
00823 }
00824 mi = rpmdbFreeIterator(mi);
00825 }
00826
00827 if (numFailed) goto exit;
00828
00829 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
00830
00831 if (rpmtsCheck(ts)) {
00832 numFailed = numPackages;
00833 stopUninstall = 1;
00834 }
00835
00836 ps = rpmtsProblems(ts);
00837 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
00838 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00839 rpmpsPrint(NULL, ps);
00840 numFailed += numPackages;
00841 stopUninstall = 1;
00842 }
00843 ps = rpmpsFree(ps);
00844 }
00845
00846 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00847 if (rpmtsOrder(ts)) {
00848 numFailed += numPackages;
00849 stopUninstall = 1;
00850 }
00851 }
00852
00853 if (numPackages && !stopUninstall) {
00854 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
00855
00856
00857 rpmtsClean(ts);
00858
00859 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
00860 ps = rpmtsProblems(ts);
00861 if (rpmpsNumProblems(ps) > 0)
00862 rpmpsPrint(NULL, ps);
00863 numFailed += numPackages;
00864 stopUninstall = 1;
00865 ps = rpmpsFree(ps);
00866 }
00867
00868 exit:
00869 rpmtsEmpty(ts);
00870
00871 return numFailed;
00872 }
00873
00874 int rpmInstallSource(rpmts ts, const char * arg,
00875 const char ** specFilePtr, const char ** cookie)
00876 {
00877 FD_t fd;
00878 int rc;
00879
00880
00881 fd = Fopen(arg, "r.ufdio");
00882 if (fd == NULL || Ferror(fd)) {
00883 rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
00884 if (fd != NULL) (void) Fclose(fd);
00885 return 1;
00886 }
00887
00888 if (rpmIsVerbose())
00889 fprintf(stdout, _("Installing %s\n"), arg);
00890
00891 {
00892 rpmVSFlags ovsflags =
00893 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
00894 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
00895 rc = (rpmrc == RPMRC_OK ? 0 : 1);
00896 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
00897 }
00898 if (rc != 0) {
00899 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
00900
00901 if (specFilePtr && *specFilePtr)
00902 *specFilePtr = _free(*specFilePtr);
00903 if (cookie && *cookie)
00904 *cookie = _free(*cookie);
00905
00906 }
00907
00908 (void) Fclose(fd);
00909
00910 return rc;
00911 }
00912
00913
00914 static int reverse = -1;
00915
00918 static int IDTintcmp(const void * a, const void * b)
00919
00920 {
00921
00922 return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
00923
00924 }
00925
00926 IDTX IDTXfree(IDTX idtx)
00927 {
00928 if (idtx) {
00929 int i;
00930 if (idtx->idt)
00931 for (i = 0; i < idtx->nidt; i++) {
00932 IDT idt = idtx->idt + i;
00933 idt->h = headerFree(idt->h);
00934 idt->key = _free(idt->key);
00935 }
00936 idtx->idt = _free(idtx->idt);
00937 idtx = _free(idtx);
00938 }
00939 return NULL;
00940 }
00941
00942 IDTX IDTXnew(void)
00943 {
00944 IDTX idtx = xcalloc(1, sizeof(*idtx));
00945 idtx->delta = 10;
00946 idtx->size = sizeof(*((IDT)0));
00947 return idtx;
00948 }
00949
00950 IDTX IDTXgrow(IDTX idtx, int need)
00951 {
00952 if (need < 0) return NULL;
00953 if (idtx == NULL)
00954 idtx = IDTXnew();
00955 if (need == 0) return idtx;
00956
00957 if ((idtx->nidt + need) > idtx->alloced) {
00958 while (need > 0) {
00959 idtx->alloced += idtx->delta;
00960 need -= idtx->delta;
00961 }
00962 idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
00963 }
00964 return idtx;
00965 }
00966
00967 IDTX IDTXsort(IDTX idtx)
00968 {
00969 if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
00970 qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
00971 return idtx;
00972 }
00973
00974 IDTX IDTXload(rpmts ts, rpmTag tag)
00975 {
00976 IDTX idtx = NULL;
00977 rpmdbMatchIterator mi;
00978 HGE_t hge = (HGE_t) headerGetEntry;
00979 Header h;
00980
00981
00982 mi = rpmtsInitIterator(ts, tag, NULL, 0);
00983 #ifdef NOTYET
00984 (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey');
00985 #endif
00986 while ((h = rpmdbNextIterator(mi)) != NULL) {
00987 rpmTagType type = RPM_NULL_TYPE;
00988 int_32 count = 0;
00989 int_32 * tidp;
00990
00991 tidp = NULL;
00992 if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
00993 continue;
00994
00995 if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
00996 continue;
00997
00998 idtx = IDTXgrow(idtx, 1);
00999 if (idtx == NULL)
01000 continue;
01001 if (idtx->idt == NULL)
01002 continue;
01003
01004 { IDT idt;
01005
01006 idt = idtx->idt + idtx->nidt;
01007
01008 idt->h = headerLink(h);
01009 idt->key = NULL;
01010 idt->instance = rpmdbGetIteratorOffset(mi);
01011 idt->val.u32 = *tidp;
01012 }
01013 idtx->nidt++;
01014 }
01015 mi = rpmdbFreeIterator(mi);
01016
01017
01018 return IDTXsort(idtx);
01019 }
01020
01021 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
01022 {
01023 IDTX idtx = NULL;
01024 HGE_t hge = (HGE_t) headerGetEntry;
01025 Header h;
01026 int_32 * tidp;
01027 FD_t fd;
01028 const char ** av = NULL;
01029 int ac = 0;
01030 rpmRC rpmrc;
01031 int xx;
01032 int i;
01033
01034 av = NULL; ac = 0;
01035 xx = rpmGlob(globstr, &ac, &av);
01036
01037 if (xx == 0)
01038 for (i = 0; i < ac; i++) {
01039 rpmTagType type;
01040 int_32 count;
01041 int isSource;
01042
01043 fd = Fopen(av[i], "r.ufdio");
01044 if (fd == NULL || Ferror(fd)) {
01045 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
01046 Fstrerror(fd));
01047 if (fd != NULL) (void) Fclose(fd);
01048 continue;
01049 }
01050
01051 rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
01052 (void) Fclose(fd);
01053 switch (rpmrc) {
01054 default:
01055 goto bottom;
01056 break;
01057 case RPMRC_NOTTRUSTED:
01058 case RPMRC_NOKEY:
01059 case RPMRC_OK:
01060 isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
01061 if (isSource)
01062 goto bottom;
01063 break;
01064 }
01065
01066 tidp = NULL;
01067
01068 if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) {
01069
01070 idtx = IDTXgrow(idtx, 1);
01071 if (idtx == NULL || idtx->idt == NULL)
01072 goto bottom;
01073
01074 { IDT idt;
01075 idt = idtx->idt + idtx->nidt;
01076 idt->h = headerLink(h);
01077 idt->key = av[i];
01078 av[i] = NULL;
01079 idt->instance = 0;
01080 idt->val.u32 = *tidp;
01081 }
01082 idtx->nidt++;
01083 }
01084
01085 bottom:
01086 h = headerFree(h);
01087 }
01088
01089 for (i = 0; i < ac; i++)
01090 av[i] = _free(av[i]);
01091 av = _free(av); ac = 0;
01092
01093 return IDTXsort(idtx);
01094 }
01095
01097 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
01098 {
01099 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
01100 unsigned thistid = 0xffffffff;
01101 unsigned prevtid;
01102 time_t tid;
01103 IDTX itids = NULL;
01104 IDTX rtids = NULL;
01105 IDT rp;
01106 int nrids = 0;
01107 IDT ip;
01108 int niids = 0;
01109 int rc = 0;
01110 int vsflags, ovsflags;
01111 int numAdded;
01112 int numRemoved;
01113 rpmps ps;
01114 int _unsafe_rollbacks = 0;
01115 rpmtransFlags transFlags = ia->transFlags;
01116
01117 if (argv != NULL && *argv != NULL) {
01118 rc = -1;
01119 goto exit;
01120 }
01121
01122 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
01123
01124 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
01125 if (ia->qva_flags & VERIFY_DIGEST)
01126 vsflags |= _RPMVSF_NODIGESTS;
01127 if (ia->qva_flags & VERIFY_SIGNATURE)
01128 vsflags |= _RPMVSF_NOSIGNATURES;
01129 if (ia->qva_flags & VERIFY_HDRCHK)
01130 vsflags |= RPMVSF_NOHDRCHK;
01131 vsflags |= RPMVSF_NEEDPAYLOAD;
01132 ovsflags = rpmtsSetVSFlags(ts, vsflags);
01133
01134 (void) rpmtsSetFlags(ts, transFlags);
01135
01136
01137
01138
01139 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
01140
01141 itids = IDTXload(ts, RPMTAG_INSTALLTID);
01142 if (itids != NULL) {
01143 ip = itids->idt;
01144 niids = itids->nidt;
01145 } else {
01146 ip = NULL;
01147 niids = 0;
01148 }
01149
01150 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
01151 if (globstr == NULL || *globstr == '%') {
01152 globstr = _free(globstr);
01153 rc = -1;
01154 goto exit;
01155 }
01156 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
01157
01158 if (rtids != NULL) {
01159 rp = rtids->idt;
01160 nrids = rtids->nidt;
01161 } else {
01162 rp = NULL;
01163 nrids = 0;
01164 }
01165 globstr = _free(globstr);
01166 }
01167
01168 { int notifyFlags, xx;
01169 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
01170 xx = rpmtsSetNotifyCallback(ts,
01171 rpmShowProgress, (void *) ((long)notifyFlags));
01172 }
01173
01174
01175 do {
01176 prevtid = thistid;
01177 rc = 0;
01178 rpmcliPackagesTotal = 0;
01179 numAdded = 0;
01180 numRemoved = 0;
01181 ia->installInterfaceFlags &= ~ifmask;
01182
01183
01184 thistid = 0;
01185 if (ip != NULL && ip->val.u32 > thistid)
01186 thistid = ip->val.u32;
01187 if (rp != NULL && rp->val.u32 > thistid)
01188 thistid = rp->val.u32;
01189
01190
01191 if (thistid == 0 || thistid < ia->rbtid)
01192 break;
01193
01194
01195 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
01196 break;
01197
01198 rpmtsEmpty(ts);
01199 (void) rpmtsSetFlags(ts, transFlags);
01200
01201
01202 while (rp != NULL && rp->val.u32 == thistid) {
01203
01204 rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
01205 (rp->key ? rp->key : "???"));
01206
01207
01208 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
01209 0, ia->relocations);
01210
01211 if (rc != 0)
01212 goto exit;
01213
01214 numAdded++;
01215 rpmcliPackagesTotal++;
01216 if (!(ia->installInterfaceFlags & ifmask))
01217 ia->installInterfaceFlags |= INSTALL_UPGRADE;
01218
01219 #ifdef NOTYET
01220 rp->h = headerFree(rp->h);
01221 #endif
01222 nrids--;
01223 if (nrids > 0)
01224 rp++;
01225 else
01226 rp = NULL;
01227 }
01228
01229
01230 while (ip != NULL && ip->val.u32 == thistid) {
01231
01232 rpmMessage(RPMMESS_DEBUG,
01233 "\t--- erase h#%u\n", ip->instance);
01234
01235 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
01236 if (rc != 0)
01237 goto exit;
01238
01239 numRemoved++;
01240
01241 if (_unsafe_rollbacks)
01242 rpmcliPackagesTotal++;
01243
01244 if (!(ia->installInterfaceFlags & ifmask)) {
01245 ia->installInterfaceFlags |= INSTALL_ERASE;
01246 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
01247 }
01248
01249 #ifdef NOTYET
01250 ip->instance = 0;
01251 #endif
01252 niids--;
01253 if (niids > 0)
01254 ip++;
01255 else
01256 ip = NULL;
01257 }
01258
01259
01260 if (rpmcliPackagesTotal <= 0)
01261 break;
01262
01263 tid = (time_t)thistid;
01264 rpmMessage(RPMMESS_NORMAL,
01265 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
01266 numAdded, numRemoved, ctime(&tid), tid);
01267
01268 rc = rpmtsCheck(ts);
01269 ps = rpmtsProblems(ts);
01270 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
01271 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
01272 rpmpsPrint(NULL, ps);
01273 ps = rpmpsFree(ps);
01274 goto exit;
01275 }
01276 ps = rpmpsFree(ps);
01277
01278 rc = rpmtsOrder(ts);
01279 if (rc != 0)
01280 goto exit;
01281
01282
01283 rpmtsClean(ts);
01284
01285 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
01286 ps = rpmtsProblems(ts);
01287 if (rc > 0 && rpmpsNumProblems(ps) > 0)
01288 rpmpsPrint(stderr, ps);
01289 ps = rpmpsFree(ps);
01290 if (rc)
01291 goto exit;
01292
01293
01294 if (rtids && !rpmIsDebug()) {
01295 int i;
01296 rpmMessage(RPMMESS_NORMAL, _("Cleaning up repackaged packages:\n"));
01297 if (rtids->idt)
01298 for (i = 0; i < rtids->nidt; i++) {
01299 IDT rrp = rtids->idt + i;
01300 if (rrp->val.u32 != thistid)
01301 continue;
01302 if (rrp->key) {
01303 rpmMessage(RPMMESS_NORMAL, _("\tRemoving %s:\n"), rrp->key);
01304 (void) unlink(rrp->key);
01305 }
01306 }
01307 }
01308
01309
01310 } while (1);
01311
01312 exit:
01313 rtids = IDTXfree(rtids);
01314 itids = IDTXfree(itids);
01315
01316 rpmtsEmpty(ts);
01317 (void) rpmtsSetFlags(ts, transFlags);
01318
01319 return rc;
01320 }