__line: handle fixed strings correctly, posix gave us -F -x for this case

Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
Nico Schottelius 2013-10-29 12:23:07 +01:00
parent 990581a459
commit 95513ab538
4 changed files with 31 additions and 16 deletions

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# 2012 Nico Schottelius (nico-cdist at schottelius.org) # 2012-2013 Nico Schottelius (nico-cdist at schottelius.org)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -24,16 +24,18 @@ file="/$__object_id"
if [ -f "$__object/parameter/regex" ]; then if [ -f "$__object/parameter/regex" ]; then
regex=$(cat "$__object/parameter/regex") regex=$(cat "$__object/parameter/regex")
greparg=""
else else
if [ ! -f "$__object/parameter/line" ]; then if [ ! -f "$__object/parameter/line" ]; then
echo "Parameter line and regex missing - cannot explore" >&2 echo "Parameter line and regex missing - cannot explore" >&2
exit 1 exit 1
fi fi
regex="^$(cat "$__object/parameter/line")\$" regex="$(cat "$__object/parameter/line")"
greparg="-F -x"
fi fi
# Allow missing file - thus 2>/dev/null # Allow missing file - thus 2>/dev/null
if grep -q "$regex" "$file" 2>/dev/null; then if grep -q $greparg "$regex" "$file" 2>/dev/null; then
echo present echo present
else else
echo absent echo absent

View file

@ -38,7 +38,19 @@ case "$state_should" in
exit 1 exit 1
fi fi
echo "echo \"$line\" >> $file" #echo "echo \"$line\" >> $file"
#line_sanitised=$(cat "$__object/parameter/line" | sed 's/"/\"/g')
# Idea: replace ' in the string:
# '"'"'
# |------> ': end the string
# |-|---> "'": create ' in the output string
# |--> ': continue the string
#
# Replace all \ so \t and other combinations are not interpreted
#
line_sanitised=$(cat "$__object/parameter/line" | sed -e "s/'/'\"'\"'/g" -e 's/\\/\\\\/g')
echo "echo '$line_sanitised' >> $file"
;; ;;
absent) absent)
@ -47,17 +59,16 @@ case "$state_should" in
exit 1 exit 1
fi fi
[ "$line" ] && regex="^$line\$" greparg=""
if [ "$line" ]; then
# Replace all / with \/ regex="$line"
regex=$(echo "$regex" | sed 's;/;\\/;g') greparg="-F -x"
echo $regex >&2 fi
cat << eof cat << eof
tmp=\$(mktemp) tmp=\$(mktemp)
sed '/$regex/d' "$file" > \$tmp && cat "\$tmp" > "$file" && rm -f "\$tmp" grep -v $greparg '$regex' '$file' > \$tmp && cat "\$tmp" > '$file' && rm -f "\$tmp"
eof eof
#echo "echo q | ex -c \"/${line}/d|w|q\" \"${file}\""
;; ;;
*) *)
echo "Unknown state: $state_should" >&2 echo "Unknown state: $state_should" >&2

View file

@ -32,11 +32,11 @@ regex::
given line, if the given regular expression does not match. given line, if the given regular expression does not match.
In case of absent, ensure all lines matching the In case of absent, ensure all lines matching the
regular expression are absent (cannot be combined with regular expression are absent.
the line parameter, if state is absent).
If the regular expression contains / (slashes), they need The regular expression is interpreted by grep.
to be escaped with \ (backslash): / becomes \/.
Must not be combined with line, if state is absent.
file:: file::
If supplied, use this as the destination file. If supplied, use this as the destination file.
@ -64,9 +64,10 @@ __line legacy_timezone --file /etc/rc.conf --regex 'TIMEZONE=.*' --state absent
SEE ALSO SEE ALSO
-------- --------
- cdist-type(7) - cdist-type(7)
- grep(1)
COPYING COPYING
------- -------
Copyright \(C) 2012 Nico Schottelius. Free use of this software is Copyright \(C) 2012-2013 Nico Schottelius. Free use of this software is
granted under the terms of the GNU General Public License version 3 (GPLv3). granted under the terms of the GNU General Public License version 3 (GPLv3).

View file

@ -6,6 +6,7 @@ Changelog
2.3.6: 2.3.6:
* New Type: __locale * New Type: __locale
* Type __line: Ensure special characters are not interpreted
2.3.5: 2013-10-10 2.3.5: 2013-10-10
* Core: Unit test fix for remote_copy (Steven Armstrong) * Core: Unit test fix for remote_copy (Steven Armstrong)