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