Imported Upstream version 3.2.0
[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) = @_;
128     $reuse = 1 if !defined $reuse;
129
130     # prepend this (presumably new) volume to the beginning of the list
131     unshift @{$self->{'tles'}}, {
132         'datestamp' => $datestamp,
133         'label' => $label,
134         'reuse' => $reuse,
135         'comment' => $comment,
136     };
137     $self->_update_positions();
138 }
139
140 sub write {
141     my $self = shift;
142     my ($filename) = @_;
143     my $result = TRUE;
144     $filename = $self->{'filename'} if !defined $filename;
145
146     my $new_tapelist_file = $filename . "-new-" . time();
147
148     open(my $fhn, ">", $new_tapelist_file) or die("Could not open '$new_tapelist_file' for writing: $!");
149     for my $tle (@{$self->{tles}}) {
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         $result &&= print $fhn "$datestamp $label $reuse$comment\n";
155     }
156     my $result_close = close($fhn);
157     $result &&= $result_close;
158
159     return if (!$result);
160
161     unless (move($new_tapelist_file, $filename)) {
162         die ("failed to rename '$new_tapelist_file' to '$filename': $!");
163     }
164
165     # re-read from the C side to synchronize
166     C_read_tapelist($filename);
167
168     $self->unlock();
169
170     return undef;
171 }
172
173 sub unlock {
174     my $self = shift;
175
176     return if !exists $self->{'fl'};
177
178     $self->{'fl'}->unlock();
179     delete $self->{'fl'}
180 }
181
182 ## private methods
183
184 sub _take_lock {
185     my $self = shift;
186
187     if (!-e $self->{'lockname'}) {
188         open(my $fhl, ">>", $self->{'lockname'});
189         close($fhl);
190     }
191     my $fl = Amanda::Util::file_lock->new($self->{'lockname'});
192     while(($r = $fl->lock()) == 1) {
193         sleep(1);
194     }
195     if ($r == 0) {
196         $self->{'fl'} = $fl;
197     }
198 }
199
200 sub _read_tapelist {
201     my $self = shift;
202
203     my @tles;
204     open(my $fh, "<", $self->{'filename'}) or return $self;
205     while (my $line = <$fh>) {
206         my ($datestamp, $label, $reuse, $comment)
207             = $line =~ m/^([0-9]*)\s([^\s]*)\s(reuse|no-reuse)\s*(?:\#(.*))?$/mx;
208         next if !defined $datestamp; # silently filter out bogus lines
209         push @tles, {
210             'datestamp' => $datestamp,
211             'label' => $label,
212             'reuse' => ($reuse eq 'reuse'),
213             'comment' => $comment,
214         };
215     }
216     close($fh);
217
218     # sort in descending order by datestamp, sorting on position, too, to ensure
219     # that entries with the same datestamp stay in the right order
220     $self->{'tles'} = \@tles;
221     $self->_update_positions();
222     @tles = sort {
223            $b->{'datestamp'} cmp $a->{'datestamp'}
224         || $a->{'position'} <=> $b->{'position'}
225         } @tles;
226
227     $self->{'tles'} = \@tles;
228
229     # and re-calculate the positions
230     $self->_update_positions(\@tles);
231 }
232
233 # update the 'position' key for each TLE
234 sub _update_positions {
235     my $self = shift;
236     my $tles = $self->{'tles'};
237     for (my $i = 0; $i < scalar @$tles; $i++) {
238         $tles->[$i]->{'position'} = $i+1;
239     }
240 }
241
242 %}
243
244 char *get_last_reusable_tape_label(int skip);
245 char *list_new_tapes(int nb);
246
247 /* C functions -- should be called *only* from within this module */
248
249 %rename(C_read_tapelist) read_tapelist;
250 int read_tapelist(char *tapefile);
251
252 %rename(C_clear_tapelist) clear_tapelist;
253 void clear_tapelist(void);