From 698796f1aeb0d9a559488bad9f1d03e4941b061e Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Fri, 14 Dec 2018 10:35:26 +0800 Subject: [PATCH] Fix an issue in iniset function Given the file to be configured, if user "stack" even doesn't have read access, the result of configuration is not expected. iniset with "-sudo" option will always create the section and the option which we want to configure for each calling, no matter whether this section and this option exist in the file or not. The root cause is the calling of grep and ini_has_option in iniset don't use the "sudo" option. Change-Id: I9d21322046b7be411c4c7c28fefc24894fa2e131 Signed-off-by: Yi Wang --- inc/ini-config | 17 ++++++++++++----- tests/test_ini_config.sh | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/inc/ini-config b/inc/ini-config index 6fe7788158..79936823d2 100644 --- a/inc/ini-config +++ b/inc/ini-config @@ -88,17 +88,22 @@ function iniget_multiline { } # Determinate is the given option present in the INI file -# ini_has_option config-file section option +# ini_has_option [-sudo] config-file section option function ini_has_option { local xtrace xtrace=$(set +o | grep xtrace) set +o xtrace + local sudo="" + if [ $1 == "-sudo" ]; then + sudo="sudo " + shift + fi local file=$1 local section=$2 local option=$3 local line - line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") + line=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file") $xtrace [ -n "$line" ] } @@ -173,8 +178,10 @@ function iniset { xtrace=$(set +o | grep xtrace) set +o xtrace local sudo="" + local sudo_option="" if [ $1 == "-sudo" ]; then sudo="sudo " + sudo_option="-sudo " shift fi local file=$1 @@ -187,11 +194,11 @@ function iniset { return fi - if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then + if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then # Add section at the end echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null fi - if ! ini_has_option "$file" "$section" "$option"; then + if ! ini_has_option $sudo_option "$file" "$section" "$option"; then # Add it $sudo sed -i -e "/^\[$section\]/ a\\ $option = $value @@ -228,7 +235,7 @@ function iniset_multiline { # the reverse order. Do a reverse here to keep the original order. values="$v ${values}" done - if ! grep -q "^\[$section\]" "$file"; then + if ! $sudo grep -q "^\[$section\]" "$file"; then # Add section at the end echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null else diff --git a/tests/test_ini_config.sh b/tests/test_ini_config.sh index f7dc89a28d..6ed1647f34 100755 --- a/tests/test_ini_config.sh +++ b/tests/test_ini_config.sh @@ -125,14 +125,14 @@ VAL=$(iniget ${TEST_INI} bbb handlers) assert_equal "$VAL" "33,44" "inset at EOF" # test empty option -if ini_has_option ${TEST_INI} ddd empty; then +if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then passed "ini_has_option: ddd.empty present" else failed "ini_has_option failed: ddd.empty not found" fi # test non-empty option -if ini_has_option ${TEST_INI} bbb handlers; then +if ini_has_option ${SUDO_ARG} ${TEST_INI} bbb handlers; then passed "ini_has_option: bbb.handlers present" else failed "ini_has_option failed: bbb.handlers not found"