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