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