rpmlib.h mass eviction
[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
9 #include <rpm/rpmtag.h>
10 #include <rpm/rpmlib.h>         /* rpmReadPackageFile, vercmp etc */
11 #include <rpm/rpmdb.h>
12 #include <rpm/rpmds.h>
13 #include <rpm/rpmts.h>
14 #include <rpm/rpmlog.h>
15 #include <rpm/rpmfileutil.h>
16 #include <rpm/idtx.h>
17
18 #include "lib/manifest.h"
19 #include "debug.h"
20
21 int rpmcliPackagesTotal = 0;
22 int rpmcliHashesCurrent = 0;
23 int rpmcliHashesTotal = 0;
24 int rpmcliProgressCurrent = 0;
25 int rpmcliProgressTotal = 0;
26
27 /**
28  * Print a CLI progress bar.
29  * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
30  * @param amount        current
31  * @param total         final
32  */
33 static void printHash(const unsigned long amount, const unsigned long total)
34 {
35     int hashesNeeded;
36
37     rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
38
39     if (rpmcliHashesCurrent != rpmcliHashesTotal) {
40         float pct = (total ? (((float) amount) / total) : 1.0);
41         hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
42         while (hashesNeeded > rpmcliHashesCurrent) {
43             if (isatty (STDOUT_FILENO)) {
44                 int i;
45                 for (i = 0; i < rpmcliHashesCurrent; i++)
46                     (void) putchar ('#');
47                 for (; i < rpmcliHashesTotal; i++)
48                     (void) putchar (' ');
49                 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
50                 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
51                     (void) putchar ('\b');
52             } else
53                 fprintf(stdout, "#");
54
55             rpmcliHashesCurrent++;
56         }
57         (void) fflush(stdout);
58
59         if (rpmcliHashesCurrent == rpmcliHashesTotal) {
60             int i;
61             rpmcliProgressCurrent++;
62             if (isatty(STDOUT_FILENO)) {
63                 for (i = 1; i < rpmcliHashesCurrent; i++)
64                     (void) putchar ('#');
65                 pct = (rpmcliProgressTotal
66                     ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
67                     : 1);
68                 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
69             }
70             fprintf(stdout, "\n");
71         }
72         (void) fflush(stdout);
73     }
74 }
75
76 void * rpmShowProgress(const void * arg,
77                         const rpmCallbackType what,
78                         const unsigned long amount,
79                         const unsigned long total,
80                         fnpyKey key,
81                         void * data)
82 {
83     Header h = (Header) arg;
84     char * s;
85     int flags = (int) ((long)data);
86     void * rc = NULL;
87     const char * filename = (const char *)key;
88     static FD_t fd = NULL;
89     int xx;
90
91     switch (what) {
92     case RPMCALLBACK_INST_OPEN_FILE:
93         if (filename == NULL || filename[0] == '\0')
94             return NULL;
95         fd = Fopen(filename, "r.ufdio");
96         /* FIX: still necessary? */
97         if (fd == NULL || Ferror(fd)) {
98             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
99                         Fstrerror(fd));
100             if (fd != NULL) {
101                 xx = Fclose(fd);
102                 fd = NULL;
103             }
104         } else
105             fd = fdLink(fd, RPMDBG_M("persist (showProgress)"));
106         return (void *)fd;
107         break;
108
109     case RPMCALLBACK_INST_CLOSE_FILE:
110         /* FIX: still necessary? */
111         fd = fdFree(fd, RPMDBG_M("persist (showProgress)"));
112         if (fd != NULL) {
113             xx = Fclose(fd);
114             fd = NULL;
115         }
116         break;
117
118     case RPMCALLBACK_INST_START:
119         rpmcliHashesCurrent = 0;
120         if (h == NULL || !(flags & INSTALL_LABEL))
121             break;
122         /* @todo Remove headerSprintf() on a progress callback. */
123         if (flags & INSTALL_HASH) {
124             s = headerSprintf(h, "%{NAME}",
125                                 rpmTagTable, rpmHeaderFormats, NULL);
126             if (isatty (STDOUT_FILENO))
127                 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
128             else
129                 fprintf(stdout, "%-28.28s", s);
130             (void) fflush(stdout);
131             s = _free(s);
132         } else {
133             s = headerSprintf(h, "%{NAME}-%{VERSION}-%{RELEASE}",
134                                   rpmTagTable, rpmHeaderFormats, NULL);
135             fprintf(stdout, "%s\n", s);
136             (void) fflush(stdout);
137             s = _free(s);
138         }
139         break;
140
141     case RPMCALLBACK_TRANS_PROGRESS:
142     case RPMCALLBACK_INST_PROGRESS:
143         if (flags & INSTALL_PERCENT)
144             fprintf(stdout, "%%%% %f\n", (double) (total
145                                 ? ((((float) amount) / total) * 100)
146                                 : 100.0));
147         else if (flags & INSTALL_HASH)
148             printHash(amount, total);
149         (void) fflush(stdout);
150         break;
151
152     case RPMCALLBACK_TRANS_START:
153         rpmcliHashesCurrent = 0;
154         rpmcliProgressTotal = 1;
155         rpmcliProgressCurrent = 0;
156         if (!(flags & INSTALL_LABEL))
157             break;
158         if (flags & INSTALL_HASH)
159             fprintf(stdout, "%-28s", _("Preparing..."));
160         else
161             fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
162         (void) fflush(stdout);
163         break;
164
165     case RPMCALLBACK_TRANS_STOP:
166         if (flags & INSTALL_HASH)
167             printHash(1, 1);    /* Fixes "preparing..." progress bar */
168         rpmcliProgressTotal = rpmcliPackagesTotal;
169         rpmcliProgressCurrent = 0;
170         break;
171
172     case RPMCALLBACK_REPACKAGE_START:
173         rpmcliHashesCurrent = 0;
174         rpmcliProgressTotal = total;
175         rpmcliProgressCurrent = 0;
176         if (!(flags & INSTALL_LABEL))
177             break;
178         if (flags & INSTALL_HASH)
179             fprintf(stdout, "%-28s\n", _("Repackaging..."));
180         else
181             fprintf(stdout, "%s\n", _("Repackaging erased files..."));
182         (void) fflush(stdout);
183         break;
184
185     case RPMCALLBACK_REPACKAGE_PROGRESS:
186         if (amount && (flags & INSTALL_HASH))
187             printHash(1, 1);    /* Fixes "preparing..." progress bar */
188         break;
189
190     case RPMCALLBACK_REPACKAGE_STOP:
191         rpmcliProgressTotal = total;
192         rpmcliProgressCurrent = total;
193         if (flags & INSTALL_HASH)
194             printHash(1, 1);    /* Fixes "preparing..." progress bar */
195         rpmcliProgressTotal = rpmcliPackagesTotal;
196         rpmcliProgressCurrent = 0;
197         if (!(flags & INSTALL_LABEL))
198             break;
199         if (flags & INSTALL_HASH)
200             fprintf(stdout, "%-28s\n", _("Upgrading..."));
201         else
202             fprintf(stdout, "%s\n", _("Upgrading packages..."));
203         (void) fflush(stdout);
204         break;
205
206     case RPMCALLBACK_UNINST_PROGRESS:
207         break;
208     case RPMCALLBACK_UNINST_START:
209         break;
210     case RPMCALLBACK_UNINST_STOP:
211         break;
212     case RPMCALLBACK_UNPACK_ERROR:
213         break;
214     case RPMCALLBACK_CPIO_ERROR:
215         break;
216     case RPMCALLBACK_SCRIPT_ERROR:
217         break;
218     case RPMCALLBACK_UNKNOWN:
219     default:
220         break;
221     }
222
223     return rc;
224 }       
225
226 typedef const char * str_t;
227
228 struct rpmEIU {
229     Header h;
230     FD_t fd;
231     int numFailed;
232     int numPkgs;
233     char ** pkgURL;
234     str_t * fnp;
235     char * pkgState;
236     int prevx;
237     int pkgx;
238     int numRPMS;
239     int numSRPMS;
240     str_t * sourceURL;
241     int isSource;
242     int argc;
243     char ** argv;
244     rpmRelocation * relocations;
245     rpmRC rpmrc;
246 };
247
248 /** @todo Generalize --freshen policies. */
249 int rpmInstall(rpmts ts,
250                 struct rpmInstallArguments_s * ia,
251                 const char ** fileArgv)
252 {
253     struct rpmEIU * eiu = memset(alloca(sizeof(*eiu)), 0, sizeof(*eiu));
254     rpmps ps;
255     rpmprobFilterFlags probFilter;
256     rpmRelocation * relocations;
257     char * fileURL = NULL;
258     int stopInstall = 0;
259     char ** av = NULL;
260     rpmVSFlags vsflags, ovsflags, tvsflags;
261     int ac = 0;
262     int rc;
263     int xx;
264     int i;
265
266     if (fileArgv == NULL) goto exit;
267
268     rpmcliPackagesTotal = 0;
269
270     if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
271         ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
272
273     (void) rpmtsSetFlags(ts, ia->transFlags);
274
275     probFilter = ia->probFilter;
276     relocations = ia->relocations;
277
278     if (ia->installInterfaceFlags & INSTALL_UPGRADE)
279         vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
280     else
281         vsflags = rpmExpandNumeric("%{?_vsflags_install}");
282     if (ia->qva_flags & VERIFY_DIGEST)
283         vsflags |= _RPMVSF_NODIGESTS;
284     if (ia->qva_flags & VERIFY_SIGNATURE)
285         vsflags |= _RPMVSF_NOSIGNATURES;
286     if (ia->qva_flags & VERIFY_HDRCHK)
287         vsflags |= RPMVSF_NOHDRCHK;
288     ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
289
290     {   int notifyFlags;
291         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
292         xx = rpmtsSetNotifyCallback(ts,
293                         rpmShowProgress, (void *) ((long)notifyFlags));
294     }
295
296     if ((eiu->relocations = relocations) != NULL) {
297         while (eiu->relocations->oldPath)
298             eiu->relocations++;
299         if (eiu->relocations->newPath == NULL)
300             eiu->relocations = NULL;
301     }
302
303     /* Build fully globbed list of arguments in argv[argc]. */
304     for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
305         char * fn;
306         av = _free(av); ac = 0;
307         fn = rpmEscapeSpaces(*eiu->fnp);
308         rc = rpmGlob(fn, &ac, &av);
309         fn = _free(fn);
310         if (rc || ac == 0) {
311             rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
312             continue;
313         }
314
315         eiu->argv = xrealloc(eiu->argv, (eiu->argc+ac+1) * sizeof(*eiu->argv));
316         memcpy(eiu->argv+eiu->argc, av, ac * sizeof(*av));
317         eiu->argc += ac;
318         eiu->argv[eiu->argc] = NULL;
319     }
320     av = _free(av);     ac = 0;
321
322 restart:
323     /* Allocate sufficient storage for next set of args. */
324     if (eiu->pkgx >= eiu->numPkgs) {
325         eiu->numPkgs = eiu->pkgx + eiu->argc;
326         eiu->pkgURL = xrealloc(eiu->pkgURL,
327                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
328         memset(eiu->pkgURL + eiu->pkgx, 0,
329                         ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
330         eiu->pkgState = xrealloc(eiu->pkgState,
331                         (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
332         memset(eiu->pkgState + eiu->pkgx, 0,
333                         ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
334     }
335
336     /* Retrieve next set of args, cache on local storage. */
337     for (i = 0; i < eiu->argc; i++) {
338         fileURL = _free(fileURL);
339         fileURL = eiu->argv[i];
340         eiu->argv[i] = NULL;
341
342 #ifdef  NOTYET
343 if (fileURL[0] == '=') {
344     rpmds this = rpmdsSingle(RPMTAG_REQUIRENAME, fileURL+1, NULL, 0);
345
346     xx = rpmtsSolve(ts, this, NULL);
347     if (ts->suggests && ts->nsuggests > 0) {
348         fileURL = _free(fileURL);
349         fileURL = ts->suggests[0];
350         ts->suggests[0] = NULL;
351         while (ts->nsuggests-- > 0) {
352             if (ts->suggests[ts->nsuggests] == NULL)
353                 continue;
354             ts->suggests[ts->nsuggests] = _free(ts->suggests[ts->nsuggests]);
355         }
356         ts->suggests = _free(ts->suggests);
357         rpmlog(RPMLOG_DEBUG, _("Adding goal: %s\n"), fileURL);
358         eiu->pkgURL[eiu->pkgx] = fileURL;
359         fileURL = NULL;
360         eiu->pkgx++;
361     }
362     this = rpmdsFree(this);
363 } else
364 #endif
365
366         switch (urlIsURL(fileURL)) {
367         case URL_IS_HTTPS:
368         case URL_IS_HTTP:
369         case URL_IS_FTP:
370         {   char *tfn;
371
372             if (rpmIsVerbose())
373                 fprintf(stdout, _("Retrieving %s\n"), fileURL);
374
375             {   char tfnbuf[64];
376                 const char * rootDir = rpmtsRootDir(ts);
377                 if (!(rootDir && * rootDir))
378                     rootDir = "";
379                 strcpy(tfnbuf, "rpm-xfer.XXXXXX");
380 #if defined(HAVE_MKSTEMP)
381                 (void) close(mkstemp(tfnbuf));
382 #else
383                 (void) mktemp(tfnbuf);
384 #endif
385                 tfn = rpmGenPath(rootDir, "%{_tmppath}/", tfnbuf);
386             }
387
388             /* XXX undefined %{name}/%{version}/%{release} here */
389             /* XXX %{_tmpdir} does not exist */
390             rpmlog(RPMLOG_DEBUG, _(" ... as %s\n"), tfn);
391             rc = urlGetFile(fileURL, tfn);
392             if (rc < 0) {
393                 rpmlog(RPMLOG_ERR,
394                         _("skipping %s - transfer failed - %s\n"),
395                         fileURL, ftpStrerror(rc));
396                 eiu->numFailed++;
397                 eiu->pkgURL[eiu->pkgx] = NULL;
398                 tfn = _free(tfn);
399                 break;
400             }
401             eiu->pkgState[eiu->pkgx] = 1;
402             eiu->pkgURL[eiu->pkgx] = tfn;
403             eiu->pkgx++;
404         }   break;
405         case URL_IS_PATH:
406         case URL_IS_DASH:       /* WRONG WRONG WRONG */
407         case URL_IS_HKP:        /* WRONG WRONG WRONG */
408         default:
409             eiu->pkgURL[eiu->pkgx] = fileURL;
410             fileURL = NULL;
411             eiu->pkgx++;
412             break;
413         }
414     }
415     fileURL = _free(fileURL);
416
417     if (eiu->numFailed) goto exit;
418
419     /* Continue processing file arguments, building transaction set. */
420     for (eiu->fnp = (const char**) eiu->pkgURL+eiu->prevx;
421          *eiu->fnp != NULL;
422          eiu->fnp++, eiu->prevx++)
423     {
424         const char * fileName;
425
426         rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
427         (void) urlPath(*eiu->fnp, &fileName);
428
429         /* Try to read the header from a package file. */
430         eiu->fd = Fopen(*eiu->fnp, "r.ufdio");
431         if (eiu->fd == NULL || Ferror(eiu->fd)) {
432             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
433                         Fstrerror(eiu->fd));
434             if (eiu->fd != NULL) {
435                 xx = Fclose(eiu->fd);
436                 eiu->fd = NULL;
437             }
438             eiu->numFailed++; *eiu->fnp = NULL;
439             continue;
440         }
441
442         /* Read the header, verifying signatures (if present). */
443         tvsflags = rpmtsSetVSFlags(ts, vsflags);
444         eiu->rpmrc = rpmReadPackageFile(ts, eiu->fd, *eiu->fnp, &eiu->h);
445         tvsflags = rpmtsSetVSFlags(ts, tvsflags);
446         xx = Fclose(eiu->fd);
447         eiu->fd = NULL;
448
449         switch (eiu->rpmrc) {
450         case RPMRC_FAIL:
451             rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
452             eiu->numFailed++; *eiu->fnp = NULL;
453             continue;
454             break;
455         case RPMRC_NOTFOUND:
456             goto maybe_manifest;
457             break;
458         case RPMRC_NOTTRUSTED:
459         case RPMRC_NOKEY:
460         case RPMRC_OK:
461         default:
462             break;
463         }
464
465         eiu->isSource = headerIsSource(eiu->h);
466
467         if (eiu->isSource) {
468             rpmlog(RPMLOG_DEBUG, _("\tadded source package [%d]\n"),
469                 eiu->numSRPMS);
470             eiu->sourceURL = xrealloc(eiu->sourceURL,
471                                 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
472             eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
473             *eiu->fnp = NULL;
474             eiu->numSRPMS++;
475             eiu->sourceURL[eiu->numSRPMS] = NULL;
476             continue;
477         }
478
479         if (eiu->relocations) {
480             const char ** paths;
481             rpm_tagtype_t pft;
482             rpm_count_t c;
483
484             if (headerGetEntry(eiu->h, RPMTAG_PREFIXES, &pft,
485                                        (rpm_data_t *) &paths, &c) && (c == 1))
486             {
487                 eiu->relocations->oldPath = xstrdup(paths[0]);
488                 paths = headerFreeData(paths, pft);
489             } else {
490                 const char * name;
491                 xx = headerNVR(eiu->h, &name, NULL, NULL);
492                 rpmlog(RPMLOG_ERR,
493                                _("package %s is not relocatable\n"), name);
494                 eiu->numFailed++;
495                 goto exit;
496             }
497         }
498
499         /* On --freshen, verify package is installed and newer */
500         if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
501             rpmdbMatchIterator mi;
502             const char * name;
503             Header oldH;
504             int count;
505
506             xx = headerNVR(eiu->h, &name, NULL, NULL);
507             mi = rpmtsInitIterator(ts, RPMTAG_NAME, name, 0);
508             count = rpmdbGetIteratorCount(mi);
509             while ((oldH = rpmdbNextIterator(mi)) != NULL) {
510                 if (rpmVersionCompare(oldH, eiu->h) < 0)
511                     continue;
512                 /* same or newer package already installed */
513                 count = 0;
514                 break;
515             }
516             mi = rpmdbFreeIterator(mi);
517             if (count == 0) {
518                 eiu->h = headerFree(eiu->h);
519                 continue;
520             }
521             /* Package is newer than those currently installed. */
522         }
523
524         rc = rpmtsAddInstallElement(ts, eiu->h, (fnpyKey)fileName,
525                         (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
526                         relocations);
527
528         /* XXX reference held by transaction set */
529         eiu->h = headerFree(eiu->h);
530         if (eiu->relocations)
531             eiu->relocations->oldPath = _constfree(eiu->relocations->oldPath);
532
533         switch(rc) {
534         case 0:
535             rpmlog(RPMLOG_DEBUG, _("\tadded binary package [%d]\n"),
536                         eiu->numRPMS);
537             break;
538         case 1:
539             rpmlog(RPMLOG_ERR,
540                             _("error reading from file %s\n"), *eiu->fnp);
541             eiu->numFailed++;
542             goto exit;
543             break;
544         case 2:
545             rpmlog(RPMLOG_ERR,
546                             _("file %s requires a newer version of RPM\n"),
547                             *eiu->fnp);
548             eiu->numFailed++;
549             goto exit;
550             break;
551         default:
552             eiu->numFailed++;
553             goto exit;
554             break;
555         }
556
557         eiu->numRPMS++;
558         continue;
559
560 maybe_manifest:
561         /* Try to read a package manifest. */
562         eiu->fd = Fopen(*eiu->fnp, "r.fpio");
563         if (eiu->fd == NULL || Ferror(eiu->fd)) {
564             rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
565                         Fstrerror(eiu->fd));
566             if (eiu->fd != NULL) {
567                 xx = Fclose(eiu->fd);
568                 eiu->fd = NULL;
569             }
570             eiu->numFailed++; *eiu->fnp = NULL;
571             break;
572         }
573
574         /* Read list of packages from manifest. */
575 /* FIX: *eiu->argv can be NULL */
576         rc = rpmReadPackageManifest(eiu->fd, &eiu->argc, &eiu->argv);
577         if (rc != RPMRC_OK)
578             rpmlog(RPMLOG_NOTICE, _("%s: not an rpm package (or package manifest): %s\n"),
579                         *eiu->fnp, Fstrerror(eiu->fd));
580         xx = Fclose(eiu->fd);
581         eiu->fd = NULL;
582
583         /* If successful, restart the query loop. */
584         if (rc == RPMRC_OK) {
585             eiu->prevx++;
586             goto restart;
587         }
588
589         eiu->numFailed++; *eiu->fnp = NULL;
590         break;
591     }
592
593     rpmlog(RPMLOG_DEBUG, _("found %d source and %d binary packages\n"),
594                 eiu->numSRPMS, eiu->numRPMS);
595
596     if (eiu->numFailed) goto exit;
597
598     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NODEPS)) {
599
600         if (rpmtsCheck(ts)) {
601             eiu->numFailed = eiu->numPkgs;
602             stopInstall = 1;
603         }
604
605         ps = rpmtsProblems(ts);
606         if (!stopInstall && rpmpsNumProblems(ps) > 0) {
607             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
608             rpmpsPrint(NULL, ps);
609             eiu->numFailed = eiu->numPkgs;
610             stopInstall = 1;
611
612             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOSUGGEST))
613                 rpmtsPrintSuggests(ts);
614
615         }
616         ps = rpmpsFree(ps);
617     }
618
619     if (eiu->numRPMS && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
620         if (rpmtsOrder(ts)) {
621             eiu->numFailed = eiu->numPkgs;
622             stopInstall = 1;
623         }
624     }
625
626     if (eiu->numRPMS && !stopInstall) {
627
628         rpmcliPackagesTotal += eiu->numSRPMS;
629
630         rpmlog(RPMLOG_DEBUG, _("installing binary packages\n"));
631
632         /* Drop added/available package indices and dependency sets. */
633         rpmtsClean(ts);
634
635         rc = rpmtsRun(ts, NULL, probFilter);
636         ps = rpmtsProblems(ts);
637
638         if (rc < 0) {
639             eiu->numFailed += eiu->numRPMS;
640         } else if (rc > 0) {
641             eiu->numFailed += rc;
642             if (rpmpsNumProblems(ps) > 0)
643                 rpmpsPrint(stderr, ps);
644         }
645         ps = rpmpsFree(ps);
646     }
647
648     if (eiu->numSRPMS && !stopInstall) {
649         if (eiu->sourceURL != NULL)
650         for (i = 0; i < eiu->numSRPMS; i++) {
651             rpmdbCheckSignals();
652             if (eiu->sourceURL[i] == NULL) continue;
653             eiu->fd = Fopen(eiu->sourceURL[i], "r.ufdio");
654             if (eiu->fd == NULL || Ferror(eiu->fd)) {
655                 rpmlog(RPMLOG_ERR, _("cannot open file %s: %s\n"),
656                            eiu->sourceURL[i], Fstrerror(eiu->fd));
657                 if (eiu->fd != NULL) {
658                     xx = Fclose(eiu->fd);
659                     eiu->fd = NULL;
660                 }
661                 continue;
662             }
663
664             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)) {
665                 eiu->rpmrc = rpmInstallSourcePackage(ts, eiu->fd, NULL, NULL);
666                 if (eiu->rpmrc != RPMRC_OK) eiu->numFailed++;
667             }
668
669             xx = Fclose(eiu->fd);
670             eiu->fd = NULL;
671         }
672     }
673
674 exit:
675     if (eiu->pkgURL != NULL)
676     for (i = 0; i < eiu->numPkgs; i++) {
677         if (eiu->pkgURL[i] == NULL) continue;
678         if (eiu->pkgState[i] == 1)
679             (void) unlink(eiu->pkgURL[i]);
680         eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
681     }
682     eiu->pkgState = _free(eiu->pkgState);
683     eiu->pkgURL = _free(eiu->pkgURL);
684     eiu->argv = _free(eiu->argv);
685
686     rpmtsEmpty(ts);
687
688     return eiu->numFailed;
689 }
690
691 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia,
692                 const char ** argv)
693 {
694     int count;
695     const char ** arg;
696     int numFailed = 0;
697     int stopUninstall = 0;
698     int numPackages = 0;
699     rpmVSFlags vsflags, ovsflags;
700     rpmps ps;
701
702     if (argv == NULL) return 0;
703
704     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
705     if (ia->qva_flags & VERIFY_DIGEST)
706         vsflags |= _RPMVSF_NODIGESTS;
707     if (ia->qva_flags & VERIFY_SIGNATURE)
708         vsflags |= _RPMVSF_NOSIGNATURES;
709     if (ia->qva_flags & VERIFY_HDRCHK)
710         vsflags |= RPMVSF_NOHDRCHK;
711     ovsflags = rpmtsSetVSFlags(ts, vsflags);
712
713     if (rpmExpandNumeric("%{?_repackage_all_erasures}"))
714         ia->transFlags |= RPMTRANS_FLAG_REPACKAGE;
715
716     /* XXX suggest mechanism only meaningful when installing */
717     ia->transFlags |= RPMTRANS_FLAG_NOSUGGEST;
718
719     (void) rpmtsSetFlags(ts, ia->transFlags);
720
721 #ifdef  NOTYET  /* XXX no callbacks on erase yet */
722     {   int notifyFlags, xx;
723         notifyFlags = ia->eraseInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
724         xx = rpmtsSetNotifyCallback(ts,
725                         rpmShowProgress, (void *) ((long)notifyFlags));
726     }
727 #endif
728
729     for (arg = argv; *arg; arg++) {
730         rpmdbMatchIterator mi;
731
732         /* XXX HACK to get rpmdbFindByLabel out of the API */
733         mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
734         if (mi == NULL) {
735             rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
736             numFailed++;
737         } else {
738             Header h;   /* XXX iterator owns the reference */
739             count = 0;
740             while ((h = rpmdbNextIterator(mi)) != NULL) {
741                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
742
743                 if (!(count++ == 0 || (ia->eraseInterfaceFlags & UNINSTALL_ALLMATCHES))) {
744                     rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages\n"),
745                         *arg);
746                     numFailed++;
747                     break;
748                 }
749                 if (recOffset) {
750                     (void) rpmtsAddEraseElement(ts, h, recOffset);
751                     numPackages++;
752                 }
753             }
754         }
755         mi = rpmdbFreeIterator(mi);
756     }
757
758     if (numFailed) goto exit;
759
760     if (!(ia->eraseInterfaceFlags & UNINSTALL_NODEPS)) {
761
762         if (rpmtsCheck(ts)) {
763             numFailed = numPackages;
764             stopUninstall = 1;
765         }
766
767         ps = rpmtsProblems(ts);
768         if (!stopUninstall && rpmpsNumProblems(ps) > 0) {
769             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
770             rpmpsPrint(NULL, ps);
771             numFailed += numPackages;
772             stopUninstall = 1;
773         }
774         ps = rpmpsFree(ps);
775     }
776
777     if (!stopUninstall && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
778         if (rpmtsOrder(ts)) {
779             numFailed += numPackages;
780             stopUninstall = 1;
781         }
782     }
783
784     if (numPackages && !stopUninstall) {
785         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_REVERSE));
786
787         /* Drop added/available package indices and dependency sets. */
788         rpmtsClean(ts);
789
790         numPackages = rpmtsRun(ts, NULL, ia->probFilter & (RPMPROB_FILTER_DISKSPACE|RPMPROB_FILTER_DISKNODES));
791         ps = rpmtsProblems(ts);
792         if (rpmpsNumProblems(ps) > 0)
793             rpmpsPrint(NULL, ps);
794         numFailed += numPackages;
795         stopUninstall = 1;
796         ps = rpmpsFree(ps);
797     }
798
799 exit:
800     rpmtsEmpty(ts);
801
802     return numFailed;
803 }
804
805 int rpmInstallSource(rpmts ts, const char * arg,
806                 char ** specFilePtr, char ** cookie)
807 {
808     FD_t fd;
809     int rc;
810
811
812     fd = Fopen(arg, "r.ufdio");
813     if (fd == NULL || Ferror(fd)) {
814         rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
815         if (fd != NULL) (void) Fclose(fd);
816         return 1;
817     }
818
819     if (rpmIsVerbose())
820         fprintf(stdout, _("Installing %s\n"), arg);
821
822     {
823         rpmVSFlags ovsflags =
824                 rpmtsSetVSFlags(ts, (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD));
825         rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
826         rc = (rpmrc == RPMRC_OK ? 0 : 1);
827         ovsflags = rpmtsSetVSFlags(ts, ovsflags);
828     }
829     if (rc != 0) {
830         rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
831         if (specFilePtr && *specFilePtr)
832             *specFilePtr = _free(*specFilePtr);
833         if (cookie && *cookie)
834             *cookie = _free(*cookie);
835     }
836
837     (void) Fclose(fd);
838
839     return rc;
840 }
841
842 /** @todo Transaction handling, more, needs work. */
843 int rpmRollback(rpmts ts, struct rpmInstallArguments_s * ia, const char ** argv)
844 {
845     int ifmask= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL|INSTALL_ERASE);
846     unsigned thistid = 0xffffffff;
847     unsigned prevtid;
848     time_t tid;
849     IDTX itids = NULL;
850     IDTX rtids = NULL;
851     IDT rp;
852     int nrids = 0;
853     IDT ip;
854     int niids = 0;
855     int rc = 0;
856     int vsflags, ovsflags;
857     int numAdded;
858     int numRemoved;
859     rpmps ps;
860     int _unsafe_rollbacks = 0;
861     rpmtransFlags transFlags = ia->transFlags;
862
863     if (argv != NULL && *argv != NULL) {
864         rc = -1;
865         goto exit;
866     }
867
868     _unsafe_rollbacks = rpmExpandNumeric("%{?_unsafe_rollbacks}");
869
870     vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
871     if (ia->qva_flags & VERIFY_DIGEST)
872         vsflags |= _RPMVSF_NODIGESTS;
873     if (ia->qva_flags & VERIFY_SIGNATURE)
874         vsflags |= _RPMVSF_NOSIGNATURES;
875     if (ia->qva_flags & VERIFY_HDRCHK)
876         vsflags |= RPMVSF_NOHDRCHK;
877     vsflags |= RPMVSF_NEEDPAYLOAD;      /* XXX no legacy signatures */
878     ovsflags = rpmtsSetVSFlags(ts, vsflags);
879
880     (void) rpmtsSetFlags(ts, transFlags);
881
882     /*  Make the transaction a rollback transaction.  In a rollback
883      *  a best effort is what we want 
884      */
885     rpmtsSetType(ts, RPMTRANS_TYPE_ROLLBACK);
886
887     itids = IDTXload(ts, RPMTAG_INSTALLTID);
888     if (itids != NULL) {
889         ip = itids->idt;
890         niids = itids->nidt;
891     } else {
892         ip = NULL;
893         niids = 0;
894     }
895
896     {   char * globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
897         if (globstr == NULL || *globstr == '%') {
898             globstr = _free(globstr);
899             rc = -1;
900             goto exit;
901         }
902         rtids = IDTXglob(ts, globstr, RPMTAG_REMOVETID);
903
904         if (rtids != NULL) {
905             rp = rtids->idt;
906             nrids = rtids->nidt;
907         } else {
908             rp = NULL;
909             nrids = 0;
910         }
911         globstr = _free(globstr);
912     }
913
914     {   int notifyFlags, xx;
915         notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
916         xx = rpmtsSetNotifyCallback(ts,
917                         rpmShowProgress, (void *) ((long)notifyFlags));
918     }
919
920     /* Run transactions until rollback goal is achieved. */
921     do {
922         prevtid = thistid;
923         rc = 0;
924         rpmcliPackagesTotal = 0;
925         numAdded = 0;
926         numRemoved = 0;
927         ia->installInterfaceFlags &= ~ifmask;
928
929         /* Find larger of the remaining install/erase transaction id's. */
930         thistid = 0;
931         if (ip != NULL && ip->val.u32 > thistid)
932             thistid = ip->val.u32;
933         if (rp != NULL && rp->val.u32 > thistid)
934             thistid = rp->val.u32;
935
936         /* If we've achieved the rollback goal, then we're done. */
937         if (thistid == 0 || thistid < ia->rbtid)
938             break;
939
940         /* If we've reached the (configured) rollback goal, then we're done. */
941         if (_unsafe_rollbacks && thistid <= _unsafe_rollbacks)
942             break;
943
944         rpmtsEmpty(ts);
945         (void) rpmtsSetFlags(ts, transFlags);
946
947         /* Install the previously erased packages for this transaction. */
948         while (rp != NULL && rp->val.u32 == thistid) {
949
950             rpmlog(RPMLOG_DEBUG, "\t+++ install %s\n",
951                         (rp->key ? rp->key : "???"));
952
953             rc = rpmtsAddInstallElement(ts, rp->h, (fnpyKey)rp->key,
954                                0, ia->relocations);
955             if (rc != 0)
956                 goto exit;
957
958             numAdded++;
959             rpmcliPackagesTotal++;
960             if (!(ia->installInterfaceFlags & ifmask))
961                 ia->installInterfaceFlags |= INSTALL_UPGRADE;
962
963 #ifdef  NOTYET
964             rp->h = headerFree(rp->h);
965 #endif
966             nrids--;
967             if (nrids > 0)
968                 rp++;
969             else
970                 rp = NULL;
971         }
972
973         /* Erase the previously installed packages for this transaction. */
974         while (ip != NULL && ip->val.u32 == thistid) {
975
976             rpmlog(RPMLOG_DEBUG,
977                         "\t--- erase h#%u\n", ip->instance);
978
979             rc = rpmtsAddEraseElement(ts, ip->h, ip->instance);
980             if (rc != 0)
981                 goto exit;
982
983             numRemoved++;
984
985             if (_unsafe_rollbacks)
986                 rpmcliPackagesTotal++;
987
988             if (!(ia->installInterfaceFlags & ifmask)) {
989                 ia->installInterfaceFlags |= INSTALL_ERASE;
990                 (void) rpmtsSetFlags(ts, (transFlags | RPMTRANS_FLAG_REVERSE));
991             }
992
993 #ifdef  NOTYET
994             ip->instance = 0;
995 #endif
996             niids--;
997             if (niids > 0)
998                 ip++;
999             else
1000                 ip = NULL;
1001         }
1002
1003         /* Anything to do? */
1004         if (rpmcliPackagesTotal <= 0)
1005             break;
1006
1007         tid = (time_t)thistid;
1008         rpmlog(RPMLOG_NOTICE,
1009                 _("Rollback packages (+%d/-%d) to %-24.24s (0x%08x):\n"),
1010                         numAdded, numRemoved, ctime(&tid), tid);
1011
1012         rc = rpmtsCheck(ts);
1013         ps = rpmtsProblems(ts);
1014         if (rc != 0 && rpmpsNumProblems(ps) > 0) {
1015             rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
1016             rpmpsPrint(NULL, ps);
1017             ps = rpmpsFree(ps);
1018             goto exit;
1019         }
1020         ps = rpmpsFree(ps);
1021
1022         rc = rpmtsOrder(ts);
1023         if (rc != 0)
1024             goto exit;
1025
1026         /* Drop added/available package indices and dependency sets. */
1027         rpmtsClean(ts);
1028
1029         rc = rpmtsRun(ts, NULL, (ia->probFilter|RPMPROB_FILTER_OLDPACKAGE));
1030         ps = rpmtsProblems(ts);
1031         if (rc > 0 && rpmpsNumProblems(ps) > 0)
1032             rpmpsPrint(stderr, ps);
1033         ps = rpmpsFree(ps);
1034         if (rc)
1035             goto exit;
1036
1037         /* Clean up after successful rollback. */
1038         if (rtids && !rpmIsDebug()) {
1039             int i;
1040             rpmlog(RPMLOG_NOTICE, _("Cleaning up repackaged packages:\n"));
1041             if (rtids->idt)
1042             for (i = 0; i < rtids->nidt; i++) {
1043                 IDT rrp = rtids->idt + i;
1044                 if (rrp->val.u32 != thistid)
1045                     continue;
1046                 if (rrp->key) { /* XXX can't happen */
1047                     rpmlog(RPMLOG_NOTICE, _("\tRemoving %s:\n"), rrp->key);
1048                     (void) unlink(rrp->key);    /* XXX: Should check rc??? */
1049                 }
1050             }
1051         }
1052
1053
1054     } while (1);
1055
1056 exit:
1057     rtids = IDTXfree(rtids);
1058     itids = IDTXfree(itids);
1059
1060     rpmtsEmpty(ts);
1061     (void) rpmtsSetFlags(ts, transFlags);
1062
1063     return rc;
1064 }