| 1 | ########################################### | ||||||||||||||||||||||||||||
| 2 | package Archive::Tar::Merge; | ||||||||||||||||||||||||||||
| 3 | ########################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Archive::Tar::Wrapper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use File::Temp qw(tempdir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use File::Spec::Functions qw(abs2rel); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use File::Find; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Sysadm::Install qw(mkd); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ########################################### | ||||||||||||||||||||||||||||
| 20 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | ########################################### | ||||||||||||||||||||||||||||
| 22 | 9 | my($class, %options) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 9 | my $self = { | |||||||||||||||||||||||||||
| 25 | dest_tarball => undef, | ||||||||||||||||||||||||||||
| 26 | source_tarballs => [], | ||||||||||||||||||||||||||||
| 27 | hook => undef, | ||||||||||||||||||||||||||||
| 28 | %options, | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | if(@{ $self->{source_tarballs} } == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | LOGDIE "Need at least one tarball to merge"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | for my $tarball (@{ $self->{source_tarballs} }) { | |||||||||||||||||||||||||||
| 36 | 18 | ▶ | if(! -f $tarball) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | LOGDIE "Tarball not found: $tarball"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 9 | bless $self, $class; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | $self->unpack_sources(); | |||||||||||||||||||||||||||
| 44 | 9 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | ########################################### | ||||||||||||||||||||||||||||
| 48 | 9 | ▶ | sub unpack_sources { | ||||||||||||||||||||||||||
|
Subroutine
unpack_sources: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | ########################################### | ||||||||||||||||||||||||||||
| 50 | 9 | my($self) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | for my $source_tarball (@{ $self->{source_tarballs} }) { | |||||||||||||||||||||||||||
| 53 | 18 | my($tmpdir) = tempdir(CLEANUP => 1); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 18 | my %source = (); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 18 | $source{dir} = $tmpdir; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 18 | my $arch = Archive::Tar::Wrapper->new(tmpdir => $tmpdir); | |||||||||||||||||||||||||||
| 60 | 18 | $arch->read($source_tarball); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 18 | $source{archive} = $arch; | |||||||||||||||||||||||||||
| 63 | 18 | $source{tarball} = $source_tarball; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 18 | push @{ $self->{sources} }, \%source; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ###################################### | ||||||||||||||||||||||||||||
| 70 | 9 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=19 SCAR=30.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | ###################################### | ||||||||||||||||||||||||||||
| 72 | 9 | my($self) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | my $out_dir = tempdir(CLEANUP => 1); | |||||||||||||||||||||||||||
| 75 | 9 | my $out_tar = Archive::Tar::Wrapper->new(tmpdir => $out_dir); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | DEBUG "Merging tarballs ", | ||||||||||||||||||||||||||||
| 78 | 9 | join(', ', @{ $self->{source_tarballs} }), "."; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 9 | my $paths = {}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Build the following data structure: | ||||||||||||||||||||||||||||
| 83 | # rel/path1: | ||||||||||||||||||||||||||||
| 84 | # digests => digest1 => abs/path1 | ||||||||||||||||||||||||||||
| 85 | # digest2 => abs/path2 | ||||||||||||||||||||||||||||
| 86 | # paths => [abs/path1, abs/path2] | ||||||||||||||||||||||||||||
| 87 | # rel/path3: | ||||||||||||||||||||||||||||
| 88 | # digests => digest3 => abs/path3 | ||||||||||||||||||||||||||||
| 89 | # paths => [abs/path3] | ||||||||||||||||||||||||||||
| 90 | # ... | ||||||||||||||||||||||||||||
| 91 | 9 | for my $source (@{ $self->{sources} }) { | |||||||||||||||||||||||||||
| 92 | 18 | my $dir = $source->{dir}; | |||||||||||||||||||||||||||
| 93 | 89 | ▶ | find(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 94 | 89 | ▶ | return unless -f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 35 | my $rel = abs2rel($File::Find::name, $dir); | |||||||||||||||||||||||||||
| 96 | # Two down | ||||||||||||||||||||||||||||
| 97 | 35 | $rel =~ s#.*?/.*?/##; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 35 | my $digest = file_hash($File::Find::name); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # avoid autovivification | ||||||||||||||||||||||||||||
| 102 | 35 | ▶ | if(!exists $paths->{$rel} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $paths->{$rel} && exists $paths->{$rel}{'digests'}{$digest}
MC/DC100%2/2
exists $paths->{$rel} && exists $paths->{$rel}{'digests'}{$digest}
exists $paths->{$rel}
exists $paths->{$rel}{'digests'}{$digest}
Truth table100%3/3
A && B
A= exists $paths->{$rel}B= exists $paths->{$rel}{'digests'}{$digest}
| |||||||||||||||||||||||||||||
| 103 | !exists $paths->{$rel}->{digests}->{$digest}) { | ||||||||||||||||||||||||||||
| 104 | # create the data structure shown above | ||||||||||||||||||||||||||||
| 105 | 33 | $paths->{$rel}->{digests}->{$digest} = $File::Find::name; | |||||||||||||||||||||||||||
| 106 | 33 | push @{$paths->{$rel}->{paths}}, $File::Find::name; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 18 | }, $dir); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Traverse and figure out conflicts | ||||||||||||||||||||||||||||
| 112 | 9 | for my $relpath (keys %$paths) { | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 26 | my $dst_dir = File::Spec->catfile($out_dir, dirname($relpath)); | |||||||||||||||||||||||||||
| 115 | 26 | my @digests = keys %{$paths->{$relpath}->{digests}}; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 26 | my $dst_entry = File::Spec->catfile($dst_dir, | |||||||||||||||||||||||||||
| 118 | basename($relpath)); | ||||||||||||||||||||||||||||
| 119 | 26 | my $dst_content; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 26 | ▶ | mkd $dst_dir unless -d $dst_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 26 | my $src_entry = $paths->{$relpath}->{paths}->[0]; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 26 | ▶ | if(-l $src_entry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | DEBUG "Symlinking $src_entry to $dst_entry"; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | symlink(readlink($src_entry), $dst_entry) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | LOGDIE("symlinking $dst_entry failed: $!"); | ||||||||||||||||||||||||||||
| 129 | 0 | next; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 26 | ▶ | if(@digests == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # A unique file. Take it as-is, but call the | ||||||||||||||||||||||||||||
| 135 | # he hook if there's one defined. | ||||||||||||||||||||||||||||
| 136 | 19 | ▶ | if(defined $self->{hook}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | my $hook = $self->{hook}->( | ||||||||||||||||||||||||||||
| 138 | $relpath, | ||||||||||||||||||||||||||||
| 139 | $paths->{$relpath}->{paths}, | ||||||||||||||||||||||||||||
| 140 | 4 | $out_tar, | |||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 4 | ▶ | if(0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 144 | } elsif(defined $hook->{action}) { | ||||||||||||||||||||||||||||
| 145 | 1 | ▶ | if($hook->{action} eq "ignore") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 1 | DEBUG "Ignoring $relpath per hook"; | |||||||||||||||||||||||||||
| 147 | 1 | next; | |||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||
| 149 | 0 | LOGDIE "Unknown action from hook: ", | |||||||||||||||||||||||||||
| 150 | "$hook->{action}"; | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } elsif(defined $hook->{content}) { | ||||||||||||||||||||||||||||
| 153 | 2 | $dst_content = $hook->{content}; | |||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||
| 155 | # No action from hook, leave the file unmodified | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Several different versions of the file, call the | ||||||||||||||||||||||||||||
| 162 | # decider to pick one | ||||||||||||||||||||||||||||
| 163 | 7 | ▶ | if(defined $self->{decider}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | my $decision = $self->{decider}->( | ||||||||||||||||||||||||||||
| 165 | $relpath, | ||||||||||||||||||||||||||||
| 166 | $paths->{$relpath}->{paths}, | ||||||||||||||||||||||||||||
| 167 | 6 | $out_tar, | |||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 6 | ▶ | if(0) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 171 | } elsif(defined $decision->{action}) { | ||||||||||||||||||||||||||||
| 172 | 2 | ▶ | if($decision->{action} eq "ignore") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 2 | DEBUG "Ignoring $relpath per decider"; | |||||||||||||||||||||||||||
| 174 | 2 | next; | |||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||
| 176 | 0 | LOGDIE "Unknown action from decider: ", | |||||||||||||||||||||||||||
| 177 | "$decision->{action}"; | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } elsif(defined $decision->{index}) { | ||||||||||||||||||||||||||||
| 180 | $src_entry = | ||||||||||||||||||||||||||||
| 181 | 2 | $paths->{$relpath}->{paths}->[ $decision->{index} ]; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | } elsif(defined $decision->{content}) { | ||||||||||||||||||||||||||||
| 184 | 2 | $dst_content = $decision->{content}; | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 0 | LOGDIE "Decider failed to return decision"; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | } else { | ||||||||||||||||||||||||||||
| 189 | 1 | LOGDIE "Conflict: $relpath (and no decider defined)"; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 22 | ▶ | if(defined $dst_content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 4 | $out_tar->add($relpath, \$dst_content); | |||||||||||||||||||||||||||
| 195 | } else { | ||||||||||||||||||||||||||||
| 196 | 18 | $out_tar->add($relpath, $src_entry); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 8 | ▶ | if(defined $self->{dest_tarball}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 8 | my $compress = 0; | |||||||||||||||||||||||||||
| 202 | 8 | ▶ | if($self->{dest_tarball} =~ /gz$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 8 | $compress = 1; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 8 | $out_tar->write($self->{dest_tarball}, $compress); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 8 | return $out_tar; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | ###################################### | ||||||||||||||||||||||||||||
| 213 | 35 | ▶ | sub file_hash { | ||||||||||||||||||||||||||
|
Subroutine
file_hash: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 214 | ###################################### | ||||||||||||||||||||||||||||
| 215 | 35 | my($filename) = @_; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 35 | ▶ | open FH, "<$filename" or LOGDIE "Cannot open $filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 35 | my $ctx = Digest::MD5->new(); | |||||||||||||||||||||||||||
| 220 | 35 | $ctx->addfile(*FH); | |||||||||||||||||||||||||||
| 221 | 35 | my $digest = $ctx->hexdigest; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 35 | close FH; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 35 | return $digest; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 5 | 1; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | __END__ | ||||||||||||||||||||||||||||