1eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
2  & eval 'exec perl -wS "$0" $argv:q'
3    if 0;
4# Determine whether two files have the same extents by comparing
5# the logical block numbers and lengths from filefrag -v for each.
6
7# Invoke like this:
8# This helper function, f, extracts logical block number and lengths.
9# f() { awk '/^ *[0-9]/ {printf "%d %d ",$2,NF<5?$NF:$5} END {print ""}'; }
10# { filefrag -v j1 | f; filefrag -v j2 | f; } | ./filefrag-extent-compare
11
12use warnings;
13use strict;
14(my $ME = $0) =~ s|.*/||;
15
16my @line = <>;
17my $n_lines = @line;
18$n_lines == 2
19  or die "$ME: expected exactly two input lines; got $n_lines\n";
20
21my @A = split ' ', $line[0];
22my @B = split ' ', $line[1];
23@A % 2 || @B % 2
24  and die "$ME: unexpected input: odd number of numbers; expected even\n";
25
26my @a;
27my @b;
28foreach my $i (0..@A/2-1) { $a[$i] = { L_BLK => $A[2*$i], LEN => $A[2*$i+1] } };
29foreach my $i (0..@B/2-1) { $b[$i] = { L_BLK => $B[2*$i], LEN => $B[2*$i+1] } };
30
31# Merge adjacent extents in array E.
32sub merge_extents($)
33{
34  my ($e) = @_;
35
36  my $i = 0;
37  while (1)
38    {
39      !defined $e->[$i+1]
40        and last;
41      $e->[$i]->{L_BLK} + $e->[$i]->{LEN} != $e->[$i+1]->{L_BLK}
42        and ++$i, next;
43
44      $e->[$i]->{LEN} += $e->[$i+1]->{LEN};
45      # Remove $e->[$i+1]
46      splice @$e, $i+1, 1;
47    }
48}
49
50merge_extents \@a;
51merge_extents \@b;
52
53@a == @b
54  or die "$ME: extent counts differ, even after adjustment\n";
55
56my $i = 0;
57while (defined $a[$i])
58  {
59    my $start_match = $a[$i]->{L_BLK} == $b[$i]->{L_BLK};
60    my $len_match = $a[$i]->{LEN} == $b[$i]->{LEN};
61    if ( ! ($start_match && ($len_match || $i == (@a - 1))))
62      {
63        # On XFS on Linux kernel 2.6.38, it was seen that the size of the
64        # last extent can vary, and can extend beyond the length of the file.
65        # So we ignore the length of the last extent, because if the
66        # file is the wrong length we'll get failures elsewhere.
67        die "$ME: differing extent:\n"
68          . "  [$i]=$a[$i]->{L_BLK} $a[$i]->{LEN}\n"
69            . "  [$i]=$b[$i]->{L_BLK} $b[$i]->{LEN}\n";
70      }
71    $i++;
72  }
73
74### Setup "GNU" style for perl-mode and cperl-mode.
75## Local Variables:
76## mode: perl
77## perl-indent-level: 2
78## perl-continued-statement-offset: 2
79## perl-continued-brace-offset: 0
80## perl-brace-offset: 0
81## perl-brace-imaginary-offset: 0
82## perl-label-offset: -2
83## perl-extra-newline-before-brace: t
84## perl-merge-trailing-else: nil
85## End:
86