From 0a88b5eb9ad126175317572e383930f52424757c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 27 Dec 2010 08:02:57 +0100 Subject: Begin work on 'hashl copy' --- bin/hashl | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/bin/hashl b/bin/hashl index 4a7ea92..bc2fc2c 100755 --- a/bin/hashl +++ b/bin/hashl @@ -9,6 +9,7 @@ use autodie; use Cwd; use Digest::SHA qw(sha1_hex); +use File::Copy; use File::Find; use Getopt::Long; use IO::Handle; @@ -22,6 +23,7 @@ my $db_file = '.hashl.db'; my $total = 0; my $cur = 0; my $timer; +my $incoming_dir; my $VERSION = '0.1'; @@ -182,6 +184,44 @@ sub process_file { } } +sub mkdirs { + my ($base, $new) = @_; + + for my $dir (split(qr{/}, $new)) { + $base .= "/$dir"; + if (! -d $base) { + mkdir($base); + } + } +} + +sub copy_file { + my ($file, $to) = @_; + + my $base = substr($file, length($base) + 1); + if ($base =~ s{ / [^/]+ $}{}x) { + mkdirs($incoming_dir, $base); + } + + copy($file, "${to}/${base}"); +} + +sub maybe_copy_file { + my $file = $File::Find::name; + + if (not -f $file or -l $file or $file eq $db_file) { + return; + } + + $cur++; + + print $timer->report("\r\e[KCopying: %p done, %L elapsed, %E remaining", $cur); + + if (not defined file_in_db($file)) { + copy_file($file, $incoming_dir); + } +} + if ($action eq 'update') { STDOUT->autoflush(1); @@ -216,6 +256,29 @@ elsif ($action eq 'info') { db_info(); } } +elsif ($action eq 'copy') { + + if (not $ARGV[1]) { + usage(); + } + + $incoming_dir = $ARGV[1]; + if (substr($incoming_dir, 0, 1) ne '/') { + $incoming_dir = $base . '/' . $incoming_dir; + } + + STDOUT->autoflush(1); + find(\&get_total, $base); + + $timer = Time::Progress->new(); + $timer->attr( + min => 1, + max => $total, + ); + + find(\&maybe_copy_file, $base); + print "\n"; +} else { usage(); } -- cgit v1.2.3