blib/lib/File/ContentStore.pm

filter: Statementstmt 94.8% 92 / 97 Branchbran 90.9% 40 / 44 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 94.7% 18 / 19 Podpod 60.0% 3 / 5 totaltotal 91.1% 164 / 180 SCAR 33.1
CC
27
Cov
93%
link_file
15.1
__ANON__
6.0
BUILDARGS
4.0
CRAP 27.3
↑ Summary
1 package File::ContentStore;
2 3 $File::ContentStore::VERSION = '1.003';
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Standard qw( slurpy Object Bool Str ArrayRef HashRef CodeRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Types::Path::Tiny qw( Dir File );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Type::Params qw( compile );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 has path => (
15 is => 'ro',
16 isa => Dir,
17 required => 1,
18 coerce => 1,
19 );
20  
21 3 has digest => (
22 is => 'ro',
23 isa => Str,
24 default => 'SHA-1',
25 );
26  
27 has parts => (
28 is => 'lazy',
29 builder =>
30 6 ▶ sub { int( length( Digest->new( shift->digest )->hexdigest ) / 32 ) },
Subroutine _build_parts: called CC=1 SCAR=0.0
31 3 init_arg => undef,
32 );
33  
34 3 has check_for_collisions => (
35 is => 'ro',
36 isa => Bool,
37 required => 1,
38 default => 1,
39 );
40  
41 3 has make_read_only => (
42 is => 'ro',
43 isa => Bool,
44 required => 1,
45 default => 1,
46 );
47  
48 3 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 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine _build_inode: called CC=1 SCAR=0.4
60 0 my $re = qr{
61 ^
62 0 ${ \( '[a-f0-9][a-f0-9]/' x $self->parts ) }
63 0 ${ \( '[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 ▶ my ( $path, $inode ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
71 4 my $rel = $path->relative( $self->path )->stringify;
72 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
73 if -f && $rel =~ $re;
74 },
75 0 { recurse => 1 }
76 );
77 },
78 3 );
79  
80 # if a single non-hashref argument is given, assume it's 'path'
81 sub BUILDARGS {
82 8 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=4 SCAR=13.9
Pod undocumented
83 scalar @_ == 1
84 ? ref $_[0] eq 'HASH'
85 8 ▶ ? { %{ $_[0] } }
Branch: 3 branches
BranchTrueFalseTotal
scalar @_ == 1 ? : 5 3 8
ref $_[0] eq 'HASH' ? : 1 4 5
@_ % 2 ? : 0 3 3
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 ▶ Digest->new( shift->digest ); # dies if 'digest' is not installed
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
95 }
96  
97 3 my $BUFF_SIZE = 1024 * 32;
98 3 my $DIGEST_OPTS = { chunk_size => $BUFF_SIZE };
99  
100 sub link_file {
101 16 ▶ state $check = compile( Object, File );
Subroutine link_file: called CC=15 SCAR=27.1
Pod documented
102 16 my ( $self, $file ) = $check->(@_);
103  
104 # skip non-files and symbolic links
105 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
106  
107 12 my ( $digest, $content, $done );
108  
109 # check if the file's inode is in the cache
110 12 ▶ if ( $content = $self->inode->{ $file->stat->ino } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content = $self->inode->{$file->stat->ino}) { } 2 10 12
111 2 $digest = $content =~ s{/}{}gr;
112 2 $content = $self->path->child($content);
113 2 $done = 1;
114 }
115  
116 # compute content file name
117 else {
118 10 $digest = $file->digest( $DIGEST_OPTS, $self->digest );
119 $content =
120 $self->path->child(
121 10 map( { substr $digest, 2 * $_, 2 } 0 .. $self->parts - 1 ),
122 substr( $digest, 2 * $self->parts ) );
123 }
124  
125 12 ▶ $self->file_callback->( $file, $digest, $content )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_file_callback 2 10 12
126 if $self->has_file_callback;
127  
128 # if the inode is already in our store, there's nothing left to do
129 12 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 2 10 12
130  
131 # check for collisions
132 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
133 2 ▶ croak "Collision found for $file and $content: size differs"
Branch: 1 branch
BranchTrueFalseTotal
if -s $file != -s $content 0 2 2
134 if -s $file != -s $content;
135  
136 2 my @buf;
137 2 my @fh = map $_->openr_raw, $file, $content;
138 2 ▶ while( $fh[0]->sysread( $buf[0], $BUFF_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh[0]->sysread($buf[0], $BUFF_SIZE)) 3 1 4
139 3 $fh[1]->sysread( $buf[1], $BUFF_SIZE );
140 3 ▶ croak "Collision found for $file and $content: content differs"
Branch: 1 branch
BranchTrueFalseTotal
if $buf[0] ne $buf[1] 1 2 3
141 if $buf[0] ne $buf[1];
142 }
143 }
144  
145 # link both files
146 9 $content->parent->mkpath;
147 9 ▶ my ( $old, $new ) = -e $content ? ( $content, $file ) : ( $file, $content );
Branch: 1 branch
BranchTrueFalseTotal
-e $content ? : 2 7 9
148  
149 9 ▶ return if $old eq $new; # do not link a file to itself
Branch: 1 branch
BranchTrueFalseTotal
if $old eq $new 0 9 9
150 9 $new->remove;
151 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
152  
153 # optionally remove the write permissions
154 9 ▶ $old->chmod( $old->stat->mode & 07777 | 0222 ^ 0222 )
Branch: 1 branch
BranchTrueFalseTotal
if $self->make_read_only 8 1 9
155 if $self->make_read_only;
156  
157 # add the inode to the cache
158 9 $self->inode->{ $content->stat->ino } =
159 $content->relative( $self->path )->stringify;
160  
161 9 return $content;
162 }
163  
164 sub link_dir {
165 5 ▶ state $check = compile( Object, slurpy ArrayRef[Dir] );
Subroutine link_dir: called CC=2 SCAR=6.9
Pod documented
166 5 my ( $self, $dirs ) = $check->(@_);
167  
168 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
169 5 for @$dirs;
170 }
171  
172 sub fsck {
173 2 ▶ my ($self) = @_;
Subroutine fsck: called CC=1 SCAR=0.0
Pod documented
174 $self->path->visit(
175 sub {
176 14 ▶ my ( $path, $state ) = @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
177  
178 14 ▶ if ( -d $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 7 7 14
elsif (-l $path) { } 1 6 7
179  
180 # empty directory
181 7 ▶ push @{ $state->{empty} }, $path unless $path->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->children 1 6 7
182 }
183 elsif( -l $path ) {
184 1 push @{ $state->{symlink} }, $path;
185 }
186 else {
187  
188 # orphan content file
189 6 ▶ push @{ $state->{orphan} }, $path
Branch: 1 branch
BranchTrueFalseTotal
if $path->stat->nlink == 1 1 5 6
190 if $path->stat->nlink == 1;
191  
192 # content does not match name
193 6 my $digest = $path->digest( $DIGEST_OPTS, $self->digest );
194 6 ▶ push @{ $state->{corrupted} }, $path
Branch: 1 branch
BranchTrueFalseTotal
if $digest ne $path->relative($self->path) =~ s[/][]gru 1 5 6
195 if $digest ne $path->relative( $self->path ) =~ s{/}{}gr;
196 }
197 },
198 2 { recurse => 1 },
199 );
200 }
201  
202 3 1;
203  
204 __END__
↑ Summary