Brackup/StoredChunk.pm

filter: Statementstmt 83.1% 84 / 101 Branchbran 61.7% 21 / 34 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 18 totaltotal 68.7% 132 / 192 CC 22 SCAR 14.3
CC
22
Cov
76%
set_encrypted_chunkref
20.0
chunkref
4.9
new_from_inventory_value
4.8
CRAP 4.2
« Brackup/ConfigSection.pm ↑ Summary Brackup/Metafile.pm »
1 package Brackup::StoredChunk;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Brackup::Util qw(io_sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # fields:
10 # pchunk - always
11 # backlength - memoized
12 # backdigest - memoized
13 # _chunkref - memoized
14 # compchunk - composite chunk, if we were added to a composite chunk
15 # compfrom - offset in composite chunk where we start
16 # compto - offset in composite chunk where we end
17 # lite - true if this is a 'lite' or 'handle' version
18  
19 57 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
20 57 my ($class, $pchunk) = @_;
21 57 my $self = bless {}, $class;
22 57 $self->{pchunk} = $pchunk;
23 57 return $self;
24 }
25  
26 33 ▶sub pchunk { $_[0]{pchunk} }
Subroutine pchunk: called CC=1 SCAR=0.0
Pod undocumented
27  
28 # create the 'lite' or 'handle' version of a storedchunk. can't get to
29 # the chunkref from this, but callers aren't won't. and we'll DIE if they
30 # try to access the chunkref.
31 19 ▶sub new_from_inventory_value {
Subroutine new_from_inventory_value: called CC=3 SCAR=15.7
Pod undocumented
32 19 my ($class, $pchunk, $invval) = @_;
33  
34 19 my ($dig, $len, $range) = split /\s+/, $invval;
35  
36 19 my $sc = bless {
37 pchunk => $pchunk,
38 backdigest => $dig,
39 backlength => $len,
40 lite => 1,
41 }, $class;
42  
43 # normal
44 19 ▶ return $sc unless $range;
Branch: 1 branch
BranchTrueFalseTotal
unless $range 19 0 19
45  
46 # in case of little file in a composite chunk,
47 # we gotta be a range.
48 0 ▶ my ($from, $to) = $range =~ /^(\d+)-(\d+)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $range =~ /^(\d+)-(\d+)$/ or die "bogus range: $range" 0 0 0
49 or die "bogus range: $range";
50 0 $sc->{compfrom} = $from;
51 0 $sc->{compto} = $to;
52 0 return $sc;
53 }
54  
55 2 ▶sub clone_but_for_pchunk {
Subroutine clone_but_for_pchunk: called CC=2 SCAR=6.9
Pod undocumented
56 2 my ($self, $pchunk) = @_;
57 2 my $copy = bless {}, ref $self;
58 2 foreach my $f (qw(backlength backdigest compchunk compfrom compto)) {
59 10 $copy->{$f} = $self->{$f};
60 }
61 2 $copy->{pchunk} = $pchunk;
62 2 return $copy;
63 }
64  
65 5 ▶sub set_composite_chunk {
Subroutine set_composite_chunk: called CC=1 SCAR=0.0
Pod undocumented
66 5 my ($self, $cchunk, $from, $to) = @_;
67 5 $self->{compchunk} = $cchunk;
68  
69 # forget our backup length/digest. this handle information
70 # to the stored chunk should be asked of our composite
71 # chunk in the future, when it's done populating.
72 5 $self->{backdigest} = undef;
73 5 $self->{backlength} = undef;
74 5 $self->forget_chunkref;
75  
76 5 $self->{compfrom} = $from;
77 5 $self->{compto} = $to;
78 }
79  
80 119 ▶sub range_in_composite {
Subroutine range_in_composite: called CC=3 SCAR=11.0
Pod undocumented
81 119 my $self = shift;
82 119 ▶ return undef unless $self->{compfrom} || $self->{compto};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compfrom"} or $self->{"compto"} 105 14 119
Condition100%3/3
$self->{"compfrom"} or $self->{"compto"}
l!l&&r!l&&!r
95105
MC/DC100%2/2
$self->{"compfrom"} or $self->{"compto"}
$self->{"compfrom"} $self->{"compto"}
Truth table100%3/3
A or B
A= $self->{"compfrom"}B= $self->{"compto"}
ABresult
1X1
011
000
83 14 return "$self->{compfrom}-$self->{compto}";
84 }
85  
86 185 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
87 185 my $self = shift;
88 185 return $self->{pchunk}->file;
89 }
90  
91 185 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod undocumented
92 185 my $self = shift;
93 185 return $self->file->root;
94 }
95  
96 # returns true if encrypted, false otherwise
97 185 ▶sub encrypted {
Subroutine encrypted: called CC=2 SCAR=7.2
Pod undocumented
98 185 my $self = shift;
99 185 ▶ return $self->root->gpg_rcpts ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->root->gpg_rcpts ? : 0 185 185
100 }
101  
102 0 ▶sub compressed {
Subroutine compressed: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my $self = shift;
104 # TODO/FUTURE: support compressed chunks (for non-encrypted
105 # content; gpg already compresses)
106 0 return 0;
107 }
108  
109 # the original length, pre-encryption
110 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my $self = shift;
112 0 return $self->{pchunk}->length;
113 }
114  
115 # the length, either encrypted or not
116 267 ▶sub backup_length {
Subroutine backup_length: called CC=2 SCAR=6.9
Pod undocumented
117 267 my $self = shift;
118 267 ▶ return $self->{backlength} if defined $self->{backlength};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"backlength"} 219 48 267
119 48 $self->_populate_lengthdigest;
120 48 return $self->{backlength};
121 }
122  
123 # the digest, either encrypted or not
124 171 ▶sub backup_digest {
Subroutine backup_digest: called CC=2 SCAR=6.9
Pod undocumented
125 171 my $self = shift;
126 171 ▶ return $self->{backdigest} if $self->{backdigest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"backdigest"} 155 16 171
127 16 $self->_populate_lengthdigest;
128 16 return $self->{backdigest};
129 }
130  
131 64 ▶sub _populate_lengthdigest {
Subroutine _populate_lengthdigest: called CC=3 SCAR=11.0
132 64 my $self = shift;
133  
134 # Composite chunk version
135 64 ▶ if (my $cchunk = $self->{compchunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cchunk = $self->{"compchunk"}) 7 57 64
136 7 $self->{backlength} = $cchunk->backup_length;
137 7 $self->{backdigest} = $cchunk->digest;
138 7 return 1;
139 }
140  
141 57 ▶ die "ASSERT: encrypted length or digest not set" if $self->encrypted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encrypted 0 57 57
142  
143 # Unencrypted version
144 57 $self->{backdigest} = "sha1:" . io_sha1($self->{pchunk}->raw_chunkref);
145 57 $self->{backlength} = $self->{pchunk}->length; # length of raw data
146 57 return 1;
147 }
148  
149 57 ▶sub chunkref {
Subroutine chunkref: called CC=4 SCAR=15.9
Pod undocumented
150 57 my $self = shift;
151 57 ▶ if ($self->{_chunkref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_chunkref"}) 0 57 57
152 0 $self->{_chunkref}->seek(0, SEEK_SET);
153 0 return $self->{_chunkref};
154 }
155  
156 # encrypting case: chunkref gets set via set_encrypted_chunkref in Backup::backup
157 57 ▶ croak "ASSERT: encrypted but no chunkref set" if $self->encrypted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->encrypted 0 57 57
158  
159 # caller/consistency check:
160 Carp::confess("Can't access chunkref on lite StoredChunk instance (handle only)")
161 57 ▶ if $self->{lite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lite"} 0 57 57
162  
163 # non-encrypting case
164 57 return $self->{_chunkref} = $self->{pchunk}->raw_chunkref;
165 }
166  
167 # set encrypted chunk filehandle and digest/length
168 0 ▶sub set_encrypted_chunkref {
Subroutine set_encrypted_chunkref: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my ($self, $fh, $enc_length) = @_;
170 0 ▶ die "ASSERT: not enc" unless $self->encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->encrypted 0 0 0
171 0 ▶ die "ASSERT: already set?" if $self->{backlength} || $self->{backdigest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"backlength"} or $self->{"backdigest"} 0 0 0
Condition0%0/3
$self->{"backlength"} or $self->{"backdigest"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"backlength"} or $self->{"backdigest"}
$self->{"backlength"} $self->{"backdigest"}
Truth table0%0/3
A or B
A= $self->{"backlength"}B= $self->{"backdigest"}
ABresult
1X1
011
000
172  
173 0 $self->{backdigest} = "sha1:" . io_sha1($fh);
174 0 $self->{backlength} = $enc_length;
175  
176 0 return $self->{_chunkref} = $fh;
177 }
178  
179 # lose the chunkref data
180 53 ▶sub forget_chunkref {
Subroutine forget_chunkref: called CC=2 SCAR=6.9
Pod undocumented
181 53 my $self = shift;
182 53 ▶ return unless $self->{_chunkref};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_chunkref"} 5 48 53
183 48 $self->{_chunkref}->close;
184 48 delete $self->{_chunkref}; # this also deletes the tempfile, if any
185 }
186  
187 # to the format used by the metafile
188 71 ▶sub to_meta {
Subroutine to_meta: called CC=4 SCAR=14.6
Pod undocumented
189 71 my $self = shift;
190 my @parts = ($self->{pchunk}->offset,
191 71 $self->{pchunk}->length);
192  
193 71 ▶ if (my $range = $self->range_in_composite) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $self->range_in_composite) { } 9 62 71
194 9 push @parts, (
195 $range,
196 $self->backup_digest,
197 );
198 } else {
199 62 push @parts, (
200 $self->backup_length,
201 $self->backup_digest,
202 );
203 }
204  
205 # if the inventory database is lost, it should be possible to
206 # recover the inventory database from the *.brackup files.
207 # if a file only has on chunk, the digest(raw) -> digest(enc)
208 # can be inferred from the file's digest, then the stored
209 # chunk's digest. but if we have multiple chunks, we need
210 # to store each chunk's raw digest as well in the chunk
211 # list. we could do this all the time, but considering
212 # most files are small, we want to save space in the *.brackup
213 # meta file and only do it when necessary.
214 71 ▶ if ($self->encrypted && $self->file->chunks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encrypted and $self->file->chunks > 1) 0 71 71
Condition33%1/3
$self->encrypted and $self->file->chunks > 1
!ll&&!rl&&r
7100
MC/DC0%0/2
$self->encrypted and $self->file->chunks > 1
$self->encrypted $self->file->chunks > 1
Truth table33%1/3
A and B
A= $self->encryptedB= $self->file->chunks > 1
ABresult
0X0
100
111
215 0 push @parts, $self->{pchunk}->raw_digest;
216 }
217  
218 71 return join(";", @parts);
219 }
220  
221 # aka "instructions to attach to a pchunk, on how to recover the pchunk from a target"
222 48 ▶sub inventory_value {
Subroutine inventory_value: called CC=2 SCAR=6.9
Pod undocumented
223 48 my $self = shift;
224  
225 # when this chunk was stored as part of a composite chunk, the instructions
226 # are of form:
227 # sha1:deadbeef 0-50
228 # which means download "sha1:deadbeef", then the contents will be in from
229 # byte offset 0 to byte offset 50 (length of 50).
230 48 ▶ if (my $range = $self->range_in_composite) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = $self->range_in_composite) 5 43 48
231 5 return join(" ",
232 $self->backup_digest,
233 $self->backup_length,
234 $range);
235 }
236  
237 # else, the historical format:
238 # sha1:deadbeef <length>
239 43 return join(" ", $self->backup_digest, $self->backup_length);
240 }
241  
242 13 1;
« Brackup/ConfigSection.pm ↑ Summary Brackup/Metafile.pm »