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