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