summaryrefslogtreecommitdiff
path: root/bin/hashl
blob: 399e373784a1f12ec9918d858d2c2dc6ad967381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env perl
## Copyright © 2010 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL <http://sam.zoy.org/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 <action>\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 E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.