Deleted Added
full compact
cdrom.subr (259054) cdrom.subr (264840)
1if [ ! "$_MEDIA_CDROM_SUBR" ]; then _MEDIA_CDROM_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_CDROM_SUBR" ]; then _MEDIA_CDROM_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/cdrom.subr 259054 2013-12-07 00:31:01Z dteske $
27# $FreeBSD: head/usr.sbin/bsdconfig/share/media/cdrom.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/cdrom.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
45CDROM_MOUNTED=
46CDROM_PREVIOUSLY_MOUNTED=
47CDROM_INIT_QUIET=
48
49############################################################ FUNCTIONS
50
51# f_media_set_cdrom
52#
53# Return success if we both found and set the media type to be a CD.
54#
55f_media_set_cdrom()
56{
57 f_media_close
58
59 local devs ndevs
60 f_device_find "" $DEVICE_TYPE_CDROM devs
61 f_count ndevs $devs
62
63 if [ ${ndevs:=0} -eq 0 ]; then
64 f_interactive && f_show_msg "$msg_no_cd_dvd_devices_found"
65 return $FAILURE
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." media/cdrom.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
45CDROM_MOUNTED=
46CDROM_PREVIOUSLY_MOUNTED=
47CDROM_INIT_QUIET=
48
49############################################################ FUNCTIONS
50
51# f_media_set_cdrom
52#
53# Return success if we both found and set the media type to be a CD.
54#
55f_media_set_cdrom()
56{
57 f_media_close
58
59 local devs ndevs
60 f_device_find "" $DEVICE_TYPE_CDROM devs
61 f_count ndevs $devs
62
63 if [ ${ndevs:=0} -eq 0 ]; then
64 f_interactive && f_show_msg "$msg_no_cd_dvd_devices_found"
65 return $FAILURE
66 elif [ $ndevs -gt 1 ]; then
66 elif [ $ndevs -eq 1 ]; then
67 f_struct_copy $devs device_media
68 else
69 local dev
67 local title="$msg_choose_a_cd_dvd_type"
68 local prompt="$msg_please_select_a_cd_dvd_drive"
70 local title="$msg_choose_a_cd_dvd_type"
71 local prompt="$msg_please_select_a_cd_dvd_drive"
69 local hline=""
72 local hline=
70
73
71 local dev retval
72 dev=$( f_device_menu \
73 "$title" "$prompt" "$hline" $DEVICE_TYPE_CDROM \
74 dev=$( f_device_menu \
75 "$title" "$prompt" "$hline" $DEVICE_TYPE_CDROM \
74 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
75 retval=$?
76 [ "$dev" ] || return $FAILURE
76 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
77 return $FAILURE
77
78
78 f_device_find "$dev" $DEVICE_TYPE_CDROM devs
79 [ "$devs" ] || return $FAILURE
80 dev="${devs%%[$IFS]*}"
81
82 f_struct_copy device_$dev device_media
83 [ $retval -eq $SUCCESS ] || return $FAILURE
84 else
85 f_struct_copy device_$devs device_media
79 f_struct_copy "$dev" device_media
86 fi
87
88 f_struct device_media || return $FAILURE
89}
90
91# f_media_init_cdrom $device
92#
93# Initializes the CDROM media device. Returns success if able to mount the CD
94# device using mount_cd9660(8).
95#
96f_media_init_cdrom()
97{
98 local funcname=f_media_init_cdrom
99 local dev="$1" devname err
100
80 fi
81
82 f_struct device_media || return $FAILURE
83}
84
85# f_media_init_cdrom $device
86#
87# Initializes the CDROM media device. Returns success if able to mount the CD
88# device using mount_cd9660(8).
89#
90f_media_init_cdrom()
91{
92 local funcname=f_media_init_cdrom
93 local dev="$1" devname err
94
101 device_$dev get devname devname || return $FAILURE
95 f_struct "$dev" get devname devname || return $FAILURE
102 f_dprintf "Init routine called for CDROM device. devname=[%s]" \
103 "$devname"
104
105 if [ "$CDROM_MOUNTED" ]; then
106 f_dprintf "CDROM device already mounted."
107 return $SUCCESS
108 fi
109
110 if [ ! -e "$MOUNTPOINT" ]; then
111 f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
112 return $FAILURE
113 fi
114
115 if ! f_eval_catch -dk err $funcname mount_cd9660 \
116 'mount_cd9660 "%s" "%s"' "$devname" "$MOUNTPOINT"
117 then
118 err="${err#mount_cd9660: }"; err="${err#$devname: }"
119 case "$err" in
120 "Device busy")
121 # Perhaps the CDROM drive is already mounted as /cdrom
122 if f_mounted /cdrom; then
123 CDROM_PREVIOUSLY_MOUNTED=1
124 MOUNTPOINT=/cdrom
125 err=
126 fi
127 ;;
128 esac
129 case "$err" in
130 "") : good ;; # no error
131 *)
132 [ "$CDROM_INIT_QUIET" ] ||
133 f_show_msg "$msg_error_mounting_device" \
134 "$devname" "$MOUNTPOINT" "$err"
135 return $FAILURE
136 esac
137 fi
138 CDROM_MOUNTED=1
139
140 : xxx # /cdrom.inf has been deprecated since 9.0-R
141
142 # No other CDROM media validation at this time
143
144 return $SUCCESS
145}
146
147# f_media_get_cdrom $device $file [$probe_type]
148#
149# Returns data from $file on a mounted CDROM device. Similar to cat(1). If
150# $probe_type is present and non-NULL, returns success if $file exists. If
151# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
152# standard-out.
153#
154f_media_get_cdrom()
155{
156 local dev="$1" file="$2" probe_type="$3"
96 f_dprintf "Init routine called for CDROM device. devname=[%s]" \
97 "$devname"
98
99 if [ "$CDROM_MOUNTED" ]; then
100 f_dprintf "CDROM device already mounted."
101 return $SUCCESS
102 fi
103
104 if [ ! -e "$MOUNTPOINT" ]; then
105 f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
106 return $FAILURE
107 fi
108
109 if ! f_eval_catch -dk err $funcname mount_cd9660 \
110 'mount_cd9660 "%s" "%s"' "$devname" "$MOUNTPOINT"
111 then
112 err="${err#mount_cd9660: }"; err="${err#$devname: }"
113 case "$err" in
114 "Device busy")
115 # Perhaps the CDROM drive is already mounted as /cdrom
116 if f_mounted /cdrom; then
117 CDROM_PREVIOUSLY_MOUNTED=1
118 MOUNTPOINT=/cdrom
119 err=
120 fi
121 ;;
122 esac
123 case "$err" in
124 "") : good ;; # no error
125 *)
126 [ "$CDROM_INIT_QUIET" ] ||
127 f_show_msg "$msg_error_mounting_device" \
128 "$devname" "$MOUNTPOINT" "$err"
129 return $FAILURE
130 esac
131 fi
132 CDROM_MOUNTED=1
133
134 : xxx # /cdrom.inf has been deprecated since 9.0-R
135
136 # No other CDROM media validation at this time
137
138 return $SUCCESS
139}
140
141# f_media_get_cdrom $device $file [$probe_type]
142#
143# Returns data from $file on a mounted CDROM device. Similar to cat(1). If
144# $probe_type is present and non-NULL, returns success if $file exists. If
145# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
146# standard-out.
147#
148f_media_get_cdrom()
149{
150 local dev="$1" file="$2" probe_type="$3"
151 local name
157
152
153 $dev get name name
158 f_dprintf "f_media_get_cdrom: dev=[%s] file=[%s] probe_type=%s" \
154 f_dprintf "f_media_get_cdrom: dev=[%s] file=[%s] probe_type=%s" \
159 "$dev" "$file" "$probe_type"
155 "$name" "$file" "$probe_type"
160
161 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
162}
163
164# f_media_shutdown_cdrom $device
165#
166# Shuts down the CDROM device. Return status should be ignored.
167#
168f_media_shutdown_cdrom()
169{
170 local funcname=f_media_shutdown_cdrom
171 local dev="$1" err
172
173 [ "$CDROM_MOUNTED" ] || return $FAILURE
174
175 if [ "$CDROM_PREVIOUSLY_MOUNTED" ]; then
176 CDROM_MOUNTED=
177 return $SUCCESS
178 fi
179
180 if ! f_eval_catch -dk err $funcname umount \
156
157 f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
158}
159
160# f_media_shutdown_cdrom $device
161#
162# Shuts down the CDROM device. Return status should be ignored.
163#
164f_media_shutdown_cdrom()
165{
166 local funcname=f_media_shutdown_cdrom
167 local dev="$1" err
168
169 [ "$CDROM_MOUNTED" ] || return $FAILURE
170
171 if [ "$CDROM_PREVIOUSLY_MOUNTED" ]; then
172 CDROM_MOUNTED=
173 return $SUCCESS
174 fi
175
176 if ! f_eval_catch -dk err $funcname umount \
181 'umount -f "%s"' "$MOUNPOINT"
177 'umount -f "%s"' "$MOUNTPOINT"
182 then
183 err="${err#umount: }"; err="${err#*: }"
184 f_show_msg "$msg_could_not_unmount_the_cdrom_dvd" \
185 "$MOUNTPOINT" "$err"
186 else
187 CDROM_MOUNTED=
188 fi
189}
190
191# f_media_eject_cdrom $device
192#
193# Eject the media from the CDROM device. Returns success.
194#
195f_media_eject_cdrom()
196{
197 local funcname=f_media_eject_cdrom
178 then
179 err="${err#umount: }"; err="${err#*: }"
180 f_show_msg "$msg_could_not_unmount_the_cdrom_dvd" \
181 "$MOUNTPOINT" "$err"
182 else
183 CDROM_MOUNTED=
184 fi
185}
186
187# f_media_eject_cdrom $device
188#
189# Eject the media from the CDROM device. Returns success.
190#
191f_media_eject_cdrom()
192{
193 local funcname=f_media_eject_cdrom
198 local dev="$1" devname err
199 device_$dev get name devname || return $SUCCESS
200 case "$devname" in /dev/iso9660/*) return $SUCCESS; esac
194 local dev="$1" name devname err
195
196 f_struct "$dev" || return $SUCCESS
197 $dev get name name || return $SUCCESS
198 $dev get devname devname || return $SUCCESS
199
200 # Don't eject labels
201 case "$name" in */*) return $SUCCESS; esac
202
201 f_dprintf "Ejecting CDROM/DVD at %s" "$devname"
202 if ! f_eval_catch -dk err $funcname cdcontrol \
203 'cdcontrol -f "%s" eject' "$devname"
204 then
205 f_dprintf "Could not eject the CDROM/DVD from %s: %s" \
206 "$devname" "${err#cdcontrol: }"
207 fi
208 return $SUCCESS
209}
210
211############################################################ MAIN
212
213f_dprintf "%s: Successfully loaded." media/cdrom.subr
214
215fi # ! $_MEDIA_CDROM_SUBR
203 f_dprintf "Ejecting CDROM/DVD at %s" "$devname"
204 if ! f_eval_catch -dk err $funcname cdcontrol \
205 'cdcontrol -f "%s" eject' "$devname"
206 then
207 f_dprintf "Could not eject the CDROM/DVD from %s: %s" \
208 "$devname" "${err#cdcontrol: }"
209 fi
210 return $SUCCESS
211}
212
213############################################################ MAIN
214
215f_dprintf "%s: Successfully loaded." media/cdrom.subr
216
217fi # ! $_MEDIA_CDROM_SUBR