sane-which: follow nix wrappers

This commit is contained in:
Colin 2024-02-28 18:07:09 +00:00
parent 40e30cf2f8
commit b515127101
2 changed files with 37 additions and 15 deletions

View File

@ -196,7 +196,7 @@ let
which = static-nix-shell.mkBash {
pname = "sane-which";
srcRoot = ./src;
pkgs = [ "coreutils-full" "file" ];
pkgs = [ "coreutils-full" "file" "gnugrep" ];
};
wipe = static-nix-shell.mkBash {
pname = "sane-wipe";

View File

@ -1,25 +1,47 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p coreutils-full -p file
#!nix-shell -i bash -p coreutils-full -p file -p gnugrep
# traces a PATH lookup by printing the source, resolution, and any symlinks traversed
# finally, prints the content of the file
echo $1
v=$(which $1)
# this probably doesn't handle paths with spaces
while [ "$(readlink $v || echo $v)" != "$v" ]
do
# TODO: this doesn't handle relative symlinks
echo '->' "$v"
v=$(readlink "$v")
cur="$1"
next="$(which "$cur")"
getSymlinked() {
# test if $cur is a symlink
# TODO: handle relative symlinks too!
local next_
next_="$(readlink "$cur")"
[ "$?" -eq 0 ] && echo "$next_"
}
getWrapped() {
# test if $cur is a wrapper around a patch matching $1 template
local dir="$(dirname "$cur")"
local base="$(basename "$cur")"
local wrapped="$(printf "$1" "$dir" "$base")"
[ -e "$wrapped" ] && grep -q "$wrapped" "$cur" && echo "$wrapped"
}
getNext() {
getSymlinked \
|| getWrapped "%s/.sandboxed/%s" \
|| getWrapped "%s/.%s-wrapped"
}
echo "$cur"
while [ -n "$next" ]; do
cur="$next"
echo "-> $cur"
next="$(getNext "$cur")"
done
echo
echo '->' "$v"
echo ''
case $(file --brief --mime "$v") in
case $(file --brief --mime "$cur") in
(*text*)
cat "$v"
cat "$cur"
;;
(*)
echo $(file "$v")
echo $(file "$cur")
;;
esac