Introduce function for work with cached data

A cache for changes used in the alterator-net-* modules often enough,
so add some common function to alterator-net-functions.
This commit is contained in:
Mikhail Efremov 2016-03-17 17:58:12 +03:00
parent 848537975d
commit 17a65794b0

View File

@ -6,6 +6,7 @@ etcnet_iface_dir=/etc/net/ifaces
etcnet_default_iface_dir="$etcnet_iface_dir/default"
resolvconf_rdelim='[[:space:]]\+'
resolvconf_wdelim=' '
__iface_removed_tag=".REMOVED"
. alterator-hw-functions
. shell-config
@ -710,6 +711,121 @@ iface_down()
return 1
}
### cache functions
iface_will_removed()
{
local cachedir="$1"; shift
local ifname="$1"; shift
[ -n "$cachedir" -a -n "$ifname" -a -f "$cachedir/$ifname/$__iface_removed_tag" ]
}
# read stdin
__filter_out_removed_ifaces()
{
local cachedir="$1"; shift
local ifname=
while read ifname; do
iface_will_removed "$cachedir" "$ifname" || echo "$ifname"
done
}
ifacedir_with_cache()
{
local cachedir="$1"; shift
local name="$1"; shift
if [ -n "$cachedir" -a -n "$name" -a -d "$cachedir/$name" ];then
echo "$cachedir/$name"
else
echo "$etcnet_iface_dir/$name"
fi
}
next_iface_with_cache()
{
local cachedir="$1"; shift
local name="$1";shift
local i="${1:-0}"
local path1="$etcnet_iface_dir/$name"
local path2="$cachedir/$name"
while true; do
if [ ! -d "$path1$i" -a ! -d "$path2$i" ] || iface_will_removed "$cachedir" "$name"; then
echo "$name$i"
break
fi
i=$(($i + 1))
done
}
iface_has_host_with_cache()
{
local cachedir="$1"; shift
local name="$1"; shift
local i= exclude=
if [ -d "$cachedir" ]; then
if iface_has_host "$name" "$cachedir"; then
return 0
fi
# don't check in the /etc/net/ifaces ifaces which were cached
for i in $(find "$cachedir" -mindepth 1 -maxdepth 1 -type d 2>/dev/null); do
exclude="$exclude${exclude:+ -o }-path $etcnet_iface_dir/${i##*/}/options"
done
if [ -n "$exclude" ]; then
exclude="\( \( $exclude \) -prune -o -print \)"
fi
elif [ "$(read_iface_option "$etcnet_iface_dir/$name" TYPE)" = "vlan" ]; then
return 0
fi
for i in $(eval find "$etcnet_iface_dir" -maxdepth 2 -mindepth 2 -name options $exclude 2>/dev/null); do
if [ "$(read_iface_option "${i%/options}" TYPE)" = "vlan" ]; then
continue
fi
if egrep -qs "^HOST=[\"']?([[:alnum:]]+[[:blank:]])*$name([[:blank:]][[:alnum:]]+)*[\"']?[[:blank:]]*$" $i; then
return 0
fi
done
return 1
}
list_ppp_with_cache()
{
local cachedir="$1"; shift
local t="${1:-}"
{
[ -d "$cachedir" ] && list_ppp "$t" "$cachedir"
list_ppp "$t" "$etcnet_iface_dir"
} | sort -u | __filter_out_removed_ifaces "$cachedir"
}
list_bond_with_cache()
{
local cachedir="$1"; shift
{
[ -d "$cachedir" ] && list_bond "$cachedir"
list_bond "$etcnet_iface_dir"
} | sort -u | __filter_out_removed_ifaces "$cachedir"
}
# list_static_iface [ ip_version ]
list_static_iface_with_cache()
{
local cachedir="$1"; shift
local ipv="${1:-4}"
{
[ -d "$cachedir" ] && list_static_iface "$ipv" "$cachedir"
list_static_iface "$ipv" "$etcnet_iface_dir"
} | sort -u | __filter_out_removed_ifaces "$cachedir"
}
### ipv4address calculations
# For backward compatibility only.
# In new code use functions from shell-ip-address directly.