Replace all uses of RPMMESS_ERROR with RPMLOG_ERR
[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 "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(RPMMESS_NORMAL, _("    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(RPMMESS_NORMAL, "\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             if (eiu->sourceURL[i] == NULL) continue;
654             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
655             if (eiu->fd == NULL || Ferror(eiu->fd)) {
656                 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
657                            eiu->sourceURL[i], Fstrerror(eiu->fd));
658                 if (eiu->fd != NULL) {
659                     xx = Fclose(eiu->fd);
660                     eiu->fd = NULL;
661                 }
662                 continue;
663             }
664
665             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
666                 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
667                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
668             }
669
670             xx = Fclose(eiu->fd);
671             eiu->fd = NULL;
672         }
673     }
674
675 exit:
676     if (eiu->pkgURL != NULL)
677     for (i = 0; i < eiu->numPkgs; i++) {
678         if (eiu->pkgURL[i] == NULL) continue;
679         if (eiu->pkgState[i] == 1)
680             (void) Unlink(eiu->pkgURL[i]);
681         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
682     }
683     eiu->pkgState = _free(eiu->pkgState);
684     eiu->pkgURL = _free(eiu->pkgURL);
685     eiu->argv = _free(eiu->argv);
686
687     rpmtsEmpty(ts);
688
689     return eiu->numFailed;
690 }
691
692 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
693                 const char ** argv)
694 {
695     int count;
696     const char ** arg;
697     int numFailed = 0;
698     int stopUninstall = 0;
699     int numPackages = 0;
700     rpmVSFlags vsflags, ovsflags;
701     rpmps ps;
702
703     if (argv == NULL) return 0;
704
705     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
706     if (ia->qva_flags & VERIFY_DIGEST)
707         vsflags |= _RPMVSF_NODIGESTS;
708     if (ia->qva_flags & VERIFY_SIGNATURE)
709         vsflags |= _RPMVSF_NOSIGNATURES;
710     if (ia->qva_flags & VERIFY_HDRCHK)
711         vsflags |= RPMVSF_NOHDRCHK;
712     ovsflags = rpmtsSetVSFlags(ts, vsflags);
713
714     if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
715         ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
716
717     (void) rpmtsSetFlags(ts, ia->transFlags);
718
719 #ifdef  NOTYET  /* XXX no callbacks on erase yet */
720     {   int notifyFlags;
721         notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
722         xx = rpmtsSetNotifyCallback(ts,
723                         rpmShowProgress, (void *) ((long)notifyFlags)
724     }
725 #endif
726
727     ts->goal = TSM_ERASE;
728
729     for (arg = argv; *arg; arg++) {
730         rpmdbMatchIterator mi;
731
732         /* XXX HACK to get rpmdbFindByLabel out of the API */
733         mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
734         if (mi == NULL) {
735             rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
736             numFailed++;
737         } else {
738             Header h;   /* XXX iterator owns the reference */
739             count = 0;
740             while ((h = rpmdbNextIterator(mi)) != NULL) {
741                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
742
743                 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
744                     rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
745                         *arg);
746                     numFailed++;
747                     break;
748                 }
749                 if (recOffset) {
750                     (void) rpmtsAddEraseElement(ts, h, recOffset);
751                     numPackages++;
752                 }
753             }
754         }
755         mi = rpmdbFreeIterator(mi);
756     }
757
758     if (numFailed) goto exit;
759
760     if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
761
762         if (rpmtsCheck(ts)) {
763             numFailed = numPackages;
764             stopUninstall = 1;
765         }
766
767         ps = rpmtsProblems(ts);
768         if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
769             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
770             rpmpsPrint(NULL, ps);
771             numFailed += numPackages;
772             stopUninstall = 1;
773         }
774         ps = rpmpsFree(ps);
775     }
776
777     if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
778         if (rpmtsOrder(ts)) {
779             numFailed += numPackages;
780             stopUninstall = 1;
781         }
782     }
783
784     if (numPackages && !stopUninstall) {
785         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
786
787         /* Drop added/available package indices and dependency sets. */
788         rpmtsClean(ts);
789
790         numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
791         ps = rpmtsProblems(ts);
792         if (rpmpsNumProblems(ps) > 0)
793             rpmpsPrint(NULL, ps);
794         numFailed += numPackages;
795         stopUninstall = 1;
796         ps = rpmpsFree(ps);
797     }
798
799 exit:
800     rpmtsEmpty(ts);
801
802     return numFailed;
803 }
804
805 int rpmInstallSource(rpmts ts, const char * arg,
806                 const char ** specFilePtr, const char ** cookie)
807 {
808     FD_t fd;
809     int rc;
810
811
812     fd = Fopen(arg, "r.ufdio");
813     if (fd == NULL || Ferror(fd)) {
814         rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
815         if (fd != NULL) (void) Fclose(fd);
816         return 1;
817     }
818
819     if (rpmIsVerbose())
820         fprintf(stdout, _("Installing %s\n"), arg);
821
822     {
823         rpmVSFlags ovsflags =
824                 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
825         rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
826         rc = (rpmrc == RPMRC_OK ? 0 : 1);
827         ovsflags = rpmtsSetVSFlags(ts, ovsflags);
828     }
829     if (rc != 0) {
830         rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
831         if (specFilePtr && *specFilePtr)
832             *specFilePtr = _free(*specFilePtr);
833         if (cookie && *cookie)
834             *cookie = _free(*cookie);
835     }
836
837     (void) Fclose(fd);
838
839     return rc;
840 }
841
842 /** @todo Transaction handling, more, needs work. */
843 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
844 {
845     int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
846     unsigned thistid = 0xffffffff;
847     unsigned prevtid;
848     time_t tid;
849     IDTX itids = NULL;
850     IDTX rtids = NULL;
851     IDT rp;
852     int nrids = 0;
853     IDT ip;
854     int niids = 0;
855     int rc = 0;
856     int vsflags, ovsflags;
857     int numAdded;
858     int numRemoved;
859     rpmps ps;
860     int _unsafe_rollbacks = 0;
861     rpmtransFlags transFlags = ia->transFlags;
862
863     if (argv != NULL && *argv != NULL) {
864         rc = -1;
865         goto exit;
866     }
867
868     _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
869
870     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
871     if (ia->qva_flags & VERIFY_DIGEST)
872         vsflags |= _RPMVSF_NODIGESTS;
873     if (ia->qva_flags & VERIFY_SIGNATURE)
874         vsflags |= _RPMVSF_NOSIGNATURES;
875     if (ia->qva_flags & VERIFY_HDRCHK)
876         vsflags |= RPMVSF_NOHDRCHK;
877     vsflags |= RPMVSF_NEEDPAYLOAD;      /* XXX no legacy signatures */
878     ovsflags = rpmtsSetVSFlags(ts, vsflags);
879
880     (void) rpmtsSetFlags(ts, transFlags);
881
882     /*  Make the transaction a rollback transaction.  In a rollback
883      *  a best effort is what we want 
884      */
885     rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
886
887     itids = IDTXload(ts, RPMTAG_INSTALLTID);
888     if (itids != NULL) {
889         ip = itids->idt;
890         niids = itids->nidt;
891     } else {
892         ip = NULL;
893         niids = 0;
894     }
895
896     {   const char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
897         if (globstr == NULL || *globstr == '%') {
898             globstr = _free(globstr);
899             rc = -1;
900             goto exit;
901         }
902         rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
903
904         if (rtids != NULL) {
905             rp = rtids->idt;
906             nrids = rtids->nidt;
907         } else {
908             rp = NULL;
909             nrids = 0;
910         }
911         globstr = _free(globstr);
912     }
913
914     {   int notifyFlags, xx;
915         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
916         xx = rpmtsSetNotifyCallback(ts,
917                         rpmShowProgress, (void *) ((long)notifyFlags));
918     }
919
920     /* Run transactions until rollback goal is achieved. */
921     do {
922         prevtid = thistid;
923         rc = 0;
924         rpmcliPackagesTotal = 0;
925         numAdded = 0;
926         numRemoved = 0;
927         ia->installInterfaceFlags &= ~ifmask;
928
929         /* Find larger of the remaining install/erase transaction id's. */
930         thistid = 0;
931         if (ip != NULL && ip->val.u32 > thistid)
932             thistid = ip->val.u32;
933         if (rp != NULL && rp->val.u32 > thistid)
934             thistid = rp->val.u32;
935
936         /* If we've achieved the rollback goal, then we're done. */
937         if (thistid == 0 || thistid < ia->rbtid)
938             break;
939
940         /* If we've reached the (configured) rollback goal, then we're done. */
941         if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
942             break;
943
944         rpmtsEmpty(ts);
945         (void) rpmtsSetFlags(ts, transFlags);
946
947         /* Install the previously erased packages for this transaction. */
948         while (rp != NULL && rp->val.u32 == thistid) {
949
950             rpmlog(RPMLOG_DEBUG, "\t+++ install %s\n",
951                         (rp->key ? rp->key : "???"));
952
953             rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
954                                0, ia->relocations);
955             if (rc != 0)
956                 goto exit;
957
958             numAdded++;
959             rpmcliPackagesTotal++;
960             if (!(ia->installInterfaceFlags & ifmask))
961                 ia->installInterfaceFlags |= INSTALL_UPGRADE;
962
963 #ifdef  NOTYET
964             rp->h = headerFree(rp->h);
965 #endif
966             nrids--;
967             if (nrids > 0)
968                 rp++;
969             else
970                 rp = NULL;
971         }
972
973         /* Erase the previously installed packages for this transaction. */
974         while (ip != NULL && ip->val.u32 == thistid) {
975
976             rpmlog(RPMLOG_DEBUG,
977                         "\t--- erase h#%u\n", ip->instance);
978
979             rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
980             if (rc != 0)
981                 goto exit;
982
983             numRemoved++;
984
985             if (_unsafe_rollbacks)
986                 rpmcliPackagesTotal++;
987
988             if (!(ia->installInterfaceFlags & ifmask)) {
989                 ia->installInterfaceFlags |= INSTALL_ERASE;
990                 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
991             }
992
993 #ifdef  NOTYET
994             ip->instance = 0;
995 #endif
996             niids--;
997             if (niids > 0)
998                 ip++;
999             else
1000                 ip = NULL;
1001         }
1002
1003         /* Anything to do? */
1004         if (rpmcliPackagesTotal <= 0)
1005             break;
1006
1007         tid = (time_t)thistid;
1008         rpmlog(RPMMESS_NORMAL,
1009                 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1010                         numAdded, numRemoved, ctime(&tid), tid);
1011
1012         rc = rpmtsCheck(ts);
1013         ps = rpmtsProblems(ts);
1014         if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1015             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
1016             rpmpsPrint(NULL, ps);
1017             ps = rpmpsFree(ps);
1018             goto exit;
1019         }
1020         ps = rpmpsFree(ps);
1021
1022         rc = rpmtsOrder(ts);
1023         if (rc != 0)
1024             goto exit;
1025
1026         /* Drop added/available package indices and dependency sets. */
1027         rpmtsClean(ts);
1028
1029         rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1030         ps = rpmtsProblems(ts);
1031         if (rc > 0 && rpmpsNumProblems(ps) > 0)
1032             rpmpsPrint(stderr, ps);
1033         ps = rpmpsFree(ps);
1034         if (rc)
1035             goto exit;
1036
1037         /* Clean up after successful rollback. */
1038         if (rtids && !rpmIsDebug()) {
1039             int i;
1040             rpmlog(RPMMESS_NORMAL, _("Cleaning up repackaged packages:\n"));
1041             if (rtids->idt)
1042             for (i = 0; i < rtids->nidt; i++) {
1043                 IDT rrp = rtids->idt + i;
1044                 if (rrp->val.u32 != thistid)
1045                     continue;
1046                 if (rrp->key) { /* XXX can't happen */
1047                     rpmlog(RPMMESS_NORMAL, _("\tRemoving %s:\n"), rrp->key);
1048                     (void) unlink(rrp->key);    /* XXX: Should check rc??? */
1049                 }
1050             }
1051         }
1052
1053
1054     } while (1);
1055
1056 exit:
1057     rtids = IDTXfree(rtids);
1058     itids = IDTXfree(itids);
1059
1060     rpmtsEmpty(ts);
1061     (void) rpmtsSetFlags(ts, transFlags);
1062
1063     return rc;
1064 }