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