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