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