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