Follow up for creating parent dir for config files

The commit Ia5fcfe6c63f5cc40b11f7e1f3be244d7897f26f6 wanted to enable
config file creation even if its parent dir not exists. But missed that
the caller of merge_config_file, merge_config_group already checks for
hte missing directory. So creating the missing dir in merge_config_file
is too late.

This patch moves the dir creation to the caller.

Change-Id: Ied0f321f31bf8888dce71cc18749f35dde303390
Signed-off-by: Balazs Gibizer <gibi@redhat.com>
This commit is contained in:
Balazs Gibizer
2025-01-22 17:00:59 +01:00
parent 608ae718fc
commit 84652d3cb8
2 changed files with 27 additions and 10 deletions

View File

@@ -90,7 +90,6 @@ function merge_config_file {
local real_configfile local real_configfile
real_configfile=$(eval echo $configfile) real_configfile=$(eval echo $configfile)
if [ ! -f $real_configfile ]; then if [ ! -f $real_configfile ]; then
mkdir -p $(dirname $real_configfile) || die $LINENO "could not create the directory of $real_configfile ($configfile)"
touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)" touch $real_configfile || die $LINENO "could not create config file $real_configfile ($configfile)"
fi fi
@@ -186,11 +185,15 @@ function merge_config_group {
break break
fi fi
dir=$(dirname $realconfigfile) dir=$(dirname $realconfigfile)
if [[ -d $dir ]]; then
merge_config_file $localfile $group $configfile test -e $dir && ! test -d $dir && die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir exists but it is not a directory)"
else
die $LINENO "bogus config file specification $configfile ($configfile=$realconfigfile, $dir is not a directory)" if ! [[ -e $dir ]] ; then
sudo mkdir -p $dir || die $LINENO "could not create the directory of $real_configfile ($configfile)"
sudo chown ${STACK_USER} $dir
fi fi
merge_config_file $localfile $group $configfile
done done
done done
} }

View File

@@ -137,6 +137,9 @@ foo=bar
[some] [some]
random=config random=config
[[test12|run_tests.sh/test.conf]]
foo=bar
[[test-multi-sections|test-multi-sections.conf]] [[test-multi-sections|test-multi-sections.conf]]
[sec-1] [sec-1]
cfg_item1 = abcd cfg_item1 = abcd
@@ -389,13 +392,12 @@ EXPECT_VAL=0
check_result "$VAL" "$EXPECT_VAL" check_result "$VAL" "$EXPECT_VAL"
set -e set -e
echo -n "merge_config_group test10 not directory: " echo -n "merge_config_group test10 create directory: "
set +e set +e
# function is expected to fail and exit, running it STACK_USER=$(id -u -n)
# in a subprocess to let this script proceed merge_config_group test.conf test10
(merge_config_group test.conf test10)
VAL=$? VAL=$?
EXPECT_VAL=255 EXPECT_VAL=0
check_result "$VAL" "$EXPECT_VAL" check_result "$VAL" "$EXPECT_VAL"
set -e set -e
@@ -414,9 +416,21 @@ random = config
non = sense' non = sense'
check_result "$VAL" "$EXPECT_VAL" check_result "$VAL" "$EXPECT_VAL"
echo -n "merge_config_group test12 directory as 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 test12)
VAL=$?
EXPECT_VAL=255
check_result "$VAL" "$EXPECT_VAL"
set -e
rm -f test.conf test1c.conf test2a.conf \ rm -f test.conf test1c.conf test2a.conf \
test-space.conf test-equals.conf test-strip.conf \ test-space.conf test-equals.conf test-strip.conf \
test-colon.conf test-env.conf test-multiline.conf \ test-colon.conf test-env.conf test-multiline.conf \
test-multi-sections.conf test-same.conf test-multi-sections.conf test-same.conf
rm -rf test-etc rm -rf test-etc
rm -rf does-not-exist-dir