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