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