summaryrefslogtreecommitdiffstats
path: root/tests/filefrag-extent-compare
blob: 155e7ff781b64c8e7e8c07b0caeb68ec456dbe7e (plain)
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
eval '(exit $?0)' && eval 'exec perl -wS "$0" ${1+"$@"}'
  & eval 'exec perl -wS "$0" $argv:q'
    if 0;
# Determine whether two files have the same extents by comparing
# the logical block numbers and lengths from filefrag -v for each.

# Invoke like this:
# This helper function, f, extracts logical block number and lengths.
# f() { awk '/^ *[0-9]/ {printf "%d %d ",$2,NF<5?$NF:$5} END {print ""}'; }
# { filefrag -v j1 | f; filefrag -v j2 | f; } | ./filefrag-extent-compare

use warnings;
use strict;
(my $ME = $0) =~ s|.*/||;

my @line = <>;
my $n_lines = @line;
$n_lines == 2
  or die "$ME: expected exactly two input lines; got $n_lines\n";

my @A = split ' ', $line[0];
my @B = split ' ', $line[1];
@A % 2 || @B % 2
  and die "$ME: unexpected input: odd number of numbers; expected even\n";

my @a;
my @b;
foreach my $i (0..@A/2-1) { $a[$i] = { L_BLK => $A[2*$i], LEN => $A[2*$i+1] } };
foreach my $i (0..@B/2-1) { $b[$i] = { L_BLK => $B[2*$i], LEN => $B[2*$i+1] } };

# Merge adjacent extents in array E.
sub merge_extents($)
{
  my ($e) = @_;

  my $i = 0;
  while (1)
    {
      !defined $e->[$i+1]
        and last;
      $e->[$i]->{L_BLK} + $e->[$i]->{LEN} != $e->[$i+1]->{L_BLK}
        and ++$i, next;

      $e->[$i]->{LEN} += $e->[$i+1]->{LEN};
      # Remove $e->[$i+1]
      splice @$e, $i+1, 1;
    }
}

merge_extents \@a;
merge_extents \@b;

@a == @b
  or die "$ME: extent counts differ, even after adjustment\n";

my $i = 0;
while (defined $a[$i])
  {
    my $start_match = $a[$i]->{L_BLK} == $b[$i]->{L_BLK};
    my $len_match = $a[$i]->{LEN} == $b[$i]->{LEN};
    if ( ! ($start_match && ($len_match || $i == (@a - 1))))
      {
        # On XFS on Linux kernel 2.6.38, it was seen that the size of the
        # last extent can vary, and can extend beyond the length of the file.
        # So we ignore the length of the last extent, because if the
        # file is the wrong length we'll get failures elsewhere.
        die "$ME: differing extent:\n"
          . "  [$i]=$a[$i]->{L_BLK} $a[$i]->{LEN}\n"
            . "  [$i]=$b[$i]->{L_BLK} $b[$i]->{LEN}\n";
      }
    $i++;
  }

### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables:
## mode: perl
## perl-indent-level: 2
## perl-continued-statement-offset: 2
## perl-continued-brace-offset: 0
## perl-brace-offset: 0
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## perl-extra-newline-before-brace: t
## perl-merge-trailing-else: nil
## End: