fix perms on broken links, better err handling
authorIan Kelling <iank@fsf.org>
Fri, 24 Nov 2017 16:06:30 +0000 (11:06 -0500)
committerIan Kelling <iank@fsf.org>
Fri, 24 Nov 2017 16:06:30 +0000 (11:06 -0500)
lnf

diff --git a/lnf b/lnf
index 3bc51523444ee9e70fadc523e088979fc69d0f20..0620b32929d87c19ef5f79217094a9012f3df3ce 100755 (executable)
--- a/lnf
+++ b/lnf
@@ -10,20 +10,23 @@ _lnf_existing_link() {
     if [[ -L $dest_file ]]; then
         if [[ $(readlink $dest_file) == "$target" ]]; then
             # Leave the link in place, but make sure it's
-            # ownership is right.
-            # already exists. links all get 777 perms, so
-            # we dun have to mess with that.
-            if [[ $(stat -L -c%a "$dest_dir") == 2* ]]; then
-                grp=$(stat -L -c%g "$dest_dir")
+            # 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)
+                grp=$(id -g) || return $?
             fi
             if [[ $EUID == 0 && $(stat -c%u "$dest_file") != 0 ]]; then
-                chown 0:$grp "$dest_file"
+                chown -h 0:$grp "$dest_file" || return $?
             elif [[ $(stat -c%g "$dest_file") != "$grp" ]]; then
-                chgrp $grp "$dest_file"
+                chgrp -h $grp "$dest_file" || return $?
             fi
-            return 1
+            do_exit=true
+            return 0
         fi
         to_remove+=("$dest_file")
     elif [[ -e $dest_file ]]; then
@@ -54,10 +57,10 @@ links to each TARGET in DIRECTORY.
 "
 
 
-    local temp
+    local temp nodir
     local verbose=false
     local dry_run=false
-    local nodir
+    local do_exit=false
     temp=$(getopt -l help,dry-run,verbose hnTv "$@") || usage 1
     eval set -- "$temp"
     while true; do
@@ -95,7 +98,8 @@ links to each TARGET in DIRECTORY.
     if [[ $nodir ]]; then
         dest_file="$2"
         dest_dir="$(dirname "$dest_file")"
-        _lnf_existing_link "$1" "$dest_file" "$dest_dir" || return 0
+        _lnf_existing_link "$1" "$dest_file" "$dest_dir" || return $?
+        if $do_exit; then return 0; fi
         if [[ ! -d $dest_dir ]]; then
             mkdir=true
             if [[ -e $dest_dir || -L $dest_dir ]]; then
@@ -124,7 +128,8 @@ links to each TARGET in DIRECTORY.
         to_link+=("$dest_dir")
     elif  [[ $# -eq 1 ]]; then
         dest_file="${1##*/}"
-        _lnf_existing_link "$1" "$dest_file" . || return 0
+        _lnf_existing_link "$1" "$dest_file" . || return $?
+        if $do_exit; then return 0; fi
     fi
     if (( ${#to_remove[@]} >= 1 )); then
         if type -P trash-put >/dev/null; then
@@ -139,7 +144,7 @@ links to each TARGET in DIRECTORY.
             # so revert to rm -rf in that case
             if [[ $ret == 72 ]]; then
                 echo "lnf: using rm -rf to overcome cross filesystem trash-put limitation"
-                rm -rf -- "${to_remove[@]}"
+                rm -rf -- "${to_remove[@]}" || return $?
             elif [[ $ret == 74 ]]; then
                 echo "lnf: using rm -rf to overcome empty file & hardlink trash-put limitation"
                 rm -rf -- "${to_remove[@]}"