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