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 = NULL;
00554 const char * arch = NULL;
00555 Header oldH = NULL;
00556
00557 headerGetEntry(eiu->h, RPMTAG_ARCH, NULL, (void **) &arch, NULL);
00558
00559 xx = headerNVR(eiu->h, &name, NULL, NULL);
00560 mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
00561 if (rpmtsColor(ts) && arch)
00562 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
00563 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
00564
00565 if (rpmVersionCompare(oldH, eiu->h) < 0)
00566 break;
00567 }
00568
00569 mi = rpmdbFreeIterator(mi);
00570 if (oldH == NULL) {
00571 eiu->h = headerFree(eiu->h);
00572 continue;
00573 }
00574
00575 }
00576
00577
00578 rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
00579 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
00580 relocations);
00581
00582
00583
00584 eiu->h = headerFree(eiu->h);
00585 if (eiu->relocations)
00586 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
00587
00588 switch(rc) {
00589 case 0:
00590 rpmMessage(RPMMESS_DEBUG, _("\tadded binary package [%d]\n"),
00591 eiu->numRPMS);
00592 break;
00593 case 1:
00594 rpmMessage(RPMMESS_ERROR,
00595 _("error reading from file %s\n"), *eiu->fnp);
00596 eiu->numFailed++;
00597 goto exit;
00598 break;
00599 case 2:
00600 rpmMessage(RPMMESS_ERROR,
00601 _("file %s requires a newer version of RPM\n"),
00602 *eiu->fnp);
00603 eiu->numFailed++;
00604 goto exit;
00605 break;
00606 default:
00607 eiu->numFailed++;
00608 goto exit;
00609 break;
00610 }
00611
00612 eiu->numRPMS++;
00613 continue;
00614
00615 maybe_manifest:
00616
00617 eiu->fd = Fopen(*eiu->fnp, "r.fpio");
00618 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00619 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
00620 Fstrerror(eiu->fd));
00621 if (eiu->fd != NULL) {
00622 xx = Fclose(eiu->fd);
00623 eiu->fd = NULL;
00624 }
00625 eiu->numFailed++; *eiu->fnp = NULL;
00626 break;
00627 }
00628
00629
00630
00631 rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
00632
00633 if (rc != RPMRC_OK)
00634 rpmError(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
00635 *eiu->fnp, Fstrerror(eiu->fd));
00636 xx = Fclose(eiu->fd);
00637 eiu->fd = NULL;
00638
00639
00640 if (rc == RPMRC_OK) {
00641 eiu->prevx++;
00642 goto restart;
00643 }
00644
00645 eiu->numFailed++; *eiu->fnp = NULL;
00646 break;
00647 }
00648
00649 rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
00650 eiu->numSRPMS, eiu->numRPMS);
00651
00652 if (eiu->numFailed) goto exit;
00653
00654 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
00655
00656 if (rpmtsCheck(ts)) {
00657 eiu->numFailed = eiu->numPkgs;
00658 stopInstall = 1;
00659 }
00660
00661 ps = rpmtsProblems(ts);
00662 if (!stopInstall && rpmpsNumProblems(ps) > 0) {
00663 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00664 rpmpsPrint(NULL, ps);
00665 eiu->numFailed = eiu->numPkgs;
00666 stopInstall = 1;
00667
00668
00669 if (ts->suggests != NULL && ts->nsuggests > 0) {
00670 rpmMessage(RPMMESS_NORMAL, _(" Suggested resolutions:\n"));
00671 for (i = 0; i < ts->nsuggests; i++) {
00672 const char * str = ts->suggests[i];
00673
00674 if (str == NULL)
00675 break;
00676
00677 rpmMessage(RPMMESS_NORMAL, "\t%s\n", str);
00678
00679 ts->suggests[i] = NULL;
00680 str = _free(str);
00681 }
00682 ts->suggests = _free(ts->suggests);
00683 }
00684
00685 }
00686 ps = rpmpsFree(ps);
00687 }
00688
00689 if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00690 if (rpmtsOrder(ts)) {
00691 eiu->numFailed = eiu->numPkgs;
00692 stopInstall = 1;
00693 }
00694 }
00695
00696 if (eiu->numRPMS && !stopInstall) {
00697
00698 rpmcliPackagesTotal += eiu->numSRPMS;
00699
00700 rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
00701
00702
00703 rpmtsClean(ts);
00704
00705 rc = rpmtsRun(ts, NULL, probFilter);
00706 ps = rpmtsProblems(ts);
00707
00708 if (rc < 0) {
00709 eiu->numFailed += eiu->numRPMS;
00710 } else if (rc > 0) {
00711 eiu->numFailed += rc;
00712 if (rpmpsNumProblems(ps) > 0)
00713 rpmpsPrint(stderr, ps);
00714 }
00715 ps = rpmpsFree(ps);
00716 }
00717
00718 if (eiu->numSRPMS && !stopInstall) {
00719 if (eiu->sourceURL != NULL)
00720 for (i = 0; i < eiu->numSRPMS; i++) {
00721 rpmdbCheckSignals();
00722 if (eiu->sourceURL[i] == NULL) continue;
00723 eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
00724 if (eiu->fd == NULL || Ferror(eiu->fd)) {
00725 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
00726 eiu->sourceURL[i], Fstrerror(eiu->fd));
00727 if (eiu->fd != NULL) {
00728 xx = Fclose(eiu->fd);
00729 eiu->fd = NULL;
00730 }
00731 continue;
00732 }
00733
00734 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
00735 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
00736 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
00737 }
00738
00739 xx = Fclose(eiu->fd);
00740 eiu->fd = NULL;
00741 }
00742 }
00743
00744 exit:
00745 if (eiu->pkgURL != NULL)
00746 for (i = 0; i < eiu->numPkgs; i++) {
00747 if (eiu->pkgURL[i] == NULL) continue;
00748 if (eiu->pkgState[i] == 1)
00749 (void) Unlink(eiu->pkgURL[i]);
00750 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
00751 }
00752 eiu->pkgState = _free(eiu->pkgState);
00753 eiu->pkgURL = _free(eiu->pkgURL);
00754 eiu->argv = _free(eiu->argv);
00755
00756 if (secontexts) {
00757 matchpathcon_fini();
00758 }
00759
00760 rpmtsEmpty(ts);
00761
00762 return eiu->numFailed;
00763 }
00764
00765
00766 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
00767 const char ** argv)
00768 {
00769 int count;
00770 const char ** arg;
00771 int numFailed = 0;
00772 int stopUninstall = 0;
00773 int numPackages = 0;
00774 rpmVSFlags vsflags, ovsflags;
00775 rpmps ps;
00776
00777 if (argv == NULL) return 0;
00778
00779 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00780 if (ia->qva_flags & VERIFY_DIGEST)
00781 vsflags |= _RPMVSF_NODIGESTS;
00782 if (ia->qva_flags & VERIFY_SIGNATURE)
00783 vsflags |= _RPMVSF_NOSIGNATURES;
00784 if (ia->qva_flags & VERIFY_HDRCHK)
00785 vsflags |= RPMVSF_NOHDRCHK;
00786 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00787
00788 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00789 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00790
00791 (void) rpmtsSetFlags(ts, ia->transFlags);
00792
00793 #ifdef NOTYET
00794 { int notifyFlags;
00795 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00796 xx = rpmtsSetNotifyCallback(ts,
00797 rpmShowProgress, (void *) ((long)notifyFlags)
00798 }
00799 #endif
00800
00801 ts->goal = TSM_ERASE;
00802
00803 for (arg = argv; *arg; arg++) {
00804 rpmdbMatchIterator mi;
00805
00806
00807 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
00808 if (mi == NULL) {
00809 rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
00810 numFailed++;
00811 } else {
00812 Header h;
00813 count = 0;
00814 while ((h = rpmdbNextIterator(mi)) != NULL) {
00815 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
00816
00817 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
00818 rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
00819 *arg);
00820 numFailed++;
00821 break;
00822 }
00823 if (recOffset) {
00824 (void) rpmtsAddEraseElement(ts, h, recOffset);
00825 numPackages++;
00826 }
00827 }
00828 }
00829 mi = rpmdbFreeIterator(mi);
00830 }
00831
00832 if (numFailed) goto exit;
00833
00834 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
00835
00836 if (rpmtsCheck(ts)) {
00837 numFailed = numPackages;
00838 stopUninstall = 1;
00839 }
00840
00841 ps = rpmtsProblems(ts);
00842 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
00843 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00844 rpmpsPrint(NULL, ps);
00845 numFailed += numPackages;
00846 stopUninstall = 1;
00847 }
00848 ps = rpmpsFree(ps);
00849 }
00850
00851 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00852 if (rpmtsOrder(ts)) {
00853 numFailed += numPackages;
00854 stopUninstall = 1;
00855 }
00856 }
00857
00858 if (numPackages && !stopUninstall) {
00859 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
00860
00861
00862 rpmtsClean(ts);
00863
00864 numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
00865 ps = rpmtsProblems(ts);
00866 if (rpmpsNumProblems(ps) > 0)
00867 rpmpsPrint(NULL, ps);
00868 numFailed += numPackages;
00869 stopUninstall = 1;
00870 ps = rpmpsFree(ps);
00871 }
00872
00873 exit:
00874 rpmtsEmpty(ts);
00875
00876 return numFailed;
00877 }
00878
00879 int rpmInstallSource(rpmts ts, const char * arg,
00880 const char ** specFilePtr, const char ** cookie)
00881 {
00882 FD_t fd;
00883 int rc;
00884
00885
00886 fd = Fopen(arg, "r.ufdio");
00887 if (fd == NULL || Ferror(fd)) {
00888 rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
00889 if (fd != NULL) (void) Fclose(fd);
00890 return 1;
00891 }
00892
00893 if (rpmIsVerbose())
00894 fprintf(stdout, _("Installing %s\n"), arg);
00895
00896 {
00897 rpmVSFlags ovsflags =
00898 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
00899 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
00900 rc = (rpmrc == RPMRC_OK ? 0 : 1);
00901 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
00902 }
00903 if (rc != 0) {
00904 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
00905
00906 if (specFilePtr && *specFilePtr)
00907 *specFilePtr = _free(*specFilePtr);
00908 if (cookie && *cookie)
00909 *cookie = _free(*cookie);
00910
00911 }
00912
00913 (void) Fclose(fd);
00914
00915 return rc;
00916 }
00917
00918
00919 static int reverse = -1;
00920
00923 static int IDTintcmp(const void * a, const void * b)
00924
00925 {
00926
00927 return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
00928
00929 }
00930
00931 IDTX IDTXfree(IDTX idtx)
00932 {
00933 if (idtx) {
00934 int i;
00935 if (idtx->idt)
00936 for (i = 0; i < idtx->nidt; i++) {
00937 IDT idt = idtx->idt + i;
00938 idt->h = headerFree(idt->h);
00939 idt->key = _free(idt->key);
00940 }
00941 idtx->idt = _free(idtx->idt);
00942 idtx = _free(idtx);
00943 }
00944 return NULL;
00945 }
00946
00947 IDTX IDTXnew(void)
00948 {
00949 IDTX idtx = xcalloc(1, sizeof(*idtx));
00950 idtx->delta = 10;
00951 idtx->size = sizeof(*((IDT)0));
00952 return idtx;
00953 }
00954
00955 IDTX IDTXgrow(IDTX idtx, int need)
00956 {
00957 if (need < 0) return NULL;
00958 if (idtx == NULL)
00959 idtx = IDTXnew();
00960 if (need == 0) return idtx;
00961
00962 if ((idtx->nidt + need) > idtx->alloced) {
00963 while (need > 0) {
00964 idtx->alloced += idtx->delta;
00965 need -= idtx->delta;
00966 }
00967 idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
00968 }
00969 return idtx;
00970 }
00971
00972 IDTX IDTXsort(IDTX idtx)
00973 {
00974 if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
00975 qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
00976 return idtx;
00977 }
00978
00979 IDTX IDTXload(rpmts ts, rpmTag tag)
00980 {
00981 IDTX idtx = NULL;
00982 rpmdbMatchIterator mi;
00983 HGE_t hge = (HGE_t) headerGetEntry;
00984 Header h;
00985
00986
00987 mi = rpmtsInitIterator(ts, tag, NULL, 0);
00988 #ifdef NOTYET
00989 (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey');
00990 #endif
00991 while ((h = rpmdbNextIterator(mi)) != NULL) {
00992 rpmTagType type = RPM_NULL_TYPE;
00993 int_32 count = 0;
00994 int_32 * tidp;
00995
00996 tidp = NULL;
00997 if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
00998 continue;
00999
01000 if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
01001 continue;
01002
01003 idtx = IDTXgrow(idtx, 1);
01004 if (idtx == NULL)
01005 continue;
01006 if (idtx->idt == NULL)
01007 continue;
01008
01009 { IDT idt;
01010
01011 idt = idtx->idt + idtx->nidt;
01012
01013 idt->h = headerLink(h);
01014 idt->key = NULL;
01015 idt->instance = rpmdbGetIteratorOffset(mi);
01016 idt->val.u32 = *tidp;
01017 }
01018 idtx->nidt++;
01019 }
01020 mi = rpmdbFreeIterator(mi);
01021
01022
01023 return IDTXsort(idtx);
01024 }
01025
01026 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
01027 {
01028 IDTX idtx = NULL;
01029 HGE_t hge = (HGE_t) headerGetEntry;
01030 Header h;
01031 int_32 * tidp;
01032 FD_t fd;
01033 const char ** av = NULL;
01034 int ac = 0;
01035 rpmRC rpmrc;
01036 int xx;
01037 int i;
01038
01039 av = NULL; ac = 0;
01040 xx = rpmGlob(globstr, &ac, &av);
01041
01042 if (xx == 0)
01043 for (i = 0; i < ac; i++) {
01044 rpmTagType type;
01045 int_32 count;
01046 int isSource;
01047
01048 fd = Fopen(av[i], "r.ufdio");
01049 if (fd == NULL || Ferror(fd)) {
01050 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
01051 Fstrerror(fd));
01052 if (fd != NULL) (void) Fclose(fd);
01053 continue;
01054 }
01055
01056 rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
01057 (void) Fclose(fd);
01058 switch (rpmrc) {
01059 default:
01060 goto bottom;
01061 break;
01062 case RPMRC_NOTTRUSTED:
01063 case RPMRC_NOKEY:
01064 case RPMRC_OK:
01065 isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
01066 if (isSource)
01067 goto bottom;
01068 break;
01069 }
01070
01071 tidp = NULL;
01072
01073 if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) {
01074
01075 idtx = IDTXgrow(idtx, 1);
01076 if (idtx == NULL || idtx->idt == NULL)
01077 goto bottom;
01078
01079 { IDT idt;
01080 idt = idtx->idt + idtx->nidt;
01081 idt->h = headerLink(h);
01082 idt->key = av[i];
01083 av[i] = NULL;
01084 idt->instance = 0;
01085 idt->val.u32 = *tidp;
01086 }
01087 idtx->nidt++;
01088 }
01089
01090 bottom:
01091 h = headerFree(h);
01092 }
01093
01094 for (i = 0; i < ac; i++)
01095 av[i] = _free(av[i]);
01096 av = _free(av); ac = 0;
01097
01098 return IDTXsort(idtx);
01099 }
01100
01102 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
01103 {
01104 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
01105 unsigned thistid = 0xffffffff;
01106 unsigned prevtid;
01107 time_t tid;
01108 IDTX itids = NULL;
01109 IDTX rtids = NULL;
01110 IDT rp;
01111 int nrids = 0;
01112 IDT ip;
01113 int niids = 0;
01114 int rc = 0;
01115 int vsflags, ovsflags;
01116 int numAdded;
01117 int numRemoved;
01118 rpmps ps;
01119 int _unsafe_rollbacks = 0;
01120 rpmtransFlags transFlags = ia->transFlags;
01121
01122 if (argv != NULL && *argv != NULL) {
01123 rc = -1;
01124 goto exit;
01125 }
01126
01127 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
01128
01129 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
01130 if (ia->qva_flags & VERIFY_DIGEST)
01131 vsflags |= _RPMVSF_NODIGESTS;
01132 if (ia->qva_flags & VERIFY_SIGNATURE)
01133 vsflags |= _RPMVSF_NOSIGNATURES;
01134 if (ia->qva_flags & VERIFY_HDRCHK)
01135 vsflags |= RPMVSF_NOHDRCHK;
01136 vsflags |= RPMVSF_NEEDPAYLOAD;
01137 ovsflags = rpmtsSetVSFlags(ts, vsflags);
01138
01139 (void) rpmtsSetFlags(ts, transFlags);
01140
01141
01142
01143
01144 rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
01145
01146 itids = IDTXload(ts, RPMTAG_INSTALLTID);
01147 if (itids != NULL) {
01148 ip = itids->idt;
01149 niids = itids->nidt;
01150 } else {
01151 ip = NULL;
01152 niids = 0;
01153 }
01154
01155 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
01156 if (globstr == NULL || *globstr == '%') {
01157 globstr = _free(globstr);
01158 rc = -1;
01159 goto exit;
01160 }
01161 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
01162
01163 if (rtids != NULL) {
01164 rp = rtids->idt;
01165 nrids = rtids->nidt;
01166 } else {
01167 rp = NULL;
01168 nrids = 0;
01169 }
01170 globstr = _free(globstr);
01171 }
01172
01173 { int notifyFlags, xx;
01174 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
01175 xx = rpmtsSetNotifyCallback(ts,
01176 rpmShowProgress, (void *) ((long)notifyFlags));
01177 }
01178
01179
01180 do {
01181 prevtid = thistid;
01182 rc = 0;
01183 rpmcliPackagesTotal = 0;
01184 numAdded = 0;
01185 numRemoved = 0;
01186 ia->installInterfaceFlags &= ~ifmask;
01187
01188
01189 thistid = 0;
01190 if (ip != NULL && ip->val.u32 > thistid)
01191 thistid = ip->val.u32;
01192 if (rp != NULL && rp->val.u32 > thistid)
01193 thistid = rp->val.u32;
01194
01195
01196 if (thistid == 0 || thistid < ia->rbtid)
01197 break;
01198
01199
01200 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
01201 break;
01202
01203 rpmtsEmpty(ts);
01204 (void) rpmtsSetFlags(ts, transFlags);
01205
01206
01207 while (rp != NULL && rp->val.u32 == thistid) {
01208
01209 rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
01210 (rp->key ? rp->key : "???"));
01211
01212
01213 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
01214 0, ia->relocations);
01215
01216 if (rc != 0)
01217 goto exit;
01218
01219 numAdded++;
01220 rpmcliPackagesTotal++;
01221 if (!(ia->installInterfaceFlags & ifmask))
01222 ia->installInterfaceFlags |= INSTALL_UPGRADE;
01223
01224 #ifdef NOTYET
01225 rp->h = headerFree(rp->h);
01226 #endif
01227 nrids--;
01228 if (nrids > 0)
01229 rp++;
01230 else
01231 rp = NULL;
01232 }
01233
01234
01235 while (ip != NULL && ip->val.u32 == thistid) {
01236
01237 rpmMessage(RPMMESS_DEBUG,
01238 "\t--- erase h#%u\n", ip->instance);
01239
01240 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
01241 if (rc != 0)
01242 goto exit;
01243
01244 numRemoved++;
01245
01246 if (_unsafe_rollbacks)
01247 rpmcliPackagesTotal++;
01248
01249 if (!(ia->installInterfaceFlags & ifmask)) {
01250 ia->installInterfaceFlags |= INSTALL_ERASE;
01251 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
01252 }
01253
01254 #ifdef NOTYET
01255 ip->instance = 0;
01256 #endif
01257 niids--;
01258 if (niids > 0)
01259 ip++;
01260 else
01261 ip = NULL;
01262 }
01263
01264
01265 if (rpmcliPackagesTotal <= 0)
01266 break;
01267
01268 tid = (time_t)thistid;
01269 rpmMessage(RPMMESS_NORMAL,
01270 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
01271 numAdded, numRemoved, ctime(&tid), tid);
01272
01273 rc = rpmtsCheck(ts);
01274 ps = rpmtsProblems(ts);
01275 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
01276 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
01277 rpmpsPrint(NULL, ps);
01278 ps = rpmpsFree(ps);
01279 goto exit;
01280 }
01281 ps = rpmpsFree(ps);
01282
01283 rc = rpmtsOrder(ts);
01284 if (rc != 0)
01285 goto exit;
01286
01287
01288 rpmtsClean(ts);
01289
01290 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
01291 ps = rpmtsProblems(ts);
01292 if (rc > 0 && rpmpsNumProblems(ps) > 0)
01293 rpmpsPrint(stderr, ps);
01294 ps = rpmpsFree(ps);
01295 if (rc)
01296 goto exit;
01297
01298
01299 if (rtids && !rpmIsDebug()) {
01300 int i;
01301 rpmMessage(RPMMESS_NORMAL, _("Cleaning up repackaged packages:\n"));
01302 if (rtids->idt)
01303 for (i = 0; i < rtids->nidt; i++) {
01304 IDT rrp = rtids->idt + i;
01305 if (rrp->val.u32 != thistid)
01306 continue;
01307 if (rrp->key) {
01308 rpmMessage(RPMMESS_NORMAL, _("\tRemoving %s:\n"), rrp->key);
01309 (void) unlink(rrp->key);
01310 }
01311 }
01312 }
01313
01314
01315 } while (1);
01316
01317 exit:
01318 rtids = IDTXfree(rtids);
01319 itids = IDTXfree(itids);
01320
01321 rpmtsEmpty(ts);
01322 (void) rpmtsSetFlags(ts, transFlags);
01323
01324 return rc;
01325 }