summaryrefslogtreecommitdiff
path: root/lib/App/Hashl.pm
blob: 58af4a60726022eef52bd0ff6da1c9a698dec738 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package App::Hashl;

use strict;
use warnings;
use autodie;
use 5.010;

use Digest::SHA qw(sha1_hex);
use Storable qw(nstore retrieve);

my $VERSION = '0.1';

=head1 NAME

App::Hashl - Partially hash files, check if files are equal etc.

=head1 SYNOPSIS

    use App::Hashl;

    my $hashl = App::Hashl->new();
    # or: App::Hashl->new_from_file($database_file);

    for my $file (@files) {
        $hashl->add_file($file, {
            hash

=cut


sub new {
	my ($obj, %conf) = @_;
	my $ref = {
		files => {},
		ignored => {},
	};

	$ref->{config} = \%conf;
	$ref->{config}->{read_size} //=  (2 ** 20) * 4, # 4 MiB

	return bless($ref, $obj);
}

sub new_from_file {
	my ($obj, $file) = @_;
	my $ref = retrieve($file);
	return bless($ref, $obj);
}

sub si_size {
	my ($self, $bytes) = @_;
	my @post = (' ', qw(k M G T));

	while ($bytes >= 1024) {
		$bytes /= 1024;
		shift @post;
	}

	return sprintf("%6.1f%s", $bytes, $post[0]);
}

sub hash_file {
	my ($self, $file) = @_;
	my ($fh, $data);

	open($fh, '<', $file);
	binmode($fh);
	read($fh, $data, $self->{config}->{read_size});
	close($fh);

	return sha1_hex($data);
}

sub hash_in_db {
	my ($self, $hash) = @_;

	if ($self->{ignored}->{hashes}) {
		for my $ihash (@{$self->{ignored}->{hashes}}) {
			if ($hash eq $ihash) {
				return '// ignored';
			}
		}
	}

	for my $name ($self->files()) {
		my $file = $self->file($name);

		if ($file->{hash} eq $hash) {
			return $name;
		}
	}
	return undef;
}

sub file_in_db {
	my ($self, $file) = @_;

	return $self->hash_in_db($self->hash_file($file));
}

sub read_size {
	my ($self) = @_;
	return $self->{config}->{read_size};
}

sub file {
	my ($self, $name) = @_;
	return $self->{files}->{$name};
}

sub delete_file {
	my ($self, $name) = @_;
	delete $self->{files}->{$name};
}

sub files {
	my ($self) = @_;
	return sort keys %{ $self->{files} };
}

sub add_file {
	my ($self, %data) = @_;
	my $file = $data{file};
	my $path = $data{path};

	if ($self->file($file) and
			$self->file($file)->{mtime} == $data{mtime} and
			$self->file($file)->{size} == $data{size} ) {
		return;
	}

	$self->{files}->{$file} = {
		hash  => $self->hash_file($file),
		mtime => $data{mtime},
		size  => $data{size},
	};
}

sub ignored {
	my ($self) = @_;
	if (exists $self->{ignored}->{hashes}) {
		return @{ $self->{ignored}->{hashes} };
	}
	else {
		return ();
	}
}

sub ignore {
	my ($self, $file) = @_;

	push(@{ $self->{ignored}->{hashes} }, $file);
}

sub save {
	my ($self, $file) = @_;
	nstore($self, $file);
}

1;

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=over

=back

=head1 DEPENDENCIES

=head1 SEE ALSO

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

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