+_lnf_existing_link() {
+ local target dest_file dest_dir
+ target="$1"
+ dest_file="$2"
+ dest_dir="$3"
+ if [[ -L $dest_file ]]; then
+ if [[ $(readlink $dest_file) == "$target" ]]; then
+ # Leave the link in place, but make sure it's
+ # owner & group is as if we created it.
+ # links all get 777 perms, so
+ # we already know that is right.
+
+ # test for setgid.
+ if [[ $(stat -L -c%a "$dest_dir") == 2??? ]]; then
+ grp=$(stat -L -c%g "$dest_dir") || return $?
+ else
+ grp=$(id -g) || return $?
+ fi
+ if [[ $EUID == 0 && $(stat -c%u "$dest_file") != 0 ]]; then
+ chown -h 0:$grp "$dest_file" || return $?
+ elif [[ $(stat -c%g "$dest_file") != "$grp" ]]; then
+ chgrp -h $grp "$dest_file" || return $?
+ fi
+ do_exit=true
+ return 0
+ fi
+ to_remove+=("$dest_file")
+ elif [[ -e $dest_file ]]; then
+ to_remove+=("$dest_file")
+ fi
+ to_link+=("$target")
+}