Big filesystem testing update - skip the repair check for the moment, takes too long...
[xfstests-dev.git] / tools / srcdiff
1 #!/usr/bin/perl -w
2 use strict;
3
4 # srcdiff is used to compare current user level code with the current
5 # kernel code and advise of any differences between files which are
6 # sharing some or all of their content.
7
8 # There are two classes of sharing which we will check - header files
9 # in the include directory, which must be exactly the same (use diff)
10 # and source files which contain routines which must be exactly the
11 # same (but the userland file is always a subset of the kernel file,
12 # and hence a more flexible mechanism to "diff" is required).
13
14 # NB: to cross check that srcdiff is finding all the functions in the
15 #     user source file, providing you have "mkproto" installed, you
16 #     can "cd cmd/xfsprogs/libxfs" and cut&paste this in a bourne shell:
17 #     $ for file in xfs_*.c; do
18 #     > mkproto -nps < $file | perl -ne '
19 #     > END { print "    $count\t- " }
20 #     > s/^.* (xfs\w+|\*xfs\w+|xlog\w+|\*xlog\w+) \(.*/\1/ && { $count++ }'
21 #     > echo $file
22 #     > done
23 # (compare this to "srcdiff | fgrep Total:")
24
25
26 die "WORKAREA not set" unless defined $ENV{'WORKAREA'};
27 chdir $ENV{'WORKAREA'};
28 my $xdiff = $ENV{'XDIFF'};
29 my $quiet=0;
30 my $usage=0;
31
32 foreach (@ARGV) {
33         if (/^-q$/) {
34                 $quiet++;
35         } else {
36                 print STDERR "Illegal option $_\n";
37                 $usage++;
38         }
39 }
40
41 if ($usage) {
42     print STDERR "Usage: $0 [-q]\n";
43     exit 1;
44 }
45
46 my @pkglist = qw( xfstests attr acl dmapi xfsdump xfsprogs );
47 my @difflist = qw(
48         xfs_ag.h  xfs_alloc.h  xfs_alloc_btree.h xfs_arch.h
49         xfs_attr_leaf.h  xfs_attr_sf.h  xfs_bit.h  xfs_bmap.h
50         xfs_bmap_btree.h  xfs_btree.h  xfs_buf_item.h
51         xfs_da_btree.h  xfs_dfrag.h  xfs_dinode.h  xfs_dir.h
52         xfs_dir2.h  xfs_dir2_block.h  xfs_dir2_data.h
53         xfs_dir2_leaf.h  xfs_dir2_node.h  xfs_dir2_sf.h
54         xfs_dir_leaf.h  xfs_dir_sf.h  xfs_extfree_item.h  xfs_ialloc.h
55         xfs_imap.h  xfs_ialloc_btree.h  xfs_inode.h  xfs_inode_item.h
56         xfs_inum.h  xfs_log.h  xfs_log_priv.h  xfs_log_recover.h
57         xfs_mount.h  xfs_quota.h  xfs_rtalloc.h
58         xfs_sb.h  xfs_trans.h  xfs_trans_space.h  xfs_types.h
59         xfs_fs.h xfs_acl.h xfs_cap.h xfs_mac.h
60 );
61
62 sub straightdiff {
63         my ( $file, $prefix1, $prefix2 ) = @_;
64
65         `diff $prefix1/$file $prefix2/$file >/dev/null 2>&1`;
66         if (!$quiet) {
67                 print sprintf("\t%-35s ... ", $file);
68                 if ($? != 0)    { printf("FAILED\n(%s/%s differs to %s/%s)\n",
69                                          $prefix1, $file, $prefix2, $file); }
70                 else            { print "ok\n"; }
71         
72         } elsif ($? != 0) {
73                 printf("\t%-35s ... FAILED\n(%s/%s differs to %s/%s)\n",
74                         $file, $prefix1, $file, $prefix2, $file);
75         }
76 }
77
78 #
79 # xfstests directory m4 directory is a repository of all of the
80 # custom m4 macros used in the packages we look after.
81 #
82 sub m4macrodiff {
83         my ( $package ) = @_;
84
85         foreach (`ls cmd/$package/m4/*.m4`) {
86                 my $m4 = `basename $_`;
87                 chomp($m4);
88                 straightdiff $m4, "cmd/$package/m4", "cmd/xfstests/m4";
89         }
90 }
91
92 my $first = shift @pkglist;
93 foreach (@pkglist) {
94         print "\n=== Checking $_ package ===\n";
95         m4macrodiff $_;
96         straightdiff 'buildrules', "cmd/$first/include", "cmd/$_/include";
97         straightdiff 'buildmacros', "cmd/$first/include", "cmd/$_/include";
98         straightdiff 'Makefile', "cmd/$first/build", "cmd/$_/build";
99         straightdiff 'Makefile', "cmd/$first/build/rpm", "cmd/$_/build/rpm";
100         straightdiff 'Makefile', "cmd/$first/build/tar", "cmd/$_/build/tar";
101 }
102 print "\n=== Checking headers ===\n";
103 foreach (@difflist) {
104         straightdiff $_, 'cmd/xfsprogs/include', 'linux/fs/xfs';
105 }
106 straightdiff 'dmapi_kern.h', 'cmd/dmapi/include', 'linux/fs/xfs/dmapi';
107 straightdiff 'dmapi.h', 'cmd/dmapi/include', 'linux/fs/xfs/dmapi';
108
109
110 # setstate
111 # Implements a tri-state FSA, see comments for state transitions
112 #  (knows about the way the XFS kernel code is written, & makes
113 #   some assumptions so as to not need to parse generic C code).
114 # Accepts one line at a time from a source file, picking out the
115 #   function bodies so they can be subsequently compared.
116
117
118 my $line;       # line number in current source file
119 my $state;      # current FSA state
120 my $funcbody;   # current function body (contents)
121
122 sub setstate {
123         my ( $newline ) = @_;
124         $line++;
125
126         # - state 0:
127         #       if line looks like start of a function, transition to 1
128         #               & squirrel line away as line 1 of current function
129         if ($state == 0) {
130                 if ($newline =~ m/^[xfs|xlog]/) {
131                         $state = 1;
132                         $funcbody = $newline;
133                 }
134         }
135
136         # - state 1:
137         #       if line looks like start of a function, stay here
138         #               & squirrel line away as line 1 of current function
139         #       otherwise if line isn't start of function body,
140         #               squirrel line away as next line of current function
141         #               (args/..., but not sure this is a real function yet)
142         #       otherwise (start of function)
143         #               squirrel line away as next line of current function
144         #               transition to state 2
145         elsif ($state == 1) {
146                 if ($newline =~ m/^[xfs|xlog]/) {
147                         $funcbody = $newline;
148                 }
149                 elsif ($newline =~ m/^\{/) {
150                         $state = 2;
151                         $funcbody .= $newline;
152                 }
153         }
154
155         # - state 2:
156         #       if line looks like end of function body,
157         #               squirrel line away as last line of current function
158         #               tell someone we have a complete function ready
159         #               transition to state 0
160         #       otherwise
161         #               squirrel line away as next line of current function
162         elsif ($state == 2) {
163                 $funcbody .= $newline;
164                 if ($newline =~ m/^\}/) {
165                         $state = 0;
166                         return $funcbody;
167                 }
168         }
169
170         else {
171                 die "unknown state transition";
172         }
173         return undef;   # i.e. not at end of a function
174 }
175
176 sub listfuncs {
177         my ( $file ) = @_;
178         my @funcs;
179
180         $funcbody = '';
181         $state = $line = 0;
182
183         open(USER, "$file") || die "cannot open $file";
184         while (<USER>) {
185                 my $func = setstate($_);
186                 push @funcs, $func if (defined($func)); # store function away
187         }
188         close USER;
189         return @funcs;
190 }
191
192 sub hashfuncs {
193         my ( $file ) = @_;
194         my %funcs;
195
196         $funcbody = '';
197         $state = $line = 0;
198
199         open(KERN, "$file") || die "cannot open $file";
200         while (<KERN>) {
201                 my $func = setstate($_);
202                 if (defined($func)) {
203                         $func =~ m/^([xfs|xlog]\w+)\s*\(/;
204                         next unless defined($1);
205                         my $name = $1;
206                         if (defined($func)) {
207                                 $funcs{$name} = $func;  # store function away
208                         }
209                 }
210         }
211         close KERN;
212         return %funcs;
213 }
214
215 sub diffme {
216         my ( $sa, $sb ) = @_;
217
218         return unless defined($xdiff);
219
220         open(FILEA, "> /tmp/diff.user.$$") || die "cannot write to /tmp/diff.user.$$";
221         open(FILEB, "> /tmp/diff.kern.$$") || die "cannot write to /tmp/diff.kern.$$";
222         print FILEA $sa;
223         print FILEB $sb;
224         close FILEA;
225         close FILEB;
226         `$xdiff /tmp/diff.user.$$ /tmp/diff.kern.$$`;
227         unlink ("/tmp/diff.user.$$","/tmp/diff.kern.$$");
228 }
229
230 sub functiondiff {
231         my ( $file, $prefix1, $prefix2 ) = @_;
232         my $plural = '';
233         my $count = 0;
234         my $name;
235         my $found = 0;
236
237         print "\n=== Checking $file routines ===\n" unless ($quiet);
238
239         # iterate over user funcs, match up to kernel funcs
240         # 
241         my @user = listfuncs "$prefix1/$file";
242         my %kern = hashfuncs "$prefix2/$file";
243
244         foreach my $userfunc (@user) {
245                 
246                 $userfunc =~ m/^([xfs|xlog]\w+)\s*\(/;
247                 next unless (defined($1));
248                 $name = $1;
249                 $count++;
250
251                 if (exists($kern{$name})) {
252                         if ($userfunc ne $kern{$name}) {
253                                 print "\n=== $file routines ===\n"
254                                     if (!$found++ && $quiet);
255                                     
256                                 printf("\t%-35s ... ", $name);
257                                 print "FAILED\n";
258                                 diffme $userfunc, $kern{$name};
259                         }
260                         elsif (!$quiet) {
261                                 printf("\t%-35s ... ", $name);
262                                 print "ok\n";
263                         }
264                 }
265                 else {
266                         print "Cannot find kernel function $userfunc";
267                         print " in file $prefix2/$file\n";
268                 }
269         }
270         ($count != 1) && ( $plural = 's' );
271         print "( Total: $count routine$plural checked in $file )\n" unless ($quiet);
272 }
273
274 # cmd/xfsprogs/{libxfs,libxlog}/* fs/xfs/*
275 my @funclist = qw(
276         xfs_alloc.c  xfs_alloc_btree.c  xfs_attr_leaf.c
277         xfs_bmap.c  xfs_bmap_btree.c  xfs_btree.c  xfs_da_btree.c
278         xfs_dir.c  xfs_dir2.c  xfs_dir2_block.c  xfs_dir2_data.c
279         xfs_dir2_leaf.c  xfs_dir2_node.c  xfs_dir2_sf.c
280         xfs_dir_leaf.c  xfs_ialloc.c  xfs_ialloc_btree.c
281         xfs_inode.c  xfs_rtalloc.c  xfs_mount.c  xfs_trans.c
282 );
283
284 print "\n=== Checking libxfs code ===\n";
285 foreach (@funclist) {
286         functiondiff $_, 'cmd/xfsprogs/libxfs', 'linux/fs/xfs';
287 }
288 print "\n=== Checking libxlog code ===\n";
289 functiondiff 'xfs_log_recover.c', 'cmd/xfsprogs/libxlog', 'linux/fs/xfs';