xref: /freebsd/usr.sbin/bsdconfig/share/media/directory.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
17323adacSDevin Teskeif [ ! "$_MEDIA_DIRECTORY_SUBR" ]; then _MEDIA_DIRECTORY_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/directory.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 TeskeDIRECTORY_CHECKED=
457323adacSDevin Teske
467323adacSDevin Teske############################################################ FUNCTIONS
477323adacSDevin Teske
487323adacSDevin Teske# f_media_set_directory
497323adacSDevin Teske#
507323adacSDevin Teske# Return success if we both found and set the media type to be a local
517323adacSDevin Teske# directory.
527323adacSDevin Teske#
537323adacSDevin Teske# Variables from variable.subr that can be used to script user input:
547323adacSDevin Teske#
557323adacSDevin Teske# 	VAR_DIRECTORY_PATH
567323adacSDevin Teske# 		Path to an existing directory containing the FreeBSD
577323adacSDevin Teske# 		distribution files.
587323adacSDevin Teske#
597323adacSDevin Teskef_media_set_directory()
607323adacSDevin Teske{
617323adacSDevin Teske	local path
627323adacSDevin Teske
637323adacSDevin Teske	f_media_close
647323adacSDevin Teske
657323adacSDevin Teske	f_variable_get_value $VAR_DIRECTORY_PATH \
667323adacSDevin Teske		"$msg_enter_a_fully_qualified_pathname_for_the_directory"
677323adacSDevin Teske	f_getvar $VAR_DIRECTORY_PATH path
687323adacSDevin Teske	[ "$path" ] || return $FAILURE
697323adacSDevin Teske
707323adacSDevin Teske	f_struct_new DEVICE device_directory
71a0f49bbaSDevin Teske	device_directory set name     "$path"
727323adacSDevin Teske	device_directory set get      f_media_get_directory
737323adacSDevin Teske	device_directory set init     f_media_init_directory
747323adacSDevin Teske	device_directory set shutdown f_media_shutdown_directory
757323adacSDevin Teske	device_directory set private  "$path"
767323adacSDevin Teske
777323adacSDevin Teske	f_struct_copy device_directory device_media
787323adacSDevin Teske	f_struct_free device_directory
797323adacSDevin Teske
807323adacSDevin Teske	f_struct device_media || return $FAILURE
817323adacSDevin Teske}
827323adacSDevin Teske
837323adacSDevin Teske# f_media_init_directory $device
847323adacSDevin Teske#
857323adacSDevin Teske# Initializes the Directory media device. Returns success if the directory path
867323adacSDevin Teske# both exists and is a directory.
877323adacSDevin Teske#
887323adacSDevin Teskef_media_init_directory()
897323adacSDevin Teske{
907323adacSDevin Teske	local dev="$1" path
917323adacSDevin Teske
92*9ecd54f2SDevin Teske	$dev get private path || return $FAILURE
937323adacSDevin Teske	f_dprintf "Init routine called for Directory device. path=[%s]" \
947323adacSDevin Teske	          "$path"
957323adacSDevin Teske
967323adacSDevin Teske	# Track whether we've been through here before (for remote filesystems
977323adacSDevin Teske	# mounted in the directory path, not repeating these queries saves us
987323adacSDevin Teske	# valuable time for slow/uncooperative links).
997323adacSDevin Teske	if [ "$DIRECTORY_CHECKED" ]; then
1007323adacSDevin Teske		f_dprintf "Directory device already checked."
1017323adacSDevin Teske		return $SUCCESS
1027323adacSDevin Teske	fi
1037323adacSDevin Teske
1047323adacSDevin Teske	if [ ! -e "$path" ]; then
1057323adacSDevin Teske		f_show_msg "$msg_no_such_file_or_directory" \
1067323adacSDevin Teske		           "f_media_init_directory" "$path"
1077323adacSDevin Teske		return $FAILURE
1087323adacSDevin Teske	elif [ ! -d "$path" ]; then
1097323adacSDevin Teske		f_show_msg "$msg_not_a_directory" \
1107323adacSDevin Teske		           "f_media_init_directory" "$path"
1117323adacSDevin Teske		return $FAILURE
1127323adacSDevin Teske	fi
1137323adacSDevin Teske	DIRECTORY_CHECKED=1
1147323adacSDevin Teske	return $SUCCESS
1157323adacSDevin Teske}
1167323adacSDevin Teske
117dde7be41SDevin Teske# f_media_get_directory $device $file [$probe_type]
1187323adacSDevin Teske#
1197323adacSDevin Teske# Returns data from $file in the existing/current filesystem. Similar to
120dde7be41SDevin Teske# cat(1). If $probe_type is present and non-NULL, returns success if $file
121dde7be41SDevin Teske# exists. If $probe_type is equal to $PROBE_SIZE, prints the size of $file in
122dde7be41SDevin Teske# bytes to standard-out.
1237323adacSDevin Teske#
1247323adacSDevin Teskef_media_get_directory()
1257323adacSDevin Teske{
126dde7be41SDevin Teske	local dev="$1" file="$2" probe_type="$3" path
127*9ecd54f2SDevin Teske	local name
1287323adacSDevin Teske
129*9ecd54f2SDevin Teske	$dev get name name
130dde7be41SDevin Teske	f_dprintf "f_media_get_directory: dev=[%s] file=[%s] probe_type=%s" \
131*9ecd54f2SDevin Teske	          "$name" "$file" "$probe_type"
1327323adacSDevin Teske
133*9ecd54f2SDevin Teske	$dev get private path
134dde7be41SDevin Teske	f_media_generic_get "$path" "$file" "$probe_type"
1357323adacSDevin Teske}
1367323adacSDevin Teske
1377323adacSDevin Teske# f_media_shutdown_directory $device
1387323adacSDevin Teske#
1397323adacSDevin Teske# Shuts down the Directory device. Return status should be ignored.
1407323adacSDevin Teske#
1417323adacSDevin Teskef_media_shutdown_directory()
1427323adacSDevin Teske{
1437323adacSDevin Teske	DIRECTORY_CHECKED=
1447323adacSDevin Teske}
1457323adacSDevin Teske
1467323adacSDevin Teske############################################################ MAIN
1477323adacSDevin Teske
1487323adacSDevin Teskef_dprintf "%s: Successfully loaded." media/directory.subr
1497323adacSDevin Teske
1507323adacSDevin Teskefi # ! $_MEDIA_DIRECTORY_SUBR
151