blib/lib/File/ContentStore.pm

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