generic: new case to test getcwd(2)
[xfstests-dev.git] / tools / fs-walk
1 #!/usr/bin/perl -w
2 #
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # traverse the entire filesystem dumping info.
20 #
21 #                                                       dxm 5/10/00
22 use MD5;
23 use Getopt::Std;
24
25 my %opt;
26
27 sub scan($)
28 {
29     my ($file)=@_;
30     my ($md5)=new MD5;
31     my (@stat);
32
33     unless (@stat=lstat("$file")) {
34         printf("%-" . ($opt{v}?65:32) . "s $file\n", "!!! could not lstat");
35         return;
36     }
37
38     $stat[0]=$stat[8]=""; # wipe the device and access time
39     $md5->reset;
40     $md5->add(join(" ",@stat));
41     
42     print join(" ",@stat), "\n";
43     
44     if ($opt{v}) {
45         print $md5->hexdigest . " ";
46         $md5->reset;
47     }
48
49     if (-l "$file") {
50         if (!defined($link = readlink $file)) {
51             printf("%-32s $file\n", "!!! could not readlink");
52             return;
53         }
54         $md5->add($link);
55     } elsif (-f "$file") {
56         if (!open(FILE, "$file")) {
57             printf("%-32s $file\n", "!!! could not read");
58             return;
59         }
60         $md5->addfile(FILE);
61         close (FILE);
62     }
63     print $md5->hexdigest . " $file\n";
64 }
65
66 getopts('vs', \%opt);
67
68 die "Usage: $0 <dir>\n" unless (@ARGV == 1);
69
70 $dir=shift @ARGV;
71 die "can't read $dir\n" unless (-r $dir);
72 die "$dir is not a directory\n" unless (-d _);
73
74 chomp($HOST = `hostname -s`);
75
76 print "*** fs-walk host $HOST dir $dir\n";
77
78 @todo=$dir;
79 while ($dir = shift @todo) {
80     scan($dir);
81     unless (opendir(DIR,$dir)) {
82         printf("%-" . ($opt{v}?65:32) . "s $dir\n", "!!! could not opendir");
83         next;
84     }
85     unless (@all=readdir(DIR)) {
86         printf("%-" . ($opt{v}?65:32) . "s $dir\n", "!!! could not readdir");
87         next;
88     }
89     closedir(DIR);
90     @dirs=grep(-d "$dir/$_" && !-l "$dir/$_", @all);
91     foreach (@all) {
92         next if /^\.\.?$/;
93         scan("$dir/$_");
94     }
95     
96     foreach (grep(!/^\.\.?$/, @dirs)) {
97         push (@todo,"$dir/$_");
98     }
99 }
100