Example #1
0
File: pkg.c Project: AsherBond/pkg
void
pkg_reset(struct pkg *pkg, pkg_t type)
{
	int i;

	if (pkg == NULL)
		return;

	ucl_object_unref(pkg->fields);
	pkg->fields = ucl_object_typed_new(UCL_OBJECT);
	pkg->flags &= ~PKG_LOAD_CATEGORIES;
	pkg->flags &= ~PKG_LOAD_LICENSES;
	pkg->flags &= ~PKG_LOAD_ANNOTATIONS;

	for (i = 0; i < PKG_NUM_SCRIPTS; i++)
		sbuf_reset(pkg->scripts[i]);
	pkg_list_free(pkg, PKG_DEPS);
	pkg_list_free(pkg, PKG_RDEPS);
	pkg_list_free(pkg, PKG_FILES);
	pkg_list_free(pkg, PKG_DIRS);
	pkg_list_free(pkg, PKG_OPTIONS);
	pkg_list_free(pkg, PKG_USERS);
	pkg_list_free(pkg, PKG_GROUPS);
	pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);
	pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);
	pkg_list_free(pkg, PKG_PROVIDES);
	if (pkg->rootfd != -1)
		close(pkg->rootfd);
	pkg->rootfd = -1;

	pkg->type = type;
}
Example #2
0
File: pkg.c Project: AsherBond/pkg
void
pkg_free(struct pkg *pkg)
{
	if (pkg == NULL)
		return;

	ucl_object_unref(pkg->fields);

	for (int i = 0; i < PKG_NUM_SCRIPTS; i++)
		sbuf_free(pkg->scripts[i]);

	pkg_list_free(pkg, PKG_DEPS);
	pkg_list_free(pkg, PKG_RDEPS);
	pkg_list_free(pkg, PKG_FILES);
	pkg_list_free(pkg, PKG_DIRS);
	pkg_list_free(pkg, PKG_OPTIONS);
	pkg_list_free(pkg, PKG_USERS);
	pkg_list_free(pkg, PKG_GROUPS);
	pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);
	pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);
	if (pkg->rootfd != -1)
		close(pkg->rootfd);

	free(pkg);
}
Example #3
0
int
pkg_register_shlibs(struct pkg *pkg, const char *root)
{
	struct pkg_file        *file = NULL;
	char fpath[MAXPATHLEN];

	pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);

	if (elf_version(EV_CURRENT) == EV_NONE)
		return (EPKG_FATAL);

	shlib_list_init();
	if (shlib_list_from_elf_hints(_PATH_ELF_HINTS) != EPKG_OK) {
		shlib_list_free();
		return (EPKG_FATAL);
	}

	while(pkg_files(pkg, &file) == EPKG_OK) {
		if (root != NULL) {
			snprintf(fpath, sizeof(fpath), "%s%s", root, pkg_file_path(file));
			analyse_elf(pkg, fpath, add_shlibs_to_pkg, NULL);
		} else
			analyse_elf(pkg, pkg_file_path(file), add_shlibs_to_pkg, NULL);
	}

	shlib_list_free();
	return (EPKG_OK);
}
Example #4
0
int
pkg_register_shlibs(struct pkg *pkg)
{
	struct pkg_file        *file = NULL;
	bool			shlibs;

	pkg_config_bool(PKG_CONFIG_SHLIBS, &shlibs);

	pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);

	if (!shlibs)
		return (EPKG_OK);

	if (elf_version(EV_CURRENT) == EV_NONE)
		return (EPKG_FATAL);

	shlib_list_init();
	if (shlib_list_from_elf_hints(_PATH_ELF_HINTS) != EPKG_OK) {
		shlib_list_free();
		return (EPKG_FATAL);
	}

	while(pkg_files(pkg, &file) == EPKG_OK)
		analyse_elf(pkg, pkg_file_path(file), add_shlibs_to_pkg, NULL);

	shlib_list_free();
	return (EPKG_OK);
}
Example #5
0
static void
test_pkg_list_prepend(void)
{
	struct pkg_list *head = NULL, *l1, *l2, *l3;
	struct pkginfo pkg;

	pkg_list_prepend(&head, &pkg);
	test_pass(head != NULL);
	test_pass(head->next == NULL);
	test_pass(head->pkg == &pkg);
	l1 = head;

	pkg_list_prepend(&head, &pkg);
	test_pass(head != NULL);
	test_pass(head->next == l1);
	test_pass(head->pkg == &pkg);
	l2 = head;

	pkg_list_prepend(&head, &pkg);
	test_pass(head != NULL);
	test_pass(head->next == l2);
	test_pass(head->pkg == &pkg);
	l3 = head;

	pkg_list_prepend(&head, &pkg);
	test_pass(head != NULL);
	test_pass(head->next == l3);
	test_pass(head->pkg == &pkg);

	pkg_list_free(head);
}
Example #6
0
int
pkg_repo_binary_ensure_loaded(struct pkg_repo *repo,
	struct pkg *pkg, unsigned flags)
{
	sqlite3 *sqlite = PRIV_GET(repo);
	struct pkg_manifest_key *keys = NULL;
	struct pkg *cached = NULL;
	char path[MAXPATHLEN];

	if (pkg->type != PKG_INSTALLED &&
			(flags & (PKG_LOAD_FILES|PKG_LOAD_DIRS)) != 0 &&
			(pkg->flags & (PKG_LOAD_FILES|PKG_LOAD_DIRS)) == 0) {
		/*
		 * Try to get that information from fetched package in cache
		 */
		pkg_manifest_keys_new(&keys);
		
		if (pkg_repo_cached_name(pkg, path, sizeof(path)) != EPKG_OK)
			return (EPKG_FATAL);

		pkg_debug(1, "Binary> loading %s", path);
		if (pkg_open(&cached, path, keys, PKG_OPEN_TRY) != EPKG_OK) {
			pkg_free(cached);
			return (EPKG_FATAL);
		}

		/* Now move required elements to the provided package */
		pkg_list_free(pkg, PKG_FILES);
		pkg_list_free(pkg, PKG_DIRS);
		pkg->files = cached->files;
		pkg->filehash = cached->filehash;
		pkg->dirs = cached->dirs;
		pkg->dirhash = cached->dirhash;
		cached->files = NULL;
		cached->filehash = NULL;
		cached->dirs = NULL;
		cached->dirhash = NULL;

		pkg_free(cached);
		pkg->flags |= (PKG_LOAD_FILES|PKG_LOAD_DIRS);
	}

	return (pkgdb_ensure_loaded_sqlite(sqlite, pkg, flags));
}
Example #7
0
/*
 * Fix up packages in state triggers-awaited w/o the corresponding package
 * with pending triggers. This can happen when dpkg was interrupted
 * while in modstatdb_note, and the package in triggers-pending had its
 * state modified but dpkg could not finish clearing the awaiters.
 *
 * XXX: Possibly get rid of some of the checks done somewhere else for
 *      this condition at run-time.
 */
void
trig_fixup_awaiters(enum modstatdb_rw cstatus)
{
	struct pkg_list *tp;

	if (cstatus < msdbrw_write)
		return;

	for (tp = trig_awaited_pend_head; tp; tp = tp->next)
		if (!tp->pkg->trigpend_head)
			trig_clear_awaiters(tp->pkg);

	pkg_list_free(trig_awaited_pend_head);
	trig_awaited_pend_head = NULL;
}
Example #8
0
static void
test_pkg_list_new(void)
{
	struct pkg_list *l1, *l2, *l3;
	struct pkginfo pkg;

	l1 = pkg_list_new(&pkg, NULL);
	test_pass(l1 != NULL);
	test_pass(l1->next == NULL);
	test_pass(l1->pkg == &pkg);

	l2 = pkg_list_new(&pkg, l1);
	test_pass(l2 != NULL);
	test_pass(l2->next == l1);
	test_pass(l2->pkg == &pkg);

	l3 = pkg_list_new(&pkg, l2);
	test_pass(l3 != NULL);
	test_pass(l3->next == l2);
	test_pass(l3->pkg == &pkg);

	pkg_list_free(l3);
}
Example #9
0
int
pkg_info(struct pkg_info info)
{
	unsigned int cur;
	int retval;
	struct pkg **pkgs;

	retval = 1;
	pkgs = NULL;

	/* -e package name */
	if (info.check_package != NULL) {
		struct pkg *pkg;
		pkg = pkg_db_get_package(info.db, info.check_package);
		if (pkg != NULL) {
			pkg_free(pkg);
			return 0;
		}
		return 1;
	}

	/* -W <filename> */
	if (info.search_file != NULL) {
		struct stat sb;

		if (stat(info.search_file, &sb) != 0) {
			/* XXX */
			return 1;
		}
		pkgs = pkg_db_get_installed_match_count(info.db,
		    pkg_match_by_file, 1, (const void *)info.search_file);
		if (info.quiet == 0)
			printf("The following installed package(s) has %s "
			    "origin:\n", info.origin);
		printf("%s\n", pkg_get_name(pkgs[0]));
		return 0;
	}

	/* -O <origin> */
	if (info.origin != NULL) {
		unsigned int pos;
		pkgs = pkg_db_get_installed_match(info.db, pkg_match_by_origin,
		    (const void *)info.origin);
		if (info.quiet == 0)
			printf("The following installed package(s) has %s "
			    "origin:\n", info.origin);
		for (pos = 0; pkgs[pos] != NULL; pos++) {
			printf("%s\n", pkg_get_name(pkgs[pos]));
		}
		return 0;
	}
	
	switch(info.match_type) {
	case MATCH_ALL:
	case MATCH_GLOB:
	case MATCH_NGLOB:
	case MATCH_REGEX:
	case MATCH_EREGEX:
		/* Display all packages installed */
		if (info.match_type == MATCH_ALL)
			pkgs = pkg_db_get_installed(info.db);
		else if (info.match_type == MATCH_REGEX ||
		         info.match_type == MATCH_EREGEX)
			pkgs = match_regex(info.db, (const char**)info.pkgs,
			    (info.match_type == MATCH_EREGEX));
		else if (info.match_type == MATCH_GLOB ||
		         info.match_type == MATCH_NGLOB)
			pkgs = match_glob(info.db, (const char**)info.pkgs,
			    (info.match_type == MATCH_GLOB));
		else
			errx(1, "ERROR: Inconsistancy in pkg_info");

		/* Sort the packages and display them */
		if (pkgs == NULL) {
			/* XXX Error message */
			return 1;
		}
		for (cur = 0; pkgs[cur] != NULL; cur++)
			continue;
		qsort(pkgs, cur, sizeof(struct pkg *), pkg_compare);
		for (cur = 0; pkgs[cur] != NULL; cur++) {
			show(info.db, pkgs[cur], info.flags, info.quiet,
			    info.seperator, info.use_blocksize);
		}
		retval = 0;
		break;
	case MATCH_EXACT:
		/* Only match the exact names given */
		retval = 0;
		
		for (cur = 0; info.pkgs[cur] != NULL; cur++) {
			struct pkg *pkg;

			pkg = pkg_db_get_package(info.db, info.pkgs[cur]);
			if (pkg != NULL)
				show(info.db, pkg, info.flags, info.quiet,
				    info.seperator, info.use_blocksize);
			else {
				warnx("pkg_info: can't find package '%s' "
				    "installed or in a file!", info.pkgs[cur]);
				retval = 1;
			}
		}
		break;
	}
	if (pkgs != NULL)
		pkg_list_free(pkgs);
	return retval;
}
Example #10
0
int
pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
{
	struct pkg_file *file = NULL;
	char *sh;
	khint_t k;
	int ret = EPKG_OK;
	char fpath[MAXPATHLEN];
	const char *lib;
	bool failures = false;

	if (kh_count(pkg->shlibs_required) != 0)
		pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);
	if (kh_count(pkg->shlibs_provided) != 0)
		pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);

	if (elf_version(EV_CURRENT) == EV_NONE)
		return (EPKG_FATAL);

	shlib_list_init();

	if (stage != NULL && pkg_object_bool(pkg_config_get("ALLOW_BASE_SHLIBS"))) {
		/* Do not check the return */
		shlib_list_from_stage(stage);
	}

	ret = shlib_list_from_elf_hints(_PATH_ELF_HINTS);
	if (ret != EPKG_OK)
		goto cleanup;

	/* Assume no architecture dependence, for contradiction */
	if (developer_mode)
		pkg->flags &= ~(PKG_CONTAINS_ELF_OBJECTS |
				PKG_CONTAINS_STATIC_LIBS |
				PKG_CONTAINS_H_OR_LA);

	while (pkg_files(pkg, &file) == EPKG_OK) {
		if (stage != NULL)
			snprintf(fpath, sizeof(fpath), "%s/%s", stage, file->path);
		else
			strlcpy(fpath, file->path, sizeof(fpath));

		ret = analyse_elf(pkg, fpath);
		if (developer_mode) {
			if (ret != EPKG_OK && ret != EPKG_END) {
				failures = true;
				continue;
			}
			analyse_fpath(pkg, fpath);
		}
	}

	/*
	 * Do not depend on libraries that a package provides itself
	 */
	kh_each_value(pkg->shlibs_required, sh, {
		if (kh_contains(strings, pkg->shlibs_provided, sh)) {
			pkg_debug(2, "remove %s from required shlibs as the "
			    "package %s provides this library itself",
			    sh, pkg->name);
			k = kh_get_strings(pkg->shlibs_required, sh);
			kh_del_strings(pkg->shlibs_required, k);
			continue;
		}
		file = NULL;
		while (pkg_files(pkg, &file) == EPKG_OK) {
			if ((lib = strstr(file->path, sh)) != NULL &&
			    strlen(lib) == strlen(sh) && lib[-1] == '/') {
				pkg_debug(2, "remove %s from required shlibs as "
				    "the package %s provides this library itself",
				    sh, pkg->name);
				k = kh_get_strings(pkg->shlibs_required, sh);
				kh_del_strings(pkg->shlibs_required, k);
				break;
			}
		}
	});
Example #11
0
void
trig_awaited_pend_free(void)
{
	pkg_list_free(trig_awaited_pend_head);
	trig_awaited_pend_head = NULL;
}
Example #12
0
void
pkg_free(struct pkg *pkg)
{
	if (pkg == NULL)
		return;

	free(pkg->name);
	free(pkg->origin);
	free(pkg->old_version);
	free(pkg->maintainer);
	free(pkg->www);
	free(pkg->arch);
	free(pkg->abi);
	free(pkg->uid);
	free(pkg->digest);
	free(pkg->old_digest);
	free(pkg->prefix);
	free(pkg->comment);
	free(pkg->desc);
	free(pkg->sum);
	free(pkg->repopath);
	free(pkg->repourl);
	free(pkg->reason);
	free(pkg->dep_formula);

	for (int i = 0; i < PKG_NUM_SCRIPTS; i++)
		if (pkg->scripts[i])
			utstring_free(pkg->scripts[i]);

	pkg_list_free(pkg, PKG_DEPS);
	pkg_list_free(pkg, PKG_RDEPS);
	pkg_list_free(pkg, PKG_FILES);
	pkg_list_free(pkg, PKG_DIRS);
	pkg_list_free(pkg, PKG_OPTIONS);
	pkg_list_free(pkg, PKG_USERS);
	pkg_list_free(pkg, PKG_GROUPS);
	pkg_list_free(pkg, PKG_SHLIBS_REQUIRED);
	pkg_list_free(pkg, PKG_SHLIBS_PROVIDED);
	pkg_list_free(pkg, PKG_PROVIDES);
	pkg_list_free(pkg, PKG_REQUIRES);
	pkg_list_free(pkg, PKG_CATEGORIES);
	pkg_list_free(pkg, PKG_LICENSES);

	LL_FREE(pkg->message, pkg_message_free);
	LL_FREE(pkg->annotations, pkg_kv_free);

	if (pkg->rootfd != -1)
		close(pkg->rootfd);

	free(pkg);
}
Example #13
0
int
pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
{
	struct pkg_dep	*rdep = NULL;
	int		 ret;
	bool		 handle_rc = false;
	const char	*origin;

	assert(pkg != NULL);
	assert(db != NULL);
	/*
	 * Do not trust the existing entries as it may have changed if we
	 * delete packages in batch.
	 */
	pkg_list_free(pkg, PKG_RDEPS);
	/*
	 * Ensure that we have all the informations we need
	 */
	if ((ret = pkgdb_load_rdeps(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_load_files(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_load_dirs(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_load_scripts(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_load_mtree(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_load_annotations(db, pkg)) != EPKG_OK)
		return (ret);

	if ((flags & PKG_DELETE_UPGRADE) == 0)
		pkg_emit_deinstall_begin(pkg);

	/* If the package is locked */
	if (pkg_is_locked(pkg)) {
		pkg_emit_locked(pkg);
		return (EPKG_LOCKED);
	}

	/* If there are dependencies */
	if ((flags & (PKG_DELETE_UPGRADE|PKG_DELETE_CONFLICT)) == 0) {
		if (pkg_rdeps(pkg, &rdep) == EPKG_OK) {
			pkg_emit_required(pkg, flags & PKG_DELETE_FORCE);
			if ((flags & PKG_DELETE_FORCE) == 0)
				return (EPKG_REQUIRED);
		}
	}

	/*
	 * stop the different related services if the users do want that
	 * and that the service is running
	 */
	handle_rc = pkg_object_bool(pkg_config_get("HANDLE_RC_SCRIPTS"));
	if (handle_rc)
		pkg_start_stop_rc_scripts(pkg, PKG_RC_STOP);

	if ((flags & PKG_DELETE_NOSCRIPT) == 0) {
		if (flags & PKG_DELETE_UPGRADE) {
			ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_UPGRADE);
			if (ret != EPKG_OK)
				return (ret);
		} else {
			ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_DEINSTALL);
			if (ret != EPKG_OK)
				return (ret);
		}
	}

	if ((ret = pkg_delete_files(pkg, flags & PKG_DELETE_FORCE ? 1 : 0))
            != EPKG_OK)
		return (ret);

	if ((flags & (PKG_DELETE_NOSCRIPT | PKG_DELETE_UPGRADE)) == 0) {
		ret = pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL);
		if (ret != EPKG_OK)
			return (ret);
	}

	ret = pkg_delete_dirs(db, pkg, flags & PKG_DELETE_FORCE);
	if (ret != EPKG_OK)
		return (ret);

	if ((flags & PKG_DELETE_UPGRADE) == 0)
		pkg_emit_deinstall_finished(pkg);

	pkg_get(pkg, PKG_ORIGIN, &origin);

	return (pkgdb_unregister_pkg(db, origin));
}
Example #14
0
int
pkg_delete2(struct pkg *pkg, struct pkgdb *db, int force, int upgrade)
{
	struct pkg_dep *rdep = NULL;
	int ret;

	assert(pkg != NULL);
	assert(db != NULL);

	/*
	 * Do not trust the existing entries as it may have changed if we
	 * delete packages in batch.
	 */
	pkg_list_free(pkg, PKG_RDEPS);

	/*
	 * Ensure that we have all the informations we need
	 */
	if ((ret = pkgdb_loadrdeps(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loadfiles(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loaddirs(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loadscripts(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loadmtree(db, pkg)) != EPKG_OK)
		return (ret);

	if (!upgrade)
		pkg_emit_deinstall_begin(pkg);
	else
		pkg_emit_upgrade_begin(pkg);

	/* If there are dependencies */
	if (pkg_rdeps(pkg, &rdep) == EPKG_OK) {
		pkg_emit_required(pkg, force);
		if (!force)
			return (EPKG_REQUIRED);
	}

	if (upgrade) {
		if (( ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_UPGRADE)) != EPKG_OK )
			return (ret);
	} else {
		if ((ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_DEINSTALL)) != EPKG_OK)
			return (ret);
	}

	if ((ret = pkg_delete_files(pkg, force)) != EPKG_OK)
		return (ret);

	if (!upgrade)
		if ((ret = pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL)) != EPKG_OK)
			return (ret);

	if ((ret = pkg_delete_dirs(db, pkg, force)) != EPKG_OK)
		return (ret);

	if (!upgrade)
		pkg_emit_deinstall_finished(pkg);

	return (pkgdb_unregister_pkg(db, pkg_get(pkg, PKG_ORIGIN)));
}
Example #15
0
int
pkg_info(struct pkg_info info)
{
	unsigned int cur;
	int retval;
	struct pkg **pkgs;

	retval = 1;
	pkgs = NULL;

	switch(info.match_type) {
	case MATCH_ALL:
		/* Display all packages installed */
		pkgs = pkg_db_get_installed(info.db);
		if (pkgs == NULL) {
			/* XXX Error message */
			return 1;
		}
		for (cur = 0; pkgs[cur] != NULL; cur++)
			continue;
		qsort(pkgs, cur, sizeof(struct pkg *), pkg_compare);
		for (cur = 0; pkgs[cur] != NULL; cur++) {
			show(info.db, pkgs[cur], info.flags, info.quiet);
		}
		retval = 0;
		break;
	case MATCH_REGEX:
	case MATCH_EREGEX:
		/* Match all packages that match one of the [e]regex given */
		{
		char *prev;

		//pkgs = pkg_db_get_installed(info.db);
		//if (pkgs == NULL) {
			/* XXX Error message */
		//	break;
		//}

		pkgs = match_regex(info.db, info.pkgs,
		    (info.match_type == MATCH_EREGEX));

		/* Display all packages that matches atleast one regex */
		prev = NULL;
		for (cur = 0; pkgs[cur] != NULL; cur++) {
			/* Only show one instance of each package */
			if (prev == NULL ||
			    strcmp(prev, pkg_get_name(pkgs[cur])) != 0) {
				show(info.db, pkgs[cur], info.flags, info.quiet);
			}
			prev = pkg_get_name(pkgs[cur]);
		}
		}
		retval = 0;
		break;
	case MATCH_GLOB:
	case MATCH_NGLOB:
		errx(1, "Unsupported match type (use -x or -X)");
		break;
	case MATCH_EXACT:
		/* Only match the exact names given */
		retval = 0;
		
		for (cur = 0; info.pkgs[cur] != NULL; cur++) {
			struct pkg *pkg;

			pkg = pkg_db_get_package(info.db, info.pkgs[cur]);
			if (pkg != NULL)
				show(info.db, pkg, info.flags, info.quiet);
			else
				retval = 1;
		}
		break;
	}
	if (pkgs != NULL)
		pkg_list_free(pkgs);
	return retval;
}
Example #16
0
void
pkg_queue_destroy(struct pkg_queue *queue)
{
	pkg_list_free(queue->head);
	pkg_queue_init(queue);
}