xref: /freebsd/usr.sbin/bsdconfig/share/media/dos.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
17323adacSDevin Teskeif [ ! "$_MEDIA_DOS_SUBR" ]; then _MEDIA_DOS_SUBR=1
27323adacSDevin Teske#
37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
4f8ea072aSDevin Teske# All rights reserved.
57323adacSDevin Teske#
67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without
77323adacSDevin Teske# modification, are permitted provided that the following conditions
87323adacSDevin Teske# are met:
97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright
107323adacSDevin Teske#    notice, this list of conditions and the following disclaimer.
117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
127323adacSDevin Teske#    notice, this list of conditions and the following disclaimer in the
137323adacSDevin Teske#    documentation and/or other materials provided with the distribution.
147323adacSDevin Teske#
157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187323adacSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257323adacSDevin Teske# SUCH DAMAGE.
267323adacSDevin Teske#
277323adacSDevin Teske#
287323adacSDevin Teske############################################################ INCLUDES
297323adacSDevin Teske
307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
327323adacSDevin Teskef_dprintf "%s: loading includes..." media/dos.subr
337323adacSDevin Teskef_include $BSDCFG_SHARE/device.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
357323adacSDevin Teskef_include $BSDCFG_SHARE/media/common.subr
361de60ff0SDevin Teskef_include $BSDCFG_SHARE/struct.subr
371de60ff0SDevin Teskef_include $BSDCFG_SHARE/variable.subr
387323adacSDevin Teske
397323adacSDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
407323adacSDevin Teskef_include_lang $BSDCFG_LIBE/include/messages.subr
417323adacSDevin Teske
427323adacSDevin Teske############################################################ GLOBALS
437323adacSDevin Teske
447323adacSDevin TeskeDOS_MOUNTED=
457323adacSDevin Teske
467323adacSDevin Teske############################################################ FUNCTIONS
477323adacSDevin Teske
487323adacSDevin Teske# f_media_set_dos
497323adacSDevin Teske#
507323adacSDevin Teske# Return success if we both found and set the media type to be a DOS partition.
517323adacSDevin Teske#
527323adacSDevin Teskef_media_set_dos()
537323adacSDevin Teske{
547323adacSDevin Teske	f_media_close
557323adacSDevin Teske
567323adacSDevin Teske	local devs ndevs
577323adacSDevin Teske	f_device_find "" $DEVICE_TYPE_DOS devs
58d4ae33f0SDevin Teske	f_count ndevs $devs
597323adacSDevin Teske
607323adacSDevin Teske	if [ ${ndevs:=0} -eq 0 ]; then
617079fc4eSDevin Teske		f_show_msg "$msg_no_dos_primary_partitions_found"
627323adacSDevin Teske		return $FAILURE
63*9ecd54f2SDevin Teske	elif [ $ndevs -eq 1 ]; then
64*9ecd54f2SDevin Teske		f_struct_copy $devs device_media
65*9ecd54f2SDevin Teske	else
66*9ecd54f2SDevin Teske		local dev
677323adacSDevin Teske		local title="$msg_choose_a_dos_partition"
687323adacSDevin Teske		local prompt="$msg_please_select_dos_partition"
69*9ecd54f2SDevin Teske		local hline=
707323adacSDevin Teske
717323adacSDevin Teske		dev=$( f_device_menu \
727323adacSDevin Teske			"$title" "$prompt" "$hline" $DEVICE_TYPE_DOS \
73*9ecd54f2SDevin Teske			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
74*9ecd54f2SDevin Teske			return $FAILURE
757323adacSDevin Teske
76*9ecd54f2SDevin Teske		f_struct_copy "$dev" device_media
777323adacSDevin Teske	fi
787323adacSDevin Teske
797323adacSDevin Teske	f_struct device_media || return $FAILURE
807323adacSDevin Teske}
817323adacSDevin Teske
827323adacSDevin Teske# f_media_init_dos $device
837323adacSDevin Teske#
847323adacSDevin Teske# Initializes the DOS media device. Returns success if able to mount the DOS
857323adacSDevin Teske# partition device using mount_msdosfs(8).
867323adacSDevin Teske#
877323adacSDevin Teskef_media_init_dos()
887323adacSDevin Teske{
89d4ae33f0SDevin Teske	local funcname=f_media_init_dos
907323adacSDevin Teske	local dev="$1" devname err
917323adacSDevin Teske
92*9ecd54f2SDevin Teske	$dev get devname devname || return $FAILURE
937323adacSDevin Teske	f_dprintf "Init routine called for DOS device. devname=[%s]" \
947323adacSDevin Teske	          "$devname"
957323adacSDevin Teske
967323adacSDevin Teske	if [ "$DOS_MOUNTED" ]; then
977323adacSDevin Teske		f_dprintf "DOS device already mounted."
987323adacSDevin Teske		return $SUCCESS
997323adacSDevin Teske	fi
1007323adacSDevin Teske
101d4ae33f0SDevin Teske	if [ ! -e "$MOUNTPOINT" ]; then
102d4ae33f0SDevin Teske		f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
1037323adacSDevin Teske			return $FAILURE
1047323adacSDevin Teske	fi
1057323adacSDevin Teske
106d4ae33f0SDevin Teske	if ! f_eval_catch -dk err $funcname mount_msdosfs \
107d4ae33f0SDevin Teske		'mount_msdosfs "%s" "%s"' "$devname" "$MOUNTPOINT"
1087323adacSDevin Teske	then
1097323adacSDevin Teske		err="${err#mount_msdosfs: }"; err="${err#$devname: }"
1107323adacSDevin Teske		f_show_msg "$msg_error_mounting_device" \
1117323adacSDevin Teske		           "$devname" "$MOUNTPOINT" "$err"
1127323adacSDevin Teske		return $FAILURE
1137323adacSDevin Teske	fi
1147323adacSDevin Teske	DOS_MOUNTED=1
1157323adacSDevin Teske	return $SUCCESS
1167323adacSDevin Teske}
1177323adacSDevin Teske
118dde7be41SDevin Teske# f_media_get_dos $device $file [$probe_type]
1197323adacSDevin Teske#
1207323adacSDevin Teske# Returns data from $file on a mounted DOS partition device. Similar to cat(1).
121dde7be41SDevin Teske# If $probe_type is present and non-NULL, returns success if $file exists. If
122dde7be41SDevin Teske# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
123dde7be41SDevin Teske# standard-out.
1247323adacSDevin Teske#
1257323adacSDevin Teskef_media_get_dos()
1267323adacSDevin Teske{
127dde7be41SDevin Teske	local dev="$1" file="$2" probe_type="$3"
128*9ecd54f2SDevin Teske	local name
1297323adacSDevin Teske
130*9ecd54f2SDevin Teske	$dev get name name
131dde7be41SDevin Teske	f_dprintf "f_media_get_dos: dev=[%s] file=[%s] probe_type=%s" \
132*9ecd54f2SDevin Teske	          "$name" "$file" "$probe_type"
1337323adacSDevin Teske
134dde7be41SDevin Teske	f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
1357323adacSDevin Teske}
1367323adacSDevin Teske
1377323adacSDevin Teske# f_media_shutdown_dos $device
1387323adacSDevin Teske#
1397323adacSDevin Teske# Shuts down the DOS partition device using umount(8). Return status should be
1407323adacSDevin Teske# ignored.
1417323adacSDevin Teske#
1427323adacSDevin Teskef_media_shutdown_dos()
1437323adacSDevin Teske{
144d4ae33f0SDevin Teske	local funcname=f_media_shutdown_dos
1457323adacSDevin Teske	local dev="$1" err
1467323adacSDevin Teske
147f677a9e2SDevin Teske	[ "$DOS_MOUNTED" ] || return $FAILURE
1487323adacSDevin Teske
149d4ae33f0SDevin Teske	if ! f_eval_catch -dk err $funcname umount \
150d4ae33f0SDevin Teske		'umount -f "%s"' "$MOUNTPOINT"
151d4ae33f0SDevin Teske	then
1527323adacSDevin Teske		err="${err#umount: }"; err="${err#*: }"
1537323adacSDevin Teske		f_show_msg "$msg_could_not_unmount_the_dos_partition" \
1547323adacSDevin Teske		           "$MOUNTPOINT" "$err"
1557323adacSDevin Teske	else
1567323adacSDevin Teske		DOS_MOUNTED=
1577323adacSDevin Teske	fi
1587323adacSDevin Teske}
1597323adacSDevin Teske
1607323adacSDevin Teske############################################################ MAIN
1617323adacSDevin Teske
1627323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/dos.subr
1637323adacSDevin Teske
1647323adacSDevin Teskefi # ! $_MEDIA_DOS_SUBR
165