fist dedup implementation
[cloudstore.git] / lib / CloudStore / dedup.pm
1 package CloudStore::dedup;
2 use warnings;
3 use strict;
4 use autodie;
5
6 sub path_md5 {
7         my ( $path, $md5 ) = @_;
8
9         my $pool = 'md5'; # FIXME sharding?
10         mkdir $pool unless -e $pool;
11
12         if ( -e "$pool/$md5" ) {
13                 warn "dedup hit $md5 $path\n";
14                 my $dedup = $path . '.dedup';
15                 rename $path, $dedup;
16                 link "$pool/$md5", $path;
17                 unlink $dedup;
18         } else {
19                 link $path, "$pool/$md5";
20         }
21 }
22
23 sub path_remove {
24         my ( $path ) = @_;
25
26         my ( undef, undef, undef, $nlink ) = stat $path;
27
28         warn "nlink $path $nlink";
29 }
30
31 my $empty_md5 = " " x 32;
32
33 sub data {
34         my $data = shift;
35
36         my $path = "users/$data->{login}/blob/$data->{file}";
37
38         return if -d $path;
39
40         if ( $data->{md5} ne $empty_md5 ) {
41                 path_md5 $path => $data->{md5};
42         } else {
43                 path_remove $path;
44         }
45 }
46
47 1;