--- IO-AIO/bin/treescan 2007/09/10 11:24:20 1.1 +++ IO-AIO/bin/treescan 2011/09/30 00:23:44 1.9 @@ -3,57 +3,83 @@ # inspired by treescan by Jamie Lokier # about 40% faster than the original version (on my fs and raid :) +use strict; use Getopt::Long; use IO::AIO; -Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order"); +our $VERSION = $IO::AIO::VERSION; -my ($opt_silent, $opt_print0, $opt_stat); +Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order", "auto_help", "auto_version"); + +my ($opt_silent, $opt_print0, $opt_stat, $opt_nodirs, $opt_nofiles, $opt_grep); GetOptions "quiet|q" => \$opt_silent, "print0|0" => \$opt_print0, "stat|s" => \$opt_stat, -; + "dirs|d" => \$opt_nofiles, + "files|f" => \$opt_nodirs, + "grep|g=s" => \$opt_grep, + or die "Usage: try $0 --help"; @ARGV = "." unless @ARGV; +$opt_grep &&= qr{$opt_grep}s; + sub printfn { - my ($path, $files, $suffix) = @_; + my ($prefix, $files, $suffix) = @_; + if ($opt_grep) { + @$files = grep "$prefix$_" =~ $opt_grep, @$files; + } + if ($opt_print0) { - print map "$path/$_$suffix\0", @$files; + print map "$prefix$_$suffix\0", @$files; } elsif (!$opt_silent) { - print map "$path/$_$suffix\n", @$files; + print map "$prefix$_$suffix\n", @$files; } } sub scan { my ($path) = @_; + $path .= "/"; + + IO::AIO::poll_cb; + + aioreq_pri -1; aio_scandir $path, 8, sub { - my ($dirs, $files) = @_; + my ($dirs, $files) = @_ + or warn "$path: $!\n"; + + printfn "", [$path] unless $opt_nodirs; + printfn $path, $files unless $opt_nofiles; if ($opt_stat) { - aio_stat "$path/$_" for @$files; - } + aio_wd $path, sub { + my $wd = shift; - for (@$dirs) { - printfn $path, $dirs, "/"; - &scan ("$path/$_") + aio_lstat [$wd, $_] for @$files; + }; } - printfn $path, $files; + &scan ("$path$_") for @$dirs; }; } -IO::AIO::max_outstanding 64; -IO::AIO::min_parallel 32; +IO::AIO::max_outstanding 100; # two fds per directory, so limit accordingly +IO::AIO::min_parallel 20; for my $seed (@ARGV) { $seed =~ s/\/+$//; aio_lstat "$seed/.", sub { - scan $seed if -d _; + if ($_[0]) { + print STDERR "$seed: $!\n"; + } elsif (-d _) { + scan $seed; + } else { + printfn "", $seed, "/"; + } }; }