lib/File/FStore/File.pm

filter: Statementstmt 67.6% 249 / 368 Branchbran 38.2% 75 / 196 Conditioncond 31.0% 46 / 148 MC/DCmcdc 2.1% 2 / 93 Subroutinesub 74.4% 32 / 43 Podpod 100% 13 / 13 totaltotal 48.4% 417 / 861 CC 161 SCAR 55.0
CC
161
Cov
52%
__ANON__
618.4
__ANON__
506.0
_known_provider
20.0
CRAP 245.7
« blib/lib/File/FStore/File.pm ↑ Summary blib/lib/File/FStore.pm »
1 # Copyright (c) 2025 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: Module for interacting with file stores
6  
7  
8 package File::FStore::File;
9  
10 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Fcntl qw(SEEK_SET S_IWUSR S_IWGRP S_IWOTH);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Data::Identifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Data::Identifier::Generate;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use File::FStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use parent qw(File::FStore::Base Data::Identifier::Interface::Known);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 use constant {
26 3 WRITE_BITS => S_IWUSR|S_IWGRP|S_IWOTH,
27 RE_ISE => qr<^(?:[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|[1-3](?:\.(?:0|[1-9][0-9]*))+|[a-zA-Z][a-zA-Z0-9\+\.\-]+:.*)$>,
28 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 our $VERSION = v0.06;
31  
32 3 my @_xattr_hashes = qw(sha-1-160 sha-2-256 sha-3-512);
33  
34 3 my %_valid_properties = (
35 size => qr<^(?:0|[1-9][0-9]*)$>,
36 inode => qr<>, # any
37 mediasubtype => qr<^(?:application|audio|example|font|haptics|image|message|model|multipart|text|video)/[0-9a-zA-Z][0-9a-zA-Z\!\#\$\&\-\^_\.\+]{0,126}$>,
38 contentise => RE_ISE,
39 inodeise => RE_ISE,
40 );
41  
42 3 my %_exts = (
43 'image/jpeg' => 'jpg',
44 'image/png' => 'png',
45 'image/gif' => 'gif',
46 'image/vnd.microsoft.icon' => 'ico',
47 'audio/flac' => 'flac',
48 'application/pdf' => 'pdf',
49 'application/zip' => 'zip',
50 'text/plain' => 'txt',
51 );
52  
53 3 my %_db_tags = (
54 # well known tags:
55 final_file_size => Data::Identifier->new(uuid => '1cd4a6c6-0d7c-48d1-81e7-4e8d41fdb45d'),
56 final_file_encoding => Data::Identifier->new(uuid => '448c50a8-c847-4bc7-856e-0db5fea8f23b'),
57 final_file_hash => Data::Identifier->new(uuid => '79385945-0963-44aa-880a-bca4a42e9002'),
58 also_has_role => Data::Identifier->new(uuid => 'd2750351-aed7-4ade-aa80-c32436cc6030'),
59 also_has_state => Data::Identifier->new(uuid => '4c426c3c-900e-4350-8443-e2149869fbc9'),
60 has_final_state => Data::Identifier->new(uuid => '54d30193-2000-4d8a-8c28-3fa5af4cad6b'),
61 specific_proto_file_state => Data::Identifier->new(uuid => '63da70a8-78a4-51b0-8b87-86872b474a5d'),
62 );
63  
64  
65 sub dbname {
66 6 ▶ my ($self) = @_;
Subroutine dbname: called CC=3 SCAR=11.3
Pod documented
67 6 ▶ return $self->{dbname} //= do {
Condition66%2/3
$self->{"dbname"} //= do {
l!l&&r!l&&!r
420
MC/DC0%0/2
$self->{"dbname"} //= do { my $sth = $self->_prepare("SELECT filename FROM file WHERE id = ?"); my $res; $sth->execute($self->{"dbid"}); $res = $sth->fetchall_arrayref; $res->[0][0] // croak("Database error") }
$self->{"dbname"} do { my $sth = $self->_prepare("SELECT filename FROM file WHERE id = ?"); my $res; $sth->execute($self->{"dbid"}); $res = $sth->fetchall_arrayref; $res->[0][0] // croak("Database error") }
Truth table66%2/3
A //= B
A= $self->{"dbname"}B= do { my $sth = $self->_prepare("SELECT filename FROM file WHERE id = ?"); my $res; $sth->execute($self->{"dbid"}); $res = $sth->fetchall_arrayref; $res->[0][0] // croak("Database error") }
ABresult
1X1
011
000
68 2 my $sth = $self->_prepare('SELECT filename FROM file WHERE id = ?');
69 2 my $res;
70  
71 2 $sth->execute($self->{dbid});
72 2 $res = $sth->fetchall_arrayref;
73  
74 2 ▶ $res->[0][0] // croak 'Database error';
Condition33%1/3
$res->[0][0] // croak("Database error")
l!l&&r!l&&!r
200
MC/DC0%0/2
$res->[0][0] // croak("Database error")
$res->[0][0] croak("Database error")
Truth table33%1/3
A // B
A= $res->[0][0]B= croak("Database error")
ABresult
1X1
011
000
75 };
76 }
77  
78  
79 sub filename {
80 3 ▶ my ($self) = @_;
Subroutine filename: called CC=2 SCAR=7.0
Pod documented
81 3 ▶ return $self->{filename} //= do {
Condition66%2/3
$self->{"filename"} //= do {
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"filename"} //= do { $self->store->_file(("v2", "store"), $self->dbname) }
$self->{"filename"} do { $self->store->_file(("v2", "store"), $self->dbname) }
Truth table66%2/3
A //= B
A= $self->{"filename"}B= do { $self->store->_file(("v2", "store"), $self->dbname) }
ABresult
1X1
011
000
82 2 $self->store->_file(qw(v2 store), $self->dbname);
83 };
84 }
85  
86  
87 sub open {
88 1 ▶ my ($self) = @_;
Subroutine open: called CC=1 SCAR=0.0
Pod documented
89 1 my $fh = $self->_open;
90  
91 1 $self->stat;
92 1 $self->_detach_fh;
93  
94 1 return $fh;
95 }
96  
97  
98 sub link_out {
99 0 ▶ my ($self, $filename) = @_;
Subroutine link_out: not called CC=2 SCAR=17.9
Pod documented
100 0 ▶ link($self->filename, $filename) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
link $self->filename, $filename or croak($!) 0 0 0
101 }
102  
103  
104 sub symlink_out {
105 0 ▶ my ($self, $filename) = @_;
Subroutine symlink_out: not called CC=2 SCAR=17.9
Pod documented
106 0 ▶ symlink($self->filename, $filename) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
symlink $self->filename, $filename or croak($!) 0 0 0
107 }
108  
109  
110 sub update {
111 2 ▶ my ($self, %opts) = @_;
Subroutine update: called CC=2 SCAR=9.2
Pod documented
112 2 my File::FStore $store = $self->store;
113 2 my $no_digests = delete($opts{no_digests});
114 2 my $on_pre_set = delete($opts{on_pre_set});
115 2 my $on_post_set = delete($opts{on_post_set});
116 2 my $inode;
117 my $properties;
118 2 my $digests;
119  
120 2 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 2 2
121  
122 2 $store->_init_link_style;
123  
124 $store->in_transaction(rw => sub {
125 2 ▶ my $fh = $self->_open;
Subroutine __ANON__: called CC=61 SCAR=64.3
126  
127 2 delete $self->{stat}; #clear stat cache.
128  
129 2 $inode = $self->as('File::Information::Inode');
130  
131 # Perform a verify via File::Information
132 2 ▶ unless ($no_digests) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_digests) 1 1 2
133 1 my $verify_result = $inode->verify;
134 1 ▶ unless ($verify_result->has_passed || $verify_result->has_no_data || $verify_result->has_insufficient_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($verify_result->has_passed or $verify_result->has_no_data or $verify_result->has_insufficient_data) 0 1 1
Condition33%2/6
$verify_result->has_passed or $verify_result->has_no_data or $verify_result->has_insufficient_data
l!l&&r!l&&!r
100
$verify_result->has_passed or $verify_result->has_no_data
l!l&&r!l&&!r
010
MC/DC0%0/3
$verify_result->has_passed or $verify_result->has_no_data or $verify_result->has_insufficient_data
$verify_result->has_passed $verify_result->has_no_data $verify_result->has_insufficient_data
Truth table25%1/4
A or B or C
A= $verify_result->has_passedB= $verify_result->has_no_dataC= $verify_result->has_insufficient_data
ABCresult
1XX1
01X1
0011
0000
135 0 croak sprintf('File (%s) is in bad state: %s', $self->dbname, $verify_result->status);
136 }
137 }
138  
139 # Perform a verify with our own data.
140 {
141 2 my $data = $self->get;
142 2 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
143 $atime,$mtime,$ctime,$blksize,$blocks) = $self->stat;
144  
145 2 ▶ $properties = $data->{properties} //= {};
Condition50%1/2
$data->{"properties"} //= {}
l!l
20
MC/DC0%0/1
$data->{"properties"} //= {}
$data->{"properties"}
Truth table50%1/2
A
A= $data->{"properties"}
Aresult
11
00
146 2 ▶ $digests = $data->{digests} //= {};
Condition50%1/2
$data->{"digests"} //= {}
l!l
20
MC/DC0%0/1
$data->{"digests"} //= {}
$data->{"digests"}
Truth table50%1/2
A
A= $data->{"digests"}
Aresult
11
00
147  
148 2 ▶ $properties->{size} //= $size;
Condition33%1/3
$properties->{"size"} //= $size
l!l&&r!l&&!r
200
MC/DC0%0/2
$properties->{"size"} //= $size
$properties->{"size"} $size
Truth table33%1/3
A //= B
A= $properties->{"size"}B= $size
ABresult
1X1
011
000
149 2 ▶ croak 'Size missmatch' if $properties->{size} != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"size"} != $size 0 2 2
150 2 ▶ $properties->{inode} //= $ino;
Condition33%1/3
$properties->{"inode"} //= $ino
l!l&&r!l&&!r
200
MC/DC0%0/2
$properties->{"inode"} //= $ino
$properties->{"inode"} $ino
Truth table33%1/3
A //= B
A= $properties->{"inode"}B= $ino
ABresult
1X1
011
000
151 2 ▶ croak 'inode missmatch' if $properties->{inode} != $ino;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"inode"} != $ino 0 2 2
152  
153 # Load some basic properties, first the final values, than the current ones.
154 2 foreach my $lifecycle (qw(final current)) {
155 4 ▶ if (defined(my $v = $inode->get('size', lifecycle => $lifecycle, default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->get("size", "lifecycle", $lifecycle, "default", undef))) 2 2 4
156 2 ▶ $properties->{size} //= $v;
Condition33%1/3
$properties->{"size"} //= $v
l!l&&r!l&&!r
200
MC/DC0%0/2
$properties->{"size"} //= $v
$properties->{"size"} $v
Truth table33%1/3
A //= B
A= $properties->{"size"}B= $v
ABresult
1X1
011
000
157 2 ▶ croak 'Size missmatch' if $properties->{size} != $v;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"size"} != $v 0 2 2
158 }
159  
160 4 ▶ if (defined(my $v = $inode->get('mediatype', lifecycle => $lifecycle, default => undef, as => 'mediatype'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->get("mediatype", "lifecycle", $lifecycle, "default", undef, "as", "mediatype"))) 0 4 4
161 0 ▶ $properties->{mediasubtype} //= $v;
Condition0%0/3
$properties->{"mediasubtype"} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{"mediasubtype"} //= $v
$properties->{"mediasubtype"} $v
Truth table0%0/3
A //= B
A= $properties->{"mediasubtype"}B= $v
ABresult
1X1
011
000
162 0 ▶ croak sprintf('Media subtype missmatch on (%s): "%s" vs. "%s"', $self->dbname, $properties->{mediasubtype}, $v) if $properties->{mediasubtype} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"mediasubtype"} ne $v 0 0 0
163 }
164  
165 4 ▶ if (defined(my $v = $inode->get('inodeise', lifecycle => $lifecycle, default => undef, as => 'mediatype'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->get("inodeise", "lifecycle", $lifecycle, "default", undef, "as", "mediatype"))) 0 4 4
166 0 ▶ $properties->{inodeise} //= $v;
Condition0%0/3
$properties->{"inodeise"} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$properties->{"inodeise"} //= $v
$properties->{"inodeise"} $v
Truth table0%0/3
A //= B
A= $properties->{"inodeise"}B= $v
ABresult
1X1
011
000
167 # XXX: We ignore missmatches here. This can be for a number of reasons. Such as switches between different sources of the value.
168 # TODO: A better policy should be implemented later on.
169 }
170  
171 4 ▶ if (defined(my $v = $inode->get('st_ino', lifecycle => $lifecycle, default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->get("st_ino", "lifecycle", $lifecycle, "default", undef))) 2 2 4
172 2 ▶ $properties->{inode} //= $v;
Condition33%1/3
$properties->{"inode"} //= $v
l!l&&r!l&&!r
200
MC/DC0%0/2
$properties->{"inode"} //= $v
$properties->{"inode"} $v
Truth table33%1/3
A //= B
A= $properties->{"inode"}B= $v
ABresult
1X1
011
000
173 2 ▶ croak 'inode missmatch' if $properties->{inode} != $v;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"inode"} != $v 0 2 2
174 }
175 }
176  
177 # First load all known final ones.
178 2 foreach my $digest (@{$self->_used_digests}) {
179 20 ▶ if (defined(my $v = $inode->digest($digest, lifecycle => 'final', default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->digest($digest, "lifecycle", "final", "default", undef))) 0 20 20
180 0 ▶ $digests->{$digest} //= $v;
Condition0%0/3
$digests->{$digest} //= $v
l!l&&r!l&&!r
000
MC/DC0%0/2
$digests->{$digest} //= $v
$digests->{$digest} $v
Truth table0%0/3
A //= B
A= $digests->{$digest}B= $v
ABresult
1X1
011
000
181 0 ▶ croak 'Digest missmatch for '.$digest if $digests->{$digest} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $digests->{$digest} ne $v 0 0 0
182 }
183 }
184  
185 # Then test against the current ones.
186 2 ▶ unless ($no_digests) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_digests) 1 1 2
187 1 foreach my $digest (@{$self->_used_digests}) {
188 10 ▶ if (defined(my $v = $inode->digest($digest, default => undef))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $inode->digest($digest, "default", undef))) 10 0 10
189 10 ▶ $digests->{$digest} //= $v;
Condition33%1/3
$digests->{$digest} //= $v
l!l&&r!l&&!r
1000
MC/DC0%0/2
$digests->{$digest} //= $v
$digests->{$digest} $v
Truth table33%1/3
A //= B
A= $digests->{$digest}B= $v
ABresult
1X1
011
000
190 10 ▶ croak 'Digest missmatch for '.$digest if $digests->{$digest} ne $v;
Branch: 1 branch
BranchTrueFalseTotal
if $digests->{$digest} ne $v 0 10 10
191 }
192 }
193 }
194  
195 2 ▶ if (defined(my $contentise = eval {$self->_calculate_contentise($data)->uuid})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $contentise = eval { do { $self->_calculate_contentise($data)->uuid } })) 2 0 2
196 2 ▶ $properties->{contentise} //= $contentise;
Condition33%1/3
$properties->{"contentise"} //= $contentise
l!l&&r!l&&!r
200
MC/DC0%0/2
$properties->{"contentise"} //= $contentise
$properties->{"contentise"} $contentise
Truth table33%1/3
A //= B
A= $properties->{"contentise"}B= $contentise
ABresult
1X1
011
000
197 2 ▶ croak 'Content ISE missmatch' if $properties->{contentise} ne $contentise;
Branch: 1 branch
BranchTrueFalseTotal
if $properties->{"contentise"} ne $contentise 0 2 2
198 }
199  
200 2 ▶ $on_pre_set->($self) if defined $on_pre_set;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_pre_set 0 2 2
201 2 $self->set($data);
202 }
203  
204 # Create symlinks:
205 2 ▶ if ((my $link_style = $store->setting('link_style')) ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $link_style = $store->setting("link_style")) ne "none") 2 0 2
206 2 state $up = File::Spec->updir;
207 2 my $dbname = $self->dbname;
208 2 ▶ my $level = $link_style =~ /^([0-9]+)-level$/ ? int($1): 1;
Branch: 1 branch
BranchTrueFalseTotal
$link_style =~ /^([0-9]+)-level$/ ? : 2 0 2
209 2 my $filename = File::Spec->catfile(( map {$up} 0 .. $level), 'store', $dbname);
210  
211 2 foreach my $fn ($self->_linknames($digests, $link_style)) {
212 20 ▶ next if -l $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -l $fn 10 10 20
213 10 ▶ symlink($filename, $fn) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
symlink $filename, $fn or croak($!) 0 10 10
214 }
215 }
216  
217 2 ▶ if (defined(my $handle = File::FStore::File::_DUMMY_FOR_XATTR->new($fh))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $handle = "File::FStore::File::_DUMMY_FOR_XATTR"->new($fh))) 0 2 2
218 0 ▶ if (defined($properties->{size}) && !defined($handle->getfattr('utag.final.file.size'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties->{"size"} and not defined $handle->getfattr("utag.final.file.size")) 0 0 0
Condition0%0/3
defined $properties->{"size"} and not defined $handle->getfattr("utag.final.file.size")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $properties->{"size"} and not defined $handle->getfattr("utag.final.file.size")
defined $properties->{"size"} not defined $handle->getfattr("utag.final.file.size")
Truth table0%0/3
A and not(B)
A= defined $properties->{"size"}B= not defined $handle->getfattr("utag.final.file.size")
ABresult
0X0
100
111
219 0 $handle->setfattr('utag.final.file.size' => $properties->{size});
220 }
221  
222 0 ▶ if (defined($properties->{mediasubtype}) && !defined($handle->getfattr('mime_type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties->{"mediasubtype"} and not defined $handle->getfattr("mime_type")) 0 0 0
Condition0%0/3
defined $properties->{"mediasubtype"} and not defined $handle->getfattr("mime_type")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $properties->{"mediasubtype"} and not defined $handle->getfattr("mime_type")
defined $properties->{"mediasubtype"} not defined $handle->getfattr("mime_type")
Truth table0%0/3
A and not(B)
A= defined $properties->{"mediasubtype"}B= not defined $handle->getfattr("mime_type")
ABresult
0X0
100
111
223 0 $handle->setfattr('mime_type' => $properties->{mediasubtype});
224 }
225  
226 0 ▶ if (defined($properties->{mediasubtype}) && !defined($handle->getfattr('utag.final.file.encoding'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties->{"mediasubtype"} and not defined $handle->getfattr("utag.final.file.encoding")) 0 0 0
Condition0%0/3
defined $properties->{"mediasubtype"} and not defined $handle->getfattr("utag.final.file.encoding")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $properties->{"mediasubtype"} and not defined $handle->getfattr("utag.final.file.encoding")
defined $properties->{"mediasubtype"} not defined $handle->getfattr("utag.final.file.encoding")
Truth table0%0/3
A and not(B)
A= defined $properties->{"mediasubtype"}B= not defined $handle->getfattr("utag.final.file.encoding")
ABresult
0X0
100
111
227 my $v = Data::Identifier::Generate->generic(
228 namespace => '50d7c533-2d9b-4208-b560-bcbbf75ce3f9',
229 input => $properties->{mediasubtype},
230 0 )->uuid;
231 0 $handle->setfattr('utag.final.file.encoding' => $v);
232 }
233  
234 0 ▶ if (!defined($handle->getfattr('utag.write-mode'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handle->getfattr("utag.write-mode")) 0 0 0
235 0 $handle->setfattr('utag.write-mode' => '7b177183-083c-4387-abd3-8793eb647373');
236 }
237  
238 0 ▶ if (!defined($handle->getfattr('utag.final-mode'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handle->getfattr("utag.final-mode")) 0 0 0
239 0 $handle->setfattr('utag.final-mode' => 'f418cdb9-64a7-4f15-9a18-63f7755c5b47');
240 }
241  
242 0 ▶ if (defined(my $size = $properties->{size}) && !defined($handle->getfattr('utag.final.file.hash'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $size = $properties->{"size"}) and not defined $handle->getfattr("utag.final.file.hash")) 0 0 0
Condition0%0/3
defined(my $size = $properties->{"size"}) and not defined $handle->getfattr("utag.final.file.hash")
!ll&&!rl&&r
000
MC/DC0%0/2
defined(my $size = $properties->{"size"}) and not defined $handle->getfattr("utag.final.file.hash")
defined(my $size = $properties->{"size"}) not defined $handle->getfattr("utag.final.file.hash")
Truth table0%0/3
A and not(B)
A= defined(my $size = $properties->{"size"})B= not defined $handle->getfattr("utag.final.file.hash")
ABresult
0X0
100
111
243 0 my @el;
244 0 my $v = '';
245  
246 0 foreach my $algo (@_xattr_hashes) {
247 0 ▶ push(@el, sprintf(' %s bytes 0-%u/%u %s', $algo, $size - 1, $size, $digests->{$algo} // next));
Condition0%0/2
$digests->{$algo} // (next)
l!l
00
MC/DC0%0/1
$digests->{$algo} // (next)
$digests->{$algo}
Truth table0%0/2
A
A= $digests->{$algo}
Aresult
11
00
248 }
249  
250 0 ▶ for (my $i = 0; $i < scalar(@el); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @el) 0 0 0
251 0 ▶ $v .= ' ' if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
252 0 ▶ $v .= $i == $#el ? 'v0' : 'v0m';
Branch: 1 branch
BranchTrueFalseTotal
$i == $#el ? : 0 0 0
253 0 $v .= $el[$i];
254 }
255  
256 0 ▶ $handle->setfattr('utag.final.file.hash' => $v) if length $v;
Branch: 1 branch
BranchTrueFalseTotal
if length $v 0 0 0
257 }
258 }
259  
260 # Try to alter the file mode to make the file read only.
261 {
262 2 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
263 $atime,$mtime,$ctime,$blksize,$blocks) = $self->stat;
264  
265 2 ▶ if (defined($mode) && ($mode & WRITE_BITS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode and $mode & 146) 1 1 2
Condition66%2/3
defined $mode and $mode & 146
!ll&&!rl&&r
011
MC/DC50%1/2
defined $mode and $mode & 146
defined $mode $mode & 146
Truth table66%2/3
A and B
A= defined $modeB= $mode & 146
ABresult
0X0
100
111
266 1 my $n = $mode & (07777 ^ WRITE_BITS);
267 1 eval { chmod($n, $fh) }; # we don't care if it fails.
268 }
269 }
270  
271 2 ▶ $on_post_set->($self) if defined $on_post_set;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_post_set 0 2 2
272 2 });
273 }
274  
275  
276 sub stat {
277 6 ▶ my ($self) = @_;
Subroutine stat: called CC=4 SCAR=14.4
Pod documented
278  
279 6 ▶ $self->{stat} //= do {
Condition66%2/3
$self->{"stat"} //= do {
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{"stat"} //= do { my @s; if (defined $self->{"fh"}) { @s = CORE::stat $self->{"fh"}; } else { @s = CORE::stat $self->filename; } ; croak('File missing on filesystem, store is corruped: ' . $self->dbname) unless scalar @s; \@s }
$self->{"stat"} do { my @s; if (defined $self->{"fh"}) { @s = CORE::stat $self->{"fh"}; } else { @s = CORE::stat $self->filename; } ; croak('File missing on filesystem, store is corruped: ' . $self->dbname) unless scalar @s; \@s }
Truth table66%2/3
A //= B
A= $self->{"stat"}B= do { my @s; if (defined $self->{"fh"}) { @s = CORE::stat $self->{"fh"}; } else { @s = CORE::stat $self->filename; } ; croak('File missing on filesystem, store is corruped: ' . $self->dbname) unless scalar @s; \@s }
ABresult
1X1
011
000
280 4 my @s;
281  
282 4 ▶ if (defined $self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'fh'}) { } 4 0 4
283 4 @s = stat($self->{fh});
284 } else {
285 0 @s = stat($self->filename);
286 }
287  
288 4 ▶ croak 'File missing on filesystem, store is corruped: '.$self->dbname unless scalar(@s);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @s 0 4 4
289  
290 4 \@s;
291 };
292  
293 6 return @{$self->{stat}};
294 }
295  
296  
297 sub get {
298 16 ▶ my ($self, $domain, $key) = @_;
Subroutine get: called CC=11 SCAR=24.1
Pod documented
299 16 ▶ if (defined($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 14 2 16
300 14 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 10 4 14
301 10 my $sth;
302 my $res;
303  
304 10 ▶ if ($domain eq 'properties') {
Branch: 2 branches
BranchTrueFalseTotal
if ($domain eq 'properties') { } 6 4 10
elsif ($domain eq 'digests') { } 4 0 4
305 6 $sth = $self->_prepare('SELECT value FROM file_properties WHERE file = ? AND key = ?');
306 } elsif ($domain eq 'digests') {
307 4 $sth = $self->_prepare('SELECT hash FROM file_hash WHERE file = ? AND algo = ?');
308 }
309  
310 10 ▶ croak 'Invalid domain: '.$domain unless defined $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sth 0 10 10
311  
312 10 $sth->execute($self->{dbid}, $key);
313 10 $res = $sth->fetchall_arrayref;
314  
315 10 ▶ return $res->[0][0] // croak 'No value for domain '.$domain.' key '.$key;
Condition100%3/3
$res->[0][0] // croak('No value for domain ' . $domain . ' key ' . $key)
l!l&&r!l&&!r
811
MC/DC0%0/2
$res->[0][0] // croak('No value for domain ' . $domain . ' key ' . $key)
$res->[0][0] croak('No value for domain ' . $domain . ' key ' . $key)
Truth table33%1/3
A // B
A= $res->[0][0]B= croak('No value for domain ' . $domain . ' key ' . $key)
ABresult
1X1
011
000
316 } else {
317 4 my $sth;
318 my %res;
319  
320 4 ▶ if ($domain eq 'properties') {
Branch: 2 branches
BranchTrueFalseTotal
if ($domain eq 'properties') { } 2 2 4
elsif ($domain eq 'digests') { } 2 0 2
321 2 $sth = $self->_prepare('SELECT key,value FROM file_properties WHERE file = ?');
322 } elsif ($domain eq 'digests') {
323 2 $sth = $self->_prepare('SELECT algo,hash FROM file_hash WHERE file = ?');
324 }
325  
326 4 ▶ croak 'Invalid domain: '.$domain unless defined $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sth 0 4 4
327  
328 4 $sth->execute($self->{dbid});
329  
330 4 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 26 4 30
331 26 $res{$row->[0]} = $row->[1];
332 }
333  
334 4 return \%res;
335 }
336 } else {
337 2 return {map {$_ => $self->get($_)} qw(properties digests)};
338 }
339 }
340  
341  
342 sub set {
343 4 ▶ my ($self, $domain, $key, $value) = @_;
Subroutine set: called CC=4 SCAR=14.6
Pod documented
344 4 my $dbid = $self->{dbid};
345 4 my $data;
346  
347 4 ▶ croak 'No data given' unless defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 4 4
348  
349 # get things in standard format:
350 4 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 0 4 4
351 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
352 0 $data = {$domain => {$key => $value}};
353 } else {
354 0 $data = {$domain => $key};
355 }
356 } else {
357 4 $data = $domain;
358 }
359  
360 $self->store->in_transaction(rw => sub {
361 4 ▶ foreach my $cdomain (keys %{$data}) {
Subroutine __ANON__: called CC=8 SCAR=21.1
362 7 my $d = $data->{$cdomain};
363 7 my $sth;
364 my $valids;
365  
366 7 ▶ if ($cdomain eq 'properties') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cdomain eq 'properties') { } 4 3 7
elsif ($cdomain eq 'digests') { } 3 0 3
367 4 $sth = $self->_prepare('INSERT INTO file_properties (file,key,value) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT TRUE FROM file_properties WHERE file = ? AND key = ? AND value = ?)');
368 4 $valids = \%_valid_properties;
369 } elsif ($cdomain eq 'digests') {
370 3 $sth = $self->_prepare('INSERT INTO file_hash (file,algo,hash) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT TRUE FROM file_hash WHERE file = ? AND algo = ? AND hash = ?)');
371 3 $valids = $self->_valid_digests;
372 }
373  
374 7 ▶ croak 'Invalid domain: '.$domain unless defined $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sth 0 7 7
375  
376 7 foreach my $key (keys %{$d}) {
377 40 ▶ croak 'Invalid key '.$key.' for domain '.$cdomain unless defined $valids->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valids->{$key} 0 40 40
378 40 ▶ croak 'Invalid value for key '.$key.' for domain '.$cdomain unless $d->{$key} =~ $valids->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $d->{$key} =~ /$valids->{$key}/ 0 40 40
379 40 $sth->execute($dbid, $key, $d->{$key}, $dbid, $key, $d->{$key});
380 }
381 }
382 4 });
383 }
384  
385  
386 sub delete {
387 0 ▶ my ($self) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
388 0 my $store = $self->store;
389 0 ▶ my $dbid = $self->{dbid} or croak 'Call on invalid object';
Branch: 1 branch
BranchTrueFalseTotal
my $dbid = $self->{"dbid"} or croak("Call on invalid object") 0 0 0
390  
391 $store->in_transaction(rw => sub {
392 0 ▶ my $filename = $self->filename;
Subroutine __ANON__: not called CC=3 SCAR=24.8
393 0 my @linknames = $self->_linknames(undef, 'all');
394 0 my $sth;
395  
396 0 ▶ unlink($filename) or croak 'Cannot unlink file: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename or croak('Cannot unlink file: ' . $!) 0 0 0
397 0 unlink($_) foreach @linknames;
398  
399 0 $sth = $self->_prepare('DELETE FROM file_hash WHERE file = ?');
400 0 $sth->execute($dbid);
401 0 $sth = $self->_prepare('DELETE FROM file_properties WHERE file = ?');
402 0 $sth->execute($dbid);
403 0 $sth = $self->_prepare('DELETE FROM file WHERE id = ?');
404 0 $sth->execute($dbid);
405 0 });
406  
407 0 %{$self} = ();
408 }
409  
410  
411 sub sync_with_db {
412 0 ▶ my ($self, %opts) = @_;
Subroutine sync_with_db: not called CC=2 SCAR=17.9
Pod documented
413 0 ▶ my $db = $opts{db} // $self->db;
Condition0%0/3
$opts{"db"} // $self->db
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"db"} // $self->db
$opts{"db"} $self->db
Truth table0%0/3
A // B
A= $opts{"db"}B= $self->db
ABresult
1X1
011
000
414 0 my $fii_inode = $self->as('File::Information::Inode');
415 $db->in_transaction(rw => sub {
416 0 ▶ my $data = $self->get;
Subroutine __ANON__: not called CC=22 SCAR=62.3
417 my %ids = (
418 contentise => $self->contentise(as => 'Data::Identifier'),
419 inodeise => $fii_inode->get('inodeise', as => 'Data::Identifier', default => undef),
420 proto => (defined($opts{proto}) ? $opts{proto}->Data::Identifier::as('Data::Identifier') : undef),
421  
422 # Related values:
423  
424 encoding => (defined($data->{properties}{mediasubtype}) ? Data::Identifier::Generate->generic(
425 namespace => '50d7c533-2d9b-4208-b560-bcbbf75ce3f9',
426 input => $data->{properties}{mediasubtype},
427 0 ▶ ): undef),
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'proto'} ? : 0 0 0
defined $data->{'properties'}{'mediasubtype'} ? : 0 0 0
428  
429 %_db_tags,
430 );
431 0 my %tags = map {$_ => scalar(eval {$ids{$_}->as('Data::TagDB::Tag', %opts{autocreate}, db => $db)})} grep {defined $ids{$_}} keys %ids;
432  
433 0 ▶ if (defined(my $tag = $tags{contentise})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tag = $tags{"contentise"})) 0 0 0
434 0 my $size = $data->{properties}{size};
435  
436 0 ▶ if (defined($tags{also_has_role}) && defined($tags{specific_proto_file_state})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags{"also_has_role"} and defined $tags{"specific_proto_file_state"}) 0 0 0
Condition0%0/3
defined $tags{"also_has_role"} and defined $tags{"specific_proto_file_state"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tags{"also_has_role"} and defined $tags{"specific_proto_file_state"}
defined $tags{"also_has_role"} defined $tags{"specific_proto_file_state"}
Truth table0%0/3
A and B
A= defined $tags{"also_has_role"}B= defined $tags{"specific_proto_file_state"}
ABresult
0X0
100
111
437 0 $db->create_relation(tag => $tag, relation => $tags{also_has_role}, related => $tags{specific_proto_file_state});
438 }
439  
440 0 ▶ if (defined($size) && defined($tags{final_file_size})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and defined $tags{"final_file_size"}) 0 0 0
Condition0%0/3
defined $size and defined $tags{"final_file_size"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and defined $tags{"final_file_size"}
defined $size defined $tags{"final_file_size"}
Truth table0%0/3
A and B
A= defined $sizeB= defined $tags{"final_file_size"}
ABresult
0X0
100
111
441 0 $db->create_metadata(tag => $tag, relation => $tags{final_file_size}, data_raw => $size);
442 }
443  
444 0 ▶ if (defined($tags{encoding}) && defined($tags{final_file_encoding})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags{"encoding"} and defined $tags{"final_file_encoding"}) 0 0 0
Condition0%0/3
defined $tags{"encoding"} and defined $tags{"final_file_encoding"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tags{"encoding"} and defined $tags{"final_file_encoding"}
defined $tags{"encoding"} defined $tags{"final_file_encoding"}
Truth table0%0/3
A and B
A= defined $tags{"encoding"}B= defined $tags{"final_file_encoding"}
ABresult
0X0
100
111
445 0 $db->create_relation(tag => $tag, relation => $tags{final_file_encoding}, related => $tags{encoding});
446 }
447  
448 0 ▶ if (defined($tags{final_file_hash}) && defined($size) && $size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags{"final_file_hash"} and defined $size and $size > 0) 0 0 0
Condition0%0/6
defined $tags{"final_file_hash"} and defined $size and $size > 0
!ll&&!rl&&r
000
defined $tags{"final_file_hash"} and defined $size
!ll&&!rl&&r
000
MC/DC0%0/3
defined $tags{"final_file_hash"} and defined $size and $size > 0
defined $tags{"final_file_hash"} defined $size $size > 0
Truth table0%0/4
A and B and C
A= defined $tags{"final_file_hash"}B= defined $sizeC= $size > 0
ABCresult
0XX0
10X0
1100
1111
449 0 foreach my $digest (keys %{$data->{digests}}) {
450 0 ▶ my $v = sprintf('v0 %s bytes 0-%u/%u %s', $digest, $size - 1, $size, $data->{digests}{$digest} // next);
Condition0%0/2
$data->{"digests"}{$digest} // (next)
l!l
00
MC/DC0%0/1
$data->{"digests"}{$digest} // (next)
$data->{"digests"}{$digest}
Truth table0%0/2
A
A= $data->{"digests"}{$digest}
Aresult
11
00
451 0 $db->create_metadata(tag => $tag, relation => $tags{final_file_hash}, data_raw => $v);
452 }
453 }
454 }
455  
456 0 ▶ if (defined(my $proto = $tags{proto})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $proto = $tags{"proto"})) 0 0 0
457 0 ▶ if (defined(my $tag = $tags{contentise})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tag = $tags{"contentise"})) 0 0 0
458 0 ▶ if (defined(my $relation = $opts{final_of_proto} ? $tags{has_final_state} : $tags{also_has_state})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $relation = $opts{"final_of_proto"} ? $tags{"has_final_state"} : $tags{"also_has_state"})) 0 0 0
$opts{'final_of_proto'} ? : 0 0 0
459 0 $db->create_relation(tag => $proto, relation => $relation, related => $tag);
460 }
461 }
462 }
463  
464 0 ▶ if (defined(my $inode = $tags{inodeise})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $inode = $tags{"inodeise"})) 0 0 0
465 0 ▶ if (defined(my $tag = $tags{contentise})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tag = $tags{"contentise"})) 0 0 0
466 0 ▶ if (defined($tags{has_final_state})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tags{"has_final_state"}) 0 0 0
467 0 $db->create_relation(tag => $inode, relation => $tags{has_final_state}, related => $tag);
468 }
469 }
470 }
471 0 });
472 }
473  
474  
475 # --- Overrides for Data::URIID::Base ---
476 sub contentise {
477 1 ▶ my ($self, @args) = @_;
Subroutine contentise: called CC=3 SCAR=12.3
Pod documented
478  
479 1 ▶ $self->{contentise} //= eval {Data::Identifier->new(ise => $self->get(properties => 'contentise'))};
Condition33%1/3
$self->{"contentise"} //= eval {
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"contentise"} //= eval { do { "Data::Identifier"->new("ise", $self->get("properties", "contentise")) } }
$self->{"contentise"} eval { do { "Data::Identifier"->new("ise", $self->get("properties", "contentise")) } }
Truth table33%1/3
A //= B
A= $self->{"contentise"}B= eval { do { "Data::Identifier"->new("ise", $self->get("properties", "contentise")) } }
ABresult
1X1
011
000
480 1 ▶ $self->{contentise} //= $self->_calculate_contentise;
Condition33%1/3
$self->{"contentise"} //= $self->_calculate_contentise
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"contentise"} //= $self->_calculate_contentise
$self->{"contentise"} $self->_calculate_contentise
Truth table33%1/3
A //= B
A= $self->{"contentise"}B= $self->_calculate_contentise
ABresult
1X1
011
000
481  
482 1 return $self->SUPER::contentise(@args);
483 }
484  
485 sub as {
486 3 ▶ my ($self, $as, %opts) = @_;
Subroutine as: called CC=8 SCAR=28.9
Pod documented
487  
488 3 ▶ if ($as eq 'File::Information::Inode' || $as eq 'File::Information::Base') {
Branch: 2 branches
BranchTrueFalseTotal
if ($as eq 'File::Information::Inode' or $as eq 'File::Information::Base') { } 2 1 3
elsif ($as eq 'File::Information::Link') { } 0 1 1
Condition66%2/3
$as eq "File::Information::Inode" or $as eq "File::Information::Base"
l!l&&r!l&&!r
201
MC/DC50%1/2
$as eq "File::Information::Inode" or $as eq "File::Information::Base"
$as eq "File::Information::Inode" $as eq "File::Information::Base"
Truth table66%2/3
A or B
A= $as eq "File::Information::Inode"B= $as eq "File::Information::Base"
ABresult
1X1
011
000
489 2 ▶ if (defined $opts{fii}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'fii'}) { } 0 2 2
490 0 return $opts{fii}->for_handle($self->_open);
491 } else {
492 2 ▶ return $self->{fii_inode} //= $self->fii->for_handle($self->_open);
Condition33%1/3
$self->{"fii_inode"} //= $self->fii->for_handle($self->_open)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"fii_inode"} //= $self->fii->for_handle($self->_open)
$self->{"fii_inode"} $self->fii->for_handle($self->_open)
Truth table33%1/3
A //= B
A= $self->{"fii_inode"}B= $self->fii->for_handle($self->_open)
ABresult
1X1
011
000
493 }
494 } elsif ($as eq 'File::Information::Link') {
495 0 ▶ if (defined $opts{fii}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'fii'}) { } 0 0 0
496 0 return $opts{fii}->for_link($self->filename);
497 } else {
498 0 ▶ return $self->{fii_inode} //= $self->fii->for_link($self->filename);
Condition0%0/3
$self->{"fii_inode"} //= $self->fii->for_link($self->filename)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"fii_inode"} //= $self->fii->for_link($self->filename)
$self->{"fii_inode"} $self->fii->for_link($self->filename)
Truth table0%0/3
A //= B
A= $self->{"fii_inode"}B= $self->fii->for_link($self->filename)
ABresult
1X1
011
000
499 }
500 }
501  
502 1 return $self->SUPER::as($as, %opts);
503 }
504  
505  
506 # --- Overrides for Data::Identifier::Interface::Known ---
507 sub _known_provider {
508 0 ▶ my ($pkg, $class, %opts) = @_;
Subroutine _known_provider: not called CC=4 SCAR=30.0
509 0 state $classes = {
510 properties => [[keys %_valid_properties], not_identifiers => 1],
511 digests => [[keys %File::FStore::_valid_digests], not_identifiers => 1],
512 link_styles => [[keys %File::FStore::_valid_link_styles], not_identifiers => 1],
513 store_styles => [[keys %File::FStore::_valid_store_styles], not_identifiers => 1],
514 domains => [[qw(properties digests)], not_identifiers => 1],
515 other_tags => [[values %_db_tags], rawtype => 'Data::Identifier'],
516 };
517 0 ▶ croak 'Unsupported options passed' if scalar(keys %opts);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
518 0 $class =~ tr/-/_/;
519 0 ▶ return @{$classes->{$class}} if defined $classes->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $classes->{$class} 0 0 0
520 0 ▶ return ([map {@{$_->[0]}} values %{$classes}], not_identifiers => 1) if $class eq ':all';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq ":all" 0 0 0
521 0 croak 'Unsupported class';
522 }
523  
524  
525  
526 # ---- Private helpers ----
527 sub _valid_digests {
528 3 ▶ return state $digests = {map {$_ => qr/^[0-9a-f]{$File::FStore::_valid_digests{$_}}$/} keys %File::FStore::_valid_digests};
Subroutine _valid_digests: called CC=1 SCAR=0.0
529 }
530  
531 sub _new {
532 3 ▶ my ($pkg, %opts) = @_;
Subroutine _new: called CC=3 SCAR=11.3
533 3 my $self = bless \%opts, $pkg;
534  
535 3 ▶ croak 'No store is given' unless defined $self->{store};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"store"} 0 3 3
536 3 ▶ croak 'No dbid is given' unless defined $self->{dbid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dbid"} 0 3 3
537  
538 3 return $self;
539 }
540  
541 sub _prepare {
542 23 ▶ my ($self, $q) = @_;
Subroutine _prepare: called CC=2 SCAR=7.0
543 23 ▶ my $dbh = $self->{dbh} //= $self->store->{dbh};
Condition66%2/3
$self->{"dbh"} //= $self->store->{"dbh"}
l!l&&r!l&&!r
2030
MC/DC0%0/2
$self->{"dbh"} //= $self->store->{"dbh"}
$self->{"dbh"} $self->store->{"dbh"}
Truth table66%2/3
A //= B
A= $self->{"dbh"}B= $self->store->{"dbh"}
ABresult
1X1
011
000
544 23 return $dbh->prepare($q);
545 }
546  
547 sub _open {
548 6 ▶ my ($self, %opts) = @_;
Subroutine _open: called CC=5 SCAR=16.4
549 6 my $fh;
550  
551 6 ▶ $self->{fh} = $opts{fh} if defined $opts{fh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"fh"} 1 5 6
552  
553 6 ▶ $fh = $self->{fh} //= do {
Condition66%2/3
$self->{"fh"} //= do {
l!l&&r!l&&!r
330
MC/DC0%0/2
$self->{"fh"} //= do { CORE::open my $in, "<", $self->filename or croak($!); $in }
$self->{"fh"} do { CORE::open my $in, "<", $self->filename or croak($!); $in }
Truth table66%2/3
A //= B
A= $self->{"fh"}B= do { CORE::open my $in, "<", $self->filename or croak($!); $in }
ABresult
1X1
011
000
554 3 ▶ CORE::open(my $in, '<', $self->filename) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $in, "<", $self->filename or croak($!) 0 3 3
555 3 $in;
556 };
557  
558 6 ▶ seek($fh, 0, SEEK_SET) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or croak($!) 0 6 6
559  
560 6 return $fh;
561 }
562  
563 sub _detach_fh {
564 1 ▶ my ($self) = @_;
Subroutine _detach_fh: called CC=1 SCAR=0.0
565 1 $self->{fh} = undef;
566 }
567  
568 sub _used_digests {
569 3 ▶ my ($self) = @_;
Subroutine _used_digests: called CC=2 SCAR=7.1
570 3 ▶ return $self->{used_digests} //= $self->store->_used_digests;
Condition66%2/3
$self->{"used_digests"} //= $self->store->_used_digests
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"used_digests"} //= $self->store->_used_digests
$self->{"used_digests"} $self->store->_used_digests
Truth table66%2/3
A //= B
A= $self->{"used_digests"}B= $self->store->_used_digests
ABresult
1X1
011
000
571 }
572  
573 sub _calculate_contentise {
574 2 ▶ my ($self, $data) = @_;
Subroutine _calculate_contentise: called CC=10 SCAR=27.3
575 2 my $sha_1_160 = eval { $self->get(digests => 'sha-1-160') };
576 2 my $sha_3_512 = eval { $self->get(digests => 'sha-3-512') };
577 2 my $size = eval { $self->get(properties => 'size') };
578  
579 2 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 2 0 2
580 2 ▶ $data->{digests} //= {};
Condition50%1/2
$data->{"digests"} //= {}
l!l
20
MC/DC0%0/1
$data->{"digests"} //= {}
$data->{"digests"}
Truth table50%1/2
A
A= $data->{"digests"}
Aresult
11
00
581 2 ▶ $data->{properties} //= {};
Condition50%1/2
$data->{"properties"} //= {}
l!l
20
MC/DC0%0/1
$data->{"properties"} //= {}
$data->{"properties"}
Truth table50%1/2
A
A= $data->{"properties"}
Aresult
11
00
582 2 ▶ $sha_1_160 //= $data->{digests}{'sha-1-160'};
Condition33%1/3
$sha_1_160 //= $data->{"digests"}{"sha-1-160"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$sha_1_160 //= $data->{"digests"}{"sha-1-160"}
$sha_1_160 $data->{"digests"}{"sha-1-160"}
Truth table33%1/3
A //= B
A= $sha_1_160B= $data->{"digests"}{"sha-1-160"}
ABresult
1X1
011
000
583 2 ▶ $sha_3_512 //= $data->{digests}{'sha-3-512'};
Condition33%1/3
$sha_3_512 //= $data->{"digests"}{"sha-3-512"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$sha_3_512 //= $data->{"digests"}{"sha-3-512"}
$sha_3_512 $data->{"digests"}{"sha-3-512"}
Truth table33%1/3
A //= B
A= $sha_3_512B= $data->{"digests"}{"sha-3-512"}
ABresult
1X1
011
000
584 2 ▶ $size //= $data->{properties}{size};
Condition33%1/3
$size //= $data->{"properties"}{"size"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$size //= $data->{"properties"}{"size"}
$size $data->{"properties"}{"size"}
Truth table33%1/3
A //= B
A= $sizeB= $data->{"properties"}{"size"}
ABresult
1X1
011
000
585 }
586  
587 2 ▶ if (defined($sha_1_160) && defined($sha_3_512) && defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sha_1_160 and defined $sha_3_512 and defined $size) 2 0 2
Condition33%2/6
defined $sha_1_160 and defined $sha_3_512 and defined $size
!ll&&!rl&&r
002
defined $sha_1_160 and defined $sha_3_512
!ll&&!rl&&r
002
MC/DC0%0/3
defined $sha_1_160 and defined $sha_3_512 and defined $size
defined $sha_1_160 defined $sha_3_512 defined $size
Truth table25%1/4
A and B and C
A= defined $sha_1_160B= defined $sha_3_512C= defined $size
ABCresult
0XX0
10X0
1100
1111
588 2 my $digest = sprintf('v0m sha-1-160 bytes 0-%u/%u %s v0 sha-3-512 bytes 0-%u/%u %s',
589 $size - 1, $size, $sha_1_160,
590 $size - 1, $size, $sha_3_512,
591 );
592  
593 2 return Data::Identifier::Generate->generic(
594 namespace => '66d488c0-3b19-4e6c-856f-79edf2484f37',
595 input => $digest,
596 );
597 }
598 0 return undef;
599 }
600  
601 sub _ext {
602 2 ▶ my ($self) = @_;
Subroutine _ext: called CC=6 SCAR=21.5
603  
604 2 ▶ return $self->{ext} if exists $self->{ext};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ext"} 0 2 2
605  
606 {
607 2 ▶ my $mediasubtype = eval {$self->get(properties => 'mediasubtype')} // 'x.x/x.x';
Condition50%1/2
eval { // "x.x/x.x"
l!l
02
MC/DC0%0/1
eval { do { $self->get("properties", "mediasubtype") } } // "x.x/x.x"
eval { do { $self->get("properties", "mediasubtype") } }
Truth table50%1/2
A
A= eval { do { $self->get("properties", "mediasubtype") } }
Aresult
11
00
608 2 ▶ return $self->{ext} = $_exts{$mediasubtype} if defined $_exts{$mediasubtype};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_exts{$mediasubtype} 0 2 2
609 }
610  
611 {
612 2 my $dbname = $self->dbname;
613 2 ▶ if ($dbname =~ /\.([a-z0-9]{1,4})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbname =~ /\.([a-z0-9]{1,4})$/) 0 2 2
614 0 my $ext = $1;
615  
616 0 ▶ if ($dbname =~ /\.(tar\.(?:gz|bz2|xz|lz|zst))$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbname =~ /\.(tar\.(?:gz|bz2|xz|lz|zst))$/) 0 0 0
617 0 $ext = $1;
618 }
619  
620 0 return $self->{ext} = $ext;
621 }
622 }
623  
624 2 return $self->{ext} = undef;
625 }
626  
627 sub _linknames {
628 2 ▶ my ($self, $digests, $link_style) = @_;
Subroutine _linknames: called CC=11 SCAR=28.4
629 2 my File::FStore $store = $self->store;
630 2 my $ext = $self->_ext;
631 2 my @res;
632  
633 2 ▶ $ext = '.'.$ext if defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 0 2 2
634  
635 2 ▶ $digests //= $self->get('digests');
Condition33%1/3
$digests //= $self->get("digests")
l!l&&r!l&&!r
200
MC/DC0%0/2
$digests //= $self->get("digests")
$digests $self->get("digests")
Truth table33%1/3
A //= B
A= $digestsB= $self->get("digests")
ABresult
1X1
011
000
636 2 ▶ $link_style //= $store->setting('link_style');
Condition33%1/3
$link_style //= $store->setting("link_style")
l!l&&r!l&&!r
200
MC/DC0%0/2
$link_style //= $store->setting("link_style")
$link_style $store->setting("link_style")
Truth table33%1/3
A //= B
A= $link_styleB= $store->setting("link_style")
ABresult
1X1
011
000
637  
638 2 foreach my $digest (keys %{$digests}) {
639 20 ▶ my $v = $digests->{$digest} // next;
Condition50%1/2
$digests->{$digest} // (next)
l!l
200
MC/DC0%0/1
$digests->{$digest} // (next)
$digests->{$digest}
Truth table50%1/2
A
A= $digests->{$digest}
Aresult
11
00
640  
641 20 ▶ $v .= $ext if defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 0 20 20
642  
643 20 ▶ if ($link_style eq '1-level' || $link_style eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_style eq "1-level" or $link_style eq "all") 20 0 20
Condition33%1/3
$link_style eq "1-level" or $link_style eq "all"
l!l&&r!l&&!r
2000
MC/DC0%0/2
$link_style eq "1-level" or $link_style eq "all"
$link_style eq "1-level" $link_style eq "all"
Truth table33%1/3
A or B
A= $link_style eq "1-level"B= $link_style eq "all"
ABresult
1X1
011
000
644 20 push(@res, $store->_file(v2 => by => $digest => $v));
645 }
646 20 ▶ if ($link_style eq '2-level' || $link_style eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_style eq "2-level" or $link_style eq "all") 0 20 20
Condition33%1/3
$link_style eq "2-level" or $link_style eq "all"
l!l&&r!l&&!r
0020
MC/DC0%0/2
$link_style eq "2-level" or $link_style eq "all"
$link_style eq "2-level" $link_style eq "all"
Truth table33%1/3
A or B
A= $link_style eq "2-level"B= $link_style eq "all"
ABresult
1X1
011
000
647 0 push(@res, $store->_file(v2 => by => $digest => substr($v, 0, 2) => $v));
648 }
649 }
650  
651 2 return @res;
652 }
653  
654 # Bad workaround for File::ExtAttr
655 3 package File::FStore::File::_DUMMY_FOR_XATTR {
656  
657 sub new {
658 2 ▶ my ($pkg, $fh) = @_;
Subroutine new: called CC=2 SCAR=9.2
659 2 ▶ return undef unless eval {require File::ExtAttr; File::ExtAttr->import; 1;};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require File::ExtAttr; "File::ExtAttr"->import; 1 } } 2 0 2
660 0 return bless \$fh;
661 }
662 sub isa {
663 0 ▶ my ($self, $pkg) = @_;
Subroutine isa: not called CC=2 SCAR=17.9
664 0 ▶ return 1 if $pkg eq 'IO::Handle';
Branch: 1 branch
BranchTrueFalseTotal
if $pkg eq "IO::Handle" 0 0 0
665 0 return $self->SUPER::isa($pkg);
666 }
667 sub fileno {
668 0 ▶ my ($self) = @_;
Subroutine fileno: not called CC=1 SCAR=6.9
669 0 return ${$self}->fileno;
670 }
671 sub getfattr {
672 0 ▶ my ($self, $key) = @_;
Subroutine getfattr: not called CC=1 SCAR=6.9
673 0 return eval { $self->File::ExtAttr::getfattr($key, { namespace => 'user' }) };
674 }
675 sub setfattr {
676 0 ▶ my ($self, $key, $value) = @_;
Subroutine setfattr: not called CC=1 SCAR=6.9
677 0 return eval { $self->File::ExtAttr::setfattr($key => $value, { namespace => 'user' }) };
678 }
679 }
680  
681  
682 3 1;
683  
684 __END__
685  
686 =pod
687  
688 =encoding UTF-8
689  
690 =head1 NAME
691  
692 File::FStore::File - Module for interacting with file stores
693  
694 =head1 VERSION
695  
696 version v0.06
697  
698 =head1 SYNOPSIS
699  
700 use File::FStore;
701  
702 my File::FStore $store = File::FStore->new(path => '...');
703  
704 my File::FStore::File $file = $store->query(...);
705  
706 This package provides access to file level values.
707  
708 This package inherits from L<File::FStore::Base>, and L<Data::Identifier::Interface::Known>.
709  
710 =head1 METHODS
711  
712 =head2 dbname
713  
714 my $dbname = $file->dbname;
715  
716 This returns the name of the file relative to the store's data directory.
717  
718 B<Note:>
719 This method is generally not very useful for most applications.
720  
721 See also:
722 L</filename>,
723 L</open>.
724  
725 =head2 filename
726  
727 my $filename = $file->filename;
728  
729 This returns the filename within the store.
730 The filename is returned in a format that is suitable to be passed to operating system functions.
731  
732 B<Note:>
733 When possible L</open> should be preferred.
734  
735 See also:
736 L</open>.
737  
738 =head2 open
739  
740 my $fh = $file->open;
741  
742 $fh->binmode; # allow binary data.
743  
744 Opens the file and returns a filehandle.
745 The file is opened read-only (as all files in the store are read-only to begin with).
746  
747 B<Note:>
748 This module doesn't set the handle to binary mode similar to L<perlfunc/open>.
749 See L<perlfunc/binmode> for details.
750  
751 See also:
752 L</filename>.
753  
754 =head2 link_out
755  
756 $file->link_out($filename);
757  
758 Creates an hardlink to this file as C<$filename>.
759 C<die>s on any error.
760  
761 See also:
762 L<perlfunc/link>.
763  
764 =head2 symlink_out
765  
766 $file->symlink_out($filename);
767  
768 Creates an symlink to this file as C<$filename>.
769 C<die>s on any error.
770  
771 See also:
772 L<perlfunc/symlink>.
773  
774 =head2 update
775  
776 $file->update;
777 # or:
778 $file->update(%opts);
779  
780 Updates the file in the database. Also performs a verify of the file as part of the update.
781  
782 The following (all optional) options are supported:
783  
784 =over
785  
786 =item C<no_digests>
787  
788 This will try to skip digest calculation.
789  
790 =item C<on_pre_set>
791  
792 A callback that is called before any data is set (see L</set>) on this file.
793 C<$file> is passed as first argument.
794  
795 =item C<on_post_set>
796  
797 A callback that is called after all data is set (see L</set>) on this file.
798 C<$file> is passed as first argument.
799  
800 =back
801  
802 =head2 stat
803  
804 my @res = $file->stat;
805  
806 This method performs the same task and returns the same values as L<perlfunc/stat>.
807 In contrast however it C<die>s if the request cannot be performed. This however can only happen if the store is corruped.
808  
809 The value is cached. Therefore some attributes (mostly C<atime>) might be out of sync.
810 However those attributes are unreliable to begin with, hence the cache doesn't really make this worse.
811  
812 B<Note:>
813 If you plan to perform an L</open> on this file call that first and then this method.
814 This will improve performance and reduce the chance of race conditions.
815  
816 =head2 get
817  
818 my $value = $file->get($domain => $key);
819 # or:
820 my $hashref = $file->get($domain);
821 # or:
822 my $hashhashref = $file->get;
823  
824 Returns information about the file.
825 If a C<$domain> and C<$key> is given the value is returned as scalar.
826 If only a C<$domain> is given all values for that domain are returned as a hashref.
827 If no parameters are given a hashref with the domains as keys and the hashrefs of the per-domain values is returned.
828  
829 If a value is unknown this method C<die>s.
830  
831 The following domains are supported:
832  
833 =over
834  
835 =item C<properties>
836  
837 This domain contains flat properties of the file sich as it's size.
838  
839 See L</PROPERTIES>.
840  
841 =item C<digests>
842  
843 This domain contains digests for the file as known. Those are the I<final> digests.
844 If the file does not match the those values at this point it is corruped.
845  
846 The key is the digest name in universal tag (utag) format (e.g. C<sha-3-224>).
847  
848 =back
849  
850 =head2 set
851  
852 $file->set($domain => $key => $value);
853 # or:
854 $file->set($domain => {$key => $value});
855 # or:
856 $file->set({$domain => {$key => $value}});
857  
858 This sets a value on the file.
859 The value is checked against already known values.
860 If a value is set for a key that already holds a value this method C<die>s if the values missmatch.
861  
862 This method takes a domain-key-value triplet, or a domain and a hashref with multiple values, or
863 a single hashref with the domain(s) as keys and hashrefs with key-value pairs as values.
864  
865 See also:
866 L</get>.
867  
868 =head2 delete
869  
870 $file->delete;
871  
872 Removes the given file from the store.
873 This also unlinks all links within the store.
874  
875 If there is no hardlink outside the store to the given file it is
876 fully removed from the filesystem.
877  
878 B<Note:>
879 After this call B<no> future calls are allowed on this handle
880 or any other handle referencing the same file.
881  
882 =head2 sync_with_db
883  
884 $file->sync_with_db;
885 # or:
886 $file->sync_with_db(%opts);
887  
888 Syncs the file with the database.
889 It may read data from, and write data to the database related to this file.
890  
891 B<Note:>
892 This method calls L<Data::TagDB/in_transaction> with type C<rw>.
893  
894 It mainly operates on three tags:
895  
896 =over
897  
898 =item content
899  
900 The content tag is the tag that represents the state of this file (size, digests, ...)
901 (but not the filesystem object or other objects).
902  
903 See also L</contentise>.
904  
905 =item inode
906  
907 The inode tag represents the actual file on the filesystem.
908 This is the tag file system browsing software will use.
909  
910 The inode tag has the content tag as it's final state.
911  
912 See also L<File::Information::Base/inodeise>.
913  
914 =item proto
915  
916 The proto tag represents the work independent on the filesystem.
917 This is often used by software that implement some kind of catalogue.
918  
919 The proto tag has the content tag as a state, maybe as it's final state.
920  
921 =back
922  
923 The following (all optional) options are supported:
924  
925 =over
926  
927 =item C<autocreate>
928  
929 Whether tags should be automatically created if not yet part of the database.
930  
931 Defaults to false.
932  
933 See also L<Data::Identifier/as>.
934  
935 =item C<db>
936  
937 The database object to use.
938  
939 Defaults to the value returned by L</db>.
940  
941 =item C<final_of_proto>
942  
943 Whether this file is the final state of the proto tag (true value) or
944 just one of it's states (false value).
945  
946 Defaults to false.
947  
948 =item C<proto>
949  
950 The tag to be used as proto tag (if any).
951 This may be anything that L<Data::Identifier/as> will accept.
952  
953 Defaults to C<undef>.
954  
955 =back
956  
957 =head2 known
958  
959 my @list = File::FStore::File->known($class [, %opts ] );
960 # or:
961 my @list = $file->known($class [, %opts ] );
962  
963 (since v0.05)
964  
965 Lists known objects. See L<Data::Identifier::Interface::Known/known> for details.
966  
967 The following classes are defined:
968  
969 =over
970  
971 =item C<domains>
972  
973 The list of domains this module supports.
974  
975 =item C<properties>
976  
977 The list of keys for the C<properties> domain this module knows.
978  
979 =item C<digests>
980  
981 The list of keys for the C<domains> domain this module knows.
982 Note that actual support of those digests depend on installed modules and is not reflected by this list.
983  
984 =item C<link_styles>
985  
986 The list of supported link styles. See L<File::FStore/new>.
987  
988 =item C<store_styles>
989  
990 The list of supported store styles. See L<File::FStore/new>.
991  
992 =item C<other-tags>
993  
994 A list of other tags this module knows about.
995 This list is hardly useful for most operations.
996 However it can be used to prime a database.
997  
998 =back
999  
1000 =head1 PROPERTIES
1001  
1002 The following properties are known.
1003  
1004 =head2 size
1005  
1006 The file size in bytes.
1007  
1008 =head2 inode
1009  
1010 The inode number. The value is specific to the filesystem the file is on.
1011  
1012 =head2 mediasubtype
1013  
1014 The media subtype of the file.
1015  
1016 B<Warning:>
1017 This property is commonly very missunderstood.
1018 It is best to not set this manually and let the store maintain the value.
1019 Setting this value is acceptable when importing data from another store or from a L<File::Information::Base> object.
1020  
1021 B<Note:>
1022 Only values listed at L<https://www.iana.org/assignments/media-types/media-types.xhtml> are valid.
1023 Any value not listed by IANA is invalid.
1024  
1025 =head2 contentise
1026  
1027 The content ISE (identifier) value.
1028 This value is mainly maintained internally, however may be set early as a mean to verify the file integrity.
1029 For reading the value there is a special method L</contentise>.
1030  
1031 =head2 inodeise
1032  
1033 The inode ISE (identifier) value.
1034 This value represents the inode on the file system.
1035 It is mainly maintained internally, but used by and imported from external sources
1036 such as tagpool.
1037 The value is stable for the inode's lifetime.
1038 It may be different or the same if the inode number is reused.
1039 It is stable since when the inode was created and also doesn't change when the inode is written to (in contrast to L</contenise>.
1040  
1041 See also:
1042 L<File::Information::Base/get>.
1043  
1044 =head1 AUTHOR
1045  
1046 Philipp Schafft <lion@cpan.org>
1047  
1048 =head1 COPYRIGHT AND LICENSE
1049  
1050 This software is Copyright (c) 2025 by Philipp Schafft <lion@cpan.org>.
1051  
1052 This is free software, licensed under:
1053  
1054 The Artistic License 2.0 (GPL Compatible)
1055  
1056 =cut
« blib/lib/File/FStore/File.pm ↑ Summary blib/lib/File/FStore.pm »