예제 #1
0
int
xbps_pkgdb_lock(struct xbps_handle *xhp)
{
	mode_t prev_umask;
	int rv;
	/*
	 * Use a mandatory file lock to only allow one writer to pkgdb,
	 * other writers will block.
	 */
	xhp->pkgdb_plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
	if (xbps_pkgdb_init(xhp) == ENOENT) {
		/* if metadir does not exist, create it */
		if (access(xhp->metadir, R_OK|X_OK) == -1) {
			if (errno != ENOENT)
				return errno;

			if (xbps_mkpath(xhp->metadir, 0755) == -1) {
				rv = errno;
				xbps_dbg_printf(xhp, "[pkgdb] failed to create metadir "
				    "%s: %s\n", xhp->metadir, strerror(rv));
				return rv;
			}
		}
		/* if pkgdb is unexistent, create it with an empty dictionary */
		xhp->pkgdb = xbps_dictionary_create();
		if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist)) {
			rv = errno;
			xbps_dbg_printf(xhp, "[pkgdb] failed to create pkgdb "
			    "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
			return rv;
		}
	}

	prev_umask = umask(022);
	if ((pkgdb_fd = open(xhp->pkgdb_plist, O_CREAT|O_RDWR|O_CLOEXEC, 0664)) == -1) {
		rv = errno;
		xbps_dbg_printf(xhp, "[pkgdb] cannot open pkgdb for locking "
		    "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
		free(xhp->pkgdb_plist);
		umask(prev_umask);
		return rv;
	}
	umask(prev_umask);

	if (lockf(pkgdb_fd, F_TLOCK, 0) == -1) {
		rv = errno;
		xbps_dbg_printf(xhp, "[pkgdb] cannot lock pkgdb: %s\n", strerror(rv));
		return rv;
	}
	return 0;
}
예제 #2
0
파일: repo_sync.c 프로젝트: bougyman/xbps
/*
 * Returns -1 on error, 0 if transfer was not necessary (local/remote
 * size and/or mtime match) and 1 if downloaded successfully.
 */
int HIDDEN
xbps_repo_sync(struct xbps_handle *xhp, const char *uri)
{
	const char *arch, *fetchstr = NULL;
	char *repodata, *lrepodir, *uri_fixedp;
	int rv = 0;

	assert(uri != NULL);

	/* ignore non remote repositories */
	if (!xbps_repository_is_remote(uri))
		return 0;

	uri_fixedp = xbps_get_remote_repo_string(uri);
	if (uri_fixedp == NULL)
		return -1;

	if (xhp->target_arch)
		arch = xhp->target_arch;
	else
		arch = xhp->native_arch;

	/*
	 * Full path to repository directory to store the plist
	 * index file.
	 */
	lrepodir = xbps_xasprintf("%s/%s", xhp->metadir, uri_fixedp);
	free(uri_fixedp);
	/*
	 * Create repodir in metadir.
	 */
	if ((rv = xbps_mkpath(lrepodir, 0755)) == -1) {
		if (errno != EEXIST) {
			xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
			    errno, NULL, "[reposync] failed "
			    "to create repodir `%s': %s", lrepodir,
			strerror(errno));
			free(lrepodir);
			return rv;
		}
	}
	if (chdir(lrepodir) == -1) {
		xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL, errno, NULL,
		    "[reposync] failed to change dir to repodir `%s': %s",
		    lrepodir, strerror(errno));
		free(lrepodir);
		return -1;
	}
	free(lrepodir);
	/*
	 * Remote repository plist index full URL.
	 */
	repodata = xbps_xasprintf("%s/%s-repodata", uri, arch);

	/* reposync start cb */
	xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC, 0, repodata, NULL);
	/*
	 * Download plist index file from repository.
	 */
	if ((rv = xbps_fetch_file(xhp, repodata, NULL)) == -1) {
		/* reposync error cb */
		fetchstr = xbps_fetch_error_string();
		xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
		    fetchLastErrCode != 0 ? fetchLastErrCode : errno, NULL,
		    "[reposync] failed to fetch file `%s': %s",
		    repodata, fetchstr ? fetchstr : strerror(errno));
	} else if (rv == 1)
		rv = 0;

	free(repodata);

	return rv;
}
예제 #3
0
static int
create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname)
{
	unsigned int i, cnt;

	cnt = xbps_array_count(a);
	for (i = 0; i < cnt; i++) {
		xbps_string_t str;
		char *tgt_dup, *tgt_dir, *lnk_dup, *lnk_dir;
		char *l, *lnk, *tgt = NULL;
		const char *tgt0;
		int rv;

		str = xbps_array_get(a, i);
		l = left(xbps_string_cstring_nocopy(str));
		assert(l);
		tgt0 = right(xbps_string_cstring_nocopy(str));
		assert(tgt0);
		/* always create target dir, for dangling symlinks */
		tgt_dup = strdup(tgt0);
		assert(tgt_dup);
		tgt_dir = dirname(tgt_dup);
		if (strcmp(tgt_dir, ".")) {
			tgt = xbps_xasprintf("%s%s", xhp->rootdir, tgt_dir);
			if (xbps_mkpath(tgt, 0755) != 0) {
				if (errno != EEXIST) {
					rv = errno;
					xbps_dbg_printf(xhp, "failed to create "
					    "target dir '%s' for group '%s': %s\n",
					    tgt, grname, strerror(errno));
					free(tgt_dup);
					free(tgt);
					free(l);
					return rv;
				}
			}
			free(tgt);
		}
		/* always create link dir, for dangling symlinks */
		lnk_dup = strdup(l);
		assert(lnk_dup);
		lnk_dir = dirname(lnk_dup);
		if (strcmp(lnk_dir, ".")) {
			lnk = xbps_xasprintf("%s%s", xhp->rootdir, lnk_dir);
			if (xbps_mkpath(lnk, 0755) != 0) {
				if (errno != EEXIST) {
					rv = errno;
					xbps_dbg_printf(xhp, "failed to create symlink"
					    "dir '%s' for group '%s': %s\n",
					    lnk, grname, strerror(errno));
					free(tgt_dup);
					free(lnk_dup);
					free(lnk);
					free(l);
					return rv;
				}
			}
			free(lnk);
		}
		free(lnk_dup);

		if (l[0] != '/') {
			lnk = xbps_xasprintf("%s%s/%s", xhp->rootdir, tgt_dir, l);
			free(tgt_dup);
			tgt_dup = strdup(tgt0);
			assert(tgt_dup);
			tgt = strdup(basename(tgt_dup));
			free(tgt_dup);
		} else {
			free(tgt_dup);
			tgt = strdup(tgt0);
			lnk = xbps_xasprintf("%s%s", xhp->rootdir, l);
		}
		xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_ADDED, 0, NULL,
		    "Creating '%s' alternatives group symlink: %s -> %s", grname, l, tgt);
		unlink(lnk);
		if (tgt[0] == '/') {
			tgt_dup = relpath(lnk + strlen(xhp->rootdir), tgt);
			free(tgt);
			tgt = tgt_dup;
		}
		if ((rv = symlink(tgt, lnk)) != 0) {
			xbps_dbg_printf(xhp, "failed to create alt symlink '%s'"
			    "for group '%s': %s\n", lnk, grname,
			    strerror(errno));
			free(tgt);
			free(lnk);
			free(l);
			return rv;
		}
		free(tgt);
		free(lnk);
		free(l);
	}

	return 0;
}
예제 #4
0
파일: repo.c 프로젝트: niamtokik/xbps
int
xbps_repo_key_import(struct xbps_repo *repo)
{
	xbps_dictionary_t repokeyd = NULL;
	xbps_data_t pubkey = NULL;
	uint16_t pubkey_size = 0;
	const char *signedby = NULL;
	char *hexfp = NULL;
	char *p, *dbkeyd, *rkeyfile = NULL;
	int import, rv = 0;

	assert(repo);
	/*
	 * If repository does not have required metadata plist, ignore it.
	 */
	if (!xbps_dictionary_count(repo->idxmeta)) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' unsigned repository!\n", repo->uri);
		return 0;
	}
	/*
	 * Check for required objects in index-meta:
	 * 	- signature-by (string)
	 * 	- public-key (data)
	 * 	- public-key-size (number)
	 */
	xbps_dictionary_get_cstring_nocopy(repo->idxmeta, "signature-by", &signedby);
	xbps_dictionary_get_uint16(repo->idxmeta, "public-key-size", &pubkey_size);
	pubkey = xbps_dictionary_get(repo->idxmeta, "public-key");

	if (signedby == NULL || pubkey_size == 0 ||
	    xbps_object_type(pubkey) != XBPS_TYPE_DATA) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s': incomplete signed repository "
		    "(missing objs)\n", repo->uri);
		rv = EINVAL;
		goto out;
	}
	hexfp = xbps_pubkey2fp(repo->xhp, pubkey);
	/*
	 * Check if the public key is alredy stored.
	 */
	rkeyfile = xbps_xasprintf("%s/keys/%s.plist", repo->xhp->metadir, hexfp);
	repokeyd = xbps_plist_dictionary_from_file(repo->xhp, rkeyfile);
	if (xbps_object_type(repokeyd) == XBPS_TYPE_DICTIONARY) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' public key already stored.\n", repo->uri);
		goto out;
	}
	/*
	 * Notify the client and take appropiate action to import
	 * the repository public key. Pass back the public key openssh fingerprint
	 * to the client.
	 */
	import = xbps_set_cb_state(repo->xhp, XBPS_STATE_REPO_KEY_IMPORT, 0,
			hexfp, "`%s' repository has been RSA signed by \"%s\"",
			repo->uri, signedby);
	if (import <= 0) {
		rv = EAGAIN;
		goto out;
	}

	p = strdup(rkeyfile);
	dbkeyd = dirname(p);
	assert(dbkeyd);
	if (access(dbkeyd, R_OK|W_OK) == -1) {
		rv = errno;
		if (rv == ENOENT)
			rv = xbps_mkpath(dbkeyd, 0755);
		if (rv != 0) {
			rv = errno;
			xbps_dbg_printf(repo->xhp,
			    "[repo] `%s' cannot create %s: %s\n",
			    repo->uri, dbkeyd, strerror(errno));
			free(p);
			goto out;
		}
	}
	free(p);

	repokeyd = xbps_dictionary_create();
	xbps_dictionary_set(repokeyd, "public-key", pubkey);
	xbps_dictionary_set_uint16(repokeyd, "public-key-size", pubkey_size);
	xbps_dictionary_set_cstring_nocopy(repokeyd, "signature-by", signedby);

	if (!xbps_dictionary_externalize_to_file(repokeyd, rkeyfile)) {
		rv = errno;
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' failed to externalize %s: %s\n",
		    repo->uri, rkeyfile, strerror(rv));
	}

out:
	if (hexfp)
		free(hexfp);
	if (repokeyd)
		xbps_object_release(repokeyd);
	if (rkeyfile)
		free(rkeyfile);
	return rv;
}
예제 #5
0
static int
create_pkg_metaplist(struct xbps_handle *xhp, const char *pkgname, const char *pkgver,
		     xbps_dictionary_t propsd, xbps_dictionary_t filesd,
		     const void *instbuf, const size_t instbufsiz,
		     const void *rembuf, const size_t rembufsiz)
{
	xbps_array_t array;
	xbps_dictionary_t pkg_metad;
	xbps_data_t data;
	char *buf;
	int rv = 0;

	xbps_dictionary_make_immutable(propsd);
	pkg_metad = xbps_dictionary_copy_mutable(propsd);

	/* Add objects from XBPS_PKGFILES */
	array = xbps_dictionary_get(filesd, "files");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "files", array);
	array = xbps_dictionary_get(filesd, "conf_files");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "conf_files", array);
	array = xbps_dictionary_get(filesd, "links");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "links", array);
	array = xbps_dictionary_get(filesd, "dirs");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "dirs", array);

	/* Add install/remove scripts data objects */
	if (instbuf != NULL) {
		data = xbps_data_create_data(instbuf, instbufsiz);
		assert(data);
		xbps_dictionary_set(pkg_metad, "install-script", data);
		xbps_object_release(data);
	}
	if (rembuf != NULL) {
		data = xbps_data_create_data(rembuf, rembufsiz);
		assert(data);
		xbps_dictionary_set(pkg_metad, "remove-script", data);
		xbps_object_release(data);
	}
	/* Remove unneeded objs from transaction */
	xbps_dictionary_remove(pkg_metad, "remove-and-update");
	xbps_dictionary_remove(pkg_metad, "transaction");
	xbps_dictionary_remove(pkg_metad, "state");
	xbps_dictionary_remove(pkg_metad, "pkgname");
	xbps_dictionary_remove(pkg_metad, "version");

	/*
	 * Externalize pkg dictionary to metadir.
	 */
	if (access(xhp->metadir, R_OK|X_OK) == -1) {
		if (errno == ENOENT) {
			xbps_mkpath(xhp->metadir, 0755);
		} else {
			return errno;
		}
	}
	buf = xbps_xasprintf("%s/.%s.plist", XBPS_META_PATH, pkgname);
	if (!xbps_dictionary_externalize_to_file(pkg_metad, buf)) {
		rv = errno;
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    errno, pkgver,
		    "%s: [unpack] failed to write metadata file `%s': %s",
		    pkgver, buf, strerror(errno));
	}
	free(buf);
	xbps_object_release(pkg_metad);

	return rv;
}
예제 #6
0
static int
unpack_archive(struct xbps_handle *xhp,
	       xbps_dictionary_t pkg_repod,
	       const char *pkgver,
	       const char *fname,
	       struct archive *ar)
{
	xbps_dictionary_t propsd, filesd, old_filesd;
	xbps_array_t array, obsoletes;
	xbps_object_t obj;
	void *instbuf = NULL, *rembuf = NULL;
	struct stat st;
	struct xbps_unpack_cb_data xucd;
	struct archive_entry *entry;
	size_t i, entry_idx = 0, instbufsiz = 0, rembufsiz = 0;
	ssize_t entry_size;
	const char *file, *entry_pname, *transact,  *tgtlnk;
	char *pkgname, *dname, *buf, *buf2, *p, *p2;
	int ar_rv, rv, entry_type, flags;
	bool preserve, update, conf_file, file_exists, skip_obsoletes;
	bool softreplace, skip_extract, force, metafile;
	uid_t euid;

	assert(xbps_object_type(pkg_repod) == XBPS_TYPE_DICTIONARY);
	assert(ar != NULL);

	propsd = filesd = old_filesd = NULL;
	force = preserve = update = conf_file = file_exists = false;
	skip_obsoletes = softreplace = metafile = false;

	xbps_dictionary_get_bool(pkg_repod, "preserve", &preserve);
	xbps_dictionary_get_bool(pkg_repod, "skip-obsoletes", &skip_obsoletes);
	xbps_dictionary_get_bool(pkg_repod, "softreplace", &softreplace);
	xbps_dictionary_get_cstring_nocopy(pkg_repod,
	    "transaction", &transact);

	euid = geteuid();

	pkgname = xbps_pkg_name(pkgver);
	assert(pkgname);

	if (xhp->flags & XBPS_FLAG_FORCE_UNPACK)
		force = true;

	if (xhp->unpack_cb != NULL) {
		/* initialize data for unpack cb */
		memset(&xucd, 0, sizeof(xucd));
	}
	if (access(xhp->rootdir, R_OK) == -1) {
		if (errno != ENOENT) {
			rv = errno;
			goto out;
		}
		if (xbps_mkpath(xhp->rootdir, 0750) == -1) {
			rv = errno;
			goto out;
		}
	}
	if (chdir(xhp->rootdir) == -1) {
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    errno, pkgver,
		    "%s: [unpack] failed to chdir to rootdir `%s': %s",
		    pkgver, xhp->rootdir, strerror(errno));
		rv = errno;
		goto out;
	}
	if (strcmp(transact, "update") == 0)
		update = true;
	/*
	 * Process the archive files.
	 */
	flags = set_extract_flags(euid);
	for (;;) {
		ar_rv = archive_read_next_header(ar, &entry);
		if (ar_rv == ARCHIVE_EOF || ar_rv == ARCHIVE_FATAL)
			break;
		else if (ar_rv == ARCHIVE_RETRY)
			continue;

		entry_pname = archive_entry_pathname(entry);
		entry_size = archive_entry_size(entry);
		entry_type = archive_entry_filetype(entry);
		/*
		 * Ignore directories from archive.
		 */
		if (entry_type == AE_IFDIR) {
			archive_read_data_skip(ar);
			continue;
		}
		if (strcmp("./INSTALL", entry_pname) == 0) {
			/*
			 * Store file in a buffer and execute
			 * the "pre" action from it.
			 */
			instbufsiz = entry_size;
			instbuf = malloc(entry_size);
			assert(instbuf);

			if (archive_read_data(ar, instbuf, entry_size) !=
			    entry_size) {
				rv = EINVAL;
				goto out;
			}

			rv = xbps_pkg_exec_buffer(xhp, instbuf, instbufsiz,
					pkgver, "pre", update);
			if (rv != 0) {
				xbps_set_cb_state(xhp,
				    XBPS_STATE_UNPACK_FAIL,
				    rv, pkgver,
				    "%s: [unpack] INSTALL script failed "
				    "to execute pre ACTION: %s",
				    pkgver, strerror(rv));
				goto out;
			}
			continue;

		} else if (strcmp("./REMOVE", entry_pname) == 0) {
			/* store file in a buffer */
			rembufsiz = entry_size;
			rembuf = malloc(entry_size);
			assert(rembuf);
			if (archive_read_data(ar, rembuf, entry_size) !=
			    entry_size) {
				rv = EINVAL;
				goto out;
			}
			continue;

		} else if (strcmp("./files.plist", entry_pname) == 0) {
			filesd = xbps_archive_get_dictionary(ar, entry);
			if (filesd == NULL) {
				rv = errno;
				goto out;
			}
			continue;
		} else if (strcmp("./props.plist", entry_pname) == 0) {
			propsd = xbps_archive_get_dictionary(ar, entry);
			if (propsd == NULL) {
				rv = errno;
				goto out;
			}
			continue;
		}
		/*
		 * XXX: duplicate code.
		 * Create the metaplist file before unpacking any real file.
		 */
		if (propsd && filesd && !metafile) {
			rv = create_pkg_metaplist(xhp, pkgname, pkgver,
			    propsd, filesd, instbuf, instbufsiz,
			    rembuf, rembufsiz);
			if (rv != 0) {
				xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
				    rv, pkgver,
				    "%s: [unpack] failed to create metaplist file: %s",
				    pkgver, strerror(rv));
				goto out;
			}
			metafile = true;
		}
		/*
		 * If XBPS_PKGFILES or XBPS_PKGPROPS weren't found
		 * in the archive at this phase, skip all data.
		 */
		if (propsd == NULL || filesd == NULL) {
			archive_read_data_skip(ar);
			/*
			 * If we have processed 4 entries and the two
			 * required metadata files weren't found, bail out.
			 * This is not an XBPS binary package.
			 */
			if (entry_idx >= 3) {
				xbps_set_cb_state(xhp,
				    XBPS_STATE_UNPACK_FAIL, ENODEV, pkgver,
				    "%s: [unpack] invalid binary package `%s'.",
				    pkgver, fname);
				rv = ENODEV;
				goto out;
			}

			entry_idx++;
			continue;
		}
		/*
		 * Prepare unpack callback ops.
		 */
		if (xhp->unpack_cb != NULL) {
			xucd.xhp = xhp;
			xucd.pkgver = pkgver;
			xucd.entry = entry_pname;
			xucd.entry_size = entry_size;
			xucd.entry_is_conf = false;
		}
		/*
		 * Compute total entries in progress data, if set.
		 * total_entries = files + conf_files + links.
		 */
		if (xhp->unpack_cb != NULL) {
			xucd.entry_total_count = 0;
			array = xbps_dictionary_get(filesd, "files");
			xucd.entry_total_count +=
			    (ssize_t)xbps_array_count(array);
			array = xbps_dictionary_get(filesd, "conf_files");
			xucd.entry_total_count +=
			    (ssize_t)xbps_array_count(array);
			array = xbps_dictionary_get(filesd, "links");
			xucd.entry_total_count +=
			    (ssize_t)xbps_array_count(array);
		}
		/*
		 * Always check that extracted file exists and hash
		 * doesn't match, in that case overwrite the file.
		 * Otherwise skip extracting it.
		 */
		conf_file = skip_extract = file_exists = false;
		if (lstat(entry_pname, &st) == 0)
			file_exists = true;

		if (!force && (entry_type == AE_IFREG)) {
			buf = strchr(entry_pname, '.') + 1;
			assert(buf != NULL);
			if (file_exists) {
				/*
				 * Handle configuration files. Check if current
				 * entry is a configuration file and take action
				 * if required. Skip packages that don't have
				 * "conf_files" array on its XBPS_PKGPROPS
				 * dictionary.
				 */
				if (xbps_entry_is_a_conf_file(propsd, buf)) {
					conf_file = true;
					if (xhp->unpack_cb != NULL)
						xucd.entry_is_conf = true;

					rv = xbps_entry_install_conf_file(xhp,
					    filesd, entry, entry_pname, pkgver,
					    pkgname);
					if (rv == -1) {
						/* error */
						goto out;
					} else if (rv == 0) {
						/*
						 * Keep curfile as is.
						 */
						skip_extract = true;
					}
				} else {
					rv = xbps_file_hash_check_dictionary(
					    xhp, filesd, "files", buf);
					if (rv == -1) {
						/* error */
						xbps_dbg_printf(xhp,
						    "%s: failed to check"
						    " hash for `%s': %s\n",
						    pkgver, entry_pname,
						    strerror(errno));
						goto out;
					} else if (rv == 0) {
						/*
						 * hash match, skip extraction.
						 */
						xbps_dbg_printf(xhp,
						    "%s: file %s "
						    "matches existing SHA256, "
						    "skipping...\n",
						    pkgver, entry_pname);
						skip_extract = true;
					}
				}
			}
		} else if (!force && (entry_type == AE_IFLNK)) {
			/*
			 * Check if current link from binpkg hasn't been
			 * modified, otherwise extract new link.
			 */
			buf = realpath(entry_pname, NULL);
			if (buf) {
				if (strcmp(xhp->rootdir, "/")) {
					p = buf;
					p += strlen(xhp->rootdir);
				} else
					p = buf;
				tgtlnk = find_pkg_symlink_target(filesd,
				    entry_pname);
				assert(tgtlnk);
				if (strncmp(tgtlnk, "./", 2) == 0) {
					buf2 = strdup(entry_pname);
					assert(buf2);
					dname = dirname(buf2);
					p2 = xbps_xasprintf("%s/%s", dname, tgtlnk);
					free(buf2);
				} else {
					p2 = strdup(tgtlnk);
					assert(p2);
				}
				xbps_dbg_printf(xhp, "%s: symlink %s cur: %s "
				    "new: %s\n", pkgver, entry_pname, p, p2);

				if (strcmp(p, p2) == 0) {
					xbps_dbg_printf(xhp, "%s: symlink "
					    "%s matched, skipping...\n",
					    pkgver, entry_pname);
					skip_extract = true;
				}
				free(buf);
				free(p2);
			}
		}
		/*
		 * Check if current file mode differs from file mode
		 * in binpkg and apply perms if true.
		 */
		if (!force && file_exists && skip_extract &&
		    (archive_entry_mode(entry) != st.st_mode)) {
			if (chmod(entry_pname,
			    archive_entry_mode(entry)) != 0) {
				xbps_dbg_printf(xhp,
				    "%s: failed "
				    "to set perms %s to %s: %s\n",
				    pkgver, archive_entry_strmode(entry),
				    entry_pname,
				    strerror(errno));
				rv = EINVAL;
				goto out;
			}
			xbps_dbg_printf(xhp, "%s: entry %s changed file "
			    "mode to %s.\n", pkgver, entry_pname,
			    archive_entry_strmode(entry));
		}
		/*
		 * Check if current uid/gid differs from file in binpkg,
		 * and change permissions if true.
		 */
		if ((!force && file_exists && skip_extract && (euid == 0)) &&
		    (((archive_entry_uid(entry) != st.st_uid)) ||
		    ((archive_entry_gid(entry) != st.st_gid)))) {
			if (lchown(entry_pname,
			    archive_entry_uid(entry),
			    archive_entry_gid(entry)) != 0) {
				xbps_dbg_printf(xhp,
				    "%s: failed "
				    "to set uid/gid to %u:%u (%s)\n",
				    pkgver, archive_entry_uid(entry),
				    archive_entry_gid(entry),
				    strerror(errno));
			} else {
				xbps_dbg_printf(xhp, "%s: entry %s changed "
				    "uid/gid to %u:%u.\n", pkgver, entry_pname,
				    archive_entry_uid(entry),
				    archive_entry_gid(entry));
			}
		}

		if (!update && conf_file && file_exists && !skip_extract) {
			/*
			 * If installing new package preserve old configuration
			 * file but renaming it to <file>.old.
			 */
			buf = xbps_xasprintf("%s.old", entry_pname);
			(void)rename(entry_pname, buf);
			free(buf);
			buf = NULL;
			xbps_set_cb_state(xhp,
			    XBPS_STATE_CONFIG_FILE, 0, pkgver,
			    "Renamed old configuration file "
			    "`%s' to `%s.old'.", entry_pname, entry_pname);
		}

		if (!force && skip_extract) {
			archive_read_data_skip(ar);
			continue;
		}
		/*
		 * Reset entry_pname again because if entry's pathname
		 * has been changed it will become a dangling pointer.
		 */
		entry_pname = archive_entry_pathname(entry);
		/*
		 * Extract entry from archive.
		 */
		if (archive_read_extract(ar, entry, flags) != 0) {
			rv = archive_errno(ar);
			xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
			    rv, pkgver,
			    "%s: [unpack] failed to extract file `%s': %s",
			    pkgver, entry_pname, strerror(rv));
		} else {
			if (xhp->unpack_cb != NULL) {
				xucd.entry_extract_count++;
				(*xhp->unpack_cb)(&xucd, xhp->unpack_cb_data);
			}
		}
	}
	/*
	 * XXX: duplicate code.
	 * Create the metaplist file if it wasn't created before.
	 */
	if (propsd && filesd && !metafile) {
		rv = create_pkg_metaplist(xhp, pkgname, pkgver,
		    propsd, filesd, instbuf, instbufsiz,
		    rembuf, rembufsiz);
		if (rv != 0) {
			xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
			    rv, pkgver,
			    "%s: [unpack] failed to create metaplist file: %s",
			    pkgver, strerror(rv));
			goto out;
		}
	}
	/*
	 * If there was any error extracting files from archive, error out.
	 */
	if ((rv = archive_errno(ar)) != 0) {
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    rv, pkgver, NULL,
		    "%s: [unpack] failed to extract files: %s",
		    pkgver, fname, archive_error_string(ar));
		goto out;
	}
	/*
	 * Skip checking for obsolete files on:
	 * 	- New package installation without "softreplace" keyword.
	 * 	- Package with "preserve" keyword.
	 * 	- Package with "skip-obsoletes" keyword.
	 */
	if (skip_obsoletes || preserve || (!softreplace && !update))
		goto out;
	/*
	 * Check and remove obsolete files on:
	 * 	- Package upgrade.
	 * 	- Package with "softreplace" keyword.
	 */
	old_filesd = xbps_pkgdb_get_pkg_metadata(xhp, pkgname);
	if (old_filesd == NULL)
		goto out;

	obsoletes = xbps_find_pkg_obsoletes(xhp, old_filesd, filesd);
	for (i = 0; i < xbps_array_count(obsoletes); i++) {
		obj = xbps_array_get(obsoletes, i);
		file = xbps_string_cstring_nocopy(obj);
		if (remove(file) == -1) {
			xbps_set_cb_state(xhp,
			    XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL,
			    errno, pkgver,
			    "%s: failed to remove obsolete entry `%s': %s",
			    pkgver, file, strerror(errno));
			continue;
		}
		xbps_set_cb_state(xhp,
		    XBPS_STATE_REMOVE_FILE_OBSOLETE,
		    0, pkgver, "%s: removed obsolete entry: %s", pkgver, file);
		xbps_object_release(obj);
	}
	xbps_object_release(old_filesd);

out:
	if (xbps_object_type(filesd) == XBPS_TYPE_DICTIONARY)
		xbps_object_release(filesd);
	if (xbps_object_type(propsd) == XBPS_TYPE_DICTIONARY)
		xbps_object_release(propsd);
	if (pkgname != NULL)
		free(pkgname);
	if (instbuf != NULL)
		free(instbuf);
	if (rembuf != NULL)
		free(rembuf);

	return rv;
}
예제 #7
0
static int
download_binpkgs(struct xbps_handle *xhp, prop_object_iterator_t iter)
{
    prop_object_t obj;
    const char *pkgver, *repoloc, *filen, *trans;
    const char *pkgname, *version, *fetchstr;
    char *binfile;
    int rv = 0;

    while ((obj = prop_object_iterator_next(iter)) != NULL) {
        prop_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
        if ((strcmp(trans, "remove") == 0) ||
                (strcmp(trans, "configure") == 0))
            continue;

        prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
        prop_dictionary_get_cstring_nocopy(obj, "version", &version);
        prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
        assert(repoloc != NULL);
        prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
        assert(pkgver != NULL);
        prop_dictionary_get_cstring_nocopy(obj, "filename", &filen);
        assert(filen != NULL);

        binfile = xbps_path_from_repository_uri(xhp, obj, repoloc);
        if (binfile == NULL) {
            rv = EINVAL;
            break;
        }
        /*
         * If downloaded package is in cachedir continue.
         */
        if (access(binfile, R_OK) == 0) {
            free(binfile);
            continue;
        }
        /*
         * Create cachedir.
         */
        if (xbps_mkpath(xhp->cachedir, 0755) == -1) {
            xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
                              errno, pkgname, version,
                              "%s: [trans] cannot create cachedir `%s': %s",
                              pkgver, xhp->cachedir, strerror(errno));
            free(binfile);
            rv = errno;
            break;
        }
        xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD,
                          0, pkgname, version,
                          "Downloading binary package `%s' (from `%s')...",
                          filen, repoloc);
        /*
         * Fetch binary package.
         */
        rv = xbps_fetch_file(xhp, binfile, xhp->cachedir, false, NULL);
        if (rv == -1) {
            fetchstr = xbps_fetch_error_string();
            xbps_set_cb_state(xhp, XBPS_STATE_DOWNLOAD_FAIL,
                              fetchLastErrCode != 0 ? fetchLastErrCode : errno,
                              pkgname, version,
                              "%s: [trans] failed to download binary package "
                              "`%s' from `%s': %s", pkgver, filen, repoloc,
                              fetchstr ? fetchstr : strerror(errno));
            free(binfile);
            break;
        }
        rv = 0;
        free(binfile);
    }
    prop_object_iterator_reset(iter);

    return rv;
}