Cpio.pm

filter: Statementstmt 17.2% 16 / 93 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 26.3% 5 / 19 Podpod 90.9% 10 / 11 totaltotal 18.5% 31 / 167 CC 21 SCAR 28.2
CC
21
Cov
12%
write
56.0
add_data
20.0
read
12.0
CRAP 16.7
« Cpio/FileHandle_with_pushback.pm ↑ Summary Cpio/OldBinary.pm »
1 package Archive::Cpio;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.10';
7  
8 1 ▶use Archive::Cpio::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Archive::Cpio::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Archive::Cpio::OldBinary;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Archive::Cpio - module for manipulations of cpio archives
15  
16 =head1 SYNOPSIS
17  
18 use Archive::Cpio;
19  
20 # simple example removing entry "foo"
21  
22 my $cpio = Archive::Cpio->new;
23 $cpio->read($file);
24 $cpio->remove('foo');
25 $cio->write($file);
26  
27 # more complex example, filtering on the fly
28  
29 my $cpio = Archive::Cpio->new;
30 $cpio->read_with_handler(\*STDIN,
31 sub {
32 my ($e) = @_;
33 if ($e->name ne 'foo') {
34 $cpio->write_one(\*STDOUT, $e);
35 }
36 });
37 $cpio->write_trailer(\*STDOUT);
38  
39 =head1 DESCRIPTION
40  
41 Archive::Cpio provides a few functions to read and write cpio files.
42  
43 =cut
44  
45  
46 =head2 Archive::Cpio->new()
47  
48 Create an object
49  
50 =cut
51  
52 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
53 0 my ($class, %options) = @_;
54 0 bless \%options, $class;
55 }
56  
57 =head2 $cpio->read($filename)
58  
59 =head2 $cpio->read($filehandle)
60  
61 Reads the cpio file
62  
63 =cut
64  
65 0 ▶sub read {
Subroutine read: not called CC=3 SCAR=24.8
Pod documented
66 0 my ($cpio, $file) = @_;
67  
68 0 my $IN;
69 0 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
70 0 $IN = $file;
71 } else {
72 0 ▶ open($IN, '<', $file) or die "can't open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $IN, "<", $file or die "can't open ${file}: $!\n" 0 0 0
73 }
74  
75 0 ▶ read_with_handler($cpio, $IN, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my ($e) = @_;
77 0 push @{$cpio->{list}}, $e;
78 0 });
79 }
80  
81 =head2 $cpio->write($filename)
82  
83 =head2 $cpio->write($filehandle)
84  
85 Writes the entries and the trailer
86  
87 =cut
88  
89 0 ▶sub write {
Subroutine write: not called CC=7 SCAR=40.3
Pod documented
90 0 my ($cpio, $file, $fmt) = @_;
91  
92 0 my $OUT;
93 0 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
94 0 $OUT = $file;
95 } else {
96 0 ▶ open($OUT, '>', $file) or die "can't open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $OUT, ">", $file or die "can't open ${file}: $!\n" 0 0 0
97 }
98  
99 # Set the format if not done or if specified
100 0 ▶ if (!$cpio->{archive_format} || $fmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cpio->{"archive_format"} or $fmt) 0 0 0
Condition0%0/3
not $cpio->{"archive_format"} or $fmt
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cpio->{"archive_format"} or $fmt
not $cpio->{"archive_format"} $fmt
Truth table0%0/3
not(A) or B
A= not $cpio->{"archive_format"}B= $fmt
ABresult
1X1
011
000
101 0 ▶ $cpio->{archive_format} = _create_archive_format($fmt || 'ODC');
Condition0%0/2
$fmt || 'ODC'
l!l
00
MC/DC0%0/1
$fmt || 'ODC'
$fmt
Truth table0%0/2
A
A= $fmt
Aresult
11
00
102 }
103  
104 0 $cpio->write_one($OUT, $_) foreach @{$cpio->{list}};
105 0 $cpio->write_trailer($OUT);
106 }
107  
108 =head2 $cpio->remove(@filenames)
109  
110 Removes any entries with names matching any of the given filenames from the in-memory archive
111  
112 =cut
113  
114 0 ▶sub remove {
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
115 0 my ($cpio, @filenames) = @_;
116 0 ▶ $cpio->{list} or die "can't remove from nothing\n";
Branch: 1 branch
BranchTrueFalseTotal
$cpio->{"list"} or die "can't remove from nothing\n" 0 0 0
117  
118 0 my %filenames = map { $_ => 1 } @filenames;
119  
120 0 @{$cpio->{list}} = grep { !$filenames{$_->name} } @{$cpio->{list}};
121 }
122  
123 =head2 $cpio->get_files([ @filenames ])
124  
125 Returns a list of C<Archive::Cpio::File> (after a C<$cpio->read>)
126  
127 =cut
128  
129 0 ▶sub get_files {
Subroutine get_files: not called CC=2 SCAR=17.9
Pod documented
130 0 my ($cpio, @list) = @_;
131 0 ▶ if (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) { } 0 0 0
132 0 map { get_file($cpio, $_) } @list;
133 } else {
134 0 @{$cpio->{list}};
135 }
136 }
137  
138 =head2 $cpio->get_file($filename)
139  
140 Returns the C<Archive::Cpio::File> matching C<$filename< (after a C<$cpio->read>)
141  
142 =cut
143  
144 0 ▶sub get_file {
Subroutine get_file: not called CC=3 SCAR=24.8
Pod documented
145 0 my ($cpio, $file) = @_;
146 0 foreach (@{$cpio->{list}}) {
147 0 ▶ $_->name eq $file and return $_;
Branch: 1 branch
BranchTrueFalseTotal
$_->name eq $file and return $_ 0 0 0
148 }
149 0 undef;
150 }
151  
152 =head2 $cpio->add_data($filename, $data, $opthashref)
153  
154 Takes a filename, a scalar full of data and optionally a reference to a hash with specific options.
155  
156 Will add a file to the in-memory archive, with name C<$filename> and content C<$data>.
157 Specific properties can be set using C<$opthashref>.
158  
159 =cut
160  
161 0 ▶sub add_data {
Subroutine add_data: not called CC=4 SCAR=30.0
Pod documented
162 0 my ($cpio, $filename, $data, $opthashref) = @_;
163 0 ▶ my $entry = $opthashref || {};
Condition0%0/2
$opthashref || {}
l!l
00
MC/DC0%0/1
$opthashref || {}
$opthashref
Truth table0%0/2
A
A= $opthashref
Aresult
11
00
164 0 $entry->{name} = $filename;
165 0 $entry->{data} = $data;
166 0 ▶ $entry->{nlink} ||= 1;
Condition0%0/2
$entry->{"nlink"} ||= 1
l!l
00
MC/DC0%0/1
$entry->{"nlink"} ||= 1
$entry->{"nlink"}
Truth table0%0/2
A
A= $entry->{"nlink"}
Aresult
11
00
167 0 ▶ $entry->{mode} ||= 0100644;
Condition0%0/2
$entry->{"mode"} ||= 33188
l!l
00
MC/DC0%0/1
$entry->{"mode"} ||= 33188
$entry->{"mode"}
Truth table0%0/2
A
A= $entry->{"mode"}
Aresult
11
00
168 0 push @{$cpio->{list}}, Archive::Cpio::File->new($entry);
169 }
170  
171 =head2 $cpio->read_with_handler($filehandle, $coderef)
172  
173 Calls the handler function on each header. An C<Archive::Cpio::File> is passed as a parameter
174  
175 =cut
176  
177 0 ▶sub read_with_handler {
Subroutine read_with_handler: not called CC=2 SCAR=17.9
Pod documented
178 0 my ($cpio, $F, $handler) = @_;
179  
180 0 my $FHwp = Archive::Cpio::FileHandle_with_pushback->new($F);
181 0 $cpio->{archive_format} = detect_archive_format($FHwp);
182  
183 0 ▶ while (my $entry = $cpio->{archive_format}->read_one($FHwp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $cpio->{"archive_format"}->read_one($FHwp)) 0 0 0
184 0 $entry = Archive::Cpio::File->new($entry);
185 0 $handler->($entry);
186 }
187 }
188  
189 =head2 $cpio->write_one($filehandle, $entry)
190  
191 Writes a C<Archive::Cpio::File> (beware, a valid cpio needs a trailer using C<write_trailer>)
192  
193 =cut
194  
195 0 ▶sub write_one {
Subroutine write_one: not called CC=1 SCAR=6.9
Pod documented
196 0 my ($cpio, $F, $entry) = @_;
197 0 $cpio->{archive_format}->write_one($F, $entry);
198 }
199  
200 =head2 $cpio->write_trailer($filehandle)
201  
202 Writes the trailer to finish the cpio file
203  
204 =cut
205  
206 0 ▶sub write_trailer {
Subroutine write_trailer: not called CC=1 SCAR=6.9
Pod documented
207 0 my ($cpio, $F) = @_;
208 0 $cpio->{archive_format}->write_trailer($F);
209 }
210  
211  
212  
213  
214 0 ▶sub _default_magic {
Subroutine _default_magic: not called CC=2 SCAR=17.9
215 0 my ($archive_format) = @_;
216 0 my $magics = Archive::Cpio::Common::magics();
217 0 my %format2magic = reverse %$magics;
218 0 ▶ $format2magic{$archive_format} or die "unknown archive_format $archive_format\n";
Branch: 1 branch
BranchTrueFalseTotal
$format2magic{$archive_format} or die "unknown archive_format $archive_format\n" 0 0 0
219 }
220  
221 0 ▶sub _create_archive_format {
Subroutine _create_archive_format: not called CC=2 SCAR=17.9
222 0 my ($archive_format, $magic) = @_;
223  
224 0 ▶ $magic ||= _default_magic($archive_format);
Condition0%0/3
$magic ||= _default_magic($archive_format)
l!l&&r!l&&!r
000
MC/DC0%0/2
$magic ||= _default_magic($archive_format)
$magic _default_magic($archive_format)
Truth table0%0/3
A ||= B
A= $magicB= _default_magic($archive_format)
ABresult
1X1
011
000
225  
226 # perl_checker: require Archive::Cpio::NewAscii
227 # perl_checker: require Archive::Cpio::OldBinary
228 0 my $class = "Archive::Cpio::$archive_format";
229 0 eval "require $class";
230 0 return $class->new($magic);
231 }
232  
233 0 ▶sub detect_archive_format {
Subroutine detect_archive_format: not called CC=3 SCAR=24.8
Pod undocumented
234 0 my ($FHwp) = @_;
235  
236 0 my $magics = Archive::Cpio::Common::magics();
237  
238 0 my $max_length = max(map { length $_ } values %$magics);
239 0 my $s = $FHwp->read_ahead($max_length);
240  
241 0 foreach my $magic (keys %$magics) {
242 0 my $archive_format = $magics->{$magic};
243 0 ▶ begins_with($s, $magic) or next;
Branch: 1 branch
BranchTrueFalseTotal
begins_with($s, $magic) or next 0 0 0
244  
245 #warn "found magic for $archive_format\n";
246  
247 # perl_checker: require Archive::Cpio::NewAscii
248 # perl_checker: require Archive::Cpio::OldBinary
249 0 return _create_archive_format($archive_format, $magic);
250 }
251 0 die "invalid archive\n";
252 }
253  
254 =head1 AUTHOR
255  
256 Pascal Rigaux <pixel@mandriva.com>
257  
258 =cut
« Cpio/FileHandle_with_pushback.pm ↑ Summary Cpio/OldBinary.pm »