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