Imported Upstream version 3.3.3
[debian/amanda] / perl / Amanda / Changer / single.pm
1 # Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19
20 package Amanda::Changer::single;
21
22 use strict;
23 use warnings;
24 use Carp;
25 use vars qw( @ISA );
26 @ISA = qw( Amanda::Changer );
27
28 use File::Glob qw( :glob );
29 use File::Path;
30 use Amanda::Config qw( :getconf );
31 use Amanda::Debug;
32 use Amanda::Changer;
33 use Amanda::MainLoop;
34 use Amanda::Device qw( :constants );
35
36 =head1 NAME
37
38 Amanda::Changer::single
39
40 =head1 DESCRIPTION
41
42 This changer represents a single drive as a changer.  It may eventually morph
43 into something similar to the old C<chg-manual>.
44
45 Whatever you load, you get the volume in the drive.  The volume's either
46 reserved or not.  All pretty straightforward.
47
48 See the amanda-changers(7) manpage for usage information.
49
50 =cut
51
52 sub new {
53     my $class = shift;
54     my ($config, $tpchanger) = @_;
55     my ($device_name) = ($tpchanger =~ /chg-single:(.*)/);
56
57     # check that $device_name is an honest-to-goodness device
58     my $tmpdev = Amanda::Device->new($device_name);
59     if ($tmpdev->status() != $DEVICE_STATUS_SUCCESS) {
60         return Amanda::Changer->make_error("fatal", undef,
61             message => "chg-single: error opening device '$device_name': " .
62                         $tmpdev->error_or_status());
63     }
64
65     my $self = {
66         config => $config,
67         device_name => $device_name,
68         reserved => 0,
69     };
70
71     bless ($self, $class);
72     return $self;
73 }
74
75 sub load {
76     my $self = shift;
77     my %params = @_;
78     $self->validate_params('load', \%params);
79
80     return if $self->check_error($params{'res_cb'});
81
82     confess "no res_cb supplied" unless (exists $params{'res_cb'});
83
84     if ($self->{'reserved'}) {
85         return $self->make_error("failed", $params{'res_cb'},
86             reason => "volinuse",
87             message => "'$self->{device_name}' is already reserved");
88     }
89
90     if (keys %{$params{'except_slots'}} > 0) {
91         return $self->make_error("failed", $params{'res_cb'},
92                 reason => "notfound",
93                 message => "all slots have been loaded");
94     }
95
96     my $device = Amanda::Device->new($self->{'device_name'});
97     if ($device->status() != $DEVICE_STATUS_SUCCESS) {
98         return $self->make_error("fatal", $params{'res_cb'},
99             message => "error opening device '$self->{device_name}': " . $device->error_or_status());
100     }
101
102     if (my $msg = $self->{'config'}->configure_device($device)) {
103         # a failure to configure a device is fatal, since it's probably
104         # a user configuration error (and thus unlikely to work for the
105         # next device, either)
106         return $self->make_error("fatal", $params{'res_cb'},
107             message => $msg);
108     }
109
110     my $res = Amanda::Changer::single::Reservation->new($self, $device);
111     $device->read_label();
112     $params{'res_cb'}->(undef, $res);
113 }
114
115 sub inventory {
116     my $self = shift;
117     my %params = @_;
118
119     my @inventory;
120     my $s = { slot => 0,
121               state => undef,
122               device_status => undef,
123               f_type => undef,
124               label => undef };
125     push @inventory, $s;
126
127     $params{'inventory_cb'}->(undef, \@inventory);
128 }
129
130 sub info_key {
131     my $self = shift;
132     my ($key, %params) = @_;
133     my %results;
134
135     return if $self->check_error($params{'info_cb'});
136
137     if ($key eq 'num_slots') {
138         $results{$key} = 1;
139     } elsif ($key eq 'fast_search') {
140         # (asking the user for a specific label is faster than asking
141         # for each "slot" in a sequential scan, so search is "fast")
142         $results{$key} = 0;
143     }
144
145     $params{'info_cb'}->(undef, %results) if $params{'info_cb'};
146 }
147
148 package Amanda::Changer::single::Reservation;
149 use vars qw( @ISA );
150 @ISA = qw( Amanda::Changer::Reservation );
151
152 sub new {
153     my $class = shift;
154     my ($chg, $device) = @_;
155     my $self = Amanda::Changer::Reservation::new($class);
156
157     $self->{'chg'} = $chg;
158
159     $self->{'device'} = $device;
160
161     $self->{'this_slot'} = '1';
162     $chg->{'reserved'} = 1;
163
164     return $self;
165 }
166
167 sub do_release {
168     my $self = shift;
169     my %params = @_;
170
171     $self->{'device'}->eject() if (exists $self->{'device'} and
172                                    exists $params{'eject'} and
173                                    $params{'eject'});
174
175     $self->{'chg'}->{'reserved'} = 0;
176
177     # unref the device, for good measure
178     $self->{'device'} = undef;
179
180     $params{'finished_cb'}->(undef) if $params{'finished_cb'};
181 }