Browse Source

sync: improve internal symlink handling

Signed-off-by: Alejandro Mery <amery@geeks.cl>
master
Alejandro Mery 4 years ago
parent
commit
e5d0f7ea75
1 changed files with 53 additions and 24 deletions
  1. +53
    -24
      sync2.sh

+ 53
- 24
sync2.sh

@ -3,35 +3,64 @@
info() { echo "$$: $*"; }
err() { echo "E: $$: $*" >&2; }
set -u
FILES="$(pwd -P)"
for x; do
if [ -d "$x" ]; then
true # skip dirs
elif [ -L "$HOME/$x" ]; then
d0="$(readlink "$HOME/$x")"
d1="$(echo "$FILES/$x" | sed -e 's,/\./,/,g')"
d1="$(readlink -f "$d1")"
if [ "$d0" != "$d1" ]; then
err "$x: relinking, was [$d0]"
ln -snf "$d1" "$HOME/$x"
fi
# clean
#
x="${x#./}"
# find target (d1)
#
if [ -L "$FILES/$x" ]; then
d0="$(readlink "$FILES/$x")"
d1="$(readlink -f "$FILES/$x")"
[ "$d1" = "${d1#$FILES/}" ] || d1="$d0"
elif [ ! -d "$FILES/$x" ]; then
d1="$FILES/$x"
else
d="$(dirname "$x")"
if [ ! -d "$HOME/$d/" ]; then
info "$d: creating dir"
mkdir -p "$HOME/$d/"
fi
continue
fi
d="$(echo "$FILES/$x" | sed -e 's,/\./,/,g')"
if [ -e "$HOME/$x" ]; then
info "$x: importing changes and linking"
rm "$d"
cp -a "$HOME/$x" "$d"
else
info "$x: linking"
d="$(readlink -f "$d")"
# link dir
#
d="${x%/*}"
if [ "$d" = "$x" ]; then
d=
elif [ ! -d "$HOME/$d/" ]; then
info "$d: creating dir"
mkdir -p "$HOME/$d/"
fi
# link
#
if [ -L "$HOME/$x" ]; then
# preexisting link ($d1)
d0="$(readlink "$HOME/$x")"
[ "$d0" != "$d1" ] || continue
err "$x: relinking, was [$d0]"
elif [ ! -e "$HOME/$x" ]; then
# new
info "$x: linking"
elif [ -d "$HOME/$x" ]; then
# preexisting dir
err "$x: importing directory and linking [$d1]"
if ! rmdir "$HOME/$x" 2> /dev/null; then
rm "$FILES/$x"
mv "$HOME/$x" "$FILES/$x"
fi
ln -snf "$d" "$HOME/$x"
else
# preexisting file
info "$x: importing changes and linking [$d1]"
rm "$FILES/$x"
cp -a "$HOME/$x" "$FILES/$x"
fi
ln -snf "$d1" "$HOME/$x"
done

Loading…
Cancel
Save