Deleted Added
full compact
usb.subr (259054) usb.subr (264840)
1if [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
1if [ ! "$_MEDIA_USB_SUBR" ]; then _MEDIA_USB_SUBR=1
2#
3# Copyright (c) 2012-2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/usb.subr 259054 2013-12-07 00:31:01Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/usb.subr 264840 2014-04-23 22:04:04Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/usb.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/media/common.subr
37f_include $BSDCFG_SHARE/struct.subr
38f_include $BSDCFG_SHARE/variable.subr
39
40BSDCFG_LIBE="/usr/libexec/bsdconfig"
41f_include_lang $BSDCFG_LIBE/include/messages.subr
42
43############################################################ GLOBALS
44
45USB_MOUNTED=
46
47############################################################ FUNCTIONS
48
49# f_media_set_usb
50#
51# Attempt to use USB as the media type. Return success if we both found and set
52# the media type to be a USB drive.
53#
54f_media_set_usb()
55{
56 f_media_close
57
58 local devs ndevs
59 f_device_find "" $DEVICE_TYPE_USB devs
60 f_count ndevs $devs
61
62 if [ ${ndevs:=0} -eq 0 ]; then
63 f_show_msg "$msg_no_usb_devices_found"
64 return $FAILURE
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/usb.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/dialog.subr
36f_include $BSDCFG_SHARE/media/common.subr
37f_include $BSDCFG_SHARE/struct.subr
38f_include $BSDCFG_SHARE/variable.subr
39
40BSDCFG_LIBE="/usr/libexec/bsdconfig"
41f_include_lang $BSDCFG_LIBE/include/messages.subr
42
43############################################################ GLOBALS
44
45USB_MOUNTED=
46
47############################################################ FUNCTIONS
48
49# f_media_set_usb
50#
51# Attempt to use USB as the media type. Return success if we both found and set
52# the media type to be a USB drive.
53#
54f_media_set_usb()
55{
56 f_media_close
57
58 local devs ndevs
59 f_device_find "" $DEVICE_TYPE_USB devs
60 f_count ndevs $devs
61
62 if [ ${ndevs:=0} -eq 0 ]; then
63 f_show_msg "$msg_no_usb_devices_found"
64 return $FAILURE
65 elif [ $ndevs -gt 1 ]; then
65 elif [ $ndevs -eq 1 ]; then
66 f_struct_copy $devs device_media
67 else
68 local dev
66 local title="$msg_choose_a_usb_drive"
67 local prompt="$msg_please_select_a_usb_drive"
69 local title="$msg_choose_a_usb_drive"
70 local prompt="$msg_please_select_a_usb_drive"
68 local hline=""
71 local hline=
69
72
70 local dev retval
71 dev=$( f_device_menu \
72 "$title" "$prompt" "$hline" $DEVICE_TYPE_USB \
73 dev=$( f_device_menu \
74 "$title" "$prompt" "$hline" $DEVICE_TYPE_USB \
73 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
74 retval=$?
75 [ "$dev" ] || return $FAILURE
75 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
76 return $FAILURE
76
77
77 f_device_find "$dev" $DEVICE_TYPE_USB devs
78 [ "$devs" ] || return $FAILURE
79 dev="${devs%%[$IFS]*}"
80
81 f_struct_copy device_$dev device_media
82 [ $retval -eq $SUCCESS ] || return $FAILURE
83 else
84 f_struct_copy device_$devs device_media
78 f_struct_copy "$dev" device_media
85 fi
86
87 f_struct device_media &&
88 device_media unset private
89
90 if f_interactive; then
91 local name
92 f_struct device_media get name name
93 f_show_msg "$msg_using_usb_device" "$name"
94 fi
95
96 f_struct device_media || return $FAILURE
97}
98
99# f_media_init_usb $device
100#
101# Initializes the USB media device. Returns success if able to mount the USB
102# disk device using mount(8).
103#
104f_media_init_usb()
105{
106 local funcname=f_media_init_usb
107 local dev="$1" devname err
108
79 fi
80
81 f_struct device_media &&
82 device_media unset private
83
84 if f_interactive; then
85 local name
86 f_struct device_media get name name
87 f_show_msg "$msg_using_usb_device" "$name"
88 fi
89
90 f_struct device_media || return $FAILURE
91}
92
93# f_media_init_usb $device
94#
95# Initializes the USB media device. Returns success if able to mount the USB
96# disk device using mount(8).
97#
98f_media_init_usb()
99{
100 local funcname=f_media_init_usb
101 local dev="$1" devname err
102
109 device_$dev get devname devname || return $FAILURE
103 $dev get devname devname || return $FAILURE
110 f_dprintf "Init routine called for USB device. devname=[%s]" \
111 "$devname"
112
113 if [ "$USB_MOUNTED" ]; then
114 f_dprintf "USB device already mounted."
115 return $SUCCESS
116 fi
117
118 if [ ! -e "$MOUNTPOINT" ]; then
119 f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
120 return $FAILURE
121 fi
122
123 if f_eval_catch -dk err $funcname mount \
124 'mount "%s" "%s"' "$devname" "$MOUNTPOINT"
125 then
126 USB_MOUNTED=1
127 return $SUCCESS
128 fi
129
130 err="${err#mount: }"; err="${err#$devname: }"
131 f_show_msg "$msg_error_mounting_usb_drive" \
132 "$devname" "$MOUNTPOINT" "$err"
133 return $FAILURE
134}
135
136# f_media_get_usb $device $file [$probe_type]
137#
138# Returns data from $file on a mounted USB disk device. Similar to cat(1). If
139# $probe_type is present and non-NULL, returns success if $file exists. If
140# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
141# standard-out.
142#
143f_media_get_usb()
144{
145 local dev="$1" file="$2" probe_type="$3"
104 f_dprintf "Init routine called for USB device. devname=[%s]" \
105 "$devname"
106
107 if [ "$USB_MOUNTED" ]; then
108 f_dprintf "USB device already mounted."
109 return $SUCCESS
110 fi
111
112 if [ ! -e "$MOUNTPOINT" ]; then
113 f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
114 return $FAILURE
115 fi
116
117 if f_eval_catch -dk err $funcname mount \
118 'mount "%s" "%s"' "$devname" "$MOUNTPOINT"
119 then
120 USB_MOUNTED=1
121 return $SUCCESS
122 fi
123
124 err="${err#mount: }"; err="${err#$devname: }"
125 f_show_msg "$msg_error_mounting_usb_drive" \
126 "$devname" "$MOUNTPOINT" "$err"
127 return $FAILURE
128}
129
130# f_media_get_usb $device $file [$probe_type]
131#
132# Returns data from $file on a mounted USB disk device. Similar to cat(1). If
133# $probe_type is present and non-NULL, returns success if $file exists. If
134# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
135# standard-out.
136#
137f_media_get_usb()
138{
139 local dev="$1" file="$2" probe_type="$3"
140 local name
146
141
142 $dev get name name
147 f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
143 f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
148 "$dev" "$file" "$probe_type"
144 "$name" "$file" "$probe_type"
149
150 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
151}
152
153# f_media_shutdown_usb $device
154#
155# Shuts down the USB disk device using umount(8). Return status should be
156# ignored.
157#
158f_media_shutdown_usb()
159{
160 local funcname=f_media_shutdown_usb
161 local dev="$1" err
162
163 [ "$USB_MOUNTED" ] || return $FAILURE
164
165 if ! f_eval_catch -dk err $funcname umount \
166 'umount -f "%s"' "$MOUNTPOINT"
167 then
168 err="${err#umount: }"; err="${err#*: }"
169 f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
170 "$MOUNTPOINT" "$err"
171 else
172 USB_MOUNTED=
173 fi
174}
175
176############################################################ MAIN
177
178f_dprintf "%s: Successfully loaded." media/usb.subr
179
180fi # ! $_MEDIA_USB_SUBR
145
146 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
147}
148
149# f_media_shutdown_usb $device
150#
151# Shuts down the USB disk device using umount(8). Return status should be
152# ignored.
153#
154f_media_shutdown_usb()
155{
156 local funcname=f_media_shutdown_usb
157 local dev="$1" err
158
159 [ "$USB_MOUNTED" ] || return $FAILURE
160
161 if ! f_eval_catch -dk err $funcname umount \
162 'umount -f "%s"' "$MOUNTPOINT"
163 then
164 err="${err#umount: }"; err="${err#*: }"
165 f_show_msg "$msg_could_not_unmount_the_ufs_partition" \
166 "$MOUNTPOINT" "$err"
167 else
168 USB_MOUNTED=
169 fi
170}
171
172############################################################ MAIN
173
174f_dprintf "%s: Successfully loaded." media/usb.subr
175
176fi # ! $_MEDIA_USB_SUBR