Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

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

Generated at Thu Sep 6 11:32:30 2001 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001