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