2 * Copyright (c) 2008, 2009, 2010 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
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
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
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
21 %module "Amanda::Tapelist"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
25 %include "Amanda/Tapelist.pod"
32 use Amanda::Debug qw(:logging);
33 use Amanda::Config qw( config_dir_relative );
35 use Fcntl qw(:flock); # import LOCK_* constants
41 my ($filename, $lock ) = @_;
43 filename => $filename,
44 lockname => $filename . '.lock',
77 C_read_tapelist($self->{'filename'});
79 $self->_read_tapelist();
82 sub lookup_tapelabel {
86 for my $tle (@{$self->{'tles'}}) {
87 return $tle if ($tle->{'label'} eq $label);
97 $self->_update_positions();
98 return $self->{'tles'}->[$position-1];
101 sub lookup_tapedate {
103 my ($datestamp) = @_;
105 for my $tle (@{$self->{'tles'}}) {
106 return $tle if ($tle->{'datestamp'} eq $datestamp);
112 sub remove_tapelabel {
116 for (my $i = 0; $i < @{$self->{tles}}; $i++) {
117 if ($self->{tles}->[$i]->{'label'} eq $label) {
118 splice @{$self->{tles}}, $i, 1;
119 $self->_update_positions();
127 my ($datestamp, $label, $comment, $reuse, $meta, $barcode, $blocksize) = @_;
128 $reuse = 1 if !defined $reuse;
130 # prepend this (presumably new) volume to the beginning of the list
132 'datestamp' => $datestamp,
135 'barcode' => $barcode,
137 'blocksize' => $blocksize,
138 'comment' => $comment,
140 my $tles = $self->{'tles'};
141 if (!defined $tles->[0] ||
142 $tles->[0]->{'datestamp'} le $datestamp) {
143 unshift @{$tles}, $tle;
144 } elsif (defined $tles->[0] &&
145 $tles->[@$tles-1]->{'datestamp'} gt $datestamp) {
149 for my $i (0..(@$tles-1)) {
150 if ($tles->[$i]->{'datestamp'} le $datestamp) {
151 splice @{$tles}, $i, 0, $tle;
156 push @{$tles}, $tle if !$added;
158 $self->_update_positions();
165 $filename = $self->{'filename'} if !defined $filename;
167 my $new_tapelist_file = $filename . "-new-" . time();
169 open(my $fhn, ">", $new_tapelist_file) or die("Could not open '$new_tapelist_file' for writing: $!");
170 for my $tle (@{$self->{tles}}) {
171 my $datestamp = $tle->{'datestamp'};
172 my $label = $tle->{'label'};
173 my $reuse = $tle->{'reuse'} ? 'reuse' : 'no-reuse';
174 my $barcode = (defined $tle->{'barcode'})? (" BARCODE:" . $tle->{'barcode'}) : '';
175 my $meta = (defined $tle->{'meta'})? (" META:" . $tle->{'meta'}) : '';
176 my $blocksize = (defined $tle->{'blocksize'})? (" BLOCKSIZE:" . $tle->{'blocksize'}) : '';
177 my $comment = (defined $tle->{'comment'})? (" #" . $tle->{'comment'}) : '';
178 $result &&= print $fhn "$datestamp $label $reuse$barcode$meta$blocksize$comment\n";
180 my $result_close = close($fhn);
181 $result &&= $result_close;
183 return if (!$result);
185 unless (move($new_tapelist_file, $filename)) {
186 die ("failed to rename '$new_tapelist_file' to '$filename': $!");
189 # re-read from the C side to synchronize
190 C_read_tapelist($filename);
200 return if !exists $self->{'fl'};
202 $self->{'fl'}->unlock();
211 if (!-e $self->{'lockname'}) {
212 open(my $fhl, ">>", $self->{'lockname'});
215 my $fl = Amanda::Util::file_lock->new($self->{'lockname'});
216 while(($r = $fl->lock()) == 1) {
228 open(my $fh, "<", $self->{'filename'}) or return $self;
229 while (my $line = <$fh>) {
230 my ($datestamp, $label, $reuse, $barcode, $meta, $blocksize, $comment)
231 = $line =~ m/^([0-9]*)\s([^\s]*)\s(reuse|no-reuse)\s*(?:BARCODE:([^\s]*))?\s*(?:META:([^\s]*))?\s*(?:BLOCKSIZE:([^\s]*))?\s*(?:\#(.*))?$/mx;
232 next if !defined $datestamp; # silently filter out bogus lines
234 'datestamp' => $datestamp,
236 'reuse' => ($reuse eq 'reuse'),
237 'barcode' => $barcode,
239 'blocksize' => $blocksize,
240 'comment' => $comment,
245 # sort in descending order by datestamp, sorting on position, too, to ensure
246 # that entries with the same datestamp stay in the right order
247 $self->{'tles'} = \@tles;
248 $self->_update_positions();
250 $b->{'datestamp'} cmp $a->{'datestamp'}
251 || $a->{'position'} <=> $b->{'position'}
254 $self->{'tles'} = \@tles;
256 # and re-calculate the positions
257 $self->_update_positions(\@tles);
260 # update the 'position' key for each TLE
261 sub _update_positions {
263 my $tles = $self->{'tles'};
264 for (my $i = 0; $i < scalar @$tles; $i++) {
265 $tles->[$i]->{'position'} = $i+1;
271 char *get_last_reusable_tape_label(int skip);
272 char *list_new_tapes(int nb);
274 /* C functions -- should be called *only* from within this module */
276 %rename(C_read_tapelist) read_tapelist;
277 int read_tapelist(char *tapefile);
279 %rename(C_clear_tapelist) clear_tapelist;
280 void clear_tapelist(void);