xfs/098: adapt to external log devices
[xfstests-dev.git] / tools / fs-walk
1 #!/usr/bin/perl -w
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # traverse the entire filesystem dumping info.
6 #
7 use MD5;
8 use Getopt::Std;
9
10 my %opt;
11
12 sub scan($)
13 {
14     my ($file)=@_;
15     my ($md5)=new MD5;
16     my (@stat);
17
18     unless (@stat=lstat("$file")) {
19         printf("%-" . ($opt{v}?65:32) . "s $file\n", "!!! could not lstat");
20         return;
21     }
22
23     $stat[0]=$stat[8]=""; # wipe the device and access time
24     $md5->reset;
25     $md5->add(join(" ",@stat));
26     
27     print join(" ",@stat), "\n";
28     
29     if ($opt{v}) {
30         print $md5->hexdigest . " ";
31         $md5->reset;
32     }
33
34     if (-l "$file") {
35         if (!defined($link = readlink $file)) {
36             printf("%-32s $file\n", "!!! could not readlink");
37             return;
38         }
39         $md5->add($link);
40     } elsif (-f "$file") {
41         if (!open(FILE, "$file")) {
42             printf("%-32s $file\n", "!!! could not read");
43             return;
44         }
45         $md5->addfile(FILE);
46         close (FILE);
47     }
48     print $md5->hexdigest . " $file\n";
49 }
50
51 getopts('vs', \%opt);
52
53 die "Usage: $0 <dir>\n" unless (@ARGV == 1);
54
55 $dir=shift @ARGV;
56 die "can't read $dir\n" unless (-r $dir);
57 die "$dir is not a directory\n" unless (-d _);
58
59 chomp($HOST = `hostname -s`);
60
61 print "*** fs-walk host $HOST dir $dir\n";
62
63 @todo=$dir;
64 while ($dir = shift @todo) {
65     scan($dir);
66     unless (opendir(DIR,$dir)) {
67         printf("%-" . ($opt{v}?65:32) . "s $dir\n", "!!! could not opendir");
68         next;
69     }
70     unless (@all=readdir(DIR)) {
71         printf("%-" . ($opt{v}?65:32) . "s $dir\n", "!!! could not readdir");
72         next;
73     }
74     closedir(DIR);
75     @dirs=grep(-d "$dir/$_" && !-l "$dir/$_", @all);
76     foreach (@all) {
77         next if /^\.\.?$/;
78         scan("$dir/$_");
79     }
80     
81     foreach (grep(!/^\.\.?$/, @dirs)) {
82         push (@todo,"$dir/$_");
83     }
84 }
85