Suppress error messages from Makefile when git-describe is not available.
[cumulus.git] / lbs-util
1 #!/usr/bin/perl -w
2 #
3 # lbs-util: Tool for managing LBS archives.
4 #
5 # Usage: lbs-util <repository> <cmd> <args>
6 #
7 # Available commands:
8 #   --list-snapshots
9 #   --list-segments
10 #   --verify-snapshot <snapshot>
11
12 use strict;
13 use LBS qw(parse_headers);
14 use Term::ReadPassword;
15
16 sub get_password {
17     return if exists $ENV{LBS_GPG_PASSPHRASE};
18     $ENV{LBS_GPG_PASSPHRASE} = read_password('Passphrase: ');
19 }
20
21 die "Too few arguments!\n" unless scalar(@ARGV) >= 2;
22 die "Must specify a repository!\n" unless -d $ARGV[0];
23
24 my $store = new LBS::Store $ARGV[0];
25 my $cmd = $ARGV[1];
26 my @args = @ARGV[2 .. $#ARGV];
27
28 if ($cmd eq "--list-snapshots") {
29     foreach ($store->list_snapshots()) {
30         print $_, "\n";
31     }
32 } elsif ($cmd eq "--list-segments") {
33     foreach ($store->list_segments()) {
34         print $_, "\n";
35     }
36 } elsif ($cmd eq "--verify-snapshot") {
37     get_password();
38
39     my $snapshot = $store->load_snapshot($args[0]);
40     my %info = parse_headers($snapshot);
41     print "Root: $info{Root}\n";
42
43     my $metadata = new LBS::MetadataParser $store, $info{Root};
44     while ((my %item = $metadata->get_item())) {
45         print $item{name}, "\n";
46         if ($item{type} eq '-') {
47             my $size = 0;
48             my $verifier = new LBS::ChecksumVerifier $item{checksum};
49             foreach (split /\s+/, $item{data}) {
50                 my $data = $store->load_ref($_);
51                 $verifier->add($data);
52                 $size += length($data);
53             }
54             if (!$verifier->verify() || $size != $item{size}) {
55                 fprintf STDERR "Verification failure for $item{name}\n";
56             }
57         }
58     }
59 } else {
60     die "Unknown command: $cmd\n";
61 }