<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 <sys/utsname.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>

#include "xbps_api_impl.h"

/**
 * @file lib/repository_pool.c
 * @brief Repository pool routines
 * @defgroup repopool Repository pool functions
 */

/*
 * Returns true if repository URI contains "noarch" or matching architecture
 * in last component, false otherwise.
 */
static bool
check_repo_arch(const char *uri)
{
	struct utsname un;
	char *p, *b;

	if ((p = strdup(uri)) == NULL)
		return false;

	uname(&un);
	b = basename(p);
	if ((strcmp(b, "noarch")) && (strcmp(b, un.machine))) {
		free(p);
		return false;
	}
	free(p);
	return true;
}

int HIDDEN
xbps_repository_pool_init(struct xbps_handle *xhp)
{
	prop_dictionary_t d = NULL;
	size_t i, ntotal = 0, nmissing = 0;
	const char *repouri;
	char *plist;
	int rv = 0;

	if (prop_object_type(xhp->repo_pool) == PROP_TYPE_ARRAY)
		return 0;
	else if (xhp->cfg == NULL)
		return ENOTSUP;

	xhp->repo_pool = prop_array_create();
	if (xhp->repo_pool == NULL)
		return ENOMEM;

	for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
		repouri = cfg_getnstr(xhp->cfg, "repositories", i);
		ntotal++;
		/*
		 * Check if repository doesn't match our architecture.
		 */
		if (!check_repo_arch(repouri)) {
			xbps_dbg_printf("[rpool] `%s' arch not matched, "
			    "ignoring.\n", repouri);
			nmissing++;
			continue;
		}
		/*
		 * If index file is not there, skip.
		 */
		plist = xbps_pkg_index_plist(repouri);
		if (plist == NULL) {
			rv = errno;
			goto out;
		}
		if (access(plist, R_OK) == -1) {
			xbps_dbg_printf("[rpool] `%s' missing index "
			    "file, ignoring.\n", repouri);
			free(plist);
			nmissing++;
			continue;
		}
		/*
		 * Register repository into the array.
		 */
		d = prop_dictionary_create();
		if (d == NULL) {
			rv = ENOMEM;
			free(plist);
			goto out;
		}
		if (!prop_dictionary_set_cstring_nocopy(d, "uri", repouri)) {
			rv = EINVAL;
			prop_object_release(d);
			free(plist);
			goto out;
		}
		if (!xbps_add_obj_to_dict(d,
		    prop_array_internalize_from_zfile(plist),
		    "index")) {
			rv = EINVAL;
			prop_object_release(d);
			free(plist);
			goto out;
		}
		free(plist);
		if (!prop_array_add(xhp->repo_pool, d)) {<commit_msg>xbps_repository_pool_init: make registered index arrays immutable.<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 <sys/utsname.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>

#include "xbps_api_impl.h"

/**
 * @file lib/repository_pool.c
 * @brief Repository pool routines
 * @defgroup repopool Repository pool functions
 */

/*
 * Returns true if repository URI contains "noarch" or matching architecture
 * in last component, false otherwise.
 */
static bool
check_repo_arch(const char *uri)
{
	struct utsname un;
	char *p, *b;

	if ((p = strdup(uri)) == NULL)
		return false;

	uname(&un);
	b = basename(p);
	if ((strcmp(b, "noarch")) && (strcmp(b, un.machine))) {
		free(p);
		return false;
	}
	free(p);
	return true;
}

int HIDDEN
xbps_repository_pool_init(struct xbps_handle *xhp)
{
	prop_dictionary_t d = NULL;
	prop_array_t array;
	size_t i, ntotal = 0, nmissing = 0;
	const char *repouri;
	char *plist;
	int rv = 0;

	if (prop_object_type(xhp->repo_pool) == PROP_TYPE_ARRAY)
		return 0;
	else if (xhp->cfg == NULL)
		return ENOTSUP;

	xhp->repo_pool = prop_array_create();
	if (xhp->repo_pool == NULL)
		return ENOMEM;

	for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
		repouri = cfg_getnstr(xhp->cfg, "repositories", i);
		ntotal++;
		/*
		 * Check if repository doesn't match our architecture.
		 */
		if (!check_repo_arch(repouri)) {
			xbps_dbg_printf("[rpool] `%s' arch not matched, "
			    "ignoring.\n", repouri);
			nmissing++;
			continue;
		}
		/*
		 * If index file is not there, skip.
		 */
		plist = xbps_pkg_index_plist(repouri);
		if (plist == NULL) {
			rv = errno;
			goto out;
		}
		if (access(plist, R_OK) == -1) {
			xbps_dbg_printf("[rpool] `%s' missing index "
			    "file, ignoring.\n", repouri);
			free(plist);
			nmissing++;
			continue;
		}
		/*
		 * Register repository into the array.
		 */
		d = prop_dictionary_create();
		if (d == NULL) {
			rv = ENOMEM;
			free(plist);
			goto out;
		}
		if (!prop_dictionary_set_cstring_nocopy(d, "uri", repouri)) {
			rv = EINVAL;
			prop_object_release(d);
			free(plist);
			goto out;
		}
		array = prop_array_internalize_from_zfile(plist);
		if (array == NULL) {
			rv = EINVAL;
			prop_object_release(d);
			free(plist);
			goto out;
		}
		free(plist);
		prop_array_make_immutable(array);
		if (!xbps_add_obj_to_dict(d, array, "index")) {
			rv = EINVAL;
			prop_object_release(d);
			goto out;
		}
		if (!prop_array_add(xhp->repo_pool, d)) {
{'fraction_non_alphanumeric': 0.11707580253574319, 'fraction_numerical': 0.004316158618829242, 'mean_word_length': 3.774629748873149, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, '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': '5485290', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2433, 'n_words': 816}