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
This commit is contained in:
Thomas Morin 2015-09-01 10:33:10 +02:00
parent f86d2e1a05
commit 85f42f698c
2 changed files with 59 additions and 3 deletions

View File

@ -89,9 +89,10 @@ function merge_config_file {
# note, configfile might be a variable (note the iniset, etc
# created in the mega-awk below is "eval"ed too, so we just leave
# it alone.
local real_configfile=$(eval echo $configfile)
local real_configfile
real_configfile=$(eval echo $configfile)
if [ ! -f $real_configfile ]; then
touch $real_configfile
touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
fi
get_meta_section $file $matchgroup $configfile | \
@ -177,8 +178,18 @@ function merge_config_group {
local configfile group
for group in $matchgroups; do
for configfile in $(get_meta_section_files $localfile $group); do
if [[ -d $(dirname $(eval "echo $configfile")) ]]; then
local realconfigfile
local dir
realconfigfile=$(eval "echo $configfile")
if [[ -z $realconfigfile ]]; then
die $LINENO "bogus config file specification: $configfile is undefined"
fi
dir=$(dirname $realconfigfile)
if [[ -d $dir ]]; then
merge_config_file $localfile $group $configfile
else
die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)"
fi
done
done

View File

@ -23,6 +23,12 @@ function check_result {
fi
}
# mock function-common:die so that it does not
# interupt our test script
function die {
exit -1
}
TEST_1C_ADD="[eee]
type=new
multi = foo2"
@ -110,6 +116,15 @@ attr = strip_trailing_space
[DEFAULT]
servers=10.11.12.13:80
[[test8|/permission-denied.conf]]
foo=bar
[[test9|\$UNDEF]]
foo=bar
[[test10|does-not-exist-dir/test.conf]]
foo=bar
[[test-multi-sections|test-multi-sections.conf]]
[sec-1]
cfg_item1 = abcd
@ -340,6 +355,36 @@ EXPECT_VAL="
servers = 10.11.12.13:80"
check_result "$VAL" "$EXPECT_VAL"
echo "merge_config_file test8 non-touchable conf file: "
set +e
# function is expected to fail and exit, running it
# in a subprocess to let this script proceed
(merge_config_file test.conf test8 /permission-denied.conf)
VAL=$?
EXPECT_VAL=255
check_result "$VAL" "$EXPECT_VAL"
set -e
echo -n "merge_config_group test9 undefined conf file: "
set +e
# function is expected to fail and exit, running it
# in a subprocess to let this script proceed
(merge_config_group test.conf test9)
VAL=$?
EXPECT_VAL=255
check_result "$VAL" "$EXPECT_VAL"
set -e
echo -n "merge_config_group test10 not directory: "
set +e
# function is expected to fail and exit, running it
# in a subprocess to let this script proceed
(merge_config_group test.conf test10)
VAL=$?
EXPECT_VAL=255
check_result "$VAL" "$EXPECT_VAL"
set -e
rm -f test.conf test1c.conf test2a.conf \
test-space.conf test-equals.conf test-strip.conf \
test-colon.conf test-env.conf test-multiline.conf \