Imported Upstream version 2.6.1
[debian/amanda] / server-src / amdevcheck.pl
index 1e6d8b4c12ae3816ed1e44fc7d49fef68c8f6320..e96c3b0788bf387f6dae75baced9940c10416143 100644 (file)
@@ -1,10 +1,30 @@
 #! @PERL@
+# Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
+# 
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 as published 
+# by the Free Software Foundation.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+# 
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+# 
+# Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
+# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
+
 use lib '@amperldir@';
+use Getopt::Long;
+
 use strict;
 use Amanda::Device qw( :constants );
 use Amanda::Config qw( :getconf :init );
 use Amanda::Debug qw( :logging );
-use Amanda::Util qw( :running_as_flags );
+use Amanda::Util qw( :constants );
 
 # try to open the device and read its label, returning the device_read_label
 # result (one or more of ReadLabelStatusFlags)
@@ -12,25 +32,34 @@ sub try_read_label {
     my ($device_name) = @_;
 
     if ( !$device_name ) {
-        return $READ_LABEL_STATUS_DEVICE_MISSING;
+       die("No device name specified.\n");
     }
 
+    my $result;
+
     my $device = Amanda::Device->new($device_name);
     if ( !$device ) {
-        return $READ_LABEL_STATUS_DEVICE_MISSING
-             | $READ_LABEL_STATUS_DEVICE_ERROR;
+       die("Error creating $device_name");
     }
 
-    $device->set_startup_properties_from_config();
+    if ($device->status() == $DEVICE_STATUS_SUCCESS) {
+       $result = $device->read_label();
+    } else {
+       $result = $device->status();
+    }
 
-    return $device->read_label();
+    print_result( $result, $device->error() );
+    return $result;
 }
 
 # print the results, one flag per line
 sub print_result {
-    my ($flags) = @_;
+    my ($flags, $errmsg) = @_;
 
-    print join( "\n", ReadLabelStatusFlags_to_strings($flags) ), "\n";
+    if ($flags != $DEVICE_STATUS_SUCCESS) {
+       print "MESSAGE $errmsg\n";
+    }
+    print join( "\n", DeviceStatusFlags_to_strings($flags) ), "\n";
 }
 
 sub usage {
@@ -42,13 +71,27 @@ EOF
 
 ## Application initialization
 
-Amanda::Util::setup_application("amdevcheck", "server", "cmdline");
+Amanda::Util::setup_application("amdevcheck", "server", $CONTEXT_SCRIPTUTIL);
+
+my $config_overwrites = new_config_overwrites($#ARGV+1);
+
+Getopt::Long::Configure(qw(bundling));
+GetOptions(
+    'help|usage|?' => \&usage,
+    'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
+) or usage();
 
 usage() if ( @ARGV < 1 || @ARGV > 2 );
 my $config_name = $ARGV[0];
-if (!config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name)) {
-    critical('errors processing config file "' .
-              Amanda::Config::get_config_filename() . '"');
+
+config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
+apply_config_overwrites($config_overwrites);
+my ($cfgerr_level, @cfgerr_errors) = config_errors();
+if ($cfgerr_level >= $CFGERR_WARNINGS) {
+    config_print_errors();
+    if ($cfgerr_level >= $CFGERR_ERRORS) {
+       die("errors processing config file");
+    }
 }
 
 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
@@ -62,4 +105,5 @@ if ( $#ARGV == 1 ) {
     $device_name = getconf($CNF_TAPEDEV);
 }
 
-print_result( try_read_label($device_name) );
+try_read_label($device_name);
+exit 0;