Skip to content

Commit 85f42f6

Browse files
committed
Provide an error message on bogus config file spec
If local.conf specifies a config file addition in the following way... [[post-config|$MY_CONF_FILE]] [xyz] foo=bar ...and $MY_CONF_FILE points to a file whose directory is not writable by the user running the script, then stack.sh aborts with the following obscure message: 2015-09-01 08:20:08.113 | touch: setting times of '/': Permission denied This patch modifies inc/meta-config to provide a useful error message, such as: 2015-09-01 08:20:08.114 | could not create config file / ($MY_CONF_FILE) This patch also modifies inc/meta-config so that it provides an error message if $MY_CONF_FILE is empty (instead of silently ignoring this local.conf statement): 2015-09-01 09:38:53.406 | bogus config file specification: $MY_CONF_FILE is undefined Change-Id: I9b78407420318548561012a8672762bc7fdd6db6 Closes-Bug: 1490881
1 parent f86d2e1 commit 85f42f6

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

inc/meta-config

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ function merge_config_file {
8989
# note, configfile might be a variable (note the iniset, etc
9090
# created in the mega-awk below is "eval"ed too, so we just leave
9191
# it alone.
92-
local real_configfile=$(eval echo $configfile)
92+
local real_configfile
93+
real_configfile=$(eval echo $configfile)
9394
if [ ! -f $real_configfile ]; then
94-
touch $real_configfile
95+
touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
9596
fi
9697

9798
get_meta_section $file $matchgroup $configfile | \
@@ -177,8 +178,18 @@ function merge_config_group {
177178
local configfile group
178179
for group in $matchgroups; do
179180
for configfile in $(get_meta_section_files $localfile $group); do
180-
if [[ -d $(dirname $(eval "echo $configfile")) ]]; then
181+
local realconfigfile
182+
local dir
183+
184+
realconfigfile=$(eval "echo $configfile")
185+
if [[ -z $realconfigfile ]]; then
186+
die $LINENO "bogus config file specification: $configfile is undefined"
187+
fi
188+
dir=$(dirname $realconfigfile)
189+
if [[ -d $dir ]]; then
181190
merge_config_file $localfile $group $configfile
191+
else
192+
die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)"
182193
fi
183194
done
184195
done

tests/test_meta_config.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ function check_result {
2323
fi
2424
}
2525

26+
# mock function-common:die so that it does not
27+
# interupt our test script
28+
function die {
29+
exit -1
30+
}
31+
2632
TEST_1C_ADD="[eee]
2733
type=new
2834
multi = foo2"
@@ -110,6 +116,15 @@ attr = strip_trailing_space
110116
[DEFAULT]
111117
servers=10.11.12.13:80
112118
119+
[[test8|/permission-denied.conf]]
120+
foo=bar
121+
122+
[[test9|\$UNDEF]]
123+
foo=bar
124+
125+
[[test10|does-not-exist-dir/test.conf]]
126+
foo=bar
127+
113128
[[test-multi-sections|test-multi-sections.conf]]
114129
[sec-1]
115130
cfg_item1 = abcd
@@ -340,6 +355,36 @@ EXPECT_VAL="
340355
servers = 10.11.12.13:80"
341356
check_result "$VAL" "$EXPECT_VAL"
342357

358+
echo "merge_config_file test8 non-touchable conf file: "
359+
set +e
360+
# function is expected to fail and exit, running it
361+
# in a subprocess to let this script proceed
362+
(merge_config_file test.conf test8 /permission-denied.conf)
363+
VAL=$?
364+
EXPECT_VAL=255
365+
check_result "$VAL" "$EXPECT_VAL"
366+
set -e
367+
368+
echo -n "merge_config_group test9 undefined conf file: "
369+
set +e
370+
# function is expected to fail and exit, running it
371+
# in a subprocess to let this script proceed
372+
(merge_config_group test.conf test9)
373+
VAL=$?
374+
EXPECT_VAL=255
375+
check_result "$VAL" "$EXPECT_VAL"
376+
set -e
377+
378+
echo -n "merge_config_group test10 not directory: "
379+
set +e
380+
# function is expected to fail and exit, running it
381+
# in a subprocess to let this script proceed
382+
(merge_config_group test.conf test10)
383+
VAL=$?
384+
EXPECT_VAL=255
385+
check_result "$VAL" "$EXPECT_VAL"
386+
set -e
387+
343388
rm -f test.conf test1c.conf test2a.conf \
344389
test-space.conf test-equals.conf test-strip.conf \
345390
test-colon.conf test-env.conf test-multiline.conf \

0 commit comments

Comments
 (0)