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 int rpmcliPackagesTotal = 0;
21 int rpmcliHashesCurrent = 0;
22 int rpmcliHashesTotal = 0;
23 int rpmcliProgressCurrent = 0;
24 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_TRANS_PROGRESS:
156 case RPMCALLBACK_INST_PROGRESS:
157 if (flags & INSTALL_PERCENT)
158 fprintf(stdout, "%%%% %f\n", (double) (total
159 ? ((((float) amount) / total) * 100)
161 else if (flags & INSTALL_HASH)
162 printHash(amount, total);
163 (void) fflush(stdout);
166 case RPMCALLBACK_TRANS_START:
167 rpmcliHashesCurrent = 0;
168 rpmcliProgressTotal = 1;
169 rpmcliProgressCurrent = 0;
170 if (!(flags & INSTALL_LABEL))
172 if (flags & INSTALL_HASH)
173 fprintf(stdout, "%-28s", _("Preparing..."));
175 fprintf(stdout, "%s\n", _("Preparing packages for installation..."));
176 (void) fflush(stdout);
179 case RPMCALLBACK_TRANS_STOP:
180 if (flags & INSTALL_HASH)
181 printHash(1, 1); /* Fixes "preparing..." progress bar */
182 rpmcliProgressTotal = rpmcliPackagesTotal;
183 rpmcliProgressCurrent = 0;
186 case RPMCALLBACK_UNINST_PROGRESS:
188 case RPMCALLBACK_UNINST_START:
190 case RPMCALLBACK_UNINST_STOP:
192 case RPMCALLBACK_UNPACK_ERROR:
194 case RPMCALLBACK_CPIO_ERROR:
196 case RPMCALLBACK_SCRIPT_ERROR:
198 case RPMCALLBACK_UNKNOWN:
206 static void setNotifyFlag(struct rpmInstallArguments_s * ia,
211 notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
212 rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
228 rpmRelocation * relocations;
232 static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
240 int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
242 if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
244 if (rpmtsCheck(ts)) {
249 ps = rpmtsProblems(ts);
250 if (!stop && rpmpsNumProblems(ps) > 0) {
251 rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
252 rpmpsPrint(NULL, ps);
259 if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
260 if (rpmtsOrder(ts)) {
266 if (numPackages && !stop) {
267 rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
268 "installing binary packages\n");
270 rc = rpmtsRun(ts, NULL, ia->probFilter);
272 ps = rpmtsProblems(ts);
274 if ((rpmpsNumProblems(ps) > 0) && (eflags? 1 : (rc > 0)))
275 rpmpsPrint((eflags? NULL : stderr), ps);
282 static int tryReadManifest(struct rpmEIU * eiu)
286 /* Try to read a package manifest. */
287 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
288 if (fd == NULL || Ferror(fd)) {
289 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
295 eiu->numFailed++; *eiu->fnp = NULL;
299 /* Read list of packages from manifest. */
300 rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
302 rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
303 *eiu->fnp, Fstrerror(fd));
308 eiu->numFailed++; *eiu->fnp = NULL;
313 static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, Header * hdrp)
315 /* Try to read the header from a package file. */
316 FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
317 if (fd == NULL || Ferror(fd)) {
318 rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
324 eiu->numFailed++; *eiu->fnp = NULL;
328 /* Read the header, verifying signatures (if present). */
329 eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, hdrp);
333 /* Honor --nomanifest */
334 if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
335 eiu->rpmrc = RPMRC_FAIL;
337 if(eiu->rpmrc == RPMRC_FAIL) {
338 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
339 eiu->numFailed++; *eiu->fnp = NULL;
346 /* On --freshen, verify package is installed and newer */
347 static int checkFreshenStatus(rpmts ts, Header h)
349 rpmdbMatchIterator mi = NULL;
350 const char * name = headerGetString(h, RPMTAG_NAME);
351 const char *arch = headerGetString(h, RPMTAG_ARCH);
355 mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
356 if (rpmtsColor(ts) && arch)
357 rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
359 while ((oldH = rpmdbNextIterator(mi)) != NULL) {
360 /* Package is newer than those currently installed. */
361 if (rpmVersionCompare(oldH, h) < 0)
365 rpmdbFreeIterator(mi);
366 return (oldH != NULL);
369 /** @todo Generalize --freshen policies. */
370 int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
372 struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
373 rpmRelocation * relocations;
374 char * fileURL = NULL;
375 rpmVSFlags vsflags, ovsflags;
379 vsflags = setvsFlags(ia);
380 ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
382 if (fileArgv == NULL) goto exit;
384 rpmcliPackagesTotal = 0;
386 (void) rpmtsSetFlags(ts, ia->transFlags);
388 relocations = ia->relocations;
390 setNotifyFlag(ia, ts);
392 if ((eiu->relocations = relocations) != NULL) {
393 while (eiu->relocations->oldPath)
395 if (eiu->relocations->newPath == NULL)
396 eiu->relocations = NULL;
399 /* Build fully globbed list of arguments in argv[argc]. */
400 for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
405 fn = rpmEscapeSpaces(*eiu->fnp);
406 rc = rpmGlob(fn, &ac, &av);
409 rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
414 argvAppend(&(eiu->argv), av);
420 /* Allocate sufficient storage for next set of args. */
421 if (eiu->pkgx >= eiu->numPkgs) {
422 eiu->numPkgs = eiu->pkgx + eiu->argc;
423 eiu->pkgURL = xrealloc(eiu->pkgURL,
424 (eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
425 memset(eiu->pkgURL + eiu->pkgx, 0,
426 ((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
427 eiu->pkgState = xrealloc(eiu->pkgState,
428 (eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
429 memset(eiu->pkgState + eiu->pkgx, 0,
430 ((eiu->argc + 1) * sizeof(*eiu->pkgState)));
433 /* Retrieve next set of args, cache on local storage. */
434 for (i = 0; i < eiu->argc; i++) {
435 fileURL = _free(fileURL);
436 fileURL = eiu->argv[i];
439 switch (urlIsURL(fileURL)) {
447 fprintf(stdout, _("Retrieving %s\n"), fileURL);
449 tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn);
452 rc = urlGetFile(fileURL, tfn);
459 _("skipping %s - transfer failed\n"), fileURL);
461 eiu->pkgURL[eiu->pkgx] = NULL;
465 eiu->pkgState[eiu->pkgx] = 1;
466 eiu->pkgURL[eiu->pkgx] = tfn;
470 case URL_IS_DASH: /* WRONG WRONG WRONG */
471 case URL_IS_HKP: /* WRONG WRONG WRONG */
473 eiu->pkgURL[eiu->pkgx] = fileURL;
479 fileURL = _free(fileURL);
481 if (eiu->numFailed) goto exit;
483 /* Continue processing file arguments, building transaction set. */
484 for (eiu->fnp = eiu->pkgURL+eiu->prevx;
486 eiu->fnp++, eiu->prevx++)
489 const char * fileName;
491 rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
492 (void) urlPath(*eiu->fnp, &fileName);
494 if (tryReadHeader(ts, eiu, &h) == RPMRC_FAIL)
497 if (eiu->rpmrc == RPMRC_NOTFOUND) {
498 rc = tryReadManifest(eiu);
499 if (rc == RPMRC_OK) {
505 if (headerIsSource(h)) {
506 rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
508 eiu->sourceURL = xrealloc(eiu->sourceURL,
509 (eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
510 eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
513 eiu->sourceURL[eiu->numSRPMS] = NULL;
517 if (eiu->relocations) {
518 struct rpmtd_s prefixes;
520 headerGet(h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
521 if (rpmtdCount(&prefixes) == 1) {
522 eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
523 rpmtdFreeData(&prefixes);
525 rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
526 headerGetString(h, RPMTAG_NAME));
532 if (ia->installInterfaceFlags & INSTALL_FRESHEN)
533 if (checkFreshenStatus(ts, h) != 1) {
538 rc = rpmtsAddInstallElement(ts, h, (fnpyKey)fileName,
539 (ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
543 if (eiu->relocations)
544 eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
548 rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
553 _("error reading from file %s\n"), *eiu->fnp);
566 rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
567 eiu->numSRPMS, eiu->numRPMS);
569 if (eiu->numFailed) goto exit;
572 int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
574 eiu->numFailed += eiu->numRPMS;
576 eiu->numFailed += rc;
579 if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
580 for (i = 0; i < eiu->numSRPMS; i++) {
582 if (eiu->sourceURL[i] != NULL) {
584 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
585 rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
593 if (eiu->pkgURL != NULL) {
594 for (i = 0; i < eiu->numPkgs; i++) {
595 if (eiu->pkgURL[i] == NULL) continue;
596 if (eiu->pkgState[i] == 1)
597 (void) unlink(eiu->pkgURL[i]);
598 eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
601 eiu->pkgState = _free(eiu->pkgState);
602 eiu->pkgURL = _free(eiu->pkgURL);
603 eiu->argv = _free(eiu->argv);
608 rpmtsSetVSFlags(ts, ovsflags);
613 int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
619 rpmVSFlags vsflags, ovsflags;
621 if (argv == NULL) return 0;
623 vsflags = setvsFlags(ia);
624 ovsflags = rpmtsSetVSFlags(ts, vsflags);
626 (void) rpmtsSetFlags(ts, ia->transFlags);
628 #ifdef NOTYET /* XXX no callbacks on erase yet */
629 setNotifyFlag(ia, ts);
632 qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
633 for (arg = argv; *arg; arg++) {
634 rpmdbMatchIterator mi;
638 /* Iterator count isn't reliable with labels, count manually... */
639 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
640 while (rpmdbNextIterator(mi) != NULL) {
643 rpmdbFreeIterator(mi);
646 rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
649 Header h; /* XXX iterator owns the reference */
652 !(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
653 rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
659 mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
660 while ((h = rpmdbNextIterator(mi)) != NULL) {
662 (void) rpmtsAddEraseElement(ts, h, -1);
665 char *nevra = headerFormat(h, qfmt, NULL);
666 rpmlog(RPMLOG_NOTICE, " %s", nevra);
670 rpmdbFreeIterator(mi);
675 if (numFailed) goto exit;
676 numFailed = rpmcliTransaction(ts, ia, numPackages);
679 rpmtsSetVSFlags(ts, ovsflags);
684 int rpmInstallSource(rpmts ts, const char * arg,
685 char ** specFilePtr, char ** cookie)
691 fd = Fopen(arg, "r.ufdio");
692 if (fd == NULL || Ferror(fd)) {
693 rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
694 if (fd != NULL) (void) Fclose(fd);
698 if (rpmIsVerbose() && specFilePtr != NULL)
699 fprintf(stdout, _("Installing %s\n"), arg);
702 rpmVSFlags ovsflags =
703 rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
704 rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
705 rc = (rpmrc == RPMRC_OK ? 0 : 1);
706 rpmtsSetVSFlags(ts, ovsflags);
709 rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
710 if (specFilePtr && *specFilePtr)
711 *specFilePtr = _free(*specFilePtr);
712 if (cookie && *cookie)
713 *cookie = _free(*cookie);