diff --git a/lib/initend.c b/lib/initend.c index 31f5e81e..2e72314f 100644 --- a/lib/initend.c +++ b/lib/initend.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "xbps_api_impl.h" @@ -164,7 +165,7 @@ parse_files_glob(struct xbps_handle *xhp, const char *path, bool nested, bool vp glob(path, 0, NULL, &globbuf); for(i = 0; globbuf.gl_pathv[i]; i++) { - if((rv = parse_file(xhp, globbuf.gl_pathv[i], nested, vpkgconf)) != 0) + if ((rv = parse_file(xhp, globbuf.gl_pathv[i], nested, vpkgconf)) != 0) break; } globfree(&globbuf); @@ -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..d6d65250 --- /dev/null +++ b/tests/xbps/libxbps/config/Kyuafile @@ -0,0 +1,5 @@ +syntax("kyuafile", 1) + +test_suite("libxbps") + +atf_test_program{name="config_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..7f779f39 --- /dev/null +++ b/tests/xbps/libxbps/config/main.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2014 Enno Boland. + * 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(config_include_test); +ATF_TC_HEAD(config_include_test, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test including files by file globbing"); +} + +ATF_TC_BODY(config_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, config_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