Browse code

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

Thomas Morin authored on 2015/09/01 17:33:10
Showing 2 changed files
... ...
@@ -89,9 +89,10 @@ function merge_config_file {
89 89
     # note, configfile might be a variable (note the iniset, etc
90 90
     # created in the mega-awk below is "eval"ed too, so we just leave
91 91
     # it alone.
92
-    local real_configfile=$(eval echo $configfile)
92
+    local real_configfile
93
+    real_configfile=$(eval echo $configfile)
93 94
     if [ ! -f $real_configfile ]; then
94
-        touch $real_configfile
95
+        touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
95 96
     fi
96 97
 
97 98
     get_meta_section $file $matchgroup $configfile | \
... ...
@@ -177,8 +178,18 @@ function merge_config_group {
177 177
     local configfile group
178 178
     for group in $matchgroups; do
179 179
         for configfile in $(get_meta_section_files $localfile $group); do
180
-            if [[ -d $(dirname $(eval "echo $configfile")) ]]; then
180
+            local realconfigfile
181
+            local dir
182
+
183
+            realconfigfile=$(eval "echo $configfile")
184
+            if [[ -z $realconfigfile ]]; then
185
+                die $LINENO "bogus config file specification: $configfile is undefined"
186
+            fi
187
+            dir=$(dirname $realconfigfile)
188
+            if [[ -d $dir ]]; then
181 189
                 merge_config_file $localfile $group $configfile
190
+            else
191
+                die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)"
182 192
             fi
183 193
         done
184 194
     done
... ...
@@ -23,6 +23,12 @@ function check_result {
23 23
     fi
24 24
 }
25 25
 
26
+# mock function-common:die so that it does not
27
+# interupt our test script
28
+function die {
29
+    exit -1
30
+}
31
+
26 32
 TEST_1C_ADD="[eee]
27 33
 type=new
28 34
 multi = foo2"
... ...
@@ -110,6 +116,15 @@ attr = strip_trailing_space
110 110
 [DEFAULT]
111 111
 servers=10.11.12.13:80
112 112
 
113
+[[test8|/permission-denied.conf]]
114
+foo=bar
115
+
116
+[[test9|\$UNDEF]]
117
+foo=bar
118
+
119
+[[test10|does-not-exist-dir/test.conf]]
120
+foo=bar
121
+
113 122
 [[test-multi-sections|test-multi-sections.conf]]
114 123
 [sec-1]
115 124
 cfg_item1 = abcd
... ...
@@ -340,6 +355,36 @@ EXPECT_VAL="
340 340
 servers = 10.11.12.13:80"
341 341
 check_result "$VAL" "$EXPECT_VAL"
342 342
 
343
+echo "merge_config_file test8 non-touchable conf file: "
344
+set +e
345
+# function is expected to fail and exit, running it
346
+# in a subprocess to let this script proceed
347
+(merge_config_file test.conf test8 /permission-denied.conf)
348
+VAL=$?
349
+EXPECT_VAL=255
350
+check_result "$VAL" "$EXPECT_VAL"
351
+set -e
352
+
353
+echo -n "merge_config_group test9 undefined conf file: "
354
+set +e
355
+# function is expected to fail and exit, running it
356
+# in a subprocess to let this script proceed
357
+(merge_config_group test.conf test9)
358
+VAL=$?
359
+EXPECT_VAL=255
360
+check_result "$VAL" "$EXPECT_VAL"
361
+set -e
362
+
363
+echo -n "merge_config_group test10 not directory: "
364
+set +e
365
+# function is expected to fail and exit, running it
366
+# in a subprocess to let this script proceed
367
+(merge_config_group test.conf test10)
368
+VAL=$?
369
+EXPECT_VAL=255
370
+check_result "$VAL" "$EXPECT_VAL"
371
+set -e
372
+
343 373
 rm -f test.conf test1c.conf test2a.conf \
344 374
     test-space.conf test-equals.conf test-strip.conf \
345 375
     test-colon.conf test-env.conf test-multiline.conf \