Re-implement $probe_only aspect of f_media_get_TYPE() (where TYPE is cdrom,

nfs, ftp, http, httpproxy, etc.) and f_device_get() (abstract method for
calling aforementioned f_media_get_TYPE()).

Previously, if $probe_only was present and non-NULL, the TYPE functions
would check for $file and exit with an appropriate error status (success if
the file exists and readable, failure otherwise).

While this has been retained, a pair of globals has been introduced:
$PROBE_EXIST and $PROBE_SIZE (see `/usr/share/bsdconfig/media/common.subr')

The $PROBE_EXIST global can be used where you need the functionality of
simply testing for existence (previously the _only_ functionality).

Meanwhile, the new $PROBE_SIZE global can be used to cause the TYPE function
to print the size of the file (in bytes) on standard-out (or -1) if not
found or an error occurs. NOTE: If an error occurs, it is logged with the
dprintf function, which is visible with `-d' flag or debug=1.

In many cases, where you need to get the size of a file _and_ check for its
existence, you can use the return status of a $PROBE_SIZE call.
This commit is contained in:
Devin Teske 2013-07-14 03:08:52 +00:00
parent 0ccd2fb09e
commit dde7be41df
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=253333
12 changed files with 159 additions and 81 deletions

View File

@ -144,19 +144,21 @@ f_media_init_cdrom()
return $SUCCESS
}
# f_media_get_cdrom $device $file [$probe_only]
# f_media_get_cdrom $device $file [$probe_type]
#
# Returns data from $file on a mounted CDROM device. Similar to cat(1). If
# $probe_only is present and non-NULL, returns success if $file exists.
# $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_get_cdrom()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_cdrom: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_cdrom: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
}
# f_media_shutdown_cdrom $device

View File

@ -42,6 +42,13 @@ f_include $BSDCFG_SHARE/struct.subr
#
MOUNTPOINT=/dist
#
# Media probe values to use for `f_media_get_TYPE media $file $PROBE' or
# `f_device_get media $file $PROBE' (where $PROBE is one of the below values).
#
PROBE_EXIST=1
PROBE_SIZE=2
############################################################ FUNCTIONS
# f_media_open
@ -83,18 +90,20 @@ f_media_verify()
f_struct device_media || f_media_get_type
}
# f_media_generic_get $base $file [$probe_only]
# f_media_generic_get $base $file [$probe_type]
#
# A generic open which follows a well-known "path" of places to look. If
# $probe_only is present and non-NULL, returns success if $file exists.
# $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_generic_get()
{
local base="$1" file="$2" probe_only="$3"
local base="$1" file="$2" probe_type="$3"
local fname=f_media_generic_get
f_dprintf "%s: base=[%s] files=[%s] probe_only=%s" \
$fname "$base" "$file" "$probe_only"
f_dprintf "%s: base=[%s] files=[%s] probe_type=%s" \
$fname "$base" "$file" "$probe_type"
local rel path
f_getvar $VAR_RELNAME rel
@ -106,7 +115,17 @@ f_media_generic_get()
; do
if [ -f "$path" -a -r "$path" ]; then
f_dprintf "%s: file exists path=[%s]" $fname "$path"
[ "$probe_only" ] && return $SUCCESS
if [ "$probe_type" = "$PROBE_SIZE" ]; then
local size
if ! size=$( stat -f %z "$path" 2>&1 ); then
f_dprintf "stat: %s" "$size"
echo "-1"
else
f_isinteger "$size" || size=-1
echo $size
fi
fi
[ "$probe_type" ] && return $SUCCESS
cat "$path"
return
fi
@ -115,8 +134,19 @@ f_media_generic_get()
path="$base/releases/$rel/$file" # Final path to try
if [ -f "$path" -a -r "$path" ]; then
f_dprintf "%s: file exists path=[%s]" $fname "$path"
[ "$probe_only" ] && return $SUCCESS
elif [ "$probe_only" ]; then
if [ "$probe_type" = "$PROBE_SIZE" ]; then
local size
if ! size=$( stat -f %z "$path" 2>&1 ); then
f_dprintf "stat: %s" "$size"
echo "-1"
else
f_isinteger "$size" || size=-1
echo $size
fi
fi
[ "$probe_type" ] && return $SUCCESS
elif [ "$probe_type" ]; then
[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
return $FAILURE
fi
cat "$base/releases/$rel/$file" # Final path to try

View File

@ -115,21 +115,22 @@ f_media_init_directory()
return $SUCCESS
}
# f_media_get_directory $device $file [$probe_only]
# f_media_get_directory $device $file [$probe_type]
#
# Returns data from $file in the existing/current filesystem. Similar to
# cat(1). If $probe_only is present and non-NULL, returns success if $file
# exists.
# cat(1). If $probe_type is present and non-NULL, returns success if $file
# exists. If $probe_type is equal to $PROBE_SIZE, prints the size of $file in
# bytes to standard-out.
#
f_media_get_directory()
{
local dev="$1" file="$2" probe_only="$3" path
local dev="$1" file="$2" probe_type="$3" path
f_dprintf "f_media_get_directory: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_directory: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
device_$dev get private path
f_media_generic_get "$path" "$file" "$probe_only"
f_media_generic_get "$path" "$file" "$probe_type"
}
# f_media_shutdown_directory $device

View File

@ -122,19 +122,21 @@ f_media_init_dos()
return $SUCCESS
}
# f_media_get_dos $device $file [$probe_only]
# f_media_get_dos $device $file [$probe_type]
#
# Returns data from $file on a mounted DOS partition device. Similar to cat(1).
# If $probe_only is present and non-NULL, returns success if $file exists.
# If $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_get_dos()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_dos: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_dos: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
}
# f_media_shutdown_dos $device

View File

@ -144,17 +144,19 @@ f_media_init_floppy()
return $SUCCESS
}
# f_media_get_floppy $device $file [$probe_only]
# f_media_get_floppy $device $file [$probe_type]
#
# Returns data from $file on a mounted Floppy disk device. Similar to cat(1).
# If $probe_only is present and non-null, limits retries to zero.
# If $probe_type is present and non-NULL, limits retries to zero and returns
# success if $file exists. If $probe_type is equal to $PROBE_SIZE, prints the
# size of $file in bytes to standard-out.
#
f_media_get_floppy()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_floppy: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_floppy: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
#
# floppies don't use f_media_generic_get() because it's too expensive
@ -166,11 +168,13 @@ f_media_get_floppy()
local fp="${mp:=$MOUNTPOINT}/$file"
if ! [ -f "$fp" -a -r "$fp" ]; then
local nretries=4
[ "$probe_only" ] && return $FAILURE
[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
[ "$probe_type" ] && return $FAILURE
while ! [ -f "$fp" -a -r "$fp" ]; do
if [ $nretries -eq 0 ]; then
f_show_msg "$msg_failed_to_get_floppy_file" \
"$fp"
[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
return $FAILURE
fi
FLOPPY_DISTWANTED="$fp"
@ -178,9 +182,17 @@ f_media_get_floppy()
f_media_init_floppy "$dev" || return $FAILURE
nretries=$(( $nretries - 1 ))
done
elif [ "$probe_only" ]; then
return $SUCCESS
fi
#
# If we reach here, $file exists
#
if [ "$probe_type" = "$PROBE_SIZE" ]; then
local size
size=$( stat -f %z "$fp" 2>&1 ) || f_dprintf "stat: %s" "$size"
f_isinteger "$size" || size=-1
echo "$size"
fi
[ "$probe_type" ] && return $SUCCESS
cat "$fp"
}

View File

@ -787,12 +787,13 @@ f_media_init_ftp()
return $FAILURE
}
# f_media_get_ftp $device $file [$probe_only]
# f_media_get_ftp $device $file [$probe_type]
#
# Returns data from $file on an FTP server using ftp(1). Please note that
# $device is unused but must be present (even if null). Information is instead
# gathered from the environment. If $probe_only is present and non-NULL,
# returns success if $file exists.
# gathered from the environment. If $probe_type is present and non-NULL,
# returns success if $file exists. If $probe_type is equal to $PROBE_SIZE,
# prints the size of $file in bytes to standard-out.
#
# Variables from variable.subr used to configure the connection are as follows
# (all of which are configured by f_media_set_ftp above):
@ -826,10 +827,10 @@ f_media_init_ftp()
#
f_media_get_ftp()
{
local dev="$1" file="$2" probe_only="$3" hosts=
local dev="$1" file="$2" probe_type="$3" hosts=
f_dprintf "f_media_get_ftp: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_ftp: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
local ftp_host ftp_port
f_getvar $VAR_FTP_HOST ftp_host
@ -899,14 +900,16 @@ f_media_get_ftp()
f_dprintf "sending ftp request for: %s" "ftp://$host$port/$dir/$file"
if [ "$probe_only" ]; then
local url="ftp://$userpass$host$port/$dir/$file"
if [ "$probe_type" ]; then
local url="ftp://$userpass$host$port/$dir/$file" size
[ "$use_anon" ] && url="ftp://$host$port/$dir/$file"
if ! size=$( fetch -s "$url" 2>&1 ) || ! f_isinteger "$size"
then
f_dprintf "request failed! size response=[%s]" "$size"
[ "$probe_type" = "$PROBE_SIZE" ] && echo "-1"
return $FAILURE
fi
[ "$probe_type" = "$PROBE_SIZE" ] && echo "$size"
return $SUCCESS
fi

View File

@ -510,13 +510,15 @@ f_media_init_http()
return $http_found
}
# f_media_get_http $device $file [$probe_only]
# f_media_get_http $device $file [$probe_type]
#
# Returns data from $file on an HTTP server using nc(1). Please note that
# $device is unused but must be present (even if null). Information is instead
# gathered from the environment. If $probe_only is both present and non-NULL,
# gathered from the environment. If $probe_type is both present and non-NULL,
# this function exits after receiving the HTTP header response from the server
# (if the HTTP response code is 200, success is returned; otherwise failure).
# If $probe_type is equal to $PROBE_SIZE, prints the content-length in bytes
# from the response (or -1 if not found) to standard-out.
#
# The variables used to configure the connection are as follows (all of which
# are configured by f_media_set_http above):
@ -542,10 +544,10 @@ f_media_init_http()
#
f_media_get_http()
{
local dev="$1" file="$2" probe_only="$3" hosts=
local dev="$1" file="$2" probe_type="$3" hosts=
f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_http: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
local http_host http_port
f_getvar $VAR_HTTP_HOST http_host
@ -591,7 +593,7 @@ f_media_get_http()
# this is extremely quick'n dirty
#
rv=0
rv=0 length=-1
while read LINE; do
case "$LINE" in
HTTP*)
@ -599,6 +601,12 @@ f_media_get_http()
set -- $LINE; rv=$2
f_isinteger "$rv" || rv=0
;;
"Content-Length: "*)
length="${LINE% }"
length="${length#Content-Length: }"
f_dprintf "received content-length: %s" \
"$length"
;;
*)
[ "${LINE% }" ] || break # End of headers
esac
@ -610,14 +618,17 @@ f_media_get_http()
[ $rv -ge 300 ] && exit 3
[ $rv -eq 200 ] || exit $FAILURE
if [ ! "$probe_only" ]; then
if [ ! "$probe_type" ]; then
cat # output the rest ``as-is''
elif [ "$probe_type" = "$PROBE_SIZE" ]; then
f_isinteger "$length" || length=-1
echo "$length"
fi
exit 200
)
local retval=$?
[ $retval -eq 200 ] && return $SUCCESS
[ "$probe_only" ] && return $FAILURE
[ "$probe_type" ] && return $FAILURE
case "$retval" in
5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;

View File

@ -329,14 +329,16 @@ f_media_init_http_proxy()
return $http_found
}
# f_media_get_http_proxy $device $file [$probe_only]
# f_media_get_http_proxy $device $file [$probe_type]
#
# Returns data from $file on an FTP server via HTTP proxy using nc(1). Please
# note that $device is unused but must be present (even if null). Information
# is instead gathered from the environment. If $probe_only is both present and
# is instead gathered from the environment. If $probe_type is both present and
# non-NULL, this function exits after receiving the HTTP header response from
# the proxy server (if the HTTP response code is 200, success is returned;
# otherwise failure).
# otherwise failure). If $probe_type is equal to $PROBE_SIZE, prints the
# content-length in bytes from the response (or -1 if not found) to standard-
# out.
#
# The variables used to configure the connection are as follows (all of which
# are configured by f_media_set_http_proxy above):
@ -358,10 +360,10 @@ f_media_init_http_proxy()
#
f_media_get_http_proxy()
{
local dev="$1" file="$2" probe_only="$3" hosts=
local dev="$1" file="$2" probe_type="$3" hosts=
f_dprintf "f_media_get_http_proxy: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_http_proxy: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
local proxy_host proxy_port
f_getvar $VAR_HTTP_PROXY_HOST proxy_host
@ -408,7 +410,7 @@ f_media_get_http_proxy()
# this is extremely quick'n dirty
#
rv=0
rv=0 length=-1
while read LINE; do
case "$LINE" in
HTTP*)
@ -416,6 +418,12 @@ f_media_get_http_proxy()
set -- $LINE; rv=$2
f_isinteger "$rv" || rv=0
;;
"Content-Length: "*)
length="${LINE% }"
length="${length#Content-Length: }"
f_dprintf "received content-length: %s" \
"$length"
;;
*)
[ "${LINE% }" ] || break # End of headers
esac
@ -427,14 +435,17 @@ f_media_get_http_proxy()
[ $rv -ge 300 ] && exit 3
[ $rv -eq 200 ] || exit $FAILURE
if [ ! "$probe_only" ]; then
if [ ! "$probe_type" ]; then
cat # output the rest ``as-is''
elif [ "$probe_type" = "$PROBE_SIZE" ]; then
f_isinteger "$length" || length=-1
echo "$length"
fi
exit 200
)
local retval=$?
[ $retval -eq 200 ] && return $SUCCESS
[ "$probe_only" ] && return $FAILURE
[ "$probe_type" ] && return $FAILURE
case "$retval" in
5) f_show_msg "$msg_server_error_when_requesting_url" "$url" ;;

View File

@ -208,19 +208,21 @@ f_media_init_nfs()
return $SUCCESS
}
# f_media_get_nfs $device $file [$probe_only]
# f_media_get_nfs $device $file [$probe_type]
#
# Returns data from $file on a mounted NFS device. Similar to cat(1). If
# $probe_only is present and non-NULL, returns success if $file exists.
# $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_get_nfs()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_nfs: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
}
# f_media_shutdown_nfs $device

View File

@ -152,19 +152,21 @@ f_media_init_ufs()
return $SUCCESS
}
# f_media_get_ufs $device $file [$probe_only]
# f_media_get_ufs $device $file [$probe_type]
#
# Returns data from $file on a mounted UFS partition device. Similar to cat(1).
# If $probe_only is present and non-NULL, returns success if $file exists.
# If $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_get_ufs()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_ufs: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_ufs: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
}
# f_media_shutdown_ufs $device

View File

@ -132,19 +132,21 @@ f_media_init_usb()
return $FAILURE
}
# f_media_get_usb $device $file [$probe_only]
# f_media_get_usb $device $file [$probe_type]
#
# Returns data from $file on a mounted USB disk device. Similar to cat(1).
# If $probe_only is present and non-NULL, returns success if $file exists.
# Returns data from $file on a mounted USB disk device. Similar to cat(1). If
# $probe_type is present and non-NULL, returns success if $file exists. If
# $probe_type is equal to $PROBE_SIZE, prints the size of $file in bytes to
# standard-out.
#
f_media_get_usb()
{
local dev="$1" file="$2" probe_only="$3"
local dev="$1" file="$2" probe_type="$3"
f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_only=%s" \
"$dev" "$file" "$probe_only"
f_dprintf "f_media_get_usb: dev=[%s] file=[%s] probe_type=%s" \
"$dev" "$file" "$probe_type"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_only"
f_media_generic_get "$MOUNTPOINT" "$file" "$probe_type"
}
# f_media_shutdown_usb $device

View File

@ -993,9 +993,9 @@ f_package_extract()
esac
# We have a path, call the device strategy routine to get the file
local pkg_ext probe_only=1 found=
local pkg_ext found=
for pkg_ext in "" $PACKAGE_EXTENSIONS; do
if f_device_get $device "$path$pkg_ext" $probe_only; then
if f_device_get $device "$path$pkg_ext" $PROBE_EXIST; then
path="$path$pkg_ext"
f_dprintf "%s: found path=[%s] dev=[%s]" \
$fname "$path" "$device"