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