Showing 1 changed files with 29 additions and 19 deletions
+29 -19
garmin_varia_transcode.sh
@@ -45,7 +45,9 @@ STAGING_PROVIDED=false
45 45
 STAGING_RAMDISK_MB="$DEFAULT_STAGING_RAMDISK_MB"
46 46
 AUTO_CREATED_STAGING_RAMDISK=false
47 47
 AUTO_CREATED_STAGING_PATH=""
48
+AUTO_CREATED_STAGING_DEV=""
48 49
 STAGING_RAMDISK_CREATED_AT=0
50
+AUTO_STAGING_CLEANED_UP=false
49 51
 RUN_STARTED_AT=0
50 52
 FIRST_ENCODE_STARTED_AT=0
51 53
 DEBUG_TIMING_LIMIT=0
@@ -1078,6 +1080,9 @@ normalize_dest_dir() {
1078 1080
   DEST_DIR="$(to_abs_path "$DEST_DIR")"
1079 1081
 }
1080 1082
 
1083
+# Sets globals: AUTO_CREATED_STAGING_RAMDISK, AUTO_CREATED_STAGING_PATH,
1084
+# AUTO_CREATED_STAGING_DEV, STAGING_RAMDISK_CREATED_AT
1085
+# Must be called directly (NOT inside $(...)) to preserve global assignments.
1081 1086
 create_missing_staging_ramdisk_if_needed() {
1082 1087
   local staging_path="$1"
1083 1088
   local ramdisk_name=""
@@ -1088,7 +1093,7 @@ create_missing_staging_ramdisk_if_needed() {
1088 1093
   staging_path="${staging_path%/}"
1089 1094
 
1090 1095
   if [[ -d "$staging_path" ]]; then
1091
-    printf '%s\n' "$staging_path"
1096
+    # Already exists; nothing to create.
1092 1097
     return
1093 1098
   fi
1094 1099
 
@@ -1130,20 +1135,15 @@ create_missing_staging_ramdisk_if_needed() {
1130 1135
   fi
1131 1136
 
1132 1137
   mount_point="$(/usr/sbin/diskutil info "$dev" 2>/dev/null | /usr/bin/awk -F': *' '/Mount Point/ {print $2; exit}')"
1133
-  if [[ -n "$mount_point" && -d "$mount_point" ]]; then
1134
-    AUTO_CREATED_STAGING_RAMDISK=true
1135
-    AUTO_CREATED_STAGING_PATH="$mount_point"
1136
-    STAGING_RAMDISK_CREATED_AT="$(date +%s)"
1137
-    printf '%s\n' "$mount_point"
1138
-    return
1138
+  if [[ -z "$mount_point" ]]; then
1139
+    mount_point="$staging_path"
1139 1140
   fi
1140 1141
 
1141
-  if [[ -d "$staging_path" ]]; then
1142
+  if [[ -d "$mount_point" ]]; then
1142 1143
     AUTO_CREATED_STAGING_RAMDISK=true
1143
-    AUTO_CREATED_STAGING_PATH="$staging_path"
1144
+    AUTO_CREATED_STAGING_PATH="$mount_point"
1145
+    AUTO_CREATED_STAGING_DEV="$dev"
1144 1146
     STAGING_RAMDISK_CREATED_AT="$(date +%s)"
1145
-    printf '%s\n' "$staging_path"
1146
-    return
1147 1147
   fi
1148 1148
 }
1149 1149
 
@@ -1152,11 +1152,17 @@ attempt_unmount_auto_staging_ramdisk() {
1152 1152
     return
1153 1153
   fi
1154 1154
 
1155
+  if [[ "$AUTO_STAGING_CLEANED_UP" == true ]]; then
1156
+    return
1157
+  fi
1158
+  AUTO_STAGING_CLEANED_UP=true
1159
+
1155 1160
   if [[ "$(uname -s)" != "Darwin" ]]; then
1156 1161
     return
1157 1162
   fi
1158 1163
 
1159
-  if /usr/sbin/diskutil eject "$AUTO_CREATED_STAGING_PATH" >/dev/null 2>&1; then
1164
+  local target="${AUTO_CREATED_STAGING_DEV:-$AUTO_CREATED_STAGING_PATH}"
1165
+  if /usr/bin/hdiutil detach "$target" >/dev/null 2>&1; then
1160 1166
     log_msg "INFO" "Auto-created staging RAM disk unmounted: $AUTO_CREATED_STAGING_PATH"
1161 1167
     return
1162 1168
   fi
@@ -1164,10 +1170,16 @@ attempt_unmount_auto_staging_ramdisk() {
1164 1170
   if [[ -d "$AUTO_CREATED_STAGING_PATH" ]]; then
1165 1171
     log_msg "WARN" "Could not unmount auto-created staging RAM disk; it remains mounted: $AUTO_CREATED_STAGING_PATH"
1166 1172
   else
1167
-    log_msg "WARN" "Could not confirm unmount status for auto-created staging RAM disk: $AUTO_CREATED_STAGING_PATH"
1173
+    log_msg "INFO" "Auto-created staging RAM disk already gone: $AUTO_CREATED_STAGING_PATH"
1168 1174
   fi
1169 1175
 }
1170 1176
 
1177
+cleanup_on_exit() {
1178
+  local rc=$?
1179
+  attempt_unmount_auto_staging_ramdisk
1180
+  return "$rc"
1181
+}
1182
+
1171 1183
 normalize_staging_dir() {
1172 1184
   if [[ "$STAGING_PROVIDED" != true ]]; then
1173 1185
     STAGING_DIR=""
@@ -1176,10 +1188,9 @@ normalize_staging_dir() {
1176 1188
 
1177 1189
   STAGING_DIR="$(to_abs_path "$STAGING_DIR")"
1178 1190
   if [[ ! -d "$STAGING_DIR" ]]; then
1179
-    local created_staging_dir=""
1180
-    created_staging_dir="$(create_missing_staging_ramdisk_if_needed "$STAGING_DIR")"
1181
-    if [[ -n "$created_staging_dir" && -d "$created_staging_dir" ]]; then
1182
-      STAGING_DIR="$created_staging_dir"
1191
+    create_missing_staging_ramdisk_if_needed "$STAGING_DIR"
1192
+    if [[ "$AUTO_CREATED_STAGING_RAMDISK" == true && -d "$AUTO_CREATED_STAGING_PATH" ]]; then
1193
+      STAGING_DIR="$AUTO_CREATED_STAGING_PATH"
1183 1194
       log_msg "INFO" "Created staging RAM disk: $STAGING_DIR"
1184 1195
     else
1185 1196
       die "Staging directory not found: $STAGING_DIR"
@@ -1575,6 +1586,7 @@ main() {
1575 1586
   RUN_STARTED_AT="$total_started_at"
1576 1587
 
1577 1588
   trap 'handle_interrupt' INT TERM
1589
+  trap 'cleanup_on_exit' EXIT
1578 1590
 
1579 1591
   parse_args "$@"
1580 1592
   check_tools
@@ -1724,8 +1736,6 @@ main() {
1724 1736
     "$INPUT_BYTES_PROCESSED" \
1725 1737
     "$OUTPUT_BYTES_PROCESSED"
1726 1738
 
1727
-  attempt_unmount_auto_staging_ramdisk
1728
-
1729 1739
   if [[ "$ERRORS" -gt 0 ]]; then
1730 1740
     exit 1
1731 1741
   fi