Merge tag 'upstream/3.3.2'
[debian/amanda] / server-src / amcheck-device.pl
index d9689cb9ad8ae55966b8b1fbf1a805dd0c79009b..87331a6fa27a01d07a8f56243addd74e9fc6c232 100644 (file)
@@ -1,5 +1,5 @@
 #! @PERL@
-# Copyright (c) 2009, 2010 Zmanda Inc.  All Rights Reserved.
+# Copyright (c) 2009-2012 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
@@ -29,6 +29,7 @@ use Amanda::Device qw( :constants );
 use Amanda::MainLoop;
 use Amanda::Changer;
 use Amanda::Taper::Scan;
+use Amanda::Interactivity;
 use Getopt::Long;
 
 Amanda::Util::setup_application("amcheck-device", "server", $CONTEXT_CMDLINE);
@@ -37,6 +38,7 @@ my $config_overrides = new_config_overrides($#ARGV+1);
 my $overwrite = 0;
 Getopt::Long::Configure(qw{bundling});
 GetOptions(
+    'version' => \&Amanda::Util::version_opt,
     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
     'w' => \$overwrite,
 ) or usage();
@@ -117,7 +119,12 @@ sub _user_msg_fn {
                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_UNLABELED and
                          $dev->volume_header and
                          $dev->volume_header->{'type'} == $Amanda::Header::F_WEIRD) {
-                    print STDERR " contains a non-Amanda volume; check and relabel it with 'amlabel -f'\n";
+                   my $autolabel = getconf($CNF_AUTOLABEL);
+                   if ($autolabel->{'non_amanda'}) {
+                       print STDERR " contains a non-Amanda volume\n";
+                   } else {
+                       print STDERR " contains a non-Amanda volume; check and relabel it with 'amlabel -f'\n";
+                   }
                 } elsif ($dev->status & $DEVICE_STATUS_VOLUME_ERROR) {
                     my $message = $dev->error_or_status();
                     print STDERR " Can't read label: $message\n";
@@ -141,16 +148,23 @@ sub failure {
 sub do_check {
     my ($finished_cb) = @_;
     my ($res, $label, $mode);
+    my $tlf = Amanda::Config::config_dir_relative(getconf($CNF_TAPELIST));
+    my $tl = Amanda::Tapelist->new($tlf);
+    my $chg = Amanda::Changer->new(undef, tapelist => $tl);
+    return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
+    my $interactivity = Amanda::Interactivity->new(
+                                       name => getconf($CNF_INTERACTIVITY));
+    my $scan_name = getconf($CNF_TAPERSCAN);
+    my $taperscan = Amanda::Taper::Scan->new(algorithm => $scan_name,
+                                            changer => $chg,
+                                            interactivity => $interactivity,
+                                            tapelist => $tl);
 
     my $steps = define_steps
-       cb_ref => \$finished_cb;
+       cb_ref => \$finished_cb,
+       finalize => sub { $taperscan->quit(); };
 
     step start => sub {
-       my $chg = Amanda::Changer->new();
-
-       return failure($chg, $finished_cb) if ($chg->isa("Amanda::Changer::Error"));
-
-       my $taperscan = Amanda::Taper::Scan->new(changer => $chg);
        $taperscan->scan(
            result_cb => $steps->{'result_cb'},
            user_msg_fn => \&_user_msg_fn
@@ -159,23 +173,28 @@ sub do_check {
 
     step result_cb => sub {
        (my $err, $res, $label, $mode) = @_;
-       return failure($err, $finished_cb) if $err;
-
-       if (defined $res->{'device'}->volume_label()) {
-           $res->set_label(label => $res->{'device'}->volume_label(),
-                           finished_cb => $steps->{'set_labeled'});
-       } else {
-           $steps->{'set_labeled'}->(undef);
-       };
-    };
+       if ($err) {
+           if ($res) {
+               $res->release(finished_cb => sub {
+                   return failure($err, $finished_cb);
+               });
+               return;
+           } else {
+               return failure($err, $finished_cb);
+           }
+       }
 
-    step set_labeled => sub {
        my $modestr = ($mode == $ACCESS_APPEND)? "append" : "write";
        my $slot = $res->{'this_slot'};
-       if (defined $res->{'device'}->volume_label()) {
+       if (defined $res->{'device'} and defined $res->{'device'}->volume_label()) {
            print "Will $modestr to volume '$label' in slot $slot.\n";
        } else {
-           print "Will $modestr label '$label' to new volume in slot $slot.\n";
+           my $header = $res->{'device'}->volume_header();
+           if ($header->{'type'} == $Amanda::Header::F_WEIRD) {
+               print "Will $modestr label '$label' to non-Amanda volume in slot $slot.\n";
+           } else {
+               print "Will $modestr label '$label' to new volume in slot $slot.\n";
+           }
        }
 
        $steps->{'check_access_type'}->();