- fix: segfault with --checksig, plug memory leak (#72455).
[platform/upstream/rpm.git] / lib / rpminstall.c
1 /** \ingroup rpmcli
2  * \file lib/rpminstall.c
3  */
4
5 #include "system.h"
6
7 #include <rpmcli.h>
8
9 #include "rpmdb.h"
10 #include "rpmds.h"
11
12 #define _RPMTS_INTERNAL         /* ts->goal, ts->dbmode, ts->suggests */
13 #include "rpmts.h"
14
15 #include "manifest.h"
16 #include "misc.h"       /* XXX for rpmGlob() */
17 #include "debug.h"
18
19 /*@access rpmts @*/     /* XXX ts->goal, ts->dbmode */
20 /*@access rpmps @*/     /* XXX compared with NULL */
21 /*@access Header @*/            /* XXX compared with NULL */
22 /*@access rpmdb @*/             /* XXX compared with NULL */
23 /*@access FD_t @*/              /* XXX compared with NULL */
24 /*@access IDTX @*/
25 /*@access IDT @*/
26
27 /*@unchecked@*/
28 int rpmcliPackagesTotal = 0;
29 /*@unchecked@*/
30 int rpmcliHashesCurrent = 0;
31 /*@unchecked@*/
32 int rpmcliHashesTotal = 0;
33 /*@unchecked@*/
34 int rpmcliProgressCurrent = 0;
35 /*@unchecked@*/
36 int rpmcliProgressTotal = 0;
37
38 /**
39  * Print a CLI progress bar.
40  * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
41  * @param amount        current
42  * @param total         final
43  */
44 static void printHash(const unsigned long amount, const unsigned long total)
45         /*@globals rpmcliHashesCurrent, rpmcliHashesTotal,
46                 rpmcliProgressCurrent, fileSystem @*/
47         /*@modifies rpmcliHashesCurrent, rpmcliHashesTotal,
48                 rpmcliProgressCurrent, fileSystem @*/
49 {
50     int hashesNeeded;
51
52     rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
53
54     if (rpmcliHashesCurrent != rpmcliHashesTotal) {
55         float pct = (total ? (((float) amount) / total) : 1.0);
56         hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
57         while (hashesNeeded > rpmcliHashesCurrent) {
58             if (isatty (STDOUT_FILENO)) {
59                 int i;
60                 for (i = 0; i < rpmcliHashesCurrent; i++)
61                     (void) putchar ('#');
62                 for (; i < rpmcliHashesTotal; i++)
63                     (void) putchar (' ');
64                 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
65                 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
66                     (void) putchar ('\b');
67             } else
68                 fprintf(stdout, "#");
69
70             rpmcliHashesCurrent++;
71         }
72         (void) fflush(stdout);
73
74         if (rpmcliHashesCurrent == rpmcliHashesTotal) {
75             int i;
76             rpmcliProgressCurrent++;
77             if (isatty(STDOUT_FILENO)) {
78                 for (i = 1; i < rpmcliHashesCurrent; i++)
79                     (void) putchar ('#');
80                 pct = (rpmcliProgressTotal
81                     ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
82                     : 1);
83                 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
84             }
85             fprintf(stdout, "\n");
86         }
87         (void) fflush(stdout);
88     }
89 }
90
91 void * rpmShowProgress(/*@null@*/ const void * arg,
92                         const rpmCallbackType what,
93                         const unsigned long amount,
94                         const unsigned long total,
95                         /*@null@*/ fnpyKey key,
96                         /*@null@*/ void * data)
97         /*@globals rpmcliHashesCurrent, rpmcliProgressCurrent, rpmcliProgressTotal,
98                 fileSystem @*/
99         /*@modifies rpmcliHashesCurrent, rpmcliProgressCurrent, rpmcliProgressTotal,
100                 fileSystem @*/
101 {
102     /*@-castexpose@*/
103     Header h = (Header) arg;
104     /*@=castexpose@*/
105     char * s;
106     int flags = (int) ((long)data);
107     void * rc = NULL;
108     /*@-assignexpose -abstract @*/
109     const char * filename = (const char *)key;
110     /*@=assignexpose =abstract @*/
111     static FD_t fd = NULL;
112     int xx;
113
114     switch (what) {
115     case RPMCALLBACK_INST_OPEN_FILE:
116 /*@-boundsread@*/
117         if (filename == NULL || filename[0] == '\0')
118             return NULL;
119 /*@=boundsread@*/
120         fd = Fopen(filename, "r.ufdio");
121         /*@-type@*/ /* FIX: still necessary? */
122         if (fd == NULL || Ferror(fd)) {
123             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), filename,
124                         Fstrerror(fd));
125             if (fd) {
126                 xx = Fclose(fd);
127                 fd = NULL;
128             }
129         } else
130             fd = fdLink(fd, "persist (showProgress)");
131         /*@=type@*/
132         return fd;
133         /*@notreached@*/ break;
134
135     case RPMCALLBACK_INST_CLOSE_FILE:
136         /*@-type@*/ /* FIX: still necessary? */
137         fd = fdFree(fd, "persist (showProgress)");
138         /*@=type@*/
139         if (fd) {
140             xx = Fclose(fd);
141             fd = NULL;
142         }
143         break;
144
145     case RPMCALLBACK_INST_START:
146         rpmcliHashesCurrent = 0;
147         if (h == NULL || !(flags & INSTALL_LABEL))
148             break;
149         /* @todo Remove headerSprintf() on a progress callback. */
150         if (flags & INSTALL_HASH) {
151             s = headerSprintf(h, "%{NAME}",
152                                 rpmTagTable, rpmHeaderFormats, NULL);
153             if (isatty (STDOUT_FILENO))
154                 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
155             else
156                 fprintf(stdout, "%-28.28s", s);
157             (void) fflush(stdout);
158             s = _free(s);
159         } else {
160             s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
161                                   rpmTagTable, rpmHeaderFormats, NULL);
162             fprintf(stdout, "%s\n", s);
163             (void) fflush(stdout);
164             s = _free(s);
165         }
166         break;
167
168     case RPMCALLBACK_TRANS_PROGRESS:
169     case RPMCALLBACK_INST_PROGRESS:
170         if (flags & INSTALL_PERCENT)
171             fprintf(stdout, "%%%% %f\n", (double) (total
172                                 ? ((((float) amount) / total) * 100)
173                                 : 100.0));
174         else if (flags & INSTALL_HASH)
175             printHash(amount, total);
176         (void) fflush(stdout);
177         break;
178
179     case RPMCALLBACK_TRANS_START:
180         rpmcliHashesCurrent = 0;
181         rpmcliProgressTotal = 1;
182         rpmcliProgressCurrent = 0;
183         if (!(flags & INSTALL_LABEL))
184             break;
185         if (flags & INSTALL_HASH)
186             fprintf(stdout, "%-28s", _("Preparing..."));
187         else
188             fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
189         (void) fflush(stdout);
190         break;
191
192     case RPMCALLBACK_TRANS_STOP:
193         if (flags & INSTALL_HASH)
194             printHash(1, 1);    /* Fixes "preparing..." progress bar */
195         rpmcliProgressTotal = rpmcliPackagesTotal;
196         rpmcliProgressCurrent = 0;
197         break;
198
199     case RPMCALLBACK_REPACKAGE_START:
200         rpmcliHashesCurrent = 0;
201         rpmcliProgressTotal = total;
202         rpmcliProgressCurrent = 0;
203         if (!(flags & INSTALL_LABEL))
204             break;
205         if (flags & INSTALL_HASH)
206             fprintf(stdout, "%-28s\n", _("Repackaging..."));
207         else
208             fprintf(stdout, "%s\n", _("Repackaging erased files..."));
209         (void) fflush(stdout);
210         break;
211
212     case RPMCALLBACK_REPACKAGE_PROGRESS:
213         if (amount && (flags & INSTALL_HASH))
214             printHash(1, 1);    /* Fixes "preparing..." progress bar */
215         break;
216
217     case RPMCALLBACK_REPACKAGE_STOP:
218         rpmcliProgressTotal = total;
219         rpmcliProgressCurrent = total;
220         if (flags & INSTALL_HASH)
221             printHash(1, 1);    /* Fixes "preparing..." progress bar */
222         rpmcliProgressTotal = rpmcliPackagesTotal;
223         rpmcliProgressCurrent = 0;
224         if (!(flags & INSTALL_LABEL))
225             break;
226         if (flags & INSTALL_HASH)
227             fprintf(stdout, "%-28s\n", _("Upgrading..."));
228         else
229             fprintf(stdout, "%s\n", _("Upgrading packages..."));
230         (void) fflush(stdout);
231         break;
232
233     case RPMCALLBACK_UNINST_PROGRESS:
234         break;
235     case RPMCALLBACK_UNINST_START:
236         break;
237     case RPMCALLBACK_UNINST_STOP:
238         break;
239     case RPMCALLBACK_UNPACK_ERROR:
240         break;
241     case RPMCALLBACK_CPIO_ERROR:
242         break;
243     case RPMCALLBACK_UNKNOWN:
244     default:
245         break;
246     }
247
248     return rc;
249 }       
250
251 typedef /*@only@*/ /*@null@*/ const char * str_t;
252
253 struct rpmEIU {
254     Header h;
255     FD_t fd;
256     int numFailed;
257     int numPkgs;
258 /*@only@*/
259     str_t * pkgURL;
260 /*@dependent@*/ /*@null@*/
261     str_t * fnp;
262 /*@only@*/
263     char * pkgState;
264     int prevx;
265     int pkgx;
266     int numRPMS;
267     int numSRPMS;
268 /*@only@*/ /*@null@*/
269     str_t * sourceURL;
270     int isSource;
271     int argc;
272 /*@only@*/ /*@null@*/
273     str_t * argv;
274 /*@dependent@*/
275     rpmRelocation * relocations;
276     rpmRC rpmrc;
277 };
278
279 /** @todo Generalize --freshen policies. */
280 /*@-bounds@*/
281 int rpmInstall(rpmts ts,
282                 struct rpmInstallArguments_s * ia,
283                 const char ** fileArgv)
284 {
285     struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
286     rpmps ps;
287     rpmprobFilterFlags probFilter;
288     rpmRelocation * relocations;
289 /*@only@*/ /*@null@*/ const char * fileURL = NULL;
290     int stopInstall = 0;
291     const char ** av = NULL;
292     rpmVSFlags vsflags, ovsflags, tvsflags;
293     int ac = 0;
294     int rc;
295     int xx;
296     int i;
297
298     if (fileArgv == NULL) goto exit;
299
300     ts->goal = TSM_INSTALL;
301     rpmcliPackagesTotal = 0;
302
303     (void) rpmtsSetFlags(ts, ia->transFlags);
304     probFilter = ia->probFilter;
305     relocations = ia->relocations;
306
307     if (ia->installInterfaceFlags & INSTALL_UPGRADE)
308         vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
309     else
310         vsflags = rpmExpandNumeric("%{?_vsflags_install}");
311     if (ia->qva_flags & VERIFY_DIGEST)
312         vsflags |= _RPMVSF_NODIGESTS;
313     if (ia->qva_flags & VERIFY_SIGNATURE)
314         vsflags |= _RPMVSF_NOSIGNATURES;
315     if (ia->qva_flags & VERIFY_HDRCHK)
316         vsflags |= RPMVSF_NOHDRCHK;
317     ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
318
319     {   int notifyFlags;
320         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
321         xx = rpmtsSetNotifyCallback(ts,
322                         rpmShowProgress, (void *) ((long)notifyFlags));
323     }
324
325     if ((eiu->relocations = relocations) != NULL) {
326         while (eiu->relocations->oldPath)
327             eiu->relocations++;
328         if (eiu->relocations->newPath == NULL)
329             eiu->relocations = NULL;
330     }
331
332     /* Build fully globbed list of arguments in argv[argc]. */
333     /*@-branchstate@*/
334     /*@-temptrans@*/
335     for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
336     /*@=temptrans@*/
337         av = _free(av); ac = 0;
338         rc = rpmGlob(*eiu->fnp, &ac, &av);
339         if (rc || ac == 0) continue;
340
341         eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
342         memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
343         eiu->argc += ac;
344         eiu->argv[eiu->argc] = NULL;
345     }
346     /*@=branchstate@*/
347     av = _free(av);     ac = 0;
348
349 restart:
350     /* Allocate sufficient storage for next set of args. */
351     if (eiu->pkgx >= eiu->numPkgs) {
352         eiu->numPkgs = eiu->pkgx + eiu->argc;
353         eiu->pkgURL = xrealloc(eiu->pkgURL,
354                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
355         memset(eiu->pkgURL + eiu->pkgx, 0,
356                         ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
357         eiu->pkgState = xrealloc(eiu->pkgState,
358                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
359         memset(eiu->pkgState + eiu->pkgx, 0,
360                         ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
361     }
362
363     /* Retrieve next set of args, cache on local storage. */
364     for (i = 0; i < eiu->argc; i++) {
365         fileURL = _free(fileURL);
366         fileURL = eiu->argv[i];
367         eiu->argv[i] = NULL;
368
369 #ifdef  NOTYET
370 if (fileURL[0] == '=') {
371     rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
372
373     xx = rpmtsSolve(ts, this, NULL);
374     if (ts->suggests && ts->nsuggests > 0) {
375         fileURL = _free(fileURL);
376         fileURL = ts->suggests[0];
377         ts->suggests[0] = NULL;
378         while (ts->nsuggests-- > 0) {
379             if (ts->suggests[ts->nsuggests] == NULL)
380                 continue;
381             ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
382         }
383         ts->suggests = _free(ts->suggests);
384         rpmMessage(RPMMESS_DEBUG, _("Adding goal: %s\n"), fileURL);
385         eiu->pkgURL[eiu->pkgx] = fileURL;
386         fileURL = NULL;
387         eiu->pkgx++;
388     }
389     this = rpmdsFree(this);
390 } else
391 #endif
392
393         switch (urlIsURL(fileURL)) {
394         case URL_IS_FTP:
395         case URL_IS_HTTP:
396         {   const char *tfn;
397
398             if (rpmIsVerbose())
399                 fprintf(stdout, _("Retrieving %s\n"), fileURL);
400
401             {   char tfnbuf[64];
402                 const char * rootDir = rpmtsRootDir(ts);
403                 if (!(rootDir && * rootDir))
404                     rootDir = "";
405                 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
406                 (void) mktemp(tfnbuf);
407                 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
408             }
409
410             /* XXX undefined %{name}/%{version}/%{release} here */
411             /* XXX %{_tmpdir} does not exist */
412             rpmMessage(RPMMESS_DEBUG, _(" ... as %s\n"), tfn);
413             rc = urlGetFile(fileURL, tfn);
414             if (rc < 0) {
415                 rpmMessage(RPMMESS_ERROR,
416                         _("skipping %s - transfer failed - %s\n"),
417                         fileURL, ftpStrerror(rc));
418                 eiu->numFailed++;
419                 eiu->pkgURL[eiu->pkgx] = NULL;
420                 tfn = _free(tfn);
421                 /*@switchbreak@*/ break;
422             }
423             eiu->pkgState[eiu->pkgx] = 1;
424             eiu->pkgURL[eiu->pkgx] = tfn;
425             eiu->pkgx++;
426         }   /*@switchbreak@*/ break;
427         case URL_IS_PATH:
428         default:
429             eiu->pkgURL[eiu->pkgx] = fileURL;
430             fileURL = NULL;
431             eiu->pkgx++;
432             /*@switchbreak@*/ break;
433         }
434     }
435     fileURL = _free(fileURL);
436
437     if (eiu->numFailed) goto exit;
438
439     /* Continue processing file arguments, building transaction set. */
440     for (eiu->fnp = eiu->pkgURL+eiu->prevx;
441          *eiu->fnp != NULL;
442          eiu->fnp++, eiu->prevx++)
443     {
444         const char * fileName;
445
446         rpmMessage(RPMMESS_DEBUG, "============== %s\n", *eiu->fnp);
447         (void) urlPath(*eiu->fnp, &fileName);
448
449         /* Try to read the header from a package file. */
450         eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
451         if (eiu->fd == NULL || Ferror(eiu->fd)) {
452             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
453                         Fstrerror(eiu->fd));
454             if (eiu->fd) {
455                 xx = Fclose(eiu->fd);
456                 eiu->fd = NULL;
457             }
458             eiu->numFailed++; *eiu->fnp = NULL;
459             continue;
460         }
461
462         /* Read the header, verifying signatures (if present). */
463         tvsflags = rpmtsSetVSFlags(ts, vsflags);
464         eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
465         tvsflags = rpmtsSetVSFlags(ts, tvsflags);
466         xx = Fclose(eiu->fd);
467         eiu->fd = NULL;
468
469         switch (eiu->rpmrc) {
470         case RPMRC_FAIL:
471             rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *eiu->fnp);
472             eiu->numFailed++; *eiu->fnp = NULL;
473             continue;
474             /*@notreached@*/ /*@switchbreak@*/ break;
475         case RPMRC_NOTFOUND:
476             goto maybe_manifest;
477             /*@notreached@*/ /*@switchbreak@*/ break;
478         case RPMRC_NOTTRUSTED:
479         case RPMRC_NOKEY:
480         case RPMRC_OK:
481         default:
482             /*@switchbreak@*/ break;
483         }
484
485         eiu->isSource = headerIsEntry(eiu->h, RPMTAG_SOURCEPACKAGE);
486
487         if (eiu->isSource) {
488             rpmMessage(RPMMESS_DEBUG, "\tadded source package [%d]\n",
489                 eiu->numSRPMS);
490             eiu->sourceURL = xrealloc(eiu->sourceURL,
491                                 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
492             eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
493             *eiu->fnp = NULL;
494             eiu->numSRPMS++;
495             eiu->sourceURL[eiu->numSRPMS] = NULL;
496             continue;
497         }
498
499         if (eiu->relocations) {
500             const char ** paths;
501             int pft;
502             int c;
503
504             if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
505                                        (void **) &paths, &c) && (c == 1))
506             {
507                 eiu->relocations->oldPath = xstrdup(paths[0]);
508                 paths = headerFreeData(paths, pft);
509             } else {
510                 const char * name;
511                 xx = headerNVR(eiu->h, &name, NULL, NULL);
512                 rpmMessage(RPMMESS_ERROR,
513                                _("package %s is not relocateable\n"), name);
514                 eiu->numFailed++;
515                 goto exit;
516                 /*@notreached@*/
517             }
518         }
519
520         /* On --freshen, verify package is installed and newer */
521         if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
522             rpmdbMatchIterator mi;
523             const char * name;
524             Header oldH;
525             int count;
526
527             xx = headerNVR(eiu->h, &name, NULL, NULL);
528             mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
529             count = rpmdbGetIteratorCount(mi);
530             while ((oldH = rpmdbNextIterator(mi)) != NULL) {
531                 if (rpmVersionCompare(oldH, eiu->h) < 0)
532                     /*@innercontinue@*/ continue;
533                 /* same or newer package already installed */
534                 count = 0;
535                 /*@innerbreak@*/ break;
536             }
537             mi = rpmdbFreeIterator(mi);
538             if (count == 0) {
539                 eiu->h = headerFree(eiu->h);
540                 continue;
541             }
542             /* Package is newer than those currently installed. */
543         }
544
545         /*@-abstract@*/
546         rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
547                         (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
548                         relocations);
549         /*@=abstract@*/
550
551         /* XXX reference held by transaction set */
552         eiu->h = headerFree(eiu->h);
553         if (eiu->relocations)
554             eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
555
556         switch(rc) {
557         case 0:
558             rpmMessage(RPMMESS_DEBUG, "\tadded binary package [%d]\n",
559                         eiu->numRPMS);
560             /*@switchbreak@*/ break;
561         case 1:
562             rpmMessage(RPMMESS_ERROR,
563                             _("error reading from file %s\n"), *eiu->fnp);
564             eiu->numFailed++;
565             goto exit;
566             /*@notreached@*/ /*@switchbreak@*/ break;
567         case 2:
568             rpmMessage(RPMMESS_ERROR,
569                             _("file %s requires a newer version of RPM\n"),
570                             *eiu->fnp);
571             eiu->numFailed++;
572             goto exit;
573             /*@notreached@*/ /*@switchbreak@*/ break;
574         }
575
576         eiu->numRPMS++;
577         continue;
578
579 maybe_manifest:
580         /* Try to read a package manifest. */
581         eiu->fd = Fopen(*eiu->fnp, "r.fpio");
582         if (eiu->fd == NULL || Ferror(eiu->fd)) {
583             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *eiu->fnp,
584                         Fstrerror(eiu->fd));
585             if (eiu->fd) {
586                 xx = Fclose(eiu->fd);
587                 eiu->fd = NULL;
588             }
589             eiu->numFailed++; *eiu->fnp = NULL;
590             break;
591         }
592
593         /* Read list of packages from manifest. */
594         rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
595         if (rc != RPMRC_OK)
596             rpmError(RPMERR_MANIFEST, _("%s: not an rpm package (or package manifest): %s\n"),
597                         *eiu->fnp, Fstrerror(eiu->fd));
598         xx = Fclose(eiu->fd);
599         eiu->fd = NULL;
600
601         /* If successful, restart the query loop. */
602         if (rc == RPMRC_OK) {
603             eiu->prevx++;
604             goto restart;
605         }
606
607         eiu->numFailed++; *eiu->fnp = NULL;
608         break;
609     }
610
611     rpmMessage(RPMMESS_DEBUG, _("found %d source and %d binary packages\n"),
612                 eiu->numSRPMS, eiu->numRPMS);
613
614     if (eiu->numFailed) goto exit;
615
616     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
617
618         if (rpmtsCheck(ts)) {
619             eiu->numFailed = eiu->numPkgs;
620             stopInstall = 1;
621         }
622
623         ps = rpmtsProblems(ts);
624         if (!stopInstall && rpmpsNumProblems(ps) > 0) {
625             rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
626             rpmpsPrint(NULL, ps);
627             eiu->numFailed = eiu->numPkgs;
628             stopInstall = 1;
629
630             /*@-branchstate@*/
631             if (ts->suggests != NULL && ts->nsuggests > 0) {
632                 rpmMessage(RPMMESS_NORMAL, _("    Suggested resolutions:\n"));
633                 for (i = 0; i < ts->nsuggests; i++) {
634                     const char * str = ts->suggests[i];
635
636                     if (str == NULL)
637                         break;
638
639                     rpmMessage(RPMMESS_NORMAL, "\t%s\n", str);
640                 
641                     ts->suggests[i] = NULL;
642                     str = _free(str);
643                 }
644                 ts->suggests = _free(ts->suggests);
645             }
646             /*@=branchstate@*/
647         }
648         ps = rpmpsFree(ps);
649     }
650
651     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
652         if (rpmtsOrder(ts)) {
653             eiu->numFailed = eiu->numPkgs;
654             stopInstall = 1;
655         }
656     }
657
658     if (eiu->numRPMS && !stopInstall) {
659
660         rpmcliPackagesTotal += eiu->numSRPMS;
661
662         rpmMessage(RPMMESS_DEBUG, _("installing binary packages\n"));
663
664         /* Drop added/available package indices and dependency sets. */
665         rpmtsClean(ts);
666
667         rc = rpmtsRun(ts, NULL, probFilter);
668         ps = rpmtsProblems(ts);
669
670         if (rc < 0) {
671             eiu->numFailed += eiu->numRPMS;
672         } else if (rc > 0) {
673             eiu->numFailed += rc;
674             if (rpmpsNumProblems(ps) > 0)
675                 rpmpsPrint(stderr, ps);
676         }
677         ps = rpmpsFree(ps);
678     }
679
680     if (eiu->numSRPMS && !stopInstall) {
681         if (eiu->sourceURL != NULL)
682         for (i = 0; i < eiu->numSRPMS; i++) {
683             if (eiu->sourceURL[i] == NULL) continue;
684             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
685             if (eiu->fd == NULL || Ferror(eiu->fd)) {
686                 rpmMessage(RPMMESS_ERROR, _("cannot open file %s: %s\n"),
687                            eiu->sourceURL[i], Fstrerror(eiu->fd));
688                 if (eiu->fd) {
689                     xx = Fclose(eiu->fd);
690                     eiu->fd = NULL;
691                 }
692                 continue;
693             }
694
695             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
696                 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
697                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
698             }
699
700             xx = Fclose(eiu->fd);
701             eiu->fd = NULL;
702         }
703     }
704
705 exit:
706     if (eiu->pkgURL != NULL)
707     for (i = 0; i < eiu->numPkgs; i++) {
708         if (eiu->pkgURL[i] == NULL) continue;
709         if (eiu->pkgState[i] == 1)
710             (void) Unlink(eiu->pkgURL[i]);
711         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
712     }
713     eiu->pkgState = _free(eiu->pkgState);
714     eiu->pkgURL = _free(eiu->pkgURL);
715     eiu->argv = _free(eiu->argv);
716
717     rpmtsEmpty(ts);
718
719     return eiu->numFailed;
720 }
721 /*@=bounds@*/
722
723 int rpmErase(rpmts ts,
724                 const struct rpmInstallArguments_s * ia,
725                 const char ** argv)
726 {
727     int count;
728     const char ** arg;
729     int numFailed = 0;
730     int stopUninstall = 0;
731     int numPackages = 0;
732     rpmVSFlags vsflags, ovsflags;
733     rpmps ps;
734
735     if (argv == NULL) return 0;
736
737     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
738     if (ia->qva_flags & VERIFY_DIGEST)
739         vsflags |= _RPMVSF_NODIGESTS;
740     if (ia->qva_flags & VERIFY_SIGNATURE)
741         vsflags |= _RPMVSF_NOSIGNATURES;
742     if (ia->qva_flags & VERIFY_HDRCHK)
743         vsflags |= RPMVSF_NOHDRCHK;
744     ovsflags = rpmtsSetVSFlags(ts, vsflags);
745
746     (void) rpmtsSetFlags(ts, ia->transFlags);
747
748 #ifdef  NOTYET  /* XXX no callbacks on erase yet */
749     {   int notifyFlags;
750         notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
751         xx = rpmtsSetNotifyCallback(ts,
752                         rpmShowProgress, (void *) ((long)notifyFlags)
753     }
754 #endif
755
756     ts->goal = TSM_ERASE;
757
758     for (arg = argv; *arg; arg++) {
759         rpmdbMatchIterator mi;
760
761         /* XXX HACK to get rpmdbFindByLabel out of the API */
762         mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
763         count = rpmdbGetIteratorCount(mi);
764         if (count <= 0) {
765             rpmMessage(RPMMESS_ERROR, _("package %s is not installed\n"), *arg);
766             numFailed++;
767         } else if (!(count == 1 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
768             rpmMessage(RPMMESS_ERROR, _("\"%s\" specifies multiple packages\n"),
769                         *arg);
770             numFailed++;
771         } else {
772             Header h;   /* XXX iterator owns the reference */
773             while ((h = rpmdbNextIterator(mi)) != NULL) {
774                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
775                 if (recOffset) {
776                     (void) rpmtsAddEraseElement(ts, h, recOffset);
777                     numPackages++;
778                 }
779             }
780         }
781         mi = rpmdbFreeIterator(mi);
782     }
783
784     if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
785
786         if (rpmtsCheck(ts)) {
787             numFailed = numPackages;
788             stopUninstall = 1;
789         }
790
791         ps = rpmtsProblems(ts);
792         if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
793             rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
794             rpmpsPrint(NULL, ps);
795             numFailed += numPackages;
796             stopUninstall = 1;
797         }
798         ps = rpmpsFree(ps);
799     }
800
801     if (!stopUninstall) {
802         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
803
804         /* Drop added/available package indices and dependency sets. */
805         rpmtsClean(ts);
806
807         numPackages = rpmtsRun(ts, NULL, 0);
808         ps = rpmtsProblems(ts);
809         if (rpmpsNumProblems(ps) > 0)
810             rpmpsPrint(NULL, ps);
811         numFailed += numPackages;
812         stopUninstall = 1;
813         ps = rpmpsFree(ps);
814     }
815
816     rpmtsEmpty(ts);
817
818     return numFailed;
819 }
820
821 int rpmInstallSource(rpmts ts, const char * arg,
822                 const char ** specFilePtr, const char ** cookie)
823 {
824     FD_t fd;
825     int rc;
826
827
828     fd = Fopen(arg, "r.ufdio");
829     if (fd == NULL || Ferror(fd)) {
830         rpmMessage(RPMMESS_ERROR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
831         if (fd) (void) Fclose(fd);
832         return 1;
833     }
834
835     if (rpmIsVerbose())
836         fprintf(stdout, _("Installing %s\n"), arg);
837
838     {
839         rpmVSFlags ovsflags =
840                 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
841         rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
842         rc = (rpmrc == RPMRC_OK ? 0 : 1);
843         ovsflags = rpmtsSetVSFlags(ts, ovsflags);
844     }
845     if (rc != 0) {
846         rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), arg);
847         /*@-unqualifiedtrans@*/
848         if (specFilePtr && *specFilePtr)
849             *specFilePtr = _free(*specFilePtr);
850         if (cookie && *cookie)
851             *cookie = _free(*cookie);
852         /*@=unqualifiedtrans@*/
853     }
854
855     (void) Fclose(fd);
856
857     return rc;
858 }
859
860 /*@unchecked@*/
861 static int reverse = -1;
862
863 /**
864  */
865 static int IDTintcmp(const void * a, const void * b)
866         /*@*/
867 {
868     /*@-castexpose@*/
869     return ( reverse * (((IDT)a)->val.u32 - ((IDT)b)->val.u32) );
870     /*@=castexpose@*/
871 }
872
873 IDTX IDTXfree(IDTX idtx)
874 {
875     if (idtx) {
876         int i;
877         if (idtx->idt)
878         for (i = 0; i < idtx->nidt; i++) {
879             IDT idt = idtx->idt + i;
880             idt->h = headerFree(idt->h);
881             idt->key = _free(idt->key);
882         }
883         idtx->idt = _free(idtx->idt);
884         idtx = _free(idtx);
885     }
886     return NULL;
887 }
888
889 IDTX IDTXnew(void)
890 {
891     IDTX idtx = xcalloc(1, sizeof(*idtx));
892     idtx->delta = 10;
893     idtx->size = sizeof(*((IDT)0));
894     return idtx;
895 }
896
897 IDTX IDTXgrow(IDTX idtx, int need)
898 {
899     if (need < 0) return NULL;
900     if (idtx == NULL)
901         idtx = IDTXnew();
902     if (need == 0) return idtx;
903
904     if ((idtx->nidt + need) > idtx->alloced) {
905         while (need > 0) {
906             idtx->alloced += idtx->delta;
907             need -= idtx->delta;
908         }
909         idtx->idt = xrealloc(idtx->idt, (idtx->alloced * idtx->size) );
910     }
911     return idtx;
912 }
913
914 IDTX IDTXsort(IDTX idtx)
915 {
916     if (idtx != NULL && idtx->idt != NULL && idtx->nidt > 0)
917         qsort(idtx->idt, idtx->nidt, idtx->size, IDTintcmp);
918     return idtx;
919 }
920
921 IDTX IDTXload(rpmts ts, rpmTag tag)
922 {
923     IDTX idtx = NULL;
924     rpmdbMatchIterator mi;
925     HGE_t hge = (HGE_t) headerGetEntry;
926     Header h;
927
928     /*@-branchstate@*/
929     mi = rpmtsInitIterator(ts, tag, NULL, 0);
930     while ((h = rpmdbNextIterator(mi)) != NULL) {
931         rpmTagType type = RPM_NULL_TYPE;
932         int_32 count = 0;
933         int_32 * tidp;
934
935         tidp = NULL;
936         if (!hge(h, tag, &type, (void **)&tidp, &count) || tidp == NULL)
937             continue;
938
939         if (type == RPM_INT32_TYPE && (*tidp == 0 || *tidp == -1))
940             continue;
941
942         idtx = IDTXgrow(idtx, 1);
943         if (idtx == NULL)
944             continue;
945         if (idtx->idt == NULL)
946             continue;
947
948         {   IDT idt;
949             /*@-nullderef@*/
950             idt = idtx->idt + idtx->nidt;
951             /*@=nullderef@*/
952             idt->h = headerLink(h);
953             idt->key = NULL;
954             idt->instance = rpmdbGetIteratorOffset(mi);
955             idt->val.u32 = *tidp;
956         }
957         idtx->nidt++;
958     }
959     mi = rpmdbFreeIterator(mi);
960     /*@=branchstate@*/
961
962     return IDTXsort(idtx);
963 }
964
965 IDTX IDTXglob(rpmts ts, const char * globstr, rpmTag tag)
966 {
967     IDTX idtx = NULL;
968     HGE_t hge = (HGE_t) headerGetEntry;
969     Header h;
970     int_32 * tidp;
971     FD_t fd;
972     const char ** av = NULL;
973     int ac = 0;
974     rpmRC rpmrc;
975     int xx;
976     int i;
977
978     av = NULL;  ac = 0;
979     xx = rpmGlob(globstr, &ac, &av);
980
981     if (xx == 0)
982     for (i = 0; i < ac; i++) {
983         rpmTagType type;
984         int_32 count;
985         int isSource;
986
987         fd = Fopen(av[i], "r.ufdio");
988         if (fd == NULL || Ferror(fd)) {
989             rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), av[i],
990                         Fstrerror(fd));
991             if (fd) (void) Fclose(fd);
992             continue;
993         }
994
995         rpmrc = rpmReadPackageFile(ts, fd, av[i], &h);
996         (void) Fclose(fd);
997         switch (rpmrc) {
998         default:
999             goto bottom;
1000             /*@notreached@*/ /*@switchbreak@*/ break;
1001         case RPMRC_NOTTRUSTED:
1002         case RPMRC_NOKEY:
1003         case RPMRC_OK:
1004             isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
1005             if (isSource)
1006                 goto bottom;
1007             /*@switchbreak@*/ break;
1008         }
1009
1010         tidp = NULL;
1011         /*@-branchstate@*/
1012         if (hge(h, tag, &type, (void **) &tidp, &count) && tidp) {
1013
1014             idtx = IDTXgrow(idtx, 1);
1015             if (idtx == NULL || idtx->idt == NULL)
1016                 goto bottom;
1017
1018             {   IDT idt;
1019                 idt = idtx->idt + idtx->nidt;
1020                 idt->h = headerLink(h);
1021                 idt->key = av[i];
1022                 av[i] = NULL;
1023                 idt->instance = 0;
1024                 idt->val.u32 = *tidp;
1025             }
1026             idtx->nidt++;
1027         }
1028         /*@=branchstate@*/
1029 bottom:
1030         h = headerFree(h);
1031     }
1032
1033     for (i = 0; i < ac; i++)
1034         av[i] = _free(av[i]);
1035     av = _free(av);     ac = 0;
1036
1037     return idtx;
1038 }
1039
1040 /** @todo Transaction handling, more, needs work. */
1041 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
1042 {
1043     int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
1044     unsigned thistid = 0xffffffff;
1045     unsigned prevtid;
1046     time_t tid;
1047     IDTX itids = NULL;
1048     IDTX rtids = NULL;
1049     IDT rp;
1050     int nrids = 0;
1051     IDT ip;
1052     int niids = 0;
1053     int rc = 0;
1054     int vsflags, ovsflags;
1055     int numAdded;
1056     int numRemoved;
1057     rpmps ps;
1058     int _unsafe_rollbacks = 0;
1059     rpmtransFlags transFlags = ia->transFlags;
1060
1061     if (argv != NULL && *argv != NULL) {
1062         rc = -1;
1063         goto exit;
1064     }
1065
1066     _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
1067
1068     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
1069     if (ia->qva_flags & VERIFY_DIGEST)
1070         vsflags |= _RPMVSF_NODIGESTS;
1071     if (ia->qva_flags & VERIFY_SIGNATURE)
1072         vsflags |= _RPMVSF_NOSIGNATURES;
1073     if (ia->qva_flags & VERIFY_HDRCHK)
1074         vsflags |= RPMVSF_NOHDRCHK;
1075     vsflags |= RPMVSF_NEEDPAYLOAD;      /* XXX no legacy signatures */
1076     ovsflags = rpmtsSetVSFlags(ts, vsflags);
1077
1078     (void) rpmtsSetFlags(ts, transFlags);
1079
1080     itids = IDTXload(ts, RPMTAG_INSTALLTID);
1081     if (itids != NULL) {
1082         ip = itids->idt;
1083         niids = itids->nidt;
1084     } else {
1085         ip = NULL;
1086         niids = 0;
1087     }
1088
1089     {   const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
1090         if (globstr == NULL || *globstr == '%') {
1091             globstr = _free(globstr);
1092             rc = -1;
1093             goto exit;
1094         }
1095         rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
1096         if (rtids != NULL) {
1097             rp = rtids->idt;
1098             nrids = rtids->nidt;
1099         } else {
1100             rp = NULL;
1101             nrids = 0;
1102         }
1103         globstr = _free(globstr);
1104     }
1105
1106     {   int notifyFlags, xx;
1107         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
1108         xx = rpmtsSetNotifyCallback(ts,
1109                         rpmShowProgress, (void *) ((long)notifyFlags));
1110     }
1111
1112     /* Run transactions until rollback goal is achieved. */
1113     do {
1114         prevtid = thistid;
1115         rc = 0;
1116         rpmcliPackagesTotal = 0;
1117         numAdded = 0;
1118         numRemoved = 0;
1119         ia->installInterfaceFlags &= ~ifmask;
1120
1121         /* Find larger of the remaining install/erase transaction id's. */
1122         thistid = 0;
1123         if (ip != NULL && ip->val.u32 > thistid)
1124             thistid = ip->val.u32;
1125         if (rp != NULL && rp->val.u32 > thistid)
1126             thistid = rp->val.u32;
1127
1128         /* If we've achieved the rollback goal, then we're done. */
1129         if (thistid == 0 || thistid < ia->rbtid)
1130             break;
1131
1132         rpmtsEmpty(ts);
1133         (void) rpmtsSetFlags(ts, transFlags);
1134
1135         /* Install the previously erased packages for this transaction. */
1136         while (rp != NULL && rp->val.u32 == thistid) {
1137
1138             rpmMessage(RPMMESS_DEBUG, "\t+++ install %s\n",
1139                         (rp->key ? rp->key : "???"));
1140
1141 /*@-abstract@*/
1142             rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
1143                                0, ia->relocations);
1144 /*@=abstract@*/
1145             if (rc != 0)
1146                 goto exit;
1147
1148             numAdded++;
1149             rpmcliPackagesTotal++;
1150             if (!(ia->installInterfaceFlags & ifmask))
1151                 ia->installInterfaceFlags |= INSTALL_UPGRADE;
1152
1153 #ifdef  NOTYET
1154             rp->h = headerFree(rp->h);
1155 #endif
1156             nrids--;
1157             if (nrids > 0)
1158                 rp++;
1159             else
1160                 rp = NULL;
1161         }
1162
1163         /* Erase the previously installed packages for this transaction. */
1164         while (ip != NULL && ip->val.u32 == thistid) {
1165
1166             rpmMessage(RPMMESS_DEBUG,
1167                         "\t--- erase h#%u\n", ip->instance);
1168
1169             rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
1170             if (rc != 0)
1171                 goto exit;
1172
1173             numRemoved++;
1174
1175             if (_unsafe_rollbacks)
1176                 rpmcliPackagesTotal++;
1177
1178             if (!(ia->installInterfaceFlags & ifmask)) {
1179                 ia->installInterfaceFlags |= INSTALL_ERASE;
1180                 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
1181             }
1182
1183 #ifdef  NOTYET
1184             ip->instance = 0;
1185 #endif
1186             niids--;
1187             if (niids > 0)
1188                 ip++;
1189             else
1190                 ip = NULL;
1191         }
1192
1193         /* Anything to do? */
1194         if (rpmcliPackagesTotal <= 0)
1195             break;
1196
1197         tid = (time_t)thistid;
1198         rpmMessage(RPMMESS_NORMAL,
1199                 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1200                         numAdded, numRemoved, ctime(&tid), tid);
1201
1202         rc = rpmtsCheck(ts);
1203         ps = rpmtsProblems(ts);
1204         if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1205             rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
1206             rpmpsPrint(NULL, ps);
1207             ps = rpmpsFree(ps);
1208             goto exit;
1209         }
1210         ps = rpmpsFree(ps);
1211
1212         rc = rpmtsOrder(ts);
1213         if (rc != 0)
1214             goto exit;
1215
1216         /* Drop added/available package indices and dependency sets. */
1217         rpmtsClean(ts);
1218
1219         rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1220         ps = rpmtsProblems(ts);
1221         if (rc > 0 && rpmpsNumProblems(ps) > 0)
1222             rpmpsPrint(stderr, ps);
1223         ps = rpmpsFree(ps);
1224         if (rc)
1225             goto exit;
1226
1227         /* Clean up after successful rollback. */
1228         if (rtids && !rpmIsDebug()) {
1229             int i;
1230             if (rtids->idt)
1231             for (i = 0; i < rtids->nidt; i++) {
1232                 IDT rrp = rtids->idt + i;
1233                 if (rrp->val.u32 != thistid)
1234                     /*@innercontinue@*/ continue;
1235                 if (rrp->key)   /* XXX can't happen */
1236                     (void) unlink(rrp->key);
1237             }
1238         }
1239
1240
1241     } while (1);
1242
1243 exit:
1244     rtids = IDTXfree(rtids);
1245     itids = IDTXfree(itids);
1246
1247     rpmtsEmpty(ts);
1248     (void) rpmtsSetFlags(ts, transFlags);
1249
1250     return rc;
1251 }