Beta stage: traverses through /**/* without error (some files excluded; see test/totest.sh)
This commit is contained in:
84
rdlink.sh
84
rdlink.sh
@@ -18,14 +18,45 @@ LOG_LEVEL="${LOG_LEVEL:-1}" # 7 = debug -> 0 = emergency
|
|||||||
|
|
||||||
rl::rdlink() {
|
rl::rdlink() {
|
||||||
local subject=
|
local subject=
|
||||||
|
local work=
|
||||||
|
local resolved=0
|
||||||
|
|
||||||
info "pwd: $(pwd)"
|
info "pwd: $(pwd)"
|
||||||
info "Processing... $@"
|
info "Processing... $@"
|
||||||
|
|
||||||
subject="$(rl::normalize "${1:-}")" || true
|
subject="$(rl::normalize "${1:-}")" || true
|
||||||
# Follow multiple symlinks
|
# Follow multiple symlinks
|
||||||
while subject="$(rl::quicklink "${subject}")" ; do : ; done
|
while subject="$(rl::quicklink "${subject}")" ; do
|
||||||
subject="$(rl::canon "${subject}")" || true
|
# A link was resolved at least once
|
||||||
|
info "rl::rdlink - Link found: $subject"
|
||||||
|
resolved=1
|
||||||
|
done
|
||||||
|
|
||||||
|
# Special cases handling
|
||||||
|
{
|
||||||
|
# If subject is still a link, after rl::quicklink call(s)
|
||||||
|
# current user has no permission to access the link itself
|
||||||
|
# (e.g. /proc/**/cwd)
|
||||||
|
if [ -L "${subject}" ] ; then
|
||||||
|
info "rl::rdlink exit - can't access link ${subject}"
|
||||||
|
printf "\n"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Empty output if (dirname $subject) is not a valid path
|
||||||
|
if ! work="$(rl::canon "${subject}")" ; then
|
||||||
|
# Special: Links resolved to something like: /proc/2267178/fd/pipe:[22306727]
|
||||||
|
# are not existing files but `readlink -f` prints result anyway
|
||||||
|
# Printing result if a link was resolved at least once
|
||||||
|
if (( ! resolved )); then
|
||||||
|
info "rl::rdlink exit - invalid path ${subject}"
|
||||||
|
printf "\n"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
subject="${work}"
|
||||||
|
fi
|
||||||
|
|
||||||
printf "${subject}\n"
|
printf "${subject}\n"
|
||||||
}
|
}
|
||||||
@@ -43,13 +74,13 @@ rl::quicklink() {
|
|||||||
fi
|
fi
|
||||||
info " - symlink ${1} -> ${subject}"
|
info " - symlink ${1} -> ${subject}"
|
||||||
|
|
||||||
# relative target; append root
|
# relative symlink target; prepend its parent direcotry
|
||||||
if [[ "${subject}" != "/"* ]]; then
|
if [[ "${subject}" != "/"* ]]; then
|
||||||
work="$(rl::canon "$(dirname -- "${1:-}")")"
|
work="$(rl::canon "$(dirname -- "${1:-}")")"
|
||||||
subject="${work}/${subject}"
|
subject="${work}/${subject}"
|
||||||
info " - relative link resolved: ${subject}"
|
info " - relative link resolved: ${subject}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "${subject}\n"
|
printf "${subject}\n"
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -61,23 +92,40 @@ rl::canon() {
|
|||||||
|
|
||||||
info "Canonicalize path... ${1:-}"
|
info "Canonicalize path... ${1:-}"
|
||||||
if work="$(cd "${1:-}" >/dev/null 2>&1 && pwd -P)" ; then
|
if work="$(cd "${1:-}" >/dev/null 2>&1 && pwd -P)" ; then
|
||||||
subject="${work}"
|
# Special: `pwd -P` returns with // as root for links starting at /
|
||||||
|
# e.g. $(readlink /proc/self/root) == "/"
|
||||||
|
# $(cd /proc/self/root/mnt && pwd -P) == "//mnt"
|
||||||
|
subject="$(rl::normalize "${work}")"
|
||||||
info " - directory: ${subject}"
|
info " - directory: ${subject}"
|
||||||
elif work="$(cd "$(dirname -- "${1:-}")" >/dev/null 2>&1 && pwd -P)" ; then
|
elif work="$(cd "$(dirname -- "${1:-}")" >/dev/null 2>&1 && pwd -P)" ; then
|
||||||
bname="$(basename -- "${1:-}")"
|
bname="$(basename -- "${1:-}")"
|
||||||
|
|
||||||
|
# Special: / produces //
|
||||||
|
[[ "${work}" == "/" ]] && work=
|
||||||
|
|
||||||
subject="${work}${bname:+"/${bname}"}"
|
subject="${work}${bname:+"/${bname}"}"
|
||||||
info " - parent: ${subject}"
|
info " - parent: ${subject}"
|
||||||
else
|
else
|
||||||
info " - no hit"
|
info " - no hit for: ${1:-}"
|
||||||
subject="${1:-}"
|
subject="${1:-}"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "${subject}\n"
|
printf -- "${subject}\n"
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::normalize() {
|
rl::normalize() {
|
||||||
|
local work=
|
||||||
info "Normalizing... ${1:-}"
|
info "Normalizing... ${1:-}"
|
||||||
printf -- "${1:-}\n"
|
|
||||||
|
work="${1:-}"
|
||||||
|
# Remove multiple /
|
||||||
|
while [[ "${work:-}" = *"//"* ]]; do
|
||||||
|
work="${work//'//'/'/'}"
|
||||||
|
done
|
||||||
|
|
||||||
|
printf -- "${work}\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::main() {
|
rl::main() {
|
||||||
@@ -118,20 +166,20 @@ function rl::log () {
|
|||||||
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
||||||
done <<< "${@:-}"
|
done <<< "${@:-}"
|
||||||
}
|
}
|
||||||
emergency () { rl::log emergency "${@}"; exit 1; }
|
emergency() { rl::log emergency "${@}"; exit 1; }
|
||||||
alarm() { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && rl::log alert "${@}"; true; }
|
alarm() { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && rl::log alert "${@}"; true; }
|
||||||
critical () { [[ "${LOG_LEVEL:-0}" -ge 2 ]] && rl::log critical "${@}"; true; }
|
critical() { [[ "${LOG_LEVEL:-0}" -ge 2 ]] && rl::log critical "${@}"; true; }
|
||||||
error () { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && rl::log error "${@}"; true; }
|
error() { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && rl::log error "${@}"; true; }
|
||||||
warning () { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && rl::log warning "${@}"; true; }
|
warning() { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && rl::log warning "${@}"; true; }
|
||||||
notice () { [[ "${LOG_LEVEL:-0}" -ge 5 ]] && rl::log notice "${@}"; true; }
|
notice() { [[ "${LOG_LEVEL:-0}" -ge 5 ]] && rl::log notice "${@}"; true; }
|
||||||
info () { [[ "${LOG_LEVEL:-0}" -ge 6 ]] && rl::log info "${@}"; true; }
|
info() { [[ "${LOG_LEVEL:-0}" -ge 6 ]] && rl::log info "${@}"; true; }
|
||||||
debug () { [[ "${LOG_LEVEL:-0}" -ge 7 ]] && rl::log debug "${@}"; true; }
|
debug() { [[ "${LOG_LEVEL:-0}" -ge 7 ]] && rl::log debug "${@}"; true; }
|
||||||
msg () { echo "$@"; true; }
|
msg() { echo "$@"; true; }
|
||||||
|
|
||||||
|
|
||||||
# Provide as function to be called when sourced
|
# Provide as function to be called when sourced
|
||||||
rdlink() {
|
rdlink() {
|
||||||
rl::main "$@"
|
rl::rdlink "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
### Check if script is _sourced
|
### Check if script is _sourced
|
||||||
|
Reference in New Issue
Block a user