move helpers location to inside lib
[p5sagit/Distar.git] / lib / Distar / helpers / verify-tarball
CommitLineData
ee801c00 1#!/usr/bin/env perl
2use strict;
3use warnings FATAL => 'all';
4
5use ExtUtils::Manifest qw(maniread);
6use File::Spec;
7use Getopt::Long qw(:config gnu_getopt);
8
9GetOptions(
10 'tar=s' => \(my $tar = 'tar'),
11) or die("Error in command line arguments\n");
12
13my ($tarball, $manifest) = @ARGV;
14my $type
38513b0a 15 = $tarball =~ /\.bz2$/ ? '-j'
16 : $tarball =~ /\.xz$/ ? '-J'
17 : $tarball =~ /\.t?gz$/ ? '-z'
18 : $tarball =~ /\.Z$/ ? '-Z'
ee801c00 19 : '';
20
21my @tarfiles;
22{
23 my $null = File::Spec->devnull;
38513b0a 24 @tarfiles = `$tar -t $type -f "$tarball" 2>$null`;
ee801c00 25 chomp @tarfiles;
26}
27
28{
29 my $prefix;
30 for my $path (@tarfiles) {
31 if (!defined $prefix) {
32 ($prefix) = $path =~ m{^([^/]+)};
33 }
34 elsif ($path !~ m{^\Q$prefix\E(?:/|$)}) {
35 undef $prefix;
36 last;
37 }
38 }
39 if ($prefix) {
40 s{^\Q$prefix\E/}{} for @tarfiles;
41 }
42}
43
44@tarfiles = grep !m{(?:^|/)$}, @tarfiles;
45my %tarfiles = map +($_ => 1), @tarfiles;
46
47my %manifiles = %{ maniread($manifest) };
48
49my @extra = grep { !exists $manifiles{$_} } sort keys %tarfiles;
50my @missing = grep { !exists $tarfiles{$_} } sort keys %manifiles;
51
52my $message = '';
53if (@extra) {
54 $message .= "$tarball has extra files:\n" . join '', map " $_\n", @extra;
55}
56if (@missing) {
57 $message .= "$tarball is missing files:\n" . join '', map " $_\n", @missing;
58}
c3410697 59if ($message) {
60 die $message . <<'END_MESSAGE';
61
62This may happen if the file attributes are not compatible with the ustar format.
63END_MESSAGE
64}
ee801c00 65exit 0;