xref: /freebsd/usr.sbin/bsdconfig/share/media/dos.subr (revision e14ddd1f16e7e5788392c50de21ea7c927e0690c)
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 (INLUDING, 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/struct.subr
35f_include $BSDCFG_SHARE/device.subr
36f_include $BSDCFG_SHARE/dialog.subr
37f_include $BSDCFG_SHARE/variable.subr
38f_include $BSDCFG_SHARE/media/common.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	ndevs=$( set -- $devs; echo $# )
60
61	if [ ${ndevs:=0} -eq 0 ]; then
62		f_dialog_msgbox "$msg_no_dos_primary_partitions_found"
63		return $FAILURE
64	elif [ $ndevs -gt 1 ]; then
65		local title="$msg_choose_a_dos_partition"
66		local prompt="$msg_please_select_dos_partition"
67		local hline=""
68
69		local dev retval
70		dev=$( f_device_menu \
71			"$title" "$prompt" "$hline" $DEVICE_TYPE_DOS \
72			2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD )
73		retval=$?
74		[ "$dev" ] || return $FAILURE
75
76		f_device_find "$dev" $DEVICE_TYPE_DOS devs
77		[ "$devs" ] || return $FAILURE
78		dev="${devs%%[$IFS]*}"
79
80		f_struct_copy device_$dev device_media
81		[ $retval -eq $SUCCESS ] || return $FAILURE
82	else
83		f_struct_copy device_$devs device_media
84	fi
85
86	f_struct device_media || return $FAILURE
87}
88
89# f_media_init_dos $device
90#
91# Initializes the DOS media device. Returns success if able to mount the DOS
92# partition device using mount_msdosfs(8).
93#
94f_media_init_dos()
95{
96	local dev="$1" devname err
97
98	device_$dev get devname devname || return $FAILURE
99	f_dprintf "Init routine called for DOS device. devname=[%s]" \
100	          "$devname"
101
102	if [ "$DOS_MOUNTED" ]; then
103		f_dprintf "DOS device already mounted."
104		return $SUCCESS
105	fi
106
107	if [ ! -e "$MOUNTPOINT" ] &&
108	   ! err=$( mkdir -p "$MOUNTPOINT" 2>&1 )
109	then
110		f_dialog_msgbox "$err"
111		return $FAILURE
112	fi
113
114	if ! err=$( mount_msdosfs "$devname" "$MOUNTPOINT" 2>&1 )
115	then
116		err="${err#mount_msdosfs: }"; err="${err#$devname: }"
117		f_show_msg "$msg_error_mounting_device" \
118		           "$devname" "$MOUNTPOINT" "$err"
119		return $FAILURE
120	fi
121	DOS_MOUNTED=1
122	return $SUCCESS
123}
124
125# f_media_get_dos $device $file [$probe_only]
126#
127# Returns data from $file on a mounted DOS partition device. Similar to cat(1).
128# $probe_only is currently unused by this media type.
129#
130f_media_get_dos()
131{
132	local dev="$1" file="$2" probe_only="$3"
133
134	f_dprintf "f_media_get_dos: dev=[%s] file=[%s] probe_only=%s" \
135	          "$dev" "$file" "$probe_only"
136
137	f_media_generic_get "$MOUNTPOINT" "$file"
138}
139
140# f_media_shutdown_dos $device
141#
142# Shuts down the DOS partition device using umount(8). Return status should be
143# ignored.
144#
145f_media_shutdown_dos()
146{
147	local dev="$1" err
148
149	[ "$DOS_MOUNTED" ] || return
150
151	if ! err=$( umount -f "$MOUNTPOINT" 2>&1 ); 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