From 95513ab538396f2b5132d01a65905672ef8c34fb Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 29 Oct 2013 12:23:07 +0100 Subject: [PATCH] __line: handle fixed strings correctly, posix gave us -F -x for this case Signed-off-by: Nico Schottelius --- cdist/conf/type/__line/explorer/state | 8 +++++--- cdist/conf/type/__line/gencode-remote | 27 +++++++++++++++++++-------- cdist/conf/type/__line/man.text | 11 ++++++----- docs/changelog | 1 + 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/cdist/conf/type/__line/explorer/state b/cdist/conf/type/__line/explorer/state index d240bf4d..d04d5d09 100755 --- a/cdist/conf/type/__line/explorer/state +++ b/cdist/conf/type/__line/explorer/state @@ -1,6 +1,6 @@ #!/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. # @@ -24,16 +24,18 @@ file="/$__object_id" if [ -f "$__object/parameter/regex" ]; then regex=$(cat "$__object/parameter/regex") + greparg="" else if [ ! -f "$__object/parameter/line" ]; then echo "Parameter line and regex missing - cannot explore" >&2 exit 1 fi - regex="^$(cat "$__object/parameter/line")\$" + regex="$(cat "$__object/parameter/line")" + greparg="-F -x" fi # 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 else echo absent diff --git a/cdist/conf/type/__line/gencode-remote b/cdist/conf/type/__line/gencode-remote index d381fb20..69f4d67a 100755 --- a/cdist/conf/type/__line/gencode-remote +++ b/cdist/conf/type/__line/gencode-remote @@ -38,7 +38,19 @@ case "$state_should" in exit 1 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) @@ -47,17 +59,16 @@ case "$state_should" in exit 1 fi - [ "$line" ] && regex="^$line\$" - - # Replace all / with \/ - regex=$(echo "$regex" | sed 's;/;\\/;g') - echo $regex >&2 + greparg="" + if [ "$line" ]; then + regex="$line" + greparg="-F -x" + fi cat << eof 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 - #echo "echo q | ex -c \"/${line}/d|w|q\" \"${file}\"" ;; *) echo "Unknown state: $state_should" >&2 diff --git a/cdist/conf/type/__line/man.text b/cdist/conf/type/__line/man.text index e1a5941c..f39ee929 100644 --- a/cdist/conf/type/__line/man.text +++ b/cdist/conf/type/__line/man.text @@ -32,11 +32,11 @@ regex:: given line, if the given regular expression does not match. In case of absent, ensure all lines matching the - regular expression are absent (cannot be combined with - the line parameter, if state is absent). + regular expression are absent. - If the regular expression contains / (slashes), they need - to be escaped with \ (backslash): / becomes \/. + The regular expression is interpreted by grep. + + Must not be combined with line, if state is absent. 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 -------- - cdist-type(7) +- grep(1) 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). diff --git a/docs/changelog b/docs/changelog index 9128bab8..eb756f07 100644 --- a/docs/changelog +++ b/docs/changelog @@ -6,6 +6,7 @@ Changelog 2.3.6: * New Type: __locale + * Type __line: Ensure special characters are not interpreted 2.3.5: 2013-10-10 * Core: Unit test fix for remote_copy (Steven Armstrong)