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

Generated at Tue Dec 23 04:54:12 2003 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001