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