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