This file is indexed.

/usr/lib/amanda/perl/Amanda/Holding.pm is in amanda-common 1:3.3.3-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Copyright (c) 2009-2012 Zmanda, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
# Sunnyvale, CA 94085, USA, or: http://www.zmanda.com

package Amanda::Holding;

use base qw( Exporter );
use File::Spec;
use File::stat;
use IO::Dir;
use POSIX qw( :fcntl_h );
use Math::BigInt;
use strict;
use warnings;

use Amanda::Config qw( :getconf );
use Amanda::Debug qw( debug );
use Amanda::Header;
use Amanda::Disklist;
use Amanda::Util;

=head1 NAME

Amanda::Holding -- interface to the holding disks

=head1 SYNOPSIS

    use Amanda::Holding;

Get some statistics:

    my %size_per_host;
    for my $hfile (Amanda::Holding::files()) {
	my $hdr = Amanda::Holding::get_header($hfile);
	next unless $hdr;
	$size_per_host{$hdr->{'name'}} += Amanda::Holding::file_size($hfile);
    }

Schematic for something like C<amflush>:

    for my $ts (sort Amanda::Holding::get_all_timestamps()) {
	print $ts, "\n";
    }
    my @to_dump = <>;
    for my $hfile (Amanda::Holding::get_files_for_flush(@to_dump)) {
	# flush $hfile
    }

=head1 DESCRIPTION

=head2 TERMINOLOGY

=over

=item Holding disk

A holding disk is a directory given in a holdingdisk definition in
C<amanda.conf>.

=item Holding directory

A holding directory is a subdirectory of a holding disk, generally named by
timestamp.  Note, however, that this package does not interpret holding
directory names as timestamps, and does not provide direct access to holding
directories.

=item Holding file

A holding file describes one or more os-level files (holding file chunks) in a
holding directory, together representing a single dump file.

=item Holding chunk

A holding chunk is an individual os-level file representing part of a holding
file.  Chunks are kept small to avoid hitting filesystem size ilmits, and are
linked together internally by filename.

=back

By way of example:

  /data/holding                               <-- holding disk
  /data/holding/20070306123456                <-- holding directory
  /data/holding/20070306123456/raj._video_a   <-- holding file and chunk
  /data/holding/20070306123456/raj._video_a.1 <-- holding chunk

=head2 CONSTANTS

Holding-disk files do not have a block size, so the size of the header is fixed
at 32k.  Rather than hard-code that value, use the constant DISK_BLOCK_BYTES
from this package.

=head2 FUNCTIONS

Note that this package assumes that a config has been loaded (see
L<Amanda::Config>).

These three functions provide basic access to holding disks, files, and chunks:

=over

=item C<disks()>

returns an list of active disks, each represented as a string.  This does not
return holding disks which are defined in C<amanda.conf> but not used.

=item C<files()>

returns a list of active holding files on all disks.  Note that a dump may span
multiple disks, so there is no use in selecting files only on certain holding
disks.

=item C<file_chunks($file)>

returns a list of chunks for the given file.  Chunk filenames are always fully
qualified pathnames.

=back

C<Amanda::Holding> provides a few utility functions on holding files.  Note
that these functions require fully qualified pathnames.

=over

=item C<file_size($file, $ignore_headers)>

returns the size of the holding file I<in kilobytes>, ignoring the size of the
headers if C<$ignore_headers> is true.

=item C<file_unlink($file)>

unlinks (deletes) all chunks comprising C<$file>, returning true on success.

=item C<get_header($file)>

reads and returns the header (see L<Amanda::Header>) for C<$file>.

=back

The remaining two functions are utilities for amflush and related tools:

=over

=item C<get_all_timestamps()>

returns a sorted list of all timestamps with dumps in any active holding disk.

=item C<get_files_for_flush(@timestamps)>

returns a sorted list of files matching any of the supplied timestamps.  Files
for which no DLE exists in the disklist are ignored.  If no timestamps are
provided, then all timestamps are considered.

=back

=cut

use constant DISK_BLOCK_BYTES => 32768;

our @EXPORT_OK = qw(dirs files file_chunks
    get_files_for_flush get_all_datestamps
    file_size file_unlink get_header);

##
# utility subs

sub _is_datestr {
    my ($str) = @_;

    return 0
	unless (my ($year, $month, $day, $hour, $min, $sec) =
	    ($str =~ /(\d{4})(\d{2})(\d{2})(?:(\d{2})(\d{2})(\d{2}))/));

    return 0 if ($year < 1990 || $year > 2999);
    return 0 if ($month < 1 || $month > 12);
    return 0 if ($day < 1 || $day > 31);

    return 0 if (defined $hour and $hour > 23);
    return 0 if (defined $min and $min > 60);
    return 0 if (defined $sec and $sec > 60);

    return 1;
}

sub _walk {
    my ($file_fn, $verbose) = @_;

    # walk disks, directories, and files with nested loops
    for my $disk (disks()) {
	my $diskh = IO::Dir->new($disk);
	if (!defined $diskh) {
	    print $verbose "could not open holding dir '$disk': $!\n" if $verbose;
	    next;
	}

	while (defined(my $datestr = $diskh->read())) {
	    next if $datestr eq '.' or $datestr eq '..';

	    my $dirfn = File::Spec->catfile($disk, $datestr);

	    if (!_is_datestr($datestr)) {
		print $verbose "holding dir '$dirfn' is not a datestamp\n" if $verbose;
		next;
	    }
	    if (!-d $dirfn) {
		print $verbose "holding dir '$dirfn' is not a directory\n" if $verbose;
		next;
	    }

	    my $dirh = IO::Dir->new($dirfn);
	    while (defined(my $dirent = $dirh->read)) {
		next if $dirent eq '.' or $dirent eq '..';

		my $filename = File::Spec->catfile($disk, $datestr, $dirent);
		if (!-f $filename) {
		    print $verbose "holding file '$filename' is not a file\n" if $verbose;
		    next;
		}

		my $hdr = get_header($filename);
		next unless defined($hdr);

		$file_fn->($filename, $hdr);
	    }
	}
    }
}

##
# Package functions

sub disks {
    my @results;

    for my $hdname (@{getconf($CNF_HOLDINGDISK)}) {
	my $cfg = lookup_holdingdisk($hdname);
	next unless defined $cfg;

	my $dir = holdingdisk_getconf($cfg, $HOLDING_DISKDIR);
	next unless defined $dir;
	next unless -d $dir;
	push @results, $dir;
    }

    return @results;
}

sub files {
    my $verbose = shift;
    my @results;

    my $each_file_fn = sub {
	my ($filename, $header) = @_;
	return if $header->{'type'} != $Amanda::Header::F_DUMPFILE;

	push @results, $filename;
    };
    _walk($each_file_fn, $verbose);

    return @results;
}

sub all_files {
    my $verbose = shift;
    my @results;

    my $each_file_fn = sub {
	my ($filename, $header) = @_;
	push @results, { filename => $filename, header => $header };
    };
    _walk($each_file_fn, $verbose);

    return @results;
}

sub merge_all_files {
    my @files = @_;
    my %hfiles;
    my @result;

    for my $file (@files) {
	$hfiles{$file->{'filename'}} = $file->{'header'};
    }

    foreach my $filename (keys %hfiles) {
	next if !exists $hfiles{$filename};
	if ($hfiles{$filename}->{'type'} == $Amanda::Header::F_DUMPFILE) {
	    push @result, {filename => $filename, header => $hfiles{$filename}};
	    my $is_tmp = ($filename =~ /\.tmp$/);
	    my $cont_filename = $filename;
	    my $cfilename = $hfiles{$cont_filename}->{'cont_filename'};
	    my $cf = $cfilename;
	    $cf .= ".tmp" if $is_tmp;
	    while (defined $cfilename && $cfilename ne "" && -f $cf) {
		delete $hfiles{$cont_filename};
		$cont_filename = $cf;
		$cfilename = $hfiles{$cont_filename}->{'cont_filename'};
		$cf = $cfilename;
		$cf .= ".tmp" if $is_tmp;
	    }
	    delete $hfiles{$cont_filename};
	} elsif ($hfiles{$filename}->{'type'} != $Amanda::Header::F_CONT_DUMPFILE) {
	    push @result, {filename => $filename, header => $hfiles{$filename}};
	    delete $hfiles{$filename}
        } else {
	   # do nothing for F_CONTFILE
        }
    }

    foreach my $filename (keys %hfiles) {
	next if !exists $hfiles{$filename};
	if ($hfiles{$filename}->{'type'} == $Amanda::Header::F_CONT_DUMPFILE) {
	    push @result, {filename => $filename, header => $hfiles{$filename}};
	} else {
	    delete $hfiles{$filename}
	}
    }
    return @result;
}

sub file_chunks {
    my ($filename) = @_;
    my @results;

    while (1) {
	last unless -f $filename;
	my $hdr = get_header($filename);
	last unless defined($hdr);

	push @results, $filename;

	if ($hdr->{'cont_filename'}) {
	    $filename = $hdr->{'cont_filename'};
	} else {
	    # no continuation -> we're done
	    last;
	}
    }

    return @results;
}

sub file_tmp_chunks {
    my ($filename) = @_;
    my @results;

    while (1) {
	last unless -f $filename;
	my $hdr = get_header($filename);
	last unless defined($hdr);

	push @results, $filename;

	if ($hdr->{'cont_filename'}) {
	    $filename = $hdr->{'cont_filename'} . ".tmp";
	} else {
	    # no continuation -> we're done
	    last;
	}
    }

    return @results;
}

sub rename_tmp {
    my ($filename) = shift;
    my ($complete) = shift;

    my @files = file_tmp_chunks($filename);
    while (my $tmp_filename = pop @files) {
	my $hdr = get_header($tmp_filename);
	if ($hdr->{'is_partial'} == 0 and $complete == 0) {
	    $hdr->{'is_partial'} = 1;
	    write_header($tmp_filename, $hdr);
	}
	my $hfilename = $tmp_filename;
	$hfilename =~ s/\.tmp$//;
	rename $tmp_filename, $hfilename;
    }

    return
}

sub get_header {
    my ($filename) = @_;
    return unless -f $filename;

    my $fd = POSIX::open($filename, O_RDONLY);
    return unless $fd;

    my $hdr_bytes = Amanda::Util::full_read($fd, DISK_BLOCK_BYTES);
    POSIX::close($fd);
    if (length($hdr_bytes) == 0) {
	my $hdr = Amanda::Header->new();
	$hdr->{'type'} = $Amanda::Header::F_EMPTY;
	return $hdr;
    } elsif (length($hdr_bytes) < DISK_BLOCK_BYTES) {
	my $hdr = Amanda::Header->new();
	$hdr->{'type'} = $Amanda::Header::F_UNKNOWN;
	return $hdr;
    }

    return Amanda::Header->from_string($hdr_bytes);
}

sub write_header {
    my $filename = shift;
    my $hdr = shift;

    return unless -f $filename;
    my $fd = POSIX::open($filename, O_RDWR);
    return unless $fd;
    my $header = $hdr->to_string(DISK_BLOCK_BYTES, DISK_BLOCK_BYTES);
    Amanda::Util::full_write($fd, $header, DISK_BLOCK_BYTES);
    POSIX::close($fd);
}

sub file_unlink {
    my ($filename) = @_;

    for my $chunk (file_chunks($filename)) {
	unlink($chunk) or return 0;
    }

    return 1;
}

sub filetmp_unlink {
    my ($filename) = @_;

    for my $chunk (filetmp_chunks($filename)) {
	unlink($chunk) or return 0;
    }

    return 1;
}

sub dir_unlink {
    # walk disks, directories, and files with nested loops
    for my $disk (disks()) {
	my $diskh = IO::Dir->new($disk);
	next unless defined $diskh;

	while (defined(my $datestr = $diskh->read())) {
	    next if $datestr eq '.' or $datestr eq '..';

	    my $dirfn = File::Spec->catfile($disk, $datestr);
	    next unless _is_datestr($datestr);
	    next unless -d $dirfn;
	    rmdir $dirfn;
	}
    }
}

sub file_size {
    my ($filename, $ignore_headers) = @_;
    my $total = Math::BigInt->new(0);

    for my $chunk (file_chunks($filename)) {
	my $sb = stat($chunk);
	my $size = Math::BigInt->new($sb->size);
	$size -= DISK_BLOCK_BYTES if $ignore_headers;
	$size = ($size + 1023) / 1024;

	$total += $size;
    }

    return $total;
}

sub get_files_for_flush {
    my (@dateargs) = @_;
    my @results;

    my $each_file_fn = sub {
	my ($filename, $header) = @_;
	return if $header->{'type'} != $Amanda::Header::F_DUMPFILE;

	if (@dateargs && !grep { $_ eq $header->{'datestamp'}; } @dateargs) {
	    return;
	}

	if (!Amanda::Disklist::get_disk($header->{'name'}, $header->{'disk'})) {
	    return;
	}

	push @results, $filename;
    };
    _walk($each_file_fn, 0);

    return sort @results;
}

sub get_all_datestamps {
    my %datestamps;

    my $each_file_fn = sub {
	my ($filename, $header) = @_;
	return if $header->{'type'} != $Amanda::Header::F_DUMPFILE;

	$datestamps{$header->{'datestamp'}} = 1;
    };
    _walk($each_file_fn, 0);

    return sort keys %datestamps;
}