#!/usr/bin/env perl ## Copyright © 2010 by Daniel Friesel ## License: WTFPL ## 0. You just DO WHAT THE FUCK YOU WANT TO. use strict; use warnings; use 5.010; use autodie; use Cwd; use Digest::SHA qw(sha1_hex); use File::Find; use Storable qw(nstore retrieve); my $base = getcwd(); my $rel_paths = 1; my $read_size = (2 ** 20) * 4; # 4 MiB my $db; my $action = $ARGV[0]; if (not defined $action) { die("Usage: $0 \n"); } if (-r 'hashl.db') { $db = retrieve('hashl.db'); } sub print_status { my ($file) = @_; local $| = 1; print "\r\e[2K${file}"; } 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") { return; } if ($rel_paths) { $file = substr($file, length($base) + 1); } print_status($file); if (exists($db->{$file}) and $db->{$file}->{'mtime'} == $mtime and $db->{$file}->{'size'} == $size ) { return; } open($fh, '<', $path); binmode($fh); read($fh, $data, $read_size); close($fh); $db->{$file} = { hash => sha1_hex($data), mtime => $mtime, size => $size, }; } if ($action eq 'update') { find(\&process_file, $base); nstore($db, 'hashl.db'); } elsif ($action eq 'list') { for my $name (sort keys %{$db}) { my $file = $db->{$name}; printf("%s %s\n", $file->{'hash'}, $name); } } __END__ =head1 NAME =head1 SYNOPSIS =head1 DESCRIPTION =head1 OPTIONS =head1 EXIT STATUS =head1 CONFIGURATION =head1 DEPENDENCIES =head1 BUGS AND LIMITATIONS =head1 AUTHOR Copyright (C) 2010 by Daniel Friesel Ederf@finalrewind.orgE =head1 LICENSE 0. You just DO WHAT THE FUCK YOU WANT TO.