blib/lib/File/ContentStore.pm

filter: Statementstmt 97.4% 114 / 117 Branchbran 89.1% 41 / 46 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 24 / 24 Podpod 60.0% 3 / 5 totaltotal 92.4% 195 / 211 CC 30 SCAR 18.9
CC
30
Cov
94%
link_file
16.5
__ANON__
6.0
__ANON__
4.1
CRAP 6.6
↑ Summary
1 package File::ContentStore;
2 3 $File::ContentStore::VERSION = '1.006';
3 3 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use List::Util qw( sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Types::Standard qw( slurpy Object Bool Int Str ArrayRef HashRef CodeRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Types::Path::Tiny qw( Dir File );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Type::Params qw( signature_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 has path => (
19 is => 'ro',
20 isa => Dir,
21 required => 1,
22 coerce => 1,
23 );
24  
25 3 has digest => (
26 is => 'ro',
27 isa => Str,
28 default => 'SHA-1',
29 );
30  
31 has parts => (
32 is => 'lazy',
33 builder =>
34 6 ▶ sub { int( length( Digest->new( shift->digest )->hexdigest ) / 32 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 3 init_arg => undef,
36 );
37  
38 3 has check_for_collisions => (
39 is => 'ro',
40 isa => Bool,
41 required => 1,
42 default => 1,
43 );
44  
45 3 has make_read_only => (
46 is => 'ro',
47 isa => Bool,
48 default => 1,
49 );
50  
51 3 has file_callback => (
52 is => 'ro',
53 isa => CodeRef,
54 predicate => 1,
55 );
56  
57 has inode => (
58 is => 'lazy',
59 isa => HashRef,
60 init_arg => undef,
61 6 ▶ builder => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 6 my $re = qr{
63 ^
64 6 ${ \( '[a-f0-9][a-f0-9]/' x $self->parts ) }
65 6 ${ \( '[a-f0-9]'
66 x ( length( Digest->new( $self->digest )->hexdigest )
67 - 2 * $self->parts ) ) }
68 $
69 }x;
70 6 my $root = $self->path;
71 $root->visit(
72 4 ▶ sub ( $path, $inode ) {
Subroutine __ANON__: called CC=4 SCAR=14.2
73 4 my $rel = $path->relative($root)->stringify;
74 4 ▶ $inode->{ $path->stat->ino } = $rel
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and not -l $_ and $rel =~ /$re/u 2 2 4
Condition66%4/6
-f $_ and not -l $_ and $rel =~ /$re/u
!ll&&!rl&&r
202
-f $_ and not -l $_
!ll&&!rl&&r
202
MC/DC33%1/3
-f $_ and not -l $_ and $rel =~ /$re/u
-f $_ not -l $_ $rel =~ /$re/u
Truth table50%2/4
A and not(B) and C
A= -f $_B= not -l $_C= $rel =~ /$re/u
ABCresult
0XX0
10X0
1100
1111
75 if -f && !-l && $rel =~ $re;
76 },
77 6 { recurse => 1 }
78 );
79 },
80 3 );
81  
82 has dev => (
83 is => 'lazy',
84 isa => Int,
85 init_arg => undef,
86 5 ▶ builder => sub ($self) { $self->path->stat->dev },
Subroutine _build_dev: called CC=1 SCAR=0.0
87 3 );
88  
89 # if a single non-hashref argument is given, assume it's 'path'
90 8 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=13.9
Pod undocumented
91 8 my $class = shift;
92 scalar @_ == 1
93 ? ref $_[0] eq 'HASH'
94 8 ▶ ? { %{ $_[0] } }
Branch: 3 branches
BranchTrueFalseTotal
scalar @_ == 1 ? : 5 3 8
ref $_[0] eq 'HASH' ? : 1 4 5
@_ % 2 ? : 0 3 3
95 : { path => $_[0] }
96 : @_ % 2 ? Carp::croak(
97 "The new() method for $class expects a hash reference or a"
98 . " key/value list. You passed an odd number of arguments" )
99 : {@_};
100 }
101  
102 7 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
103 7 Digest->new( shift->digest ); # dies if 'digest' is not installed
104 }
105  
106 3 my $BUFF_SIZE = 1024 * 32;
107 3 my $DIGEST_OPTS = { chunk_size => $BUFF_SIZE };
108  
109 3 signature_for link_file => ( positional => [ Object, File ] );
110 3 signature_for link_dir => ( positional => [ Object, slurpy ArrayRef [Dir] ] );
111  
112 21 ▶sub link_file ( $self, $file ) {
Subroutine link_file: called CC=16 SCAR=28.0
113  
114 # skip non-files and symbolic links
115 21 ▶ return unless -f $file && !-l $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and not -l $file 4 17 21
Condition66%2/3
-f $file and not -l $file
!ll&&!rl&&r
0417
MC/DC50%1/2
-f $file and not -l $file
-f $file not -l $file
Truth table66%2/3
A and not(B)
A= -f $fileB= not -l $file
ABresult
0X0
100
111
116  
117 17 my $root = $self->path;
118  
119 # skip files on a different filesystem than the store
120 17 my $stat = $file->stat;
121 17 ▶ if ( $stat->dev != $self->dev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat->dev != $self->dev) 0 17 17
122 0 local our @CARP_NOT = qw( Path::Tiny );
123 0 carp sprintf
124 "%s (%d) and %s (%d) are on different devices. Skipping",
125 $file, $stat->dev, $root, $self->dev;
126 0 return;
127 }
128  
129 17 my ( $digest, $content, $done );
130  
131 # check if the file's inode is in the cache already
132 17 ▶ if ( $content = $self->inode->{ $stat->ino } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content = $self->inode->{$stat->ino}) { } 6 11 17
133 6 $digest = $content =~ s{/}{}gr;
134 6 $content = $root->child($content);
135 6 $done = 1;
136 }
137  
138 # compute content file name
139 else {
140 11 my $parts = $self->parts;
141 11 $digest = $file->digest( $DIGEST_OPTS, $self->digest );
142 $content =
143 11 $root->child( map( { substr $digest, 2 * $_, 2 } 0 .. $parts - 1 ),
144 substr( $digest, 2 * $parts ) );
145 }
146  
147 17 ▶ $self->file_callback->( $file, $digest, $content )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_file_callback 2 15 17
148 if $self->has_file_callback;
149  
150 # if the inode is already in our store, there's nothing left to do
151 17 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 6 11 17
152  
153 # check for collisions
154 11 ▶ if( -e $content && $self->check_for_collisions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $content and $self->check_for_collisions) 3 8 11
Condition100%3/3
-e $content and $self->check_for_collisions
!ll&&!rl&&r
713
MC/DC100%2/2
-e $content and $self->check_for_collisions
-e $content $self->check_for_collisions
Truth table100%3/3
A and B
A= -e $contentB= $self->check_for_collisions
ABresult
0X0
100
111
155 3 ▶ croak "Collision found for $file and $content: size differs"
Branch: 1 branch
BranchTrueFalseTotal
if -s $file != -s $content 0 3 3
156 if -s $file != -s $content;
157  
158 3 my @buf;
159 3 my @fh = map $_->openr_raw, $file, $content;
160 3 ▶ while( $fh[0]->sysread( $buf[0], $BUFF_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh[0]->sysread($buf[0], $BUFF_SIZE)) 4 2 6
161 4 $fh[1]->sysread( $buf[1], $BUFF_SIZE );
162 4 ▶ croak "Collision found for $file and $content: content differs"
Branch: 1 branch
BranchTrueFalseTotal
if $buf[0] ne $buf[1] 1 3 4
163 if $buf[0] ne $buf[1];
164 }
165 }
166  
167 # link both files
168 10 $content->parent->mkpath;
169 10 ▶ my ( $old, $new ) = -e $content ? ( $content, $file ) : ( $file, $content );
Branch: 1 branch
BranchTrueFalseTotal
-e $content ? : 3 7 10
170  
171 10 ▶ return if $old eq $new; # do not link a file to itself
Branch: 1 branch
BranchTrueFalseTotal
if $old eq $new 0 10 10
172 10 $new->remove;
173 10 ▶ link $old, $new or croak "Failed linking $new to $old: $!";
Branch: 1 branch
BranchTrueFalseTotal
link $old, $new or croak("Failed linking $new to ${old}: $!") 10 0 10
174  
175 # optionally remove the write permissions
176 10 ▶ $old->chmod( $old->stat->mode & 07777 | 0222 ^ 0222 )
Branch: 1 branch
BranchTrueFalseTotal
if $self->make_read_only 9 1 10
177 if $self->make_read_only;
178  
179 # add the inode to the cache
180 10 $self->inode->{ $content->stat->ino } =
181 $content->relative($root)->stringify;
182  
183 10 return $content;
184 }
185  
186 6 ▶sub link_dir ( $self, $dirs ) {
Subroutine link_dir: called CC=2 SCAR=6.9
187 23 ▶ $_->visit( sub { $self->link_file($_) if -f }, { recurse => 1 } )
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ 21 2 23
188 6 for @$dirs;
189 }
190  
191 2 ▶sub fsck ($self) {
Subroutine fsck: called CC=1 SCAR=0.0
Pod documented
192 2 my $root = $self->path;
193 $root->visit(
194 14 ▶ sub ( $path, $state ) {
Subroutine __ANON__: called CC=6 SCAR=17.9
195  
196 14 ▶ if ( -l $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $path) { } 1 13 14
elsif (-d $path) { } 7 6 13
197 1 push @{ $state->{symlink} }, $path;
198 }
199 elsif ( -d $path ) {
200  
201 # empty directory
202 7 ▶ push @{ $state->{empty} }, $path unless $path->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->children 1 6 7
203 }
204 else {
205  
206 # orphan content file
207 6 ▶ push @{ $state->{orphan} }, $path
Branch: 1 branch
BranchTrueFalseTotal
if $path->stat->nlink == 1 1 5 6
208 if $path->stat->nlink == 1;
209  
210 # content does not match name
211 6 my $digest = $path->digest( $DIGEST_OPTS, $self->digest );
212 6 ▶ push @{ $state->{corrupted} }, $path
Branch: 1 branch
BranchTrueFalseTotal
if $digest ne $path->relative($root) =~ s[/][]gru 1 5 6
213 if $digest ne $path->relative($root) =~ s{/}{}gr;
214 }
215 },
216 2 { recurse => 1 },
217 );
218 }
219  
220 7 ▶sub stats ($self) {
Subroutine stats: called CC=2 SCAR=6.9
Pod documented
221 7 my $root = $self->path;
222 7 my $stats = {};
223 7 for my $path ( values $self->inode->%* ) {
224 10 my $stat = $root->child($path)->stat;
225 10 my $nlink = $stat->nlink;
226 10 my $size = $stat->size;
227 10 $stats->{$nlink}{count}++;
228 10 $stats->{$nlink}{total} += $size;
229 }
230 7 return $stats;
231 }
232  
233 3 ▶sub stats_summary ($self) {
Subroutine stats_summary: called CC=1 SCAR=0.0
Pod documented
234 3 my $stats = $self->stats;
235 return {
236 store_count => sum( 0, map $_->{count}, values %$stats ),
237 store_total => sum( 0, map $_->{total}, values %$stats ),
238 linked_count => sum( 0, map $stats->{$_}{count} * ( $_ - 1 ), keys %$stats ),
239 3 linked_total => sum( 0, map $stats->{$_}{total} * ( $_ - 1 ), keys %$stats ),
240 };
241 }
242  
243 3 1;
244  
245 __END__
↑ Summary