Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/devpubd/hooks Obliterate bogus $@ usage.



details:   https://anonhg.NetBSD.org/src/rev/0e2e9d3cdf04
branches:  trunk
changeset: 1022758:0e2e9d3cdf04
user:      kre <kre%NetBSD.org@localhost>
date:      Thu Aug 05 12:52:47 2021 +0000

description:
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).

diffstat:

 sbin/devpubd/hooks/02-wedgenames |  128 ++++++++++++++++++++++++--------------
 1 files changed, 81 insertions(+), 47 deletions(-)

diffs (204 lines):

diff -r a3f16fd802a1 -r 0e2e9d3cdf04 sbin/devpubd/hooks/02-wedgenames
--- a/sbin/devpubd/hooks/02-wedgenames  Thu Aug 05 12:45:33 2021 +0000
+++ b/sbin/devpubd/hooks/02-wedgenames  Thu Aug 05 12:52:47 2021 +0000
@@ -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 @@
 
 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 @@
                        fi
                        ;;
                esac
-       done
+           done
 }
 
 simple_readlink()
@@ -41,43 +40,49 @@
        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 @@
        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



Home | Main Index | Thread Index | Old Index