<commit_before>/*-
 * Copyright (c) 2009 <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.h>

/*
 * Configure a package that is currently unpacked. This
 * runs the post INSTALL action if required and updates the
 * package state to installed.
 */
int
xbps_configure_pkg(const char *pkgname)
{
	prop_dictionary_t pkgd;
	const char *rootdir, *version;
	char *buf;
	int rv = 0, flags = 0;
	pkg_state_t state = 0;
	bool reconfigure = false;

	assert(pkgname != NULL);

	rootdir = xbps_get_rootdir();
	flags = xbps_get_flags();

	if ((rv = xbps_get_pkg_state_installed(pkgname, &state)) != 0)
		return rv;

	if (state == XBPS_PKG_STATE_INSTALLED) {
		if ((flags & XBPS_FLAG_FORCE) == 0)
			return 0;

		reconfigure = true;
	} else if (state != XBPS_PKG_STATE_UNPACKED)
		return EINVAL;

	pkgd = xbps_find_pkg_installed_from_plist(pkgname);
	if (pkgd == NULL)
		return ENOENT;

	prop_dictionary_get_cstring_nocopy(pkgd, "version", &version);
	prop_object_release(pkgd);

	printf("%sonfiguring package %s-%s...\n",
	    reconfigure ? "Rec" : "C", pkgname, version);

	buf = xbps_xasprintf(".%s/metadata/%s/INSTALL",
	    XBPS_META_PATH, pkgname);
	if (buf == NULL)
		return errno;

	if (access(buf, R_OK) == 0) {
		if (strcmp(rootdir, "") == 0)
			rootdir = "/";

		if (chdir(rootdir) == -1)
			return errno;

		if ((rv = xbps_file_chdir_exec(rootdir, buf, "post",
		     pkgname, version, NULL)) != 0) {
			free(buf);
			printf("%s: post INSTALL action returned: %s\n",
			    pkgname, strerror(errno));
			return rv;
		}
	} else {
		if (errno != ENOENT) {
			free(buf);
			return errno;
		}
	}
	free(buf);

	return xbps_set_pkg_state_installed(pkgname, XBPS_PKG_STATE_INSTALLED);
}
<commit_msg>xbps_configure_pkg: chdir(rootdir) before checking for INSTALL script!<commit_after>/*-
 * Copyright (c) 2009 <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.h>

/*
 * Configure a package that is currently unpacked. This
 * runs the post INSTALL action if required and updates the
 * package state to installed.
 */
int
xbps_configure_pkg(const char *pkgname)
{
	prop_dictionary_t pkgd;
	const char *rootdir, *version;
	char *buf;
	int rv = 0, flags = 0;
	pkg_state_t state = 0;
	bool reconfigure = false;

	assert(pkgname != NULL);

	rootdir = xbps_get_rootdir();
	flags = xbps_get_flags();

	if ((rv = xbps_get_pkg_state_installed(pkgname, &state)) != 0)
		return rv;

	if (state == XBPS_PKG_STATE_INSTALLED) {
		if ((flags & XBPS_FLAG_FORCE) == 0)
			return 0;

		reconfigure = true;
	} else if (state != XBPS_PKG_STATE_UNPACKED)
		return EINVAL;

	pkgd = xbps_find_pkg_installed_from_plist(pkgname);
	if (pkgd == NULL)
		return ENOENT;

	prop_dictionary_get_cstring_nocopy(pkgd, "version", &version);
	prop_object_release(pkgd);

	printf("%sonfiguring package %s-%s...\n",
	    reconfigure ? "Rec" : "C", pkgname, version);

	buf = xbps_xasprintf(".%s/metadata/%s/INSTALL",
	    XBPS_META_PATH, pkgname);
	if (buf == NULL)
		return errno;

	if (strcmp(rootdir, "") == 0)
		rootdir = "/";

	if (chdir(rootdir) == -1)
		return errno;

	if (access(buf, R_OK) == 0) {
		if ((rv = xbps_file_chdir_exec(rootdir, buf, "post",
		     pkgname, version, NULL)) != 0) {
			free(buf);
			printf("%s: post INSTALL action returned: %s\n",
			    pkgname, strerror(errno));
			return rv;
		}
	} else {
		if (errno != ENOENT) {
			free(buf);
			return errno;
		}
	}
	free(buf);

	return xbps_set_pkg_state_installed(pkgname, XBPS_PKG_STATE_INSTALLED);
}

{'fraction_non_alphanumeric': 0.10417690417690417, 'fraction_numerical': 0.005241605241605242, 'mean_word_length': 4.126784214945424, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '58039', 'n_tokens_mistral': 2193, 'n_tokens_neox': 1932, 'n_words': 694}