line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::ContentStore; |
2
|
|
|
|
|
|
|
$File::ContentStore::VERSION = '1.003'; |
3
|
2
|
|
|
2
|
|
130574
|
use 5.014; |
|
2
|
|
|
|
|
25
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
9
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
93
|
|
6
|
2
|
|
|
2
|
|
912
|
use Types::Standard qw( slurpy Object Bool Str ArrayRef HashRef CodeRef ); |
|
2
|
|
|
|
|
120262
|
|
|
2
|
|
|
|
|
17
|
|
7
|
2
|
|
|
2
|
|
3245
|
use Types::Path::Tiny qw( Dir File ); |
|
2
|
|
|
|
|
36572
|
|
|
2
|
|
|
|
|
13
|
|
8
|
2
|
|
|
2
|
|
1743
|
use Type::Params qw( compile ); |
|
2
|
|
|
|
|
18599
|
|
|
2
|
|
|
|
|
16
|
|
9
|
2
|
|
|
2
|
|
1143
|
use Digest; |
|
2
|
|
|
|
|
894
|
|
|
2
|
|
|
|
|
50
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
857
|
use Moo; |
|
2
|
|
|
|
|
16866
|
|
|
2
|
|
|
|
|
8
|
|
12
|
2
|
|
|
2
|
|
3185
|
use namespace::clean; |
|
2
|
|
|
|
|
18083
|
|
|
2
|
|
|
|
|
8
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has path => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => Dir, |
17
|
|
|
|
|
|
|
required => 1, |
18
|
|
|
|
|
|
|
coerce => 1, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has digest => ( |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
isa => Str, |
24
|
|
|
|
|
|
|
default => 'SHA-1', |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has parts => ( |
28
|
|
|
|
|
|
|
is => 'lazy', |
29
|
|
|
|
|
|
|
builder => |
30
|
6
|
|
|
6
|
|
68
|
sub { int( length( Digest->new( shift->digest )->hexdigest ) / 32 ) }, |
31
|
|
|
|
|
|
|
init_arg => undef, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has check_for_collisions => ( |
35
|
|
|
|
|
|
|
is => 'ro', |
36
|
|
|
|
|
|
|
isa => Bool, |
37
|
|
|
|
|
|
|
required => 1, |
38
|
|
|
|
|
|
|
default => 1, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has make_read_only => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
isa => Bool, |
44
|
|
|
|
|
|
|
required => 1, |
45
|
|
|
|
|
|
|
default => 1, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has file_callback => ( |
49
|
|
|
|
|
|
|
is => 'ro', |
50
|
|
|
|
|
|
|
isa => CodeRef, |
51
|
|
|
|
|
|
|
predicate => 1, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has inode => ( |
55
|
|
|
|
|
|
|
is => 'lazy', |
56
|
|
|
|
|
|
|
isa => HashRef, |
57
|
|
|
|
|
|
|
init_arg => undef, |
58
|
|
|
|
|
|
|
builder => sub { |
59
|
6
|
|
|
6
|
|
3727
|
my ($self) = @_; |
60
|
6
|
|
|
|
|
12
|
my $re = qr{ |
61
|
|
|
|
|
|
|
^ |
62
|
6
|
|
|
|
|
80
|
${ \( '[a-f0-9][a-f0-9]/' x $self->parts ) } |
63
|
6
|
|
|
|
|
242
|
${ \( '[a-f0-9]' |
64
|
|
|
|
|
|
|
x ( length( Digest->new( $self->digest )->hexdigest ) |
65
|
|
|
|
|
|
|
- 2 * $self->parts ) ) } |
66
|
|
|
|
|
|
|
$ |
67
|
|
|
|
|
|
|
}x; |
68
|
|
|
|
|
|
|
$self->path->visit( |
69
|
|
|
|
|
|
|
sub { |
70
|
4
|
|
|
4
|
|
722
|
my ( $path, $inode ) = @_; |
71
|
4
|
|
|
|
|
16
|
my $rel = $path->relative( $self->path )->stringify; |
72
|
4
|
100
|
66
|
|
|
608
|
$inode->{ $path->stat->ino } = $rel |
73
|
|
|
|
|
|
|
if -f && $rel =~ $re; |
74
|
|
|
|
|
|
|
}, |
75
|
6
|
|
|
|
|
515
|
{ recurse => 1 } |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
}, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# if a single non-hashref argument is given, assume it's 'path' |
81
|
|
|
|
|
|
|
sub BUILDARGS { |
82
|
8
|
|
|
8
|
0
|
63131
|
my $class = shift; |
83
|
|
|
|
|
|
|
scalar @_ == 1 |
84
|
|
|
|
|
|
|
? ref $_[0] eq 'HASH' |
85
|
8
|
100
|
|
|
|
138
|
? { %{ $_[0] } } |
|
1
|
50
|
|
|
|
17
|
|
|
|
100
|
|
|
|
|
|
86
|
|
|
|
|
|
|
: { path => $_[0] } |
87
|
|
|
|
|
|
|
: @_ % 2 ? Carp::croak( |
88
|
|
|
|
|
|
|
"The new() method for $class expects a hash reference or a" |
89
|
|
|
|
|
|
|
. " key/value list. You passed an odd number of arguments" ) |
90
|
|
|
|
|
|
|
: {@_}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub BUILD { |
94
|
7
|
|
|
7
|
0
|
3221
|
Digest->new( shift->digest ); # dies if 'digest' is not installed |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $BUFF_SIZE = 1024 * 32; |
98
|
|
|
|
|
|
|
my $DIGEST_OPTS = { chunk_size => $BUFF_SIZE }; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub link_file { |
101
|
16
|
|
|
16
|
1
|
220
|
state $check = compile( Object, File ); |
102
|
16
|
|
|
|
|
1907
|
my ( $self, $file ) = $check->(@_); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# skip non-files and symbolic links |
105
|
16
|
100
|
66
|
|
|
766
|
return unless -f $file && !-l $file; |
106
|
|
|
|
|
|
|
|
107
|
12
|
|
|
|
|
296
|
my ( $digest, $content, $done ); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# check if the file's inode is in the cache |
110
|
12
|
100
|
|
|
|
239
|
if ( $content = $self->inode->{ $file->stat->ino } ) { |
111
|
2
|
|
|
|
|
478
|
$digest = $content =~ s{/}{}gr; |
112
|
2
|
|
|
|
|
8
|
$content = $self->path->child($content); |
113
|
2
|
|
|
|
|
60
|
$done = 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# compute content file name |
117
|
|
|
|
|
|
|
else { |
118
|
10
|
|
|
|
|
7558
|
$digest = $file->digest( $DIGEST_OPTS, $self->digest ); |
119
|
|
|
|
|
|
|
$content = |
120
|
|
|
|
|
|
|
$self->path->child( |
121
|
10
|
|
|
|
|
3231
|
map( { substr $digest, 2 * $_, 2 } 0 .. $self->parts - 1 ), |
|
10
|
|
|
|
|
200
|
|
122
|
|
|
|
|
|
|
substr( $digest, 2 * $self->parts ) ); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
12
|
100
|
|
|
|
458
|
$self->file_callback->( $file, $digest, $content ) |
126
|
|
|
|
|
|
|
if $self->has_file_callback; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# if the inode is already in our store, there's nothing left to do |
129
|
12
|
100
|
|
|
|
1067
|
return if $done; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# check for collisions |
132
|
10
|
100
|
100
|
|
|
35
|
if( -e $content && $self->check_for_collisions ) { |
133
|
2
|
50
|
|
|
|
48
|
croak "Collision found for $file and $content: size differs" |
134
|
|
|
|
|
|
|
if -s $file != -s $content; |
135
|
|
|
|
|
|
|
|
136
|
2
|
|
|
|
|
49
|
my @buf; |
137
|
2
|
|
|
|
|
11
|
my @fh = map $_->openr_raw, $file, $content; |
138
|
2
|
|
|
|
|
344
|
while( $fh[0]->sysread( $buf[0], $BUFF_SIZE ) ) { |
139
|
3
|
|
|
|
|
61
|
$fh[1]->sysread( $buf[1], $BUFF_SIZE ); |
140
|
3
|
100
|
|
|
|
96
|
croak "Collision found for $file and $content: content differs" |
141
|
|
|
|
|
|
|
if $buf[0] ne $buf[1]; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# link both files |
146
|
9
|
|
|
|
|
209
|
$content->parent->mkpath; |
147
|
9
|
100
|
|
|
|
1702
|
my ( $old, $new ) = -e $content ? ( $content, $file ) : ( $file, $content ); |
148
|
|
|
|
|
|
|
|
149
|
9
|
50
|
|
|
|
188
|
return if $old eq $new; # do not link a file to itself |
150
|
9
|
|
|
|
|
63
|
$new->remove; |
151
|
9
|
50
|
|
|
|
258
|
link $old, $new or croak "Failed linking $new to $old: $!"; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# optionally remove the write permissions |
154
|
9
|
100
|
|
|
|
250
|
$old->chmod( $old->stat->mode & 07777 | 0222 ^ 0222 ) |
155
|
|
|
|
|
|
|
if $self->make_read_only; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# add the inode to the cache |
158
|
9
|
|
|
|
|
1266
|
$self->inode->{ $content->stat->ino } = |
159
|
|
|
|
|
|
|
$content->relative( $self->path )->stringify; |
160
|
|
|
|
|
|
|
|
161
|
9
|
|
|
|
|
2763
|
return $content; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub link_dir { |
165
|
5
|
|
|
5
|
1
|
2316
|
state $check = compile( Object, slurpy ArrayRef[Dir] ); |
166
|
5
|
|
|
|
|
11491
|
my ( $self, $dirs ) = $check->(@_); |
167
|
|
|
|
|
|
|
|
168
|
18
|
100
|
|
18
|
|
2049
|
$_->visit( sub { $self->link_file($_) if -f }, { recurse => 1 } ) |
169
|
5
|
|
|
|
|
413
|
for @$dirs; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub fsck { |
173
|
2
|
|
|
2
|
1
|
5873
|
my ($self) = @_; |
174
|
|
|
|
|
|
|
$self->path->visit( |
175
|
|
|
|
|
|
|
sub { |
176
|
14
|
|
|
14
|
|
2617
|
my ( $path, $state ) = @_; |
177
|
|
|
|
|
|
|
|
178
|
14
|
100
|
|
|
|
32
|
if ( -d $path ) { |
|
|
100
|
|
|
|
|
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# empty directory |
181
|
7
|
100
|
|
|
|
87
|
push @{ $state->{empty} }, $path unless $path->children; |
|
1
|
|
|
|
|
53
|
|
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
elsif( -l $path ) { |
184
|
1
|
|
|
|
|
25
|
push @{ $state->{symlink} }, $path; |
|
1
|
|
|
|
|
5
|
|
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
else { |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# orphan content file |
189
|
6
|
100
|
|
|
|
147
|
push @{ $state->{orphan} }, $path |
|
1
|
|
|
|
|
121
|
|
190
|
|
|
|
|
|
|
if $path->stat->nlink == 1; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# content does not match name |
193
|
6
|
|
|
|
|
642
|
my $digest = $path->digest( $DIGEST_OPTS, $self->digest ); |
194
|
6
|
100
|
|
|
|
1876
|
push @{ $state->{corrupted} }, $path |
|
1
|
|
|
|
|
207
|
|
195
|
|
|
|
|
|
|
if $digest ne $path->relative( $self->path ) =~ s{/}{}gr; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
}, |
198
|
2
|
|
|
|
|
21
|
{ recurse => 1 }, |
199
|
|
|
|
|
|
|
); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
1; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
__END__ |