diff options
author | Mat M <matm@gmx.fr> | 2019-12-08 00:25:53 +0100 |
---|---|---|
committer | Mike Burns <mburns@thoughtbot.com> | 2020-01-17 16:46:06 -0500 |
commit | f2fb351c391dca7c188a8623e71519619c2ce9a0 (patch) | |
tree | 28efb32af5fa87d62807131e85dbe1529ef23c4a /bin | |
parent | fb8045f1cf3c8a76cba700d4e0507992be738f89 (diff) |
Do not symlink a symlink in mkrc
We have a bug when calling mkrc(1) on a symlink:
```sh
mkrc ~/.vimrc # links ~/.vimrc to ~/.dotfiles/vimrc
mkrc ~/.vimrc # deletes ~/.dotfiles/vimrc
```
This catches that case ahead of time, preventing the user from running
mkrc(1) on a symlink.
Fix #144.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/mkrc.in | 23 | ||||
-rwxr-xr-x | bin/rcup.in | 4 |
2 files changed, 23 insertions, 4 deletions
diff --git a/bin/mkrc.in b/bin/mkrc.in index 5d6af7e..d246c33 100755 --- a/bin/mkrc.in +++ b/bin/mkrc.in @@ -20,6 +20,28 @@ destination() { fi } +exit_if_dangerous() { + local file="$1" + + if [ -L "$file" ]; then + $ERROR 1 "'$file' is a symlink. Cannot process file." + elif is_nested "$file"; then + # Remove DEST_DIR in case $HOME is under a symlink + saved_ifs="$IFS" + IFS=/ + set -- $(dirname "$file" | sed "s|$DEST_DIR/||") + IFS="$saved_ifs" + + built_dir="$DEST_DIR" + for dir in $@; do + built_dir="$built_dir/$dir" + if [ -L "$built_dir" ]; then + $ERROR 1 "'$file' path contains a symlink ($dir). Cannot process file." + fi + done + fi +} + show_help() { local exit_code=${1:-0} @@ -84,6 +106,7 @@ fi files="" for i; do + exit_if_dangerous "$i" files="$(printf "$files\n$i")" done diff --git a/bin/rcup.in b/bin/rcup.in index 910e534..78f5faa 100755 --- a/bin/rcup.in +++ b/bin/rcup.in @@ -121,10 +121,6 @@ replace_file() { link_file "$src" "$dest" "$sigil" } -is_nested() { - echo "$1" | sed "s:$DEST_DIR/::" | grep '/' >/dev/null -} - is_identical() { diff -c "$1" "$2" > /dev/null 2>&1 } |