From 152ec0354fda03cceae263e60896cb70048195c8 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Tue, 13 May 2014 11:37:09 +0200 Subject: [PATCH] libxbps: file including in xbps.conf supports relative paths. --- lib/initend.c | 24 ++++++++ tests/xbps/libxbps/Makefile | 1 + tests/xbps/libxbps/common/Kyuafile | 1 + tests/xbps/libxbps/config/1.include.conf | 1 + tests/xbps/libxbps/config/2.include.conf | 1 + tests/xbps/libxbps/config/Kyuafile | 5 ++ tests/xbps/libxbps/config/Makefile | 8 +++ tests/xbps/libxbps/config/main.c | 64 ++++++++++++++++++++++ tests/xbps/libxbps/config/xbps.conf | 2 + tests/xbps/libxbps/shell/xbps_conf_test.sh | 16 ++++++ 10 files changed, 123 insertions(+) create mode 100644 tests/xbps/libxbps/config/1.include.conf create mode 100644 tests/xbps/libxbps/config/2.include.conf create mode 100644 tests/xbps/libxbps/config/Kyuafile create mode 100644 tests/xbps/libxbps/config/Makefile create mode 100644 tests/xbps/libxbps/config/main.c create mode 100644 tests/xbps/libxbps/config/xbps.conf create mode 100644 tests/xbps/libxbps/shell/xbps_conf_test.sh diff --git a/lib/initend.c b/lib/initend.c index 31f5e81e..40ef8c10 100644 --- a/lib/initend.c +++ b/lib/initend.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "xbps_api_impl.h" @@ -179,6 +180,8 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested, bool vpkgconf size_t len, nlines = 0; ssize_t read; char *line = NULL; + char ocwd[XBPS_MAXPATH], tmppath[XBPS_MAXPATH]; + char *cwd; int rv = 0; if ((fp = fopen(path, "r")) == NULL) { @@ -191,6 +194,20 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested, bool vpkgconf xbps_dbg_printf(xhp, "Parsing configuration file: %s\n", path); } + /* cwd to the dir containing the config file */ + strncpy(tmppath, path, sizeof(tmppath)); + cwd = dirname(tmppath); + if(getcwd(ocwd, sizeof(ocwd)) == NULL) { + rv = errno; + xbps_dbg_printf(xhp, "cannot get cwd: %s\n", strerror(rv)); + return rv; + } + if(chdir(cwd)) { + rv = errno; + xbps_dbg_printf(xhp, "cannot chdir to %s: %s\n", cwd, strerror(rv)); + return rv; + } + while ((read = getline(&line, &len, fp)) != -1) { char *p, *k, *v; @@ -244,6 +261,13 @@ parse_file(struct xbps_handle *xhp, const char *path, bool nested, bool vpkgconf free(line); fclose(fp); + /* Going back to old working directory */ + if(chdir(ocwd)) { + rv = errno; + xbps_dbg_printf(xhp, "cannot chdir to %s: %s\n", ocwd, strerror(rv)); + return rv; + } + return rv; } diff --git a/tests/xbps/libxbps/Makefile b/tests/xbps/libxbps/Makefile index 6ec2673a..f8e6424f 100644 --- a/tests/xbps/libxbps/Makefile +++ b/tests/xbps/libxbps/Makefile @@ -10,6 +10,7 @@ SUBDIRS += util SUBDIRS += find_pkg_obsoletes SUBDIRS += find_pkg_orphans SUBDIRS += pkgdb +SUBDIRS += config SUBDIRS += shell include ../../../mk/subdir.mk diff --git a/tests/xbps/libxbps/common/Kyuafile b/tests/xbps/libxbps/common/Kyuafile index d2eaaa8e..d4754210 100644 --- a/tests/xbps/libxbps/common/Kyuafile +++ b/tests/xbps/libxbps/common/Kyuafile @@ -19,6 +19,7 @@ atf_test_program{name="installmode_test"} atf_test_program{name="obsoletefiles_test"} atf_test_program{name="scripts_test"} atf_test_program{name="incorrect_deps_test"} +atf_test_program{name="config_test"} include('find_pkg_orphans/Kyuafile') include('pkgdb/Kyuafile') diff --git a/tests/xbps/libxbps/config/1.include.conf b/tests/xbps/libxbps/config/1.include.conf new file mode 100644 index 00000000..2a3020f3 --- /dev/null +++ b/tests/xbps/libxbps/config/1.include.conf @@ -0,0 +1 @@ +repository=1 diff --git a/tests/xbps/libxbps/config/2.include.conf b/tests/xbps/libxbps/config/2.include.conf new file mode 100644 index 00000000..926aead5 --- /dev/null +++ b/tests/xbps/libxbps/config/2.include.conf @@ -0,0 +1 @@ +repository=2 diff --git a/tests/xbps/libxbps/config/Kyuafile b/tests/xbps/libxbps/config/Kyuafile new file mode 100644 index 00000000..0d015857 --- /dev/null +++ b/tests/xbps/libxbps/config/Kyuafile @@ -0,0 +1,5 @@ +syntax("kyuafile", 1) + +test_suite("libxbps") + +atf_test_program{name="xbps_conf_test"} diff --git a/tests/xbps/libxbps/config/Makefile b/tests/xbps/libxbps/config/Makefile new file mode 100644 index 00000000..bae1f42b --- /dev/null +++ b/tests/xbps/libxbps/config/Makefile @@ -0,0 +1,8 @@ +TOPDIR = ../../../.. +-include $(TOPDIR)/config.mk + +TESTSSUBDIR = xbps/libxbps/config +TEST = config_test +EXTRA_FILES = Kyuafile xbps.conf 1.include.conf 2.include.conf + +include $(TOPDIR)/mk/test.mk diff --git a/tests/xbps/libxbps/config/main.c b/tests/xbps/libxbps/config/main.c new file mode 100644 index 00000000..9a4f02de --- /dev/null +++ b/tests/xbps/libxbps/config/main.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2013 Juan Romero Pardines. + * 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 +#include + +ATF_TC(xbps_conf_include_test); +ATF_TC_HEAD(xbps_conf_include_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test including files by file globbing"); +} + +ATF_TC_BODY(xbps_conf_include_test, tc) +{ + struct xbps_handle xh; + const char *tcsdir; + char conffile[XBPS_MAXPATH-1]; + + /* get test source dir */ + tcsdir = atf_tc_get_config_var(tc, "srcdir"); + + /* change dir to make sure relative paths won't match */ + ATF_REQUIRE_EQ(chdir("/"), 0); + memset(&xh, 0, sizeof(xh)); + strncpy(xh.rootdir, tcsdir, sizeof(xh.rootdir)); + strncpy(xh.metadir, tcsdir, sizeof(xh.metadir)); + strncpy(conffile, tcsdir, sizeof(conffile)); + strncat(conffile, "/xbps.conf", sizeof(conffile)); + xh.conffile = conffile; + xh.flags = XBPS_FLAG_DEBUG; + ATF_REQUIRE_EQ(xbps_init(&xh), 0); + + /* should contain both repositories defined in [12].include.conf */ + ATF_REQUIRE_EQ(xbps_array_count(xh.repositories), 2); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, xbps_conf_include_test); + + return atf_no_error(); +} diff --git a/tests/xbps/libxbps/config/xbps.conf b/tests/xbps/libxbps/config/xbps.conf new file mode 100644 index 00000000..41d51c5d --- /dev/null +++ b/tests/xbps/libxbps/config/xbps.conf @@ -0,0 +1,2 @@ +# relative path +include=*.include.conf diff --git a/tests/xbps/libxbps/shell/xbps_conf_test.sh b/tests/xbps/libxbps/shell/xbps_conf_test.sh new file mode 100644 index 00000000..9caff268 --- /dev/null +++ b/tests/xbps/libxbps/shell/xbps_conf_test.sh @@ -0,0 +1,16 @@ +#! /usr/bin/env atf-sh + +# Tests to verify that xbps.conf features work + +atf_test_case tc1 + +tc1_head() { + atf_set "descr" "Tests for xbps.conf: include " +} + +tc1_body() { +} + +atf_init_test_cases() { + atf_add_test_case tc1 +}