blib/lib/File/Copy/Recursive/Verify.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 100% 57 / 57 CC 2 SCAR 1.3
CC
2
Cov
100%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package File::Copy::Recursive::Verify;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.1.0';
6  
7 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny::Retry ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Copy::Verify qw(verify_copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Class::Tiny qw(src_dir dst_dir),
Subroutine BEGIN: called CC=1 SCAR=0.0
12 {
13 tries => 10,
14 hash_algo => 'MD5',
15 src_hash => {},
16 dst_hash => {},
17 2 };
18  
19 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our @EXPORT_OK = qw(verify_rcopy rcopy);
22  
23 =encoding utf-8
24  
25 =head1 NAME
26  
27 File::Copy::Recursive::Verify - data-safe recursive copy
28  
29 =head1 SYNOPSIS
30  
31 use File::Copy::Recursive::Verify qw(verify_rcopy);
32  
33 verify_rcopy($dir_a, $dir_b);
34  
35 #OOP equivalent
36  
37 File::Copy::Recursive::Verify->new(
38 src_dir => $dir_a,
39 dst_dir => $dir_b,
40 )->copy();
41  
42 #some complex copy - I know SHA-256 hash of subdir/a.dat file
43 #tree $dir_a:
44 #.
45 #├── c.dat
46 #└── subdir
47 # ├── a.dat
48 # └── b.dat
49  
50 verify_rcopy($dir_a, $dir_b, {tries => 3, hash_algo => 'SHA-256', src_hash => {'subdir/a.dat' => '0'x64}});
51  
52 #OOP equivalent
53  
54 File::Copy::Recursive::Verify->new(
55 src_dir => $dir_a,
56 dst_dir => $dir_b,
57 tries => 3,
58 hash_algo => 'SHA-256',
59 src_hash => {'subdir/a.dat' => 0x64},
60 )->copy();
61  
62 =head1 DESCRIPTION
63  
64 Use L<File::Copy::Verify> for recursive copy.
65  
66 =head1 FUNCTIONS
67  
68 =head2 verify_rcopy($src_dir, $dst_dir, $options)
69  
70 functional api
71  
72 Recusive copy of C<dir_a> to C<dir_b>.
73  
74 Retry mechanism is via L<Try::Tiny::Retry> (Each file will try verify_copy 10 times with exponential backoff in default).
75  
76 As verification digest are use fastest I<MD5> in default.
77  
78 C<$options> is HashRef of L<attributes|/attributes>.
79  
80 return I<HashRef> of copied files (key source, value destination)
81  
82 =cut
83  
84 4 ▶sub verify_rcopy {
Subroutine verify_rcopy: called CC=1 SCAR=0.0
Pod documented
85 4 my ($src_dir, $dst_dir, $options) = @_;
86  
87 4 return File::Copy::Recursive::Verify->new(
88 src_dir => $src_dir,
89 dst_dir => $dst_dir,
90 %$options
91 )->copy();
92 }
93  
94 =head2 rcopy
95  
96 alias of C<verify_rcopy>
97  
98 =cut
99  
100 sub rcopy;
101 2 *rcopy = \&verify_rcopy;
102  
103 =head1 METHODS
104  
105 =head2 new(%attributes)
106  
107 =head3 %attributes
108  
109 =head4 src_dir
110  
111 source dir
112  
113 =head4 src_hash
114  
115 source I<HashRef> of path -> hash
116  
117 =head4 dst_dir
118  
119 destination dir
120  
121 =head4 dst_hash
122  
123 destination I<HashRef> of path -> hash
124  
125 =head4 hash_algo
126  
127 hash algorithm
128  
129 default I<MD5>
130  
131 =head4 tries
132  
133 number of tries
134  
135 more about retry - L<Try::Tiny::Retry>
136  
137 =head2 copy;
138  
139 start recursive copy
140  
141 return I<HashRef> of copied files (key source, value destination)
142  
143 =cut
144  
145 4 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
146 4 my ($self) = @_;
147  
148 return path($self->src_dir)->visit(
149 16 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
150 16 my ($path, $copied) = @_;
151  
152 16 ▶ return if $path->is_dir();
Branch: 1 branch
BranchTrueFalseTotal
if $path->is_dir 4 12 16
153  
154 12 my $rel_src = $path->relative($self->src_dir);
155 12 my $dst = path($self->dst_dir, $rel_src);
156 12 $dst->parent->mkpath();
157 12 my $rel_dst = $dst->relative($self->dst_dir);
158  
159 12 ▶ retry {
Subroutine __ANON__: called CC=1 SCAR=0.0
160 File::Copy::Verify->new(
161 src => $path,
162 src_hash => $self->src_hash->{$rel_src->stringify()},
163 dst => $dst,
164 12 dst_hash => $self->dst_hash->{$rel_dst->stringify()},
165 hash_algo => $self->hash_algo
166 )->copy();
167  
168 10 $copied->{$path} = $dst;
169 }
170 12 ▶ delay_exp { $self->tries, 1e5 }
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 2 die $_;
173 12 };
174 },
175 4 { recurse => 1 }
176 );
177 }
178  
179 =head1 LICENSE
180  
181 Copyright (C) Avast Software.
182  
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.
185  
186 =head1 AUTHOR
187  
188 Jan Seidl E<lt>seidl@avast.comE<gt>
189  
190 =cut
191  
192 2 1;
↑ Summary