Brackup/Restore.pm

filter: Statementstmt 88.2% 202 / 229 Branchbran 57.3% 78 / 136 Conditioncond 48.3% 30 / 62 MC/DCmcdc 13.1% 5 / 38 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 3 totaltotal 68.8% 342 / 497 CC 96 SCAR 33.0
CC
96
Cov
73%
restore
53.5
_chown
37.1
_restore_file
27.8
CRAP 27.1
« Brackup/Backup.pm ↑ Summary Brackup/Target.pm »
1 package Brackup::Restore;
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 Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use POSIX qw(mkfifo);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Fcntl qw(O_RDONLY O_CREAT O_WRONLY O_TRUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use String::Escape qw(unprintable);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Brackup::DecryptedFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Brackup::Decrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod undocumented
13 7 my ($class, %opts) = @_;
14 7 my $self = bless {}, $class;
15  
16 7 $self->{to} = delete $opts{to}; # directory we're restoring to
17 7 $self->{prefix} = delete $opts{prefix}; # directory/file filename prefix, or "" for all
18 7 $self->{filename}= delete $opts{file}; # filename we're restoring from
19 7 $self->{config} = delete $opts{config}; # brackup config (if available)
20 7 $self->{verbose} = delete $opts{verbose};
21  
22 7 $self->{_local_uid_map} = {}; # remote/metafile uid -> local uid
23 7 $self->{_local_gid_map} = {}; # remote/metafile gid -> local gid
24  
25 7 ▶ $self->{prefix} =~ s/\/$// if $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefix"} 4 3 7
26  
27 7 $self->{_stats_to_run} = []; # stack (push/pop) of subrefs to reset stat info on
28  
29 7 ▶ die "Destination directory doesn't exist" unless $self->{to} && -d $self->{to};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"to"} and -d $self->{"to"} 0 7 7
Condition33%1/3
$self->{"to"} and -d $self->{"to"}
!ll&&!rl&&r
007
MC/DC0%0/2
$self->{"to"} and -d $self->{"to"}
$self->{"to"} -d $self->{"to"}
Truth table33%1/3
A and B
A= $self->{"to"}B= -d $self->{"to"}
ABresult
0X0
100
111
30 7 ▶ croak("Unknown options: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 7 7
31  
32 7 $self->{metafile} = Brackup::DecryptedFile->new(filename => $self->{filename});
33  
34 7 return $self;
35 }
36  
37 # returns a hashref of { "foo" => "bar" } from { ..., "Driver-foo" => "bar" }
38 7 ▶sub _driver_meta {
Subroutine _driver_meta: called CC=3 SCAR=11.0
39 7 my $src = shift;
40 7 my $ret = {};
41 7 foreach my $k (keys %$src) {
42 91 ▶ next unless $k =~ /^Driver-(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^Driver-(.+)/ 77 14 91
43 14 $ret->{$1} = $src->{$k};
44 }
45 7 return $ret;
46 }
47  
48 7 ▶sub restore {
Subroutine restore: called CC=39 SCAR=39.8
Pod undocumented
49 7 my ($self) = @_;
50 7 my $parser = $self->parser;
51 7 my $meta = $parser->readline;
52 7 my $driver_class = $meta->{BackupDriver};
53 7 ▶ die "No driver specified" unless $driver_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $driver_class 0 7 7
54  
55 7 my $driver_meta = _driver_meta($meta);
56  
57 7 my $confsec;
58 7 ▶ if ($self->{config} && $meta->{TargetName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"} and $meta->{"TargetName"}) 0 7 7
Condition33%1/3
$self->{"config"} and $meta->{"TargetName"}
!ll&&!rl&&r
700
MC/DC0%0/2
$self->{"config"} and $meta->{"TargetName"}
$self->{"config"} $meta->{"TargetName"}
Truth table33%1/3
A and B
A= $self->{"config"}B= $meta->{"TargetName"}
ABresult
0X0
100
111
59 0 $confsec = eval { $self->{config}->get_section('TARGET:' . $meta->{TargetName}) };
60 }
61 # If no config section, use an empty one up with no keys to simplify Target handling
62 7 ▶ $confsec ||= Brackup::ConfigSection->new('fake');
Condition33%1/3
$confsec ||= "Brackup::ConfigSection"->new("fake")
l!l&&r!l&&!r
070
MC/DC0%0/2
$confsec ||= "Brackup::ConfigSection"->new("fake")
$confsec "Brackup::ConfigSection"->new("fake")
Truth table33%1/3
A ||= B
A= $confsecB= "Brackup::ConfigSection"->new("fake")
ABresult
1X1
011
000
63  
64 7 ▶ eval "use $driver_class; 1;" or die
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $driver_class; 1;" or die "Failed to load driver ($driver_class) to restore from: $@\n" 7 0 7
65 "Failed to load driver ($driver_class) to restore from: $@\n";
66 7 my $target = eval {"$driver_class"->new_from_backup_header($driver_meta, $confsec); };
67 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
68 0 die "Failed to instantiate target ($driver_class) for restore. Perhaps it doesn't support restoring yet?\n\nThe error was: $@";
69 }
70 7 $self->{_target} = $target;
71 7 $self->{_meta} = $meta;
72  
73 # handle absolute prefixes by stripping off RootPath to relativise
74 7 ▶ if ($self->{prefix} && $self->{prefix} =~ m/^\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prefix"} and $self->{"prefix"} =~ m[^/]) 0 7 7
Condition66%2/3
$self->{"prefix"} and $self->{"prefix"} =~ m[^/]
!ll&&!rl&&r
340
MC/DC0%0/2
$self->{"prefix"} and $self->{"prefix"} =~ m[^/]
$self->{"prefix"} $self->{"prefix"} =~ m[^/]
Truth table66%2/3
A and B
A= $self->{"prefix"}B= $self->{"prefix"} =~ m[^/]
ABresult
0X0
100
111
75 0 $self->{prefix} =~ s/^\Q$meta->{RootPath}\E\/?//;
76 }
77  
78 # we first process directories, then files sorted by their first chunk,
79 # then the rest. The file sorting allows us to avoid loading composite
80 # chunks and identical single chunk files multiple times from the target
81 # (see _restore_file)
82 7 my (@dirs, @files, @rest);
83 7 ▶ while (my $it = $parser->readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $it = $parser->readline) 100 7 107
84 100 ▶ my $type = $it->{Type} || 'f';
Condition100%2/2
$it->{'Type'} || 'f'
l!l
1981
MC/DC100%1/1
$it->{'Type'} || 'f'
$it->{'Type'}
Truth table100%2/2
A
A= $it->{'Type'}
Aresult
11
00
85 100 ▶ if($type eq 'f') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'f') { } 81 19 100
elsif ($type eq 'd') { } 19 0 19
86 # find dig of first chunk
87 81 ▶ ($it->{Chunks} || '') =~ /^(\S+)/;
Condition50%1/2
$it->{'Chunks'} || ''
l!l
810
MC/DC0%0/1
$it->{'Chunks'} || ''
$it->{'Chunks'}
Truth table50%1/2
A
A= $it->{'Chunks'}
Aresult
11
00
88 81 ▶ my ($offset, $len, $enc_len, $dig) = split(/;/, $1 || '');
Condition50%1/2
$1 || ''
l!l
810
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
89 81 ▶ $it->{fst_dig} = $dig || '';
Condition50%1/2
$dig || ''
l!l
810
MC/DC0%0/1
$dig || ''
$dig
Truth table50%1/2
A
A= $dig
Aresult
11
00
90 81 push @files, $it;
91 } elsif($type eq 'd') {
92 19 push @dirs, $it;
93 } else {
94 0 push @rest, $it;
95 }
96 }
97 7 @files = sort { $a->{fst_dig} cmp $b->{fst_dig} } @files;
98  
99 7 my $restore_count = 0;
100 7 for my $it (@dirs, @files, @rest) {
101 100 ▶ my $type = $it->{Type} || "f";
Condition100%2/2
$it->{'Type'} || 'f'
l!l
1981
MC/DC100%1/1
$it->{'Type'} || 'f'
$it->{'Type'}
Truth table100%2/2
A
A= $it->{'Type'}
Aresult
11
00
102 100 my $path = unprintable($it->{Path});
103 100 my $path_escaped = $it->{Path};
104 100 my $path_escaped_stripped = $it->{Path};
105 100 ▶ die "Unknown filetype: type=$type, file: $path_escaped" unless $type =~ /^[ldfp]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^[ldfp]$/ 0 100 100
106  
107 100 ▶ if ($self->{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prefix"}) 60 40 100
108 60 ▶ next unless $path =~ m/^\Q$self->{prefix}\E(?:\/|$)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^\Q$self->{"prefix"}\E(?:/|$)] 54 6 60
109 # if non-dir and $path eq $self->{prefix}, strip all but last component
110 6 ▶ if ($type ne 'd' && $path =~ m/^\Q$self->{prefix}\E\/?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne 'd' and $path =~ m[^\Q$self->{'prefix'}\E/?$]) { } 2 4 6
Condition100%3/3
$type ne "d" and $path =~ m[^\Q$self->{"prefix"}\E/?$]
!ll&&!rl&&r
222
MC/DC100%2/2
$type ne "d" and $path =~ m[^\Q$self->{"prefix"}\E/?$]
$type ne "d" $path =~ m[^\Q$self->{"prefix"}\E/?$]
Truth table100%3/3
A and B
A= $type ne "d"B= $path =~ m[^\Q$self->{"prefix"}\E/?$]
ABresult
0X0
100
111
111 2 ▶ if (my ($leading_prefix) = ($self->{prefix} =~ m/^(.*\/)[^\/]+\/?$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($leading_prefix) = $self->{"prefix"} =~ m[^(.*/)[^/]+/?$]) 1 1 2
112 1 $path =~ s/^\Q$leading_prefix\E//;
113 1 $path_escaped_stripped =~ s/^\Q$leading_prefix\E//;
114 }
115 }
116 else {
117 4 $path =~ s/^\Q$self->{prefix}\E\/?//;
118 4 $path_escaped_stripped =~ s/^\Q$self->{prefix}\E\/?//;
119 }
120 }
121  
122 46 $restore_count++;
123 46 my $full = $self->{to} . "/" . $path;
124 46 my $full_escaped = $self->{to} . "/" . $path_escaped_stripped;
125  
126 # restore default modes/user/group from header
127 46 ▶ $it->{Mode} ||= ($type eq 'd' ? $meta->{DefaultDirMode} : $meta->{DefaultFileMode});
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'd' ? : 9 32 41
Condition66%2/3
$it->{"Mode"} ||= $type eq "d" ? $meta->{"DefaultDirMode"} : $meta->{"DefaultFileMode"}
l!l&&r!l&&!r
5410
MC/DC0%0/2
$it->{"Mode"} ||= $type eq "d" ? $meta->{"DefaultDirMode"} : $meta->{"DefaultFileMode"}
$it->{"Mode"} $type eq "d" ? $meta->{"DefaultDirMode"} : $meta->{"DefaultFileMode"}
Truth table66%2/3
A ||= B
A= $it->{"Mode"}B= $type eq "d" ? $meta->{"DefaultDirMode"} : $meta->{"DefaultFileMode"}
ABresult
1X1
011
000
128 46 ▶ $it->{UID} ||= $meta->{DefaultUID};
Condition33%1/3
$it->{"UID"} ||= $meta->{"DefaultUID"}
l!l&&r!l&&!r
0460
MC/DC0%0/2
$it->{"UID"} ||= $meta->{"DefaultUID"}
$it->{"UID"} $meta->{"DefaultUID"}
Truth table33%1/3
A ||= B
A= $it->{"UID"}B= $meta->{"DefaultUID"}
ABresult
1X1
011
000
129 46 ▶ $it->{GID} ||= $meta->{DefaultGID};
Condition33%1/3
$it->{"GID"} ||= $meta->{"DefaultGID"}
l!l&&r!l&&!r
0460
MC/DC0%0/2
$it->{"GID"} ||= $meta->{"DefaultGID"}
$it->{"GID"} $meta->{"DefaultGID"}
Truth table33%1/3
A ||= B
A= $it->{"GID"}B= $meta->{"DefaultGID"}
ABresult
1X1
011
000
130  
131 46 ▶ warn " * restoring $path_escaped to $full_escaped\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 46 46
132 46 ▶ $self->_restore_link ($full, $it) if $type eq "l";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "l" 0 46 46
133 46 ▶ $self->_restore_directory($full, $it) if $type eq "d";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "d" 9 37 46
134 46 ▶ $self->_restore_fifo ($full, $it) if $type eq "p";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "p" 0 46 46
135 46 ▶ $self->_restore_file ($full, $it) if $type eq "f";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "f" 37 9 46
136  
137 46 ▶ $self->_chown($full, $it, $type, $meta) if $it->{UID} || $it->{GID};
Branch: 1 branch
BranchTrueFalseTotal
if $it->{"UID"} or $it->{"GID"} 46 0 46
Condition33%1/3
$it->{"UID"} or $it->{"GID"}
l!l&&r!l&&!r
4600
MC/DC0%0/2
$it->{"UID"} or $it->{"GID"}
$it->{"UID"} $it->{"GID"}
Truth table33%1/3
A or B
A= $it->{"UID"}B= $it->{"GID"}
ABresult
1X1
011
000
138 }
139  
140 # clear chunk cached by _restore_file
141 7 delete $self->{_cached_dig};
142 7 delete $self->{_cached_dataref};
143  
144 7 ▶ if ($restore_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restore_count) { } 6 1 7
145 6 ▶ warn " * fixing stat info\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 6 6
146 6 $self->_exec_statinfo_updates;
147 6 ▶ warn " * done\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 6 6
148 6 return 1;
149 } else {
150 1 ▶ die "nothing found matching '$self->{prefix}'.\n" if $self->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefix"} 1 0 1
151 0 die "nothing found to restore.\n";
152 }
153 }
154  
155 46 ▶sub _lookup_remote_uid {
Subroutine _lookup_remote_uid: called CC=5 SCAR=16.8
156 46 my ($self, $remote_uid, $meta) = @_;
157  
158 return $self->{_local_uid_map}->{$remote_uid}
159 46 ▶ if defined $self->{_local_uid_map}->{$remote_uid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_local_uid_map"}{$remote_uid} 40 6 46
160  
161 # meta remote user map - remote_uid => remote username
162 6 ▶ $self->{_remote_user_map} ||= { map { split /:/, $_, 2 } split /\s+/, $meta->{UIDMap} };
Condition50%1/2
$self->{"_remote_user_map"} ||= {map({split(/:/, $_, 2);} split(/\s+/, $meta->{"UIDMap"}, 0))}
l!l
06
MC/DC0%0/1
$self->{"_remote_user_map"} ||= {map({split(/:/, $_, 2);} split(/\s+/, $meta->{"UIDMap"}, 0))}
$self->{"_remote_user_map"}
Truth table50%1/2
A
A= $self->{"_remote_user_map"}
Aresult
11
00
163  
164 # try and lookup local uid using remote username
165 6 ▶ if (my $remote_user = $self->{_remote_user_map}->{$remote_uid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remote_user = $self->{"_remote_user_map"}{$remote_uid}) 6 0 6
166 6 my $local_uid = getpwnam($remote_user);
167 6 ▶ return $self->{_local_uid_map}->{$remote_uid} = $local_uid
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_uid 6 0 6
168 if defined $local_uid;
169 }
170  
171 # if remote username missing locally, fallback to $remote_uid
172 0 return $self->{_local_uid_map}->{$remote_uid} = $remote_uid;
173 }
174  
175 46 ▶sub _lookup_remote_gid {
Subroutine _lookup_remote_gid: called CC=5 SCAR=16.8
176 46 my ($self, $remote_gid, $meta) = @_;
177  
178 return $self->{_local_gid_map}->{$remote_gid}
179 46 ▶ if defined $self->{_local_gid_map}->{$remote_gid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_local_gid_map"}{$remote_gid} 40 6 46
180  
181 # meta remote group map - remote_gid => remote group
182 6 ▶ $self->{_remote_group_map} ||= { map { split /:/, $_, 2 } split /\s+/, $meta->{GIDMap} };
Condition50%1/2
$self->{"_remote_group_map"} ||= {map({split(/:/, $_, 2);} split(/\s+/, $meta->{"GIDMap"}, 0))}
l!l
06
MC/DC0%0/1
$self->{"_remote_group_map"} ||= {map({split(/:/, $_, 2);} split(/\s+/, $meta->{"GIDMap"}, 0))}
$self->{"_remote_group_map"}
Truth table50%1/2
A
A= $self->{"_remote_group_map"}
Aresult
11
00
183  
184 # try and lookup local gid using remote group
185 6 ▶ if (my $remote_group = $self->{_remote_group_map}->{$remote_gid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $remote_group = $self->{"_remote_group_map"}{$remote_gid}) 6 0 6
186 6 my $local_gid = getgrnam($remote_group);
187 6 ▶ return $self->{_local_gid_map}->{$remote_gid} = $local_gid
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_gid 6 0 6
188 if defined $local_gid;
189 }
190  
191 # if remote group missing locally, fallback to $remote_gid
192 0 return $self->{_local_gid_map}->{$remote_gid} = $remote_gid;
193 }
194  
195 46 ▶sub _chown {
Subroutine _chown: called CC=11 SCAR=36.1
196 46 my ($self, $full, $it, $type, $meta) = @_;
197  
198 46 ▶ my $uid = $self->_lookup_remote_uid($it->{UID}, $meta) if $it->{UID};
Branch: 1 branch
BranchTrueFalseTotal
if $it->{"UID"} 46 0 46
199 46 ▶ my $gid = $self->_lookup_remote_gid($it->{GID}, $meta) if $it->{GID};
Branch: 1 branch
BranchTrueFalseTotal
if $it->{"GID"} 46 0 46
200  
201 46 ▶ if ($type eq 'l') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'l') { } 0 46 46
202 0 ▶ if (! defined $self->{_lchown}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_lchown"}) 0 0 0
203 13 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
204 0 ▶ $self->{_lchown} = eval { require Lchown } && Lchown::LCHOWN_AVAILABLE;
Condition0%0/2
eval { do { require Lchown } } && 'Lchown::LCHOWN_AVAILABLE'
!ll
00
MC/DC0%0/1
eval { do { require Lchown } } && 'Lchown::LCHOWN_AVAILABLE'
eval { do { require Lchown } }
Truth table0%0/2
A
A= eval { do { require Lchown } }
Aresult
00
11
205 }
206 0 ▶ if ($self->{_lchown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_lchown"}) 0 0 0
207 0 ▶ Lchown::lchown($uid, -1, $full) if defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uid 0 0 0
208 0 ▶ Lchown::lchown(-1, $gid, $full) if defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $gid 0 0 0
209 }
210 } else {
211 # ignore errors, but change uid and gid separately to sidestep unprivileged failures
212 46 ▶ chown $uid, -1, $full if defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uid 46 0 46
213 46 ▶ chown -1, $gid, $full if defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $gid 46 0 46
214 }
215 }
216  
217 46 ▶sub _update_statinfo {
Subroutine _update_statinfo: called CC=1 SCAR=0.7
218 46 my ($self, $full, $it) = @_;
219  
220 46 ▶ push @{ $self->{_stats_to_run} }, sub {
Subroutine __ANON__: called CC=8 SCAR=26.4
221 46 ▶ if (defined $it->{Mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $it->{"Mode"}) 46 0 46
222 46 ▶ chmod(oct $it->{Mode}, $full) or
Branch: 1 branch
BranchTrueFalseTotal
chmod oct $it->{"Mode"}, $full or die "Failed to change mode of ${full}: $!" 46 0 46
223 die "Failed to change mode of $full: $!";
224 }
225  
226 46 ▶ if ($it->{Mtime} || $it->{Atime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($it->{"Mtime"} or $it->{"Atime"}) 46 0 46
Condition33%1/3
$it->{"Mtime"} or $it->{"Atime"}
l!l&&r!l&&!r
4600
MC/DC0%0/2
$it->{"Mtime"} or $it->{"Atime"}
$it->{"Mtime"} $it->{"Atime"}
Truth table33%1/3
A or B
A= $it->{"Mtime"}B= $it->{"Atime"}
ABresult
1X1
011
000
227 utime($it->{Atime} || $it->{Mtime},
228 $it->{Mtime} || $it->{Atime},
229 46 ▶ $full) or
Branch: 1 branch
BranchTrueFalseTotal
utime $it->{"Atime"} || $it->{"Mtime"}, $it->{"Mtime"} || $it->{"Atime"}, $full or die "Failed to change utime of ${full}: $!" 46 0 46
Condition33%2/6
$it->{'Atime'} || $it->{'Mtime'}
l!l&&r!l&&!r
4600
$it->{'Mtime'} || $it->{'Atime'}
l!l&&r!l&&!r
4600
MC/DC0%0/2
$it->{'Atime'} || $it->{'Mtime'}
$it->{'Atime'} $it->{'Mtime'}
MC/DC0%0/2
$it->{'Mtime'} || $it->{'Atime'}
$it->{'Mtime'} $it->{'Atime'}
Truth table33%1/3
A || B
A= $it->{'Atime'}B= $it->{'Mtime'}
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $it->{'Mtime'}B= $it->{'Atime'}
ABresult
1X1
011
000
230 die "Failed to change utime of $full: $!";
231 }
232 46 };
233 }
234  
235 6 ▶sub _exec_statinfo_updates {
Subroutine _exec_statinfo_updates: called CC=2 SCAR=6.9
236 6 my $self = shift;
237  
238 # change the modes/times in backwards order, going from deep
239 # files/directories to shallow ones. (so we can reliably change
240 # all the directory mtimes without kernel doing it for us when we
241 # modify files deeper)
242 6 ▶ while (my $sb = pop @{ $self->{_stats_to_run} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sb = pop @{$$self{"_stats_to_run"};}) 46 6 52
243 46 $sb->();
244 }
245 }
246  
247 9 ▶sub _restore_directory {
Subroutine _restore_directory: called CC=3 SCAR=11.0
248 9 my ($self, $full, $it) = @_;
249  
250 9 ▶ unless (-d $full) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $full) 5 4 9
251 5 ▶ mkdir $full or # FIXME: permissions on directory
Branch: 1 branch
BranchTrueFalseTotal
mkdir $full or die qq[Failed to make directory: $full ($it->{"Path"})] 5 0 5
252 die "Failed to make directory: $full ($it->{Path})";
253 }
254  
255 9 $self->_update_statinfo($full, $it);
256 }
257  
258 0 ▶sub _restore_link {
Subroutine _restore_link: not called CC=3 SCAR=24.8
259 0 my ($self, $full, $it) = @_;
260  
261 0 ▶ if (-e $full) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full) 0 0 0
262 # TODO: add --conflict={skip,overwrite} option, defaulting to nothing: which dies
263 0 die "Link $full ($it->{Path}) already exists. Aborting.";
264 }
265 0 ▶ symlink $it->{Link}, $full
Branch: 1 branch
BranchTrueFalseTotal
symlink $it->{"Link"}, $full or die "Failed to link" 0 0 0
266 or die "Failed to link";
267 }
268  
269 0 ▶sub _restore_fifo {
Subroutine _restore_fifo: not called CC=3 SCAR=24.8
270 0 my ($self, $full, $it) = @_;
271  
272 0 ▶ if (-e $full) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full) 0 0 0
273 0 die "Named pipe/fifo $full ($it->{Path}) already exists. Aborting.";
274 }
275  
276 0 ▶ mkfifo($full, $it->{Mode}) or die "mkfifo failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkfifo($full, $it->{"Mode"}) or die "mkfifo failed: $!" 0 0 0
277  
278 0 $self->_update_statinfo($full, $it);
279 }
280  
281 37 ▶sub _restore_file {
Subroutine _restore_file: called CC=20 SCAR=33.3
282 37 my ($self, $full, $it) = @_;
283  
284 37 ▶ if (-e $full && -s $full) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full and -s $full) 0 37 37
Condition33%1/3
-e $full and -s $full
!ll&&!rl&&r
3700
MC/DC0%0/2
-e $full and -s $full
-e $full -s $full
Truth table33%1/3
A and B
A= -e $fullB= -s $full
ABresult
0X0
100
111
285 # TODO: add --conflict={skip,overwrite} option, defaulting to nothing: which dies
286 0 die "File $full ($it->{Path}) already exists. Aborting.";
287 }
288  
289 37 ▶ sysopen(my $fh, $full, O_CREAT|O_WRONLY|O_TRUNC) or die "Failed to open '$full' for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $full, 577 or die "Failed to open '${full}' for writing: $!" 37 0 37
290 37 binmode($fh);
291 37 ▶ my @chunks = grep { $_ } split(/\s+/, $it->{Chunks} || "");
Condition50%1/2
$it->{'Chunks'} || ''
l!l
370
MC/DC0%0/1
$it->{'Chunks'} || ''
$it->{'Chunks'}
Truth table50%1/2
A
A= $it->{'Chunks'}
Aresult
11
00
292 37 foreach my $ch (@chunks) {
293 45 my ($offset, $len, $enc_len, $dig) = split(/;/, $ch);
294  
295 # we process files sorted by the dig of their first chunk, caching
296 # the last seen chunk to avoid loading composite chunks multiple
297 # times (all files included in composite chunks are single-chunk
298 # files, by definition). Even for non-composite chunks there is a
299 # speedup if we have single-chunk identical files.
300 45 my $dataref;
301 45 ▶ if($dig eq ($self->{_cached_dig} || '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dig eq ($self->{'_cached_dig'} || '')) { } 10 35 45
Condition100%2/2
$self->{'_cached_dig'} || ''
l!l
396
MC/DC100%1/1
$self->{'_cached_dig'} || ''
$self->{'_cached_dig'}
Truth table100%2/2
A
A= $self->{'_cached_dig'}
Aresult
11
00
302 10 ▶ warn " ** using cached chunk $dig\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 10 10
303 10 $dataref = $self->{_cached_dataref};
304 } else {
305 35 ▶ warn " ** loading chunk $dig from target\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 35 35
306 35 ▶ $dataref = $self->{_target}->load_chunk($dig) or
Branch: 1 branch
BranchTrueFalseTotal
$dataref = $self->{"_target"}->load_chunk($dig) or die "Error loading chunk $dig from the restore target\n" 35 0 35
307 die "Error loading chunk $dig from the restore target\n";
308 35 $self->{_cached_dig} = $dig;
309 35 $self->{_cached_dataref} = $dataref;
310 }
311  
312 45 my $len_chunk = length $$dataref;
313  
314 # using just a range of the file
315 45 ▶ if ($enc_len =~ /^(\d+)-(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc_len =~ /^(\d+)-(\d+)$/) { } 7 38 45
316 7 my ($from, $to) = ($1, $2);
317 # file range. gotta be at least as big as bigger number
318 7 ▶ unless ($len_chunk >= $to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len_chunk >= $to) 0 7 7
319 0 die "Backup chunk $dig isn't at least as big as range: got $len_chunk, needing $to\n";
320 }
321 7 my $region = substr($$dataref, $from, $to-$from);
322 7 $dataref = \$region;
323 } else {
324 # using the whole chunk, so make sure fetched size matches
325 # expected size
326 38 ▶ unless ($len_chunk == $enc_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len_chunk == $enc_len) 0 38 38
327 0 die "Backup chunk $dig isn't of expected length: got $len_chunk, expecting $enc_len\n";
328 }
329 }
330  
331 45 my $decrypted_ref = Brackup::Decrypt::decrypt_data($dataref, meta => $self->{_meta});
332 45 print $fh $$decrypted_ref;
333 }
334 37 ▶ close($fh) or die "Close failed";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Close failed" 37 0 37
335  
336 37 ▶ if (my $good_dig = $it->{Digest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $good_dig = $it->{"Digest"}) 37 0 37
337 37 ▶ die "not capable of verifying digests of from anything but sha1"
Branch: 1 branch
BranchTrueFalseTotal
unless $good_dig =~ /^sha1:(.+)/ 0 37 37
338 unless $good_dig =~ /^sha1:(.+)/;
339 37 $good_dig = $1;
340  
341 37 ▶ sysopen(my $readfh, $full, O_RDONLY) or die "Failed to reopen '$full' for verification: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $readfh, $full, 0 or die "Failed to reopen '${full}' for verification: $!" 37 0 37
342 37 binmode($readfh);
343 37 my $sha1 = Digest::SHA1->new;
344 37 $sha1->addfile($readfh);
345 37 my $actual_dig = $sha1->hexdigest;
346  
347 # TODO: support --onerror={continue,prompt}, etc, but for now we just die
348 37 ▶ unless ($actual_dig eq $good_dig || $full =~ m!\.brackup-digest\.db\b!) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual_dig eq $good_dig or $full =~ /\.brackup-digest\.db\b/) 0 37 37
Condition33%1/3
$actual_dig eq $good_dig or $full =~ /\.brackup-digest\.db\b/
l!l&&r!l&&!r
3700
MC/DC0%0/2
$actual_dig eq $good_dig or $full =~ /\.brackup-digest\.db\b/
$actual_dig eq $good_dig $full =~ /\.brackup-digest\.db\b/
Truth table33%1/3
A or B
A= $actual_dig eq $good_digB= $full =~ /\.brackup-digest\.db\b/
ABresult
1X1
011
000
349 0 die "Digest of restored file ($full) doesn't match";
350 }
351 }
352  
353 37 $self->_update_statinfo($full, $it);
354 }
355  
356 # returns iterator subref which returns hashrefs or undef on EOF
357 7 ▶sub parser {
Subroutine parser: called CC=1 SCAR=0.0
Pod undocumented
358 7 my $self = shift;
359 7 return Brackup::Metafile->open($self->{metafile}->name);
360 }
361  
362 13 1;
363  
« Brackup/Backup.pm ↑ Summary Brackup/Target.pm »