From 29732df1f3bb783875ea4c6939ec38d940f85f65 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 19 Aug 2012 13:43:40 +0200 Subject: add multi-db support to copy, list, list-files and list-ignored --- bin/hashl | 59 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/bin/hashl b/bin/hashl index 841ef7b..27aaa54 100755 --- a/bin/hashl +++ b/bin/hashl @@ -70,8 +70,8 @@ else { @ehashl = ($hashl); for my $file (@edb_files) { - if (-r $file) { - push(@ehashl, App::Hashl->new_from_file($file)); + if ( -r $file ) { + push( @ehashl, App::Hashl->new_from_file($file) ); } else { die("-e ${file}: database does not exist\n"); @@ -106,6 +106,21 @@ sub drop_deleted { return; } +sub ensure_equal_hash_sizes { + for my $i ( 1 .. $#ehashl ) { + if ( $ehashl[$i]->read_size != $hashl->read_size ) { + printf STDERR ( + 'Cannot list: main database has read size %d, but database' + . " %s has read size %d\n", + $hashl->read_size, + $edb_files[ $i - 1 ], + $ehashl[$i]->read_size + ); + exit 1; + } + } +} + sub copy_file { my ( $file, $to ) = @_; @@ -212,7 +227,7 @@ sub db_ignore { sub db_copy { my ( $file, $path ) = @_; - if ( not $hashl->file_in_db($path) ) { + if ( not any { $_->file_in_db($path) } @ehashl ) { copy_file( $path, $incoming_dir ); } @@ -345,38 +360,51 @@ sub cmd_info { return; } +sub map_with_prefix { + my ( $prefix, @items ) = @_; + return map { [ $prefix, $_ ] } @items; +} + sub cmd_list { my ($re) = @_; + ensure_equal_hash_sizes(); + printf( "# hashl v%s Read Size %d bytes (%s)\n", - $VERSION, $hashl->read_size(), $hashl->si_size( $hashl->read_size() ), + $VERSION, $hashl->read_size, $hashl->si_size( $hashl->read_size ), ); - for my $name ( sort $hashl->files() ) { - my $file = $hashl->file($name); + for my $pair ( + sort { $a->[1] cmp $b->[1] } + map { map_with_prefix( $_, $_->files ) } @ehashl + ) + { + my ( $db, $name ) = @{$pair}; + my $file = $db->file($name); if ( $re and $name !~ m{$re} ) { next; } printf( "%s %-7s %s\n", - $file->{hash}, $hashl->si_size( $file->{size} ), $name ); + $file->{hash}, $db->si_size( $file->{size} ), $name ); } return; } sub cmd_list_files { - say join( "\n", sort $hashl->files() ); + say join( "\n", sort map { $_->files } @ehashl ); return; } sub cmd_list_ignored { - for my $hash ( $hashl->ignored() ) { - say $hash; - } + + ensure_equal_hash_sizes(); + + say join( "\n", map { $_->ignored } @ehashl ); return; } @@ -432,7 +460,7 @@ Actions: =item B I -Copy all files in the current directory which are not in the database to +Copy all files in the current directory which are not in any database to I. =item B [I] @@ -488,7 +516,10 @@ Use I instead of F<.hashl.db> =item B<-e>|B<--extra-db> I Use I in addition to F<.hashl.db> / B<-d>. May be specified -several times. Does not affect all commands. +several times. + +Database files specified with this option will be opened read-only and ignored +by writing actions (such as B or B). =item B<-f>|B<--force> @@ -528,7 +559,7 @@ None, so far =item * Digest::SHA -=item = List::MoreUtils +=item * List::MoreUtils =item * Time::Progress -- cgit v1.2.3