Module Name:    src
Committed By:   kre
Date:           Thu Aug  5 12:52:47 UTC 2021

Modified Files:
        src/sbin/devpubd/hooks: 02-wedgenames

Log Message:
Obliterate bogus $@ usage.

While here, fix some quoting, change some style, and attempt
to properly handle wedge names with embedded newlines, and those
that end with a '/' character (not particularly happy with the
solution to that last one, but it is better than it was).

Is there a reason that characters that need encoding in wedge names
(white space, and more) are encoded as %%XX (XX is the hex value of
the char - but 2 % chars?  Why?).   That remains unchanged, but as
the script already relied upon sh's $'...' quoting, I think we can rely
upon printf as well, so replace the old (very elegant, but slow) encoding
function with a much simpler one (does the same thing).


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sbin/devpubd/hooks/02-wedgenames

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/devpubd/hooks/02-wedgenames
diff -u src/sbin/devpubd/hooks/02-wedgenames:1.6 src/sbin/devpubd/hooks/02-wedgenames:1.7
--- src/sbin/devpubd/hooks/02-wedgenames:1.6	Sat Jan  9 16:25:19 2021
+++ src/sbin/devpubd/hooks/02-wedgenames	Thu Aug  5 12:52:47 2021
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# $NetBSD: 02-wedgenames,v 1.6 2021/01/09 16:25:19 mlelstv Exp $
+# $NetBSD: 02-wedgenames,v 1.7 2021/08/05 12:52:47 kre Exp $
 #
 # Try to maintain symlinks to wedge devices
 #
@@ -9,16 +9,15 @@ export LC_ALL=C
 
 event="$1"
 shift
-devices=$@
 
 wedgedir=/dev/wedges
 
 recurse()
 {
-	test -d "$1" && ls -1af "$1" \
-	| while read n; do
+	test -d "$1" &&
+	    ls -1af "$1" | while read n; do
 		case $n in
-		.|..) ;;
+		.|..)	;;
 		*)
 			echo "$1/$n"
 			if [ -L "$1/$n" ]; then
@@ -28,7 +27,7 @@ recurse()
 			fi
 			;;
 		esac
-	done
+	    done
 }
 
 simple_readlink()
@@ -41,43 +40,49 @@ simple_readlink()
 	esac
 }
 
-ordtable=$(
-	for n1 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
-	for n2 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
-		echo "\$'\x$n1$n2') x=$n1$n2;;"
-	done
-	done
-)
+#ordtable=$(
+#	for n1 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
+#	for n2 in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
+#		echo "\$'\x$n1$n2') x=$n1$n2;;"
+#	done
+#	done
+#)
+#
+#ord()
+#{
+#	local x
+#	eval "case \$1 in $ordtable esac"
+#	echo -n $x
+#}
 
 ord()
 {
-	local x
-	eval "case \$1 in $ordtable esac"
-	echo -n $x
+	printf %2.2x "'$1"
 }
 
 encode()
 {
-	local a
+	local a b c
 
 	a=$1
+	b=
 	while [ -n "$a" ]; do
 		c="${a%"${a#?}"}"
 		a=${a#?}
 		case $c in
-		[[:alnum:]._:\;!^$\&~\(\)[\]{}=,+\-/])
+		[][:alnum:]._:\;!^$\&~\(\)[{}=,+/-])
 			;;
 		*)
-			c='%%'$(ord "$c")
+			c=%%$(ord "$c")
 			;;
 		esac
-		echo -n "$c"
+		b=${b}${c}
 	done
+	printf %s "$b"
 }
 
 remove_wedge() {
-	recurse "$wedgedir" \
-	| while read w; do
+	recurse "$wedgedir" | while read w; do
 		t=$(simple_readlink "$w")
 		if [ x"$t" = x"/dev/$1" ]; then
 			rm -f "$w"
@@ -87,43 +92,72 @@ remove_wedge() {
 	done
 }
 
+wedge_label() {
+	local l
+
+	# dkctl getwedgeinfo always outputs 2 "lines", the first
+	# contains the label (and may contain embedded \n chars)
+	# the second contains the size, offset, and type, and one
+	# trailing \n (stripped by the $()) - so we can safely
+	# extract the label by deleting from the final \n in the
+	# value getwedgeinfo prints to the end
+
+	l=$(dkctl "$1" getwedgeinfo)
+	l=${l%$'\n'*}
+	case "${l}" in
+	$1' at '*': '*)
+		l=${l#*: }
+		;;
+	*)	
+		l=$1
+		;;
+	esac
+
+	# The trailing <END> is to ensure a trailing \n in the label
+	# is not deleted by a command substitution which invokes us.
+	# That will be rmeoved by the caller.
+	printf %s "${l}<END>"
+}
+
 add_wedge() {
-	dkctl "$1" getwedgeinfo \
-	| while read l; do
-		case $l in
-		*': '*)
-			n="${l#*: }"
-			n=$(encode "$n")
-			test -d $wedgedir || mkdir -m 755 $wedgedir
-			basedir="$wedgedir/$n"
-			basedir=${basedir%/*}
-			test -d "$basedir" || mkdir -p -m 755 "$basedir"
-			if oldlink=$(simple_readlink "$wedgedir/$n"); then
-				if [ x"$oldlink" != x"/dev/$1" ]; then
-					rm -f "$wedgedir/$n"
-					ln -s "/dev/$1" "$wedgedir/$n"
-				fi
-			else
+	local l n
+
+	l=$(wedge_label "$1")
+	l=${l%'<END>'}
+	case "$l" in */) l="${l}Wedge";; esac
+
+	n=$(encode "${l}")
+
+	(
+		umask 022
+
+		test -d "$wedgedir" || mkdir -m 755 "$wedgedir"
+		basedir="$wedgedir/$n"
+		basedir=${basedir%/*}
+		test -d "$basedir" || mkdir -p -m 755 "$basedir"
+		if oldlink=$(simple_readlink "$wedgedir/$n"); then
+			if [ x"$oldlink" != x"/dev/$1" ]; then
+				rm -f "$wedgedir/$n"
 				ln -s "/dev/$1" "$wedgedir/$n"
 			fi
-			;;
-		esac
-		break
-	done
+		else
+			ln -s "/dev/$1" "$wedgedir/$n"
+		fi
+	)
 }
 
-for device in $devices; do
+for device do
 	case $device in
 	dk*)
 		case $event in
 		device-attach)
-			remove_wedge $device
-			add_wedge $device
+			remove_wedge "$device"
+			add_wedge "$device"
 			;;
 		device-detach)
-			remove_wedge $device
+			remove_wedge "$device"
 			;;
 		esac
 		;;
 	esac
-done
+ce-attach

Reply via email to