Brackup/Root.pm

filter: Statementstmt 68.1% 90 / 132 Branchbran 26.3% 10 / 38 Conditioncond 64.7% 11 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 77.4% 24 / 31 Podpod 33.3% 6 / 18 totaltotal 58.9% 145 / 246 CC 30 SCAR 22.9
CC
30
Cov
59%
__ANON__
42.0
encrypt
20.0
__ANON__
12.0
CRAP 9.9
« Brackup/Dict/DBM.pm ↑ Summary Brackup/Target/Amazon.pm »
1 package Brackup::Root;
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 File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Brackup::DigestCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Brackup::Util qw(io_print_to_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod undocumented
12 5 my ($class, $conf) = @_;
13 5 my $self = bless {}, $class;
14  
15 5 ▶ ($self->{name}) = $conf->name =~ m/^SOURCE:(.+)$/
Branch: 1 branch
BranchTrueFalseTotal
($self->{"name"}) = $conf->name =~ /^SOURCE:(.+)$/ or die "No backup-root name provided." 5 0 5
16 or die "No backup-root name provided.";
17 5 ▶ die "Backup-root name must be only a-z, A-Z, 0-9, and _." unless $self->{name} =~ /^\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} =~ /^\w+/ 0 5 5
18  
19 5 $self->{dir} = $conf->path_value('path');
20 5 ▶ $self->{gpg_path} = $conf->value('gpg_path') || "gpg";
Condition50%1/2
$conf->value('gpg_path') || 'gpg'
l!l
05
MC/DC0%0/1
$conf->value('gpg_path') || 'gpg'
$conf->value('gpg_path')
Truth table50%1/2
A
A= $conf->value('gpg_path')
Aresult
11
00
21 5 $self->{gpg_rcpt} = [ $conf->values('gpg_recipient') ];
22 5 $self->{chunk_size} = $conf->byte_value('chunk_size');
23 5 $self->{ignore} = [];
24  
25 5 $self->{smart_mp3_chunking} = $conf->bool_value('smart_mp3_chunking');
26  
27 5 $self->{merge_files_under} = $conf->byte_value('merge_files_under');
28 5 ▶ $self->{max_composite_size} = $conf->byte_value('max_composite_chunk_size') || 2**20;
Condition100%2/2
$conf->byte_value('max_composite_chunk_size') || 1048576.0
l!l
14
MC/DC100%1/1
$conf->byte_value('max_composite_chunk_size') || 1048576.0
$conf->byte_value('max_composite_chunk_size')
Truth table100%2/2
A
A= $conf->byte_value('max_composite_chunk_size')
Aresult
11
00
29  
30 die "'max_composite_chunk_size' must be greater than 'merge_files_under'\n" unless
31 5 ▶ $self->{max_composite_size} > $self->{merge_files_under};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"max_composite_size"} > $self->{"merge_files_under"} 0 5 5
32  
33 5 $self->{gpg_args} = []; # TODO: let user set this. for now, not possible
34  
35 5 $self->{digcache} = Brackup::DigestCache->new($self, $conf);
36 5 $self->{digcache_file} = $self->{digcache}->backing_file; # may be empty, if digest cache doesn't use a file
37  
38 5 $self->{noatime} = $conf->value('noatime');
39 5 return $self;
40 }
41  
42 5 ▶sub merge_files_under { $_[0]{merge_files_under} }
Subroutine merge_files_under: called CC=1 SCAR=0.0
Pod documented
43 1 ▶sub max_composite_size { $_[0]{max_composite_size} }
Subroutine max_composite_size: called CC=1 SCAR=0.0
Pod undocumented
44 0 ▶sub smart_mp3_chunking { $_[0]{smart_mp3_chunking} }
Subroutine smart_mp3_chunking: not called CC=1 SCAR=6.9
Pod documented
45  
46 0 ▶sub gpg_path {
Subroutine gpg_path: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my $self = shift;
48 0 return $self->{gpg_path};
49 }
50  
51 5 ▶sub gpg_args {
Subroutine gpg_args: called CC=1 SCAR=0.0
Pod undocumented
52 5 my $self = shift;
53 5 return @{ $self->{gpg_args} };
54 }
55  
56 351 ▶sub gpg_rcpts {
Subroutine gpg_rcpts: called CC=1 SCAR=0.0
Pod undocumented
57 351 my $self = shift;
58 351 return @{ $self->{gpg_rcpt} };
59 }
60  
61 # returns Brackup::DigestCache object
62 78 ▶sub digest_cache {
Subroutine digest_cache: called CC=1 SCAR=0.0
Pod undocumented
63 78 my $self = shift;
64 78 return $self->{digcache};
65 }
66  
67 57 ▶sub chunk_size {
Subroutine chunk_size: called CC=2 SCAR=6.9
Pod documented
68 57 my $self = shift;
69 57 ▶ return $self->{chunk_size} || (64 * 2**20); # default to 64MB
Condition100%2/2
$self->{'chunk_size'} || 67108864
l!l
4512
MC/DC100%1/1
$self->{'chunk_size'} || 67108864
$self->{'chunk_size'}
Truth table100%2/2
A
A= $self->{'chunk_size'}
Aresult
11
00
70 }
71  
72 5 ▶sub publicname {
Subroutine publicname: called CC=1 SCAR=0.0
Pod undocumented
73 # FIXME: let users define the public (obscured) name of their roots. s/porn/media/, etc.
74 # because their metafile key names (which contain the root) aren't encrypted.
75 5 return $_[0]{name};
76 }
77  
78 131 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
79 131 return $_[0]{name};
80 }
81  
82 26 ▶sub ignore {
Subroutine ignore: called CC=1 SCAR=0.0
Pod documented
83 26 my ($self, $pattern) = @_;
84 26 push @{ $self->{ignore} }, qr/$pattern/;
85 }
86  
87 138 ▶sub path {
Subroutine path: called CC=1 SCAR=0.0
Pod documented
88 138 return $_[0]{dir};
89 }
90  
91 70 ▶sub noatime {
Subroutine noatime: called CC=1 SCAR=0.0
Pod documented
92 70 return $_[0]{noatime};
93 }
94  
95 5 ▶sub foreach_file {
Subroutine foreach_file: called CC=2 SCAR=7.0
Pod undocumented
96 5 my ($self, $cb) = @_;
97  
98 5 ▶ chdir $self->{dir} or die "Failed to chdir to $self->{dir}";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"dir"} or die qq[Failed to chdir to $self->{"dir"}] 5 0 5
99  
100 5 my %statcache; # file -> statobj
101  
102 find({
103 no_chdir => 1,
104 13 ▶ preprocess => sub {
Subroutine __ANON__: called CC=11 SCAR=24.8
105 13 my $dir = $File::Find::dir;
106 13 my @good_dentries;
107 DENTRY:
108 13 foreach my $dentry (@_) {
109 91 ▶ next if $dentry eq "." || $dentry eq "..";
Branch: 1 branch
BranchTrueFalseTotal
if $dentry eq "." or $dentry eq ".." 26 65 91
Condition100%3/3
$dentry eq "." or $dentry eq ".."
l!l&&r!l&&!r
131365
MC/DC100%2/2
$dentry eq "." or $dentry eq ".."
$dentry eq "." $dentry eq ".."
Truth table100%3/3
A or B
A= $dentry eq "."B= $dentry eq ".."
ABresult
1X1
011
000
110  
111 65 my $path = "$dir/$dentry";
112 65 $path =~ s!^\./!!;
113  
114 # skip the digest database file. not sure if this is smart or not.
115 # for now it'd be kinda nice to have, but it's re-creatable from
116 # the backup meta files later, so let's skip it.
117 65 ▶ next if $self->{digcache_file} && $path eq $self->{digcache_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"digcache_file"} and $path eq $self->{"digcache_file"} 0 65 65
Condition33%1/3
$self->{"digcache_file"} and $path eq $self->{"digcache_file"}
!ll&&!rl&&r
0650
MC/DC0%0/2
$self->{"digcache_file"} and $path eq $self->{"digcache_file"}
$self->{"digcache_file"} $path eq $self->{"digcache_file"}
Truth table33%1/3
A and B
A= $self->{"digcache_file"}B= $path eq $self->{"digcache_file"}
ABresult
0X0
100
111
118  
119 # GC: seems to work fine as of at least gpg 1.4.5, so commenting out
120 # gpg seems to barf on files ending in whitespace, blowing
121 # stuff up, so we just skip them instead...
122 #if ($self->gpg_rcpts && $path =~ /\s+$/) {
123 # warn "Skipping file ending in whitespace: <$path>\n";
124 # next;
125 #}
126  
127 65 my $statobj = File::stat::lstat($path);
128 65 my $is_dir = -d _;
129  
130 65 foreach my $pattern (@{ $self->{ignore} }) {
131 339 ▶ next DENTRY if $path =~ /$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$pattern/ 0 339 339
132 339 ▶ next DENTRY if $is_dir && "$path/" =~ /$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dir and ("$path/") =~ /$pattern/ 0 339 339
Condition66%2/3
$is_dir and ("$path/") =~ /$pattern/
!ll&&!rl&&r
297420
MC/DC0%0/2
$is_dir and ("$path/") =~ /$pattern/
$is_dir ("$path/") =~ /$pattern/
Truth table66%2/3
A and B
A= $is_dirB= ("$path/") =~ /$pattern/
ABresult
0X0
100
111
133 339 ▶ next DENTRY if $path =~ m!(^|/)\.brackup-digest\.db(-journal)?$!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[(^|/)\.brackup-digest\.db(-journal)?$] 0 339 339
134 }
135  
136 65 $statcache{$path} = $statobj;
137 65 push @good_dentries, $dentry;
138 }
139  
140 # to let it recurse into the good directories we didn't
141 # already throw away:
142 13 return sort @good_dentries;
143 },
144  
145 70 ▶ wanted => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
146 70 my $path = $_;
147 70 $path =~ s!^\./!!;
148  
149 70 my $stat_obj = delete $statcache{$path};
150 70 my $file = Brackup::File->new(root => $self,
151 path => $path,
152 stat => $stat_obj,
153 );
154 70 $cb->($file);
155 },
156 5 }, ".");
157 }
158  
159 48 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
160 48 my $self = shift;
161 48 return $self->{name} . "($self->{dir})";
162 }
163  
164 0 ▶sub du_stats {
Subroutine du_stats: not called CC=2 SCAR=17.9
Pod undocumented
165 0 my $self = shift;
166  
167 0 my $show_all = $ENV{BRACKUP_DU_ALL};
168 0 my @dir_stack;
169 my %dir_size;
170 0 ▶ my $pop_dir = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
171 0 my $dir = pop @dir_stack;
172 0 ▶ printf("%-20d%s\n", $dir_size{$dir} || 0, $dir);
Condition0%0/2
$dir_size{$dir} || 0
l!l
00
MC/DC0%0/1
$dir_size{$dir} || 0
$dir_size{$dir}
Truth table0%0/2
A
A= $dir_size{$dir}
Aresult
11
00
173 0 delete $dir_size{$dir};
174 0 };
175 0 ▶ my $start_dir = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
176 0 my $dir = shift;
177 0 ▶ unless ($dir eq ".") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir eq ".") 0 0 0
178 0 my @parts = (".", split(m!/!, $dir));
179 0 ▶ while (@dir_stack >= @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dir_stack >= @parts) 0 0 0
180 0 $pop_dir->();
181 }
182 }
183 0 push @dir_stack, $dir;
184 0 };
185 0 ▶ $self->foreach_file(sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
186 0 my $file = shift;
187 0 my $path = $file->path;
188 0 ▶ if ($file->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_dir) 0 0 0
189 0 $start_dir->($path);
190 0 return;
191 }
192 0 ▶ if ($file->is_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_file) 0 0 0
193 0 my $size = $file->size;
194 0 ▶ my $kB = int($size / 1024) + ($size % 1024 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$size % 1024 ? : 0 0 0
195 0 ▶ printf("%-20d%s\n", $kB, $path) if $show_all;
Branch: 1 branch
BranchTrueFalseTotal
if $show_all 0 0 0
196 0 $dir_size{$_} += $kB foreach @dir_stack;
197 }
198 0 });
199  
200 0 ▶ $pop_dir->() while @dir_stack;
Branch: 1 branch
BranchTrueFalseTotal
@dir_stack and do { &$pop_dir() } 0 0 0
201 }
202  
203 # given filehandle to data, returns encrypted data
204 0 ▶sub encrypt {
Subroutine encrypt: not called CC=4 SCAR=30.0
Pod undocumented
205 0 my ($self, $data_fh, $outfn) = @_;
206 0 ▶ my @gpg_rcpts = $self->gpg_rcpts
Branch: 1 branch
BranchTrueFalseTotal
my(@gpg_rcpts) = $self->gpg_rcpts or Carp::confess("Encryption not setup for this root") 0 0 0
207 or Carp::confess("Encryption not setup for this root");
208  
209 0 my $cout = Symbol::gensym();
210 0 my $cin = Symbol::gensym();
211  
212 0 my @recipients = map {("--recipient", $_)} @gpg_rcpts;
213 0 my $pid = IPC::Open2::open2($cout, $cin,
214 $self->gpg_path, $self->gpg_args,
215 @recipients,
216 "--trust-model=always",
217 "--batch",
218 "--encrypt",
219 "--output", $outfn,
220 "--yes",
221 "-" # read from stdin
222 );
223  
224 # send data to gpg
225 0 binmode $cin;
226 0 ▶ my $bytes = io_print_to_fh($data_fh, $cin)
Branch: 1 branch
BranchTrueFalseTotal
my $bytes = io_print_to_fh($data_fh, $cin) or die "Sending data to gpg failed: $!" 0 0 0
227 or die "Sending data to gpg failed: $!";
228  
229 0 close $cin;
230 0 close $cout;
231  
232 0 waitpid($pid, 0);
233 0 ▶ die "GPG failed: $!" if $? != 0; # If gpg return status is non-zero
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
234 }
235  
236 13 1;
237  
238 __END__
« Brackup/Dict/DBM.pm ↑ Summary Brackup/Target/Amazon.pm »