File Coverage

blib/lib/Archive/Libarchive/FFI/Common.pm
Criterion Covered Total %
statement 86 115 74.7
branch 13 18 72.2
condition 2 5 40.0
subroutine 34 56 60.7
pod 0 6 0.0
total 135 200 67.5


line stmt bran cond sub pod time code
1             package Archive::Libarchive::FFI::Common;
2              
3 27     27   141 use strict;
  27         56  
  27         738  
4 27     27   143 use warnings;
  27         47  
  27         1430  
5              
6             # ABSTRACT: Libarchive private package
7             our $VERSION = '0.0900'; # VERSION
8              
9             package
10             Archive::Libarchive::FFI;
11              
12 27     27   182 use Encode qw( encode decode );
  27         47  
  27         13941  
13              
14             sub archive_write_open_memory ($$)
15             {
16 1     1 0 377 my($archive, $memory) = @_;
17 1         6 archive_write_open($archive, $memory, undef, \&_archive_write_open_memory_write, undef);
18             }
19              
20             sub _archive_write_open_memory_write
21             {
22 1     1   3 my($archive, $data, $buffer) = @_;
23 1         15 $$data .= $buffer;
24 1         4 return length $buffer;
25             }
26              
27             sub archive_read_open_fh ($$;$)
28             {
29 3     3 0 1397 my($archive, $fh, $bs) = @_;
30 3   50     18 $bs ||= 10240;
31 3         13 my $data = { bs => $bs, fh => $fh };
32 3         16 archive_read_open($archive, $data, undef, \&_archive_read_open_fh_read, undef);
33             }
34              
35             sub _archive_read_open_fh_read
36             {
37 5     5   8 my($archive, $data) = @_;
38 5         51 my $br = read $data->{fh}, my $buffer, $data->{bs};
39 5 50       13 if(defined $br)
40             {
41 5         25 return (ARCHIVE_OK(), $buffer);
42             }
43             else
44             {
45 0         0 warn 'read error';
46 0         0 return ARCHIVE_FAILED();
47             }
48             }
49              
50             sub archive_write_open_fh ($$)
51             {
52 1     1 0 657 my($archive, $fh) = @_;
53 1         4 my $data = { fh => $fh };
54 1         8 archive_write_open($archive, $data, undef, \&_archive_write_open_fh_write, undef);
55             }
56              
57             sub _archive_write_open_fh_write
58             {
59 1     1   4 my($archive, $data, $buffer) = @_;
60 1         153 my $bw = syswrite $data->{fh}, $buffer;
61 1 50       8 if(defined $bw)
62             {
63 1         5 return $bw;
64             }
65             else
66             {
67 0         0 warn 'write error';
68 0         0 return ARCHIVE_FATAL();
69             }
70             }
71              
72             # TODO: for XS version, implement this in XS
73             sub archive_entry_stat ($)
74             {
75 1     1 0 11 my($entry) = @_;
76 27     27   157 no strict 'refs';
  27         62  
  27         2848  
77 1         3 map { &{"archive_entry_$_"}($entry) } qw ( dev ino mode nlink uid gid rdev atime mtime ctime );
  10         17  
  10         56  
78             }
79              
80             # TODO: for XS version, implement this in XS
81             sub archive_entry_set_stat
82             {
83 1     1 0 10 my $entry = shift;
84 1         4 my $status = ARCHIVE_OK();
85 27     27   268 no strict 'refs';
  27         65  
  27         10507  
86 1         3 foreach my $prop (qw( dev ino mode nlink uid gid rdev ))
87             {
88 7         11 my $status2 = &{"archive_entry_set_$prop"}($entry, shift);
  7         39  
89 7 50       29 $status = $status2 if $status2 < $status;
90             }
91 1         4 foreach my $prop (qw( atime mtime ctime ))
92             {
93 3         6 my $value = shift;
94 3         6 my $status2 = &{"archive_entry_set_$prop"}($entry, $value, $value);
  3         19  
95 3 50       13 $status = $status2 if $status2 < $status;
96             }
97 1         4 $status;
98             }
99              
100             sub archive_read_data_into_fh
101             {
102 3     3 0 2687 my($archive, $fh) = @_;
103              
104 3         5 my $bw = 0;
105 3         4 my $zero;
106              
107 3         5 while(1)
108             {
109 6         18 my $r = archive_read_data_block($archive, my $buff, my $offset);
110 6 100       26 return ARCHIVE_OK() if $r == ARCHIVE_EOF();
111 3 50 33     12 if($r == ARCHIVE_OK() || $r == ARCHIVE_WARN())
112             {
113 3         9 while($offset != $bw)
114             {
115             # TODO: this is slow do something a little less brain dead.
116 0         0 print $fh "\0";
117 0         0 $bw++;
118             }
119 3         5 $bw += length $buff;
120 3         9 print $fh $buff;
121             }
122             else
123             {
124 0         0 return $r;
125             }
126             }
127             }
128              
129             *archive_entry_copy_stat = \&archive_entry_set_stat
130             if __PACKAGE__->can('archive_entry_set_stat');
131              
132             *archive_entry_copy_sourcepath = \&archive_entry_set_sourcepath
133             if __PACKAGE__->can('archive_entry_set_sourcepath');
134              
135             *archive_entry_copy_fflags_text = \&archive_entry_set_fflags_text
136             if __PACKAGE__->can('archive_entry_set_fflags_text');
137              
138             sub _sub_if_can ($$)
139             {
140 1080     1080   1766 my($name,$coderef) = @_;
141 1080 100       5634 if(__PACKAGE__->can("_$name"))
142             {
143 27     27   168 no strict 'refs';
  27         70  
  27         47151  
144 1026         1177 *{$name} = $coderef;
  1026         5824  
145             }
146             }
147              
148 126 100   126   606 sub _decode { defined $_[0] ? decode(archive_perl_codeset(),$_[0]) : $_[0] }
149 30 100   30   203 sub _encode { defined $_[0] ? encode(archive_perl_codeset(),$_[0]) : $_[0] }
150              
151             _sub_if_can( archive_version_string => sub {
152 0     0   0 _decode(_archive_version_string());
153             });
154             _sub_if_can( archive_format_name => sub {
155 12     12   69 _decode(_archive_format_name($_[0]));
156             });
157             _sub_if_can( archive_error_string => sub {
158 14     14   2049 _decode(_archive_error_string($_[0]));
159             });
160             _sub_if_can( archive_read_open_filename => sub {
161 10     10   2679 _archive_read_open_filename($_[0], _encode($_[1]), $_[2]);
162             });
163             _sub_if_can( archive_read_support_filter_program => sub {
164 0     0   0 _archive_read_support_filter_program($_[0], _encode($_[1]));
165             });
166             _sub_if_can( archive_read_set_filter_option => sub {
167 0     0   0 _archive_read_set_filter_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
168             });
169             _sub_if_can( archive_read_set_format_option => sub {
170 0     0   0 _archive_read_set_format_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
171             });
172             _sub_if_can( archive_read_set_option => sub {
173 0     0   0 _archive_read_set_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
174             });
175             _sub_if_can( archive_read_set_options => sub {
176 0     0   0 _archive_read_set_options($_[0], _encode($_[1]));
177             });
178             _sub_if_can( archive_read_set_format => sub {
179 0     0   0 _archive_read_set_format($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
180             });
181             _sub_if_can( archive_filter_name => sub {
182 16     16   58 _decode(_archive_filter_name($_[0], $_[1]));
183             });
184             _sub_if_can( archive_write_add_filter_by_name => sub {
185 0     0   0 _archive_write_add_filter_by_name($_[0], _encode($_[1]));
186             });
187             _sub_if_can( archive_write_add_filter_program => sub {
188 0     0   0 _archive_write_add_filter_program($_[0], _encode($_[1]));
189             });
190             _sub_if_can( archive_read_support_filter_program_signature => sub {
191             _archive_read_support_filter_program_signature($_[0], _encode($_[1]), $_[2]);
192             });
193             _sub_if_can( archive_read_append_filter_program_signature => sub {
194             _archive_read_append_filter_program_signature($_[0], _encode($_[1]), $_[2]);
195             });
196             _sub_if_can( archive_write_set_format_by_name => sub {
197 0     0   0 _archive_write_set_format_by_name($_[0], _encode($_[1]));
198             });
199             _sub_if_can( archive_write_open_filename => sub {
200 1     1   387 _archive_write_open_filename($_[0], _encode($_[1]));
201             });
202             _sub_if_can( archive_write_set_filter_option => sub {
203 0     0   0 _archive_write_set_filter_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
204             });
205             _sub_if_can( archive_write_set_format_option => sub {
206 1     1   116 _archive_write_set_format_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
207             });
208             _sub_if_can( archive_write_set_option => sub {
209 0     0   0 _archive_write_set_option($_[0], _encode($_[1]), _encode($_[2]), _encode($_[3]));
210             });
211             _sub_if_can( archive_write_set_options => sub {
212 0     0   0 _archive_write_set_options($_[0], _encode($_[1]));
213             });
214             _sub_if_can( archive_write_disk_gid => sub {
215 5     5   3260 _archive_write_disk_gid($_[0], _encode($_[1]), $_[2]);
216             });
217             _sub_if_can( archive_write_disk_uid => sub {
218 4     4   498 _archive_write_disk_uid($_[0], _encode($_[1]), $_[2]);
219             });
220             _sub_if_can( archive_entry_fflags_text => sub {
221 0     0   0 _decode(_archive_entry_fflags_text($_[0]));
222             });
223             _sub_if_can( archive_read_disk_open => sub {
224 0     0   0 _archive_read_disk_open($_[0], _encode($_[1]));
225             });
226             _sub_if_can( archive_read_disk_gname => sub {
227 4     4   2854 _decode(_archive_read_disk_gname($_[0], $_[1]));
228             });
229             _sub_if_can( archive_read_disk_uname => sub {
230 4     4   1170 _decode(_archive_read_disk_uname($_[0], $_[1]));
231             });
232             _sub_if_can( archive_entry_acl_add_entry => sub {
233 0     0   0 _archive_entry_acl_add_entry($_[0], $_[1], $_[2], $_[3], $_[4], _encode($_[5]));
234             });
235             _sub_if_can( archive_entry_acl_text => sub {
236 0     0   0 _decode(_archive_entry_acl_text($_[0], $_[1]));
237             });
238             _sub_if_can( archive_match_include_uname => sub {
239 2     2   780 _archive_match_include_uname($_[0], _encode($_[1]));
240             });
241             _sub_if_can( archive_match_include_gname => sub {
242 2     2   711 _archive_match_include_gname($_[0], _encode($_[1]));
243             });
244             _sub_if_can( archive_entry_set_sourcepath => sub {
245 1     1   4 _archive_entry_set_sourcepath($_[0], _encode($_[1]));
246             });
247             _sub_if_can( archive_entry_sourcepath => sub {
248 1     1   363 _decode(_archive_entry_sourcepath($_[0]));
249             });
250             _sub_if_can( archive_entry_set_fflags_text => sub {
251 0     0   0 _archive_entry_set_fflags_text($_[0], _encode($_[1]));
252             });
253             _sub_if_can( archive_entry_set_link => sub {
254 1     1   4 _archive_entry_set_link($_[0], _encode($_[1]));
255             });
256             _sub_if_can( archive_match_exclude_pattern => sub {
257 0     0   0 _archive_match_exclude_pattern($_[0], _encode($_[1]))
258             });
259             _sub_if_can( archive_match_exclude_pattern_from_file => sub {
260 0     0   0 _archive_match_exclude_pattern_from_file($_[0], _encode($_[1]), $_[2])
261             });
262             _sub_if_can( archive_match_include_pattern => sub {
263 1     1   5 _archive_match_include_pattern($_[0], _encode($_[1]))
264             });
265             _sub_if_can( archive_match_include_pattern_from_file => sub {
266 0     0     _archive_match_include_pattern_from_file($_[0], _encode($_[1]), $_[2])
267             });
268             _sub_if_can( archive_match_include_file_time => sub {
269 0     0     _archive_match_include_file_time($_[0], $_[1], _enccode( $_[2]))
270             });
271              
272             1;
273              
274             __END__