Imported Upstream version 3.2.1
[debian/amanda] / perl / Amanda / Tapelist.swg
1 /*
2  * Copyright (c) 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
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.
7  *
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
11  * for more details.
12  *
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
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %module "Amanda::Tapelist"
22 %include "amglue/amglue.swg"
23 %include "exception.i"
24
25 %include "Amanda/Tapelist.pod"
26
27 %{
28 #include "tapefile.h"
29 %}
30
31 %perlcode %{
32 use Amanda::Debug qw(:logging);
33 use Amanda::Config qw( config_dir_relative );
34 use File::Copy;
35 use Fcntl qw(:flock); # import LOCK_* constants
36
37 ## package functions
38
39 sub new {
40     my ($class)  = shift;
41     my ($filename, $lock ) = @_;
42     my $self = {
43         filename => $filename,
44         lockname => $filename . '.lock',
45     };
46     bless $self, $class;
47
48     $self->reload($lock);
49     return $self;
50 }
51
52 sub clear_tapelist {
53     my $self = shift;
54
55     # clear the C version
56     C_clear_tapelist();
57
58     $self->{'tles'} = [];
59
60     return $self;
61 }
62
63 ## methods
64
65 sub reload {
66     my $self = shift;
67     my ($lock) = @_;
68
69     if ($lock) {
70         $self->_take_lock();
71     }
72
73     # clear the C copy
74     C_clear_tapelist();
75
76     # let C read the file
77     C_read_tapelist($self->{'filename'});
78
79     $self->_read_tapelist();
80 }
81
82 sub lookup_tapelabel {
83     my $self = shift;
84     my ($label) = @_;
85
86     for my $tle (@{$self->{'tles'}}) {
87         return $tle if ($tle->{'label'} eq $label);
88     }
89
90     return undef;
91 }
92
93 sub lookup_tapepos {
94     my $self = shift;
95     my ($position) = @_;
96
97     $self->_update_positions();
98     return $self->{'tles'}->[$position-1];
99 }
100
101 sub lookup_tapedate {
102     my $self = shift;
103     my ($datestamp) = @_;
104
105     for my $tle (@{$self->{'tles'}}) {
106         return $tle if ($tle->{'datestamp'} eq $datestamp);
107     }
108
109     return undef;
110 }
111
112 sub remove_tapelabel {
113     my $self = shift;
114     my ($label) = @_;
115
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();
120             return;
121         }
122     }
123 }
124
125 sub add_tapelabel {
126     my $self = shift;
127     my ($datestamp, $label, $comment, $reuse, $meta, $barcode) = @_;
128     # $meta is unused
129     $reuse = 1 if !defined $reuse;
130
131     # prepend this (presumably new) volume to the beginning of the list
132     unshift @{$self->{'tles'}}, {
133         'datestamp' => $datestamp,
134         'label' => $label,
135         'reuse' => $reuse,
136         'barcode' => $barcode,
137         'comment' => $comment,
138     };
139     $self->_update_positions();
140 }
141
142 sub write {
143     my $self = shift;
144     my ($filename) = @_;
145     my $result = TRUE;
146     $filename = $self->{'filename'} if !defined $filename;
147
148     my $new_tapelist_file = $filename . "-new-" . time();
149
150     open(my $fhn, ">", $new_tapelist_file) or die("Could not open '$new_tapelist_file' for writing: $!");
151     for my $tle (@{$self->{tles}}) {
152         my $datestamp = $tle->{'datestamp'};
153         my $label = $tle->{'label'};
154         my $reuse = $tle->{'reuse'} ? 'reuse' : 'no-reuse';
155         my $barcode = (defined $tle->{'barcode'})? (" BARCODE:" . $tle->{'barcode'}) : '';
156         my $comment = (defined $tle->{'comment'})? (" #" . $tle->{'comment'}) : '';
157         $result &&= print $fhn "$datestamp $label $reuse$barcode$comment\n";
158     }
159     my $result_close = close($fhn);
160     $result &&= $result_close;
161
162     return if (!$result);
163
164     unless (move($new_tapelist_file, $filename)) {
165         die ("failed to rename '$new_tapelist_file' to '$filename': $!");
166     }
167
168     # re-read from the C side to synchronize
169     C_read_tapelist($filename);
170
171     $self->unlock();
172
173     return undef;
174 }
175
176 sub unlock {
177     my $self = shift;
178
179     return if !exists $self->{'fl'};
180
181     $self->{'fl'}->unlock();
182     delete $self->{'fl'}
183 }
184
185 ## private methods
186
187 sub _take_lock {
188     my $self = shift;
189
190     if (!-e $self->{'lockname'}) {
191         open(my $fhl, ">>", $self->{'lockname'});
192         close($fhl);
193     }
194     my $fl = Amanda::Util::file_lock->new($self->{'lockname'});
195     while(($r = $fl->lock()) == 1) {
196         sleep(1);
197     }
198     if ($r == 0) {
199         $self->{'fl'} = $fl;
200     }
201 }
202
203 sub _read_tapelist {
204     my $self = shift;
205
206     my @tles;
207     open(my $fh, "<", $self->{'filename'}) or return $self;
208     while (my $line = <$fh>) {
209         my ($datestamp, $label, $reuse, $barcode, $comment)
210             = $line =~ m/^([0-9]*)\s([^\s]*)\s(reuse|no-reuse)\s*(?:BARCODE:([^\s]*))?\s*(?:\#(.*))?$/mx;
211         next if !defined $datestamp; # silently filter out bogus lines
212         push @tles, {
213             'datestamp' => $datestamp,
214             'label' => $label,
215             'reuse' => ($reuse eq 'reuse'),
216             'barcode' => $barcode,
217             'comment' => $comment,
218         };
219     }
220     close($fh);
221
222     # sort in descending order by datestamp, sorting on position, too, to ensure
223     # that entries with the same datestamp stay in the right order
224     $self->{'tles'} = \@tles;
225     $self->_update_positions();
226     @tles = sort {
227            $b->{'datestamp'} cmp $a->{'datestamp'}
228         || $a->{'position'} <=> $b->{'position'}
229         } @tles;
230
231     $self->{'tles'} = \@tles;
232
233     # and re-calculate the positions
234     $self->_update_positions(\@tles);
235 }
236
237 # update the 'position' key for each TLE
238 sub _update_positions {
239     my $self = shift;
240     my $tles = $self->{'tles'};
241     for (my $i = 0; $i < scalar @$tles; $i++) {
242         $tles->[$i]->{'position'} = $i+1;
243     }
244 }
245
246 %}
247
248 char *get_last_reusable_tape_label(int skip);
249 char *list_new_tapes(int nb);
250
251 /* C functions -- should be called *only* from within this module */
252
253 %rename(C_read_tapelist) read_tapelist;
254 int read_tapelist(char *tapefile);
255
256 %rename(C_clear_tapelist) clear_tapelist;
257 void clear_tapelist(void);