<commit_before>/*-
 * Copyright (c) 2009-2012 <NAME>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "xbps_api_impl.h"

/*
 * Sorting algorithm for packages in the transaction dictionary.
 * The transaction dictionary contains all package dictionaries found from
 * the repository plist index file in the "unsorted_deps" array.
 *
 * Any package in the unsorted_deps array is added into the tail and
 * later if a package dependency has an index greater than current
 * package, the package dependency is moved just before it.
 *
 * Once that all package dependencies for a package are in the correct
 * index, the counter is increased. When all packages in the "unsorted_deps"
 * array are processed the loop is stopped and the counter should have
 * the same number than the "unsorted_deps" array... otherwise something
 * went wrong.
 */

struct pkgdep {
	TAILQ_ENTRY(pkgdep) pkgdep_entries;
	xbps_dictionary_t d;
	char *name;
};

static TAILQ_HEAD(pkgdep_head, pkgdep) pkgdep_list =
    TAILQ_HEAD_INITIALIZER(pkgdep_list);

static struct pkgdep *
pkgdep_find(const char *pkg)
{
	struct pkgdep *pd = NULL, *pd_new = NULL;
	const char *pkgver, *tract;

	TAILQ_FOREACH_SAFE(pd, &pkgdep_list, pkgdep_entries, pd_new) {
		if (pd->d == NULL) {
			/* ignore entries without dictionary */
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(pd->d,
		    "transaction", &tract);
		/* ignore pkgs to be removed */
		if (strcmp(tract, "remove") == 0)
			continue;
		/* simple match */
		xbps_dictionary_get_cstring_nocopy(pd->d, "pkgver", &pkgver);
		if (strcmp(pkgver, pkg) == 0)
			return pd;
		/* pkg expression match */
		if (xbps_pkgpattern_match(pkgver, pkg))
			return pd;
		/* virtualpkg expression match */
		if (xbps_match_virtual_pkg_in_dict(pd->d, pkg, true))
			return pd;
	}

	/* not found */
	return NULL;
}

static int32_t
pkgdep_find_idx(const char *pkg)
{
	struct pkgdep *pd, *pd_new;
	int32_t idx = 0;
	const char *pkgver, *tract;

	TAILQ_FOREACH_SAFE(pd, &pkgdep_list, pkgdep_entries, pd_new) {
		if (pd->d == NULL) {
			/* ignore entries without dictionary */
			idx++;
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(pd->d,
		    "transaction", &tract);
		/* ignore pkgs to be removed */
		if (strcmp(tract, "remove") == 0) {
			idx++;
			continue;
		}
		/* simple match */
		xbps_dictionary_get_cstring_nocopy(pd->d, "pkgver", &pkgver);
		if (strcmp(pkgver, pkg) == 0)
			return idx;
		/* pkg expression match */
		if (xbps_pkgpattern_match(pkgver, pkg))
			return idx;
		/* virtualpkg expression match */
		if (xbps_match_virtual_pkg_in_dict(pd->d, pkg, true))
			return idx;

		idx++;
	}
	/* not found */
	return -1;
}

static void
pkgdep_release(struct pkgdep *pd)
{
	free(pd->name);
	free(pd);
}

static struct pkgdep *
pkgdep_alloc(xbps_dictionary_t d, const char *pkg)
{
	struct pkgdep *pd;

	pd = malloc(sizeof(*pd));
	assert(pd);
	pd->d = d;
	pd->name = strdup(pkg);

	return pd;
}

static void
pkgdep_end(xbps_array_t sorted)
{
	struct pkgdep *pd;

	while ((pd = TAILQ_FIRST(&pkgdep_list)) != NULL) {
		TAILQ_REMOVE(&pkgdep_list, pd, pkgdep_entries);
		if (sorted != NULL && pd->d != NULL)
			xbps_array_add(sorted, pd->d);

		pkgdep_release(pd);
	}
}

static int
sort_pkg_rundeps(struct xbps_handle *xhp,
		 struct pkgdep *pd,
		 xbps_array_t pkg_rundeps,
		 xbps_array_t unsorted)
{
	xbps_dictionary_t curpkgd;
	struct pkgdep *lpd, *pdn;
	const char *str, *tract;
	int32_t pkgdepidx, curpkgidx;
	uint32_t i, idx = 0;
	int rv = 0;

	xbps_dbg_printf_append(xhp, "\n");
	curpkgidx = pkgdep_find_idx(pd->name);

again:
	for (i = idx; i < xbps_array_count(pkg_rundeps); i++) {
		xbps_array_get_cstring_nocopy(pkg_rundeps, i, &str);
		xbps_dbg_printf(xhp, "  Required dependency '%s': ", str);

		pdn = pkgdep_find(str);
		if ((pdn == NULL) && xbps_pkg_is_installed(xhp, str)) {
			/*
			 * Package dependency is installed, just add to
			 * the list but just mark it as "installed", to avoid
			 * calling xbps_check_is_installed_pkg_by_name(),
			 * which is expensive.
			 */
			xbps_dbg_printf_append(xhp, "installed.\n");
			lpd = pkgdep_alloc(NULL, str);
			TAILQ_INSERT_TAIL(&pkgdep_list, lpd, pkgdep_entries);
			continue;
		} else if (pdn != NULL && pdn->d == NULL) {
			/*
			 * Package was added previously into the list
			 * and is installed, skip.
			 */
			xbps_dbg_printf_append(xhp, "installed.\n");
			continue;
		}
		if (((curpkgd = xbps_find_pkg_in_array(unsorted, str)) == NULL) &&
		    ((curpkgd = xbps_find_virtualpkg_in_array(xhp, unsorted, str)) == NULL)) {
			rv = EINVAL;
			break;
		}
		if ((xbps_match_virtual_pkg_in_dict(curpkgd, str, true)) ||
		    (xbps_match_virtual_pkg_in_dict(curpkgd, str, false))) {
			xbps_dbg_printf_append(xhp, "ignore wrong "
			    "dependency %s (depends on itself)\n", str);
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(curpkgd,
		    "transaction", &tract);
		lpd = pkgdep_alloc(curpkgd, str);

		if (pdn == NULL) {<commit_msg>lib/transaction_sortdeps.c: fix a comment.<commit_after>/*-
 * Copyright (c) 2009-2012 <NAME>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "xbps_api_impl.h"

/*
 * Sorting algorithm for packages in the transaction dictionary.
 * The transaction dictionary contains all package dictionaries found from
 * the repository plist index file in the "unsorted_deps" array.
 *
 * Any package in the unsorted_deps array is added into the tail and
 * later if a package dependency has an index greater than current
 * package, the package dependency is moved just before it.
 *
 * Once that all package dependencies for a package are in the correct
 * index, the counter is increased. When all packages in the "unsorted_deps"
 * array are processed the loop is stopped and the counter should have
 * the same number than the "unsorted_deps" array... otherwise something
 * went wrong.
 */

struct pkgdep {
	TAILQ_ENTRY(pkgdep) pkgdep_entries;
	xbps_dictionary_t d;
	char *name;
};

static TAILQ_HEAD(pkgdep_head, pkgdep) pkgdep_list =
    TAILQ_HEAD_INITIALIZER(pkgdep_list);

static struct pkgdep *
pkgdep_find(const char *pkg)
{
	struct pkgdep *pd = NULL, *pd_new = NULL;
	const char *pkgver, *tract;

	TAILQ_FOREACH_SAFE(pd, &pkgdep_list, pkgdep_entries, pd_new) {
		if (pd->d == NULL) {
			/* ignore entries without dictionary */
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(pd->d,
		    "transaction", &tract);
		/* ignore pkgs to be removed */
		if (strcmp(tract, "remove") == 0)
			continue;
		/* simple match */
		xbps_dictionary_get_cstring_nocopy(pd->d, "pkgver", &pkgver);
		if (strcmp(pkgver, pkg) == 0)
			return pd;
		/* pkg expression match */
		if (xbps_pkgpattern_match(pkgver, pkg))
			return pd;
		/* virtualpkg expression match */
		if (xbps_match_virtual_pkg_in_dict(pd->d, pkg, true))
			return pd;
	}

	/* not found */
	return NULL;
}

static int32_t
pkgdep_find_idx(const char *pkg)
{
	struct pkgdep *pd, *pd_new;
	int32_t idx = 0;
	const char *pkgver, *tract;

	TAILQ_FOREACH_SAFE(pd, &pkgdep_list, pkgdep_entries, pd_new) {
		if (pd->d == NULL) {
			/* ignore entries without dictionary */
			idx++;
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(pd->d,
		    "transaction", &tract);
		/* ignore pkgs to be removed */
		if (strcmp(tract, "remove") == 0) {
			idx++;
			continue;
		}
		/* simple match */
		xbps_dictionary_get_cstring_nocopy(pd->d, "pkgver", &pkgver);
		if (strcmp(pkgver, pkg) == 0)
			return idx;
		/* pkg expression match */
		if (xbps_pkgpattern_match(pkgver, pkg))
			return idx;
		/* virtualpkg expression match */
		if (xbps_match_virtual_pkg_in_dict(pd->d, pkg, true))
			return idx;

		idx++;
	}
	/* not found */
	return -1;
}

static void
pkgdep_release(struct pkgdep *pd)
{
	free(pd->name);
	free(pd);
}

static struct pkgdep *
pkgdep_alloc(xbps_dictionary_t d, const char *pkg)
{
	struct pkgdep *pd;

	pd = malloc(sizeof(*pd));
	assert(pd);
	pd->d = d;
	pd->name = strdup(pkg);

	return pd;
}

static void
pkgdep_end(xbps_array_t sorted)
{
	struct pkgdep *pd;

	while ((pd = TAILQ_FIRST(&pkgdep_list)) != NULL) {
		TAILQ_REMOVE(&pkgdep_list, pd, pkgdep_entries);
		if (sorted != NULL && pd->d != NULL)
			xbps_array_add(sorted, pd->d);

		pkgdep_release(pd);
	}
}

static int
sort_pkg_rundeps(struct xbps_handle *xhp,
		 struct pkgdep *pd,
		 xbps_array_t pkg_rundeps,
		 xbps_array_t unsorted)
{
	xbps_dictionary_t curpkgd;
	struct pkgdep *lpd, *pdn;
	const char *str, *tract;
	int32_t pkgdepidx, curpkgidx;
	uint32_t i, idx = 0;
	int rv = 0;

	xbps_dbg_printf_append(xhp, "\n");
	curpkgidx = pkgdep_find_idx(pd->name);

again:
	for (i = idx; i < xbps_array_count(pkg_rundeps); i++) {
		xbps_array_get_cstring_nocopy(pkg_rundeps, i, &str);
		xbps_dbg_printf(xhp, "  Required dependency '%s': ", str);

		pdn = pkgdep_find(str);
		if ((pdn == NULL) && xbps_pkg_is_installed(xhp, str)) {
			/*
			 * Package dependency is installed, just add to
			 * the list but just mark it as "installed", to avoid
			 * calling xbps_pkg_is_installed(),
			 * which is expensive.
			 */
			xbps_dbg_printf_append(xhp, "installed.\n");
			lpd = pkgdep_alloc(NULL, str);
			TAILQ_INSERT_TAIL(&pkgdep_list, lpd, pkgdep_entries);
			continue;
		} else if (pdn != NULL && pdn->d == NULL) {
			/*
			 * Package was added previously into the list
			 * and is installed, skip.
			 */
			xbps_dbg_printf_append(xhp, "installed.\n");
			continue;
		}
		if (((curpkgd = xbps_find_pkg_in_array(unsorted, str)) == NULL) &&
		    ((curpkgd = xbps_find_virtualpkg_in_array(xhp, unsorted, str)) == NULL)) {
			rv = EINVAL;
			break;
		}
		if ((xbps_match_virtual_pkg_in_dict(curpkgd, str, true)) ||
		    (xbps_match_virtual_pkg_in_dict(curpkgd, str, false))) {
			xbps_dbg_printf_append(xhp, "ignore wrong "
			    "dependency %s (depends on itself)\n", str);
			continue;
		}
		xbps_dictionary_get_cstring_nocopy(curpkgd,
		    "transaction", &tract);
		lpd = pkgdep_alloc(curpkgd, str);

		if (pdn == NULL) {
{'fraction_non_alphanumeric': 0.110747103475829, 'fraction_numerical': 0.0041550139832201355, 'mean_word_length': 4.020457280385078, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3919704', 'n_tokens_mistral': 4775, 'n_tokens_neox': 4240, 'n_words': 1323}