rename err to bash-bear
[distro-setup] / check-subvol-stale
index 2d00cbbda084f90ccb517970df310a9bd4def376..5ae7cb6b52996305e2eed53aa731777eff455d66 100644 (file)
@@ -16,7 +16,7 @@
 
 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
 
-source /usr/local/lib/err
+source /usr/local/lib/bash-bear
 
 shopt -s nullglob
 
@@ -77,9 +77,11 @@ stale-file() {
   fi
 
 }
+pre="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}"
+
 d() {
   if $verbose; then
-    printf "%s\n" "$*"
+    printf "$pre %s\n" "$*"
   fi
 }
 
@@ -144,10 +146,13 @@ for d; do
     d "svp=$svp # subvolume path"
   fi
 
-  # Assumes we are in the 21st century.
-  ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
-  mapfile -t snaps <$tmpf
-  if [[ ! ${snaps[*]} ]]; then
+  # note: relying on null glob
+  ls_args=($root_dir/btrbk/$subvol_dir.20*)
+  if (( ${#ls_args[@]} )); then
+    # Assumes we are in the 21st century.
+    ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
+    mapfile -t snaps <$tmpf
+  else
     # no snapshots yet
     # TODO: make this an error and override with a cli flag
     echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
@@ -156,6 +161,16 @@ for d; do
 
   # last_snap by date.
   last_snap="${snaps[0]}"
+
+  case $last_snap in
+    $root_dir/btrbk/$subvol_dir.20*) : ;;
+    *)
+      echo "$0: error: unexpected last_snap:$last_snap"
+      exit 1
+      ;;
+  esac
+
+  d last_snap=$last_snap
   ## alternate slower alternative which would not rely on ls sorting:
   # last_snap=$(
   #   for s in ${snaps[@]}; do
@@ -170,7 +185,6 @@ for d; do
   #   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"
@@ -192,7 +206,7 @@ for d; do
       if [[ $cgen -gt $last_received_cgen ]]; then
         last_received_cgen=$cgen
         last_received=$f
-        elif [[ $last_received ]]; then
+      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.