diff options
author | Daniel Friesel <derf@finalrewind.org> | 2010-12-18 20:59:59 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2010-12-18 20:59:59 +0100 |
commit | 14b20592e502cb4414c614f1ebfc2be599aed9e5 (patch) | |
tree | 7a9dc84d93d02343b361347cc4c2f808b50b8fe2 /bin/hashl | |
parent | cdff687c62bbb2a7c8c7bd2e4eecfd1186d7bc44 (diff) |
Custom DB file, print progress
Diffstat (limited to 'bin/hashl')
-rwxr-xr-x | bin/hashl | 38 |
1 files changed, 33 insertions, 5 deletions
@@ -10,41 +10,64 @@ use autodie; use Cwd; use Digest::SHA qw(sha1_hex); use File::Find; +use Getopt::Long; use Storable qw(nstore retrieve); my $base = getcwd(); my $rel_paths = 1; my $read_size = (2 ** 20) * 4; # 4 MiB +my $db_file = "$base/hashl.db"; +my $total = 0; +my $cur = 0; my $db; +GetOptions( + 'd|database=s' => \$db_file, +); + my $action = $ARGV[0]; if (not defined $action) { die("Usage: $0 <action>\n"); } -if (-r 'hashl.db') { - $db = retrieve('hashl.db'); +if (-r $db_file) { + $db = retrieve($db_file); } sub print_status { my ($file) = @_; local $| = 1; - print "\r\e[2K${file}"; + printf( + "\r\e[2K%d/%d %s", + $cur, + $total, + $file, + ); +} + +sub get_total { + my $file = $File::Find::name; + if (-f $file and $file ne $db_file) { + $total++; + } } + sub process_file { my $file = $File::Find::name; my $path = $file; my ($size, $mtime) = (stat($file))[7,9]; my ($fh, $data); - if (not -f $file or $file eq "${base}/hashl.db") { + if (not -f $file or $file eq $db_file) { return; } + $cur++; + if ($rel_paths) { $file = substr($file, length($base) + 1); } @@ -67,11 +90,16 @@ sub process_file { mtime => $mtime, size => $size, }; + + if (($cur % 100) == 0) { + nstore($db, $db_file); + } } if ($action eq 'update') { + find(\&get_total, $base); find(\&process_file, $base); - nstore($db, 'hashl.db'); + nstore($db, $db_file); } elsif ($action eq 'list') { for my $name (sort keys %{$db}) { |