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