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