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