xref: /freebsd/usr.sbin/bsdconfig/share/media/dos.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1if [ ! "$_MEDIA_DOS_SUBR" ]; then _MEDIA_DOS_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/dos.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
44DOS_MOUNTED=
45
46############################################################ FUNCTIONS
47
48# f_media_set_dos
49#
50# Return success if we both found and set the media type to be a DOS partition.
51#
52f_media_set_dos()
53{
54	f_media_close
55
56	local devs ndevs
57	f_device_find "" $DEVICE_TYPE_DOS devs
58	f_count ndevs $devs
59
60	if [ ${ndevs:=0} -eq 0 ]; then
61		f_show_msg "$msg_no_dos_primary_partitions_found"
62		return $FAILURE
63	elif [ $ndevs -eq 1 ]; then
64		f_struct_copy $devs device_media
65	else
66		local dev
67		local title="$msg_choose_a_dos_partition"
68		local prompt="$msg_please_select_dos_partition"
69		local hline=
70
71		dev=$( f_device_menu \
72			"$title" "$prompt" "$hline" $DEVICE_TYPE_DOS \
73			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) ||
74			return $FAILURE
75
76		f_struct_copy "$dev" device_media
77	fi
78
79	f_struct device_media || return $FAILURE
80}
81
82# f_media_init_dos $device
83#
84# Initializes the DOS media device. Returns success if able to mount the DOS
85# partition device using mount_msdosfs(8).
86#
87f_media_init_dos()
88{
89	local funcname=f_media_init_dos
90	local dev="$1" devname err
91
92	$dev get devname devname || return $FAILURE
93	f_dprintf "Init routine called for DOS device. devname=[%s]" \
94	          "$devname"
95
96	if [ "$DOS_MOUNTED" ]; then
97		f_dprintf "DOS device already mounted."
98		return $SUCCESS
99	fi
100
101	if [ ! -e "$MOUNTPOINT" ]; then
102		f_eval_catch $funcname mkdir 'mkdir -p "%s"' "$MOUNTPOINT" ||
103			return $FAILURE
104	fi
105
106	if ! f_eval_catch -dk err $funcname mount_msdosfs \
107		'mount_msdosfs "%s" "%s"' "$devname" "$MOUNTPOINT"
108	then
109		err="${err#mount_msdosfs: }"; err="${err#$devname: }"
110		f_show_msg "$msg_error_mounting_device" \
111		           "$devname" "$MOUNTPOINT" "$err"
112		return $FAILURE
113	fi
114	DOS_MOUNTED=1
115	return $SUCCESS
116}
117
118# f_media_get_dos $device $file [$probe_type]
119#
120# Returns data from $file on a mounted DOS partition device. Similar to cat(1).
121# If $probe_type is present and non-NULL, returns success if $file exists. If
122# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
123# standard-out.
124#
125f_media_get_dos()
126{
127	local dev="$1" file="$2" probe_type="$3"
128	local name
129
130	$dev get name name
131	f_dprintf "f_media_get_dos: dev=[%s] file=[%s] probe_type=%s" \
132	          "$name" "$file" "$probe_type"
133
134	f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
135}
136
137# f_media_shutdown_dos $device
138#
139# Shuts down the DOS partition device using umount(8). Return status should be
140# ignored.
141#
142f_media_shutdown_dos()
143{
144	local funcname=f_media_shutdown_dos
145	local dev="$1" err
146
147	[ "$DOS_MOUNTED" ] || return $FAILURE
148
149	if ! f_eval_catch -dk err $funcname umount \
150		'umount -f "%s"' "$MOUNTPOINT"
151	then
152		err="${err#umount: }"; err="${err#*: }"
153		f_show_msg "$msg_could_not_unmount_the_dos_partition" \
154		           "$MOUNTPOINT" "$err"
155	else
156		DOS_MOUNTED=
157	fi
158}
159
160############################################################ MAIN
161
162f_dprintf "%s: Successfully loaded." media/dos.subr
163
164fi # ! $_MEDIA_DOS_SUBR
165