directory.subr revision 252077
1247280Sdteskeif [ ! "$_MEDIA_DIRECTORY_SUBR" ]; then _MEDIA_DIRECTORY_SUBR=1
2247280Sdteske#
3247280Sdteske# Copyright (c) 2012-2013 Devin Teske
4247280Sdteske# All Rights Reserved.
5247280Sdteske#
6247280Sdteske# Redistribution and use in source and binary forms, with or without
7247280Sdteske# modification, are permitted provided that the following conditions
8247280Sdteske# are met:
9247280Sdteske# 1. Redistributions of source code must retain the above copyright
10247280Sdteske#    notice, this list of conditions and the following disclaimer.
11247280Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12247280Sdteske#    notice, this list of conditions and the following disclaimer in the
13247280Sdteske#    documentation and/or other materials provided with the distribution.
14247280Sdteske#
15247280Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16247280Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17247280Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18247280Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19247280Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20247280Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21247280Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22247280Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23247280Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24247280Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25247280Sdteske# SUCH DAMAGE.
26247280Sdteske#
27247280Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/media/directory.subr 252077 2013-06-21 23:13:55Z dteske $
28247280Sdteske#
29247280Sdteske############################################################ INCLUDES
30247280Sdteske
31247280SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32247280Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33247280Sdteskef_dprintf "%s: loading includes..." media/directory.subr
34247280Sdteskef_include $BSDCFG_SHARE/device.subr
35247280Sdteskef_include $BSDCFG_SHARE/dialog.subr
36252077Sdteskef_include $BSDCFG_SHARE/media/common.subr
37252077Sdteskef_include $BSDCFG_SHARE/struct.subr
38247280Sdteskef_include $BSDCFG_SHARE/variable.subr
39247280Sdteske
40247280SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig"
41247280Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr
42247280Sdteske
43247280Sdteske############################################################ GLOBALS
44247280Sdteske
45247280SdteskeDIRECTORY_CHECKED=
46247280Sdteske
47247280Sdteske############################################################ FUNCTIONS
48247280Sdteske
49247280Sdteske# f_media_set_directory
50247280Sdteske#
51247280Sdteske# Return success if we both found and set the media type to be a local
52247280Sdteske# directory.
53247280Sdteske#
54247280Sdteske# Variables from variable.subr that can be used to script user input:
55247280Sdteske#
56247280Sdteske# 	VAR_DIRECTORY_PATH
57247280Sdteske# 		Path to an existing directory containing the FreeBSD
58247280Sdteske# 		distribution files.
59247280Sdteske#
60247280Sdteskef_media_set_directory()
61247280Sdteske{
62247280Sdteske	local path
63247280Sdteske
64247280Sdteske	f_media_close
65247280Sdteske
66247280Sdteske	f_variable_get_value $VAR_DIRECTORY_PATH \
67247280Sdteske		"$msg_enter_a_fully_qualified_pathname_for_the_directory"
68247280Sdteske	f_getvar $VAR_DIRECTORY_PATH path
69247280Sdteske	[ "$path" ] || return $FAILURE
70247280Sdteske
71247280Sdteske	f_struct_new DEVICE device_directory
72247280Sdteske	device_directory set get      f_media_get_directory
73247280Sdteske	device_directory set init     f_media_init_directory
74247280Sdteske	device_directory set shutdown f_media_shutdown_directory
75247280Sdteske	device_directory set private  "$path"
76247280Sdteske
77247280Sdteske	f_struct_copy device_directory device_media
78247280Sdteske	f_struct_free device_directory
79247280Sdteske
80247280Sdteske	f_struct device_media || return $FAILURE
81247280Sdteske}
82247280Sdteske
83247280Sdteske# f_media_init_directory $device
84247280Sdteske#
85247280Sdteske# Initializes the Directory media device. Returns success if the directory path
86247280Sdteske# both exists and is a directory.
87247280Sdteske#
88247280Sdteskef_media_init_directory()
89247280Sdteske{
90247280Sdteske	local dev="$1" path
91247280Sdteske
92247280Sdteske	device_$dev get private path || return $FAILURE
93247280Sdteske	f_dprintf "Init routine called for Directory device. path=[%s]" \
94247280Sdteske	          "$path"
95247280Sdteske
96247280Sdteske	# Track whether we've been through here before (for remote filesystems
97247280Sdteske	# mounted in the directory path, not repeating these queries saves us
98247280Sdteske	# valuable time for slow/uncooperative links).
99247280Sdteske	if [ "$DIRECTORY_CHECKED" ]; then
100247280Sdteske		f_dprintf "Directory device already checked."
101247280Sdteske		return $SUCCESS
102247280Sdteske	fi
103247280Sdteske
104247280Sdteske	if [ ! -e "$path" ]; then
105247280Sdteske		f_show_msg "$msg_no_such_file_or_directory" \
106247280Sdteske		           "f_media_init_directory" "$path"
107247280Sdteske		return $FAILURE
108247280Sdteske	elif [ ! -d "$path" ]; then
109247280Sdteske		f_show_msg "$msg_not_a_directory" \
110247280Sdteske		           "f_media_init_directory" "$path"
111247280Sdteske		return $FAILURE
112247280Sdteske	fi
113247280Sdteske	DIRECTORY_CHECKED=1
114247280Sdteske	return $SUCCESS
115247280Sdteske}
116247280Sdteske
117247280Sdteske# f_media_get_directory $device $file [$probe_only]
118247280Sdteske#
119247280Sdteske# Returns data from $file in the existing/current filesystem. Similar to
120247280Sdteske# cat(1). $probe_only is currently unused by this media type.
121247280Sdteske#
122247280Sdteskef_media_get_directory()
123247280Sdteske{
124247280Sdteske	local dev="$1" file="$2" probe_only="$3" path
125247280Sdteske
126247280Sdteske	f_dprintf "f_media_get_directory: dev=[%s] file=[%s] probe_only=%s" \
127247280Sdteske	          "$dev" "$file" "$probe_only"
128247280Sdteske
129247280Sdteske	device_$dev get private path
130247280Sdteske	f_media_generic_get "$path" "$file"
131247280Sdteske}
132247280Sdteske
133247280Sdteske# f_media_shutdown_directory $device
134247280Sdteske#
135247280Sdteske# Shuts down the Directory device. Return status should be ignored.
136247280Sdteske#
137247280Sdteskef_media_shutdown_directory()
138247280Sdteske{
139247280Sdteske	DIRECTORY_CHECKED=
140247280Sdteske}
141247280Sdteske
142247280Sdteske############################################################ MAIN
143247280Sdteske
144247280Sdteskef_dprintf "%s: Successfully loaded." media/directory.subr
145247280Sdteske
146247280Sdteskefi # ! $_MEDIA_DIRECTORY_SUBR
147