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) && rpmtsSELinuxEnabled(ts)) {
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 rpmprobFilterFlags probFilter;
00760
00761 if (argv == NULL) return 0;
00762
00763 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
00764 if (ia->qva_flags & VERIFY_DIGEST)
00765 vsflags |= _RPMVSF_NODIGESTS;
00766 if (ia->qva_flags & VERIFY_SIGNATURE)
00767 vsflags |= _RPMVSF_NOSIGNATURES;
00768 if (ia->qva_flags & VERIFY_HDRCHK)
00769 vsflags |= RPMVSF_NOHDRCHK;
00770 ovsflags = rpmtsSetVSFlags(ts, vsflags);
00771
00772 if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
00773 ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
00774
00775 (void) rpmtsSetFlags(ts, ia->transFlags);
00776
00777 probFilter = ia->probFilter;
00778
00779 #ifdef NOTYET
00780 { int notifyFlags;
00781 notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
00782 xx = rpmtsSetNotifyCallback(ts,
00783 rpmShowProgress, (void *) ((long)notifyFlags);
00784 }
00785 #endif
00786
00787 ts->goal = TSM_ERASE;
00788
00789 for (arg = argv; *arg; arg++) {
00790 rpmdbMatchIterator mi;
00791
00792
00793 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
00794 if (mi == NULL) {
00795 rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
00796 numFailed++;
00797 } else {
00798 Header h;
00799 count = 0;
00800 while ((h = rpmdbNextIterator(mi)) != NULL) {
00801 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
00802
00803 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
00804 rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
00805 *arg);
00806 numFailed++;
00807 break;
00808 }
00809 if (recOffset) {
00810 (void) rpmtsAddEraseElement(ts, h, recOffset);
00811 numPackages++;
00812 }
00813 }
00814 }
00815 mi = rpmdbFreeIterator(mi);
00816 }
00817
00818 if (numFailed) goto exit;
00819
00820 if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
00821
00822 if (rpmtsCheck(ts)) {
00823 numFailed = numPackages;
00824 stopUninstall = 1;
00825 }
00826
00827 ps = rpmtsProblems(ts);
00828 if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
00829 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
00830 rpmpsPrint(NULL, ps);
00831 numFailed += numPackages;
00832 stopUninstall = 1;
00833 }
00834 ps = rpmpsFree(ps);
00835 }
00836
00837 #ifdef NOTYET
00838 if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
00839 if (rpmtsOrder(ts)) {
00840 numFailed += numPackages;
00841 stopUninstall = 1;
00842 }
00843 }
00844 #endif
00845
00846 if (!stopUninstall) {
00847 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
00848
00849
00850 rpmtsClean(ts);
00851
00852 numPackages = rpmtsRun(ts, NULL, probFilter);
00853 ps = rpmtsProblems(ts);
00854 if (rpmpsNumProblems(ps) > 0)
00855 rpmpsPrint(NULL, ps);
00856 numFailed += numPackages;
00857 stopUninstall = 1;
00858 ps = rpmpsFree(ps);
00859 }
00860
00861 exit:
00862 rpmtsEmpty(ts);
00863
00864 return numFailed;
00865 }
00866
00867 int rpmInstallSource(rpmts ts, const char * arg,
00868 const char ** specFilePtr, const char ** cookie)
00869 {
00870 FD_t fd;
00871 int rc;
00872
00873
00874 fd = Fopen(arg, "r.ufdio");
00875 if (fd == NULL || Ferror(fd)) {
00876 rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
00877 if (fd != NULL) (void) Fclose(fd);
00878 return 1;
00879 }
00880
00881 if (rpmIsVerbose())
00882 fprintf(stdout, _("Installing %s\n"), arg);
00883
00884 {
00885 rpmVSFlags ovsflags =
00886 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
00887 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
00888 rc = (rpmrc == RPMRC_OK ? 0 : 1);
00889 ovsflags = rpmtsSetVSFlags(ts, ovsflags);
00890 }
00891 if (rc != 0) {
00892 rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
00893
00894 if (specFilePtr && *specFilePtr)
00895 *specFilePtr = _free(*specFilePtr);
00896 if (cookie && *cookie)
00897 *cookie = _free(*cookie);
00898
00899 }
00900
00901 (void) Fclose(fd);
00902
00903 return rc;
00904 }
00905
00906
00907 static int reverse = -1;
00908
00911 static int IDTintcmp(const void * a, const void * b)
00912
00913 {
00914
00915 return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
00916
00917 }
00918
00919 IDTX IDTXfree(IDTX idtx)
00920 {
00921 if (idtx) {
00922 int i;
00923 if (idtx->idt)
00924 for (i = 0; i < idtx->nidt; i++) {
00925 IDT idt = idtx->idt + i;
00926 idt->h = headerFree(idt->h);
00927 idt->key = _free(idt->key);
00928 }
00929 idtx->idt = _free(idtx->idt);
00930 idtx = _free(idtx);
00931 }
00932 return NULL;
00933 }
00934
00935 IDTX IDTXnew(void)
00936 {
00937 IDTX idtx = xcalloc(1, sizeof(*idtx));
00938 idtx->delta = 10;
00939 idtx->size = sizeof(*((IDT)0));
00940 return idtx;
00941 }
00942
00943 IDTX IDTXgrow(IDTX idtx, int need)
00944 {
00945 if (need < 0) return NULL;
00946 if (idtx == NULL)
00947 idtx = IDTXnew();
00948 if (need == 0) return idtx;
00949
00950 if ((idtx->nidt + need) > idtx->alloced) {
00951 while (need > 0) {
00952 idtx->alloced += idtx->delta;
00953 need -= idtx->delta;
00954 }
00955 idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
00956 }
00957 return idtx;
00958 }
00959
00960 IDTX IDTXsort(IDTX idtx)
00961 {
00962 if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
00963 qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
00964 return idtx;
00965 }
00966
00967 IDTX IDTXload(rpmts ts, rpmTag tag)
00968 {
00969 IDTX idtx = NULL;
00970 rpmdbMatchIterator mi;
00971 HGE_t hge = (HGE_t) headerGetEntry;
00972 Header h;
00973
00974
00975 mi = rpmtsInitIterator(ts, tag, NULL, 0);
00976 #ifdef NOTYET
00977 (void) rpmdbSetIteratorRE(mi, RPMTAG_NAME, RPMMIRE_DEFAULT, '!gpg-pubkey');
00978 #endif
00979 while ((h = rpmdbNextIterator(mi)) != NULL) {
00980 rpmTagType type = RPM_NULL_TYPE;
00981 int_32 count = 0;
00982 int_32 * tidp;
00983
00984 tidp = NULL;
00985 if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
00986 continue;
00987
00988 if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
00989 continue;
00990
00991 idtx = IDTXgrow(idtx, 1);
00992 if (idtx == NULL)
00993 continue;
00994 if (idtx->idt == NULL)
00995 continue;
00996
00997 { IDT idt;
00998
00999 idt = idtx->idt + idtx->nidt;
01000
01001 idt->h = headerLink(h);
01002 idt->key = NULL;
01003 idt->instance = rpmdbGetIteratorOffset(mi);
01004 idt->val.u32 = *tidp;
01005 }
01006 idtx->nidt++;
01007 }
01008 mi = rpmdbFreeIterator(mi);
01009
01010
01011 return IDTXsort(idtx);
01012 }
01013
01014 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
01015 {
01016 IDTX idtx = NULL;
01017 HGE_t hge = (HGE_t) headerGetEntry;
01018 Header h;
01019 int_32 * tidp;
01020 FD_t fd;
01021 const char ** av = NULL;
01022 int ac = 0;
01023 rpmRC rpmrc;
01024 int xx;
01025 int i;
01026
01027 av = NULL; ac = 0;
01028 xx = rpmGlob(globstr, &ac, &av);
01029
01030 if (xx == 0)
01031 for (i = 0; i < ac; i++) {
01032 rpmTagType type;
01033 int_32 count;
01034 int isSource;
01035
01036 fd = Fopen(av[i], "r.ufdio");
01037 if (fd == NULL || Ferror(fd)) {
01038 rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
01039 Fstrerror(fd));
01040 if (fd != NULL) (void) Fclose(fd);
01041 continue;
01042 }
01043
01044 rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
01045 (void) Fclose(fd);
01046 switch (rpmrc) {
01047 default:
01048 goto bottom;
01049 break;
01050 case RPMRC_NOTTRUSTED:
01051 case RPMRC_NOKEY:
01052 case RPMRC_OK:
01053 isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
01054 if (isSource)
01055 goto bottom;
01056 break;
01057 }
01058
01059 tidp = NULL;
01060
01061 if (hge(h, tag, &type, (void **) &tidp, &count) && tidp != NULL) {
01062
01063 idtx = IDTXgrow(idtx, 1);
01064 if (idtx == NULL || idtx->idt == NULL)
01065 goto bottom;
01066
01067 { IDT idt;
01068 idt = idtx->idt + idtx->nidt;
01069 idt->h = headerLink(h);
01070 idt->key = av[i];
01071 av[i] = NULL;
01072 idt->instance = 0;
01073 idt->val.u32 = *tidp;
01074 }
01075 idtx->nidt++;
01076 }
01077
01078 bottom:
01079 h = headerFree(h);
01080 }
01081
01082 for (i = 0; i < ac; i++)
01083 av[i] = _free(av[i]);
01084 av = _free(av); ac = 0;
01085
01086 return IDTXsort(idtx);
01087 }
01088
01090 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
01091 {
01092 int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
01093 unsigned thistid = 0xffffffff;
01094 unsigned prevtid;
01095 time_t tid;
01096 IDTX itids = NULL;
01097 IDTX rtids = NULL;
01098 IDT rp;
01099 int nrids = 0;
01100 IDT ip;
01101 int niids = 0;
01102 int rc = 0;
01103 int vsflags, ovsflags;
01104 int numAdded;
01105 int numRemoved;
01106 rpmps ps;
01107 int _unsafe_rollbacks = 0;
01108 rpmtransFlags transFlags = ia->transFlags;
01109
01110 if (argv != NULL && *argv != NULL) {
01111 rc = -1;
01112 goto exit;
01113 }
01114
01115 _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
01116
01117 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
01118 if (ia->qva_flags & VERIFY_DIGEST)
01119 vsflags |= _RPMVSF_NODIGESTS;
01120 if (ia->qva_flags & VERIFY_SIGNATURE)
01121 vsflags |= _RPMVSF_NOSIGNATURES;
01122 if (ia->qva_flags & VERIFY_HDRCHK)
01123 vsflags |= RPMVSF_NOHDRCHK;
01124 vsflags |= RPMVSF_NEEDPAYLOAD;
01125 ovsflags = rpmtsSetVSFlags(ts, vsflags);
01126
01127 (void) rpmtsSetFlags(ts, transFlags);
01128
01129 itids = IDTXload(ts, RPMTAG_INSTALLTID);
01130 if (itids != NULL) {
01131 ip = itids->idt;
01132 niids = itids->nidt;
01133 } else {
01134 ip = NULL;
01135 niids = 0;
01136 }
01137
01138 { const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
01139 if (globstr == NULL || *globstr == '%') {
01140 globstr = _free(globstr);
01141 rc = -1;
01142 goto exit;
01143 }
01144 rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
01145
01146 if (rtids != NULL) {
01147 rp = rtids->idt;
01148 nrids = rtids->nidt;
01149 } else {
01150 rp = NULL;
01151 nrids = 0;
01152 }
01153 globstr = _free(globstr);
01154 }
01155
01156 { int notifyFlags, xx;
01157 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
01158 xx = rpmtsSetNotifyCallback(ts,
01159 rpmShowProgress, (void *) ((long)notifyFlags));
01160 }
01161
01162
01163 do {
01164 prevtid = thistid;
01165 rc = 0;
01166 rpmcliPackagesTotal = 0;
01167 numAdded = 0;
01168 numRemoved = 0;
01169 ia->installInterfaceFlags &= ~ifmask;
01170
01171
01172 thistid = 0;
01173 if (ip != NULL && ip->val.u32 > thistid)
01174 thistid = ip->val.u32;
01175 if (rp != NULL && rp->val.u32 > thistid)
01176 thistid = rp->val.u32;
01177
01178
01179 if (thistid == 0 || thistid < ia->rbtid)
01180 break;
01181
01182
01183 if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
01184 break;
01185
01186 rpmtsEmpty(ts);
01187 (void) rpmtsSetFlags(ts, transFlags);
01188
01189
01190 while (rp != NULL && rp->val.u32 == thistid) {
01191
01192 rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
01193 (rp->key ? rp->key : "???"));
01194
01195
01196 rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
01197 0, ia->relocations);
01198
01199 if (rc != 0)
01200 goto exit;
01201
01202 numAdded++;
01203 rpmcliPackagesTotal++;
01204 if (!(ia->installInterfaceFlags & ifmask))
01205 ia->installInterfaceFlags |= INSTALL_UPGRADE;
01206
01207 #ifdef NOTYET
01208 rp->h = headerFree(rp->h);
01209 #endif
01210 nrids--;
01211 if (nrids > 0)
01212 rp++;
01213 else
01214 rp = NULL;
01215 }
01216
01217
01218 while (ip != NULL && ip->val.u32 == thistid) {
01219
01220 rpmMessage(RPMMESS_DEBUG,
01221 "\t--- erase h#%u\n", ip->instance);
01222
01223 rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
01224 if (rc != 0)
01225 goto exit;
01226
01227 numRemoved++;
01228
01229 if (_unsafe_rollbacks)
01230 rpmcliPackagesTotal++;
01231
01232 if (!(ia->installInterfaceFlags & ifmask)) {
01233 ia->installInterfaceFlags |= INSTALL_ERASE;
01234 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
01235 }
01236
01237 #ifdef NOTYET
01238 ip->instance = 0;
01239 #endif
01240 niids--;
01241 if (niids > 0)
01242 ip++;
01243 else
01244 ip = NULL;
01245 }
01246
01247
01248 if (rpmcliPackagesTotal <= 0)
01249 break;
01250
01251 tid = (time_t)thistid;
01252 rpmMessage(RPMMESS_NORMAL,
01253 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
01254 numAdded, numRemoved, ctime(&tid), tid);
01255
01256 rc = rpmtsCheck(ts);
01257 ps = rpmtsProblems(ts);
01258 if (rc != 0 && rpmpsNumProblems(ps) > 0) {
01259 rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
01260 rpmpsPrint(NULL, ps);
01261 ps = rpmpsFree(ps);
01262 goto exit;
01263 }
01264 ps = rpmpsFree(ps);
01265
01266 rc = rpmtsOrder(ts);
01267 if (rc != 0)
01268 goto exit;
01269
01270
01271 rpmtsClean(ts);
01272
01273 rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
01274 ps = rpmtsProblems(ts);
01275 if (rc > 0 && rpmpsNumProblems(ps) > 0)
01276 rpmpsPrint(stderr, ps);
01277 ps = rpmpsFree(ps);
01278 if (rc)
01279 goto exit;
01280
01281
01282 if (rtids && !rpmIsDebug()) {
01283 int i;
01284 if (rtids->idt)
01285 for (i = 0; i < rtids->nidt; i++) {
01286 IDT rrp = rtids->idt + i;
01287 if (rrp->val.u32 != thistid)
01288 continue;
01289 if (rrp->key)
01290 (void) unlink(rrp->key);
01291 }
01292 }
01293
01294
01295 } while (1);
01296
01297 exit:
01298 rtids = IDTXfree(rtids);
01299 itids = IDTXfree(itids);
01300
01301 rpmtsEmpty(ts);
01302 (void) rpmtsSetFlags(ts, transFlags);
01303
01304 return rc;
01305 }