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