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