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