Remove repackage support to finish off with rollback
[platform/upstream/rpm.git] / lib / rpminstall.c
1 /** \ingroup rpmcli
2  * \file lib/rpminstall.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmcli.h>
8 #include <rpm/rpmtag.h>
9 #include <rpm/rpmlib.h>         /* rpmReadPackageFile, vercmp etc */
10 #include <rpm/rpmdb.h>
11 #include <rpm/rpmds.h>
12 #include <rpm/rpmts.h>
13 #include <rpm/rpmlog.h>
14 #include <rpm/rpmfileutil.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 rpm_off_t amount, const rpm_off_t 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 rpm_off_t amount,
77                         const rpm_off_t 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_UNINST_PROGRESS:
171         break;
172     case RPMCALLBACK_UNINST_START:
173         break;
174     case RPMCALLBACK_UNINST_STOP:
175         break;
176     case RPMCALLBACK_UNPACK_ERROR:
177         break;
178     case RPMCALLBACK_CPIO_ERROR:
179         break;
180     case RPMCALLBACK_SCRIPT_ERROR:
181         break;
182     case RPMCALLBACK_UNKNOWN:
183     default:
184         break;
185     }
186
187     return rc;
188 }       
189
190 typedef const char * str_t;
191
192 struct rpmEIU {
193     Header h;
194     FD_t fd;
195     int numFailed;
196     int numPkgs;
197     char ** pkgURL;
198     str_t * fnp;
199     char * pkgState;
200     int prevx;
201     int pkgx;
202     int numRPMS;
203     int numSRPMS;
204     str_t * sourceURL;
205     int isSource;
206     int argc;
207     char ** argv;
208     rpmRelocation * relocations;
209     rpmRC rpmrc;
210 };
211
212 /** @todo Generalize --freshen policies. */
213 int rpmInstall(rpmts ts,
214                 struct rpmInstallArguments_s * ia,
215                 const char ** fileArgv)
216 {
217     struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
218     rpmps ps;
219     rpmprobFilterFlags probFilter;
220     rpmRelocation * relocations;
221     char * fileURL = NULL;
222     int stopInstall = 0;
223     char ** av = NULL;
224     rpmVSFlags vsflags, ovsflags, tvsflags;
225     int ac = 0;
226     int rc;
227     int xx;
228     int i;
229
230     if (fileArgv == NULL) goto exit;
231
232     rpmcliPackagesTotal = 0;
233
234     (void) rpmtsSetFlags(ts, ia->transFlags);
235
236     probFilter = ia->probFilter;
237     relocations = ia->relocations;
238
239     if (ia->installInterfaceFlags & INSTALL_UPGRADE)
240         vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
241     else
242         vsflags = rpmExpandNumeric("%{?_vsflags_install}");
243     if (ia->qva_flags & VERIFY_DIGEST)
244         vsflags |= _RPMVSF_NODIGESTS;
245     if (ia->qva_flags & VERIFY_SIGNATURE)
246         vsflags |= _RPMVSF_NOSIGNATURES;
247     if (ia->qva_flags & VERIFY_HDRCHK)
248         vsflags |= RPMVSF_NOHDRCHK;
249     ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
250
251     {   int notifyFlags;
252         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
253         xx = rpmtsSetNotifyCallback(ts,
254                         rpmShowProgress, (void *) ((long)notifyFlags));
255     }
256
257     if ((eiu->relocations = relocations) != NULL) {
258         while (eiu->relocations->oldPath)
259             eiu->relocations++;
260         if (eiu->relocations->newPath == NULL)
261             eiu->relocations = NULL;
262     }
263
264     /* Build fully globbed list of arguments in argv[argc]. */
265     for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
266         char * fn;
267         av = _free(av); ac = 0;
268         fn = rpmEscapeSpaces(*eiu->fnp);
269         rc = rpmGlob(fn, &ac, &av);
270         fn = _free(fn);
271         if (rc || ac == 0) {
272             rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
273             continue;
274         }
275
276         eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
277         memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
278         eiu->argc += ac;
279         eiu->argv[eiu->argc] = NULL;
280     }
281     av = _free(av);     ac = 0;
282
283 restart:
284     /* Allocate sufficient storage for next set of args. */
285     if (eiu->pkgx >= eiu->numPkgs) {
286         eiu->numPkgs = eiu->pkgx + eiu->argc;
287         eiu->pkgURL = xrealloc(eiu->pkgURL,
288                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
289         memset(eiu->pkgURL + eiu->pkgx, 0,
290                         ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
291         eiu->pkgState = xrealloc(eiu->pkgState,
292                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
293         memset(eiu->pkgState + eiu->pkgx, 0,
294                         ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
295     }
296
297     /* Retrieve next set of args, cache on local storage. */
298     for (i = 0; i < eiu->argc; i++) {
299         fileURL = _free(fileURL);
300         fileURL = eiu->argv[i];
301         eiu->argv[i] = NULL;
302
303 #ifdef  NOTYET
304 if (fileURL[0] == '=') {
305     rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
306
307     xx = rpmtsSolve(ts, this, NULL);
308     if (ts->suggests && ts->nsuggests > 0) {
309         fileURL = _free(fileURL);
310         fileURL = ts->suggests[0];
311         ts->suggests[0] = NULL;
312         while (ts->nsuggests-- > 0) {
313             if (ts->suggests[ts->nsuggests] == NULL)
314                 continue;
315             ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
316         }
317         ts->suggests = _free(ts->suggests);
318         rpmlog(RPMLOG_DEBUG, "Adding goal: %s\n", fileURL);
319         eiu->pkgURL[eiu->pkgx] = fileURL;
320         fileURL = NULL;
321         eiu->pkgx++;
322     }
323     this = rpmdsFree(this);
324 } else
325 #endif
326
327         switch (urlIsURL(fileURL)) {
328         case URL_IS_HTTPS:
329         case URL_IS_HTTP:
330         case URL_IS_FTP:
331         {   char *tfn;
332
333             if (rpmIsVerbose())
334                 fprintf(stdout, _("Retrieving %s\n"), fileURL);
335
336             {   char tfnbuf[64];
337                 const char * rootDir = rpmtsRootDir(ts);
338                 if (!(rootDir && * rootDir))
339                     rootDir = "";
340                 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
341 #if defined(HAVE_MKSTEMP)
342                 (void) close(mkstemp(tfnbuf));
343 #else
344                 (void) mktemp(tfnbuf);
345 #endif
346                 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
347             }
348
349             /* XXX undefined %{name}/%{version}/%{release} here */
350             /* XXX %{_tmpdir} does not exist */
351             rpmlog(RPMLOG_DEBUG, " ... as %s\n", tfn);
352             rc = urlGetFile(fileURL, tfn);
353             if (rc < 0) {
354                 rpmlog(RPMLOG_ERR,
355                         _("skipping %s - transfer failed - %s\n"),
356                         fileURL, ftpStrerror(rc));
357                 eiu->numFailed++;
358                 eiu->pkgURL[eiu->pkgx] = NULL;
359                 tfn = _free(tfn);
360                 break;
361             }
362             eiu->pkgState[eiu->pkgx] = 1;
363             eiu->pkgURL[eiu->pkgx] = tfn;
364             eiu->pkgx++;
365         }   break;
366         case URL_IS_PATH:
367         case URL_IS_DASH:       /* WRONG WRONG WRONG */
368         case URL_IS_HKP:        /* WRONG WRONG WRONG */
369         default:
370             eiu->pkgURL[eiu->pkgx] = fileURL;
371             fileURL = NULL;
372             eiu->pkgx++;
373             break;
374         }
375     }
376     fileURL = _free(fileURL);
377
378     if (eiu->numFailed) goto exit;
379
380     /* Continue processing file arguments, building transaction set. */
381     for (eiu->fnp = (const char**) eiu->pkgURL+eiu->prevx;
382          *eiu->fnp != NULL;
383          eiu->fnp++, eiu->prevx++)
384     {
385         const char * fileName;
386
387         rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
388         (void) urlPath(*eiu->fnp, &fileName);
389
390         /* Try to read the header from a package file. */
391         eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
392         if (eiu->fd == NULL || Ferror(eiu->fd)) {
393             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
394                         Fstrerror(eiu->fd));
395             if (eiu->fd != NULL) {
396                 xx = Fclose(eiu->fd);
397                 eiu->fd = NULL;
398             }
399             eiu->numFailed++; *eiu->fnp = NULL;
400             continue;
401         }
402
403         /* Read the header, verifying signatures (if present). */
404         tvsflags = rpmtsSetVSFlags(ts, vsflags);
405         eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
406         tvsflags = rpmtsSetVSFlags(ts, tvsflags);
407         xx = Fclose(eiu->fd);
408         eiu->fd = NULL;
409
410         switch (eiu->rpmrc) {
411         case RPMRC_FAIL:
412             rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
413             eiu->numFailed++; *eiu->fnp = NULL;
414             continue;
415             break;
416         case RPMRC_NOTFOUND:
417             goto maybe_manifest;
418             break;
419         case RPMRC_NOTTRUSTED:
420         case RPMRC_NOKEY:
421         case RPMRC_OK:
422         default:
423             break;
424         }
425
426         eiu->isSource = headerIsSource(eiu->h);
427
428         if (eiu->isSource) {
429             rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
430                 eiu->numSRPMS);
431             eiu->sourceURL = xrealloc(eiu->sourceURL,
432                                 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
433             eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
434             *eiu->fnp = NULL;
435             eiu->numSRPMS++;
436             eiu->sourceURL[eiu->numSRPMS] = NULL;
437             continue;
438         }
439
440         if (eiu->relocations) {
441             const char ** paths;
442             rpmTagType pft;
443             rpm_count_t c;
444
445             if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
446                                        (rpm_data_t *) &paths, &c) && (c == 1))
447             {
448                 eiu->relocations->oldPath = xstrdup(paths[0]);
449                 paths = headerFreeData(paths, pft);
450             } else {
451                 const char * name;
452                 xx = headerNVR(eiu->h, &name, NULL, NULL);
453                 rpmlog(RPMLOG_ERR,
454                                _("package %s is not relocatable\n"), name);
455                 eiu->numFailed++;
456                 goto exit;
457             }
458         }
459
460         /* On --freshen, verify package is installed and newer */
461         if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
462             rpmdbMatchIterator mi;
463             const char * name;
464             Header oldH;
465             int count;
466
467             xx = headerNVR(eiu->h, &name, NULL, NULL);
468             mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
469             count = rpmdbGetIteratorCount(mi);
470             while ((oldH = rpmdbNextIterator(mi)) != NULL) {
471                 if (rpmVersionCompare(oldH, eiu->h) < 0)
472                     continue;
473                 /* same or newer package already installed */
474                 count = 0;
475                 break;
476             }
477             mi = rpmdbFreeIterator(mi);
478             if (count == 0) {
479                 eiu->h = headerFree(eiu->h);
480                 continue;
481             }
482             /* Package is newer than those currently installed. */
483         }
484
485         rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
486                         (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
487                         relocations);
488
489         /* XXX reference held by transaction set */
490         eiu->h = headerFree(eiu->h);
491         if (eiu->relocations)
492             eiu->relocations->oldPath = _constfree(eiu->relocations->oldPath);
493
494         switch(rc) {
495         case 0:
496             rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
497                         eiu->numRPMS);
498             break;
499         case 1:
500             rpmlog(RPMLOG_ERR,
501                             _("error reading from file %s\n"), *eiu->fnp);
502             eiu->numFailed++;
503             goto exit;
504             break;
505         case 2:
506             rpmlog(RPMLOG_ERR,
507                             _("file %s requires a newer version of RPM\n"),
508                             *eiu->fnp);
509             eiu->numFailed++;
510             goto exit;
511             break;
512         default:
513             eiu->numFailed++;
514             goto exit;
515             break;
516         }
517
518         eiu->numRPMS++;
519         continue;
520
521 maybe_manifest:
522         /* Try to read a package manifest. */
523         eiu->fd = Fopen(*eiu->fnp, "r.fpio");
524         if (eiu->fd == NULL || Ferror(eiu->fd)) {
525             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
526                         Fstrerror(eiu->fd));
527             if (eiu->fd != NULL) {
528                 xx = Fclose(eiu->fd);
529                 eiu->fd = NULL;
530             }
531             eiu->numFailed++; *eiu->fnp = NULL;
532             break;
533         }
534
535         /* Read list of packages from manifest. */
536 /* FIX: *eiu->argv can be NULL */
537         rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
538         if (rc != RPMRC_OK)
539             rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
540                         *eiu->fnp, Fstrerror(eiu->fd));
541         xx = Fclose(eiu->fd);
542         eiu->fd = NULL;
543
544         /* If successful, restart the query loop. */
545         if (rc == RPMRC_OK) {
546             eiu->prevx++;
547             goto restart;
548         }
549
550         eiu->numFailed++; *eiu->fnp = NULL;
551         break;
552     }
553
554     rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
555                 eiu->numSRPMS, eiu->numRPMS);
556
557     if (eiu->numFailed) goto exit;
558
559     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
560
561         if (rpmtsCheck(ts)) {
562             eiu->numFailed = eiu->numPkgs;
563             stopInstall = 1;
564         }
565
566         ps = rpmtsProblems(ts);
567         if (!stopInstall && rpmpsNumProblems(ps) > 0) {
568             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
569             rpmpsPrint(NULL, ps);
570             eiu->numFailed = eiu->numPkgs;
571             stopInstall = 1;
572
573             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
574                 rpmtsPrintSuggests(ts);
575
576         }
577         ps = rpmpsFree(ps);
578     }
579
580     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
581         if (rpmtsOrder(ts)) {
582             eiu->numFailed = eiu->numPkgs;
583             stopInstall = 1;
584         }
585     }
586
587     if (eiu->numRPMS && !stopInstall) {
588
589         rpmcliPackagesTotal += eiu->numSRPMS;
590
591         rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
592
593         /* Drop added/available package indices and dependency sets. */
594         rpmtsClean(ts);
595
596         rc = rpmtsRun(ts, NULL, probFilter);
597         ps = rpmtsProblems(ts);
598
599         if (rc < 0) {
600             eiu->numFailed += eiu->numRPMS;
601         } else if (rc > 0) {
602             eiu->numFailed += rc;
603             if (rpmpsNumProblems(ps) > 0)
604                 rpmpsPrint(stderr, ps);
605         }
606         ps = rpmpsFree(ps);
607     }
608
609     if (eiu->numSRPMS && !stopInstall) {
610         if (eiu->sourceURL != NULL)
611         for (i = 0; i < eiu->numSRPMS; i++) {
612             rpmdbCheckSignals();
613             if (eiu->sourceURL[i] == NULL) continue;
614             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
615             if (eiu->fd == NULL || Ferror(eiu->fd)) {
616                 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
617                            eiu->sourceURL[i], Fstrerror(eiu->fd));
618                 if (eiu->fd != NULL) {
619                     xx = Fclose(eiu->fd);
620                     eiu->fd = NULL;
621                 }
622                 continue;
623             }
624
625             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
626                 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
627                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
628             }
629
630             xx = Fclose(eiu->fd);
631             eiu->fd = NULL;
632         }
633     }
634
635 exit:
636     if (eiu->pkgURL != NULL)
637     for (i = 0; i < eiu->numPkgs; i++) {
638         if (eiu->pkgURL[i] == NULL) continue;
639         if (eiu->pkgState[i] == 1)
640             (void) unlink(eiu->pkgURL[i]);
641         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
642     }
643     eiu->pkgState = _free(eiu->pkgState);
644     eiu->pkgURL = _free(eiu->pkgURL);
645     eiu->argv = _free(eiu->argv);
646
647     rpmtsEmpty(ts);
648
649     return eiu->numFailed;
650 }
651
652 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
653                 const char ** argv)
654 {
655     int count;
656     const char ** arg;
657     int numFailed = 0;
658     int stopUninstall = 0;
659     int numPackages = 0;
660     rpmVSFlags vsflags, ovsflags;
661     rpmps ps;
662
663     if (argv == NULL) return 0;
664
665     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
666     if (ia->qva_flags & VERIFY_DIGEST)
667         vsflags |= _RPMVSF_NODIGESTS;
668     if (ia->qva_flags & VERIFY_SIGNATURE)
669         vsflags |= _RPMVSF_NOSIGNATURES;
670     if (ia->qva_flags & VERIFY_HDRCHK)
671         vsflags |= RPMVSF_NOHDRCHK;
672     ovsflags = rpmtsSetVSFlags(ts, vsflags);
673
674     /* XXX suggest mechanism only meaningful when installing */
675     ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
676
677     (void) rpmtsSetFlags(ts, ia->transFlags);
678
679 #ifdef  NOTYET  /* XXX no callbacks on erase yet */
680     {   int notifyFlags, xx;
681         notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
682         xx = rpmtsSetNotifyCallback(ts,
683                         rpmShowProgress, (void *) ((long)notifyFlags));
684     }
685 #endif
686
687     for (arg = argv; *arg; arg++) {
688         rpmdbMatchIterator mi;
689
690         /* XXX HACK to get rpmdbFindByLabel out of the API */
691         mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
692         if (mi == NULL) {
693             rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
694             numFailed++;
695         } else {
696             Header h;   /* XXX iterator owns the reference */
697             count = 0;
698             while ((h = rpmdbNextIterator(mi)) != NULL) {
699                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
700
701                 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
702                     rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
703                         *arg);
704                     numFailed++;
705                     break;
706                 }
707                 if (recOffset) {
708                     (void) rpmtsAddEraseElement(ts, h, recOffset);
709                     numPackages++;
710                 }
711             }
712         }
713         mi = rpmdbFreeIterator(mi);
714     }
715
716     if (numFailed) goto exit;
717
718     if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
719
720         if (rpmtsCheck(ts)) {
721             numFailed = numPackages;
722             stopUninstall = 1;
723         }
724
725         ps = rpmtsProblems(ts);
726         if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
727             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
728             rpmpsPrint(NULL, ps);
729             numFailed += numPackages;
730             stopUninstall = 1;
731         }
732         ps = rpmpsFree(ps);
733     }
734
735     if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
736         if (rpmtsOrder(ts)) {
737             numFailed += numPackages;
738             stopUninstall = 1;
739         }
740     }
741
742     if (numPackages && !stopUninstall) {
743         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
744
745         /* Drop added/available package indices and dependency sets. */
746         rpmtsClean(ts);
747
748         numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
749         ps = rpmtsProblems(ts);
750         if (rpmpsNumProblems(ps) > 0)
751             rpmpsPrint(NULL, ps);
752         numFailed += numPackages;
753         stopUninstall = 1;
754         ps = rpmpsFree(ps);
755     }
756
757 exit:
758     rpmtsEmpty(ts);
759
760     return numFailed;
761 }
762
763 int rpmInstallSource(rpmts ts, const char * arg,
764                 char ** specFilePtr, char ** cookie)
765 {
766     FD_t fd;
767     int rc;
768
769
770     fd = Fopen(arg, "r.ufdio");
771     if (fd == NULL || Ferror(fd)) {
772         rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
773         if (fd != NULL) (void) Fclose(fd);
774         return 1;
775     }
776
777     if (rpmIsVerbose())
778         fprintf(stdout, _("Installing %s\n"), arg);
779
780     {
781         rpmVSFlags ovsflags =
782                 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
783         rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
784         rc = (rpmrc == RPMRC_OK ? 0 : 1);
785         ovsflags = rpmtsSetVSFlags(ts, ovsflags);
786     }
787     if (rc != 0) {
788         rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
789         if (specFilePtr && *specFilePtr)
790             *specFilePtr = _free(*specFilePtr);
791         if (cookie && *cookie)
792             *cookie = _free(*cookie);
793     }
794
795     (void) Fclose(fd);
796
797     return rc;
798 }
799