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