Browse code

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>

Yi Wang authored on 2018/12/14 11:35:26
Showing 2 changed files
... ...
@@ -88,17 +88,22 @@ function iniget_multiline {
88 88
 }
89 89
 
90 90
 # 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
92 92
 function ini_has_option {
93 93
     local xtrace
94 94
     xtrace=$(set +o | grep xtrace)
95 95
     set +o xtrace
96
+    local sudo=""
97
+    if [ $1 == "-sudo" ]; then
98
+        sudo="sudo "
99
+        shift
100
+    fi
96 101
     local file=$1
97 102
     local section=$2
98 103
     local option=$3
99 104
     local line
100 105
 
101
-    line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
106
+    line=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
102 107
     $xtrace
103 108
     [ -n "$line" ]
104 109
 }
... ...
@@ -173,8 +178,10 @@ function iniset {
173 173
     xtrace=$(set +o | grep xtrace)
174 174
     set +o xtrace
175 175
     local sudo=""
176
+    local sudo_option=""
176 177
     if [ $1 == "-sudo" ]; then
177 178
         sudo="sudo "
179
+        sudo_option="-sudo "
178 180
         shift
179 181
     fi
180 182
     local file=$1
... ...
@@ -187,11 +194,11 @@ function iniset {
187 187
         return
188 188
     fi
189 189
 
190
-    if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then
190
+    if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then
191 191
         # Add section at the end
192 192
         echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
193 193
     fi
194
-    if ! ini_has_option "$file" "$section" "$option"; then
194
+    if ! ini_has_option $sudo_option "$file" "$section" "$option"; then
195 195
         # Add it
196 196
         $sudo sed -i -e "/^\[$section\]/ a\\
197 197
 $option = $value
... ...
@@ -228,7 +235,7 @@ function iniset_multiline {
228 228
         # the reverse order. Do a reverse here to keep the original order.
229 229
         values="$v ${values}"
230 230
     done
231
-    if ! grep -q "^\[$section\]" "$file"; then
231
+    if ! $sudo grep -q "^\[$section\]" "$file"; then
232 232
         # Add section at the end
233 233
         echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
234 234
     else
... ...
@@ -125,14 +125,14 @@ VAL=$(iniget ${TEST_INI} bbb handlers)
125 125
 assert_equal "$VAL" "33,44" "inset at EOF"
126 126
 
127 127
 # 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
129 129
     passed "ini_has_option: ddd.empty present"
130 130
 else
131 131
     failed "ini_has_option failed: ddd.empty not found"
132 132
 fi
133 133
 
134 134
 # 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
136 136
     passed "ini_has_option: bbb.handlers present"
137 137
 else
138 138
     failed "ini_has_option failed: bbb.handlers not found"