Blame lib/rpminstall.c

2ff057
/** \ingroup rpmcli
2ff057
 * \file lib/rpminstall.c
2ff057
 */
2ff057
2ff057
#include "system.h"
2ff057
2ff057
#include <rpm/rpmcli.h>
2ff057
#include <rpm/rpmtag.h>
2ff057
#include <rpm/rpmlib.h>		/* rpmReadPackageFile, vercmp etc */
2ff057
#include <rpm/rpmdb.h>
2ff057
#include <rpm/rpmds.h>
2ff057
#include <rpm/rpmts.h>
2ff057
#include <rpm/rpmsq.h>
2ff057
#include <rpm/rpmlog.h>
2ff057
#include <rpm/rpmfileutil.h>
2ff057
2ff057
#include "lib/rpmgi.h"
2ff057
#include "lib/manifest.h"
2ff057
#include "debug.h"
2ff057
2ff057
static int rpmcliPackagesTotal = 0;
2ff057
static int rpmcliHashesCurrent = 0;
2ff057
static int rpmcliHashesTotal = 0;
2ff057
static int rpmcliProgressCurrent = 0;
2ff057
static int rpmcliProgressTotal = 0;
2ff057
static int rpmcliProgressState = 0;
2ff057
2ff057
/**
2ff057
 * Print a CLI progress bar.
2ff057
 * @todo Unsnarl isatty(STDOUT_FILENO) from the control flow.
2ff057
 * @param amount	current
2ff057
 * @param total		final
2ff057
 */
2ff057
static void printHash(const rpm_loff_t amount, const rpm_loff_t total)
2ff057
{
2ff057
    int hashesNeeded;
2ff057
2ff057
    rpmcliHashesTotal = (isatty (STDOUT_FILENO) ? 34 : 40);
2ff057
2ff057
    if (rpmcliHashesCurrent != rpmcliHashesTotal) {
2ff057
	float pct = (total ? (((float) amount) / total) : 1.0);
2ff057
	hashesNeeded = (rpmcliHashesTotal * pct) + 0.5;
2ff057
	while (hashesNeeded > rpmcliHashesCurrent) {
2ff057
	    if (isatty (STDOUT_FILENO)) {
2ff057
		int i;
2ff057
		for (i = 0; i < rpmcliHashesCurrent; i++)
2ff057
		    (void) putchar ('#');
2ff057
		for (; i < rpmcliHashesTotal; i++)
2ff057
		    (void) putchar (' ');
2ff057
		fprintf(stdout, "(%3d%%)", (int)((100 * pct) + 0.5));
2ff057
		for (i = 0; i < (rpmcliHashesTotal + 6); i++)
2ff057
		    (void) putchar ('\b');
2ff057
	    } else
2ff057
		fprintf(stdout, "#");
2ff057
2ff057
	    rpmcliHashesCurrent++;
2ff057
	}
2ff057
	(void) fflush(stdout);
2ff057
2ff057
	if (rpmcliHashesCurrent == rpmcliHashesTotal) {
2ff057
	    int i;
2ff057
	    rpmcliProgressCurrent++;
2ff057
	    if (isatty(STDOUT_FILENO)) {
2ff057
	        for (i = 1; i < rpmcliHashesCurrent; i++)
2ff057
		    (void) putchar ('#');
2ff057
		pct = (rpmcliProgressTotal
2ff057
		    ? (((float) rpmcliProgressCurrent) / rpmcliProgressTotal)
2ff057
		    : 1);
2ff057
		fprintf(stdout, " [%3d%%]", (int)((100 * pct) + 0.5));
2ff057
	    }
2ff057
	    fprintf(stdout, "\n");
2ff057
	}
2ff057
	(void) fflush(stdout);
2ff057
    }
2ff057
}
2ff057
2ff057
static rpmVSFlags setvsFlags(struct rpmInstallArguments_s * ia)
2ff057
{
2ff057
    rpmVSFlags vsflags;
2ff057
2ff057
    if (ia->installInterfaceFlags & (INSTALL_UPGRADE | INSTALL_ERASE))
2ff057
	vsflags = rpmExpandNumeric("%{?_vsflags_erase}");
2ff057
    else
2ff057
	vsflags = rpmExpandNumeric("%{?_vsflags_install}");
2ff057
2ff057
    if (rpmcliQueryFlags & VERIFY_DIGEST)
2ff057
	vsflags |= _RPMVSF_NODIGESTS;
2ff057
    if (rpmcliQueryFlags & VERIFY_SIGNATURE)
2ff057
	vsflags |= _RPMVSF_NOSIGNATURES;
2ff057
    if (rpmcliQueryFlags & VERIFY_HDRCHK)
2ff057
	vsflags |= RPMVSF_NOHDRCHK;
2ff057
    vsflags |= rpmcliVSFlags;
2ff057
2ff057
    return vsflags;
2ff057
}
2ff057
2ff057
void * rpmShowProgress(const void * arg,
2ff057
			const rpmCallbackType what,
2ff057
			const rpm_loff_t amount,
2ff057
			const rpm_loff_t total,
2ff057
			fnpyKey key,
2ff057
			void * data)
2ff057
{
2ff057
    Header h = (Header) arg;
2ff057
    int flags = (int) ((long)data);
2ff057
    void * rc = NULL;
2ff057
    const char * filename = (const char *)key;
2ff057
    static FD_t fd = NULL;
2ff057
2ff057
    switch (what) {
2ff057
    case RPMCALLBACK_INST_OPEN_FILE:
2ff057
	if (filename == NULL || filename[0] == '\0')
2ff057
	    return NULL;
2ff057
	fd = Fopen(filename, "r.ufdio");
2ff057
	/* FIX: still necessary? */
2ff057
	if (fd == NULL || Ferror(fd)) {
2ff057
	    rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), filename,
2ff057
			Fstrerror(fd));
2ff057
	    if (fd != NULL) {
2ff057
		Fclose(fd);
2ff057
		fd = NULL;
2ff057
	    }
2ff057
	} else
2ff057
	    fd = fdLink(fd);
2ff057
	return (void *)fd;
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_INST_CLOSE_FILE:
2ff057
	/* FIX: still necessary? */
2ff057
	fd = fdFree(fd);
2ff057
	if (fd != NULL) {
2ff057
	    Fclose(fd);
2ff057
	    fd = NULL;
2ff057
	}
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_INST_START:
2ff057
    case RPMCALLBACK_UNINST_START:
2ff057
	if (rpmcliProgressState != what) {
2ff057
	    rpmcliProgressState = what;
2ff057
	    if (flags & INSTALL_HASH) {
2ff057
		if (what == RPMCALLBACK_INST_START) {
2ff057
		    fprintf(stdout, _("Updating / installing...\n"));
2ff057
		} else {
2ff057
		    fprintf(stdout, _("Cleaning up / removing...\n"));
2ff057
		}
2ff057
		fflush(stdout);
2ff057
	    }
2ff057
	}
2ff057
		
2ff057
	rpmcliHashesCurrent = 0;
2ff057
	if (h == NULL || !(flags & INSTALL_LABEL))
2ff057
	    break;
2ff057
	if (flags & INSTALL_HASH) {
2ff057
	    char *s = headerGetAsString(h, RPMTAG_NEVR);
2ff057
	    if (isatty (STDOUT_FILENO))
2ff057
		fprintf(stdout, "%4d:%-33.33s", rpmcliProgressCurrent + 1, s);
2ff057
	    else
2ff057
		fprintf(stdout, "%-38.38s", s);
2ff057
	    (void) fflush(stdout);
2ff057
	    free(s);
2ff057
	} else {
2ff057
	    char *s = headerGetAsString(h, RPMTAG_NEVRA);
2ff057
	    fprintf(stdout, "%s\n", s);
2ff057
	    (void) fflush(stdout);
2ff057
	    free(s);
2ff057
	}
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_INST_STOP:
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_TRANS_PROGRESS:
2ff057
    case RPMCALLBACK_INST_PROGRESS:
2ff057
    case RPMCALLBACK_UNINST_PROGRESS:
2ff057
    case RPMCALLBACK_VERIFY_PROGRESS:
2ff057
	if (flags & INSTALL_PERCENT)
2ff057
	    fprintf(stdout, "%%%% %f\n", (double) (total
2ff057
				? ((((float) amount) / total) * 100)
2ff057
				: 100.0));
2ff057
	else if (flags & INSTALL_HASH)
2ff057
	    printHash(amount, total);
2ff057
	(void) fflush(stdout);
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_TRANS_START:
2ff057
    case RPMCALLBACK_VERIFY_START:
2ff057
	rpmcliHashesCurrent = 0;
2ff057
	rpmcliProgressTotal = 1;
2ff057
	rpmcliProgressCurrent = 0;
2ff057
	rpmcliPackagesTotal = total;
2ff057
	rpmcliProgressState = what;
2ff057
	if (!(flags & INSTALL_LABEL))
2ff057
	    break;
2ff057
	if (flags & INSTALL_HASH) {
2ff057
	    fprintf(stdout, "%-38s", (what == RPMCALLBACK_TRANS_START) ?
2ff057
		    _("Preparing...") : _("Verifying..."));
2ff057
	} else {
2ff057
	    fprintf(stdout, "%s\n", (what == RPMCALLBACK_TRANS_START) ?
2ff057
		    _("Preparing packages...") : _("Verifying packages..."));
2ff057
	}
2ff057
	(void) fflush(stdout);
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_TRANS_STOP:
2ff057
    case RPMCALLBACK_VERIFY_STOP:
2ff057
	if (flags & INSTALL_HASH)
2ff057
	    printHash(1, 1);	/* Fixes "preparing..." progress bar */
2ff057
	rpmcliProgressTotal = rpmcliPackagesTotal;
2ff057
	rpmcliProgressCurrent = 0;
2ff057
	break;
2ff057
2ff057
    case RPMCALLBACK_UNINST_STOP:
2ff057
	break;
2ff057
    case RPMCALLBACK_UNPACK_ERROR:
2ff057
	break;
2ff057
    case RPMCALLBACK_CPIO_ERROR:
2ff057
	break;
2ff057
    case RPMCALLBACK_SCRIPT_ERROR:
2ff057
	break;
2ff057
    case RPMCALLBACK_SCRIPT_START:
2ff057
	break;
2ff057
    case RPMCALLBACK_SCRIPT_STOP:
2ff057
	break;
2ff057
    case RPMCALLBACK_UNKNOWN:
2ff057
    default:
2ff057
	break;
2ff057
    }
2ff057
2ff057
    return rc;
2ff057
}	
2ff057
2ff057
static void setNotifyFlag(struct rpmInstallArguments_s * ia,
2ff057
			  rpmts ts)
2ff057
{
2ff057
    int notifyFlags;
2ff057
2ff057
    notifyFlags = ia->installInterfaceFlags | (rpmIsVerbose() ? INSTALL_LABEL : 0 );
2ff057
    rpmtsSetNotifyCallback(ts, rpmShowProgress, (void *) ((long)notifyFlags));
2ff057
}
2ff057
2ff057
struct rpmEIU {
2ff057
    int numFailed;
2ff057
    int numPkgs;
2ff057
    char ** pkgURL;
2ff057
    char ** fnp;
2ff057
    char * pkgState;
2ff057
    int prevx;
2ff057
    int pkgx;
2ff057
    int numRPMS;
2ff057
    int numSRPMS;
2ff057
    char ** sourceURL;
2ff057
    int argc;
2ff057
    char ** argv;
2ff057
    rpmRelocation * relocations;
2ff057
    rpmRC rpmrc;
2ff057
};
2ff057
2ff057
static int rpmcliTransaction(rpmts ts, struct rpmInstallArguments_s * ia,
2ff057
		      int numPackages)
2ff057
{
2ff057
    rpmps ps;
2ff057
2ff057
    int rc = 0;
2ff057
    int stop = 0;
2ff057
2ff057
    int eflags = ia->installInterfaceFlags & INSTALL_ERASE;
2ff057
2ff057
    if (!(ia->installInterfaceFlags & INSTALL_NODEPS)) {
2ff057
2ff057
	if (rpmtsCheck(ts)) {
2ff057
	    rc = numPackages;
2ff057
	    stop = 1;
2ff057
	}
2ff057
2ff057
	ps = rpmtsProblems(ts);
2ff057
	if (!stop && rpmpsNumProblems(ps) > 0) {
2ff057
	    rpmlog(RPMLOG_ERR, _("Failed dependencies:\n"));
2ff057
	    rpmpsPrint(NULL, ps);
2ff057
	    rc = numPackages;
2ff057
	    stop = 1;
2ff057
	}
2ff057
	ps = rpmpsFree(ps);
2ff057
    }
2ff057
2ff057
    if (!stop && !(ia->installInterfaceFlags & INSTALL_NOORDER)) {
2ff057
	if (rpmtsOrder(ts)) {
2ff057
	    rc = numPackages;
2ff057
	    stop = 1;
2ff057
	}
2ff057
    }
2ff057
2ff057
    if (numPackages && !stop) {
2ff057
	rpmlog(RPMLOG_DEBUG, eflags ? "erasing packages\n" :
2ff057
				      "installing binary packages\n");
2ff057
	rpmtsClean(ts);
2ff057
	rc = rpmtsRun(ts, NULL, ia->probFilter);
2ff057
2ff057
	ps = rpmtsProblems(ts);
2ff057
2ff057
	if (rpmpsNumProblems(ps) > 0 && (eflags || rc > 0))
2ff057
	    rpmpsPrint(NULL, ps);
2ff057
	ps = rpmpsFree(ps);
2ff057
    }
2ff057
2ff057
    return rc;
2ff057
}
2ff057
2ff057
static int tryReadManifest(struct rpmEIU * eiu)
2ff057
{
2ff057
    int rc;
2ff057
2ff057
    /* Try to read a package manifest. */
2ff057
    FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
2ff057
    if (fd == NULL || Ferror(fd)) {
2ff057
        rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
2ff057
	       Fstrerror(fd));
2ff057
	if (fd != NULL) {
2ff057
	    Fclose(fd);
2ff057
	    fd = NULL;
2ff057
	}
2ff057
	eiu->numFailed++;
2ff057
	*eiu->fnp = NULL;
2ff057
	return RPMRC_FAIL;
2ff057
    }
2ff057
2ff057
    /* Read list of packages from manifest. */
2ff057
    rc = rpmReadPackageManifest(fd, &eiu->argc, &eiu->argv);
2ff057
    if (rc != RPMRC_OK)
2ff057
        rpmlog(RPMLOG_ERR, _("%s: not an rpm package (or package manifest): %s\n"),
2ff057
	       *eiu->fnp, Fstrerror(fd));
2ff057
    Fclose(fd);
2ff057
    fd = NULL;
2ff057
2ff057
    if (rc != RPMRC_OK) {
2ff057
        eiu->numFailed++;
2ff057
        *eiu->fnp = NULL;
2ff057
    }
2ff057
2ff057
    return rc;
2ff057
}
2ff057
2ff057
static int tryReadHeader(rpmts ts, struct rpmEIU * eiu, Header * hdrp)
2ff057
{
2ff057
   /* Try to read the header from a package file. */
2ff057
   FD_t fd = Fopen(*eiu->fnp, "r.ufdio");
2ff057
   if (fd == NULL || Ferror(fd)) {
2ff057
       rpmlog(RPMLOG_ERR, _("open of %s failed: %s\n"), *eiu->fnp,
2ff057
	      Fstrerror(fd));
2ff057
       if (fd != NULL) {
2ff057
           Fclose(fd);
2ff057
	   fd = NULL;
2ff057
       }
2ff057
       eiu->numFailed++;
2ff057
       *eiu->fnp = NULL;
2ff057
       return RPMRC_FAIL;
2ff057
   }
2ff057
2ff057
   /* Read the header, verifying signatures (if present). */
2ff057
   eiu->rpmrc = rpmReadPackageFile(ts, fd, *eiu->fnp, hdrp);
2ff057
   Fclose(fd);
2ff057
   fd = NULL;
2ff057
   
2ff057
   /* Honor --nomanifest */
2ff057
   if (eiu->rpmrc == RPMRC_NOTFOUND && (giFlags & RPMGI_NOMANIFEST))
2ff057
       eiu->rpmrc = RPMRC_FAIL;
2ff057
2ff057
   if (eiu->rpmrc == RPMRC_FAIL) {
2ff057
       rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), *eiu->fnp);
2ff057
       eiu->numFailed++;
2ff057
       *eiu->fnp = NULL;
2ff057
   }
2ff057
2ff057
   return RPMRC_OK;
2ff057
}
2ff057
2ff057
2ff057
/* On --freshen, verify package is installed and newer */
2ff057
static int checkFreshenStatus(rpmts ts, Header h)
2ff057
{
2ff057
    rpmdbMatchIterator mi = NULL;
2ff057
    const char * name = headerGetString(h, RPMTAG_NAME);
2ff057
    const char *arch = headerGetString(h, RPMTAG_ARCH);
2ff057
    Header oldH = NULL;
2ff057
2ff057
    if (name != NULL)
2ff057
        mi = rpmtsInitIterator(ts, RPMDBI_NAME, name, 0);
2ff057
    if (rpmtsColor(ts) && arch)
2ff057
	rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
2ff057
2ff057
    while ((oldH = rpmdbNextIterator(mi)) != NULL) {
2ff057
	/* Package is newer than those currently installed. */
2ff057
        if (rpmVersionCompare(oldH, h) < 0)
2ff057
	    break;
2ff057
    }
2ff057
2ff057
    rpmdbFreeIterator(mi);
2ff057
    return (oldH != NULL);
2ff057
}
2ff057
2ff057
static int rpmNoGlob(const char *fn, int *argcPtr, ARGV_t * argvPtr)
2ff057
{
2ff057
    struct stat sb;
2ff057
    int rc = stat(fn, &sb);
2ff057
    if (rc == 0) {
2ff057
	argvAdd(argvPtr, fn);
2ff057
	*argcPtr = 1;
2ff057
    } else {
2ff057
	*argcPtr = 0;
2ff057
    }
2ff057
    return rc;
2ff057
}
2ff057
2ff057
/** @todo Generalize --freshen policies. */
2ff057
int rpmInstall(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_t fileArgv)
2ff057
{
2ff057
    struct rpmEIU * eiu = xcalloc(1, sizeof(*eiu));
2ff057
    rpmRelocation * relocations;
2ff057
    char * fileURL = NULL;
2ff057
    rpmVSFlags vsflags, ovsflags;
2ff057
    rpmVSFlags ovfyflags;
2ff057
    int rc;
2ff057
    int i;
2ff057
2ff057
    vsflags = setvsFlags(ia);
2ff057
    ovsflags = rpmtsSetVSFlags(ts, (vsflags | RPMVSF_NEEDPAYLOAD));
2ff057
    /* for rpm cli, --nosignature/--nodigest applies to both vs and vfyflags */
2ff057
    ovfyflags = rpmtsSetVfyFlags(ts, (rpmtsVfyFlags(ts) | rpmcliVSFlags));
2ff057
2ff057
    if (fileArgv == NULL) goto exit;
2ff057
2ff057
    if (rpmcliVfyLevelMask) {
2ff057
	int vfylevel = rpmtsVfyLevel(ts);
2ff057
	vfylevel &= ~rpmcliVfyLevelMask;
2ff057
	rpmtsSetVfyLevel(ts, vfylevel);
2ff057
    }
2ff057
2ff057
    (void) rpmtsSetFlags(ts, ia->transFlags);
2ff057
2ff057
    relocations = ia->relocations;
2ff057
2ff057
    setNotifyFlag(ia, ts); 
2ff057
2ff057
    if ((eiu->relocations = relocations) != NULL) {
2ff057
	while (eiu->relocations->oldPath)
2ff057
	    eiu->relocations++;
2ff057
	if (eiu->relocations->newPath == NULL)
2ff057
	    eiu->relocations = NULL;
2ff057
    }
2ff057
2ff057
    /* Build fully globbed list of arguments in argv[argc]. */
2ff057
    for (eiu->fnp = fileArgv; *eiu->fnp != NULL; eiu->fnp++) {
2ff057
    	ARGV_t av = NULL;
2ff057
    	int ac = 0;
2ff057
2ff057
	if (giFlags & RPMGI_NOGLOB) {
2ff057
	    rc = rpmNoGlob(*eiu->fnp, &ac, &av;;
2ff057
	} else {
2ff057
	    char * fn = rpmEscapeSpaces(*eiu->fnp);
2ff057
	    rc = rpmGlob(fn, &ac, &av;;
2ff057
	    fn = _free(fn);
2ff057
	}
2ff057
	if (rc || ac == 0) {
2ff057
	    if (giFlags & RPMGI_NOGLOB) {
2ff057
		rpmlog(RPMLOG_ERR, _("File not found: %s\n"), *eiu->fnp);
2ff057
	    } else {
2ff057
		rpmlog(RPMLOG_ERR, _("File not found by glob: %s\n"), *eiu->fnp);
2ff057
	    }
2ff057
	    eiu->numFailed++;
2ff057
	    continue;
2ff057
	}
2ff057
2ff057
	argvAppend(&(eiu->argv), av);
2ff057
	argvFree(av);
2ff057
	eiu->argc += ac;
2ff057
    }
2ff057
2ff057
restart:
2ff057
    /* Allocate sufficient storage for next set of args. */
2ff057
    if (eiu->pkgx >= eiu->numPkgs) {
2ff057
	eiu->numPkgs = eiu->pkgx + eiu->argc;
2ff057
	eiu->pkgURL = xrealloc(eiu->pkgURL,
2ff057
			(eiu->numPkgs + 1) * sizeof(*eiu->pkgURL));
2ff057
	memset(eiu->pkgURL + eiu->pkgx, 0,
2ff057
			((eiu->argc + 1) * sizeof(*eiu->pkgURL)));
2ff057
	eiu->pkgState = xrealloc(eiu->pkgState,
2ff057
			(eiu->numPkgs + 1) * sizeof(*eiu->pkgState));
2ff057
	memset(eiu->pkgState + eiu->pkgx, 0,
2ff057
			((eiu->argc + 1) * sizeof(*eiu->pkgState)));
2ff057
    }
2ff057
2ff057
    /* Retrieve next set of args, cache on local storage. */
2ff057
    for (i = 0; i < eiu->argc; i++) {
2ff057
	fileURL = _free(fileURL);
2ff057
	fileURL = eiu->argv[i];
2ff057
	eiu->argv[i] = NULL;
2ff057
2ff057
	switch (urlIsURL(fileURL)) {
2ff057
	case URL_IS_HTTPS:
2ff057
	case URL_IS_HTTP:
2ff057
	case URL_IS_FTP:
2ff057
	{   char *tfn = NULL;
2ff057
	    FD_t tfd;
2ff057
2ff057
	    if (rpmIsVerbose())
2ff057
		fprintf(stdout, _("Retrieving %s\n"), fileURL);
2ff057
2ff057
	    tfd = rpmMkTempFile(rpmtsRootDir(ts), &tfn;;
2ff057
	    if (tfd && tfn) {
2ff057
		Fclose(tfd);
2ff057
	    	rc = urlGetFile(fileURL, tfn);
2ff057
	    } else {
2ff057
		rc = -1;
2ff057
	    }
2ff057
2ff057
	    if (rc != 0) {
2ff057
		rpmlog(RPMLOG_ERR,
2ff057
			_("skipping %s - transfer failed\n"), fileURL);
2ff057
		eiu->numFailed++;
2ff057
		eiu->pkgURL[eiu->pkgx] = NULL;
2ff057
		tfn = _free(tfn);
2ff057
		break;
2ff057
	    }
2ff057
	    eiu->pkgState[eiu->pkgx] = 1;
2ff057
	    eiu->pkgURL[eiu->pkgx] = tfn;
2ff057
	    eiu->pkgx++;
2ff057
	}   break;
2ff057
	case URL_IS_PATH:
2ff057
	case URL_IS_DASH:	/* WRONG WRONG WRONG */
2ff057
	case URL_IS_HKP:	/* WRONG WRONG WRONG */
2ff057
	default:
2ff057
	    eiu->pkgURL[eiu->pkgx] = fileURL;
2ff057
	    fileURL = NULL;
2ff057
	    eiu->pkgx++;
2ff057
	    break;
2ff057
	}
2ff057
    }
2ff057
    fileURL = _free(fileURL);
2ff057
2ff057
    if (eiu->numFailed) goto exit;
2ff057
2ff057
    /* Continue processing file arguments, building transaction set. */
2ff057
    for (eiu->fnp = eiu->pkgURL+eiu->prevx;
2ff057
	 *eiu->fnp != NULL;
2ff057
	 eiu->fnp++, eiu->prevx++)
2ff057
    {
2ff057
	Header h = NULL;
2ff057
	const char * fileName;
2ff057
2ff057
	rpmlog(RPMLOG_DEBUG, "============== %s\n", *eiu->fnp);
2ff057
	(void) urlPath(*eiu->fnp, &fileName);
2ff057
2ff057
	if (tryReadHeader(ts, eiu, &h) == RPMRC_FAIL)
2ff057
	    continue;
2ff057
2ff057
	if (eiu->rpmrc == RPMRC_NOTFOUND) {
2ff057
	    rc = tryReadManifest(eiu);
2ff057
	    if (rc == RPMRC_OK) {
2ff057
	        eiu->prevx++;
2ff057
	        goto restart;
2ff057
	    }
2ff057
	}
2ff057
2ff057
	if (headerIsSource(h)) {
2ff057
	    if (ia->installInterfaceFlags & INSTALL_FRESHEN) {
2ff057
		headerFree(h);
2ff057
	        continue;
2ff057
	    }
2ff057
	    rpmlog(RPMLOG_DEBUG, "\tadded source package [%d]\n",
2ff057
		eiu->numSRPMS);
2ff057
	    eiu->sourceURL = xrealloc(eiu->sourceURL,
2ff057
				(eiu->numSRPMS + 2) * sizeof(*eiu->sourceURL));
2ff057
	    eiu->sourceURL[eiu->numSRPMS] = *eiu->fnp;
2ff057
	    *eiu->fnp = NULL;
2ff057
	    eiu->numSRPMS++;
2ff057
	    eiu->sourceURL[eiu->numSRPMS] = NULL;
2ff057
	    continue;
2ff057
	}
2ff057
2ff057
	if (eiu->relocations) {
2ff057
	    struct rpmtd_s prefixes;
2ff057
2ff057
	    headerGet(h, RPMTAG_PREFIXES, &prefixes, HEADERGET_DEFAULT);
2ff057
	    if (rpmtdCount(&prefixes) == 1) {
2ff057
		eiu->relocations->oldPath = xstrdup(rpmtdGetString(&prefixes));
2ff057
		rpmtdFreeData(&prefixes);
2ff057
	    } else {
2ff057
		rpmlog(RPMLOG_ERR, _("package %s is not relocatable\n"),
2ff057
		       headerGetString(h, RPMTAG_NAME));
2ff057
		eiu->numFailed++;
2ff057
		goto exit;
2ff057
	    }
2ff057
	}
2ff057
2ff057
	if (ia->installInterfaceFlags & INSTALL_FRESHEN)
2ff057
	    if (checkFreshenStatus(ts, h) != 1) {
2ff057
		headerFree(h);
2ff057
	        continue;
2ff057
	    }
2ff057
2ff057
	if (ia->installInterfaceFlags & INSTALL_REINSTALL)
2ff057
	    rc = rpmtsAddReinstallElement(ts, h, (fnpyKey)fileName);
2ff057
	else
2ff057
	    rc = rpmtsAddInstallElement(ts, h, (fnpyKey)fileName,
2ff057
			(ia->installInterfaceFlags & INSTALL_UPGRADE) != 0,
2ff057
			relocations);
2ff057
2ff057
	headerFree(h);
2ff057
	if (eiu->relocations)
2ff057
	    eiu->relocations->oldPath = _free(eiu->relocations->oldPath);
2ff057
2ff057
	switch (rc) {
2ff057
	case 0:
2ff057
	    rpmlog(RPMLOG_DEBUG, "\tadded binary package [%d]\n",
2ff057
			eiu->numRPMS);
2ff057
	    break;
2ff057
	case 1:
2ff057
	    rpmlog(RPMLOG_ERR,
2ff057
			    _("error reading from file %s\n"), *eiu->fnp);
2ff057
	    eiu->numFailed++;
2ff057
	    goto exit;
2ff057
	    break;
2ff057
	default:
2ff057
	    eiu->numFailed++;
2ff057
	    goto exit;
2ff057
	    break;
2ff057
	}
2ff057
2ff057
	eiu->numRPMS++;
2ff057
    }
2ff057
2ff057
    rpmlog(RPMLOG_DEBUG, "found %d source and %d binary packages\n",
2ff057
		eiu->numSRPMS, eiu->numRPMS);
2ff057
2ff057
    if (eiu->numFailed) goto exit;
2ff057
2ff057
    if (eiu->numRPMS) {
2ff057
        int rc = rpmcliTransaction(ts, ia, eiu->numPkgs);
2ff057
        if (rc < 0)
2ff057
            eiu->numFailed += eiu->numRPMS;
2ff057
	else if (rc > 0)
2ff057
            eiu->numFailed += rc;
2ff057
    }
2ff057
2ff057
    if (eiu->numSRPMS && (eiu->sourceURL != NULL)) {
2ff057
	rpmcliProgressState = 0;
2ff057
	rpmcliProgressTotal = 0;
2ff057
	rpmcliProgressCurrent = 0;
2ff057
	for (i = 0; i < eiu->numSRPMS; i++) {
2ff057
	    rpmsqPoll();
2ff057
	    if (eiu->sourceURL[i] != NULL) {
2ff057
	        rc = RPMRC_OK;
2ff057
		if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_TEST))
2ff057
		    rc = rpmInstallSource(ts, eiu->sourceURL[i], NULL, NULL);
2ff057
		if (rc != 0)
2ff057
		    eiu->numFailed++;
2ff057
	    }
2ff057
	}
2ff057
    }
2ff057
2ff057
exit:
2ff057
    if (eiu->pkgURL != NULL) {
2ff057
        for (i = 0; i < eiu->numPkgs; i++) {
2ff057
	    if (eiu->pkgURL[i] == NULL) continue;
2ff057
	    if (eiu->pkgState[i] == 1)
2ff057
	        (void) unlink(eiu->pkgURL[i]);
2ff057
	    eiu->pkgURL[i] = _free(eiu->pkgURL[i]);
2ff057
	}
2ff057
    }
2ff057
    eiu->pkgState = _free(eiu->pkgState);
2ff057
    eiu->pkgURL = _free(eiu->pkgURL);
2ff057
    eiu->argv = _free(eiu->argv);
2ff057
    rc = eiu->numFailed;
2ff057
    free(eiu);
2ff057
2ff057
    rpmtsEmpty(ts);
2ff057
    rpmtsSetVSFlags(ts, ovsflags);
2ff057
    rpmtsSetVfyFlags(ts, ovfyflags);
2ff057
2ff057
    return rc;
2ff057
}
2ff057
2ff057
int rpmErase(rpmts ts, struct rpmInstallArguments_s * ia, ARGV_const_t argv)
2ff057
{
2ff057
    char * const * arg;
2ff057
    char *qfmt = NULL;
2ff057
    int numFailed = 0;
2ff057
    int numPackages = 0;
2ff057
    rpmVSFlags vsflags, ovsflags;
2ff057
2ff057
    if (argv == NULL) return 0;
2ff057
2ff057
    vsflags = setvsFlags(ia);
2ff057
    ovsflags = rpmtsSetVSFlags(ts, vsflags);
2ff057
2ff057
    (void) rpmtsSetFlags(ts, ia->transFlags);
2ff057
2ff057
    setNotifyFlag(ia, ts);
2ff057
2ff057
    qfmt = rpmExpand("%{?_query_all_fmt}\n", NULL);
2ff057
    for (arg = argv; *arg; arg++) {
2ff057
	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_LABEL, *arg, 0);
2ff057
	int matches = rpmdbGetIteratorCount(mi);
2ff057
	int erasing = 1;
2ff057
2ff057
	if (! matches) {
2ff057
	    rpmlog(RPMLOG_ERR, _("package %s is not installed\n"), *arg);
2ff057
	    numFailed++;
2ff057
	} else {
2ff057
	    Header h;	/* XXX iterator owns the reference */
2ff057
2ff057
	    if (matches > 1 && 
2ff057
		!(ia->installInterfaceFlags & UNINSTALL_ALLMATCHES)) {
2ff057
		rpmlog(RPMLOG_ERR, _("\"%s\" specifies multiple packages:\n"),
2ff057
			*arg);
2ff057
		numFailed++;
2ff057
		erasing = 0;
2ff057
	    }
2ff057
2ff057
	    while ((h = rpmdbNextIterator(mi)) != NULL) {
2ff057
		if (erasing) {
2ff057
		    (void) rpmtsAddEraseElement(ts, h, -1);
2ff057
		    numPackages++;
2ff057
		} else {
2ff057
		    char *nevra = headerFormat(h, qfmt, NULL);
2ff057
		    rpmlog(RPMLOG_NOTICE, "  %s", nevra);
2ff057
		    free(nevra);
2ff057
		}
2ff057
	    }
2ff057
	}
2ff057
	rpmdbFreeIterator(mi);
2ff057
    }
2ff057
    free(qfmt);
2ff057
2ff057
    if (numFailed) goto exit;
2ff057
    numFailed = rpmcliTransaction(ts, ia, numPackages);
2ff057
exit:
2ff057
    rpmtsEmpty(ts);
2ff057
    rpmtsSetVSFlags(ts, ovsflags);
2ff057
2ff057
    return (numFailed < 0) ? numPackages : numFailed;
2ff057
}
2ff057
2ff057
int rpmInstallSource(rpmts ts, const char * arg,
2ff057
		char ** specFilePtr, char ** cookie)
2ff057
{
2ff057
    FD_t fd;
2ff057
    int rc;
2ff057
2ff057
2ff057
    fd = Fopen(arg, "r.ufdio");
2ff057
    if (fd == NULL || Ferror(fd)) {
2ff057
	rpmlog(RPMLOG_ERR, _("cannot open %s: %s\n"), arg, Fstrerror(fd));
2ff057
	if (fd != NULL) (void) Fclose(fd);
2ff057
	return 1;
2ff057
    }
2ff057
2ff057
    if (rpmIsVerbose() && specFilePtr != NULL)
2ff057
	fprintf(stdout, _("Installing %s\n"), arg);
2ff057
2ff057
    {
2ff057
	rpmVSFlags ovsflags =
2ff057
		rpmtsSetVSFlags(ts, (specFilePtr) ? (rpmtsVSFlags(ts) | RPMVSF_NEEDPAYLOAD) : rpmtsVSFlags(ts));
2ff057
	rpmRC rpmrc = rpmInstallSourcePackage(ts, fd, specFilePtr, cookie);
2ff057
	rc = (rpmrc == RPMRC_OK ? 0 : 1);
2ff057
	rpmtsSetVSFlags(ts, ovsflags);
2ff057
    }
2ff057
    if (rc != 0) {
2ff057
	rpmlog(RPMLOG_ERR, _("%s cannot be installed\n"), arg);
2ff057
	if (specFilePtr && *specFilePtr)
2ff057
	    *specFilePtr = _free(*specFilePtr);
2ff057
	if (cookie && *cookie)
2ff057
	    *cookie = _free(*cookie);
2ff057
    }
2ff057
2ff057
    (void) Fclose(fd);
2ff057
2ff057
    return rc;
2ff057
}
2ff057