xref: /freebsd/usr.sbin/bsdconfig/share/media/directory.subr (revision 02e9120893770924227138ba49df1edb3896112a)
1if [ ! "$_MEDIA_DIRECTORY_SUBR" ]; then _MEDIA_DIRECTORY_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/directory.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
44DIRECTORY_CHECKED=
45
46############################################################ FUNCTIONS
47
48# f_media_set_directory
49#
50# Return success if we both found and set the media type to be a local
51# directory.
52#
53# Variables from variable.subr that can be used to script user input:
54#
55# 	VAR_DIRECTORY_PATH
56# 		Path to an existing directory containing the FreeBSD
57# 		distribution files.
58#
59f_media_set_directory()
60{
61	local path
62
63	f_media_close
64
65	f_variable_get_value $VAR_DIRECTORY_PATH \
66		"$msg_enter_a_fully_qualified_pathname_for_the_directory"
67	f_getvar $VAR_DIRECTORY_PATH path
68	[ "$path" ] || return $FAILURE
69
70	f_struct_new DEVICE device_directory
71	device_directory set name     "$path"
72	device_directory set get      f_media_get_directory
73	device_directory set init     f_media_init_directory
74	device_directory set shutdown f_media_shutdown_directory
75	device_directory set private  "$path"
76
77	f_struct_copy device_directory device_media
78	f_struct_free device_directory
79
80	f_struct device_media || return $FAILURE
81}
82
83# f_media_init_directory $device
84#
85# Initializes the Directory media device. Returns success if the directory path
86# both exists and is a directory.
87#
88f_media_init_directory()
89{
90	local dev="$1" path
91
92	$dev get private path || return $FAILURE
93	f_dprintf "Init routine called for Directory device. path=[%s]" \
94	          "$path"
95
96	# Track whether we've been through here before (for remote filesystems
97	# mounted in the directory path, not repeating these queries saves us
98	# valuable time for slow/uncooperative links).
99	if [ "$DIRECTORY_CHECKED" ]; then
100		f_dprintf "Directory device already checked."
101		return $SUCCESS
102	fi
103
104	if [ ! -e "$path" ]; then
105		f_show_msg "$msg_no_such_file_or_directory" \
106		           "f_media_init_directory" "$path"
107		return $FAILURE
108	elif [ ! -d "$path" ]; then
109		f_show_msg "$msg_not_a_directory" \
110		           "f_media_init_directory" "$path"
111		return $FAILURE
112	fi
113	DIRECTORY_CHECKED=1
114	return $SUCCESS
115}
116
117# f_media_get_directory $device $file [$probe_type]
118#
119# Returns data from $file in the existing/current filesystem. Similar to
120# cat(1). If $probe_type is present and non-NULL, returns success if $file
121# exists. If $probe_type is equal to $PROBE_SIZE, prints the size of $file in
122# bytes to standard-out.
123#
124f_media_get_directory()
125{
126	local dev="$1" file="$2" probe_type="$3" path
127	local name
128
129	$dev get name name
130	f_dprintf "f_media_get_directory: dev=[%s] file=[%s] probe_type=%s" \
131	          "$name" "$file" "$probe_type"
132
133	$dev get private path
134	f_media_generic_get "$path" "$file" "$probe_type"
135}
136
137# f_media_shutdown_directory $device
138#
139# Shuts down the Directory device. Return status should be ignored.
140#
141f_media_shutdown_directory()
142{
143	DIRECTORY_CHECKED=
144}
145
146############################################################ MAIN
147
148f_dprintf "%s: Successfully loaded." media/directory.subr
149
150fi # ! $_MEDIA_DIRECTORY_SUBR
151