+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, $comment)
+ = $line =~ m/^([0-9]*)\s([^\s]*)\s(reuse|no-reuse)\s*(?:\#(.*))?$/mx;
+ next if !defined $datestamp; # silently filter out bogus lines
+ push @tles, {
+ 'datestamp' => $datestamp,
+ 'label' => $label,
+ 'reuse' => ($reuse eq 'reuse'),
+ '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);
+}
+