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