Add some more special handlings

Smaller updates of debug output
This commit is contained in:
2022-03-21 22:41:54 +01:00
parent 621da5e4c8
commit 333e226871

View File

@@ -21,8 +21,8 @@ rl::rdlink() {
local work= local work=
local resolved=0 local resolved=0
info "rd::link Processing... $@" info "Processing: $@"
info " pwd: $(pwd)" info " with pwd: $(pwd)"
subject="$(rl::cleanpath "${1:-}")" || true subject="$(rl::cleanpath "${1:-}")" || true
@@ -51,7 +51,7 @@ rl::rdlink() {
# are not existing files but `readlink -f` prints result anyway # are not existing files but `readlink -f` prints result anyway
# Printing result if a link was resolved at least once # Printing result if a link was resolved at least once
#if (( ! resolved )); then #if (( ! resolved )); then
info "rl::rdlink exit - invalid path ${subject}" info "rl::rdlink exit - invalid path ${work}"
printf "\n" printf "\n"
return 1 return 1
#fi #fi
@@ -90,37 +90,65 @@ rl::canon() {
local subject= local subject=
local work= local work=
local bname= local bname=
local run=1
local start=
local retval=0
info "Canonicalize path... ${1:-}" start="${1:-}"
if work="$(cd "${1:-}" >/dev/null 2>&1 && pwd -P)" ; then info "Canonicalize path... ${start}"
while (( run )) ; do
if work="$(cd "${start}" >/dev/null 2>&1 && pwd -P)" ; then
# Special: `pwd -P` returns with // as root for links starting at / # Special: `pwd -P` returns with // as root for links starting at /
# e.g. $(readlink /proc/self/root) == "/" # e.g. $(readlink /proc/self/root) == "/"
# $(cd /proc/self/root/mnt && pwd -P) == "//mnt" # $(cd /proc/self/root/mnt && pwd -P) == "//mnt"
subject="$(rl::normalize "${work}")" subject="$(rl::normalize "${work}")"
info " - directory: ${subject}" info "rl::canon valid directory: ${subject}"
elif work="$(cd "$(dirname -- "${1:-}")" >/dev/null 2>&1 && pwd -P)" ; then run=0
bname="$(basename -- "${1:-}")" elif work="$(cd "$(dirname -- "${start}")" >/dev/null 2>&1 && pwd -P)" ; then
bname="$(basename -- "${start}")"
# Special: / produces // # Special: / produces //
[[ "${work}" == "/" ]] && work= [[ "${work}" == "/" ]] && work=
subject="${work}${bname:+"/${bname}"}" subject="${work}${bname:+"/${bname}"}"
info " - parent: ${subject}" info "rl::canon valid parent: ${subject}"
else # Special: Succeed with valid element after second run; see special below
info " - no hit for: ${1:-}" # e.g. /root/.
subject="${1:-}" # * /root is valid but not accessible
return 1 if (( retval )) && [ -e "${subject}" ] ;then
info "rl::canon valid element"
retval=0
fi fi
run=0
else
# Special: Some paths may be resolvable after normalization
# e.g. somedir/..
# * base "somedir" does not exist
# * but irrelevant because of /..
# * resolves to pwd but fails by readlink -f
work="$(rl::normalize "${start}")"
if [[ "${work}" != "${start}" ]] ; then
info "rl::canon retry with: ${work}"
start="${work}"
retval=1
continue
fi
info "rl::canon invalid path: ${work}"
subject="${work}"
run=0 && retval=1
fi
done
printf -- "${subject}\n" printf -- "${subject}\n"
return 0 return ${retval}
} }
rl::cleanpath() { rl::cleanpath() {
local work= local work=
local rex_tmp= local rex_tmp=
info "rl::cleanpath...${1:-}" info "Cleaning path... ${1:-}"
work="${1:-}" work="${1:-}"
# Remove multiple / # Remove multiple /
@@ -135,7 +163,7 @@ rl::cleanpath() {
rl::normalize() { rl::normalize() {
local work= local work=
info "rl::normalize...${1:-}" info "Normalizing path... ${1:-}"
work="${1:-}" work="${1:-}"
# Remove dir/.. sequences. # Remove dir/.. sequences.
@@ -144,7 +172,7 @@ rl::normalize() {
work="${work/"${BASH_REMATCH[0]}"/}" work="${work/"${BASH_REMATCH[0]}"/}"
done done
# Remove /./ or /.$ sequences. # Remove /./ and /.$ sequences.
rex_tmp='/\.(/|$)' rex_tmp='/\.(/|$)'
while [[ "$work" =~ $rex_tmp ]]; do while [[ "$work" =~ $rex_tmp ]]; do
work="${work/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]/$//}"}" work="${work/"${BASH_REMATCH[0]}"/"${BASH_REMATCH[1]/$//}"}"