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