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