2 * \file lib/rpminstall.c
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>
16 #include "lib/rpmgi.h"
17 #include "lib/manifest.h"
20 static int rpmcliPackagesTotal = 0;
21 static int rpmcliHashesCurrent = 0;
22 static int rpmcliHashesTotal = 0;
23 static int rpmcliProgressCurrent = 0;
24 static int rpmcliProgressTotal = 0;
27 * Print a CLI progress bar.
28 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
29 * @param amount current
32 static void printHash(const rpm_loff_t amount, const rpm_loff_t total)
36 rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 44 : 50);
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)) {
44 for (i = 0; i < rpmcliHashesCurrent; i++)
46 for (; i < rpmcliHashesTotal; i++)
48 fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
49 for (i = 0; i < (rpmcliHashesTotal + 6); i++)
50 (void) putchar ('\b');
54 rpmcliHashesCurrent++;
56 (void) fflush(stdout);
58 if (rpmcliHashesCurrent == rpmcliHashesTotal) {
60 rpmcliProgressCurrent++;
61 if (isatty(STDOUT_FILENO)) {
62 for (i = 1; i < rpmcliHashesCurrent; i++)
64 pct = (rpmcliProgressTotal
65 ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
67 fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
69 fprintf(stdout, "\n");
71 (void) fflush(stdout);
75 static rpmVSFlags setvsFlags(struct rpmInstallArguments_s * ia)
79 if (ia->installInterfaceFlags & (INSTALL_UPGRADE | INSTALL_ERASE))
80 vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
82 vsflags = rpmExpandNumeric("%{?_vsflags_install}");
84 if (rpmcliQueryFlags & VERIFY_DIGEST)
85 vsflags |= _RPMVSF_NODIGESTS;
86 if (rpmcliQueryFlags & VERIFY_SIGNATURE)
87 vsflags |= _RPMVSF_NOSIGNATURES;
88 if (rpmcliQueryFlags & VERIFY_HDRCHK)
89 vsflags |= RPMVSF_NOHDRCHK;
94 void * rpmShowProgress(const void * arg,
95 const rpmCallbackType what,
96 const rpm_loff_t amount,
97 const rpm_loff_t total,
101 Header h = (Header) arg;
102 int flags = (int) ((long)data);
104 const char * filename = (const char *)key;
105 static FD_t fd = NULL;
108 case RPMCALLBACK_INST_OPEN_FILE:
109 if (filename == NULL || filename[0] == '\0')
111 fd = Fopen(filename, "r.ufdio");
112 /* FIX: still necessary? */
113 if (fd == NULL || Ferror(fd)) {
114 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
125 case RPMCALLBACK_INST_CLOSE_FILE:
126 /* FIX: still necessary? */
134 case RPMCALLBACK_INST_START:
135 rpmcliHashesCurrent = 0;
136 if (h == NULL || !(flags & INSTALL_LABEL))
138 /* @todo Remove headerFormat() on a progress callback. */
139 if (flags & INSTALL_HASH) {
140 char *s = headerFormat(h, "%{NAME}", NULL);
141 if (isatty (STDOUT_FILENO))
142 fprintf(stdout, "%4d:%-23.23s", rpmcliProgressCurrent + 1, s);
144 fprintf(stdout, "%-28.28s", s);
145 (void) fflush(stdout);
148 char *s = headerFormat(h, "%{NAME}-%{VERSION}-%{RELEASE}", NULL);
149 fprintf(stdout, "%s\n", s);
150 (void) fflush(stdout);
155 case RPMCALLBACK_INST_STOP:
158 case RPMCALLBACK_TRANS_PROGRESS:
159 case RPMCALLBACK_INST_PROGRESS:
160 if (flags & INSTALL_PERCENT)
161 fprintf(stdout, "%%%% %f\n", (double) (total
162 ? ((((float) amount) / total) * 100)
164 else if (flags & INSTALL_HASH)
165 printHash(amount, total);
166 (void) fflush(stdout);
169 case RPMCALLBACK_TRANS_START:
170 rpmcliHashesCurrent = 0;
171 rpmcliProgressTotal = 1;
172 rpmcliProgressCurrent = 0;
173 rpmcliPackagesTotal = total;
174 if (!(flags & INSTALL_LABEL))
176 if (flags & INSTALL_HASH)
177 fprintf(stdout, "%-28s", _("Preparing..."));
179 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
180 (void) fflush(stdout);
183 case RPMCALLBACK_TRANS_STOP:
184 if (flags & INSTALL_HASH)
185 printHash(1, 1); /* Fixes "preparing..." progress bar */
186 rpmcliProgressTotal = rpmcliPackagesTotal;
187 rpmcliProgressCurrent = 0;
190 case RPMCALLBACK_UNINST_PROGRESS:
192 case RPMCALLBACK_UNINST_START:
194 case RPMCALLBACK_UNINST_STOP:
196 case RPMCALLBACK_UNPACK_ERROR:
198 case RPMCALLBACK_CPIO_ERROR:
200 case RPMCALLBACK_SCRIPT_ERROR:
202 case RPMCALLBACK_SCRIPT_START:
204 case RPMCALLBACK_SCRIPT_STOP:
206 case RPMCALLBACK_UNKNOWN:
214 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
219 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
220 rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
236 rpmRelocation * relocations;
240 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
248 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
250 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
252 if (rpmtsCheck(ts)) {
257 ps = rpmtsProblems(ts);
258 if (!stop && rpmpsNumProblems(ps) > 0) {
259 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
260 rpmpsPrint(NULL, ps);
267 if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
268 if (rpmtsOrder(ts)) {
274 if (numPackages && !stop) {
275 rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
276 "installing binary packages\n");
278 rc = rpmtsRun(ts, NULL, ia->probFilter);
280 ps = rpmtsProblems(ts);
282 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
283 rpmpsPrint((eflags? NULL : stderr), ps);
290 static int tryReadManifest(struct rpmEIU * eiu)
294 /* Try to read a package manifest. */
295 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
296 if (fd == NULL || Ferror(fd)) {
297 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
303 eiu->numFailed++; *eiu->fnp = NULL;
307 /* Read list of packages from manifest. */
308 rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
310 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
311 *eiu->fnp, Fstrerror(fd));
316 eiu->numFailed++; *eiu->fnp = NULL;
321 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, Header * hdrp)
323 /* Try to read the header from a package file. */
324 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
325 if (fd == NULL || Ferror(fd)) {
326 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
332 eiu->numFailed++; *eiu->fnp = NULL;
336 /* Read the header, verifying signatures (if present). */
337 eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, hdrp);
341 /* Honor --nomanifest */
342 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
343 eiu->rpmrc = RPMRC_FAIL;
345 if(eiu->rpmrc == RPMRC_FAIL) {
346 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
347 eiu->numFailed++; *eiu->fnp = NULL;
354 /* On --freshen, verify package is installed and newer */
355 static int checkFreshenStatus(rpmts ts, Header h)
357 rpmdbMatchIterator mi = NULL;
358 const char * name = headerGetString(h, RPMTAG_NAME);
359 const char *arch = headerGetString(h, RPMTAG_ARCH);
363 mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
364 if (rpmtsColor(ts) && arch)
365 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
367 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
368 /* Package is newer than those currently installed. */
369 if (rpmVersionCompare(oldH, h) < 0)
373 rpmdbFreeIterator(mi);
374 return (oldH != NULL);
377 /** @todo Generalize --freshen policies. */
378 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
380 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
381 rpmRelocation * relocations;
382 char * fileURL = NULL;
383 rpmVSFlags vsflags, ovsflags;
387 vsflags = setvsFlags(ia);
388 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
390 if (fileArgv == NULL) goto exit;
392 (void) rpmtsSetFlags(ts, ia->transFlags);
394 relocations = ia->relocations;
396 setNotifyFlag(ia, ts);
398 if ((eiu->relocations = relocations) != NULL) {
399 while (eiu->relocations->oldPath)
401 if (eiu->relocations->newPath == NULL)
402 eiu->relocations = NULL;
405 /* Build fully globbed list of arguments in argv[argc]. */
406 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
411 fn = rpmEscapeSpaces(*eiu->fnp);
412 rc = rpmGlob(fn, &ac, &av);
415 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
420 argvAppend(&(eiu->argv), av);
426 /* Allocate sufficient storage for next set of args. */
427 if (eiu->pkgx >= eiu->numPkgs) {
428 eiu->numPkgs = eiu->pkgx + eiu->argc;
429 eiu->pkgURL = xrealloc(eiu->pkgURL,
430 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
431 memset(eiu->pkgURL + eiu->pkgx, 0,
432 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
433 eiu->pkgState = xrealloc(eiu->pkgState,
434 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
435 memset(eiu->pkgState + eiu->pkgx, 0,
436 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
439 /* Retrieve next set of args, cache on local storage. */
440 for (i = 0; i < eiu->argc; i++) {
441 fileURL = _free(fileURL);
442 fileURL = eiu->argv[i];
445 switch (urlIsURL(fileURL)) {
453 fprintf(stdout, _("Retrieving %s\n"), fileURL);
455 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
458 rc = urlGetFile(fileURL, tfn);
465 _("skipping %s - transfer failed\n"), fileURL);
467 eiu->pkgURL[eiu->pkgx] = NULL;
471 eiu->pkgState[eiu->pkgx] = 1;
472 eiu->pkgURL[eiu->pkgx] = tfn;
476 case URL_IS_DASH: /* WRONG WRONG WRONG */
477 case URL_IS_HKP: /* WRONG WRONG WRONG */
479 eiu->pkgURL[eiu->pkgx] = fileURL;
485 fileURL = _free(fileURL);
487 if (eiu->numFailed) goto exit;
489 /* Continue processing file arguments, building transaction set. */
490 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
492 eiu->fnp++, eiu->prevx++)
495 const char * fileName;
497 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
498 (void) urlPath(*eiu->fnp, &fileName);
500 if (tryReadHeader(ts, eiu, &h) == RPMRC_FAIL)
503 if (eiu->rpmrc == RPMRC_NOTFOUND) {
504 rc = tryReadManifest(eiu);
505 if (rc == RPMRC_OK) {
511 if (headerIsSource(h)) {
512 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
514 eiu->sourceURL = xrealloc(eiu->sourceURL,
515 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
516 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
519 eiu->sourceURL[eiu->numSRPMS] = NULL;
523 if (eiu->relocations) {
524 struct rpmtd_s prefixes;
526 headerGet(h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
527 if (rpmtdCount(&prefixes) == 1) {
528 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
529 rpmtdFreeData(&prefixes);
531 rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
532 headerGetString(h, RPMTAG_NAME));
538 if (ia->installInterfaceFlags & INSTALL_FRESHEN)
539 if (checkFreshenStatus(ts, h) != 1) {
544 rc = rpmtsAddInstallElement(ts, h, (fnpyKey)fileName,
545 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
549 if (eiu->relocations)
550 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
554 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
559 _("error reading from file %s\n"), *eiu->fnp);
572 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
573 eiu->numSRPMS, eiu->numRPMS);
575 if (eiu->numFailed) goto exit;
578 int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
580 eiu->numFailed += eiu->numRPMS;
582 eiu->numFailed += rc;
585 if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
586 for (i = 0; i < eiu->numSRPMS; i++) {
588 if (eiu->sourceURL[i] != NULL) {
590 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
591 rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
599 if (eiu->pkgURL != NULL) {
600 for (i = 0; i < eiu->numPkgs; i++) {
601 if (eiu->pkgURL[i] == NULL) continue;
602 if (eiu->pkgState[i] == 1)
603 (void) unlink(eiu->pkgURL[i]);
604 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
607 eiu->pkgState = _free(eiu->pkgState);
608 eiu->pkgURL = _free(eiu->pkgURL);
609 eiu->argv = _free(eiu->argv);
614 rpmtsSetVSFlags(ts, ovsflags);
619 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
625 rpmVSFlags vsflags, ovsflags;
627 if (argv == NULL) return 0;
629 vsflags = setvsFlags(ia);
630 ovsflags = rpmtsSetVSFlags(ts, vsflags);
632 (void) rpmtsSetFlags(ts, ia->transFlags);
634 #ifdef NOTYET /* XXX no callbacks on erase yet */
635 setNotifyFlag(ia, ts);
638 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
639 for (arg = argv; *arg; arg++) {
640 rpmdbMatchIterator mi;
644 /* Iterator count isn't reliable with labels, count manually... */
645 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
646 while (rpmdbNextIterator(mi) != NULL) {
649 rpmdbFreeIterator(mi);
652 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
655 Header h; /* XXX iterator owns the reference */
658 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
659 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
665 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
666 while ((h = rpmdbNextIterator(mi)) != NULL) {
668 (void) rpmtsAddEraseElement(ts, h, -1);
671 char *nevra = headerFormat(h, qfmt, NULL);
672 rpmlog(RPMLOG_NOTICE, " %s", nevra);
676 rpmdbFreeIterator(mi);
681 if (numFailed) goto exit;
682 numFailed = rpmcliTransaction(ts, ia, numPackages);
685 rpmtsSetVSFlags(ts, ovsflags);
690 int rpmInstallSource(rpmts ts, const char * arg,
691 char ** specFilePtr, char ** cookie)
697 fd = Fopen(arg, "r.ufdio");
698 if (fd == NULL || Ferror(fd)) {
699 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
700 if (fd != NULL) (void) Fclose(fd);
704 if (rpmIsVerbose() && specFilePtr != NULL)
705 fprintf(stdout, _("Installing %s\n"), arg);
708 rpmVSFlags ovsflags =
709 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
710 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
711 rc = (rpmrc == RPMRC_OK ? 0 : 1);
712 rpmtsSetVSFlags(ts, ovsflags);
715 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
716 if (specFilePtr && *specFilePtr)
717 *specFilePtr = _free(*specFilePtr);
718 if (cookie && *cookie)
719 *cookie = _free(*cookie);