| 1 | package Directory::Diff::Copy; | ||||||||||
| 2 | 1 | require Exporter; | |||||||||
| 3 | 1 | @ISA = qw(Exporter); | |||||||||
| 4 | 1 | @EXPORT_OK = qw/copy_diff_only/; | |||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | our $VERSION = '0.08'; | |||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use File::Copy 'copy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use File::Path qw/rmtree mkpath/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Directory::Diff 'directory_diff'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | #=head2 mdate | ||||||||||
| 14 | # | ||||||||||
| 15 | #my $mod_date = mdate ($filename); | ||||||||||
| 16 | # | ||||||||||
| 17 | #Returns time that $filename was last modified. | ||||||||||
| 18 | # | ||||||||||
| 19 | #=cut | ||||||||||
| 20 | |||||||||||
| 21 | sub mdate | ||||||||||
| 22 | 2 | ▶ | { | ||||||||
|
Subroutine
mdate: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||
| 23 | 2 | my ($filename) = @_; | |||||||||
| 24 | 2 | ▶ | if (!-e $filename) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | die "reference file '$filename' not found"; | |||||||||
| 26 | } | ||||||||||
| 27 | 2 | my @stat = stat ($filename); | |||||||||
| 28 | 2 | ▶ | if (@stat == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | die "'stat' failed for '$filename': $@"; | |||||||||
| 30 | } | ||||||||||
| 31 | 2 | return $stat[9]; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | sub make_subdir | ||||||||||
| 35 | 1 | ▶ | { | ||||||||
|
Subroutine
make_subdir: called CC=4 SCAR=22.9
Pod
undocumented
| |||||||||||
| 36 | 1 | my ($path, $verbose) = @_; | |||||||||
| 37 | 1 | $path =~ s!/[^/]+$!/!; | |||||||||
| 38 | 1 | ▶ | if (! -d $path) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | print "Directory::Diff::Copy: Creating $path.\n"; | |||||||||
| 41 | } | ||||||||||
| 42 | 0 | mkpath ($path); | |||||||||
| 43 | 0 | ▶ | if (! -d $path) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | die "Could not make path '$path'.\n"; | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | sub new_only_callback | ||||||||||
| 50 | 0 | ▶ | { | ||||||||
|
Subroutine
new_only_callback: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 51 | 0 | my ($data, $dir, $file, $verbose) = @_; | |||||||||
| 52 | 0 | my $output_dir = $data->{output_dir}; | |||||||||
| 53 | 0 | my $copied_file = "$dir/$file"; | |||||||||
| 54 | 0 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | print "$file will be copied from $copied_file to $output_dir/$file\n"; | |||||||||
| 56 | } | ||||||||||
| 57 | 0 | my $path = "$output_dir/$file"; | |||||||||
| 58 | 0 | ▶ | if ($file =~ m!/$!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | print "Creating $path.\n"; | |||||||||
| 61 | } | ||||||||||
| 62 | # Make the directory | ||||||||||
| 63 | 0 | mkpath ($path); | |||||||||
| 64 | } | ||||||||||
| 65 | else { | ||||||||||
| 66 | 0 | make_subdir ($path, $verbose); | |||||||||
| 67 | 0 | ▶ | if (! -f $copied_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | die "The file to copy, '$copied_file', does not exist"; | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | sane_copy ("$dir/$file", "$output_dir/$file"); | |||||||||
| 71 | 0 | $data->{count}++; | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub sane_copy | ||||||||||
| 76 | 1 | ▶ | { | ||||||||
|
Subroutine
sane_copy: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||
| 77 | 1 | my ($from, $to) = @_; | |||||||||
| 78 | 1 | my (undef, undef, $mode) = stat ($from); | |||||||||
| 79 | 1 | ▶ | if (! defined $mode) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | die "Cannot stat $from: $!"; | |||||||||
| 81 | } | ||||||||||
| 82 | # perldoc -f stat | ||||||||||
| 83 | 1 | ▶ | copy ($from, $to) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | or die "Copy of '$from' to '$to' failed: $!"; | ||||||||||
| 85 | # perldoc -f chmod | ||||||||||
| 86 | 1 | ▶ | chmod $mode & 07777, $to or die "chmod on $to failed: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub diff_callback | ||||||||||
| 90 | 1 | ▶ | { | ||||||||
|
Subroutine
diff_callback: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||
| 91 | 1 | my ($data, $old_dir, $new_dir, $file, $verbose) = @_; | |||||||||
| 92 | 1 | my $output_dir = $data->{output_dir}; | |||||||||
| 93 | 1 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | print "$file will be copied from $new_dir to $output_dir/$file\n"; | |||||||||
| 95 | } | ||||||||||
| 96 | 1 | my $path = "$output_dir/$file"; | |||||||||
| 97 | 1 | make_subdir ($path, $verbose); | |||||||||
| 98 | 1 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | print "Directory::Diff::Copy: Copying '$new_dir/$file' to '$output_dir/$file'.\n"; | |||||||||
| 100 | } | ||||||||||
| 101 | 1 | sane_copy ("$new_dir/$file", "$output_dir/$file"); | |||||||||
| 102 | 1 | $data->{count}++; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | sub copy_diff_only | ||||||||||
| 108 | 1 | ▶ | { | ||||||||
|
Subroutine
copy_diff_only: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||
| 109 | 1 | my ($old_dir, $new_dir, $output_dir, $verbose) = @_; | |||||||||
| 110 | 1 | ▶ | if (mdate ($new_dir) < mdate ($old_dir)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | croak "$new_dir is older than $old_dir\n"; | |||||||||
| 112 | } | ||||||||||
| 113 | 1 | ▶ | if (-d $output_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | rmtree ($output_dir); | |||||||||
| 115 | } | ||||||||||
| 116 | 1 | mkpath $output_dir; | |||||||||
| 117 | 1 | my %data; | |||||||||
| 118 | 1 | $data{output_dir} = $output_dir; | |||||||||
| 119 | 1 | $data{count} = 0; | |||||||||
| 120 | 1 | directory_diff ($old_dir, $new_dir, | |||||||||
| 121 | { | ||||||||||
| 122 | dir2_only => \&new_only_callback, | ||||||||||
| 123 | diff => \&diff_callback, | ||||||||||
| 124 | data => \%data, | ||||||||||
| 125 | }, | ||||||||||
| 126 | $verbose); | ||||||||||
| 127 | 1 | return $data{count}; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 1 | 1; | |||||||||