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