summaryrefslogtreecommitdiff
path: root/sbin/devpubd/hooks/02-wedgenames
blob: aeda6aa0b7d573567b23356c4f44297578557613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
#
# $NetBSD: 02-wedgenames,v 1.8 2021/08/08 10:48:35 martin Exp $
#
# Try to maintain symlinks to wedge devices
#

export LC_ALL=C

event="$1"
shift

wedgedir=/dev/wedges

recurse()
{
	test -d "$1" &&
	    ls -1af "$1" | while read n; do
		case $n in
		.|..)	;;
		*)
			echo "$1/$n"
			if [ -L "$1/$n" ]; then
				: #nothing
			elif [ -d "$1/$n" ]; then
				recurse "$1/$n"
			fi
			;;
		esac
	    done
}

simple_readlink()
{
	local x

	x=$(test -e "$1" && ls -ld "$1")
	case $x in
	*'-> '*) echo ${x#*-> };;
	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
#)
#
#ord()
#{
#	local x
#	eval "case \$1 in $ordtable esac"
#	echo -n $x
#}

ord()
{
	printf %2.2x "'$1"
}

encode()
{
	local a b c

	a=$1
	b=
	while [ -n "$a" ]; do
		c="${a%"${a#?}"}"
		a=${a#?}
		case $c in
		[][:alnum:]._:\;!^$\&~\(\)[{}=,+/-])
			;;
		*)
			c=%%$(ord "$c")
			;;
		esac
		b=${b}${c}
	done
	printf %s "$b"
}

remove_wedge() {
	recurse "$wedgedir" | while read w; do
		t=$(simple_readlink "$w")
		if [ x"$t" = x"/dev/$1" ]; then
			rm -f "$w"
			basedir=${w%/*}
			rmdir -p "$basedir" 2>/dev/null
		fi
	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() {
	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
		else
			ln -s "/dev/$1" "$wedgedir/$n"
		fi
	)
}

for device do
	case $device in
	dk*)
		case $event in
		device-attach)
			remove_wedge "$device"
			add_wedge "$device"
			;;
		device-detach)
			remove_wedge "$device"
			;;
		esac
		;;
	esac
done