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