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