2 * Copyright (c) 2008,2009 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);
40 C_read_tapelist($filename);
42 # and then read it ourselves
43 open(my $fh, "<", $filename) or return undef;
45 while (my $line = <$fh>) {
46 my ($datestamp, $label, $reuse, $comment)
47 = $line =~ m/^([0-9]*)\s([^\s]*)\s(reuse|no-reuse)\s*(?:\#(.*))?$/mx;
48 next if !defined $datestamp; # silently filter out bogus lines
50 'datestamp' => $datestamp,
52 'reuse' => ($reuse eq 'reuse'),
53 'comment' => $comment,
58 # sort in descending order by datestamp, sorting on position, too, to ensure
59 # that entries with the same datestamp stay in the right order
60 update_positions(\@tles); # call a method with an explicit $self
62 $b->{'datestamp'} cmp $a->{'datestamp'}
63 || $a->{'position'} <=> $b->{'position'}
66 # and re-calculate the positions
67 update_positions(\@tles);
69 my $self = bless \@tles, "Amanda::Tapelist";
78 # and produce an empty object
79 my $self = bless [], "Amanda::Tapelist";
80 $self->update_positions();
87 sub lookup_tapelabel {
91 for my $tle (@$self) {
92 return $tle if ($tle->{'label'} eq $label);
102 $self->update_positions();
103 return $self->[$position-1];
106 sub lookup_tapedate {
108 my ($datestamp) = @_;
110 for my $tle (@$self) {
111 return $tle if ($tle->{'datestamp'} eq $datestamp);
117 sub remove_tapelabel {
121 for (my $i = 0; $i < @$self; $i++) {
122 if ($self->[$i]->{'label'} eq $label) {
123 splice @$self, $i, 1;
124 $self->update_positions();
132 my ($datestamp, $label, $comment) = @_;
134 # prepend this (presumably new) volume to the beginning of the list
136 'datestamp' => $datestamp,
139 'comment' => $comment,
141 $self->update_positions();
148 open(my $fh, ">", $filename) or die("Could not open '$filename' for writing: $!");
149 for my $tle (@$self) {
150 my $datestamp = $tle->{'datestamp'};
151 my $label = $tle->{'label'};
152 my $reuse = $tle->{'reuse'} ? 'reuse' : 'no-reuse';
153 my $comment = (defined $tle->{'comment'})? (" #" . $tle->{'comment'}) : '';
154 print $fh "$datestamp $label $reuse$comment\n";
158 # re-read from the C side to synchronize
159 C_read_tapelist($filename);
164 # update the 'position' key for each TLE
165 sub update_positions {
167 for (my $i = 0; $i < @$self; $i++) {
168 $self->[$i]->{'position'} = $i+1;
174 char *get_last_reusable_tape_label(int skip);
175 char *list_new_tapes(int nb);
177 /* C functions -- should be called *only* from within this module */
179 %rename(C_read_tapelist) read_tapelist;
180 int read_tapelist(char *tapefile);
182 %rename(C_clear_tapelist) clear_tapelist;
183 void clear_tapelist(void);