remove no longer needed xfs_bit.c file.
[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( 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_dqblk.h  xfs_dquot_item.h
55         xfs_extfree_item.h  xfs_ialloc.h  xfs_imap.h
56         xfs_ialloc_btree.h  xfs_inode.h  xfs_inode_item.h
57         xfs_inum.h  xfs_log.h  xfs_log_priv.h  xfs_log_recover.h
58         xfs_mount.h  xfs_quota.h  xfs_rtalloc.h
59         xfs_sb.h  xfs_trans.h  xfs_trans_space.h  xfs_types.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)    { print "FAILED\n"; }
69                 else            { print "ok\n"; }
70         } elsif ($? != 0) { 
71                 printf("\t%-35s ... ", $file);
72                 print "FAILED\n"; 
73         }
74 }
75
76 my $first = shift @pkglist;
77 foreach (@pkglist) {
78         print "\n=== Checking $_ package ===\n";
79         straightdiff 'buildrules', "cmd/$first/include", "cmd/$_/include";
80         straightdiff 'buildmacros', "cmd/$first/include", "cmd/$_/include";
81 }
82 print "\n=== Checking headers ===\n";
83 foreach (@difflist) {
84         straightdiff $_, 'cmd/xfsprogs/include', 'linux/fs/xfs';
85 }
86 straightdiff 'xfs_cred.h', 'cmd/xfsprogs/include', 'linux/fs/xfs/linux';
87 straightdiff 'xfs_fs.h', 'cmd/xfsprogs/include', 'linux/include/linux';
88 straightdiff 'dmapi_kern.h', 'cmd/dmapi/include', 'linux/include/linux';
89 straightdiff 'dmapi.h', 'cmd/dmapi/include', 'linux/include/linux';
90 straightdiff 'arch.h', 'cmd/xfsprogs/include', 'linux/fs/xfs/support';
91 straightdiff 'quotaio_xfs.h', 'cmd/xfsdump/include', 'linux/include/linux';
92
93
94 # setstate
95 # Implements a tri-state FSA, see comments for state transitions
96 #  (knows about the way the XFS kernel code is written, & makes
97 #   some assumptions so as to not need to parse generic C code).
98 # Accepts one line at a time from a source file, picking out the
99 #   function bodies so they can be subsequently compared.
100
101
102 my $line;       # line number in current source file
103 my $state;      # current FSA state
104 my $funcbody;   # current function body (contents)
105
106 sub setstate {
107         my ( $newline ) = @_;
108         $line++;
109
110         # - state 0:
111         #       if line looks like start of a function, transition to 1
112         #               & squirrel line away as line 1 of current function
113         if ($state == 0) {
114                 if ($newline =~ m/^[xfs|xlog]/) {
115                         $state = 1;
116                         $funcbody = $newline;
117                 }
118         }
119
120         # - state 1:
121         #       if line looks like start of a function, stay here
122         #               & squirrel line away as line 1 of current function
123         #       otherwise if line isn't start of function body,
124         #               squirrel line away as next line of current function
125         #               (args/..., but not sure this is a real function yet)
126         #       otherwise (start of function)
127         #               squirrel line away as next line of current function
128         #               transition to state 2
129         elsif ($state == 1) {
130                 if ($newline =~ m/^[xfs|xlog]/) {
131                         $funcbody = $newline;
132                 }
133                 elsif ($newline =~ m/^\{/) {
134                         $state = 2;
135                         $funcbody .= $newline;
136                 }
137         }
138
139         # - state 2:
140         #       if line looks like end of function body,
141         #               squirrel line away as last line of current function
142         #               tell someone we have a complete function ready
143         #               transition to state 0
144         #       otherwise
145         #               squirrel line away as next line of current function
146         elsif ($state == 2) {
147                 $funcbody .= $newline;
148                 if ($newline =~ m/^\}/) {
149                         $state = 0;
150                         return $funcbody;
151                 }
152         }
153
154         else {
155                 die "unknown state transition";
156         }
157         return undef;   # i.e. not at end of a function
158 }
159
160 sub listfuncs {
161         my ( $file ) = @_;
162         my @funcs;
163
164         $funcbody = '';
165         $state = $line = 0;
166
167         open(USER, "$file") || die "cannot open $file";
168         while (<USER>) {
169                 my $func = setstate($_);
170                 push @funcs, $func if (defined($func)); # store function away
171         }
172         close USER;
173         return @funcs;
174 }
175
176 sub hashfuncs {
177         my ( $file ) = @_;
178         my %funcs;
179
180         $funcbody = '';
181         $state = $line = 0;
182
183         open(KERN, "$file") || die "cannot open $file";
184         while (<KERN>) {
185                 my $func = setstate($_);
186                 if (defined($func)) {
187                         $func =~ m/^([xfs|xlog]\w+)\s*\(/;
188                         next unless defined($1);
189                         my $name = $1;
190                         if (defined($func)) {
191                                 $funcs{$name} = $func;  # store function away
192                         }
193                 }
194         }
195         close KERN;
196         return %funcs;
197 }
198
199 sub diffme {
200         my ( $sa, $sb ) = @_;
201
202         return unless defined($xdiff);
203
204         open(FILEA, "> /tmp/diff.user.$$") || die "cannot write to /tmp/diff.user.$$";
205         open(FILEB, "> /tmp/diff.kern.$$") || die "cannot write to /tmp/diff.kern.$$";
206         print FILEA $sa;
207         print FILEB $sb;
208         close FILEA;
209         close FILEB;
210         `$xdiff /tmp/diff.user.$$ /tmp/diff.kern.$$`;
211         unlink ("/tmp/diff.user.$$","/tmp/diff.kern.$$");
212 }
213
214 sub functiondiff {
215         my ( $file, $prefix1, $prefix2 ) = @_;
216         my $plural = '';
217         my $count = 0;
218         my $name;
219         my $found = 0;
220
221         print "\n=== Checking $file routines ===\n" unless ($quiet);
222
223         # iterate over user funcs, match up to kernel funcs
224         # 
225         my @user = listfuncs "$prefix1/$file";
226         my %kern = hashfuncs "$prefix2/$file";
227
228         foreach my $userfunc (@user) {
229                 
230                 $userfunc =~ m/^([xfs|xlog]\w+)\s*\(/;
231                 next unless (defined($1));
232                 $name = $1;
233                 $count++;
234
235                 if (exists($kern{$name})) {
236                         if ($userfunc ne $kern{$name}) {
237                                 print "\n=== $file routines ===\n"
238                                     if (!$found++ && $quiet);
239                                     
240                                 printf("\t%-35s ... ", $name);
241                                 print "FAILED\n";
242                                 diffme $userfunc, $kern{$name};
243                         }
244                         elsif (!$quiet) {
245                                 printf("\t%-35s ... ", $name);
246                                 print "ok\n";
247                         }
248                 }
249                 else {
250                         print "Cannot find kernel function $userfunc";
251                         print " in file $prefix2/$file\n";
252                 }
253         }
254         ($count != 1) && ( $plural = 's' );
255         print "( Total: $count routine$plural checked in $file )\n" unless ($quiet);
256 }
257
258 # cmd/xfsprogs/{libxfs,libxlog}/* fs/xfs/*
259 my @funclist = qw(
260         xfs_alloc.c  xfs_alloc_btree.c  xfs_attr_leaf.c
261         xfs_bmap.c  xfs_bmap_btree.c  xfs_btree.c  xfs_da_btree.c
262         xfs_dir.c  xfs_dir2.c  xfs_dir2_block.c  xfs_dir2_data.c
263         xfs_dir2_leaf.c  xfs_dir2_node.c  xfs_dir2_sf.c
264         xfs_dir_leaf.c  xfs_ialloc.c  xfs_ialloc_btree.c
265         xfs_inode.c  xfs_rtalloc.c  xfs_rtbit.c xfs_mount.c
266         xfs_trans.c
267 );
268
269 print "\n=== Checking libxfs code ===\n";
270 foreach (@funclist) {
271         functiondiff $_, 'cmd/xfsprogs/libxfs', 'linux/fs/xfs';
272 }
273 print "\n=== Checking libxlog code ===\n";
274 functiondiff 'xfs_log_recover.c', 'cmd/xfsprogs/libxlog', 'linux/fs/xfs';