# lib/rpminstall.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #include <rpmcli.h>
    00008
    00009 #include "manifest.h"
    00010 #include "misc.h"       /* XXX for rpmGlob() */
    00011 #include "debug.h"
    00012
    00013 /*@access rpmTransactionSet @*/ /* XXX compared with NULL */
    00014 /*@access rpmProblemSet @*/     /* XXX compared with NULL */
    00015 /*@access Header @*/            /* XXX compared with NULL */
    00016 /*@access rpmdb @*/             /* XXX compared with NULL */
    00017 /*@access FD_t @*/              /* XXX compared with NULL */
    00018 /*@access IDTX @*/
    00019 /*@access IDT @*/
    00020
    00021 /*@unchecked@*/
    00022 static int hashesPrinted = 0;
    00023
    00024 /*@unchecked@*/
    00025 int packagesTotal = 0;
    00026 /*@unchecked@*/
    00027 static int progressTotal = 0;
    00028 /*@unchecked@*/
    00029 static int progressCurrent = 0;
    00030
    00033 static void printHash(const unsigned long amount, const unsigned long total)
    00034         /*@globals hashesPrinted, progressCurrent, fileSystem @*/
    00035         /*@modifies hashesPrinted, progressCurrent, fileSystem @*/
    00036 {
    00037     int hashesNeeded;
    00038     int hashesTotal = 50;
    00039
    00040     if (isatty (STDOUT_FILENO))
    00041         hashesTotal = 44;
    00042
    00043     if (hashesPrinted != hashesTotal) {
    00044         hashesNeeded = hashesTotal * (total ? (((float) amount) / total) : 1);
    00045         while (hashesNeeded > hashesPrinted) {
    00046             if (isatty (STDOUT_FILENO)) {
    00047                 int i;
    00048                 for (i = 0; i < hashesPrinted; i++) (void) putchar ('#');
    00049                 for (; i < hashesTotal; i++) (void) putchar (' ');
    00050                 fprintf(stdout, "(%3d%%)",
    00051                         (int)(100 * (total ? (((float) amount) / total) : 1)));
    00052                 for (i = 0; i < (hashesTotal + 6); i++) (void) putchar ('\b');
    00053             } else
    00054                 fprintf(stdout, "#");
    00055
    00056             hashesPrinted++;
    00057         }
    00058         (void) fflush(stdout);
    00059         hashesPrinted = hashesNeeded;
    00060
    00061         if (hashesPrinted == hashesTotal) {
    00062             int i;
    00063             progressCurrent++;
    00064             if (isatty(STDOUT_FILENO)) {
    00065                 for (i = 1; i < hashesPrinted; i++) (void) putchar ('#');
    00066                 fprintf(stdout, " [%3d%%]", (int)(100 * (progressTotal ?
    00067                         (((float) progressCurrent) / progressTotal) : 1)));
    00068             }
    00069             fprintf(stdout, "\n");
    00070         }
    00071         (void) fflush(stdout);
    00072     }
    00073 }
    00074
    00075 void * rpmShowProgress(/*@null@*/ const void * arg,
    00076                         const rpmCallbackType what,
    00077                         const unsigned long amount,
    00078                         const unsigned long total,
    00079                         /*@null@*/ const void * pkgKey,
    00080                         /*@null@*/ void * data)
    00081         /*@globals hashesPrinted, progressCurrent, progressTotal,
    00082                 fileSystem @*/
    00083         /*@modifies hashesPrinted, progressCurrent, progressTotal,
    00084                 fileSystem @*/
    00085 {
    00086     /*@-castexpose@*/
    00087     Header h = (Header) arg;
    00088     /*@=castexpose@*/
    00089     char * s;
    00090     int flags = (int) ((long)data);
    00091     void * rc = NULL;
    00092     const char * filename = pkgKey;
    00093     static FD_t fd = NULL;
    00094
    00095     switch (what) {
    00096     case RPMCALLBACK_INST_OPEN_FILE:
    00097         if (filename == NULL || filename[0] == '\0')
    00098             return NULL;
    00099         fd = Fopen(filename, "r.ufdio");
    00100         if (fd)
    00101             fd = fdLink(fd, "persist (showProgress)");
    00102         return fd;
    00103         /*@notreached@*/ break;
    00104
    00105     case RPMCALLBACK_INST_CLOSE_FILE:
    00106         fd = fdFree(fd, "persist (showProgress)");
    00107         if (fd) {
    00108             (void) Fclose(fd);
    00109             fd = NULL;
    00110         }
    00111         break;
    00112
    00113     case RPMCALLBACK_INST_START:
    00114         hashesPrinted = 0;
    00115         if (h == NULL || !(flags & INSTALL_LABEL))
    00116             break;
    00117         if (flags & INSTALL_HASH) {
    00118             s = headerSprintf(h, "%{NAME}",
    00119                                 rpmTagTable, rpmHeaderFormats, NULL);
    00120             if (isatty (STDOUT_FILENO))
    00121                 fprintf(stdout, "%4d:%-23.23s", progressCurrent + 1, s);
    00122             else
    00123                 fprintf(stdout, "%-28.28s", s);
    00124             (void) fflush(stdout);
    00125             s = _free(s);
    00126         } else {
    00127             s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
    00128                                   rpmTagTable, rpmHeaderFormats, NULL);
    00129             fprintf(stdout, "%s\n", s);
    00130             (void) fflush(stdout);
    00131             s = _free(s);
    00132         }
    00133         break;
    00134
    00135     case RPMCALLBACK_TRANS_PROGRESS:
    00136     case RPMCALLBACK_INST_PROGRESS:
    00137         if (flags & INSTALL_PERCENT)
    00138             fprintf(stdout, "%%%% %f\n", (double) (total
    00139                                 ? ((((float) amount) / total) * 100)
    00140                                 : 100.0));
    00141         else if (flags & INSTALL_HASH)
    00142             printHash(amount, total);
    00143         (void) fflush(stdout);
    00144         break;
    00145
    00146     case RPMCALLBACK_TRANS_START:
    00147         hashesPrinted = 0;
    00148         progressTotal = 1;
    00149         progressCurrent = 0;
    00150         if (!(flags & INSTALL_LABEL))
    00151             break;
    00152         if (flags & INSTALL_HASH)
    00153             fprintf(stdout, "%-28s", _("Preparing..."));
    00154         else
    00155             fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
    00156         (void) fflush(stdout);
    00157         break;
    00158
    00159     case RPMCALLBACK_TRANS_STOP:
    00160         if (flags & INSTALL_HASH)
    00161             printHash(1, 1);    /* Fixes "preparing..." progress bar */
    00162         progressTotal = packagesTotal;
    00163         progressCurrent = 0;
    00164         break;
    00165
    00166     case RPMCALLBACK_UNINST_PROGRESS:
    00167     case RPMCALLBACK_UNINST_START:
    00168     case RPMCALLBACK_UNINST_STOP:
    00169         /* ignore */
    00170         break;
    00171     }
    00172
    00173     return rc;
    00174 }
    00175
    00176 typedef /*@only@*/ /*@null@*/ const char * str_t;
    00177
    00178 struct rpmEIU {
    00179 /*@only@*/ rpmTransactionSet ts;
    00180 /*@only@*/ /*@null@*/ rpmdb db;
    00181     Header h;
    00182     FD_t fd;
    00183     int numFailed;
    00184     int numPkgs;
    00185 /*@only@*/ str_t * pkgURL;
    00186 /*@dependent@*/ /*@null@*/ str_t * fnp;
    00187 /*@only@*/ char * pkgState;
    00188     int prevx;
    00189     int pkgx;
    00190     int numRPMS;
    00191     int numSRPMS;
    00192 /*@only@*/ /*@null@*/ str_t * sourceURL;
    00193     int isSource;
    00194     int argc;
    00195 /*@only@*/ /*@null@*/ str_t * argv;
    00196 /*@temp@*/ rpmRelocation * relocations;
    00197     rpmRC rpmrc;
    00198 };
    00199
    00201 int rpmInstall(const char * rootdir, const char ** fileArgv,
    00202                 rpmtransFlags transFlags,
    00203                 rpmInstallInterfaceFlags interfaceFlags,
    00204                 rpmprobFilterFlags probFilter,
    00205                 rpmRelocation * relocations)
    00206 {
    00207     struct rpmEIU * eiu = alloca(sizeof(*eiu));
    00208     int notifyFlags = interfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
    00209     /*@only@*/ /*@null@*/ const char * fileURL = NULL;
    00210     int stopInstall = 0;
    00211     const char ** av = NULL;
    00212     int ac = 0;
    00213     int rc;
    00214     int xx;
    00215     int i;
    00216
    00217     if (fileArgv == NULL) return 0;
    00218
    00219     memset(eiu, 0, sizeof(*eiu));
    00220     eiu->numPkgs = 0;
    00221     eiu->prevx = 0;
    00222     eiu->pkgx = 0;
    00223
    00224     if ((eiu->relocations = relocations) != NULL) {
    00225         while (eiu->relocations->oldPath)
    00226             eiu->relocations++;
    00227         if (eiu->relocations->newPath == NULL)
    00228             eiu->relocations = NULL;
    00229     }
    00230
    00231     /* Build fully globbed list of arguments in argv[argc]. */
    00232     /*@-branchstate@*/
    00233     /*@-temptrans@*/
    00234     for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
    00235     /*@=temptrans@*/
    00236         av = _free(av); ac = 0;
    00237         rc = rpmGlob(*eiu->fnp, &ac, &av);
    00238         if (rc || ac == 0) continue;
    00239
    00240         eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
    00241         memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
    00242         eiu->argc += ac;
    00243         eiu->argv[eiu->argc] = NULL;
    00244     }
    00245     /*@=branchstate@*/
    00246     av = _free(av);     ac = 0;
    00247
    00248 restart:
    00249     /* Allocate sufficient storage for next set of args. */
    00250     if (eiu->pkgx >= eiu->numPkgs) {
    00251         eiu->numPkgs = eiu->pkgx + eiu->argc;
    00252         eiu->pkgURL = xrealloc(eiu->pkgURL,
    00253                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
    00254         memset(eiu->pkgURL + eiu->pkgx, 0,
    00255                         ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
    00256         eiu->pkgState = xrealloc(eiu->pkgState,
    00257                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
    00258         memset(eiu->pkgState + eiu->pkgx, 0,
    00259                         ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
    00260     }
    00261
    00262     /* Retrieve next set of args, cache on local storage. */
    00263     for (i = 0; i < eiu->argc; i++) {
    00264         fileURL = _free(fileURL);
    00265         fileURL = eiu->argv[i];
    00266         eiu->argv[i] = NULL;
    00267
    00268         switch (urlIsURL(fileURL)) {
    00269         case URL_IS_FTP:
    00270         case URL_IS_HTTP:
    00271         {   const char *tfn;
    00272
    00273             if (rpmIsVerbose())
    00274                 fprintf(stdout, _("Retrieving %s\n"), fileURL);
    00275
    00276             {   char tfnbuf[64];
    00277                 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
    00278                 (void) mktemp(tfnbuf);
    00279                 tfn = rpmGenPath(rootdir, "%{_tmppath}/", tfnbuf);
    00280             }
    00281
    00282             /* XXX undefined %{name}/%{version}/%{release} here */
    00283             /* XXX %{_tmpdir} does not exist */
    00284             rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
    00285             rc = urlGetFile(fileURL, tfn);
    00286             if (rc < 0) {
    00287                 rpmMessage(RPMMESS_ERROR,
    00288                         _("skipping %s - transfer failed - %s\n"),
    00289                         fileURL, ftpStrerror(rc));
    00290                 eiu->numFailed++;
    00291                 eiu->pkgURL[eiu->pkgx] = NULL;
    00292                 tfn = _free(tfn);
    00293                 /*@switchbreak@*/ break;
    00294             }
    00295             eiu->pkgState[eiu->pkgx] = 1;
    00296             eiu->pkgURL[eiu->pkgx] = tfn;
    00297             eiu->pkgx++;
    00298         }   /*@switchbreak@*/ break;
    00299         case URL_IS_PATH:
    00300         default:
    00301             eiu->pkgURL[eiu->pkgx] = fileURL;
    00302             fileURL = NULL;
    00303             eiu->pkgx++;
    00304             /*@switchbreak@*/ break;
    00305         }
    00306     }
    00307     fileURL = _free(fileURL);
    00308
    00309     if (eiu->numFailed) goto exit;
    00310
    00311     /* Continue processing file arguments, building transaction set. */
    00312     for (eiu->fnp = eiu->pkgURL+eiu->prevx;
    00313          *eiu->fnp != NULL;
    00314          eiu->fnp++, eiu->prevx++)
    00315     {
    00316         const char * fileName;
    00317
    00318         rpmMessage(RPMMESS_DEBUG, "============== %s\n", *eiu->fnp);
    00319         (void) urlPath(*eiu->fnp, &fileName);
    00320
    00321         /* Try to read the header from a package file. */
    00322         eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
    00323         if (eiu->fd == NULL || Ferror(eiu->fd)) {
    00324             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
    00325                         Fstrerror(eiu->fd));
    00326             if (eiu->fd) {
    00327                 xx = Fclose(eiu->fd);
    00328                 eiu->fd = NULL;
    00329             }
    00330             eiu->numFailed++; *eiu->fnp = NULL;
    00331             continue;
    00332         }
    00333
    00334         /*@-mustmod@*/  /* LCL: segfault */
    00335         eiu->rpmrc = rpmReadPackageHeader(eiu->fd, &eiu->h,
    00336                                 &eiu->isSource, NULL, NULL);
    00337         /*@=mustmod@*/
    00338         xx = Fclose(eiu->fd);
    00339         eiu->fd = NULL;
    00340
    00341         if (eiu->rpmrc == RPMRC_FAIL || eiu->rpmrc == RPMRC_SHORTREAD) {
    00342             eiu->numFailed++; *eiu->fnp = NULL;
    00343             continue;
    00344         }
    00345
    00346         if (eiu->isSource &&
    00347                 (eiu->rpmrc == RPMRC_OK || eiu->rpmrc == RPMRC_BADSIZE))
    00348         {
    00349             rpmMessage(RPMMESS_DEBUG, "\tadded source package [%d]\n",
    00350                 eiu->numSRPMS);
    00351             eiu->sourceURL = xrealloc(eiu->sourceURL,
    00352                                 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
    00353             eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
    00354             *eiu->fnp = NULL;
    00355             eiu->numSRPMS++;
    00356             eiu->sourceURL[eiu->numSRPMS] = NULL;
    00357             continue;
    00358         }
    00359
    00360         if (eiu->rpmrc == RPMRC_OK || eiu->rpmrc == RPMRC_BADSIZE) {
    00361             if (eiu->db == NULL) {
    00362                 int mode = (transFlags & RPMTRANS_FLAG_TEST)
    00363                                 ? O_RDONLY : (O_RDWR | O_CREAT);
    00364
    00365                 if (rpmdbOpen(rootdir, &eiu->db, mode, 0644)) {
    00366                     const char *dn;
    00367                     dn = rpmGetPath( (rootdir ? rootdir : ""),
    00368                                         "%{_dbpath}", NULL);
    00369                     rpmMessage(RPMMESS_ERROR,
    00370                                 _("cannot open Packages database in %s\n"), dn);
    00371                     dn = _free(dn);
    00372                     eiu->numFailed++; *eiu->fnp = NULL;
    00373                     break;
    00374                 }
    00375                 /*@-onlytrans@*/
    00376                 eiu->ts = rpmtransCreateSet(eiu->db, rootdir);
    00377                 /*@=onlytrans@*/
    00378             }
    00379
    00380             if (eiu->relocations) {
    00381                 const char ** paths;
    00382                 int pft;
    00383                 int c;
    00384
    00385                 if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
    00386                                        (void **) &paths, &c) && (c == 1)) {
    00387                     eiu->relocations->oldPath = xstrdup(paths[0]);
    00388                     paths = headerFreeData(paths, pft);
    00389                 } else {
    00390                     const char * name;
    00391                     xx = headerNVR(eiu->h, &name, NULL, NULL);
    00392                     rpmMessage(RPMMESS_ERROR,
    00393                                _("package %s is not relocateable\n"), name);
    00394                     eiu->numFailed++;
    00395                     goto exit;
    00396                     /*@notreached@*/
    00397                 }
    00398             }
    00399
    00400             /* On --freshen, verify package is installed and newer */
    00401             if (interfaceFlags & INSTALL_FRESHEN) {
    00402                 rpmdbMatchIterator mi;
    00403                 const char * name;
    00404                 Header oldH;
    00405                 int count;
    00406
    00407                 xx = headerNVR(eiu->h, &name, NULL, NULL);
    00408                 /*@-onlytrans@*/
    00409                 mi = rpmdbInitIterator(eiu->db, RPMTAG_NAME, name, 0);
    00410                 /*@=onlytrans@*/
    00411                 count = rpmdbGetIteratorCount(mi);
    00412                 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
    00413                     if (rpmVersionCompare(oldH, eiu->h) < 0)
    00414                         /*@innercontinue@*/ continue;
    00415                     /* same or newer package already installed */
    00416                     count = 0;
    00417                     /*@innerbreak@*/ break;
    00418                 }
    00419                 mi = rpmdbFreeIterator(mi);
    00420                 if (count == 0) {
    00421                     eiu->h = headerFree(eiu->h);
    00422                     continue;
    00423                 }
    00424                 /* Package is newer than those currently installed. */
    00425             }
    00426
    00427             rc = rpmtransAddPackage(eiu->ts, eiu->h, NULL, fileName,
    00428                                (interfaceFlags & INSTALL_UPGRADE) != 0,
    00429                                relocations);
    00430             /* XXX reference held by transaction set */
    00431             eiu->h = headerFree(eiu->h);
    00432             if (eiu->relocations)
    00433                 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
    00434
    00435             switch(rc) {
    00436             case 0:
    00437                 rpmMessage(RPMMESS_DEBUG, "\tadded binary package [%d]\n",
    00438                         eiu->numRPMS);
    00439                 /*@switchbreak@*/ break;
    00440             case 1:
    00441                 rpmMessage(RPMMESS_ERROR,
    00442                             _("error reading from file %s\n"), *eiu->fnp);
    00443                 eiu->numFailed++;
    00444                 goto exit;
    00445                 /*@notreached@*/ /*@switchbreak@*/ break;
    00446             case 2:
    00447                 rpmMessage(RPMMESS_ERROR,
    00448                             _("file %s requires a newer version of RPM\n"),
    00449                             *eiu->fnp);
    00450                 eiu->numFailed++;
    00451                 goto exit;
    00452                 /*@notreached@*/ /*@switchbreak@*/ break;
    00453             }
    00454
    00455             eiu->numRPMS++;
    00456             continue;
    00457         }
    00458
    00459         if (eiu->rpmrc != RPMRC_BADMAGIC) {
    00460             rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *eiu->fnp);
    00461             eiu->numFailed++; *eiu->fnp = NULL;
    00462             break;
    00463         }
    00464
    00465         /* Try to read a package manifest. */
    00466         eiu->fd = Fopen(*eiu->fnp, "r.fpio");
    00467         if (eiu->fd == NULL || Ferror(eiu->fd)) {
    00468             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
    00469                         Fstrerror(eiu->fd));
    00470             if (eiu->fd) {
    00471                 xx = Fclose(eiu->fd);
    00472                 eiu->fd = NULL;
    00473             }
    00474             eiu->numFailed++; *eiu->fnp = NULL;
    00475             break;
    00476         }
    00477
    00478         /* Read list of packages from manifest. */
    00479         rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
    00480         if (rc)
    00481             rpmError(RPMERR_MANIFEST, _("%s: read manifest failed: %s\n"),
    00482                         *eiu->fnp, Fstrerror(eiu->fd));
    00483         xx = Fclose(eiu->fd);
    00484         eiu->fd = NULL;
    00485
    00486         /* If successful, restart the query loop. */
    00487         if (rc == 0) {
    00488             eiu->prevx++;
    00489             goto restart;
    00490         }
    00491
    00492         eiu->numFailed++; *eiu->fnp = NULL;
    00493         break;
    00494     }
    00495
    00496     rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
    00497                 eiu->numSRPMS, eiu->numRPMS);
    00498
    00499     if (eiu->numFailed) goto exit;
    00500
    00501     if (eiu->numRPMS && !(interfaceFlags & INSTALL_NODEPS)) {
    00502         rpmDependencyConflict conflicts;
    00503         int numConflicts;
    00504
    00505         if (rpmdepCheck(eiu->ts, &conflicts, &numConflicts)) {
    00506             eiu->numFailed = eiu->numPkgs;
    00507             stopInstall = 1;
    00508         }
    00509
    00510         /*@-branchstate@*/
    00511         if (!stopInstall && conflicts) {
    00512             rpmMessage(RPMMESS_ERROR, _("failed dependencies:\n"));
    00513             printDepProblems(stderr, conflicts, numConflicts);
    00514             conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
    00515             eiu->numFailed = eiu->numPkgs;
    00516             stopInstall = 1;
    00517         }
    00518         /*@=branchstate@*/
    00519     }
    00520
    00521     if (eiu->numRPMS && !(interfaceFlags & INSTALL_NOORDER)) {
    00522         if (rpmdepOrder(eiu->ts)) {
    00523             eiu->numFailed = eiu->numPkgs;
    00524             stopInstall = 1;
    00525         }
    00526     }
    00527
    00528     if (eiu->numRPMS && !stopInstall) {
    00529         rpmProblemSet probs = NULL;
    00530
    00531         packagesTotal = eiu->numRPMS + eiu->numSRPMS;
    00532
    00533         rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
    00534         rc = rpmRunTransactions(eiu->ts, rpmShowProgress,
    00535                         (void *) ((long)notifyFlags),
    00536                         NULL, &probs, transFlags, probFilter);
    00537
    00538         if (rc < 0) {
    00539             eiu->numFailed += eiu->numRPMS;
    00540         } else if (rc > 0) {
    00541             eiu->numFailed += rc;
    00542             rpmProblemSetPrint(stderr, probs);
    00543         }
    00544
    00545         if (probs != NULL) rpmProblemSetFree(probs);
    00546     }
    00547
    00548     if (eiu->numSRPMS && !stopInstall) {
    00549         if (eiu->sourceURL != NULL)
    00550         for (i = 0; i < eiu->numSRPMS; i++) {
    00551             if (eiu->sourceURL[i] == NULL) continue;
    00552             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
    00553             if (eiu->fd == NULL || Ferror(eiu->fd)) {
    00554                 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
    00555                            eiu->sourceURL[i], Fstrerror(eiu->fd));
    00556                 if (eiu->fd) {
    00557                     xx = Fclose(eiu->fd);
    00558                     eiu->fd = NULL;
    00559                 }
    00560                 continue;
    00561             }
    00562
    00563             if (!(transFlags & RPMTRANS_FLAG_TEST)) {
    00564                 eiu->rpmrc = rpmInstallSourcePackage(rootdir, eiu->fd, NULL,
    00565                         rpmShowProgress, (void *) ((long)notifyFlags), NULL);
    00566                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
    00567             }
    00568
    00569             xx = Fclose(eiu->fd);
    00570             eiu->fd = NULL;
    00571         }
    00572     }
    00573
    00574 exit:
    00575     eiu->ts = rpmtransFree(eiu->ts);
    00576     if (eiu->pkgURL != NULL)
    00577     for (i = 0; i < eiu->numPkgs; i++) {
    00578         if (eiu->pkgURL[i] == NULL) continue;
    00579         if (eiu->pkgState[i] == 1)
    00580             (void) Unlink(eiu->pkgURL[i]);
    00581         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
    00582     }
    00583     eiu->pkgState = _free(eiu->pkgState);
    00584     eiu->pkgURL = _free(eiu->pkgURL);
    00585     eiu->argv = _free(eiu->argv);
    00586     if (eiu->db != NULL) {
    00587         xx = rpmdbClose(eiu->db);
    00588         eiu->db = NULL;
    00589     }
    00590     return eiu->numFailed;
    00591 }
    00592
    00593 int rpmErase(const char * rootdir, const char ** argv,
    00594                 rpmtransFlags transFlags,
    00595                 rpmEraseInterfaceFlags interfaceFlags)
    00596 {
    00597     rpmdb db;
    00598     int mode;
    00599     int count;
    00600     const char ** arg;
    00601     int numFailed = 0;
    00602     rpmTransactionSet ts;
    00603     rpmDependencyConflict conflicts;
    00604     int numConflicts;
    00605     int stopUninstall = 0;
    00606     int numPackages = 0;
    00607     rpmProblemSet probs;
    00608
    00609     if (argv == NULL) return 0;
    00610
    00611     if (transFlags & RPMTRANS_FLAG_TEST)
    00612         mode = O_RDONLY;
    00613     else
    00614         mode = O_RDWR | O_EXCL;
    00615
    00616     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
    00617         const char *dn;
    00618         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
    00619         rpmMessage(RPMMESS_ERROR, _("cannot open %s/packages.rpm\n"), dn);
    00620         dn = _free(dn);
    00621         return -1;
    00622     }
    00623
    00624     ts = rpmtransCreateSet(db, rootdir);
    00625     for (arg = argv; *arg; arg++) {
    00626         rpmdbMatchIterator mi;
    00627
    00628         /* XXX HACK to get rpmdbFindByLabel out of the API */
    00629         mi = rpmdbInitIterator(db, RPMDBI_LABEL, *arg, 0);
    00630         count = rpmdbGetIteratorCount(mi);
    00631         if (count <= 0) {
    00632             rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
    00633             numFailed++;
    00634         } else if (!(count == 1 || (interfaceFlags & UNINSTALL_ALLMATCHES))) {
    00635             rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
    00636                         *arg);
    00637             numFailed++;
    00638         } else {
    00639             Header h;   /* XXX iterator owns the reference */
    00640             while ((h = rpmdbNextIterator(mi)) != NULL) {
    00641                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
    00642                 if (recOffset) {
    00643                     (void) rpmtransRemovePackage(ts, recOffset);
    00644                     numPackages++;
    00645                 }
    00646             }
    00647         }
    00648         mi = rpmdbFreeIterator(mi);
    00649     }
    00650
    00651     if (!(interfaceFlags & UNINSTALL_NODEPS)) {
    00652         if (rpmdepCheck(ts, &conflicts, &numConflicts)) {
    00653             numFailed = numPackages;
    00654             stopUninstall = 1;
    00655         }
    00656
    00657         /*@-branchstate@*/
    00658         if (!stopUninstall && conflicts) {
    00659             rpmMessage(RPMMESS_ERROR, _("removing these packages would break "
    00660                               "dependencies:\n"));
    00661             printDepProblems(stderr, conflicts, numConflicts);
    00662             conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
    00663             numFailed += numPackages;
    00664             stopUninstall = 1;
    00665         }
    00666         /*@=branchstate@*/
    00667     }
    00668
    00669     if (!stopUninstall) {
    00670         transFlags |= RPMTRANS_FLAG_REVERSE;
    00671         numFailed += rpmRunTransactions(ts, NULL, NULL, NULL, &probs,
    00672                                         transFlags, 0);
    00673     }
    00674
    00675     ts = rpmtransFree(ts);
    00676     (void) rpmdbClose(db);
    00677
    00678     return numFailed;
    00679 }
    00680
    00681 int rpmInstallSource(const char * rootdir, const char * arg,
    00682                 const char ** specFile, char ** cookie)
    00683 {
    00684     FD_t fd;
    00685     int rc;
    00686
    00687     fd = Fopen(arg, "r.ufdio");
    00688     if (fd == NULL || Ferror(fd)) {
    00689         rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
    00690         if (fd) (void) Fclose(fd);
    00691         return 1;
    00692     }
    00693
    00694     if (rpmIsVerbose())
    00695         fprintf(stdout, _("Installing %s\n"), arg);
    00696
    00697     {
    00698         /*@-mayaliasunique@*/
    00699         rpmRC rpmrc = rpmInstallSourcePackage(rootdir, fd, specFile, NULL, NULL,
    00700                                  cookie);
    00701         /*@=mayaliasunique@*/
    00702         rc = (rpmrc == RPMRC_OK ? 0 : 1);
    00703     }
    00704     if (rc != 0) {
    00705         rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
    00706         /*@-unqualifiedtrans@*/
    00707         if (specFile && *specFile)
    00708             *specFile = _free(*specFile);
    00709         if (cookie && *cookie)
    00710             *cookie = _free(*cookie);
    00711         /*@=unqualifiedtrans@*/
    00712     }
    00713
    00714     (void) Fclose(fd);
    00715
    00716     return rc;
    00717 }
    00718
    00719 /*@unchecked@*/
    00720 static int reverse = -1;
    00721
    00724 static int IDTintcmp(const void * a, const void * b)
    00725         /*@*/
    00726 {
    00727     /*@-castexpose@*/
    00728     IDT ap = (IDT)a;
    00729     IDT bp = (IDT)b;
    00730     /*@=castexpose@*/
    00731     int rc = ((int)ap->val.u32 - (int)bp->val.u32);
    00732
    00733     if (rc)
    00734         return ( reverse * rc );
    00735     return ( strcmp(ap->n, bp->n) );
    00736 }
    00737
    00738 IDTX IDTXfree(IDTX idtx)
    00739 {
    00740     if (idtx) {
    00741         int i;
    00742         if (idtx->idt)
    00743         for (i = 0; i < idtx->nidt; i++) {
    00744             IDT idt = idtx->idt + i;
    00745             idt->h = headerFree(idt->h);
    00746             idt->key = _free(idt->key);
    00747         }
    00748         idtx->idt = _free(idtx->idt);
    00749         idtx = _free(idtx);
    00750     }
    00751     return NULL;
    00752 }
    00753
    00754 IDTX IDTXnew(void)
    00755 {
    00756     IDTX idtx = xcalloc(1, sizeof(*idtx));
    00757     idtx->delta = 10;
    00758     idtx->size = sizeof(*((IDT)0));
    00759     return idtx;
    00760 }
    00761
    00762 IDTX IDTXgrow(IDTX idtx, int need)
    00763 {
    00764     if (need < 0) return NULL;
    00765     if (idtx == NULL)
    00766         idtx = IDTXnew();
    00767     if (need == 0) return idtx;
    00768
    00769     if ((idtx->nidt + need) > idtx->alloced) {
    00770         while (need > 0) {
    00771             idtx->alloced += idtx->delta;
    00772             need -= idtx->delta;
    00773         }
    00774         idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
    00775     }
    00776     return idtx;
    00777 }
    00778
    00779 IDTX IDTXsort(IDTX idtx)
    00780 {
    00781     if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
    00782         qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
    00783     return idtx;
    00784 }
    00785
    00786 IDTX IDTXload(rpmdb db, rpmTag tag)
    00787 {
    00788     IDTX idtx = NULL;
    00789     rpmdbMatchIterator mi;
    00790     HGE_t hge = (HGE_t) headerGetEntry;
    00791     Header h;
    00792
    00793     mi = rpmdbInitIterator(db, tag, NULL, 0);
    00794     /*@-branchstate@*/
    00795     while ((h = rpmdbNextIterator(mi)) != NULL) {
    00796         rpmTagType type = RPM_NULL_TYPE;
    00797         int_32 count = 0;
    00798         int_32 * tidp;
    00799
    00800         tidp = NULL;
    00801         if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
    00802             continue;
    00803
    00804         if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
    00805             continue;
    00806
    00807         idtx = IDTXgrow(idtx, 1);
    00808         if (idtx == NULL)
    00809             continue;
    00810         if (idtx->idt == NULL)
    00811             continue;
    00812
    00813         {   IDT idt;
    00814             /*@-nullderef@*/
    00815             idt = idtx->idt + idtx->nidt;
    00816             /*@=nullderef@*/
    00817             idt->h = headerLink(h);
    00818             (void) headerNVR(idt->h, &idt->n, &idt->v, &idt->r);
    00819             idt->key = NULL;
    00820             idt->instance = rpmdbGetIteratorOffset(mi);
    00821             idt->val.u32 = *tidp;
    00822         }
    00823         idtx->nidt++;
    00824     }
    00825     /*@=branchstate@*/
    00826     mi = rpmdbFreeIterator(mi);
    00827
    00828     return IDTXsort(idtx);
    00829 }
    00830
    00831 IDTX IDTXglob(const char * globstr, rpmTag tag)
    00832 {
    00833     IDTX idtx = NULL;
    00834     HGE_t hge = (HGE_t) headerGetEntry;
    00835     Header h;
    00836     int_32 * tidp;
    00837     FD_t fd;
    00838     const char ** av = NULL;
    00839     int ac = 0;
    00840     int rc;
    00841     int i;
    00842
    00843     av = NULL;  ac = 0;
    00844     rc = rpmGlob(globstr, &ac, &av);
    00845
    00846     if (rc == 0)
    00847     for (i = 0; i < ac; i++) {
    00848         rpmTagType type;
    00849         int_32 count;
    00850         int isSource;
    00851         rpmRC rpmrc;
    00852
    00853         fd = Fopen(av[i], "r.ufdio");
    00854         if (fd == NULL || Ferror(fd)) {
    00855             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
    00856                         Fstrerror(fd));
    00857             if (fd) (void) Fclose(fd);
    00858             continue;
    00859         }
    00860
    00861         /*@-mustmod@*/  /* LCL: segfault */
    00862         rpmrc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
    00863         /*@=mustmod@*/
    00864         if (rpmrc != RPMRC_OK || isSource) {
    00865             (void) Fclose(fd);
    00866             continue;
    00867         }
    00868
    00869         tidp = NULL;
    00870         /*@-branchstate@*/
    00871         if (hge(h, tag, &type, (void **) &tidp, &count) && tidp) {
    00872
    00873             idtx = IDTXgrow(idtx, 1);
    00874             if (idtx == NULL || idtx->idt == NULL) {
    00875                 h = headerFree(h);
    00876                 (void) Fclose(fd);
    00877                 continue;
    00878             }
    00879             {   IDT idt;
    00880                 idt = idtx->idt + idtx->nidt;
    00881                 idt->h = headerLink(h);
    00882                 (void) headerNVR(idt->h, &idt->n, &idt->v, &idt->r);
    00883                 idt->key = xstrdup(av[i]);
    00884                 idt->instance = 0;
    00885                 idt->val.u32 = *tidp;
    00886             }
    00887             idtx->nidt++;
    00888         }
    00889         /*@=branchstate@*/
    00890
    00891         h = headerFree(h);
    00892         (void) Fclose(fd);
    00893     }
    00894
    00895     for (i = 0; i < ac; i++)
    00896         av[i] = _free(av[i]);
    00897     av = _free(av);     ac = 0;
    00898
    00899     return IDTXsort(idtx);
    00900 }
    00901
    00902 int rpmRollback(struct rpmInstallArguments_s * ia, const char ** argv)
    00903 {
    00904     rpmdb db = NULL;
    00905     rpmTransactionSet ts = NULL;
    00906     rpmDependencyConflict conflicts = NULL;
    00907     int numConflicts = 0;
    00908     rpmProblemSet probs = NULL;
    00909     IDTX itids = NULL;
    00910     IDTX rtids = NULL;
    00911     unsigned thistid = 0xffffffff;
    00912     unsigned prevtid;
    00913     time_t tid;
    00914     IDT rp;
    00915     int nrids = 0;
    00916     IDT ip;
    00917     int niids = 0;
    00918     int packagesIn;
    00919     int packagesOut;
    00920     int rc;
    00921     int i;
    00922     int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
    00923
    00924     if (argv != NULL && *argv != NULL) {
    00925         rc = -1;
    00926         goto exit;
    00927     }
    00928
    00929     rc = rpmdbOpen(ia->rootdir, &db, O_RDWR, 0644);
    00930     if (rc != 0)
    00931         goto exit;
    00932
    00933     itids = IDTXload(db, RPMTAG_INSTALLTID);
    00934     if (itids != NULL) {
    00935         ip = itids->idt;
    00936         niids = itids->nidt;
    00937     } else {
    00938         ip = NULL;
    00939         niids = 0;
    00940     }
    00941
    00942     {   const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
    00943         if (globstr == NULL || *globstr == '%') {
    00944             globstr = _free(globstr);
    00945             rc = -1;
    00946             goto exit;
    00947         }
    00948         rtids = IDTXglob(globstr, RPMTAG_REMOVETID);
    00949         if (rtids != NULL) {
    00950             rp = rtids->idt;
    00951             nrids = rtids->nidt;
    00952         } else {
    00953             rp = NULL;
    00954             nrids = 0;
    00955         }
    00956         globstr = _free(globstr);
    00957     }
    00958
    00959     /* Run transactions until rollback goal is achieved. */
    00960     do {
    00961         prevtid = thistid;
    00962         rc = 0;
    00963         packagesTotal = 0;
    00964         packagesIn = 0;
    00965         packagesOut = 0;
    00966         ia->installInterfaceFlags &= ~ifmask;
    00967
    00968         /* Find larger of the remaining install/erase transaction id's. */
    00969         thistid = 0;
    00970         if (ip != NULL && ip->val.u32 > thistid)
    00971             thistid = ip->val.u32;
    00972         if (rp != NULL && rp->val.u32 > thistid)
    00973             thistid = rp->val.u32;
    00974
    00975         /* If we've achieved the rollback goal, then we're done. */
    00976         if (thistid == 0 || thistid < ia->rbtid)
    00977             break;
    00978
    00979         ts = rpmtransCreateSet(db, ia->rootdir);
    00980
    00981         /* Install the previously erased packages for this transaction. */
    00982         while (rp != NULL && rp->val.u32 == thistid) {
    00983
    00984             rpmMessage(RPMMESS_DEBUG, "\t+++ %s-%s-%s\t(from %s)\n", rp->n, rp->v, rp->r, basename(rp->key));
    00985
    00986             if (!(ia->installInterfaceFlags & ifmask))
    00987                 ia->installInterfaceFlags |= INSTALL_INSTALL;
    00988
    00989             rc = rpmtransAddPackage(ts, rp->h, NULL, rp->key,
    00990                         (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
    00991                         ia->relocations);
    00992             if (rc != 0)
    00993                 goto exit;
    00994
    00995             packagesTotal++;
    00996             packagesIn++;
    00997
    00998 #ifdef  NOTYET
    00999             rp->h = headerFree(rp->h);
    01000 #endif
    01001             nrids--;
    01002             if (nrids > 0)
    01003                 rp++;
    01004             else
    01005                 rp = NULL;
    01006         }
    01007
    01008         /*
    01009          * XXX OK, let's prevent disaster right here, as rollbacks will merrily
    01010          * XXX erase everything in order to achieve the desired goal.
    01011          */
    01012         if (packagesIn == 0)
    01013             break;
    01014
    01015         /* Erase the previously installed packages for this transaction. */
    01016         while (ip != NULL && ip->val.u32 == thistid) {
    01017
    01018             rpmMessage(RPMMESS_DEBUG,
    01019                         "\t--- %s-%s-%s\t(from rpmdb instance #%u)\n", ip->n, ip->v, ip->r, ip->instance);
    01020
    01021             if (!(ia->installInterfaceFlags & ifmask))
    01022                 ia->installInterfaceFlags |= INSTALL_ERASE;
    01023
    01024             rc = rpmtransRemovePackage(ts, ip->instance);
    01025             if (rc != 0)
    01026                 goto exit;
    01027
    01028             packagesTotal++;
    01029             packagesOut++;
    01030
    01031 #ifdef  NOTYET
    01032             ip->instance = 0;
    01033 #endif
    01034             niids--;
    01035             if (niids > 0)
    01036                 ip++;
    01037             else
    01038                 ip = NULL;
    01039         }
    01040
    01041         /* Anything to do? */
    01042         if (packagesTotal <= 0)
    01043             break;
    01044
    01045         tid = (time_t)thistid;
    01046         rpmMessage(RPMMESS_DEBUG, _("rollback (+%d,-%d) packages to %s"),
    01047                         packagesIn, packagesOut, ctime(&tid));
    01048
    01049         conflicts = NULL;
    01050         numConflicts = 0;
    01051         rc = rpmdepCheck(ts, &conflicts, &numConflicts);
    01052         if (rc != 0) {
    01053             rpmMessage(RPMMESS_ERROR, _("failed dependencies:\n"));
    01054             printDepProblems(stderr, conflicts, numConflicts);
    01055             conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
    01056             goto exit;
    01057         }
    01058
    01059         rc = rpmdepOrder(ts);
    01060         if (rc != 0)
    01061             goto exit;
    01062
    01063         probs = NULL;
    01064         rc = rpmRunTransactions(ts,  rpmShowProgress,
    01065                 (void *) ((long)ia->installInterfaceFlags),
    01066                 NULL, &probs, ia->transFlags,
    01067                 (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
    01068         if (rc > 0) {
    01069             rpmProblemSetPrint(stderr, probs);
    01070             if (probs != NULL) rpmProblemSetFree(probs);
    01071             probs = NULL;
    01072         }
    01073         if (rc)
    01074             goto exit;
    01075
    01076         ts = rpmtransFree(ts);
    01077
    01078         /* Clean up after successful rollback. */
    01079         if (!rpmIsDebug())
    01080         for (i = 0; i < rtids->nidt; i++) {
    01081             IDT rrp = rtids->idt + i;
    01082             if (rrp->val.u32 != thistid)
    01083                 continue;
    01084             (void) unlink(rrp->key);
    01085         }
    01086
    01087     } while (1);
    01088
    01089 exit:
    01090     ts = rpmtransFree(ts);
    01091
    01092     if (db != NULL) (void) rpmdbClose(db);
    01093
    01094     rtids = IDTXfree(rtids);
    01095     itids = IDTXfree(itids);
    01096
    01097     return rc;
    01098 }