daad4a5847e54dec3f79d36a241a10ff57332a62
[debian/amanda] / perl / Amanda / Changer / single.pm
1 # Copyright (c) 2005-2008 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This library is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU Lesser General Public License version 2.1 as
5 # published by the Free Software Foundation.
6 #
7 # This library 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 Lesser General Public
10 # License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public License
13 # along with this library; if not, write to the Free Software Foundation,
14 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
15 #
16 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, 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
33 =head1 NAME
34
35 Amanda::Changer::single
36
37 =head1 DESCRIPTION
38
39 This changer represents a single drive as a changer.  It may eventually morph
40 into something similar to the old C<chg-manual>.
41
42 Whatever you load, you get the volume in the drive.  The volume's either
43 reserved or not.  All pretty straightforward.
44
45 =head1 TODO
46
47 Support notifying the user that a tape is required, and some kind of "OK,
48 loaded" feedback mechanism -- perhaps a utility script of some sort, or an
49 amtape subcommand?
50
51 =cut
52
53 sub new {
54     my $class = shift;
55     my ($cc, $tpchanger) = @_;
56     my ($device_name) = ($tpchanger =~ /chg-single:(.*)/);
57
58     my $self = {
59         device_name => $device_name,
60         reserved => 0,
61     };
62
63     bless ($self, $class);
64     return $self;
65 }
66
67 sub load {
68     my $self = shift;
69     my %params = @_;
70
71     die "no res_cb supplied" unless (exists $params{'res_cb'});
72
73     if ($self->{'reserved'}) {
74         Amanda::MainLoop::call_later($params{'res_cb'},
75             "'{$self->{device_name}}' is already reserved", undef);
76     } else {
77         Amanda::MainLoop::call_later($params{'res_cb'},
78                 undef, Amanda::Changer::single::Reservation->new($self));
79     }
80 }
81
82 sub info {
83     my $self = shift;
84     my %params = @_;
85     my %results;
86
87     die "no info_cb supplied" unless (exists $params{'info_cb'});
88     die "no info supplied" unless (exists $params{'info'});
89
90     for my $inf (@{$params{'info'}}) {
91         if ($inf eq 'num_slots') {
92             $results{$inf} = 1;
93         } else {
94             warn "Ignoring request for info key '$inf'";
95         }
96     }
97
98     Amanda::MainLoop::call_later($params{'info_cb'}, undef, %results);
99 }
100
101 package Amanda::Changer::single::Reservation;
102 use vars qw( @ISA );
103 @ISA = qw( Amanda::Changer::Reservation );
104
105 sub new {
106     my $class = shift;
107     my ($chg, $drive, $next_slot) = @_;
108     my $self = Amanda::Changer::Reservation::new($class);
109
110     $self->{'chg'} = $chg;
111
112     $self->{'device_name'} = $chg->{'device_name'};
113     $self->{'this_slot'} = '1';
114     $self->{'next_slot'} = '1';
115     $chg->{'reserved'} = 1;
116
117     return $self;
118 }
119
120 sub do_release {
121     my $self = shift;
122     my %params = @_;
123
124     $self->{'chg'}->{'reserved'} = 0;
125
126     if (exists $params{'finished_cb'}) {
127         Amanda::MainLoop::call_later($params{'finished_cb'}, undef);
128     }
129 }