xfs/194: actually check if we got the desired block size before proceeding
[xfstests-dev.git] / src / fill2attr
1 #!/usr/bin/perl -w
2 #
3 #  Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5
6 use strict;
7 use File::Basename;
8
9 # returns numbers with a normal distribution
10 sub normal {
11   my($mean) = $_[0];
12   my($stddev) = $_[1];
13   
14   my $x = -6.0;
15   for (my $i = 0; $i < 12; $i++) {
16     $x += rand;
17   }
18   
19   $x = $mean + $stddev * $x;
20   return $x;
21 }
22
23 # location of fill2
24 my $fill2="./src/fill2";
25
26 # for each file attach a random number of attributes
27 # each filled with a random amount of data
28 # attribute name is the checksum of the data stored within
29 # the attribute
30
31 my $status = 0;                 # return status
32 my $file;
33
34 while (<>) {
35   
36   chomp($file = $_);
37   die("Error: $0: $file not found\n") if ( ! -e $file);
38   
39   if ($0 =~ /fill2attr$/) {
40
41     # attach attributes to this file
42     my $num = abs(int(normal(3, 1)));
43     my $seed = 1;
44     my $verbose = 1;
45     my $tmp = "/tmp/fill2attr$$";
46     
47     for (my $i = 0; $i < $num; $i++) {
48       my $size = abs(int(normal(256, 200)));
49       my $cmd = "$fill2 -d nbytes=$size,linelength=72,seed=$seed -b 4k $tmp";
50       $cmd .= " > /dev/null 2>&1" if (! $verbose);
51       
52       if (system($cmd) != 0) {
53         die("Error $0: can't create $tmp\n");
54       }
55       
56       chomp($_ = `sum -r $tmp`);
57       my ($sum) = split(/\s+/);
58       system("cat $tmp | attr -s $sum $file > /dev/null");
59       system("rm $tmp");
60     }
61   }
62   elsif ($0 =~ /fill2attr_check/) {
63
64     # get the attributes for this file
65     my $cmd = "attr -q -l $file |";
66     open LIST, $cmd;
67     my @labels = <LIST>;
68     close LIST or die("Error listing attributes: $!");
69     chomp(@labels);
70
71     # check attribute contents
72     foreach my $label (@labels) {
73       my $sum;
74       ($sum) = split(/\s+/, `attr -q -g $label $file | sum -r`);
75       if ($sum ne $label) {
76         warn("Attribute \"$label\" does not match " .
77              "attribute contents for file $file\n");
78         $status = 1;
79       }
80     }
81   }
82 }
83
84 exit($status);