App-Hashl
view release on metacpan or search on metacpan
STDERR->autoflush(1);
GetOptions(
'd|database=s' => \$db_file,
'e|extra-db=s' => \@edb_files,
'f|force' => \$add_unignore,
'n|no-progress' => sub { $show_progress = 0 },
's|read-size=i' => sub { $read_size = $_[1] * 1024 },
'V|version' => sub { say "hashl version ${VERSION}"; exit 0 },
'x|one-file-system' => sub { $xdev_fsno = ( stat($base) )[0] },
) or usage();
if ( substr( $db_file, 0, 1 ) ne q{/} ) {
$db_file = "${base}/${db_file}";
}
my $action = shift;
sub usage {
die(<<"EOF");
$hashl->save($db_file);
exit 0;
}
sub get_total {
my $file = $File::Find::name;
if ( -f $file
and not -l $file
and $file ne $db_file
and ( not $xdev_fsno or ( stat($file) )[0] == $xdev_fsno ) )
{
$total++;
}
return;
}
sub drop_deleted {
for my $file ( $hashl->files ) {
if ( not -e $file ) {
sub process_file {
my ( $code, $write ) = ( $find_ref, $find_db_write );
my $file = $File::Find::name;
my $path = $file;
if ( not -f $file
or -l $file
or $file eq $db_file
or ( $xdev_fsno and ( stat($file) )[0] != $xdev_fsno ) )
{
return;
}
if ($rel_paths) {
$file = substr( $file, length($base) + 1 );
}
$cur++;
lib/App/Hashl.pm view on Meta::CPAN
return sprintf( '%6.1f%s', $bytes, $post[0] );
}
sub hash_file {
my ( $self, $file ) = @_;
my $data;
my $digest = Digest::SHA->new(1);
# read() fails for empty files
if ( ( stat($file) )[7] == 0 ) {
return $digest->hexdigest;
}
if ( $self->{config}->{read_size} == 0 ) {
$digest->addfile($file);
return $digest->hexdigest;
}
#<<< perltidy has problems indenting 'or die' with tabs
open( my $fh, '<', $file )
lib/App/Hashl.pm view on Meta::CPAN
sub files {
my ($self) = @_;
return keys %{ $self->{files} };
}
sub add_file {
my ( $self, %opt ) = @_;
my $file = $opt{file};
my $path = $opt{path};
my ( $size, $mtime ) = ( stat($path) )[ 7, 9 ];
if ( $self->file($file)
and $self->file($file)->{mtime} == $mtime
and $self->file($file)->{size} == $size )
{
return;
}
my $hash = $self->hash_file($path);
t/29-app-hashl.t view on Meta::CPAN
is($hashl->si_size(1024 * 1024), ' 1.0M', 'si_size 1024^2 = 1M');
is($hashl->si_size(0), 'infinite', 'si_size 0 = infinite');
is($hashl->hash_in_db('123'), undef, 'hash not in db');
is($hashl->file_in_db('t/in/4'), undef, 'file not in db');
is_deeply([$hashl->files()], [], 'no files in empty db');
is_deeply([$hashl->ignored()], [], 'no ignored files in empty db');
my $test_hash = $hashl->hash_file('t/in/4');
my ($test_size, $test_mtime) = (stat('t/in/4'))[7,9];
ok($hashl->add_file(
file => 't/in/4',
path => 't/in/4',
),
'Add new file'
);
is_deeply($hashl->file('t/in/4'),
{
hash => $test_hash,
size => $test_size,
( run in 0.507 second using v1.01-cache-2.11-cpan-49f99fa48dc )