+sub _take_lock {
+ my $self = shift;
+
+ if (!-e $self->{'lockname'}) {
+ open(my $fhl, ">>", $self->{'lockname'});
+ close($fhl);
+ }
+ my $fl = Amanda::Util::file_lock->new($self->{'lockname'});
+ while(($r = $fl->lock()) == 1) {
+ sleep(1);
+ }
+ if ($r == 0) {
+ $self->{'fl'} = $fl;
+ }
+}
+
+sub _read_tapelist {
+ my $self = shift;
+
+ my @tles;
+ open(my $fh, "<", $self->{'filename'}) or return $self;
+ while (my $line = <$fh>) {
+ my ($datestamp, $label, $reuse, $barcode, $meta, $blocksize, $comment)
+ = $line =~ m/^([0-9]+)\s*([^\s]*)\s*(?:(reuse|no-reuse))?\s*(?:BARCODE:([^\s]*))?\s*(?:META:([^\s]*))?\s*(?:BLOCKSIZE:([^\s]*))?\s*(?:\#(.*))?$/mx;
+ if (!defined $datestamp) {
+ Amanda::Debug::critical("Bogus line in the tapelist ($self->{'filename'}) file: $line");
+ }
+ push @tles, {
+ 'datestamp' => $datestamp,
+ 'label' => $label,
+ 'reuse' => (!defined $reuse || $reuse eq 'reuse'),
+ 'barcode' => $barcode,
+ 'meta' => $meta,
+ 'blocksize' => $blocksize,
+ 'comment' => $comment,
+ };
+ }
+ close($fh);
+
+ # sort in descending order by datestamp, sorting on position, too, to ensure
+ # that entries with the same datestamp stay in the right order
+ $self->{'tles'} = \@tles;
+ $self->_update_positions();
+ @tles = sort {
+ $b->{'datestamp'} cmp $a->{'datestamp'}
+ || $a->{'position'} <=> $b->{'position'}
+ } @tles;
+
+ $self->{'tles'} = \@tles;
+
+ # and re-calculate the positions
+ $self->_update_positions(\@tles);
+}
+