Update copyright dates
[xfstests-dev.git] / src / fill2attr
1 #!/usr/bin/perl -w
2 #
3
4 #
5 #  Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
6 #  
7 #  This program is free software; you can redistribute it and/or modify it
8 #  under the terms of version 2 of the GNU General Public License as
9 #  published by the Free Software Foundation.
10 #  
11 #  This program is distributed in the hope that it would be useful, but
12 #  WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 #  
15 #  Further, this software is distributed without any warranty that it is
16 #  free of the rightful claim of any third person regarding infringement
17 #  or the like.  Any license provided herein, whether implied or
18 #  otherwise, applies only to this software file.  Patent licenses, if
19 #  any, provided herein do not apply to combinations of this program with
20 #  other software, or any other product whatsoever.
21 #  
22 #  You should have received a copy of the GNU General Public License along
23 #  with this program; if not, write the Free Software Foundation, Inc., 59
24 #  Temple Place - Suite 330, Boston MA 02111-1307, USA.
25 #  
26 #  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 #  Mountain View, CA  94043, or:
28 #  
29 #  http://www.sgi.com 
30 #  
31 #  For further information regarding this notice, see: 
32 #  
33 #  http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #
35 #
36
37 #
38 # fill2attr:
39 #
40 #   $Id$
41 #
42
43 use File::Basename;
44
45
46 # returns numbers with a normal distribution
47 sub normal {
48   my($mean) = $_[0];
49   my($stddev) = $_[1];
50   
51   $x = -6.0;
52   for (my $i = 0; $i < 12; $i++) {
53     $x += rand;
54   }
55   
56   $x = $mean + $stddev * $x;
57   return $x;
58 }
59
60
61 #
62 # determine script location and find fill2
63 #
64 chomp($cwd = `pwd`);
65 chomp($_ = `which fill2 2>&1 | head -1`);
66 if (-x $_) {
67   # look in the path
68   $fill2 = fill2;
69 }
70 else {
71   # in the same directory - get absolute path
72   chomp($dirname = dirname $0);
73   if ($dirname =~ m!^/.*!) {
74     $fill2 = $dirname . "/fill2";
75   }
76   else {
77     # relative
78     $fill2 = $cwd . "/" . $dirname . "/fill2";
79   }    
80   if (! -x $fill2) {
81     die("Error: $0: can't find fill2, tried \"$fill2\"\n");
82   }
83 }
84
85
86 # for each file attach a random number of attributes
87 # each filled with a random amount of data
88 # attribute name is the checksum of the data stored within
89 # the attribute
90
91 $status = 0;                    # return status
92
93 while (<>) {
94   
95   chomp($file = $_);
96   die("Error: $0: $file not found\n")
97     if ( ! -e $file);
98   
99   if ($0 =~ /fill2attr$/) {
100
101     # attach attributes to this file
102     my $num = abs(int(normal(3, 1)));
103     my $seed = 1;
104     my $verbose = 1;
105     my $tmp = "/tmp/fill2attr$$";
106     
107     for (my $i = 0; $i < $num; $i++) {
108       my $size = abs(int(normal(256, 200)));
109       my $cmd = "$fill2 -d nbytes=$size,linelength=72,seed=$seed -b 4k $tmp";
110       $cmd .= " > /dev/null 2>&1" if (! $verbose);
111       
112       if (system($cmd) != 0) {
113         die("Error $0: can't create $tmp\n");
114       }
115       
116       chomp($_ = `sum -r $tmp`);
117       ($sum) = split(/\s+/);
118       if (system("cat $tmp | attr -s $sum $file > /dev/null 2>&1") != 0) {
119         die("Error $0: could not attach attribute:\n" . `cat $tmp` . "\n");
120       }
121     }
122   }
123   elsif ($0 =~ /fill2attr_check/) {
124
125     # get the attributes for this file
126     $cmd = "attr -q -l $file |";
127     open LIST, $cmd;
128     @labels = <LIST>;
129     close LIST or die("Error listing attributes: $!");
130     chomp(@labels);
131
132     # check attribute contents
133     foreach my $label (@labels) {
134       ($sum) = split(/\s+/, `attr -q -g $label $file | sum -r`);
135       if ($sum ne $label) {
136         warn("Attribute \"$label\" does not match " .
137              "attribute contents for file $file\n");
138         $status = 1;
139       }
140     }
141   }
142 }
143
144 exit($status);