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