Brackup/Target/Filesystem.pm

filter: Statementstmt 61.4% 142 / 231 Branchbran 38.1% 48 / 126 Conditioncond 18.1% 4 / 22 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 80.0% 24 / 30 Podpod 0.0% 0 / 17 totaltotal 49.7% 219 / 440 CC 74 SCAR 37.9
CC
74
Cov
51%
_upgrade_layout
210.0
_upgrade_chunks_in_directory
90.0
__ANON__
42.0
CRAP 44.4
« Brackup/Decrypt.pm ↑ Summary Brackup/Chunker/MP3.pm »
1 package Brackup::Target::Filesystem;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use base 'Brackup::Target::Filebased';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Brackup::Util qw(io_print_to_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 4 ▶sub new {
Subroutine new: called CC=6 SCAR=18.9
Pod undocumented
13 4 my ($class, $confsec) = @_;
14 4 my $self = $class->SUPER::new($confsec);
15  
16 4 $self->{path} = $confsec->path_value("path");
17 4 $self->{nocolons} = $confsec->value("no_filename_colons");
18  
19 # LAME: Make it work on Windows
20 4 ▶ $self->{nocolons} = ($^O eq 'MSWin32') unless defined $self->{nocolons};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"nocolons"} 4 0 4
21  
22 # see if we're operating in a pre-1.06 environment
23 4 ▶ if (opendir(my $dh, $self->{path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $self->{"path"}) 4 0 4
24 4 $self->{_no_four_hex_dirs_in_root} = 1;
25 4 ▶ while (my $file = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 8 4 12
26 8 ▶ if ($file =~ /^[0-9a-f]{4}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^[0-9a-f]{4}$/) 0 8 8
27 0 $self->{_no_four_hex_dirs_in_root} = 0;
28 }
29 }
30 }
31  
32 4 ▶ if ($ENV{BRACKUP_REARRANGE_FS_TARGET}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"BRACKUP_REARRANGE_FS_TARGET"}) 0 4 4
33 0 $self->_upgrade_layout;
34 }
35  
36 4 return $self;
37 }
38  
39 7 ▶sub new_from_backup_header {
Subroutine new_from_backup_header: called CC=4 SCAR=15.0
Pod undocumented
40 7 my ($class, $header) = @_;
41 7 my $self = bless {}, $class;
42 7 ▶ $self->{path} = $header->{"BackupPath"} or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"path"} = $header->{"BackupPath"} or die "No BackupPath specified in the backup metafile.\n" 7 0 7
43 die "No BackupPath specified in the backup metafile.\n";
44 7 ▶ $self->{nocolons} = $header->{"NoColons"} or 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"nocolons"} = $header->{"NoColons"} or '???' 0 7 7
45 7 ▶ unless (-d $self->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"path"}) 0 7 7
46 0 die "Restore path $self->{path} doesn't exist.\n";
47 }
48 7 return $self;
49 }
50  
51 245 ▶sub nocolons {
Subroutine nocolons: called CC=1 SCAR=0.0
Pod undocumented
52 245 my ($self) = @_;
53 245 return $self->{nocolons};
54 }
55  
56 5 ▶sub backup_header {
Subroutine backup_header: called CC=2 SCAR=7.2
Pod undocumented
57 5 my $self = shift;
58 return {
59 "BackupPath" => $self->{path},
60 5 ▶ "NoColons" => $self->{nocolons}?"1":"0",
Branch: 1 branch
BranchTrueFalseTotal
$self->{'nocolons'} ? : 0 5 5
61 };
62 }
63  
64 # 1.05 and before stored files on disk as: xxxx/xxxx/xxxxxxxxxx.brackup
65 # (that is, two levels of directories, each 4 hex digits long, or 65536
66 # files per directory, which is 2x what ext3 can store, leading to errors.
67 # in 1.06 and above, xx/xx/xxxxxx is used. that is, two levels of 2 hex
68 # digits. this function
69 0 ▶sub _upgrade_layout {
Subroutine _upgrade_layout: not called CC=14 SCAR=53.5
70 0 my $self = shift;
71 0 my $clean_limit = shift; # optional; if set, max top-level dirs to clean
72  
73 0 my $root = $self->{path};
74  
75 0 ▶ opendir(my $dh, $root) or die "Error opening $root: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $root or die "Error opening ${root}: $!" 0 0 0
76  
77 # read the current state of things in the root directory
78 # (which is presumably maxed out on files, at 32k or whatnot)
79 0 my %exist_twodir; # two_dir -> 1 (which two-letter directories exist)
80 my %exist_fourdir; # four_dir -> 1 (which four-letter directories exist)
81 0 my %four_of_two; # two_dir -> [ four_dir, four_dir, ... ]
82 0 ▶ while (my $dir = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $dir = readdir $dh)) 0 0 0
83 0 ▶ next unless -d "$root/$dir";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$root/$dir" 0 0 0
84 0 ▶ if ($dir =~ /^[0-9a-f]{2}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /^[0-9a-f]{2}$/) 0 0 0
85 0 $exist_twodir{$dir} = 1;
86 0 next;
87 }
88 0 ▶ if ($dir =~ /^([0-9a-f]{2})([0-9a-f]{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /^([0-9a-f]{2})([0-9a-f]{2})$/) 0 0 0
89 0 $exist_fourdir{"$1$2"} = 1;
90 0 ▶ push @{ $four_of_two{$1} ||= [] }, "$1$2";
Condition0%0/2
$four_of_two{$1} ||= []
l!l
00
MC/DC0%0/1
$four_of_two{$1} ||= []
$four_of_two{$1}
Truth table0%0/2
A
A= $four_of_two{$1}
Aresult
11
00
91 }
92 }
93  
94 # for each 4-digit directory, sorted by number of four-digit directories
95 # that exist for their leading 2-digit prefix (to most quickly free up
96 # a link in root, in 2 iterations),
97 # see if the "01/" directory exists (the leading two bytes).
98 # if not,
99 # move it to some random other 'xxxx' directory,
100 # as, say, "abcd/tmp-was-root-0123".
101 # now, for either the "0123" directory or "tmp-was-root-0123"
102 # directory, file all chunks, and move them to the
103 # right locations "01/23/*.chunk", making "01/23" if needed.
104 # (shouldn't be any out-of-link problems down one level)
105 my @four_dirs = map {
106 0 sort @{ $four_of_two{$_} }
107 }
108 sort {
109 0 scalar(@{ $four_of_two{$b} }) <=> scalar(@{ $four_of_two{$a} })
110 } keys %four_of_two;
111 0 my $n_done;
112 0 ▶ while (my $four_dir = shift @four_dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $four_dir = shift @four_dirs) 0 0 0
113 0 my $leading_two = substr($four_dir, 0, 2);
114 0 my $migrate_source;
115 0 ▶ if ($exist_twodir{$leading_two}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exist_twodir{$leading_two}) { } 0 0 0
elsif (@four_dirs) { } 0 0 0
116 # top-level destination already exists. no need for more
117 # links in the top-level
118 0 $migrate_source = $four_dir;
119 } elsif (@four_dirs) {
120 # we need to move four_dir away, into another four_dir,
121 # to make room to create a new two_dir in the root
122 0 my $holder_four_dir = $four_dirs[0];
123 0 $migrate_source = "$holder_four_dir/tmp-was-root-$four_dir";
124 0 my $temp_dir = "$root/$migrate_source";
125 0 ▶ rename "$root/$four_dir", $temp_dir
Branch: 1 branch
BranchTrueFalseTotal
rename "$root/$four_dir", $temp_dir or die "Rename of $root/$four_dir -> $temp_dir failed: $!" 0 0 0
126 or die "Rename of $root/$four_dir -> $temp_dir failed: $!";
127 } else {
128 # no four_dirs left? then I bet we aren't out of links
129 # anymore. just migrate.
130 0 $migrate_source = $four_dir;
131 }
132  
133 0 $self->_upgrade_chunks_in_directory($four_dir, $migrate_source);
134 0 ▶ if (-e "$root/$four_dir") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$root/$four_dir") 0 0 0
135 0 die "Upgrade of $root/$four_dir/* didn't seem to have worked.";
136 }
137 0 $n_done++;
138 0 ▶ last if $clean_limit && $n_done >= $clean_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $clean_limit and $n_done >= $clean_limit 0 0 0
Condition0%0/3
$clean_limit and $n_done >= $clean_limit
!ll&&!rl&&r
000
MC/DC0%0/2
$clean_limit and $n_done >= $clean_limit
$clean_limit $n_done >= $clean_limit
Truth table0%0/3
A and B
A= $clean_limitB= $n_done >= $clean_limit
ABresult
0X0
100
111
139 }
140 }
141  
142 0 ▶sub _upgrade_chunks_in_directory {
Subroutine _upgrade_chunks_in_directory: not called CC=9 SCAR=45.0
143 0 my $self = shift;
144 0 my $four_dig = shift; # first four hex digits of all files being moved
145 0 my $rel_dir = shift; # directory (relative to root) to move files from, and then remove
146 0 ▶ die "not relative" unless $rel_dir =~ m!^[^/]!;
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_dir =~ m[^[^/]] 0 0 0
147  
148 0 my $root = $self->{path};
149  
150 0 ▶ my ($hex12, $hex34) = $four_dig =~ /^([0-9a-f]{2})([0-9a-f]{2})$/
Branch: 1 branch
BranchTrueFalseTotal
() = $four_dig =~ /^([0-9a-f]{2})([0-9a-f]{2})$/ or die "four_dig not four hex digits" 0 0 0
151 or die "four_dig not four hex digits";
152  
153 0 my $dest_dir0 = "$root/$hex12";
154 0 my $dest_dir = "$root/$hex12/$hex34";
155 0 for ($dest_dir0, $dest_dir) {
156 0 ▶ next if -d $_;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
157 0 ▶ mkdir $_ or die "Failed to mkdir $_: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $_ or die "Failed to mkdir ${_}: $!" 0 0 0
158 }
159  
160 0 my @dirs;
161 0 ▶ File::Find::find({wanted => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
162 0 my $name = $File::Find::name;
163 0 ▶ if (-f $name) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $name) { } 0 0 0
elsif (-d $name) { } 0 0 0
164 0 my $basefile = $_; # stupid File::Find conventions
165 0 ▶ rename $name, "$dest_dir/$basefile" or die
Branch: 1 branch
BranchTrueFalseTotal
rename $name, "$dest_dir/$basefile" or die "Failed to move $name to ${dest_dir}: $!" 0 0 0
166 "Failed to move $name to $dest_dir: $!";
167 } elsif (-d $name) {
168 0 ▶ return if $_ eq "." || $_ eq "..";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "." or $_ eq ".." 0 0 0
Condition0%0/3
$_ eq "." or $_ eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq "." or $_ eq ".."
$_ eq "." $_ eq ".."
Truth table0%0/3
A or B
A= $_ eq "."B= $_ eq ".."
ABresult
1X1
011
000
169 0 push @dirs, $name;
170 }
171 0 }}, "$root/$rel_dir");
172  
173 0 my $final_dir = "$root/$four_dig";
174 0 for my $dir (reverse(@dirs), $final_dir) {
175 0 ▶ if (!rmdir($dir) && -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (not rmdir $dir and -d $dir) 0 0 0
Condition0%0/3
not rmdir $dir and -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
not rmdir $dir and -d $dir
not rmdir $dir -d $dir
Truth table0%0/3
not(A) and B
A= not rmdir $dirB= -d $dir
ABresult
0X0
100
111
176 0 warn "Directory not empty? $dir. Skipping cleanup.\n";
177 0 return;
178 }
179 }
180 0 warn "Rearranged & removed $four_dig\n";
181 }
182  
183 # version <= 1.05: 0123/4567/89ab/cdef/0123456789abcdef...xxx.chunk
184 # this is totally stupid. 65k files in root (twice ext3's historical/common
185 # maximum), and the leaves were always containing but one file.
186 35 ▶sub _old_diskpath {
Subroutine _old_diskpath: called CC=5 SCAR=16.3
187 35 my ($self, $dig) = @_;
188 35 my @parts;
189 35 my $fulldig = $dig;
190 35 $dig =~ s/^\w+://; # remove the "hashtype:" from beginning
191 35 ▶ $fulldig =~ s/:/./g if $self->nocolons; # Convert colons to dots if we've been asked to
Branch: 1 branch
BranchTrueFalseTotal
if $self->nocolons 0 35 35
192 35 ▶ while (length $dig && @parts < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dig and @parts < 4) 140 35 175
Condition66%2/3
length $dig and @parts < 4
!ll&&!rl&&r
035140
MC/DC50%1/2
length $dig and @parts < 4
length $dig @parts < 4
Truth table66%2/3
A and B
A= length $digB= @parts < 4
ABresult
0X0
100
111
193 140 ▶ $dig =~ s/^([0-9a-f]{4})// or die "Can't get 4 hex digits of $fulldig";
Branch: 1 branch
BranchTrueFalseTotal
$dig =~ s/^([0-9a-f]{4})// or die "Can't get 4 hex digits of $fulldig" 140 0 140
194 140 push @parts, $1;
195 }
196 35 return $self->{path} . "/" . join("/", @parts) . "/$fulldig.chunk";
197 }
198  
199 174 ▶sub chunkpath {
Subroutine chunkpath: called CC=3 SCAR=11.0
Pod undocumented
200 174 my ($self, $dig) = @_;
201  
202 # if the old (version <= 1.05) chunk still exists,
203 # just use that, unless we know (from initial scan)
204 # that such paths can't exist, thus avoiding a
205 # bunch of stats()
206 174 ▶ unless ($self->{_no_four_hex_dirs_in_root}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_no_four_hex_dirs_in_root"}) 35 139 174
207 35 my $old = $self->_old_diskpath($dig);
208 35 ▶ return $old if -e $old;
Branch: 1 branch
BranchTrueFalseTotal
if -e $old 0 35 35
209 }
210  
211 # else, use the new (version >= 1.06) location, which
212 # is much more sensible
213 174 return $self->{path} . '/' . $self->SUPER::chunkpath($dig);
214 }
215  
216 13 ▶sub metapath {
Subroutine metapath: called CC=1 SCAR=0.0
Pod undocumented
217 13 my ($self, $name) = @_;
218 13 return $self->{path} . '/' . $self->SUPER::metapath($name);
219 }
220  
221 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
222 0 my ($self, $path) = @_;
223 0 return -s $path;
224 }
225  
226 0 ▶sub has_chunk_of_handle {
Subroutine has_chunk_of_handle: not called CC=1 SCAR=6.9
Pod undocumented
227 0 my ($self, $handle) = @_;
228 0 my $dig = $handle->digest; # "sha1:sdfsdf" format scalar
229 0 my $path = $self->chunkpath($dig);
230 0 return -e $path;
231 }
232  
233 109 ▶sub load_chunk {
Subroutine load_chunk: called CC=2 SCAR=7.0
Pod undocumented
234 109 my ($self, $dig) = @_;
235 109 my $path = $self->chunkpath($dig);
236 109 ▶ open (my $fh, $path) or die "Error opening $path to load chunk: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $path or die "Error opening $path to load chunk: $!" 109 0 109
237 109 my $chunk = do { local $/; <$fh>; };
238 109 return \$chunk;
239 }
240  
241 0 ▶sub has_chunk {
Subroutine has_chunk: not called CC=3 SCAR=24.8
Pod undocumented
242 0 my ($self, $chunk) = @_;
243 0 my $dig = $chunk->backup_digest;
244 0 my $blen = $chunk->backup_length;
245 0 my $path = $self->chunkpath($dig);
246 0 my $exist_size = -s $path;
247 0 ▶ if ($exist_size && $exist_size == $blen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exist_size and $exist_size == $blen) 0 0 0
Condition0%0/3
$exist_size and $exist_size == $blen
!ll&&!rl&&r
000
MC/DC0%0/2
$exist_size and $exist_size == $blen
$exist_size $exist_size == $blen
Truth table0%0/3
A and B
A= $exist_sizeB= $exist_size == $blen
ABresult
0X0
100
111
248 0 return 1;
249 }
250 0 return 0;
251 }
252  
253 53 ▶sub store_chunk {
Subroutine store_chunk: called CC=12 SCAR=31.8
Pod undocumented
254 53 my ($self, $chunk) = @_;
255 53 my $dig = $chunk->backup_digest;
256 53 my $blen = $chunk->backup_length;
257  
258 53 my $path = $self->chunkpath($dig);
259  
260 # is it already there? then do nothing.
261 53 my $exist_size = -s $path;
262 53 ▶ if ($exist_size && $exist_size == $blen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exist_size and $exist_size == $blen) 0 53 53
Condition33%1/3
$exist_size and $exist_size == $blen
!ll&&!rl&&r
5300
MC/DC0%0/2
$exist_size and $exist_size == $blen
$exist_size $exist_size == $blen
Truth table33%1/3
A and B
A= $exist_sizeB= $exist_size == $blen
ABresult
0X0
100
111
263 0 return 1;
264 }
265  
266 53 my $dir = dirname($path);
267  
268 53 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 53 0 53
269 53 ▶ unless (eval { File::Path::mkpath($dir) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { File::Path::mkpath($dir) } }) 0 53 53
270 0 ▶ if ($!{EMLINK}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EMLINK'}) { } 0 0 0
271 0 warn "Too many directories in one directory; doing partial cleanup before proceeding...\n";
272 # NOTE: 2 directories is key to freeing up one link. imagine upgrading one:
273 # it'd remove "0000" but possibly (likely) create "00". so we do two,
274 # because, following the example, "0001" would also go into "00", so we'd have one
275 # link left in the root. _upgrade_layout orders the directories to clean in
276 # an order such that 2 will succeed or fail, but no higher will succeed when
277 # 2 won't.
278 0 $self->_upgrade_layout(2);
279 0 ▶ unless (eval { File::Path::mkpath($dir) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { File::Path::mkpath($dir) } }) 0 0 0
280 0 die "Still can't create directory $dir: $!\n";
281 }
282 } else {
283 0 die "Failed to mkdir: $dir: $!\n";
284 }
285 }
286 }
287  
288 53 my $partial = "$path.partial";
289 53 ▶ open (my $fh, '>', $partial) or die "Failed to open $partial for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $partial or die "Failed to open $partial for writing: $!\n" 53 0 53
290 53 binmode($fh);
291 53 io_print_to_fh($chunk->chunkref, $fh);
292 53 ▶ close($fh) or die "Failed to close $path\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close $path\n" 53 0 53
293  
294 53 unlink $path;
295 53 ▶ rename $partial, $path or die "Failed to rename $partial to $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $partial, $path or die "Failed to rename $partial to ${path}: $!\n" 53 0 53
296  
297 53 my $actual_size = -s $path;
298 53 my $expected_size = $chunk->backup_length;
299 53 ▶ unless (defined($actual_size)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $actual_size) 0 53 53
300 0 die "Chunk output file $path does not exist. Do you need to set no_filename_colons=1?";
301 }
302 53 ▶ unless ($actual_size == $expected_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actual_size == $expected_size) 0 53 53
303 0 die "Chunk $path was written to disk wrong: size is $actual_size, expecting $expected_size\n";
304 }
305  
306 53 return 1;
307 }
308  
309 12 ▶sub delete_chunk {
Subroutine delete_chunk: called CC=1 SCAR=0.0
Pod undocumented
310 12 my ($self, $dig) = @_;
311 12 my $path = $self->chunkpath($dig);
312 12 unlink $path;
313 }
314  
315  
316 # returns a list of names of all chunks
317 2 ▶sub chunks {
Subroutine chunks: called CC=1 SCAR=0.0
Pod undocumented
318 2 my $self = shift;
319  
320 2 my @chunks = ();
321 118 ▶ my $found_chunk = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
322 118 ▶ m/\.chunk$/ or return;
Branch: 1 branch
BranchTrueFalseTotal
/\.chunk$/ or return 36 82 118
323 36 my $chunk_name = basename($_);
324 36 $chunk_name =~ s/\.chunk$//;
325 36 ▶ $chunk_name =~ s/\./:/g if $self->nocolons;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nocolons 0 36 36
326 36 push @chunks, $chunk_name;
327 2 };
328 2 File::Find::find({ wanted => $found_chunk, no_chdir => 1}, $self->{path});
329 2 return @chunks;
330 }
331  
332 5 ▶sub store_backup_meta {
Subroutine store_backup_meta: called CC=5 SCAR=16.4
Pod undocumented
333 5 my ($self, $name, $fh) = @_;
334  
335 5 my $dir = $self->metapath();
336 5 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 4 1 5
337 4 ▶ mkdir $dir or die "Failed to mkdir $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "Failed to mkdir ${dir}: $!\n" 4 0 4
338 }
339  
340 5 my $out_filepath = "$dir/$name.brackup";
341 5 ▶ open (my $out_fh, '>', $out_filepath)
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", $out_filepath or die "Failed to open metafile '${out_filepath}': $!\n" 5 0 5
342 or die "Failed to open metafile '$out_filepath': $!\n";
343 5 io_print_to_fh($fh, $out_fh);
344 5 ▶ close $out_fh or die "Failed to close metafile '$out_filepath': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or die "Failed to close metafile '${out_filepath}': $!\n" 5 0 5
345  
346 5 return 1;
347 }
348  
349 5 ▶sub backups {
Subroutine backups: called CC=5 SCAR=16.2
Pod undocumented
350 5 my ($self) = @_;
351  
352 5 my $dir = $self->metapath();
353 5 ▶ return () unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 5 5
354  
355 5 ▶ opendir(my $dh, $dir) or
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Failed to open ${dir}: $!\n" 5 0 5
356 die "Failed to open $dir: $!\n";
357  
358 5 my @ret = ();
359 5 ▶ while (my $fn = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 17 5 22
360 17 ▶ next unless $fn =~ s/\.brackup$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn =~ s/\.brackup$// 10 7 17
361 7 my $stat = File::stat::stat("$dir/$fn.brackup");
362 7 push @ret, Brackup::TargetBackupStatInfo->new($self, $fn,
363 time => $stat->mtime,
364 size => $stat->size);
365 }
366 5 closedir($dh);
367  
368 5 return @ret;
369 }
370  
371 # downloads the given backup name to the current directory (with
372 # *.brackup extension) or to the specified location
373 2 ▶sub get_backup {
Subroutine get_backup: called CC=8 SCAR=21.7
Pod undocumented
374 2 my ($self, $name, $output_file) = @_;
375 2 my $file = $self->metapath("$name.brackup");
376  
377 2 ▶ die "File doesn't exist: $file" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 2 2
378  
379 2 ▶ $output_file ||= "$name.brackup";
Condition50%1/2
$output_file ||= "$name.brackup"
l!l
20
MC/DC0%0/1
$output_file ||= "$name.brackup"
$output_file
Truth table50%1/2
A
A= $output_file
Aresult
11
00
380  
381 2 ▶ open(my $in, $file) or die "Failed to open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, $file or die "Failed to open ${file}: $!\n" 2 0 2
382 2 ▶ open(my $out, '>', $output_file) or die "Failed to open $output_file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $output_file or die "Failed to open ${output_file}: $!\n" 2 0 2
383  
384 2 my $buf;
385 my $rv;
386 2 ▶ while ($rv = sysread($in, $buf, 128*1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv = sysread($in, $buf, 131072)) 2 2 4
387 2 my $outv = syswrite($out, $buf);
388 2 ▶ die "copy error" unless $outv == $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $outv == $rv 0 2 2
389 }
390 2 ▶ die "copy error" unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 2 2
391  
392 2 return 1;
393 }
394  
395 1 ▶sub delete_backup {
Subroutine delete_backup: called CC=2 SCAR=7.0
Pod undocumented
396 1 my $self = shift;
397 1 my $name = shift;
398  
399 1 my $file = $self->metapath("$name.brackup");
400 1 ▶ die "File doesn't exist: $file" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 1 1
401 1 unlink $file;
402 1 return 1;
403 }
404  
405 4 1;
406  
407  
408 =head1 NAME
409  
410 Brackup::Target::Filesystem - backup to a locally mounted filesystem
411  
412 =head1 DESCRIPTION
413  
414 Back up to an NFS or Samba server, another disk array (external storage), etc.
415  
416 =head1 EXAMPLE
417  
418 In your ~/.brackup.conf file:
419  
420 [TARGET:nfs_in_garage]
421 type = Filesystem
422 path = /mnt/nfs-garage/brackup/
423  
424 =head1 CONFIG OPTIONS
425  
426 =over
427  
428 =item B<type>
429  
430 Must be "B<Filesystem>".
431  
432 =item B<path>
433  
434 Path to backup to.
435  
436 =back
437  
438 =head1 SEE ALSO
439  
440 L<Brackup::Target>
« Brackup/Decrypt.pm ↑ Summary Brackup/Chunker/MP3.pm »