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