lots o fixes, beets, shellcheck stuff
[distro-setup] / check-subvol-stale
index 286f3ba8b63121a7c3c0f5d51493b383cf84ffe5..2d00cbbda084f90ccb517970df310a9bd4def376 100644 (file)
@@ -97,8 +97,10 @@ mapper-dev() {
     done
   fi
 }
-
+tmpf=$(mktemp)
+d tmpf=$tmpf
 for d; do
+
   if $subvol_path; then
     svp=$d
     root_dir=${d%/*}
@@ -142,7 +144,9 @@ for d; do
     d "svp=$svp # subvolume path"
   fi
 
-  snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
+  # Assumes we are in the 21st century.
+  ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
+  mapfile -t snaps <$tmpf
   if [[ ! ${snaps[*]} ]]; then
     # no snapshots yet
     # TODO: make this an error and override with a cli flag
@@ -150,6 +154,33 @@ for d; do
     continue
   fi
 
+  # last_snap by date.
+  last_snap="${snaps[0]}"
+  ## alternate slower alternative which would not rely on ls sorting:
+  # last_snap=$(
+  #   for s in ${snaps[@]}; do
+  #     f=${s##*/}
+  #     unix_time=$(date -d $(sed -r  's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
+  #     printf "%s %s\n" $unix_time $s # part of the pipeline
+  #     # sort will fail
+  #   done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
+  #          )
+  # if [[ ! $last_snap ]]; then
+  #   # should not happen.
+  #   echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
+  #   exit 1
+  # fi
+  d last_snap=$last_snap
+
+  if [[ ! -e $svp ]]; then
+    echo "$0: warning: subvol does not exist: $svp"
+    echo "$0 assuming this host was just for receiving and latest snap is freshest"
+    freshest_snap=$last_snap
+    stale=true
+    stale-file
+    continue
+  fi
+
   # get info on last received sub
   last_received=
   last_received_cgen=0
@@ -161,39 +192,17 @@ for d; do
       if [[ $cgen -gt $last_received_cgen ]]; then
         last_received_cgen=$cgen
         last_received=$f
+        elif [[ $last_received ]]; then
+        # optimization: we are looking in reverse order by date, so if
+        # we find one that has a lesser cgen, assume the rest will all
+        # be lesser.
+        break
       fi
     fi
   done
   d last_received_cgen=$last_received_cgen
   d last_received=$last_received
 
-  # Get last_snap by date.
-  # when a btrbk bugfix makes it into the distro,
-  # we might replace this with btrbk list latest /mnt/root/$vol | ...
-  last_snap=$(
-    for s in ${snaps[@]}; do
-      f=${s##*/}
-      unix_time=$(date -d $(sed -r  's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
-      printf "%s %s\n" $unix_time $s # part of the pipeline
-      # sort will fail
-    done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
-           )
-  if [[ ! $last_snap ]]; then
-    # should not happen.
-    echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
-    exit 1
-  fi
-  d last_snap=$last_snap
-
-  if [[ ! -e $svp ]]; then
-    echo "$0: warning: subvol does not exist: $svp"
-    echo "$0 assuming this host was just for receiving and latest snap is freshest"
-    freshest_snap=$last_snap
-    stale=true
-    stale-file
-    continue
-  fi
-
 
   # if there is a last_received, we can assume stale or fresh if we are newer/older
   if [[ $last_received ]]; then
@@ -215,7 +224,7 @@ for d; do
   stale=true
   # fresh if $svp has $last_snap as a snapshot,
   if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
-      grep -xF ${last_snap#$root_dir/} >/dev/null; then
+      grep -xF ${last_snap#"$root_dir"/} >/dev/null; then
     stale=false
   else # or else $svp is a snapshot of $last_snap. we use a uuid
     # comparison, which if I remember from the docs, is a bit more
@@ -228,3 +237,4 @@ for d; do
 
   stale-file
 done
+rm $tmpf