Simplify url retrieval in rpmInstall()
[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         switch (urlIsURL(fileURL)) {
300         case URL_IS_HTTPS:
301         case URL_IS_HTTP:
302         case URL_IS_FTP:
303         {   char *tfn;
304             FD_t tfd;
305
306             if (rpmIsVerbose())
307                 fprintf(stdout, _("Retrieving %s\n"), fileURL);
308
309             tfd = rpmMkTemp(rpmtsRootDir(ts), &tfn);
310             if (tfd && tfn) {
311                 Fclose(tfd);
312                 rc = urlGetFile(fileURL, tfn);
313             } else {
314                 rc = -1;
315             }
316
317             if (rc != 0) {
318                 rpmlog(RPMLOG_ERR,
319                         _("skipping %s - transfer failed - %s\n"),
320                         fileURL, ftpStrerror(rc));
321                 eiu->numFailed++;
322                 eiu->pkgURL[eiu->pkgx] = NULL;
323                 tfn = _free(tfn);
324                 break;
325             }
326             eiu->pkgState[eiu->pkgx] = 1;
327             eiu->pkgURL[eiu->pkgx] = tfn;
328             eiu->pkgx++;
329         }   break;
330         case URL_IS_PATH:
331         case URL_IS_DASH:       /* WRONG WRONG WRONG */
332         case URL_IS_HKP:        /* WRONG WRONG WRONG */
333         default:
334             eiu->pkgURL[eiu->pkgx] = fileURL;
335             fileURL = NULL;
336             eiu->pkgx++;
337             break;
338         }
339     }
340     fileURL = _free(fileURL);
341
342     if (eiu->numFailed) goto exit;
343
344     /* Continue processing file arguments, building transaction set. */
345     for (eiu->fnp = eiu->pkgURL+eiu->prevx;
346          *eiu->fnp != NULL;
347          eiu->fnp++, eiu->prevx++)
348     {
349         const char * fileName;
350
351         rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
352         (void) urlPath(*eiu->fnp, &fileName);
353
354         /* Try to read the header from a package file. */
355         eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
356         if (eiu->fd == NULL || Ferror(eiu->fd)) {
357             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
358                         Fstrerror(eiu->fd));
359             if (eiu->fd != NULL) {
360                 xx = Fclose(eiu->fd);
361                 eiu->fd = NULL;
362             }
363             eiu->numFailed++; *eiu->fnp = NULL;
364             continue;
365         }
366
367         /* Read the header, verifying signatures (if present). */
368         tvsflags = rpmtsSetVSFlags(ts, vsflags);
369         eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
370         tvsflags = rpmtsSetVSFlags(ts, tvsflags);
371         xx = Fclose(eiu->fd);
372         eiu->fd = NULL;
373
374         switch (eiu->rpmrc) {
375         case RPMRC_FAIL:
376             rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
377             eiu->numFailed++; *eiu->fnp = NULL;
378             continue;
379             break;
380         case RPMRC_NOTFOUND:
381             goto maybe_manifest;
382             break;
383         case RPMRC_NOTTRUSTED:
384         case RPMRC_NOKEY:
385         case RPMRC_OK:
386         default:
387             break;
388         }
389
390         eiu->isSource = headerIsSource(eiu->h);
391
392         if (eiu->isSource) {
393             rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
394                 eiu->numSRPMS);
395             eiu->sourceURL = xrealloc(eiu->sourceURL,
396                                 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
397             eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
398             *eiu->fnp = NULL;
399             eiu->numSRPMS++;
400             eiu->sourceURL[eiu->numSRPMS] = NULL;
401             continue;
402         }
403
404         if (eiu->relocations) {
405             const char ** paths;
406             rpmTagType pft;
407             rpm_count_t c;
408
409             if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
410                                        (rpm_data_t *) &paths, &c) && (c == 1))
411             {
412                 eiu->relocations->oldPath = xstrdup(paths[0]);
413                 paths = headerFreeData(paths, pft);
414             } else {
415                 const char * name;
416                 xx = headerNVR(eiu->h, &name, NULL, NULL);
417                 rpmlog(RPMLOG_ERR,
418                                _("package %s is not relocatable\n"), name);
419                 eiu->numFailed++;
420                 goto exit;
421             }
422         }
423
424         /* On --freshen, verify package is installed and newer */
425         if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
426             rpmdbMatchIterator mi;
427             const char * name;
428             Header oldH;
429             int count;
430
431             xx = headerNVR(eiu->h, &name, NULL, NULL);
432             mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
433             count = rpmdbGetIteratorCount(mi);
434             while ((oldH = rpmdbNextIterator(mi)) != NULL) {
435                 if (rpmVersionCompare(oldH, eiu->h) < 0)
436                     continue;
437                 /* same or newer package already installed */
438                 count = 0;
439                 break;
440             }
441             mi = rpmdbFreeIterator(mi);
442             if (count == 0) {
443                 eiu->h = headerFree(eiu->h);
444                 continue;
445             }
446             /* Package is newer than those currently installed. */
447         }
448
449         rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
450                         (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
451                         relocations);
452
453         /* XXX reference held by transaction set */
454         eiu->h = headerFree(eiu->h);
455         if (eiu->relocations)
456             eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
457
458         switch(rc) {
459         case 0:
460             rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
461                         eiu->numRPMS);
462             break;
463         case 1:
464             rpmlog(RPMLOG_ERR,
465                             _("error reading from file %s\n"), *eiu->fnp);
466             eiu->numFailed++;
467             goto exit;
468             break;
469         case 2:
470             rpmlog(RPMLOG_ERR,
471                             _("file %s requires a newer version of RPM\n"),
472                             *eiu->fnp);
473             eiu->numFailed++;
474             goto exit;
475             break;
476         default:
477             eiu->numFailed++;
478             goto exit;
479             break;
480         }
481
482         eiu->numRPMS++;
483         continue;
484
485 maybe_manifest:
486         /* Try to read a package manifest. */
487         eiu->fd = Fopen(*eiu->fnp, "r.fpio");
488         if (eiu->fd == NULL || Ferror(eiu->fd)) {
489             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
490                         Fstrerror(eiu->fd));
491             if (eiu->fd != NULL) {
492                 xx = Fclose(eiu->fd);
493                 eiu->fd = NULL;
494             }
495             eiu->numFailed++; *eiu->fnp = NULL;
496             break;
497         }
498
499         /* Read list of packages from manifest. */
500 /* FIX: *eiu->argv can be NULL */
501         rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
502         if (rc != RPMRC_OK)
503             rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
504                         *eiu->fnp, Fstrerror(eiu->fd));
505         xx = Fclose(eiu->fd);
506         eiu->fd = NULL;
507
508         /* If successful, restart the query loop. */
509         if (rc == RPMRC_OK) {
510             eiu->prevx++;
511             goto restart;
512         }
513
514         eiu->numFailed++; *eiu->fnp = NULL;
515         break;
516     }
517
518     rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
519                 eiu->numSRPMS, eiu->numRPMS);
520
521     if (eiu->numFailed) goto exit;
522
523     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
524
525         if (rpmtsCheck(ts)) {
526             eiu->numFailed = eiu->numPkgs;
527             stopInstall = 1;
528         }
529
530         ps = rpmtsProblems(ts);
531         if (!stopInstall && rpmpsNumProblems(ps) > 0) {
532             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
533             rpmpsPrint(NULL, ps);
534             eiu->numFailed = eiu->numPkgs;
535             stopInstall = 1;
536
537             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
538                 rpmtsPrintSuggests(ts);
539
540         }
541         ps = rpmpsFree(ps);
542     }
543
544     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
545         if (rpmtsOrder(ts)) {
546             eiu->numFailed = eiu->numPkgs;
547             stopInstall = 1;
548         }
549     }
550
551     if (eiu->numRPMS && !stopInstall) {
552
553         rpmcliPackagesTotal += eiu->numSRPMS;
554
555         rpmlog(RPMLOG_DEBUG, "installing binary packages\n");
556
557         /* Drop added/available package indices and dependency sets. */
558         rpmtsClean(ts);
559
560         rc = rpmtsRun(ts, NULL, probFilter);
561         ps = rpmtsProblems(ts);
562
563         if (rc < 0) {
564             eiu->numFailed += eiu->numRPMS;
565         } else if (rc > 0) {
566             eiu->numFailed += rc;
567             if (rpmpsNumProblems(ps) > 0)
568                 rpmpsPrint(stderr, ps);
569         }
570         ps = rpmpsFree(ps);
571     }
572
573     if (eiu->numSRPMS && !stopInstall) {
574         if (eiu->sourceURL != NULL)
575         for (i = 0; i < eiu->numSRPMS; i++) {
576             rpmdbCheckSignals();
577             if (eiu->sourceURL[i] == NULL) continue;
578             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
579             if (eiu->fd == NULL || Ferror(eiu->fd)) {
580                 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
581                            eiu->sourceURL[i], Fstrerror(eiu->fd));
582                 if (eiu->fd != NULL) {
583                     xx = Fclose(eiu->fd);
584                     eiu->fd = NULL;
585                 }
586                 continue;
587             }
588
589             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
590                 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
591                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
592             }
593
594             xx = Fclose(eiu->fd);
595             eiu->fd = NULL;
596         }
597     }
598
599 exit:
600     if (eiu->pkgURL != NULL)
601     for (i = 0; i < eiu->numPkgs; i++) {
602         if (eiu->pkgURL[i] == NULL) continue;
603         if (eiu->pkgState[i] == 1)
604             (void) unlink(eiu->pkgURL[i]);
605         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
606     }
607     eiu->pkgState = _free(eiu->pkgState);
608     eiu->pkgURL = _free(eiu->pkgURL);
609     eiu->argv = _free(eiu->argv);
610
611     rpmtsEmpty(ts);
612
613     return eiu->numFailed;
614 }
615
616 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
617 {
618     int count;
619     char * const * arg;
620     int numFailed = 0;
621     int stopUninstall = 0;
622     int numPackages = 0;
623     rpmVSFlags vsflags, ovsflags;
624     rpmps ps;
625
626     if (argv == NULL) return 0;
627
628     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
629     if (ia->qva_flags & VERIFY_DIGEST)
630         vsflags |= _RPMVSF_NODIGESTS;
631     if (ia->qva_flags & VERIFY_SIGNATURE)
632         vsflags |= _RPMVSF_NOSIGNATURES;
633     if (ia->qva_flags & VERIFY_HDRCHK)
634         vsflags |= RPMVSF_NOHDRCHK;
635     ovsflags = rpmtsSetVSFlags(ts, vsflags);
636
637     /* XXX suggest mechanism only meaningful when installing */
638     ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
639
640     (void) rpmtsSetFlags(ts, ia->transFlags);
641
642 #ifdef  NOTYET  /* XXX no callbacks on erase yet */
643     {   int notifyFlags, xx;
644         notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
645         xx = rpmtsSetNotifyCallback(ts,
646                         rpmShowProgress, (void *) ((long)notifyFlags));
647     }
648 #endif
649
650     for (arg = argv; *arg; arg++) {
651         rpmdbMatchIterator mi;
652
653         /* XXX HACK to get rpmdbFindByLabel out of the API */
654         mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
655         if (mi == NULL) {
656             rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
657             numFailed++;
658         } else {
659             Header h;   /* XXX iterator owns the reference */
660             count = 0;
661             while ((h = rpmdbNextIterator(mi)) != NULL) {
662                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
663
664                 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
665                     rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
666                         *arg);
667                     numFailed++;
668                     break;
669                 }
670                 if (recOffset) {
671                     (void) rpmtsAddEraseElement(ts, h, recOffset);
672                     numPackages++;
673                 }
674             }
675         }
676         mi = rpmdbFreeIterator(mi);
677     }
678
679     if (numFailed) goto exit;
680
681     if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
682
683         if (rpmtsCheck(ts)) {
684             numFailed = numPackages;
685             stopUninstall = 1;
686         }
687
688         ps = rpmtsProblems(ts);
689         if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
690             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
691             rpmpsPrint(NULL, ps);
692             numFailed += numPackages;
693             stopUninstall = 1;
694         }
695         ps = rpmpsFree(ps);
696     }
697
698     if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
699         if (rpmtsOrder(ts)) {
700             numFailed += numPackages;
701             stopUninstall = 1;
702         }
703     }
704
705     if (numPackages && !stopUninstall) {
706         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
707
708         /* Drop added/available package indices and dependency sets. */
709         rpmtsClean(ts);
710
711         numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
712         ps = rpmtsProblems(ts);
713         if (rpmpsNumProblems(ps) > 0)
714             rpmpsPrint(NULL, ps);
715         numFailed += numPackages;
716         stopUninstall = 1;
717         ps = rpmpsFree(ps);
718     }
719
720 exit:
721     rpmtsEmpty(ts);
722
723     return numFailed;
724 }
725
726 int rpmInstallSource(rpmts ts, const char * arg,
727                 char ** specFilePtr, char ** cookie)
728 {
729     FD_t fd;
730     int rc;
731
732
733     fd = Fopen(arg, "r.ufdio");
734     if (fd == NULL || Ferror(fd)) {
735         rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
736         if (fd != NULL) (void) Fclose(fd);
737         return 1;
738     }
739
740     if (rpmIsVerbose())
741         fprintf(stdout, _("Installing %s\n"), arg);
742
743     {
744         rpmVSFlags ovsflags =
745                 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
746         rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
747         rc = (rpmrc == RPMRC_OK ? 0 : 1);
748         ovsflags = rpmtsSetVSFlags(ts, ovsflags);
749     }
750     if (rc != 0) {
751         rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
752         if (specFilePtr && *specFilePtr)
753             *specFilePtr = _free(*specFilePtr);
754         if (cookie && *cookie)
755             *cookie = _free(*cookie);
756     }
757
758     (void) Fclose(fd);
759
760     return rc;
761 }
762