Brackup/Backup.pm

filter: Statementstmt 85.7% 247 / 288 Branchbran 52.8% 56 / 106 Conditioncond 66.6% 28 / 42 MC/DCmcdc 26.9% 7 / 26 Subroutinesub 100% 32 / 32 Podpod 0.0% 0 / 17 totaltotal 72.4% 370 / 511 CC 76 SCAR 33.1
CC
76
Cov
76%
backup
67.1
report_progress
26.1
backup_header
8.3
CRAP 27.3
« Brackup/BackupStats.pm ↑ Summary Brackup/Restore.pm »
1 package Brackup::Backup;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Brackup::ChunkIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Brackup::CompositeChunk;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Brackup::GPGProcManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Brackup::GPGProcess;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
13 5 my ($class, %opts) = @_;
14 5 my $self = bless {}, $class;
15  
16 5 $self->{root} = delete $opts{root}; # Brackup::Root
17 5 $self->{target} = delete $opts{target}; # Brackup::Target
18 5 $self->{dryrun} = delete $opts{dryrun}; # bool
19 5 $self->{verbose} = delete $opts{verbose}; # bool
20 5 $self->{inventory} = delete $opts{inventory}; # bool
21 5 $self->{savefiles} = delete $opts{savefiles}; # bool
22 5 $self->{zenityprogress} = delete $opts{zenityprogress}; # bool
23  
24 5 $self->{modecounts} = {}; # type -> mode(octal) -> count
25 5 $self->{idcounts} = {}; # type -> uid/gid -> count
26  
27 5 $self->{_uid_map} = {}; # uid -> username
28 5 $self->{_gid_map} = {}; # gid -> group
29  
30 5 $self->{saved_files} = []; # list of Brackup::File objects backed up
31 5 $self->{unflushed_files} = []; # list of Brackup::File objects not in backup_file
32  
33 5 ▶ croak("Unknown options: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 5 5
34  
35 5 return $self;
36 }
37  
38 # returns true (a Brackup::BackupStats object) on success, or dies with error
39 5 ▶sub backup {
Subroutine backup: called CC=38 SCAR=42.1
Pod undocumented
40 5 my ($self, $backup_file) = @_;
41  
42 5 my $root = $self->{root};
43 5 my $target = $self->{target};
44  
45 5 my $stats = Brackup::BackupStats->new;
46  
47 5 my @gpg_rcpts = $self->{root}->gpg_rcpts;
48  
49 5 my $n_kb = 0.0; # num: kb of all files in root
50 5 my $n_files = 0; # int: # of files in root
51 5 my $n_kb_done = 0.0; # num: kb of files already done with (uploaded or skipped)
52  
53 # if we're pre-calculating the amount of data we'll
54 # actually need to upload, store it here.
55 5 my $n_files_up = 0;
56 5 my $n_kb_up = 0.0;
57 5 my $n_kb_up_need = 0.0; # by default, not calculated/used.
58  
59 5 my $n_files_done = 0; # int
60 5 my @files; # Brackup::File objs
61  
62 5 $self->debug("Discovering files in ", $root->path, "...\n");
63 5 $self->report_progress(0, "Discovering files in " . $root->path . "...");
64 70 ▶ $root->foreach_file(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 70 my ($file) = @_; # a Brackup::File
66 70 push @files, $file;
67 70 $self->record_mode_ids($file);
68 70 $n_files++;
69 70 $n_kb += $file->size / 1024;
70 5 });
71  
72 5 $self->debug("Number of files: $n_files\n");
73 5 $stats->timestamp('File Discovery');
74 5 $stats->set('Number of Files' => $n_files);
75 5 $stats->set('Total File Size' => sprintf('%0.01f MB', $n_kb / 1024));
76  
77 # calc needed chunks
78 5 ▶ if ($ENV{CALC_NEEDED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CALC_NEEDED"}) 0 5 5
79 0 my $fn = 0;
80 0 foreach my $f (@files) {
81 0 $fn++;
82 0 ▶ if ($fn % 100 == 0) { warn "$fn / $n_files ...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($fn % 100 == 0) 0 0 0
83 0 foreach my $pc ($f->chunks) {
84 0 ▶ if ($target->stored_chunk_from_inventory($pc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->stored_chunk_from_inventory($pc)) 0 0 0
85 0 $pc->forget_chunkref;
86 0 next;
87 }
88 0 $n_kb_up_need += $pc->length / 1024;
89 0 $pc->forget_chunkref;
90 }
91 }
92 0 warn "kb need to upload = $n_kb_up_need\n";
93 0 $stats->timestamp('Calc Needed');
94 }
95  
96  
97 5 my $chunk_iterator = Brackup::ChunkIterator->new(@files);
98 5 undef @files;
99 5 $stats->timestamp('Chunk Iterator');
100  
101 5 my $gpg_iter;
102 my $gpg_pm; # gpg ProcessManager
103 5 ▶ if (@gpg_rcpts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gpg_rcpts) 0 5 5
104 0 ($chunk_iterator, $gpg_iter) = $chunk_iterator->mux_into(2);
105 0 $gpg_pm = Brackup::GPGProcManager->new($gpg_iter, $target);
106 }
107  
108 # begin temp backup_file
109 5 my ($metafh, $meta_filename);
110 5 ▶ unless ($self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dryrun"}) 5 0 5
111 5 ($metafh, $meta_filename) = tempfile(
112 '.' . basename($backup_file) . 'XXXXX',
113 DIR => dirname($backup_file),
114 );
115 5 ▶ if (! @gpg_rcpts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@gpg_rcpts) 5 0 5
116 5 ▶ if (eval { require IO::Compress::Gzip }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require IO::Compress::Gzip } }) 5 0 5
117 5 close $metafh;
118 5 ▶ $metafh = IO::Compress::Gzip->new($meta_filename)
Branch: 1 branch
BranchTrueFalseTotal
$metafh = "IO::Compress::Gzip"->new($meta_filename) or die "Cannot open tempfile with IO::Compress::Gzip: $IO::Compress::Gzip::GzipError" 5 0 5
119 or die "Cannot open tempfile with IO::Compress::Gzip: $IO::Compress::Gzip::GzipError";
120 }
121 }
122 5 print $metafh $self->backup_header;
123 }
124  
125 5 my $cur_file; # current (last seen) file
126 my @stored_chunks;
127 5 my $file_has_shown_status = 0;
128  
129 5 my $merge_under = $root->merge_files_under;
130 5 my $comp_chunk = undef;
131  
132 75 ▶ my $end_file = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
133 75 ▶ return unless $cur_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur_file 5 70 75
134 70 ▶ if ($merge_under && $comp_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merge_under and $comp_chunk) { } 14 56 70
Condition100%3/3
$merge_under and $comp_chunk
!ll&&!rl&&r
55114
MC/DC100%2/2
$merge_under and $comp_chunk
$merge_under $comp_chunk
Truth table100%3/3
A and B
A= $merge_underB= $comp_chunk
ABresult
0X0
100
111
135 # defer recording to backup_file until CompositeChunk finalization
136 14 $self->add_unflushed_file($cur_file, [ @stored_chunks ]);
137 }
138 else {
139 56 ▶ print $metafh $cur_file->as_rfc822([ @stored_chunks ], $self) if $metafh;
Branch: 1 branch
BranchTrueFalseTotal
if $metafh 56 0 56
140 }
141 70 ▶ $self->add_saved_file($cur_file, [ @stored_chunks ]) if $self->{savefiles};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"savefiles"} 70 0 70
142 70 $n_files_done++;
143 70 $n_kb_done += $cur_file->size / 1024;
144 70 $cur_file = undef;
145 5 };
146 51 ▶ my $show_status = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
147 # use either size of files in normal case, or if we pre-calculated
148 # the size-to-upload (by looking in inventory, then we'll show the
149 # more accurate percentage)
150 51 ▶ my $percdone = 100 * ($n_kb_up_need ?
Branch: 1 branch
BranchTrueFalseTotal
$n_kb_up_need ? : 0 51 51
151 ($n_kb_up / $n_kb_up_need) :
152 ($n_kb_done / $n_kb));
153 51 ▶ my $mb_remain = ($n_kb_up_need ?
Branch: 1 branch
BranchTrueFalseTotal
$n_kb_up_need ? : 0 51 51
154 ($n_kb_up_need - $n_kb_up) :
155 ($n_kb - $n_kb_done)) / 1024;
156  
157 51 $self->debug(sprintf("* %-60s %d/%d (%0.02f%%; remain: %0.01f MB)",
158 $cur_file->path, $n_files_done, $n_files, $percdone,
159 $mb_remain));
160  
161 51 $self->report_progress($percdone);
162 5 };
163 70 ▶ my $start_file = sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
164 70 $end_file->();
165 70 $cur_file = shift;
166 70 @stored_chunks = ();
167 70 ▶ $show_status->() if $cur_file->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_file->is_dir 13 57 70
168 70 ▶ if ($gpg_iter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpg_iter) 0 70 70
169 # catch our gpg iterator up. we want it to be ahead of us,
170 # nothing iteresting is behind us.
171 0 ▶ $gpg_iter->next while $gpg_iter->behind_by > 1;
Branch: 1 branch
BranchTrueFalseTotal
$gpg_iter->behind_by > 1 and do { $gpg_iter->next } 0 0 0
172 }
173 70 $file_has_shown_status = 0;
174 5 };
175  
176 # records are either Brackup::File (for symlinks, directories, etc), or
177 # PositionedChunks, in which case the file can asked of the chunk
178 5 ▶ while (my $rec = $chunk_iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $chunk_iterator->next) 82 5 87
179 82 ▶ if ($rec->isa("Brackup::File")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->isa("Brackup::File")) 13 69 82
180 13 $start_file->($rec);
181 13 next;
182 }
183 69 my $pchunk = $rec;
184 69 ▶ if ($pchunk->file != $cur_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pchunk->file != $cur_file) 57 12 69
185 57 $start_file->($pchunk->file);
186 }
187  
188 # have we already stored this chunk before? (iterative backup)
189 69 my $schunk;
190 69 ▶ if ($schunk = $target->stored_chunk_from_inventory($pchunk)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schunk = $target->stored_chunk_from_inventory($pchunk)) 19 50 69
191 19 $pchunk->forget_chunkref;
192 19 push @stored_chunks, $schunk;
193 19 next;
194 }
195  
196 # weird case... have we stored this same pchunk digest in the
197 # current comp_chunk we're building? these aren't caught by
198 # the above inventory check, because chunks in a composite
199 # chunk aren't added to the inventory until after the the composite
200 # chunk has fully grown (because it's not until it's fully grown
201 # that we know the handle for it, its digest)
202 50 ▶ if ($comp_chunk && ($schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_chunk and $schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk)) 2 48 50
Condition100%3/3
$comp_chunk and $schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk)
!ll&&!rl&&r
3992
MC/DC100%2/2
$comp_chunk and $schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk)
$comp_chunk $schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk)
Truth table100%3/3
A and B
A= $comp_chunkB= $schunk = $comp_chunk->stored_chunk_from_dup_internal_raw($pchunk)
ABresult
0X0
100
111
203 2 $pchunk->forget_chunkref;
204 2 push @stored_chunks, $schunk;
205 2 next;
206 }
207  
208 48 ▶ unless ($file_has_shown_status++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_has_shown_status++) 38 10 48
209 38 $show_status->();
210 38 $n_files_up++;
211 }
212 48 $self->debug(" * storing chunk: ", $pchunk->as_string, "\n");
213 48 $self->report_progress(undef, $pchunk->file->path . " (" . $pchunk->offset . "," . $pchunk->length . ")");
214  
215 48 ▶ unless ($self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dryrun"}) 48 0 48
216 48 $schunk = Brackup::StoredChunk->new($pchunk);
217  
218 # encrypt it
219 48 ▶ if (@gpg_rcpts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gpg_rcpts) 0 48 48
220 0 $schunk->set_encrypted_chunkref($gpg_pm->enc_chunkref_of($pchunk));
221 }
222  
223 # see if we should pack it into a bigger blob
224 48 my $chunk_size = $schunk->backup_length;
225  
226 # see if we should merge this chunk (in this case, file) together with
227 # other small files we encountered earlier, into a "composite chunk",
228 # to be stored on the target in one go.
229  
230 # Note: no technical reason for only merging small files (is_entire_file),
231 # and not the tails of larger files. just don't like the idea of files being
232 # both split up (for big head) and also merged together (for little end).
233 # would rather just have 1 type of magic per file. (split it or join it)
234 48 ▶ if ($merge_under && $chunk_size < $merge_under && $pchunk->is_entire_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merge_under and $chunk_size < $merge_under and $pchunk->is_entire_file) { } 5 43 48
Condition83%5/6
$merge_under and $chunk_size < $merge_under and $pchunk->is_entire_file
!ll&&!rl&&r
4305
$merge_under and $chunk_size < $merge_under
!ll&&!rl&&r
3855
MC/DC66%2/3
$merge_under and $chunk_size < $merge_under and $pchunk->is_entire_file
$merge_under $chunk_size < $merge_under $pchunk->is_entire_file
Truth table75%3/4
A and B and C
A= $merge_underB= $chunk_size < $merge_underC= $pchunk->is_entire_file
ABCresult
0XX0
10X0
1100
1111
235 5 ▶ if ($comp_chunk && ! $comp_chunk->can_fit($chunk_size)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_chunk and not $comp_chunk->can_fit($chunk_size)) 0 5 5
Condition66%2/3
$comp_chunk and not $comp_chunk->can_fit($chunk_size)
!ll&&!rl&&r
140
MC/DC0%0/2
$comp_chunk and not $comp_chunk->can_fit($chunk_size)
$comp_chunk not $comp_chunk->can_fit($chunk_size)
Truth table66%2/3
A and not(B)
A= $comp_chunkB= not $comp_chunk->can_fit($chunk_size)
ABresult
0X0
100
111
236 0 $self->debug("Finalizing composite chunk $comp_chunk...");
237 0 $comp_chunk->finalize;
238 0 $comp_chunk = undef;
239 0 $self->flush_files($metafh);
240 }
241 5 ▶ $comp_chunk ||= Brackup::CompositeChunk->new($root, $target);
Condition66%2/3
$comp_chunk ||= "Brackup::CompositeChunk"->new($root, $target)
l!l&&r!l&&!r
410
MC/DC0%0/2
$comp_chunk ||= "Brackup::CompositeChunk"->new($root, $target)
$comp_chunk "Brackup::CompositeChunk"->new($root, $target)
Truth table66%2/3
A ||= B
A= $comp_chunkB= "Brackup::CompositeChunk"->new($root, $target)
ABresult
1X1
011
000
242 5 $comp_chunk->append_little_chunk($schunk);
243 } else {
244 # store it regularly, as its own chunk on the target
245 43 ▶ $target->store_chunk($schunk)
Branch: 1 branch
BranchTrueFalseTotal
$target->store_chunk($schunk) or die "Chunk storage failed.\n" 43 0 43
246 or die "Chunk storage failed.\n";
247 43 $target->add_to_inventory($pchunk => $schunk);
248 }
249  
250 # if only this worked... (LWP protocol handler seems to
251 # get confused by its syscalls getting interrupted?)
252 #local $SIG{CHLD} = sub {
253 # print "some child finished!\n";
254 # $gpg_pm->start_some_processes;
255 #};
256  
257  
258 48 $n_kb_up += $pchunk->length / 1024;
259 48 $schunk->forget_chunkref;
260 48 push @stored_chunks, $schunk;
261 }
262  
263 #$stats->note_stored_chunk($schunk);
264  
265 # DEBUG: verify it got written correctly
266 48 ▶ if ($ENV{BRACKUP_PARANOID}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BRACKUP_PARANOID"}) 0 48 48
267 0 die "FIX UP TO NEW API";
268 #my $saved_ref = $target->load_chunk($handle);
269 #my $saved_len = length $$saved_ref;
270 #unless ($saved_len == $chunk->backup_length) {
271 # warn "Saved length of $saved_len doesn't match our length of " . $chunk->backup_length . "\n";
272 # die;
273 #}
274 }
275  
276 48 $stats->check_maxmem;
277 48 $pchunk->forget_chunkref;
278 }
279 5 $end_file->();
280 5 ▶ $comp_chunk->finalize if $comp_chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $comp_chunk 1 4 5
281 5 $self->flush_files($metafh);
282 5 $stats->timestamp('Chunk Storage');
283 5 $stats->set('Number of Files Uploaded:', $n_files_up);
284 5 $stats->set('Total File Size Uploaded:', sprintf('%0.01f MB', $n_kb_up / 1024));
285  
286 5 ▶ unless ($self->{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dryrun"}) 5 0 5
287 5 ▶ close $metafh or die "Close on metafile '$backup_file' failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $metafh or die "Close on metafile '${backup_file}' failed: $!" 5 0 5
288 5 ▶ rename $meta_filename, $backup_file
Branch: 1 branch
BranchTrueFalseTotal
rename $meta_filename, $backup_file or die "Failed to rename temporary backup_file: $!\n" 5 0 5
289 or die "Failed to rename temporary backup_file: $!\n";
290  
291 5 my ($store_fh, $store_filename);
292 5 my $is_encrypted = 0;
293  
294 # store the metafile, encrypted, on the target
295 5 ▶ if (@gpg_rcpts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@gpg_rcpts) { } 0 5 5
296 0 my $encfile = $backup_file . ".enc";
297 0 my @recipients = map {("--recipient", $_)} @gpg_rcpts;
298 system($self->{root}->gpg_path, $self->{root}->gpg_args,
299 0 ▶ @recipients,
Branch: 1 branch
BranchTrueFalseTotal
system $self->{"root"}->gpg_path, $self->{"root"}->gpg_args, @recipients, "--trust-model=always", "--batch", "--encrypt", "--output=$encfile", "--yes", $backup_file and die "Failed to run gpg while encryping metafile: $!\n" 0 0 0
300 "--trust-model=always",
301 "--batch",
302 "--encrypt",
303 "--output=$encfile",
304 "--yes",
305 $backup_file)
306 and die "Failed to run gpg while encryping metafile: $!\n";
307 0 ▶ open ($store_fh, $encfile) or die "Failed to open encrypted metafile '$encfile': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $store_fh, $encfile or die "Failed to open encrypted metafile '${encfile}': $!\n" 0 0 0
308 0 $store_filename = $encfile;
309 0 $is_encrypted = 1;
310 } else {
311 # Reopen $metafh to reset file pointer (no backward seek with IO::Compress::Gzip)
312 5 ▶ open($store_fh, $backup_file) or die "Failed to open metafile '$backup_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $store_fh, $backup_file or die "Failed to open metafile '${backup_file}': $!\n" 5 0 5
313 5 $store_filename = $backup_file;
314 }
315  
316 # store it on the target
317 5 $self->debug("Storing metafile to " . ref($target));
318 5 my $name = $self->{root}->publicname . "-" . $self->backup_time;
319 5 $target->store_backup_meta($name, $store_fh, { filename => $store_filename, is_encrypted => $is_encrypted });
320 5 $stats->timestamp('Metafile Storage');
321  
322 # cleanup encrypted metafile
323 5 ▶ if ($is_encrypted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encrypted) 0 5 5
324 0 ▶ close $store_fh or die "Close on encrypted metafile failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $store_fh or die "Close on encrypted metafile failed: $!" 0 0 0
325 0 unlink $store_filename;
326 }
327 }
328 5 $self->report_progress(100, "Backup complete.");
329  
330 5 return $stats;
331 }
332  
333 62 ▶sub default_file_mode {
Subroutine default_file_mode: called CC=2 SCAR=7.1
Pod undocumented
334 62 my $self = shift;
335 62 ▶ return $self->{_def_file_mode} ||= $self->_default_mode('f');
Condition66%2/3
$self->{"_def_file_mode"} ||= $self->_default_mode("f")
l!l&&r!l&&!r
5750
MC/DC0%0/2
$self->{"_def_file_mode"} ||= $self->_default_mode("f")
$self->{"_def_file_mode"} $self->_default_mode("f")
Truth table66%2/3
A ||= B
A= $self->{"_def_file_mode"}B= $self->_default_mode("f")
ABresult
1X1
011
000
336 }
337  
338 18 ▶sub default_directory_mode {
Subroutine default_directory_mode: called CC=2 SCAR=7.1
Pod undocumented
339 18 my $self = shift;
340 18 ▶ return $self->{_def_dir_mode} ||= $self->_default_mode('d');
Condition66%2/3
$self->{"_def_dir_mode"} ||= $self->_default_mode("d")
l!l&&r!l&&!r
1350
MC/DC0%0/2
$self->{"_def_dir_mode"} ||= $self->_default_mode("d")
$self->{"_def_dir_mode"} $self->_default_mode("d")
Truth table66%2/3
A ||= B
A= $self->{"_def_dir_mode"}B= $self->_default_mode("d")
ABresult
1X1
011
000
341 }
342  
343 10 ▶sub _default_mode {
Subroutine _default_mode: called CC=2 SCAR=7.0
344 10 my ($self, $type) = @_;
345 10 ▶ my $map = $self->{modecounts}{$type} || {};
Condition50%1/2
$self->{'modecounts'}{$type} || {}
l!l
100
MC/DC0%0/1
$self->{'modecounts'}{$type} || {}
$self->{'modecounts'}{$type}
Truth table50%1/2
A
A= $self->{'modecounts'}{$type}
Aresult
11
00
346 10 return (sort { $map->{$b} <=> $map->{$a} } keys %$map)[0];
347 }
348  
349 75 ▶sub default_uid {
Subroutine default_uid: called CC=2 SCAR=7.1
Pod undocumented
350 75 my $self = shift;
351 75 ▶ return $self->{_def_uid} ||= $self->_default_id('u');
Condition66%2/3
$self->{"_def_uid"} ||= $self->_default_id("u")
l!l&&r!l&&!r
7050
MC/DC0%0/2
$self->{"_def_uid"} ||= $self->_default_id("u")
$self->{"_def_uid"} $self->_default_id("u")
Truth table66%2/3
A ||= B
A= $self->{"_def_uid"}B= $self->_default_id("u")
ABresult
1X1
011
000
352 }
353  
354 75 ▶sub default_gid {
Subroutine default_gid: called CC=2 SCAR=7.1
Pod undocumented
355 75 my $self = shift;
356 75 ▶ return $self->{_def_gid} ||= $self->_default_id('g');
Condition66%2/3
$self->{"_def_gid"} ||= $self->_default_id("g")
l!l&&r!l&&!r
7050
MC/DC0%0/2
$self->{"_def_gid"} ||= $self->_default_id("g")
$self->{"_def_gid"} $self->_default_id("g")
Truth table66%2/3
A ||= B
A= $self->{"_def_gid"}B= $self->_default_id("g")
ABresult
1X1
011
000
357 }
358  
359 10 ▶sub _default_id {
Subroutine _default_id: called CC=2 SCAR=7.6
360 10 my ($self, $type) = @_;
361 10 ▶ my $map = $self->{idcounts}{$type} || {};
Condition50%1/2
$self->{'idcounts'}{$type} || {}
l!l
100
MC/DC0%0/1
$self->{'idcounts'}{$type} || {}
$self->{'idcounts'}{$type}
Truth table50%1/2
A
A= $self->{'idcounts'}{$type}
Aresult
11
00
362 0 return (sort { $map->{$b} <=> $map->{$a} } keys %$map)[0];
363 }
364  
365 # space-separated list of local uid:username mappings
366 5 ▶sub uid_map {
Subroutine uid_map: called CC=3 SCAR=11.2
Pod undocumented
367 5 my $self = shift;
368 5 my @map;
369 5 my $uidcounts = $self->{idcounts}{u};
370 0 for my $uid (sort { $a <=> $b } keys %$uidcounts) {
371 5 ▶ if (my $name = getpwuid($uid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = getpwuid $uid) 5 0 5
372 5 push @map, "$uid:$name";
373 }
374 }
375 5 return join(' ', @map);
376 }
377  
378 # space-separated list of local gid:group mappings
379 5 ▶sub gid_map {
Subroutine gid_map: called CC=3 SCAR=11.2
Pod undocumented
380 5 my $self = shift;
381 5 my @map;
382 5 my $gidcounts = $self->{idcounts}{g};
383 0 for my $gid (sort { $a <=> $b } keys %$gidcounts) {
384 5 ▶ if (my $name = getgrgid($gid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = getgrgid $gid) 5 0 5
385 5 push @map, "$gid:$name";
386 }
387 }
388 5 return join(' ', @map);
389 }
390  
391 10 ▶sub backup_time {
Subroutine backup_time: called CC=2 SCAR=6.9
Pod undocumented
392 10 my $self = shift;
393 10 ▶ return $self->{backup_time} ||= time();
Condition100%2/2
$self->{"backup_time"} ||= time
l!l
55
MC/DC100%1/1
$self->{"backup_time"} ||= time
$self->{"backup_time"}
Truth table100%2/2
A
A= $self->{"backup_time"}
Aresult
11
00
394 }
395  
396 5 ▶sub backup_header {
Subroutine backup_header: called CC=8 SCAR=21.2
Pod undocumented
397 5 my $self = shift;
398 5 my $ret = "";
399 5 my $now = $self->backup_time;
400 5 $ret .= "BackupTime: " . $now . " (" . localtime($now) . ")\n";
401 5 $ret .= "BackupDriver: " . ref($self->{target}) . "\n";
402 5 ▶ if (my $fields = $self->{target}->backup_header) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fields = $self->{"target"}->backup_header) 5 0 5
403 5 foreach my $k (sort keys %$fields) {
404 10 ▶ die "Bogus header field from driver" unless $k =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^\w+$/ 0 10 10
405 10 my $val = $fields->{$k};
406 10 ▶ next if ! defined $val || $val eq ''; # skip keys with empty values
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or $val eq "" 0 10 10
Condition33%1/3
not defined $val or $val eq ""
l!l&&r!l&&!r
0010
MC/DC0%0/2
not defined $val or $val eq ""
not defined $val $val eq ""
Truth table33%1/3
not(A) or B
A= not defined $valB= $val eq ""
ABresult
1X1
011
000
407 10 ▶ die "Bogus header value from driver" if $val =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /[\r\n]/ 0 10 10
408 10 $ret .= "Driver-$k: $val\n";
409 }
410 }
411 5 $ret .= "RootName: " . $self->{root}->name . "\n";
412 5 $ret .= "RootPath: " . $self->{root}->path . "\n";
413 5 $ret .= "TargetName: " . $self->{target}->name . "\n";
414 5 $ret .= "DefaultFileMode: " . $self->default_file_mode . "\n";
415 5 $ret .= "DefaultDirMode: " . $self->default_directory_mode . "\n";
416 5 $ret .= "DefaultUID: " . $self->default_uid . "\n";
417 5 $ret .= "DefaultGID: " . $self->default_gid . "\n";
418 5 $ret .= "UIDMap: " . $self->uid_map . "\n";
419 5 $ret .= "GIDMap: " . $self->gid_map . "\n";
420 5 $ret .= "GPG-Recipient: $_\n" for $self->{root}->gpg_rcpts;
421 5 $ret .= "\n";
422 5 return $ret;
423 }
424  
425 70 ▶sub record_mode_ids {
Subroutine record_mode_ids: called CC=1 SCAR=0.0
Pod undocumented
426 70 my ($self, $file) = @_;
427 70 $self->{modecounts}{$file->type}{$file->mode}++;
428 70 $self->{idcounts}{u}{$file->uid}++;
429 70 $self->{idcounts}{g}{$file->gid}++;
430 }
431  
432 14 ▶sub add_unflushed_file {
Subroutine add_unflushed_file: called CC=1 SCAR=0.0
Pod undocumented
433 14 my ($self, $file, $handlelist) = @_;
434 14 push @{ $self->{unflushed_files} }, [ $file, $handlelist ];
435 }
436  
437 5 ▶sub flush_files {
Subroutine flush_files: called CC=3 SCAR=11.0
Pod undocumented
438 5 my ($self, $fh) = @_;
439 5 ▶ while (my $rec = shift @{ $self->{unflushed_files} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = shift @{$$self{"unflushed_files"};}) 14 5 19
440 14 ▶ next unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 14 14
441 14 my ($file, $stored_chunks) = @$rec;
442 14 print $fh $file->as_rfc822($stored_chunks, $self);
443 }
444 }
445  
446 70 ▶sub add_saved_file {
Subroutine add_saved_file: called CC=1 SCAR=0.0
Pod undocumented
447 70 my ($self, $file, $handlelist) = @_;
448 70 push @{ $self->{saved_files} }, [ $file, $handlelist ];
449 }
450  
451 1 ▶sub foreach_saved_file {
Subroutine foreach_saved_file: called CC=2 SCAR=6.9
Pod undocumented
452 1 my ($self, $cb) = @_;
453 1 foreach my $rec (@{ $self->{saved_files} }) {
454 15 $cb->(@$rec); # Brackup::File, arrayref of Brackup::StoredChunk
455 }
456 }
457  
458 114 ▶sub debug {
Subroutine debug: called CC=2 SCAR=10.1
Pod undocumented
459 114 my ($self, @m) = @_;
460 114 ▶ return unless $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"verbose"} 114 0 114
461 0 my $line = join("", @m);
462 0 chomp $line;
463 0 print $line, "\n";
464 }
465  
466 109 ▶sub report_progress {
Subroutine report_progress: called CC=6 SCAR=32.6
Pod undocumented
467 109 my ($self, $percent, $message) = @_;
468  
469 109 ▶ if ($self->{zenityprogress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"zenityprogress"}) 0 109 109
470 0 ▶ if (defined($message) && length($message) > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and length $message > 100) 0 0 0
Condition0%0/3
defined $message and length $message > 100
!ll&&!rl&&r
000
MC/DC0%0/2
defined $message and length $message > 100
defined $message length $message > 100
Truth table0%0/3
A and B
A= defined $messageB= length $message > 100
ABresult
0X0
100
111
471 0 $message = substr($message, 0, 100)."...";
472 }
473 0 ▶ print STDOUT "#", $message, "\n" if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 0 0 0
474 0 ▶ print STDOUT $percent, "\n" if defined $percent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $percent 0 0 0
475 }
476 }
477  
478 13 1;
479  
« Brackup/BackupStats.pm ↑ Summary Brackup/Restore.pm »