oops, need this patch file too
[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 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 Carp;
24 use vars qw( @ISA );
25 @ISA = qw( Amanda::Changer );
26
27 use File::Glob qw( :glob );
28 use File::Path;
29 use Amanda::Config qw( :getconf );
30 use Amanda::Debug;
31 use Amanda::Changer;
32 use Amanda::MainLoop;
33 use Amanda::Device qw( :constants );
34
35 =head1 NAME
36
37 Amanda::Changer::single
38
39 =head1 DESCRIPTION
40
41 This changer represents a single drive as a changer.  It may eventually morph
42 into something similar to the old C<chg-manual>.
43
44 Whatever you load, you get the volume in the drive.  The volume's either
45 reserved or not.  All pretty straightforward.
46
47 See the amanda-changers(7) manpage for usage information.
48
49 =cut
50
51 sub new {
52     my $class = shift;
53     my ($config, $tpchanger) = @_;
54     my ($device_name) = ($tpchanger =~ /chg-single:(.*)/);
55
56     # check that $device_name is an honest-to-goodness device
57     my $tmpdev = Amanda::Device->new($device_name);
58     if ($tmpdev->status() != $DEVICE_STATUS_SUCCESS) {
59         return Amanda::Changer->make_error("fatal", undef,
60             message => "chg-single: error opening device '$device_name': " .
61                         $tmpdev->error_or_status());
62     }
63
64     my $self = {
65         config => $config,
66         device_name => $device_name,
67         reserved => 0,
68     };
69
70     bless ($self, $class);
71     return $self;
72 }
73
74 sub load {
75     my $self = shift;
76     my %params = @_;
77     $self->validate_params('load', \%params);
78
79     return if $self->check_error($params{'res_cb'});
80
81     confess "no res_cb supplied" unless (exists $params{'res_cb'});
82
83     if ($self->{'reserved'}) {
84         return $self->make_error("failed", $params{'res_cb'},
85             reason => "volinuse",
86             message => "'$self->{device_name}' is already reserved");
87     }
88
89     if (keys %{$params{'except_slots'}} > 0) {
90         return $self->make_error("failed", $params{'res_cb'},
91                 reason => "notfound",
92                 message => "all slots have been loaded");
93     }
94
95     my $device = Amanda::Device->new($self->{'device_name'});
96     if ($device->status() != $DEVICE_STATUS_SUCCESS) {
97         return $self->make_error("fatal", $params{'res_cb'},
98             message => "error opening device '$self->{device_name}': " . $device->error_or_status());
99     }
100
101     if (my $msg = $self->{'config'}->configure_device($device)) {
102         # a failure to configure a device is fatal, since it's probably
103         # a user configuration error (and thus unlikely to work for the
104         # next device, either)
105         return $self->make_error("fatal", $params{'res_cb'},
106             message => $msg);
107     }
108
109     my $res = Amanda::Changer::single::Reservation->new($self, $device);
110     $device->read_label();
111     $params{'res_cb'}->(undef, $res);
112 }
113
114 sub inventory {
115     my $self = shift;
116     my %params = @_;
117
118     my @inventory;
119     my $s = { slot => 0,
120               state => undef,
121               device_status => undef,
122               f_type => undef,
123               label => undef };
124     push @inventory, $s;
125
126     $params{'inventory_cb'}->(undef, \@inventory);
127 }
128
129 sub info_key {
130     my $self = shift;
131     my ($key, %params) = @_;
132     my %results;
133
134     return if $self->check_error($params{'info_cb'});
135
136     if ($key eq 'num_slots') {
137         $results{$key} = 1;
138     } elsif ($key eq 'fast_search') {
139         # (asking the user for a specific label is faster than asking
140         # for each "slot" in a sequential scan, so search is "fast")
141         $results{$key} = 0;
142     }
143
144     $params{'info_cb'}->(undef, %results) if $params{'info_cb'};
145 }
146
147 package Amanda::Changer::single::Reservation;
148 use vars qw( @ISA );
149 @ISA = qw( Amanda::Changer::Reservation );
150
151 sub new {
152     my $class = shift;
153     my ($chg, $device) = @_;
154     my $self = Amanda::Changer::Reservation::new($class);
155
156     $self->{'chg'} = $chg;
157
158     $self->{'device'} = $device;
159
160     $self->{'this_slot'} = '1';
161     $chg->{'reserved'} = 1;
162
163     return $self;
164 }
165
166 sub do_release {
167     my $self = shift;
168     my %params = @_;
169
170     $self->{'device'}->eject() if (exists $self->{'device'} and
171                                    exists $params{'eject'} and
172                                    $params{'eject'});
173
174     $self->{'chg'}->{'reserved'} = 0;
175
176     # unref the device, for good measure
177     $self->{'device'} = undef;
178
179     $params{'finished_cb'}->(undef) if $params{'finished_cb'};
180 }