1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | # inspired by treescan by Jamie Lokier <jamie@imbolc.ucc.ie> |
3 | # inspired by treescan by Jamie Lokier <jamie@imbolc.ucc.ie> |
4 | # about 40% faster than the original version (on my fs and raid :) |
4 | # about 40% faster than the original version (on my fs and raid :) |
5 | |
5 | |
|
|
6 | use strict; |
6 | use Getopt::Long; |
7 | use Getopt::Long; |
|
|
8 | use Time::HiRes (); |
7 | use IO::AIO; |
9 | use IO::AIO; |
8 | |
10 | |
9 | Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order"); |
11 | our $VERSION = $IO::AIO::VERSION; |
10 | |
12 | |
|
|
13 | Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order", "auto_help", "auto_version"); |
|
|
14 | |
11 | my ($opt_silent, $opt_print0, $opt_stat); |
15 | my ($opt_silent, $opt_print0, $opt_stat, $opt_nodirs, |
|
|
16 | $opt_nofiles, $opt_grep, $opt_progress); |
12 | |
17 | |
13 | GetOptions |
18 | GetOptions |
14 | "quiet|q" => \$opt_silent, |
19 | "quiet|q" => \$opt_silent, |
15 | "print0|0" => \$opt_print0, |
20 | "print0|0" => \$opt_print0, |
16 | "stat|s" => \$opt_stat, |
21 | "stat|s" => \$opt_stat, |
17 | ; |
22 | "dirs|d" => \$opt_nofiles, |
|
|
23 | "files|f" => \$opt_nodirs, |
|
|
24 | "grep|g=s" => \$opt_grep, |
|
|
25 | "progress|p" => \$opt_progress, |
|
|
26 | or die "Usage: try $0 --help"; |
18 | |
27 | |
19 | @ARGV = "." unless @ARGV; |
28 | @ARGV = "." unless @ARGV; |
20 | |
29 | |
|
|
30 | $opt_grep &&= qr{$opt_grep}s; |
|
|
31 | |
|
|
32 | my ($n_dirs, $n_files, $n_stats) = (0, 0, 0); |
|
|
33 | my $n_last; |
|
|
34 | my $n_start = Time::HiRes::time; |
|
|
35 | |
21 | sub printfn { |
36 | sub printfn { |
22 | my ($path, $files, $suffix) = @_; |
37 | my ($prefix, $files, $suffix) = @_; |
23 | |
38 | |
|
|
39 | if ($opt_grep) { |
|
|
40 | @$files = grep "$prefix$_" =~ $opt_grep, @$files; |
|
|
41 | } |
|
|
42 | |
24 | if ($opt_print0) { |
43 | if ($opt_print0) { |
25 | print map "$path/$_$suffix\0", @$files; |
44 | print map "$prefix$_$suffix\0", @$files; |
26 | } elsif (!$opt_silent) { |
45 | } elsif (!$opt_silent) { |
27 | print map "$path/$_$suffix\n", @$files; |
46 | print map "$prefix$_$suffix\n", @$files; |
28 | } |
47 | } |
29 | } |
48 | } |
30 | |
49 | |
31 | sub scan { |
50 | sub scan { |
32 | my ($path) = @_; |
51 | my ($path) = @_; |
33 | |
52 | |
|
|
53 | $path .= "/"; |
|
|
54 | |
|
|
55 | IO::AIO::poll_cb; |
|
|
56 | |
|
|
57 | if ($opt_progress and $n_last + 1 < Time::HiRes::time) { |
|
|
58 | $n_last = Time::HiRes::time; |
|
|
59 | printf STDERR "%d dirs %d files %d stats %g stats/s \r", $n_dirs, $n_files, $n_stats, $n_stats / ($n_last - $n_start) |
|
|
60 | if $opt_progress; |
|
|
61 | } |
|
|
62 | |
|
|
63 | aioreq_pri -1; |
|
|
64 | ++$n_dirs; |
34 | aio_scandir $path, 8, sub { |
65 | aio_scandir $path, 8, sub { |
35 | my ($dirs, $files) = @_; |
66 | my ($dirs, $files) = @_ |
|
|
67 | or warn "$path: $!\n"; |
|
|
68 | |
|
|
69 | printfn "", [$path] unless $opt_nodirs; |
|
|
70 | printfn $path, $files unless $opt_nofiles; |
|
|
71 | |
|
|
72 | $n_files += @$files; |
36 | |
73 | |
37 | if ($opt_stat) { |
74 | if ($opt_stat) { |
|
|
75 | aio_wd $path, sub { |
|
|
76 | my $wd = shift; |
|
|
77 | |
38 | aio_stat "$path/$_" for @$files; |
78 | aio_lstat [$wd, $_] for @$files; |
|
|
79 | $n_stats += @$files; |
|
|
80 | }; |
39 | } |
81 | } |
40 | |
82 | |
41 | for (@$dirs) { |
83 | &scan ("$path$_") for @$dirs; |
42 | printfn $path, $dirs, "/"; |
|
|
43 | &scan ("$path/$_") |
|
|
44 | } |
|
|
45 | |
|
|
46 | printfn $path, $files; |
|
|
47 | }; |
84 | }; |
48 | } |
85 | } |
49 | |
86 | |
50 | IO::AIO::max_outstanding 64; |
87 | IO::AIO::max_outstanding 100; # two fds per directory, so limit accordingly |
51 | IO::AIO::min_parallel 32; |
88 | IO::AIO::min_parallel 20; |
52 | |
89 | |
53 | for my $seed (@ARGV) { |
90 | for my $seed (@ARGV) { |
54 | $seed =~ s/\/+$//; |
91 | $seed =~ s/\/+$//; |
|
|
92 | ++$n_stats; |
55 | aio_lstat "$seed/.", sub { |
93 | aio_lstat "$seed/.", sub { |
|
|
94 | if ($_[0]) { |
|
|
95 | print STDERR "$seed: $!\n"; |
|
|
96 | } elsif (-d _) { |
56 | scan $seed if -d _; |
97 | scan $seed; |
|
|
98 | } else { |
|
|
99 | printfn "", $seed, "/"; |
|
|
100 | } |
57 | }; |
101 | }; |
58 | } |
102 | } |
59 | |
103 | |
60 | IO::AIO::flush; |
104 | IO::AIO::flush; |
61 | |
105 | |