From bc10704aec66e143b8c9e715a74ddb82f44a3204 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Tue, 30 Apr 2024 15:00:34 +1000 Subject: [PATCH] ctdb-scripts: Improve NFS-Ganesha export path extraction Path values do not need to have quotes. The current code fails if there aren't any. Instead, implement a 2 stage parser using 2 sed commands. See comments in the code for details. Regexps are POSIX basic regular expressions, apart from \ (used to ensure WORD is on word boundaries, and the 'i' flag for case insensitivity. The latter is supported in FreeBSD sed. This code successfully parses Path values out of the following monstrosity: path = "/foo/bar1;a"; Path = /foo/bar2; Something = false; Pseudo = "/foo/bar3x" ; Path = "/foo/bar3; y" ; Access_type = RO; Pseudo = "/foo/bar4x" ; path=/foo/bar4; Access_type = RO; Pseudo = "/foo/barNONONO" ; not_Path=/foo/barNONONO; Access_type = RO; Path = /foo/bar5 Pseudo = "/foo/bar6x Path=foo" ; Path=/foo/bar6; Access_type = RO This is probably the best that can be done within a shell script. Signed-off-by: Martin Schwenke Reviewed-by: Volker Lendecke --- ctdb/doc/examples/nfs-ganesha-callout | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/ctdb/doc/examples/nfs-ganesha-callout b/ctdb/doc/examples/nfs-ganesha-callout index 64f57506d9f..f08dc495626 100755 --- a/ctdb/doc/examples/nfs-ganesha-callout +++ b/ctdb/doc/examples/nfs-ganesha-callout @@ -324,9 +324,29 @@ nfs_startup() nfs_monitor_list_shares() { - grep Path "$nfs_exports_file" | - cut -f2 -d\" | - sort -u + # The 1st sed command prints anything after "Path = ", where + # Path is matched case-insensitively, and must be on a word + # boundary. This also deletes any semicolon-terminated items + # before Path. Each output line now starts with a value for + # Path, but may have other settings after a semicolon. + _s1='s/.*;*[[:space:]]*\[[:space:]]*=[[:space:]]*//ip' + + # The 2nd sed command has 2 steps: + # + # a. Attempt to match an unquoted value not containing + # semicolon or double-quote, followed by an optional + # line-terminating semicolon or a semicolon followed by + # anything else. Keep the value and double-quote it. If + # the value was already quoted then the line will be + # unchanged. The pattern space now starts with a + # double-quoted value. + _s2a='s/^\([^";][^";]*\)[[:space:]]*\(;*[[:space:]]*$\|;.*\)/"\1"/' + # b. Finally, print the contents of double-quotes at the + # beginning of the pattern space, discarding anything + # that follows. + _s2b='s/^"\([^"][^"]*\)".*/\1/p' + + sed -n -e "$_s1" "$nfs_exports_file" | sed -n -e "$_s2a" -e "$_s2b" } ################################################## -- 2.47.3