Skip to content

Commit 698796f

Browse files
committed
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 <yi.c.wang@intel.com>
1 parent a88a229 commit 698796f

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

inc/ini-config

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,22 @@ function iniget_multiline {
8888
}
8989

9090
# Determinate is the given option present in the INI file
91-
# ini_has_option config-file section option
91+
# ini_has_option [-sudo] config-file section option
9292
function ini_has_option {
9393
local xtrace
9494
xtrace=$(set +o | grep xtrace)
9595
set +o xtrace
96+
local sudo=""
97+
if [ $1 == "-sudo" ]; then
98+
sudo="sudo "
99+
shift
100+
fi
96101
local file=$1
97102
local section=$2
98103
local option=$3
99104
local line
100105

101-
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
106+
line=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
102107
$xtrace
103108
[ -n "$line" ]
104109
}
@@ -173,8 +178,10 @@ function iniset {
173178
xtrace=$(set +o | grep xtrace)
174179
set +o xtrace
175180
local sudo=""
181+
local sudo_option=""
176182
if [ $1 == "-sudo" ]; then
177183
sudo="sudo "
184+
sudo_option="-sudo "
178185
shift
179186
fi
180187
local file=$1
@@ -187,11 +194,11 @@ function iniset {
187194
return
188195
fi
189196

190-
if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
197+
if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then
191198
# Add section at the end
192199
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
193200
fi
194-
if ! ini_has_option "$file" "$section" "$option"; then
201+
if ! ini_has_option $sudo_option "$file" "$section" "$option"; then
195202
# Add it
196203
$sudo sed -i -e "/^\[$section\]/ a\\
197204
$option = $value
@@ -228,7 +235,7 @@ function iniset_multiline {
228235
# the reverse order. Do a reverse here to keep the original order.
229236
values="$v ${values}"
230237
done
231-
if ! grep -q "^\[$section\]" "$file"; then
238+
if ! $sudo grep -q "^\[$section\]" "$file"; then
232239
# Add section at the end
233240
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
234241
else

tests/test_ini_config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ VAL=$(iniget ${TEST_INI} bbb handlers)
125125
assert_equal "$VAL" "33,44" "inset at EOF"
126126

127127
# test empty option
128-
if ini_has_option ${TEST_INI} ddd empty; then
128+
if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then
129129
passed "ini_has_option: ddd.empty present"
130130
else
131131
failed "ini_has_option failed: ddd.empty not found"
132132
fi
133133

134134
# test non-empty option
135-
if ini_has_option ${TEST_INI} bbb handlers; then
135+
if ini_has_option ${SUDO_ARG} ${TEST_INI} bbb handlers; then
136136
passed "ini_has_option: bbb.handlers present"
137137
else
138138
failed "ini_has_option failed: bbb.handlers not found"

0 commit comments

Comments
 (0)