Filesystem/BlobStore.pm

filter: Statementstmt 96.9% 255 / 263 Branchbran 71.6% 116 / 162 Conditioncond 55.0% 38 / 69 MC/DCmcdc 30.0% 12 / 40 Subroutinesub 98.0% 49 / 50 Podpod 100% 6 / 6 totaltotal 80.6% 476 / 590 CC 106 SCAR 17.9
CC
106
Cov
83%
read
14.8
_range_options
13.1
_open_blob
8.5
CRAP 6.0
« Filesystem.pm ↑ Summary
1 package Net::Blossom::Server::Backend::Filesystem::BlobStore;
2  
3 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Class::Tiny qw(root generation);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Crypt::PRNG qw(random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Errno qw(EEXIST ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Fcntl qw(O_RDONLY SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use File::Sync qw(fsync);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=8 SCAR=21.2
Pod documented
17 10 my $class = shift;
18 10 my %args = _constructor_args(@_);
19 10 my %known = map { $_ => 1 } qw(root generation);
20 10 my @unknown = grep { !$known{$_} } keys %args;
21 10 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 10 10
22  
23 croak "root is required"
24 10 ▶ unless defined $args{root} && !ref($args{root}) && length $args{root};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"root"} and not ref $args{"root"} and length $args{"root"} 1 9 10
Condition66%4/6
defined $args{"root"} and not ref $args{"root"} and length $args{"root"}
!ll&&!rl&&r
109
defined $args{"root"} and not ref $args{"root"}
!ll&&!rl&&r
109
MC/DC33%1/3
defined $args{"root"} and not ref $args{"root"} and length $args{"root"}
defined $args{"root"} not ref $args{"root"} length $args{"root"}
Truth table50%2/4
A and not(B) and C
A= defined $args{"root"}B= not ref $args{"root"}C= length $args{"root"}
ABCresult
0XX0
10X0
1100
1111
25 9 ▶ croak "root contains unsafe characters" if $args{root} =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"root"} =~ /\0/ 0 9 9
26 9 my $root = File::Spec->rel2abs($args{root});
27  
28 my $generation = defined $args{generation}
29 ? $args{generation}
30 0 ▶ : sub { return unpack 'H*', random_bytes(16) };
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'generation'} ? : 6 3 9
31 9 ▶ croak "generation must be a code reference" unless ref($generation) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $generation eq "CODE" 1 8 9
32  
33 return {
34 8 root => $root,
35 generation => $generation,
36 };
37 }
38  
39 8 ▶sub deploy_schema {
Subroutine deploy_schema: called CC=4 SCAR=13.9
Pod documented
40 8 my ($self) = @_;
41 8 my @created = _ensure_directory($self->root, 'root');
42 7 _sync_directory(_parent_directory($_)) for @created;
43 7 ▶ _sync_directory($self->root)
Branch: 1 branch
BranchTrueFalseTotal
if _ensure_directory($self->_blob_dir, "blob") 6 1 7
44 if _ensure_directory($self->_blob_dir, 'blob');
45 7 ▶ _sync_directory($self->root)
Branch: 1 branch
BranchTrueFalseTotal
if _ensure_directory($self->_staging_dir, "staging") 6 1 7
46 if _ensure_directory($self->_staging_dir, 'staging');
47 7 return 1;
48 }
49  
50 9 ▶sub begin_upload {
Subroutine begin_upload: called CC=3 SCAR=11.2
Pod documented
51 9 my ($self) = @_;
52 9 my $staging = $self->_staging_dir;
53 9 ▶ croak "filesystem staging directory is not deployed" unless -d $staging;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $staging 0 9 9
54 9 my ($fh, $path) = tempfile(
55 'net-blossom-filesystem-upload-XXXXXX',
56 DIR => $staging,
57 UNLINK => 0,
58 );
59 9 ▶ binmode $fh or croak "unable to binmode upload staging file: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("unable to binmode upload staging file: $!") 9 0 9
60 9 return Net::Blossom::Server::Backend::Filesystem::BlobStore::_Upload->new(
61 store => $self,
62 fh => $fh,
63 path => $path,
64 );
65 }
66  
67 11 ▶sub get_blob {
Subroutine get_blob: called CC=4 SCAR=13.9
Pod documented
68 11 my ($self, $storage_key, %opts) = @_;
69 11 my ($fh, $size) = $self->_open_blob($storage_key, %opts);
70 5 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 1 4 5
71 4 ▶ if (!$size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size) 1 3 4
72 1 ▶ close $fh or croak "unable to close filesystem object: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("unable to close filesystem object: $!") 1 0 1
73 1 return '';
74 }
75  
76 3 return Net::Blossom::Server::Backend::Filesystem::BlobStore::_Stream->new(
77 fh => $fh,
78 size => $size,
79 );
80 }
81  
82 2 ▶sub get_blob_range {
Subroutine get_blob_range: called CC=6 SCAR=18.3
Pod documented
83 2 my ($self, $storage_key, %opts) = @_;
84 2 my %known = map { $_ => 1 } qw(offset length size);
85 2 my @unknown = grep { !$known{$_} } keys %opts;
86 2 ▶ croak "unknown option(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 2 2
87 2 _range_options(\%opts);
88  
89 my ($fh, $size) = $self->_open_blob(
90 $storage_key,
91 2 ▶ (defined $opts{size} ? (size => $opts{size}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'size'} ? : 2 0 2
92 );
93 2 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 2 2
94 croak "range exceeds file size"
95 2 ▶ if $opts{offset} + $opts{length} > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"offset"} + $opts{"length"} > $size 1 1 2
96 1 ▶ seek($fh, $opts{offset}, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $opts{"offset"}, 0 or croak("unable to seek filesystem object: $!") 1 0 1
97 or croak "unable to seek filesystem object: $!";
98  
99 return Net::Blossom::Server::Backend::Filesystem::BlobStore::_Stream->new(
100 fh => $fh,
101 size => $size,
102 remaining => $opts{length},
103 1 );
104 }
105  
106 13 ▶sub _open_blob {
Subroutine _open_blob: called CC=8 SCAR=21.3
107 13 my ($self, $storage_key, %opts) = @_;
108 13 my $path = $self->_path_for_key($storage_key);
109 8 ▶ sysopen my $fh, $path, O_RDONLY or do {
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $path, 0 or do { return if $! == 2; croak("unable to open filesystem object: $!") } 7 1 8
110 1 ▶ return if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 1 0 1
111 0 croak "unable to open filesystem object: $!";
112 };
113 7 ▶ binmode $fh or croak "unable to binmode filesystem object: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("unable to binmode filesystem object: $!") 7 0 7
114 7 ▶ croak "filesystem object is not a regular file" unless -f $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fh 0 7 7
115 7 my $size = -s $fh;
116 7 ▶ croak "unable to stat filesystem object: $!" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 7 7
117 croak "file size does not match metadata"
118 7 ▶ if defined $opts{size} && $size != $opts{size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"size"} and $size != $opts{"size"} 1 6 7
Condition100%3/3
defined $opts{"size"} and $size != $opts{"size"}
!ll&&!rl&&r
421
MC/DC100%2/2
defined $opts{"size"} and $size != $opts{"size"}
defined $opts{"size"} $size != $opts{"size"}
Truth table100%3/3
A and B
A= defined $opts{"size"}B= $size != $opts{"size"}
ABresult
0X0
100
111
119 6 return ($fh, $size);
120 }
121  
122 2 ▶sub delete_blob {
Subroutine delete_blob: called CC=3 SCAR=11.2
Pod documented
123 2 my ($self, $storage_key) = @_;
124 2 my $path = $self->_path_for_key($storage_key);
125 2 ▶ if (!unlink $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $path) 1 1 2
126 1 ▶ return 0 if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 1 0 1
127 0 croak "unable to delete filesystem object: $!";
128 }
129 1 _sync_directory(_parent_directory($path));
130 1 return 1;
131 }
132  
133 7 ▶sub _storage_key {
Subroutine _storage_key: called CC=7 SCAR=21.0
134 7 my ($self, $sha256) = @_;
135 7 ▶ croak "sha256 must be 64 lowercase hexadecimal characters"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/ 1 6 7
Condition50%3/6
defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/
!ll&&!rl&&r
016
defined $sha256 and not ref $sha256
!ll&&!rl&&r
007
MC/DC33%1/3
defined $sha256 and not ref $sha256 and $sha256 =~ /\A[0-9a-f]{64}\z/
defined $sha256 not ref $sha256 $sha256 =~ /\A[0-9a-f]{64}\z/
Truth table50%2/4
A and not(B) and C
A= defined $sha256B= not ref $sha256C= $sha256 =~ /\A[0-9a-f]{64}\z/
ABCresult
0XX0
10X0
1100
1111
136 unless defined $sha256 && !ref($sha256)
137 && $sha256 =~ /\A[0-9a-f]{64}\z/;
138 6 my $generation = $self->generation->();
139 6 ▶ croak "generation returned an unsafe filesystem path segment"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/ 1 5 6
Condition50%3/6
defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
!ll&&!rl&&r
015
defined $generation and not ref $generation
!ll&&!rl&&r
006
MC/DC33%1/3
defined $generation and not ref $generation and $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
defined $generation not ref $generation $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
Truth table50%2/4
A and not(B) and C
A= defined $generationB= not ref $generationC= $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/
ABCresult
0XX0
10X0
1100
1111
140 unless defined $generation
141 && !ref($generation)
142 && $generation =~ /\A[A-Za-z0-9][A-Za-z0-9._-]*\z/;
143  
144 5 return join '/', substr($sha256, 0, 2), substr($sha256, 2, 2),
145 "$sha256-$generation";
146 }
147  
148 20 ▶sub _path_for_key {
Subroutine _path_for_key: called CC=6 SCAR=18.6
149 20 my ($self, $storage_key) = @_;
150 20 ▶ croak "invalid filesystem storage key"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $storage_key and not ref $storage_key 0 20 20
Condition33%1/3
defined $storage_key and not ref $storage_key
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $storage_key and not ref $storage_key
defined $storage_key not ref $storage_key
Truth table33%1/3
A and not(B)
A= defined $storage_keyB= not ref $storage_key
ABresult
0X0
100
111
151 unless defined $storage_key && !ref($storage_key);
152 20 my ($first, $second, $sha256) = $storage_key =~ m{
153 \A([0-9a-f]{2})/([0-9a-f]{2})/
154 ([0-9a-f]{64})-[A-Za-z0-9][A-Za-z0-9._-]*\z
155 }x;
156 20 ▶ croak "invalid filesystem storage key" unless defined $sha256;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sha256 4 16 20
157 16 ▶ croak "invalid filesystem storage key"
Branch: 1 branch
BranchTrueFalseTotal
unless $first eq substr($sha256, 0, 2) and $second eq substr($sha256, 2, 2) 1 15 16
Condition66%2/3
$first eq substr($sha256, 0, 2) and $second eq substr($sha256, 2, 2)
!ll&&!rl&&r
1015
MC/DC50%1/2
$first eq substr($sha256, 0, 2) and $second eq substr($sha256, 2, 2)
$first eq substr($sha256, 0, 2) $second eq substr($sha256, 2, 2)
Truth table66%2/3
A and B
A= $first eq substr($sha256, 0, 2)B= $second eq substr($sha256, 2, 2)
ABresult
0X0
100
111
158 unless $first eq substr($sha256, 0, 2)
159 && $second eq substr($sha256, 2, 2);
160 15 return File::Spec->catfile($self->_blob_dir, split m{/}, $storage_key);
161 }
162  
163 5 ▶sub _publish {
Subroutine _publish: called CC=4 SCAR=14.1
164 5 my ($self, $staging_path, $storage_key) = @_;
165 5 my $path = $self->_path_for_key($storage_key);
166 5 my $parent = $self->_ensure_blob_shards($storage_key);
167  
168 5 ▶ if (!link $staging_path, $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (link $staging_path, $path) 1 4 5
169 1 ▶ croak "filesystem object already exists" if $! == EEXIST;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 17 1 0 1
170 0 croak "unable to publish filesystem object: $!";
171 }
172 4 _sync_directory($parent);
173 4 ▶ unlink $staging_path
Branch: 1 branch
BranchTrueFalseTotal
unlink $staging_path or croak("unable to remove upload staging file: $!") 4 0 4
174 or croak "unable to remove upload staging file: $!";
175 4 _sync_directory($self->_staging_dir);
176 4 return 1;
177 }
178  
179 5 ▶sub _ensure_blob_shards {
Subroutine _ensure_blob_shards: called CC=3 SCAR=11.0
180 5 my ($self, $storage_key) = @_;
181 5 my ($first, $second) = split m{/}, $storage_key;
182 5 my $first_path = File::Spec->catdir($self->_blob_dir, $first);
183 5 ▶ _sync_directory($self->_blob_dir)
Branch: 1 branch
BranchTrueFalseTotal
if _ensure_directory($first_path, "blob shard") 4 1 5
184 if _ensure_directory($first_path, 'blob shard');
185 5 my $second_path = File::Spec->catdir($first_path, $second);
186 5 ▶ _sync_directory($first_path)
Branch: 1 branch
BranchTrueFalseTotal
if _ensure_directory($second_path, "blob shard") 4 1 5
187 if _ensure_directory($second_path, 'blob shard');
188 5 return $second_path;
189 }
190  
191 31 ▶sub _blob_dir {
Subroutine _blob_dir: called CC=1 SCAR=0.0
192 31 my ($self) = @_;
193 31 return File::Spec->catdir($self->root, 'blobs');
194 }
195  
196 25 ▶sub _staging_dir {
Subroutine _staging_dir: called CC=1 SCAR=0.0
197 25 my ($self) = @_;
198 25 return File::Spec->catdir($self->root, '.staging');
199 }
200  
201 32 ▶sub _ensure_directory {
Subroutine _ensure_directory: called CC=7 SCAR=19.5
202 32 my ($path, $name) = @_;
203 32 my @created;
204 32 ▶ if (!-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path) 23 9 32
205 23 ▶ eval { @created = make_path($path); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @created = make_path($path); 1 } } or croak("unable to create $name directory: $@") 23 0 23
206 or croak "unable to create $name directory: $@";
207 }
208 32 ▶ croak "$name path is not a directory" unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 1 31 32
209 31 ▶ croak "$name directory is not writable" unless -w $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $path 0 31 31
210 31 ▶ return wantarray ? @created : @created ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 7 24 31
@created ? : 20 4 24
211 }
212  
213 33 ▶sub _sync_directory {
Subroutine _sync_directory: called CC=4 SCAR=14.6
214 33 my ($path) = @_;
215 33 ▶ sysopen my $fh, $path, O_RDONLY
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $path, 0 or croak("unable to open filesystem directory for sync: $!") 33 0 33
216 or croak "unable to open filesystem directory for sync: $!";
217 33 ▶ fsync($fh) or croak "unable to sync filesystem directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
&fsync($fh) or croak("unable to sync filesystem directory: $!") 33 0 33
218 33 ▶ close $fh or croak "unable to close filesystem directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("unable to close filesystem directory: $!") 33 0 33
219 33 return 1;
220 }
221  
222 5 ▶sub _parent_directory {
Subroutine _parent_directory: called CC=1 SCAR=0.0
223 5 my ($path) = @_;
224 5 my ($volume, $directories) = File::Spec->splitpath($path);
225 5 return File::Spec->catpath($volume, $directories, '');
226 }
227  
228 10 ▶sub _constructor_args {
Subroutine _constructor_args: called CC=4 SCAR=17.1
229 0 ▶ return %{$_[0]} if @_ == 1 && ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 10 10
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
230 10 ▶ croak "constructor arguments must be name/value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 10 10
231 10 return @_;
232 }
233  
234 2 ▶sub _range_options {
Subroutine _range_options: called CC=7 SCAR=25.7
235 2 my ($opts) = @_;
236 croak "offset must be a non-negative integer"
237 unless defined $opts->{offset}
238 && !ref($opts->{offset})
239 2 ▶ && $opts->{offset} =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/ 0 2 2
Condition33%2/6
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
!ll&&!rl&&r
002
defined $opts->{"offset"} and not ref $opts->{"offset"}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $opts->{"offset"} and not ref $opts->{"offset"} and $opts->{"offset"} =~ /\A[0-9]+\z/
defined $opts->{"offset"} not ref $opts->{"offset"} $opts->{"offset"} =~ /\A[0-9]+\z/
Truth table25%1/4
A and not(B) and C
A= defined $opts->{"offset"}B= not ref $opts->{"offset"}C= $opts->{"offset"} =~ /\A[0-9]+\z/
ABCresult
0XX0
10X0
1100
1111
240 croak "length must be a positive integer"
241 unless defined $opts->{length}
242 && !ref($opts->{length})
243 2 ▶ && $opts->{length} =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/ 0 2 2
Condition33%2/6
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
002
defined $opts->{"length"} and not ref $opts->{"length"}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $opts->{"length"} and not ref $opts->{"length"} and $opts->{"length"} =~ /\A[1-9][0-9]*\z/
defined $opts->{"length"} not ref $opts->{"length"} $opts->{"length"} =~ /\A[1-9][0-9]*\z/
Truth table25%1/4
A and not(B) and C
A= defined $opts->{"length"}B= not ref $opts->{"length"}C= $opts->{"length"} =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
244 2 return;
245 }
246  
247 {
248 package Net::Blossom::Server::Backend::Filesystem::BlobStore::_Upload;
249  
250 7 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 7 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
253 7 ▶ use Class::Tiny qw(store fh path storage_key), {
Subroutine BEGIN: called CC=1 SCAR=0.0
254 prepared => 0,
255 committed => 0,
256 aborted => 0,
257 7 };
258  
259 9 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
260 9 my ($self) = @_;
261 9 $self->prepared;
262 9 $self->committed;
263 9 $self->aborted;
264 9 return;
265 }
266  
267 10 ▶ sub write {
Subroutine write: called CC=5 SCAR=17.0
268 10 my ($self, $chunk) = @_;
269 10 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 10 10
270 10 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 10 10
271 10 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 10 10
272 10 ▶ print {$self->{fh}} $chunk or croak "storage write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"fh"};} $chunk or croak("storage write failed: $!") 10 0 10
273 10 return length $chunk;
274 }
275  
276 8 ▶ sub prepare {
Subroutine prepare: called CC=7 SCAR=19.9
277 8 my ($self, %metadata) = @_;
278 8 ▶ croak "upload is already prepared" if $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prepared"} 0 8 8
279 8 ▶ croak "upload is already committed" if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 0 8 8
280 8 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 8 8
281 8 $self->_sync_and_close;
282  
283 8 my $size = -s $self->{path};
284 8 ▶ croak "unable to stat upload staging file: $!" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 8 8
285 croak "upload size does not match metadata"
286 8 ▶ unless defined $metadata{size} && $metadata{size} == $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $metadata{"size"} and $metadata{"size"} == $size 1 7 8
Condition66%2/3
defined $metadata{"size"} and $metadata{"size"} == $size
!ll&&!rl&&r
017
MC/DC50%1/2
defined $metadata{"size"} and $metadata{"size"} == $size
defined $metadata{"size"} $metadata{"size"} == $size
Truth table66%2/3
A and B
A= defined $metadata{"size"}B= $metadata{"size"} == $size
ABresult
0X0
100
111
287 7 my $storage_key = $self->{store}->_storage_key($metadata{sha256});
288 5 $self->{store}->_publish($self->{path}, $storage_key);
289  
290 4 $self->{path} = undef;
291 4 $self->{storage_key} = $storage_key;
292 4 $self->{prepared} = 1;
293 4 return $storage_key;
294 }
295  
296 5 ▶ sub commit {
Subroutine commit: called CC=4 SCAR=14.0
297 5 my ($self) = @_;
298 5 ▶ return 1 if $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} 1 4 5
299 4 ▶ croak "upload is aborted" if $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} 0 4 4
300 4 ▶ croak "upload is not prepared" unless $self->{prepared};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"prepared"} 0 4 4
301 4 $self->{committed} = 1;
302 4 return 1;
303 }
304  
305 6 ▶ sub abort {
Subroutine abort: called CC=3 SCAR=11.0
306 6 my ($self) = @_;
307 6 ▶ return 1 if $self->{aborted} || $self->{committed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"aborted"} or $self->{"committed"} 1 5 6
Condition66%2/3
$self->{"aborted"} or $self->{"committed"}
l!l&&r!l&&!r
105
MC/DC50%1/2
$self->{"aborted"} or $self->{"committed"}
$self->{"aborted"} $self->{"committed"}
Truth table66%2/3
A or B
A= $self->{"aborted"}B= $self->{"committed"}
ABresult
1X1
011
000
308 5 $self->{aborted} = 1;
309 5 $self->_cleanup;
310 5 return 1;
311 }
312  
313 5 ▶ sub _cleanup {
Subroutine _cleanup: called CC=4 SCAR=14.8
314 5 my ($self) = @_;
315 5 $self->_close;
316 5 ▶ if (defined $self->{path} && -e $self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"path"} and -e $self->{"path"}) 5 0 5
Condition33%1/3
defined $self->{"path"} and -e $self->{"path"}
!ll&&!rl&&r
005
MC/DC0%0/2
defined $self->{"path"} and -e $self->{"path"}
defined $self->{"path"} -e $self->{"path"}
Truth table33%1/3
A and B
A= defined $self->{"path"}B= -e $self->{"path"}
ABresult
0X0
100
111
317 unlink $self->{path}
318 5 ▶ or croak "unable to remove upload staging file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"path"} or croak("unable to remove upload staging file: $!") 5 0 5
319 Net::Blossom::Server::Backend::Filesystem::BlobStore::_sync_directory(
320 $self->{store}->_staging_dir,
321 5 );
322 }
323 5 $self->{path} = undef;
324 5 return 1;
325 }
326  
327 8 ▶ sub _sync_and_close {
Subroutine _sync_and_close: called CC=4 SCAR=14.6
328 8 my ($self) = @_;
329 8 ▶ return 1 unless defined $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fh"} 0 8 8
330 8 ▶ $self->{fh}->flush or croak "unable to flush upload staging file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->flush or croak("unable to flush upload staging file: $!") 8 0 8
331 8 ▶ $self->{fh}->sync or croak "unable to sync upload staging file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->sync or croak("unable to sync upload staging file: $!") 8 0 8
332 8 return $self->_close;
333 }
334  
335 13 ▶ sub _close {
Subroutine _close: called CC=3 SCAR=11.0
336 13 my ($self) = @_;
337 13 ▶ return 1 unless defined $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"fh"} 4 9 13
338 9 ▶ close $self->{fh} or croak "unable to close upload staging file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"fh"} or croak("unable to close upload staging file: $!") 9 0 9
339 9 $self->{fh} = undef;
340 9 return 1;
341 }
342  
343 9 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=14.2
344 9 my ($self) = @_;
345 9 ▶ return if $self->{committed} || $self->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"committed"} or $self->{"aborted"} 9 0 9
Condition66%2/3
$self->{"committed"} or $self->{"aborted"}
l!l&&r!l&&!r
450
MC/DC0%0/2
$self->{"committed"} or $self->{"aborted"}
$self->{"committed"} $self->{"aborted"}
Truth table66%2/3
A or B
A= $self->{"committed"}B= $self->{"aborted"}
ABresult
1X1
011
000
346 0 eval { $self->abort };
347 0 return;
348 }
349 }
350  
351 {
352 7 package Net::Blossom::Server::Backend::Filesystem::BlobStore::_Stream;
353  
354 7 ▶ use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
355  
356 7 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
357 7 ▶ use Class::Tiny qw(fh size remaining), { closed => 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
358  
359 4 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
360 4 my ($self) = @_;
361 4 $self->closed;
362 4 return;
363 }
364  
365 11 ▶ sub read {
Subroutine read: called CC=14 SCAR=27.0
366 11 my ($self, undef, $length) = @_;
367 11 ▶ croak "stream is closed" if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 10 11
368 10 ▶ croak "read length must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $length or ref $length or not $length =~ /\A[0-9]+\z/ 1 9 10
Condition50%3/6
not defined $length or ref $length or not $length =~ /\A[0-9]+\z/
l!l&&r!l&&!r
019
not defined $length or ref $length
l!l&&r!l&&!r
0010
MC/DC33%1/3
not defined $length or ref $length or not $length =~ /\A[0-9]+\z/
not defined $length ref $length not $length =~ /\A[0-9]+\z/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $lengthB= ref $lengthC= not $length =~ /\A[0-9]+\z/
ABCresult
1XX1
01X1
0011
0000
369 if !defined $length || ref($length) || $length !~ /\A[0-9]+\z/;
370 9 $_[1] = '';
371 9 ▶ return 0 unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 1 8 9
372 8 ▶ return 0 if defined $self->{remaining} && !$self->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"remaining"} and not $self->{"remaining"} 1 7 8
Condition100%3/3
defined $self->{"remaining"} and not $self->{"remaining"}
!ll&&!rl&&r
611
MC/DC100%2/2
defined $self->{"remaining"} and not $self->{"remaining"}
defined $self->{"remaining"} not $self->{"remaining"}
Truth table100%3/3
A and not(B)
A= defined $self->{"remaining"}B= not $self->{"remaining"}
ABresult
0X0
100
111
373 $length = $self->{remaining}
374 7 ▶ if defined $self->{remaining} && $length > $self->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"remaining"} and $length > $self->{"remaining"} 1 6 7
Condition66%2/3
defined $self->{"remaining"} and $length > $self->{"remaining"}
!ll&&!rl&&r
601
MC/DC50%1/2
defined $self->{"remaining"} and $length > $self->{"remaining"}
defined $self->{"remaining"} $length > $self->{"remaining"}
Truth table66%2/3
A and B
A= defined $self->{"remaining"}B= $length > $self->{"remaining"}
ABresult
0X0
100
111
375 7 my $read = CORE::read($self->{fh}, $_[1], $length);
376 7 ▶ croak "unable to read filesystem object: $!" unless defined $read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $read 0 7 7
377 croak "filesystem range ended before requested length"
378 7 ▶ if defined $self->{remaining} && !$read;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"remaining"} and not $read 0 7 7
Condition66%2/3
defined $self->{"remaining"} and not $read
!ll&&!rl&&r
610
MC/DC0%0/2
defined $self->{"remaining"} and not $read
defined $self->{"remaining"} not $read
Truth table66%2/3
A and not(B)
A= defined $self->{"remaining"}B= not $read
ABresult
0X0
100
111
379 7 ▶ $self->{remaining} -= $read if defined $self->{remaining};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"remaining"} 1 6 7
380 7 return $read;
381 }
382  
383 2 ▶ sub getline {
Subroutine getline: called CC=2 SCAR=6.9
384 2 my ($self) = @_;
385 2 my $chunk = '';
386 2 my $read = $self->read($chunk, 8192);
387 2 ▶ return unless $read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 1 1 2
388 1 return $chunk;
389 }
390  
391 5 ▶ sub close {
Subroutine close: called CC=3 SCAR=11.0
392 5 my ($self) = @_;
393 5 ▶ return 1 if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 4 5
394 4 ▶ close $self->{fh} or croak "unable to close filesystem object: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $self->{"fh"} or croak("unable to close filesystem object: $!") 4 0 4
395 4 $self->{closed} = 1;
396 4 $self->{fh} = undef;
397 4 return 1;
398 }
399  
400 4 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
401 4 my ($self) = @_;
402 4 ▶ eval { $self->close } unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 3 1 4
403 4 return;
404 }
405 }
406  
407 7 1;
408  
409 =pod
410  
411 =head1 NAME
412  
413 Net::Blossom::Server::Backend::Filesystem::BlobStore - Blossom blob files
414  
415 =head1 DESCRIPTION
416  
417 This module implements L<Net::Blossom::Server::BlobStore> using ordinary files.
418 It is normally constructed by
419 L<Net::Blossom::Server::Backend::Filesystem>.
420  
421 Files use generation-specific, hash-sharded paths below C<root/blobs>. Uploads
422 are staged below C<root/.staging>, synchronized, and atomically hard-linked into
423 place without overwriting an existing file.
424  
425 =head1 CONSTRUCTOR
426  
427 =head2 new
428  
429 my $blobs = Net::Blossom::Server::Backend::Filesystem::BlobStore->new(
430 root => '/srv/blossom',
431 );
432  
433 Requires C<root>. C<generation> may provide a callback for generation filename
434 suffixes; it defaults to random 128-bit hexadecimal values. A suffix must begin
435 with an ASCII letter or digit and contain only ASCII letters, digits, periods,
436 underscores, or hyphens. Reusing a suffix for the same hash makes C<prepare>
437 fail instead of overwriting the existing file.
438  
439 The platform and filesystem must support hard links and directory
440 synchronization. C<root/blobs> and C<root/.staging> must be on the same
441 filesystem.
442  
443 =head1 METHODS
444  
445 =head2 BUILDARGS
446  
447 Internal C<Class::Tiny> constructor hook.
448  
449 =head2 root
450  
451 Returns the absolute filesystem root.
452  
453 =head2 generation
454  
455 Returns the callback used to create generation filename suffixes.
456  
457 =head2 deploy_schema
458  
459 Creates the root, blob, and staging directories. Repeated calls are safe.
460  
461 =head2 begin_upload
462  
463 Returns a file-backed upload writer.
464  
465 =head2 get_blob
466  
467 Returns C<''> for an empty file, a stream for a nonempty file, or C<undef> when
468 the file is absent. When C<size> is supplied, a size mismatch is an error.
469  
470 =head2 get_blob_range
471  
472 Returns a bounded stream for the requested zero-based C<offset> and positive
473 C<length>, or C<undef> when the file is absent. The stream starts at C<offset>
474 and ends after C<length> bytes. When C<size> is supplied, a size mismatch is an
475 error.
476  
477 =head2 delete_blob
478  
479 Deletes one file and reports whether it existed.
480  
481 =cut
« Filesystem.pm ↑ Summary