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

Generated on Wed Mar 13 15:34:48 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002