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