Imported Upstream version 3.1.0
[debian/amanda] / changer-src / chg-lib.sh.in
index 7ed4e5a98a8b6cfacb4116ae8efa8482a67af44e..a53c48a8cfff139732818a8be0e8b8d2ccbe148c 100644 (file)
@@ -28,21 +28,12 @@ amlibexecdir="@amlibexecdir@"
 # Eponymous functions to access various amanda apps
 # TODO: move to amanda-sh-lib.sh
 
-if test "@USE_VERSION_SUFFIXES@" = "yes"; then
-    amgetconf() {
-        "${sbindir}/amgetconf-@VERSION@" "${@}"
-    }
-    amdevcheck() {
-        "${sbindir}/amdevcheck-@VERSION@" "${@}"
-    }
-else
-    amgetconf() {
-        "${sbindir}/amgetconf" "${@}"
-    }
-    amdevcheck() {
-        "${sbindir}/amdevcheck" "${@}"
-    }
-fi
+amgetconf() {
+    "${sbindir}/amgetconf" "${@}"
+}
+amdevcheck() {
+    "${sbindir}/amdevcheck" "${@}"
+}
 
 # This function tries to find a useable mt binary. If a fully-qualified path
 # was provided at configure time or via a config file, we check that it
@@ -84,26 +75,39 @@ try_find_mt() {
 # This function strips the tape: from the front of device names.
 # Capture its output with ``.
 tape_device_filename() {
-    # Easy (?) to express this in Perl.
-    perl -e '$ARGV[0] =~ /^(?:([^:]+):)?(.*)$/;
-             print $2 if ($1 || "tape") eq "tape"' "$1"
+    if echo "$1"|grep '^tape:' >/dev/null; then
+       echo "$1" | sed 's/^tape://'
+    else
+       if echo "$1"|grep '^/' >/dev/null; then
+           echo "$1"
+       fi
+    fi
 }
 
-# Runs amdevcheck to find out if a particular device is usable. Finds the
-# config name based on pwd, so run it from the config directory. The sole
-# argument should be the device name to check. Note that amdevcheck can FP
-# on device status, since some devices can't differentiate between device
-# problems and an unlabeled volume.
+# Invoke amdevcheck to determine whether the device is ready for use.
+#
+# @return 0 if a tape is loaded or error
+# @return 1 if a tape is tape offline or busy
+#
+# @side-effect: $amdevcheck_message is the contents of all MESSAGE lines from
+#               amdevcheck, suitable for use in higher-level error messages
 amdevcheck_status() {
-    amdevcheck_config_dir=`pwd`
-    amdevcheck_config_name=`basename $amdevcheck_config_dir`
-    amdevcheck_output=`amdevcheck $amdevcheck_config_name $@`
+    amdevcheck_message=
+    local amdevcheck_output=`amdevcheck . $@`
+    local amdevcheck_status=$?
 
-    if test $? -eq 0 || \
-        echo $amdevcheck_output | grep UNLABELED > /dev/null; then
-        return 0
+    test "$amdevcheck_status" -ne 0 && return 0
+
+    # extract any messages
+    amdevcheck_message=`echo "$amdevcheck_output" | sed -n -e '/^MESSAGE /{' -e 's/^MESSAGE //' -e 'p' -e 'q' -e '}'`
+
+    # Return 1 if it's possible that the device is offline or busy; if the device cannot
+    # distinguish this state from an error condition, then our caller will just have to
+    # time out
+    if echo "$amdevcheck_output" | $EGREP "VOLUME_MISSING|DEVICE_BUSY" > /dev/null; then
+       return 1
     else
-        return 1
+       return 0
     fi
 }
 
@@ -112,7 +116,7 @@ amdevcheck_status() {
 # be extended at some later time.
 
 try_eject_device() {
-    if echo "$1" | grep -e '^tape:' > /dev/null; then
+    if echo "$1" | grep '^tape:' > /dev/null; then
         try_eject_device_tape="`echo \"$1\" | cut -b6-`"
     elif echo "$1" | grep -v : > /dev/null; then
         try_eject_device_tape="$1"
@@ -122,7 +126,7 @@ try_eject_device() {
 
     if test -n "$try_eject_device_tape"; then
         if try_find_mt; then
-            $MT $MTF "$try_eject_device_tape" eject
+            $MT $MTF "$try_eject_device_tape" offline
         fi
     else
         # Technically we failed to eject the device, but we presume that's