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