Skip to content

Commit

Permalink
Add rm wrapper __antidote_del
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmc3 committed Jul 18, 2024
1 parent 3b2de5a commit 6cf8d24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
32 changes: 32 additions & 0 deletions functions/__antidote_del
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/zsh
# Call me paranoid, but I want to be really certain antidote will never rm something it
# shouldn't. This function wraps rm to double check that any paths being removed are
# valid. If it's not in your $HOME or $TMPDIR, we need to block it.

#function __antidote_del {
emulate -L zsh; setopt local_options

local -a rmflags rmpaths
local p

while (( $# )); do
case "$1" in
--) shift; break ;;
-*) rmflags+=($1) ;;
*) break ;;
esac
shift
done

(( $# > 0 )) || return 1

for p in $@; do
p="${p:a}"
if [[ "$p" != $HOME/* ]] && [[ "$p" != ${TMPDIR:-/tmp}/* ]]; then
print -ru2 -- "antidote: Blocked attempt to rm path: '$p'."
return 1
fi
done

command rm ${rmflags[@]} -- "$@"
#}
2 changes: 1 addition & 1 deletion functions/__antidote_load_prep
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
if [[ ! $staticfile -nt $bundlefile ]] || [[ $force_bundle -eq 1 ]]; then
antidote bundle <"$bundlefile" >|"$staticfile"
if [[ -r "${staticfile}.zwc" ]] && ! zstyle -t ':antidote:static' zcompile; then
command rm -f -- "${staticfile}.zwc"
__antidote_del -f -- "${staticfile}.zwc"
fi
fi

Expand Down
6 changes: 3 additions & 3 deletions functions/antidote-purge
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
[[ ${REPLY:u} == "Y" ]] || return 1
# remove antidote home and static cache file
command rm -rf -- "$antidote_home"
__antidote_del -rf -- "$antidote_home"

if [[ -e "${bundlefile:r}.zsh" ]]; then
zstyle -s ':antidote:purge:all' answer 'REPLY' || {
Expand Down Expand Up @@ -68,14 +68,14 @@
fi

# remove
command rm -rf "$bundledir"
__antidote_del -rf "$bundledir"
print "Removed '$bundle'."

# attempt to comment out the bundle from .zsh_plugins.txt
if [[ -e "$bundlefile" ]]; then
local tmpfile="${bundlefile}.antidote.tmp"
$__adote_awkcmd -v pat="$bundle" '$0~"^[[:blank:]]*"pat{print "# " $0;next}1' <$bundlefile >|$tmpfile
command mv -f "$tmpfile" "$bundlefile" || command rm -f "$tmpfile"
command mv -f "$tmpfile" "$bundlefile" || __antidote_del -f "$tmpfile"
print "Bundle '$bundle' was commented out in '$bundlefile'."
fi
fi
Expand Down
4 changes: 2 additions & 2 deletions functions/antidote-update
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
local bundledir url repo

# remove zcompiled files
command rm -rf -- $(antidote-home)/**/*.zwc(N)
__antidote_del -rf -- $(antidote-home)/**/*.zwc(N)

# remove check file
local loadable_check_path="$(antidote-home)/.antidote.load"
[[ -r "$loadable_check_path" ]] && command rm -- "$loadable_check_path"
[[ -r "$loadable_check_path" ]] && __antidote_del -- "$loadable_check_path"

# update all bundles
for bundledir in $(antidote-list --dirs); do
Expand Down
10 changes: 10 additions & 0 deletions tests/test_helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
%
```

## Safe removal

Appease my paranoia and ensure that you can't remove a path you shouldn't be able to:

```zsh
% __antidote_del -rf -- /foo/bar
antidote: Blocked attempt to rm path: '/foo/bar'.
%
```

## Bundle type

```zsh
Expand Down

0 comments on commit 6cf8d24

Please sign in to comment.