Archive.xs

filter: Statementstmt 61.1% 883 / 1443 Branchbran 40.5% 535 / 1320 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 51.3% 1418 / 2763 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« arch_io.c ↑ Summary tar.c »
1 /*
2 * Archive.xs - File::Raw::Archive XS shim.
3 *
4 * Registers the built-in tar plugin at BOOT. Exposes thin XSUBs that
5 * the Perl-facing layer (File/Raw/Archive.pm) drives:
6 * _open_read, _read_next, _read_data, _read_close
7 * _open_write, _write_add, _write_close
8 * _extract_to_fd, _apply_xattrs
9 * _list_plugins, _probe
10 *
11 * Higher-level convenience (each, list, extract, extract_all, pack)
12 * lives in pure Perl over these primitives.
13 */
14  
15 #define PERL_NO_GET_CONTEXT
16 #include "EXTERN.h"
17 #include "perl.h"
18 #include "XSUB.h"
19  
20 /* PERL_VERSION_GE was added to perl.h in 5.33.3; define it for older builds. */
21 #ifndef PERL_VERSION_GE
22 # define PERL_VERSION_GE(r,v,s) \
23 (PERL_REVISION > (r) || (PERL_REVISION == (r) && \
24 (PERL_VERSION > (v) || (PERL_VERSION == (v) && PERL_SUBVERSION >= (s)))))
25 #endif
26  
27 /* GvCV_set gained cache-invalidation semantics (and became a real function)
28 * around 5.14. On older builds the simple assignment is equivalent. */
29 #ifndef GvCV_set
30 # define GvCV_set(gv, cv) (GvCV(gv) = (cv))
31 #endif
32  
33 #include "archive_plugin.h"
34 #include "arch_io.h"
35 #include "tar.h"
36 #include "extract.h"
37 #include "marshal.h"
38  
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <unistd.h>
48 #include <utime.h>
49  
50 #define ARCHIVE_PATH_MAX 4096
51  
52 /* ============================================================
53 * Object slot layout
54 * ============================================================
55 *
56 * Reader / Writer / Entry are blessed AVs (Object::Proto pattern).
57 * Compile-time slot indices replace runtime hash-key lookups, which
58 * is faster and keeps every per-entry access in C to a pointer + add.
59 *
60 * Reader AV: [ handle, consumed, closed, cur_entry ]
61 * Writer AV: [ handle, closed ]
62 * Entry AV: [ reader, meta, slurped ]
63 * Meta AV: [ name, size, mode, mtime, mtime_ns, uid, gid, type,
64 * link_target, xattrs, is_sparse ]
65 *
66 * `handle` is a blessed-IV-ref to archive_handle_t* (class
67 * File::Raw::Archive::Reader or ::Writer); its DESTROY frees the C
68 * struct via maybe_free_handle. `meta` is a plain RV-AV. `xattrs`
69 * stays a hashref because callers index into it by arbitrary key.
70 */
71  
72 #define R_HANDLE 0
73 #define R_CONSUMED 1
74 #define R_CLOSED 2
75 #define R_CUR_ENTRY 3
76 #define R_SLOT_COUNT 4
77  
78 #define W_HANDLE 0
79 #define W_CLOSED 1
80 #define W_SLOT_COUNT 2
81  
82 #define E_READER 0
83 #define E_META 1
84 #define E_SLURPED 2
85 #define E_SLOT_COUNT 3
86  
87 #define M_NAME 0
88 #define M_SIZE 1
89 #define M_MODE 2
90 #define M_MTIME 3
91 #define M_MTIME_NS 4
92 #define M_UID 5
93 #define M_GID 6
94 #define M_TYPE 7
95 #define M_LINK_TARGET 8
96 #define M_XATTRS 9
97 #define M_IS_SPARSE 10
98 #define M_SLOT_COUNT 11
99  
100 #ifndef XS_EXTERNAL
101 #define XS_EXTERNAL(name) XS(name)
102 #endif
103  
104 /* ============================================================
105 * Reader / writer state
106 * ============================================================ */
107  
108 typedef struct {
109 int fd;
110 archive_fd_state_t fd_state;
111 archive_gz_src_t *gz_src;
112 archive_gz_sink_t *gz_sink;
113 const ArchivePlugin *plugin;
114 void *cursor;
115 int is_writer;
116 int closed;
117 /* The most recently emitted entry from Reader::next, kept here so
118 * Reader::next can drain its payload before advancing. Owned by
119 * the handle (refcount +1); released on next/close. */
120 SV *cur_entry_rv;
121 int consumed; /* current entry's payload drained? */
122 } archive_handle_t;
123  
124 static archive_handle_t *
125 110 make_handle(void)
126 {
127 110 archive_handle_t *h = (archive_handle_t *)calloc(1, sizeof *h);
128 110 ▶ if (h) h->fd = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (h) h->fd = -1; 110 0 110
129 110 return h;
130 }
131  
132 static void
133 110 free_handle(pTHX_ archive_handle_t *h)
134 {
135 110 ▶ if (!h || h->closed) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!h || h->closed) return; 110 0 110
if (!h || h->closed) return; 0 110 110
136 110 h->closed = 1;
137 110 ▶ if (h->cur_entry_rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->cur_entry_rv) { 0 110 110
138 0 SvREFCNT_dec(h->cur_entry_rv);
139 0 h->cur_entry_rv = NULL;
140 }
141 110 ▶ if (h->plugin && h->cursor) {
Branch: 2 branches
BranchTrueFalseTotal
if (h->plugin && h->cursor) { 110 0 110
if (h->plugin && h->cursor) { 110 0 110
142 110 ▶ if (h->is_writer) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->is_writer) { 40 70 110
143 40 ▶ if (h->plugin->write_close)
Branch: 1 branch
BranchTrueFalseTotal
if (h->plugin->write_close) 40 0 40
144 40 h->plugin->write_close(aTHX_ h->plugin, h->cursor);
145 } else {
146 70 ▶ if (h->plugin->read_close)
Branch: 1 branch
BranchTrueFalseTotal
if (h->plugin->read_close) 70 0 70
147 70 h->plugin->read_close(aTHX_ h->plugin, h->cursor);
148 }
149 }
150 110 ▶ if (h->gz_src) archive_gz_src_free(h->gz_src);
Branch: 1 branch
BranchTrueFalseTotal
if (h->gz_src) archive_gz_src_free(h->gz_src); 8 102 110
151 110 ▶ if (h->gz_sink) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->gz_sink) { 4 106 110
152 4 archive_gz_sink_finish(h->gz_sink);
153 4 archive_gz_sink_free(h->gz_sink);
154 }
155 110 ▶ if (h->fd >= 0) close(h->fd);
Branch: 1 branch
BranchTrueFalseTotal
if (h->fd >= 0) close(h->fd); 110 0 110
156 110 free(h);
157 }
158  
159 /* Forward decls: defined further down but used by open_reader below. */
160 static int sniff_gzip(int fd);
161 static int resolve_compression(pTHX_ int fd, HV *opts);
162 static const ArchivePlugin *resolve_plugin(pTHX_ HV *opts, int fd, int gz);
163  
164 /* Common reader-open helper used by every top-level path-taking
165 * method. Mirrors what _open_read does but factored for reuse from
166 * list/extract/extract_all/each. Returns archive_handle_t* on success
167 * or NULL after croaking. */
168 static archive_handle_t *
169 71 open_reader(pTHX_ SV *path_sv, HV *opts)
170 {
171 archive_handle_t *h;
172 int gz, fd;
173 const ArchivePlugin *plugin;
174 archive_pull_fn pull;
175 void *src;
176  
177 71 fd = open(SvPV_nolen(path_sv), O_RDONLY);
178 72 ▶ if (fd < 0) croak("File::Raw::Archive: cannot open '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) croak("File::Raw::Archive: cannot open '%s': %s", 1 70 71
179 SvPV_nolen(path_sv), strerror(errno));
180  
181 70 gz = resolve_compression(aTHX_ fd, opts);
182 70 plugin = resolve_plugin(aTHX_ opts, fd, gz);
183 70 ▶ if (!plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if (!plugin) { 0 70 70
184 0 close(fd);
185 0 croak("File::Raw::Archive: no plugin selected (auto-detection failed)");
186 }
187  
188 70 h = make_handle();
189 70 ▶ if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); } 0 70 70
190 70 h->fd = fd;
191 70 h->plugin = plugin;
192 70 h->consumed = 1; /* no entry yet -> nothing to drain */
193  
194 70 ▶ if (gz) {
Branch: 1 branch
BranchTrueFalseTotal
if (gz) { 8 62 70
195 8 h->gz_src = archive_gz_src_new(fd, ARCHIVE_DEFAULT_CHUNK);
196 8 ▶ if (!h->gz_src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!h->gz_src) { 0 8 8
197 0 free_handle(aTHX_ h);
198 0 croak("File::Raw::Archive: gzip stream init failed");
199 }
200 8 pull = archive_pull_gz;
201 8 src = h->gz_src;
202 } else {
203 62 h->fd_state.fd = fd;
204 62 pull = archive_pull_fd;
205 62 src = &h->fd_state;
206 }
207  
208 70 ▶ if (plugin->read_open(aTHX_ plugin, pull, src, opts, &h->cursor) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plugin->read_open(aTHX_ plugin, pull, src, opts, &h->cursor) < 0) { 0 70 70
209 0 free_handle(aTHX_ h);
210 0 croak("File::Raw::Archive: read_open failed");
211 }
212 70 return h;
213 }
214  
215 /* Drain the current entry's payload bytes if not already consumed.
216 * Called between read_next invocations and during close. */
217 static void
218 0 drain_current_entry(pTHX_ archive_handle_t *h)
219 {
220 0 ▶ if (h->consumed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (h->consumed) return; 0 0 0
221 char buf[16 * 1024];
222 int n;
223 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
224 0 ▶ buf, sizeof buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
buf, sizeof buf)) > 0) {} 0 0 0
225 0 h->consumed = 1;
226 0 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 0 0
227 }
228  
229 /* Common writer-open helper. */
230 static archive_handle_t *
231 41 open_writer(pTHX_ SV *path_sv, HV *opts)
232 {
233 archive_handle_t *h;
234 41 int gz = 0, fd;
235 const ArchivePlugin *plugin;
236 archive_push_fn push;
237 void *sink;
238  
239 41 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 41 0 41
240 41 SV **sv = hv_fetchs(opts, "compression", 0);
241 41 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 4 37 41
if (sv && *sv && SvOK(*sv)) { 4 0 4
if (sv && *sv && SvOK(*sv)) { 4 0 4
242 STRLEN nn;
243 4 const char *pp = SvPV(*sv, nn);
244 4 ▶ if (nn == 4 && memcmp(pp, "gzip", 4) == 0) gz = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (nn == 4 && memcmp(pp, "gzip", 4) == 0) gz = 1; 4 0 4
if (nn == 4 && memcmp(pp, "gzip", 4) == 0) gz = 1; 4 0 4
245 0 ▶ else if (nn == 4 && memcmp(pp, "auto", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (nn == 4 && memcmp(pp, "auto", 4) == 0) { 0 0 0
else if (nn == 4 && memcmp(pp, "auto", 4) == 0) { 0 0 0
246 STRLEN pl;
247 0 const char *pp2 = SvPV(path_sv, pl);
248 0 ▶ if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1; 0 0 0
if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1; 0 0 0
249 }
250 }
251 }
252  
253 41 fd = open(SvPV_nolen(path_sv), O_WRONLY | O_CREAT | O_TRUNC, 0644);
254 41 ▶ if (fd < 0) croak("File::Raw::Archive: cannot open '%s' for write: %s",
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) croak("File::Raw::Archive: cannot open '%s' for write: %s", 0 41 41
255 SvPV_nolen(path_sv), strerror(errno));
256  
257 {
258 41 ▶ SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL; 41 0 41
259 42 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 1 40 41
if (sv && *sv && SvOK(*sv)) { 1 0 1
if (sv && *sv && SvOK(*sv)) { 1 0 1
260 STRLEN nn;
261 1 const char *pp = SvPV(*sv, nn);
262 char namebuf[64];
263 1 ▶ if (nn >= sizeof namebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (nn >= sizeof namebuf) { 0 1 1
264 0 close(fd);
265 0 croak("File::Raw::Archive: plugin name too long");
266 }
267 1 memcpy(namebuf, pp, nn);
268 1 namebuf[nn] = '\0';
269 1 plugin = archive_lookup_plugin(aTHX_ namebuf);
270 } else {
271 40 plugin = archive_lookup_plugin(aTHX_ "tar");
272 }
273 }
274 41 ▶ if (!plugin) { close(fd); croak("File::Raw::Archive: unknown plugin"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!plugin) { close(fd); croak("File::Raw::Archive: unknown plugin"); } 1 40 41
275  
276 40 h = make_handle();
277 40 ▶ if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); } 0 40 40
278 40 h->fd = fd;
279 40 h->plugin = plugin;
280 40 h->is_writer = 1;
281 40 h->consumed = 1;
282  
283 40 ▶ if (gz) {
Branch: 1 branch
BranchTrueFalseTotal
if (gz) { 4 36 40
284 4 int level = 6;
285 4 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 4 0 4
286 4 SV **lsv = hv_fetchs(opts, "level", 0);
287 4 ▶ if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv);
Branch: 3 branches
BranchTrueFalseTotal
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 2 2 4
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 2 0 2
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 2 0 2
288 }
289 4 h->gz_sink = archive_gz_sink_new(fd, ARCHIVE_DEFAULT_CHUNK, level);
290 4 ▶ if (!h->gz_sink) {
Branch: 1 branch
BranchTrueFalseTotal
if (!h->gz_sink) { 0 4 4
291 0 free_handle(aTHX_ h);
292 0 croak("File::Raw::Archive: gzip sink init failed");
293 }
294 4 push = archive_push_gz;
295 4 sink = h->gz_sink;
296 } else {
297 36 h->fd_state.fd = fd;
298 36 push = archive_push_fd;
299 36 sink = &h->fd_state;
300 }
301  
302 40 ▶ if (plugin->write_open(aTHX_ plugin, push, sink, opts, &h->cursor) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plugin->write_open(aTHX_ plugin, push, sink, opts, &h->cursor) < 0) { 0 40 40
303 0 free_handle(aTHX_ h);
304 0 croak("File::Raw::Archive: write_open failed (bad format option?)");
305 }
306 40 return h;
307 }
308  
309 /* ============================================================
310 * Helpers
311 * ============================================================ */
312  
313 /* Detect gzip magic 1f 8b at offset 0. */
314 static int
315 67 sniff_gzip(int fd)
316 {
317 unsigned char magic[2];
318 67 off_t pos = lseek(fd, 0, SEEK_CUR);
319 67 ▶ if (pos < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos < 0) return 0; 0 67 67
320 67 ssize_t n = read(fd, magic, 2);
321 67 lseek(fd, pos, SEEK_SET);
322 67 ▶ if (n < 2) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) return 0; 0 67 67
323 67 ▶ return (magic[0] == 0x1f && magic[1] == 0x8b);
Branch: 2 branches
BranchTrueFalseTotal
return (magic[0] == 0x1f && magic[1] == 0x8b); 6 61 67
return (magic[0] == 0x1f && magic[1] == 0x8b); 6 0 6
324 }
325  
326 /* Resolve compression option: returns 1 if gzip should be used. */
327 static int
328 70 resolve_compression(pTHX_ int fd, HV *opts)
329 {
330 70 ▶ if (!opts) return sniff_gzip(fd);
Branch: 1 branch
BranchTrueFalseTotal
if (!opts) return sniff_gzip(fd); 0 70 70
331 70 SV **sv = hv_fetchs(opts, "compression", 0);
332 70 ▶ if (!sv || !*sv || !SvOK(*sv)) return sniff_gzip(fd);
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !*sv || !SvOK(*sv)) return sniff_gzip(fd); 5 65 70
if (!sv || !*sv || !SvOK(*sv)) return sniff_gzip(fd); 5 0 5
if (!sv || !*sv || !SvOK(*sv)) return sniff_gzip(fd); 0 5 5
333 STRLEN n;
334 5 const char *p = SvPV(*sv, n);
335 5 ▶ if (n == 4 && memcmp(p, "auto", 4) == 0) return sniff_gzip(fd);
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(p, "auto", 4) == 0) return sniff_gzip(fd); 5 0 5
if (n == 4 && memcmp(p, "auto", 4) == 0) return sniff_gzip(fd); 2 3 5
336 3 ▶ if (n == 4 && memcmp(p, "gzip", 4) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(p, "gzip", 4) == 0) return 1; 3 0 3
if (n == 4 && memcmp(p, "gzip", 4) == 0) return 1; 2 1 3
337 1 ▶ if (n == 4 && memcmp(p, "none", 4) == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(p, "none", 4) == 0) return 0; 1 0 1
if (n == 4 && memcmp(p, "none", 4) == 0) return 0; 1 0 1
338 0 return sniff_gzip(fd);
339 }
340  
341 /* Resolve plugin option: returns plugin or NULL with error string. */
342 static const ArchivePlugin *
343 70 resolve_plugin(pTHX_ HV *opts, int fd, int gz)
344 {
345 70 ▶ SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL; 70 0 70
346 70 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 0 70 70
if (sv && *sv && SvOK(*sv)) { 0 0 0
if (sv && *sv && SvOK(*sv)) { 0 0 0
347 STRLEN n;
348 0 const char *p = SvPV(*sv, n);
349 0 ▶ if (n == 4 && memcmp(p, "auto", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 4 && memcmp(p, "auto", 4) == 0) { 0 0 0
if (n == 4 && memcmp(p, "auto", 4) == 0) { 0 0 0
350 /* Probe by reading the first block. */
351 char probe_buf[512];
352 0 off_t pos = lseek(fd, 0, SEEK_CUR);
353 0 ▶ if (pos < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (pos < 0) return NULL; 0 0 0
354 /* If gzip-wrapped, we'd need to inflate to probe. For now,
355 * default to tar when auto + gzip. */
356 0 ▶ if (gz) return archive_lookup_plugin(aTHX_ "tar");
Branch: 1 branch
BranchTrueFalseTotal
if (gz) return archive_lookup_plugin(aTHX_ "tar"); 0 0 0
357 0 ssize_t got = read(fd, probe_buf, sizeof probe_buf);
358 0 lseek(fd, pos, SEEK_SET);
359 0 ▶ if (got <= 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) return NULL; 0 0 0
360 0 return archive_probe_for(aTHX_ probe_buf, (size_t)got);
361 }
362 char namebuf[64];
363 0 ▶ if (n >= sizeof namebuf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof namebuf) return NULL; 0 0 0
364 0 memcpy(namebuf, p, n);
365 0 namebuf[n] = '\0';
366 0 return archive_lookup_plugin(aTHX_ namebuf);
367 }
368 70 return archive_lookup_plugin(aTHX_ "tar");
369 }
370  
371 /* Build a snapshot hashref from an ArchiveEntry. Used only by `list`
372 * for a friendlier AoH return shape. Internal Entry objects use AV
373 * slot indices (entry_to_av) for speed. */
374 static SV *
375 564 entry_to_hv_snapshot(pTHX_ const ArchiveEntry *e)
376 {
377 564 HV *h = newHV();
378 564 ▶ hv_stores(h, "name", newSVpvn(e->name ? e->name : "", e->name_len));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "name", newSVpvn(e->name ? e->name : "", e->name_len)); 564 0 564
379 564 hv_stores(h, "size", newSVuv((UV)e->size));
380 564 hv_stores(h, "mode", newSVuv((UV)e->mode));
381 564 hv_stores(h, "mtime", newSVuv((UV)e->mtime));
382 564 hv_stores(h, "mtime_ns", newSVuv((UV)e->mtime_ns));
383 564 hv_stores(h, "uid", newSVuv((UV)e->uid));
384 564 hv_stores(h, "gid", newSVuv((UV)e->gid));
385 564 hv_stores(h, "type", newSViv((IV)e->type));
386 564 hv_stores(h, "is_sparse", newSViv(e->is_sparse));
387 564 ▶ if (e->link_target)
Branch: 1 branch
BranchTrueFalseTotal
if (e->link_target) 0 564 564
388 0 hv_stores(h, "link_target",
389 newSVpvn(e->link_target, e->link_target_len));
390 564 ▶ if (e->xattr_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->xattr_count > 0) { 0 564 564
391 0 HV *xh = newHV();
392 size_t i;
393 0 ▶ for (i = 0; i < e->xattr_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->xattr_count; i++) { 0 0 0
394 0 hv_store(xh, e->xattrs[i].key, (I32)e->xattrs[i].key_len,
395 newSVpvn(e->xattrs[i].value, e->xattrs[i].value_len), 0);
396 }
397 0 hv_stores(h, "xattrs", newRV_noinc((SV *)xh));
398 }
399 564 return newRV_noinc((SV *)h);
400 }
401  
402 /* Build a meta AV from an ArchiveEntry. Slot layout: M_NAME..M_IS_SPARSE.
403 * Returns RV-AV at refcount +1. Slots that aren't applicable (e.g.
404 * link_target on a regular file) are left empty rather than stored as
405 * undef so size() sees a missing slot and returns the default. */
406 static SV *
407 111 entry_to_av(pTHX_ const ArchiveEntry *e)
408 {
409 111 AV *meta = newAV();
410 111 av_extend(meta, M_SLOT_COUNT - 1);
411 111 ▶ av_store(meta, M_NAME,
Branch: 1 branch
BranchTrueFalseTotal
av_store(meta, M_NAME, 111 0 111
412 newSVpvn(e->name ? e->name : "", e->name_len));
413 111 av_store(meta, M_SIZE, newSVuv((UV)e->size));
414 111 av_store(meta, M_MODE, newSVuv((UV)e->mode));
415 111 av_store(meta, M_MTIME, newSVuv((UV)e->mtime));
416 111 av_store(meta, M_MTIME_NS, newSVuv((UV)e->mtime_ns));
417 111 av_store(meta, M_UID, newSVuv((UV)e->uid));
418 111 av_store(meta, M_GID, newSVuv((UV)e->gid));
419 111 av_store(meta, M_TYPE, newSViv((IV)e->type));
420 111 av_store(meta, M_IS_SPARSE, newSViv(e->is_sparse));
421 111 ▶ if (e->link_target) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->link_target) { 4 107 111
422 4 av_store(meta, M_LINK_TARGET,
423 newSVpvn(e->link_target, e->link_target_len));
424 }
425 111 ▶ if (e->xattr_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->xattr_count > 0) { 3 108 111
426 3 HV *xh = newHV();
427 size_t i;
428 8 ▶ for (i = 0; i < e->xattr_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->xattr_count; i++) { 5 3 8
429 5 hv_store(xh, e->xattrs[i].key, (I32)e->xattrs[i].key_len,
430 newSVpvn(e->xattrs[i].value, e->xattrs[i].value_len), 0);
431 }
432 3 av_store(meta, M_XATTRS, newRV_noinc((SV *)xh));
433 }
434 111 return newRV_noinc((SV *)meta);
435 }
436  
437 /* Decode a Perl entry hashref into ArchiveEntry. The returned entry
438 * borrows pointers into SVs that must outlive the call - caller keeps
439 * the hash alive until write_add returns. */
440 static void
441 679 hv_to_entry(pTHX_ HV *h, ArchiveEntry *e,
442 ArchiveXattr *xattr_buf, size_t xattr_buf_n,
443 size_t *xattr_used)
444 {
445 679 memset(e, 0, sizeof *e);
446 679 *xattr_used = 0;
447  
448 679 SV **sv = hv_fetchs(h, "name", 0);
449 679 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 679 0 679
if (sv && *sv && SvOK(*sv)) { 679 0 679
if (sv && *sv && SvOK(*sv)) { 679 0 679
450 STRLEN nl;
451 679 e->name = SvPV(*sv, nl);
452 679 e->name_len = nl;
453 }
454 679 sv = hv_fetchs(h, "type", 0);
455 679 ▶ if (sv && *sv && SvOK(*sv)) e->type = (int)SvIV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->type = (int)SvIV(*sv); 679 0 679
if (sv && *sv && SvOK(*sv)) e->type = (int)SvIV(*sv); 679 0 679
if (sv && *sv && SvOK(*sv)) e->type = (int)SvIV(*sv); 679 0 679
456 0 else e->type = AE_FILE;
457  
458 679 sv = hv_fetchs(h, "mode", 0);
459 679 ▶ if (sv && *sv && SvOK(*sv)) e->mode = (uint32_t)SvUV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->mode = (uint32_t)SvUV(*sv); 679 0 679
if (sv && *sv && SvOK(*sv)) e->mode = (uint32_t)SvUV(*sv); 679 0 679
if (sv && *sv && SvOK(*sv)) e->mode = (uint32_t)SvUV(*sv); 679 0 679
460  
461 679 sv = hv_fetchs(h, "size", 0);
462 679 ▶ if (sv && *sv && SvOK(*sv)) e->size = (uint64_t)SvUV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->size = (uint64_t)SvUV(*sv); 0 679 679
if (sv && *sv && SvOK(*sv)) e->size = (uint64_t)SvUV(*sv); 0 0 0
if (sv && *sv && SvOK(*sv)) e->size = (uint64_t)SvUV(*sv); 0 0 0
463  
464 679 sv = hv_fetchs(h, "mtime", 0);
465 679 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 8 671 679
if (sv && *sv && SvOK(*sv)) { 8 0 8
if (sv && *sv && SvOK(*sv)) { 8 0 8
466 8 ▶ if (SvNOKp(*sv) || (SvPOKp(*sv) && strchr(SvPV_nolen(*sv), '.'))) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvNOKp(*sv) || (SvPOKp(*sv) && strchr(SvPV_nolen(*sv), '.'))) { 8 0 8
if (SvNOKp(*sv) || (SvPOKp(*sv) && strchr(SvPV_nolen(*sv), '.'))) { 0 8 8
if (SvNOKp(*sv) || (SvPOKp(*sv) && strchr(SvPV_nolen(*sv), '.'))) { 0 0 0
467 0 NV nv = SvNV(*sv);
468 0 uint64_t whole = (uint64_t)nv;
469 0 double frac = nv - (double)whole;
470 0 ▶ if (frac < 0) frac = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (frac < 0) frac = 0; 0 0 0
471 0 e->mtime = whole;
472 0 e->mtime_ns = (uint32_t)(frac * 1e9 + 0.5);
473 0 ▶ if (e->mtime_ns >= 1000000000U) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime_ns >= 1000000000U) { 0 0 0
474 0 e->mtime++;
475 0 e->mtime_ns -= 1000000000U;
476 }
477 } else {
478 8 e->mtime = (uint64_t)SvUV(*sv);
479 }
480 }
481 679 sv = hv_fetchs(h, "mtime_ns", 0);
482 679 ▶ if (sv && *sv && SvOK(*sv)) e->mtime_ns = (uint32_t)SvUV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->mtime_ns = (uint32_t)SvUV(*sv); 4 675 679
if (sv && *sv && SvOK(*sv)) e->mtime_ns = (uint32_t)SvUV(*sv); 4 0 4
if (sv && *sv && SvOK(*sv)) e->mtime_ns = (uint32_t)SvUV(*sv); 4 0 4
483  
484 679 sv = hv_fetchs(h, "uid", 0);
485 679 ▶ if (sv && *sv && SvOK(*sv)) e->uid = (uint32_t)SvUV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->uid = (uint32_t)SvUV(*sv); 8 671 679
if (sv && *sv && SvOK(*sv)) e->uid = (uint32_t)SvUV(*sv); 8 0 8
if (sv && *sv && SvOK(*sv)) e->uid = (uint32_t)SvUV(*sv); 8 0 8
486 679 sv = hv_fetchs(h, "gid", 0);
487 679 ▶ if (sv && *sv && SvOK(*sv)) e->gid = (uint32_t)SvUV(*sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) e->gid = (uint32_t)SvUV(*sv); 6 673 679
if (sv && *sv && SvOK(*sv)) e->gid = (uint32_t)SvUV(*sv); 6 0 6
if (sv && *sv && SvOK(*sv)) e->gid = (uint32_t)SvUV(*sv); 6 0 6
488  
489 679 sv = hv_fetchs(h, "link_target", 0);
490 679 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 4 675 679
if (sv && *sv && SvOK(*sv)) { 4 0 4
if (sv && *sv && SvOK(*sv)) { 4 0 4
491 STRLEN ll;
492 4 e->link_target = SvPV(*sv, ll);
493 4 e->link_target_len = ll;
494 }
495  
496 679 sv = hv_fetchs(h, "xattrs", 0);
497 679 ▶ if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVHV) { 3 676 679
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVHV) { 3 0 3
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVHV) { 3 0 3
if (sv && *sv && SvROK(*sv) && SvTYPE(SvRV(*sv)) == SVt_PVHV) { 3 0 3
498 3 HV *xh = (HV *)SvRV(*sv);
499 3 hv_iterinit(xh);
500 HE *he;
501 8 ▶ while ((he = hv_iternext(xh)) && *xattr_used < xattr_buf_n) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(xh)) && *xattr_used < xattr_buf_n) { 5 3 8
while ((he = hv_iternext(xh)) && *xattr_used < xattr_buf_n) { 5 0 5
502 I32 klen_i;
503 5 const char *k = hv_iterkey(he, &klen_i);
504 5 SV *v = hv_iterval(xh, he);
505 STRLEN vlen;
506 5 const char *vp = SvPV(v, vlen);
507 5 xattr_buf[*xattr_used].key = k;
508 5 xattr_buf[*xattr_used].key_len = (size_t)klen_i;
509 5 xattr_buf[*xattr_used].value = vp;
510 5 xattr_buf[*xattr_used].value_len = vlen;
511 5 (*xattr_used)++;
512 }
513 3 e->xattrs = xattr_buf;
514 3 e->xattr_count = *xattr_used;
515 }
516 679 }
517  
518 /* Bless a handle pointer into a class. */
519 static SV *
520 80 new_handle_obj(pTHX_ archive_handle_t *h, const char *cls)
521 {
522 80 SV *iv = newSViv(PTR2IV(h));
523 80 SV *rv = newRV_noinc(iv);
524 80 return sv_bless(rv, gv_stashpv(cls, GV_ADD));
525 }
526  
527 static archive_handle_t *
528 839 unwrap_handle(pTHX_ SV *sv)
529 {
530 839 ▶ if (!SvROK(sv) || !SvIOK(SvRV(sv))) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || !SvIOK(SvRV(sv))) return NULL; 838 1 839
if (!SvROK(sv) || !SvIOK(SvRV(sv))) return NULL; 0 838 838
531 838 return INT2PTR(archive_handle_t *, SvIV(SvRV(sv)));
532 }
533  
534 /* Idempotent close-and-free for a blessed-IV-ref handle SV. After the
535 * first call, the underlying IV is set to 0 so subsequent calls (e.g.
536 * from DESTROY after an explicit close) become no-ops without a double
537 * free. */
538 static void
539 160 maybe_free_handle(pTHX_ SV *handle_sv)
540 {
541 160 ▶ if (!handle_sv || !SvROK(handle_sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_sv || !SvROK(handle_sv)) return; 160 0 160
if (!handle_sv || !SvROK(handle_sv)) return; 1 159 160
542 159 SV *iv_sv = SvRV(handle_sv);
543 159 ▶ if (!SvIOK(iv_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvIOK(iv_sv)) return; 0 159 159
544 159 archive_handle_t *h = INT2PTR(archive_handle_t *, SvIV(iv_sv));
545 159 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 79 80 159
546 80 free_handle(aTHX_ h);
547 80 sv_setiv(iv_sv, 0);
548 }
549  
550 /* Hash-key lookup helper. Returns the SV* at key, or NULL if not
551 * present / not defined. dTHX picks up the interpreter from
552 * PERL_GET_THX so callers don't have to thread pTHX_ through. */
553 static SV *
554 0 hv_get(HV *h, const char *key, I32 klen)
555 {
556 dTHX;
557 0 SV **p = hv_fetch(h, key, klen, 0);
558 0 ▶ if (!p || !*p || !SvOK(*p)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !*p || !SvOK(*p)) return NULL; 0 0 0
if (!p || !*p || !SvOK(*p)) return NULL; 0 0 0
if (!p || !*p || !SvOK(*p)) return NULL; 0 0 0
559 0 return *p;
560 }
561  
562 /* SAVEDESTRUCTOR_X target: closes a handle when the current dynamic
563 * scope unwinds, including via croak. Used by the new public top-
564 * level XSUBs that own the open + work + close lifecycle. */
565 static void
566 30 free_handle_destructor(pTHX_ void *data)
567 {
568 30 free_handle(aTHX_ (archive_handle_t *)data);
569 30 }
570  
571 /* Compile-time platform gate for parallel extract: needs fork(2)/
572 * pipe(2)/waitpid(2). Windows lacks them, so parallel=N silently
573 * falls back to sequential there. */
574 static int
575 2 parallel_supported(void)
576 {
577 #ifdef _WIN32
578 return 0;
579 #else
580 2 return 1;
581 #endif
582 }
583  
584 /* Build an opts HV from a variadic-style stack pointer. `args` should
585 * be the address of ST(start) at the call site so this function works
586 * outside an XSUB. The HV is mortalised. Caller must have validated
587 * that (count) is even. */
588 static HV *
589 112 build_opts_from_args(pTHX_ SV **args, int count)
590 {
591 112 HV *opts = newHV();
592 112 sv_2mortal((SV *)opts);
593 int i;
594 140 ▶ for (i = 0; i + 1 < count; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < count; i += 2) { 28 112 140
595 STRLEN klen;
596 28 const char *kp = SvPV(args[i], klen);
597 28 hv_store(opts, kp, klen, newSVsv(args[i + 1]), 0);
598 }
599 112 return opts;
600 }
601  
602 /* The shared sequential extract-all loop. Croaks on error; caller is
603 * responsible for handle lifetime via SAVEDESTRUCTOR_X. */
604 static void
605 10 do_extract_all_seq(pTHX_ archive_handle_t *h,
606 const char *dest, size_t dest_len,
607 int apply_xattrs, int unsafe_paths,
608 SV *filter_sv)
609 {
610 char path_buf[ARCHIVE_PATH_MAX];
611 char skip_buf[16 * 1024];
612 int has_filter;
613  
614 10 ▶ if (!h || h->is_writer || h->closed)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->is_writer || h->closed) 10 0 10
if (!h || h->is_writer || h->closed) 10 0 10
if (!h || h->is_writer || h->closed) 0 10 10
615 0 croak("File::Raw::Archive::extract_all: invalid handle");
616 10 ▶ if (dest_len == 0 || dest_len >= sizeof path_buf - 2)
Branch: 2 branches
BranchTrueFalseTotal
if (dest_len == 0 || dest_len >= sizeof path_buf - 2) 10 0 10
if (dest_len == 0 || dest_len >= sizeof path_buf - 2) 0 10 10
617 0 croak("File::Raw::Archive::extract_all: bad dest length");
618  
619 10 ▶ has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) &&
Branch: 3 branches
BranchTrueFalseTotal
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 10 10
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 0 0
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 0 0
620 0 ▶ SvTYPE(SvRV(filter_sv)) == SVt_PVCV);
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(filter_sv)) == SVt_PVCV); 0 0 0
621  
622 10 ▶ if (archive_mkpath(dest, 0755) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_mkpath(dest, 0755) < 0) { 0 10 10
623 0 croak("File::Raw::Archive::extract_all: cannot create dest '%s': %s",
624 dest, strerror(errno));
625 }
626  
627 85 for (;;) {
628 ArchiveEntry e;
629 95 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
630 95 ▶ if (rc < 0) croak("File::Raw::Archive: malformed archive");
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) croak("File::Raw::Archive: malformed archive"); 0 95 95
631 95 ▶ if (rc == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) break; 8 87 95
632  
633 87 ▶ if (!unsafe_paths) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe_paths) { 87 0 87
634 87 ▶ if (!archive_path_is_safe(e.name, e.name_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!archive_path_is_safe(e.name, e.name_len)) { 2 85 87
635 2 croak("File::Raw::Archive::extract_all: refusing unsafe path '%.*s'",
636 (int)e.name_len, e.name);
637 }
638 }
639  
640 85 ▶ if (has_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_filter) { 0 85 85
641 0 int keep = 1;
642 0 dSP;
643 0 ENTER;
644 0 SAVETMPS;
645 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
646 0 SV *entry_rv = entry_to_av(aTHX_ &e);
647 0 sv_2mortal(entry_rv);
648 0 ▶ XPUSHs(entry_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(entry_rv); 0 0 0
649 0 PUTBACK;
650 0 int count = call_sv(filter_sv, G_SCALAR);
651 0 SPAGAIN;
652 0 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 0 0 0
653 0 SV *r = POPs;
654 0 keep = SvTRUE(r) ? 1 : 0;
655 }
656 0 PUTBACK;
657 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
658 0 LEAVE;
659 0 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 0 0 0
660 int n;
661 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
662 0 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 0 0
663 0 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 0 0
664 0 continue;
665 }
666 }
667  
668 85 ▶ if (dest_len + 1 + e.name_len + 1 > sizeof path_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (dest_len + 1 + e.name_len + 1 > sizeof path_buf) { 0 85 85
669 0 croak("File::Raw::Archive::extract_all: path too long for '%.*s'",
670 (int)e.name_len, e.name);
671 }
672 85 memcpy(path_buf, dest, dest_len);
673 85 path_buf[dest_len] = '/';
674 85 memcpy(path_buf + dest_len + 1, e.name, e.name_len);
675 85 path_buf[dest_len + 1 + e.name_len] = '\0';
676  
677 85 switch (e.type) {
678 2 case AE_DIR:
679 2 ▶ if (archive_extract_dir(path_buf, e.mode) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_dir(path_buf, e.mode) < 0) { 0 2 2
680 0 croak("File::Raw::Archive::extract_all: mkdir '%s': %s",
681 path_buf, strerror(errno));
682 }
683 2 break;
684 2 case AE_SYMLINK: {
685 2 ▶ const char *target = e.link_target ? e.link_target : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *target = e.link_target ? e.link_target : ""; 2 0 2
686 2 ▶ if (archive_extract_symlink(path_buf, target) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_symlink(path_buf, target) < 0) { 0 2 2
687 0 warn("File::Raw::Archive: symlink failed for %s: %s",
688 path_buf, strerror(errno));
689 }
690 int n;
691 2 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
692 2 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 2 2
693 2 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 2 2
694 2 break;
695 }
696 81 case AE_FILE: {
697 81 ▶ if (archive_make_parent_dir(path_buf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_make_parent_dir(path_buf) < 0) { 0 81 81
698 0 croak("File::Raw::Archive::extract_all: mkpath parent '%s': %s",
699 path_buf, strerror(errno));
700 }
701 81 const char *errstage = NULL;
702 81 ▶ if (archive_extract_entry(aTHX_ h->plugin, h->cursor, &e,
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_entry(aTHX_ h->plugin, h->cursor, &e, 0 81 81
703 path_buf, apply_xattrs,
704 &errstage) < 0) {
705 0 ▶ croak("File::Raw::Archive::extract_all: %s '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::Archive::extract_all: %s '%s': %s", 0 0 0
706 errstage ? errstage : "?", path_buf, strerror(errno));
707 }
708 81 break;
709 }
710 0 default: {
711 int n;
712 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
713 0 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 0 0
714 0 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 0 0
715 0 break;
716 }
717 }
718 }
719 8 }
720  
721 /* Shared list helper. Returns RV-AV with one entry per archive entry. */
722 static SV *
723 13 do_list(pTHX_ archive_handle_t *h)
724 {
725 char skip_buf[16 * 1024];
726 AV *rows;
727  
728 13 ▶ if (!h || h->is_writer || h->closed)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->is_writer || h->closed) 13 0 13
if (!h || h->is_writer || h->closed) 13 0 13
if (!h || h->is_writer || h->closed) 0 13 13
729 0 croak("File::Raw::Archive::list: invalid handle");
730  
731 13 rows = newAV();
732 564 for (;;) {
733 ArchiveEntry e;
734 577 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
735 577 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 3 574 577
736 3 SvREFCNT_dec((SV *)rows);
737 3 croak("File::Raw::Archive: malformed archive");
738 }
739 574 ▶ if (rc == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) break; 10 564 574
740  
741 /* list() returns an AoH for friendlier external consumption.
742 * Internal Entry objects use AV slots; this is a snapshot. */
743 564 SV *entry_rv = entry_to_hv_snapshot(aTHX_ &e);
744 564 av_push(rows, entry_rv);
745  
746 int n;
747 1128 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
748 1128 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 564 564 1128
749 564 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 564 564
750 0 SvREFCNT_dec((SV *)rows);
751 0 croak("File::Raw::Archive: read_data failed");
752 }
753 }
754 10 return newRV_noinc((SV *)rows);
755 }
756  
757 /* Shared single-entry extract helper. Returns 1 if found+written, 0
758 * if name not in archive. */
759 static IV
760 5 do_extract_one(pTHX_ archive_handle_t *h,
761 const char *match_name, STRLEN match_len,
762 const char *dest_path, int apply_xattrs)
763 {
764 char skip_buf[16 * 1024];
765  
766 5 ▶ if (!h || h->is_writer || h->closed)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->is_writer || h->closed) 5 0 5
if (!h || h->is_writer || h->closed) 5 0 5
if (!h || h->is_writer || h->closed) 0 5 5
767 0 croak("File::Raw::Archive::extract: invalid handle");
768  
769 8 for (;;) {
770 ArchiveEntry e;
771 13 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
772 13 ▶ if (rc < 0) croak("File::Raw::Archive: malformed archive");
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) croak("File::Raw::Archive: malformed archive"); 0 13 13
773 16 ▶ if (rc == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) return 0; 2 11 13
774  
775 11 ▶ if (e.name_len == match_len
Branch: 1 branch
BranchTrueFalseTotal
if (e.name_len == match_len 4 7 11
776 4 ▶ && memcmp(e.name, match_name, match_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(e.name, match_name, match_len) == 0) { 3 1 4
777 3 ▶ if (e.type == AE_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (e.type == AE_FILE) { 3 0 3
778 3 ▶ if (archive_make_parent_dir(dest_path) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_make_parent_dir(dest_path) < 0) { 0 3 3
779 0 croak("File::Raw::Archive::extract: mkpath parent '%s': %s",
780 dest_path, strerror(errno));
781 }
782 3 const char *errstage = NULL;
783 3 ▶ if (archive_extract_entry(aTHX_ h->plugin, h->cursor, &e,
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_entry(aTHX_ h->plugin, h->cursor, &e, 0 3 3
784 dest_path, apply_xattrs,
785 &errstage) < 0) {
786 0 ▶ croak("File::Raw::Archive::extract: %s '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("File::Raw::Archive::extract: %s '%s': %s", 0 0 0
787 errstage ? errstage : "?", dest_path, strerror(errno));
788 }
789 0 ▶ } else if (e.type == AE_DIR) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e.type == AE_DIR) { 0 0 0
790 0 ▶ if (archive_extract_dir(dest_path, e.mode) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_dir(dest_path, e.mode) < 0) { 0 0 0
791 0 croak("File::Raw::Archive::extract: mkdir '%s': %s",
792 dest_path, strerror(errno));
793 }
794 0 ▶ } else if (e.type == AE_SYMLINK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e.type == AE_SYMLINK) { 0 0 0
795 0 ▶ const char *target = e.link_target ? e.link_target : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *target = e.link_target ? e.link_target : ""; 0 0 0
796 0 ▶ if (archive_extract_symlink(dest_path, target) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_symlink(dest_path, target) < 0) { 0 0 0
797 0 warn("File::Raw::Archive::extract: symlink failed for %s: %s",
798 dest_path, strerror(errno));
799 }
800 }
801 3 return 1;
802 }
803  
804 int n;
805 14 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
806 14 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 6 8 14
807 8 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 8 8
808 }
809 }
810  
811 /* Build a File::Raw::Archive::Entry blessed AV pointing back at the
812 * shared `reader_sv`. Slots: [reader, meta, slurped]. Returns
813 * RV-AV at +1; caller mortalises if needed. */
814 static SV *
815 111 build_entry_obj(pTHX_ SV *reader_sv, SV *meta_rv)
816 {
817 111 AV *entry = newAV();
818 111 av_extend(entry, E_SLOT_COUNT - 1);
819 111 av_store(entry, E_READER, SvREFCNT_inc(reader_sv));
820 111 av_store(entry, E_META, meta_rv); /* takes ownership */
821 /* E_SLURPED stays empty until ->slurp memoises bytes there. */
822 111 SV *obj = newRV_noinc((SV *)entry);
823 111 sv_bless(obj, gv_stashpv("File::Raw::Archive::Entry", GV_ADD));
824 111 return obj;
825 }
826  
827 /* Shared each-iteration helper. Calls cb_sv per entry, optionally
828 * filtered. cb_sv must be a CV ref. The reader_sv is a blessed Reader
829 * hashref (built by the caller) that Entry objects link back to so
830 * `$entry->slurp` / `$entry->_skip` keep working. Croaks on error. */
831 static void
832 4 do_each(pTHX_ archive_handle_t *h, SV *reader_sv,
833 SV *cb_sv, SV *filter_sv)
834 {
835 char skip_buf[16 * 1024];
836 int has_filter;
837  
838 4 ▶ if (!h || h->is_writer || h->closed)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->is_writer || h->closed) 4 0 4
if (!h || h->is_writer || h->closed) 4 0 4
if (!h || h->is_writer || h->closed) 0 4 4
839 0 croak("File::Raw::Archive::each: invalid handle");
840  
841 5 ▶ has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) &&
Branch: 3 branches
BranchTrueFalseTotal
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 1 3 4
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 1 0 1
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 1 0 1
842 1 ▶ SvTYPE(SvRV(filter_sv)) == SVt_PVCV);
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(filter_sv)) == SVt_PVCV); 1 0 1
843  
844 46 for (;;) {
845 ArchiveEntry e;
846 50 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
847 50 ▶ if (rc < 0) croak("File::Raw::Archive: malformed archive");
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) croak("File::Raw::Archive: malformed archive"); 0 50 50
848 50 ▶ if (rc == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) break; 4 46 50
849  
850 46 SV *entry_rv = entry_to_av(aTHX_ &e); /* RV-HV at +1 */
851 46 SV *entry_obj = build_entry_obj(aTHX_ reader_sv, entry_rv);
852  
853 46 ▶ if (has_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_filter) { 40 6 46
854 40 int keep = 1;
855 40 dSP;
856 40 ENTER;
857 40 SAVETMPS;
858 40 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 40 40
859 40 ▶ XPUSHs(sv_2mortal(SvREFCNT_inc(entry_obj)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(SvREFCNT_inc(entry_obj))); 0 40 40
860 40 PUTBACK;
861 40 int count = call_sv(filter_sv, G_SCALAR);
862 40 SPAGAIN;
863 40 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 40 0 40
864 40 SV *r = POPs;
865 40 keep = SvTRUE(r) ? 1 : 0;
866 }
867 40 PUTBACK;
868 40 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 40 0 40
869 40 LEAVE;
870 40 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 20 20 40
871 20 SvREFCNT_dec(entry_obj);
872 int n;
873 40 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
874 40 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 20 20 40
875 20 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 20 20
876 20 continue;
877 }
878 }
879  
880 {
881 26 dSP;
882 26 ENTER;
883 26 SAVETMPS;
884 26 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 26 26
885 26 ▶ XPUSHs(sv_2mortal(SvREFCNT_inc(entry_obj)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(SvREFCNT_inc(entry_obj))); 0 26 26
886 26 PUTBACK;
887 26 call_sv(cb_sv, G_VOID | G_DISCARD);
888 26 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 26 0 26
889 26 LEAVE;
890 }
891  
892 26 SvREFCNT_dec(entry_obj);
893  
894 /* Drain any unread payload bytes so the cursor advances. */
895 int n;
896 52 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
897 52 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 26 26 52
898 26 ▶ if (n < 0) croak("File::Raw::Archive: read_data failed");
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) croak("File::Raw::Archive: read_data failed"); 0 26 26
899 }
900 4 }
901  
902 /* ============================================================
903 * Parallel extract: worker loop body + parent orchestrator
904 * ============================================================
905 *
906 * The parent process iterates the archive, slurps each regular file's
907 * payload, marshals (path, content, metadata, xattrs) into a single
908 * binary record, and writes it to one of N forked children's pipes
909 * round-robin. Each child runs `do_worker_loop` (this same function
910 * body) and writes file/chmod/utime errors back to a shared error
911 * pipe. After the parent finishes dispatch and closes all worker job
912 * pipes (signalling EOF), it drains the error pipe and reaps the
913 * children. Any error → consolidated croak.
914 *
915 * fork(2) inside an XSUB is safe as long as the child path doesn't
916 * unwind back through Perl - we _exit(2) directly so Perl END / DESTROY
917 * blocks don't fire and corrupt shared state. */
918  
919 static void
920 0 do_worker_loop(int job_fd, int err_fd)
921 {
922 ParallelJob job;
923 char errbuf[1024];
924 ArchiveEntry e;
925 0 for (;;) {
926 0 int rc = marshal_read_job(job_fd, &job);
927 0 ▶ if (rc == 1) break; /* clean EOF */
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 1) break; /* clean EOF */ 0 0 0
928 0 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 0 0
929 0 const char *m = "worker: malformed job\n";
930 0 ssize_t _ignored = write(err_fd, m, strlen(m));
931 (void)_ignored;
932 0 break;
933 }
934  
935 0 memset(&e, 0, sizeof e);
936 0 e.name = job.path;
937 0 e.name_len = job.path_len;
938 0 e.size = job.content_len;
939 0 e.mode = job.mode;
940 0 e.mtime = job.mtime;
941 0 e.mtime_ns = job.mtime_ns;
942 0 e.uid = job.uid;
943 0 e.gid = job.gid;
944 0 e.type = AE_FILE;
945 0 e.xattrs = job.xattrs;
946 0 e.xattr_count = job.xattr_count;
947  
948 0 const char *stage = NULL;
949 0 ▶ if (archive_extract_bytes(&e, job.path,
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_bytes(&e, job.path, 0 0 0
950 0 job.content, job.content_len,
951 job.apply_xattrs, &stage) < 0) {
952 0 int n = snprintf(errbuf, sizeof errbuf,
953 "%s: %s: %s\n",
954 0 ▶ job.path, stage ? stage : "?",
Branch: 1 branch
BranchTrueFalseTotal
job.path, stage ? stage : "?", 0 0 0
955 0 strerror(errno));
956 0 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 0 0 0
957 0 ▶ if ((size_t)n > sizeof errbuf) n = (int)sizeof errbuf;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)n > sizeof errbuf) n = (int)sizeof errbuf; 0 0 0
958 0 ssize_t _ignored = write(err_fd, errbuf, (size_t)n);
959 (void)_ignored;
960 }
961 }
962  
963 0 parallel_job_free(&job);
964 }
965 0 }
966  
967 typedef struct {
968 int write_fd;
969 pid_t pid;
970 } parallel_worker_t;
971  
972 static void
973 2 do_extract_all_parallel(pTHX_ archive_handle_t *h,
974 const char *dest, size_t dest_len,
975 int parallel, int apply_xattrs,
976 SV *filter_sv, int unsafe_paths)
977 {
978 char path_buf[ARCHIVE_PATH_MAX];
979 char skip_buf[16 * 1024];
980 2 parallel_worker_t *workers = NULL;
981 2 int err_r = -1, err_w = -1;
982 2 int num_started = 0;
983 int has_filter;
984 2 int dispatch_err = 0;
985 2 char err_msg[512] = "";
986 int err_pipe[2];
987 struct sigaction old_sigpipe, new_sa;
988 2 int i, rr = 0;
989  
990 2 ▶ if (!h || h->is_writer || h->closed)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->is_writer || h->closed) 2 0 2
if (!h || h->is_writer || h->closed) 2 0 2
if (!h || h->is_writer || h->closed) 0 2 2
991 0 croak("File::Raw::Archive::extract_all: invalid handle");
992 2 ▶ if (dest_len == 0 || dest_len >= sizeof path_buf - 2)
Branch: 2 branches
BranchTrueFalseTotal
if (dest_len == 0 || dest_len >= sizeof path_buf - 2) 2 0 2
if (dest_len == 0 || dest_len >= sizeof path_buf - 2) 0 2 2
993 0 croak("File::Raw::Archive::extract_all: bad dest length");
994 2 ▶ if (parallel < 1) parallel = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (parallel < 1) parallel = 1; 0 2 2
995  
996 2 ▶ has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) &&
Branch: 3 branches
BranchTrueFalseTotal
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 2 2
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 0 0
has_filter = (filter_sv && SvOK(filter_sv) && SvROK(filter_sv) && 0 0 0
997 0 ▶ SvTYPE(SvRV(filter_sv)) == SVt_PVCV);
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(filter_sv)) == SVt_PVCV); 0 0 0
998  
999 2 ▶ if (archive_mkpath(dest, 0755) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_mkpath(dest, 0755) < 0) { 0 2 2
1000 0 croak("File::Raw::Archive::extract_all: cannot create dest '%s': %s",
1001 dest, strerror(errno));
1002 }
1003  
1004 2 ▶ if (pipe(err_pipe) < 0) croak("pipe: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(err_pipe) < 0) croak("pipe: %s", strerror(errno)); 0 2 2
1005 2 err_r = err_pipe[0];
1006 2 err_w = err_pipe[1];
1007  
1008 2 workers = (parallel_worker_t *)calloc(parallel, sizeof *workers);
1009 2 ▶ if (!workers) {
Branch: 1 branch
BranchTrueFalseTotal
if (!workers) { 0 2 2
1010 0 close(err_r); close(err_w);
1011 0 croak("File::Raw::Archive: out of memory");
1012 }
1013  
1014 /* Ignore SIGPIPE while workers are alive. A worker crash would
1015 * otherwise SIGPIPE the parent on the next dispatch write. */
1016 2 new_sa.sa_handler = SIG_IGN;
1017 2 sigemptyset(&new_sa.sa_mask);
1018 2 new_sa.sa_flags = 0;
1019 2 sigaction(SIGPIPE, &new_sa, &old_sigpipe);
1020  
1021 8 ▶ for (i = 0; i < parallel; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < parallel; i++) { 6 2 8
1022 int job_pipe[2];
1023 6 ▶ if (pipe(job_pipe) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(job_pipe) < 0) { 0 6 6
1024 0 int saved = errno;
1025 int j;
1026 0 ▶ for (j = 0; j < num_started; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_started; j++) { 0 0 0
1027 0 close(workers[j].write_fd);
1028 0 kill(workers[j].pid, SIGTERM);
1029 0 waitpid(workers[j].pid, NULL, 0);
1030 }
1031 0 close(err_r); close(err_w);
1032 0 free(workers);
1033 0 sigaction(SIGPIPE, &old_sigpipe, NULL);
1034 0 croak("File::Raw::Archive: pipe: %s", strerror(saved));
1035 }
1036 6 pid_t pid = fork();
1037 6 ▶ if (pid < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid < 0) { 0 6 6
1038 0 int saved = errno;
1039 int j;
1040 0 close(job_pipe[0]); close(job_pipe[1]);
1041 0 ▶ for (j = 0; j < num_started; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_started; j++) { 0 0 0
1042 0 close(workers[j].write_fd);
1043 0 kill(workers[j].pid, SIGTERM);
1044 0 waitpid(workers[j].pid, NULL, 0);
1045 }
1046 0 close(err_r); close(err_w);
1047 0 free(workers);
1048 0 sigaction(SIGPIPE, &old_sigpipe, NULL);
1049 0 croak("File::Raw::Archive: fork: %s", strerror(saved));
1050 }
1051 6 ▶ if (pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid == 0) { 0 6 6
1052 /* Child: keep its job-read fd and the shared err-write fd;
1053 * everything else inherited from the parent gets closed. */
1054 int j;
1055 0 close(job_pipe[1]);
1056 0 close(err_r);
1057 0 ▶ for (j = 0; j < num_started; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_started; j++) { 0 0 0
1058 0 close(workers[j].write_fd);
1059 }
1060 0 do_worker_loop(job_pipe[0], err_w);
1061 0 close(job_pipe[0]);
1062 0 close(err_w);
1063 0 _exit(0);
1064 }
1065 /* Parent. */
1066 6 close(job_pipe[0]);
1067 6 workers[num_started].write_fd = job_pipe[1];
1068 6 workers[num_started].pid = pid;
1069 6 num_started++;
1070 }
1071 2 close(err_w); err_w = -1;
1072  
1073 /* Dispatch loop. */
1074 61 for (;;) {
1075 ArchiveEntry e;
1076 63 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
1077 63 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 63 63
1078 0 snprintf(err_msg, sizeof err_msg, "malformed archive");
1079 0 dispatch_err = 1;
1080 0 break;
1081 }
1082 63 ▶ if (rc == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) break; 2 61 63
1083  
1084 61 ▶ if (!unsafe_paths) {
Branch: 1 branch
BranchTrueFalseTotal
if (!unsafe_paths) { 61 0 61
1085 61 ▶ if (!archive_path_is_safe(e.name, e.name_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!archive_path_is_safe(e.name, e.name_len)) { 0 61 61
1086 0 snprintf(err_msg, sizeof err_msg,
1087 "refusing unsafe path '%.*s'",
1088 0 (int)e.name_len, e.name);
1089 0 dispatch_err = 1;
1090 0 break;
1091 }
1092 }
1093  
1094 61 ▶ if (has_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_filter) { 0 61 61
1095 0 int keep = 1;
1096 0 dSP;
1097 0 ENTER;
1098 0 SAVETMPS;
1099 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1100 0 SV *entry_rv = entry_to_av(aTHX_ &e);
1101 0 sv_2mortal(entry_rv);
1102 0 ▶ XPUSHs(entry_rv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(entry_rv); 0 0 0
1103 0 PUTBACK;
1104 0 int count = call_sv(filter_sv, G_SCALAR);
1105 0 SPAGAIN;
1106 0 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 0 0 0
1107 0 SV *r = POPs;
1108 0 keep = SvTRUE(r) ? 1 : 0;
1109 }
1110 0 PUTBACK;
1111 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1112 0 LEAVE;
1113 0 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 0 0 0
1114 int n;
1115 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
1116 0 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 0 0
1117 0 ▶ if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; } 0 0 0
1118 0 continue;
1119 }
1120 }
1121  
1122 61 ▶ if (dest_len + 1 + e.name_len + 1 > sizeof path_buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (dest_len + 1 + e.name_len + 1 > sizeof path_buf) { 0 61 61
1123 0 snprintf(err_msg, sizeof err_msg,
1124 0 "path too long for '%.*s'", (int)e.name_len, e.name);
1125 0 dispatch_err = 1;
1126 0 break;
1127 }
1128 61 memcpy(path_buf, dest, dest_len);
1129 61 path_buf[dest_len] = '/';
1130 61 memcpy(path_buf + dest_len + 1, e.name, e.name_len);
1131 61 path_buf[dest_len + 1 + e.name_len] = '\0';
1132  
1133 61 ▶ if (e.type == AE_DIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (e.type == AE_DIR) { 0 61 61
1134 0 ▶ if (archive_extract_dir(path_buf, e.mode) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_dir(path_buf, e.mode) < 0) { 0 0 0
1135 0 snprintf(err_msg, sizeof err_msg,
1136 0 "mkdir '%s': %s", path_buf, strerror(errno));
1137 0 dispatch_err = 1;
1138 0 break;
1139 }
1140 0 continue;
1141 }
1142 61 ▶ if (e.type == AE_SYMLINK) {
Branch: 1 branch
BranchTrueFalseTotal
if (e.type == AE_SYMLINK) { 0 61 61
1143 0 ▶ const char *target = e.link_target ? e.link_target : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *target = e.link_target ? e.link_target : ""; 0 0 0
1144 0 ▶ if (archive_extract_symlink(path_buf, target) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_extract_symlink(path_buf, target) < 0) { 0 0 0
1145 0 warn("File::Raw::Archive: symlink failed for %s: %s",
1146 path_buf, strerror(errno));
1147 }
1148 int n;
1149 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
1150 0 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 0 0
1151 0 ▶ if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; } 0 0 0
1152 0 continue;
1153 }
1154 61 ▶ if (e.type != AE_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (e.type != AE_FILE) { 0 61 61
1155 int n;
1156 0 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
1157 0 ▶ skip_buf, sizeof skip_buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
skip_buf, sizeof skip_buf)) > 0) {} 0 0 0
1158 0 ▶ if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { snprintf(err_msg, sizeof err_msg, "read_data failed"); dispatch_err = 1; break; } 0 0 0
1159 0 continue;
1160 }
1161  
1162 /* Regular file: ensure parent dir exists, slurp content, marshal, dispatch. */
1163 61 ▶ if (archive_make_parent_dir(path_buf) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_make_parent_dir(path_buf) < 0) { 0 61 61
1164 0 snprintf(err_msg, sizeof err_msg,
1165 0 "mkpath parent '%s': %s", path_buf, strerror(errno));
1166 0 dispatch_err = 1;
1167 0 break;
1168 }
1169  
1170 61 char *content = NULL;
1171 61 size_t content_cap = 0, content_len = 0;
1172 61 int read_err = 0;
1173 61 for (;;) {
1174 122 ▶ if (content_len + 16384 > content_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (content_len + 16384 > content_cap) { 61 61 122
1175 61 ▶ size_t new_cap = content_cap ? content_cap * 2 : 65536;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = content_cap ? content_cap * 2 : 65536; 0 61 61
1176 61 ▶ while (new_cap < content_len + 16384) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < content_len + 16384) new_cap *= 2; 0 61 61
1177 61 char *p = (char *)realloc(content, new_cap);
1178 61 ▶ if (!p) { read_err = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { read_err = 1; break; } 0 61 61
1179 61 content = p;
1180 61 content_cap = new_cap;
1181 }
1182 122 int n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
1183 content + content_len, 16384);
1184 122 ▶ if (n < 0) { read_err = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { read_err = 1; break; } 0 122 122
1185 122 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 61 61 122
1186 61 content_len += (size_t)n;
1187 }
1188 61 ▶ if (read_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_err) { 0 61 61
1189 0 free(content);
1190 0 snprintf(err_msg, sizeof err_msg, "read_data failed for '%s'", path_buf);
1191 0 dispatch_err = 1;
1192 0 break;
1193 }
1194  
1195 61 char *payload = NULL;
1196 61 size_t payload_len = 0;
1197 61 ▶ if (marshal_job(path_buf, strlen(path_buf),
Branch: 1 branch
BranchTrueFalseTotal
if (marshal_job(path_buf, strlen(path_buf), 0 61 61
1198 content, content_len,
1199 e.mode,
1200 e.mtime, e.mtime_ns,
1201 e.uid, e.gid,
1202 apply_xattrs,
1203 e.xattrs, e.xattr_count,
1204 &payload, &payload_len) < 0) {
1205 0 free(content);
1206 0 snprintf(err_msg, sizeof err_msg, "marshal_job: out of memory");
1207 0 dispatch_err = 1;
1208 0 break;
1209 }
1210 61 free(content);
1211  
1212 61 ▶ if (marshal_send(workers[rr].write_fd, payload, payload_len) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (marshal_send(workers[rr].write_fd, payload, payload_len) < 0) { 0 61 61
1213 0 int saved = errno;
1214 0 free(payload);
1215 0 snprintf(err_msg, sizeof err_msg,
1216 "pipe write to worker %d: %s", rr, strerror(saved));
1217 0 dispatch_err = 1;
1218 0 break;
1219 }
1220 61 free(payload);
1221 61 rr = (rr + 1) % parallel;
1222 }
1223  
1224 /* Close all worker write fds (signals EOF). */
1225 8 ▶ for (i = 0; i < num_started; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_started; i++) { 6 2 8
1226 6 ▶ if (workers[i].write_fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (workers[i].write_fd >= 0) { 6 0 6
1227 6 close(workers[i].write_fd);
1228 6 workers[i].write_fd = -1;
1229 }
1230 }
1231  
1232 /* Drain error pipe (workers' per-job errors). */
1233 2 SV *errors_sv = sv_2mortal(newSVpvs(""));
1234 {
1235 char buf[4096];
1236 1 for (;;) {
1237 3 ssize_t n = read(err_r, buf, sizeof buf);
1238 3 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 3 3
1239 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1240 0 break;
1241 }
1242 3 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 2 1 3
1243 1 sv_catpvn(errors_sv, buf, (STRLEN)n);
1244 }
1245 }
1246 2 close(err_r);
1247  
1248 /* Reap. */
1249 8 ▶ for (i = 0; i < num_started; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_started; i++) { 6 2 8
1250 6 waitpid(workers[i].pid, NULL, 0);
1251 }
1252 2 free(workers);
1253  
1254 /* Restore SIGPIPE handler. */
1255 2 sigaction(SIGPIPE, &old_sigpipe, NULL);
1256  
1257 2 ▶ if (dispatch_err) {
Branch: 1 branch
BranchTrueFalseTotal
if (dispatch_err) { 0 2 2
1258 0 croak("File::Raw::Archive::extract_all: %s", err_msg);
1259 }
1260 STRLEN err_len;
1261 2 const char *err_pv = SvPV(errors_sv, err_len);
1262 2 ▶ if (err_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (err_len > 0) { 1 1 2
1263 1 croak("File::Raw::Archive::extract_all: errors during parallel extract:\n %.*s",
1264 (int)err_len, err_pv);
1265 }
1266 1 }
1267  
1268 /* ============================================================
1269 * Custom-op accessor infrastructure (Entry methods)
1270 * ============================================================
1271 *
1272 * `$entry->name`, `->size`, `->is_dir` etc. are normally method
1273 * dispatches: ENTERSUB walks @ISA, finds our XSUB, sets up a CALL
1274 * frame, runs the XSUB body, returns. We can do better at compile
1275 * time by replacing the ENTERSUB op with a custom op that jumps
1276 * straight to a lightweight pp_* handler. The handler reads the
1277 * entry's slot directly and returns - no @ISA walk, no method-cache
1278 * touch, no XSUB call frame.
1279 *
1280 * Mechanism (Object::Proto pattern):
1281 * - Define one pp_entry_accessor handler that uses op_targ to
1282 * know which Meta slot to read (M_NAME .. M_IS_SPARSE).
1283 * - Define one pp_entry_predicate for is_file / is_dir / etc.
1284 * - At BOOT, register a call checker on each accessor's CV.
1285 * The checker walks the op tree at compile time and rewrites
1286 * ENTERSUB to OP_CUSTOM with op_ppaddr / op_targ set.
1287 * - The checker bails (returns the original entersubop unchanged)
1288 * on call forms it can't handle - dynamic method names,
1289 * stashed coderefs, extra args - so the XSUB still runs.
1290 *
1291 * Skipped on perl < 5.14 (cv_set_call_checker not available);
1292 * the XSUB methods stay correct on those builds, just slower.
1293 */
1294  
1295 #if PERL_VERSION_GE(5,14,0)
1296  
1297 static XOP entry_accessor_xop;
1298 static XOP entry_predicate_xop;
1299  
1300 static OP *
1301 0 pp_entry_accessor(pTHX)
1302 {
1303 0 dSP;
1304 0 SV *self_sv = POPs;
1305 0 int slot = (int)PL_op->op_targ;
1306 0 SV *result = &PL_sv_undef;
1307  
1308 0 ▶ if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) { 0 0 0
if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) { 0 0 0
1309 0 AV *self = (AV *)SvRV(self_sv);
1310 0 SV **meta_ref = av_fetch(self, E_META, 0);
1311 0 ▶ if (meta_ref && *meta_ref && SvROK(*meta_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
1312 0 ▶ && SvTYPE(SvRV(*meta_ref)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*meta_ref)) == SVt_PVAV) { 0 0 0
1313 0 AV *meta = (AV *)SvRV(*meta_ref);
1314 0 SV **val = av_fetch(meta, slot, 0);
1315 0 ▶ if (val && *val && SvOK(*val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (val && *val && SvOK(*val)) { 0 0 0
if (val && *val && SvOK(*val)) { 0 0 0
if (val && *val && SvOK(*val)) { 0 0 0
1316 0 result = sv_2mortal(SvREFCNT_inc(*val));
1317 } else {
1318 /* Numeric defaults for the integer-typed slots, to
1319 * match the XSUB accessor behaviour. */
1320 0 ▶ switch (slot) {
Branch: 1 branch
BranchTrueFalseTotal
switch (slot) { 0 0 0
1321 0 case M_SIZE: case M_MODE: case M_MTIME: case M_MTIME_NS:
1322 case M_UID: case M_GID: case M_TYPE: case M_IS_SPARSE:
1323 0 result = sv_2mortal(newSViv(0));
1324 0 break;
1325 0 default:
1326 /* string-typed slots: stay undef. */
1327 0 break;
1328 }
1329 }
1330 }
1331 }
1332  
1333 0 ▶ XPUSHs(result);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result); 0 0 0
1334 0 RETURN;
1335 }
1336  
1337 static OP *
1338 0 pp_entry_predicate(pTHX)
1339 {
1340 0 dSP;
1341 0 SV *self_sv = POPs;
1342 0 int kind = (int)PL_op->op_targ;
1343 0 IV t = (IV)AE_FILE;
1344 0 int rc = 0;
1345  
1346 0 ▶ if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) { 0 0 0
if (SvROK(self_sv) && SvTYPE(SvRV(self_sv)) == SVt_PVAV) { 0 0 0
1347 0 AV *self = (AV *)SvRV(self_sv);
1348 0 SV **meta_ref = av_fetch(self, E_META, 0);
1349 0 ▶ if (meta_ref && *meta_ref && SvROK(*meta_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
if (meta_ref && *meta_ref && SvROK(*meta_ref) 0 0 0
1350 0 ▶ && SvTYPE(SvRV(*meta_ref)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*meta_ref)) == SVt_PVAV) { 0 0 0
1351 0 AV *meta = (AV *)SvRV(*meta_ref);
1352 0 SV **type_ref = av_fetch(meta, M_TYPE, 0);
1353 0 ▶ if (type_ref && *type_ref && SvOK(*type_ref))
Branch: 3 branches
BranchTrueFalseTotal
if (type_ref && *type_ref && SvOK(*type_ref)) 0 0 0
if (type_ref && *type_ref && SvOK(*type_ref)) 0 0 0
if (type_ref && *type_ref && SvOK(*type_ref)) 0 0 0
1354 0 t = SvIV(*type_ref);
1355 }
1356 }
1357  
1358 0 switch (kind) {
1359 0 case 1: rc = (t == AE_FILE); break;
1360 0 case 2: rc = (t == AE_DIR); break;
1361 0 case 3: rc = (t == AE_SYMLINK); break;
1362 0 ▶ case 4: rc = (t == AE_SYMLINK || t == AE_HARDLINK); break;
Branch: 2 branches
BranchTrueFalseTotal
case 4: rc = (t == AE_SYMLINK || t == AE_HARDLINK); break; 0 0 0
case 4: rc = (t == AE_SYMLINK || t == AE_HARDLINK); break; 0 0 0
1363 }
1364 0 ▶ XPUSHs(rc ? &PL_sv_yes : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(rc ? &PL_sv_yes : &PL_sv_no); 0 0 0
XPUSHs(rc ? &PL_sv_yes : &PL_sv_no); 0 0 0
1365 0 RETURN;
1366 }
1367  
1368 /* Call checker shared by accessors and predicates. ckobj packs the
1369 * slot/kind in the high bits and a 1-bit "predicate?" flag in the low
1370 * bit so the same checker function can dispatch to either pp_*. */
1371 static OP *
1372 0 entry_method_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
1373 {
1374 OP *pushop, *self_op, *cv_op, *newop;
1375 IV packed;
1376 int slot_or_kind;
1377 int is_predicate;
1378  
1379 PERL_UNUSED_ARG(namegv);
1380  
1381 0 packed = SvIV(ckobj);
1382 0 is_predicate = (int)(packed & 1);
1383 0 slot_or_kind = (int)(packed >> 1);
1384  
1385 /* Walk: entersub -> pushmark -> invocant -> &method_cv. We require
1386 * exactly one invocant (no extra args), otherwise bail. */
1387 0 pushop = cUNOPx(entersubop)->op_first;
1388 0 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 0 0 0
1389 0 pushop = cUNOPx(pushop)->op_first;
1390 }
1391 0 ▶ self_op = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
self_op = OpSIBLING(pushop); 0 0 0
1392 0 ▶ if (!self_op) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!self_op) return entersubop; 0 0 0
1393 0 ▶ cv_op = OpSIBLING(self_op);
Branch: 1 branch
BranchTrueFalseTotal
cv_op = OpSIBLING(self_op); 0 0 0
1394 0 ▶ if (!cv_op) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!cv_op) return entersubop; 0 0 0
1395 0 ▶ if (OpSIBLING(cv_op)) return entersubop; /* extra args */
Branch: 2 branches
BranchTrueFalseTotal
if (OpSIBLING(cv_op)) return entersubop; /* extra args */ 0 0 0
if (OpSIBLING(cv_op)) return entersubop; /* extra args */ 0 0 0
1396  
1397 /* Detach self_op from the entersub tree. */
1398 0 OpMORESIB_set(pushop, cv_op);
1399 0 OpLASTSIB_set(self_op, NULL);
1400 0 self_op = op_contextualize(self_op, G_SCALAR);
1401  
1402 /* Build OP_NULL first (avoids -DDEBUGGING newUNOP assert), then
1403 * convert to OP_CUSTOM and set op_ppaddr / op_targ. */
1404 0 newop = newUNOP(OP_NULL, 0, self_op);
1405 0 newop->op_type = OP_CUSTOM;
1406 0 ▶ newop->op_ppaddr = is_predicate ? pp_entry_predicate : pp_entry_accessor;
Branch: 1 branch
BranchTrueFalseTotal
newop->op_ppaddr = is_predicate ? pp_entry_predicate : pp_entry_accessor; 0 0 0
1407 0 newop->op_targ = (PADOFFSET)slot_or_kind;
1408  
1409 0 op_free(entersubop);
1410 0 return newop;
1411 }
1412  
1413 static void
1414 435 install_entry_method_checker(pTHX_ const char *method,
1415 int slot_or_kind, int is_predicate)
1416 {
1417 char full[128];
1418 435 snprintf(full, sizeof full,
1419 "File::Raw::Archive::Entry::%s", method);
1420 435 CV *cv = get_cv(full, 0);
1421 435 ▶ if (!cv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) return; 0 435 435
1422 435 ▶ SV *ckobj = newSViv(((IV)slot_or_kind << 1)
Branch: 1 branch
BranchTrueFalseTotal
SV *ckobj = newSViv(((IV)slot_or_kind << 1) 116 319 435
1423 | (is_predicate ? 1 : 0));
1424 435 cv_set_call_checker(cv, entry_method_checker, ckobj);
1425 }
1426  
1427 #endif /* PERL_VERSION_GE(5,14,0) */
1428  
1429 /* ============================================================ */
1430  
1431 MODULE = File::Raw::Archive PACKAGE = File::Raw::Archive
1432  
1433 PROTOTYPES: DISABLE
1434  
1435 BOOT:
1436 29 archive_register_plugin(aTHX_ &tar_plugin);
1437 {
1438 29 HV *stash = gv_stashpv("File::Raw::Archive", GV_ADD);
1439 29 newCONSTSUB(stash, "AE_FILE", newSViv(AE_FILE));
1440 29 newCONSTSUB(stash, "AE_DIR", newSViv(AE_DIR));
1441 29 newCONSTSUB(stash, "AE_SYMLINK", newSViv(AE_SYMLINK));
1442 29 newCONSTSUB(stash, "AE_HARDLINK", newSViv(AE_HARDLINK));
1443 29 newCONSTSUB(stash, "AE_FIFO", newSViv(AE_FIFO));
1444 29 newCONSTSUB(stash, "AE_CHAR", newSViv(AE_CHAR));
1445 29 newCONSTSUB(stash, "AE_BLOCK", newSViv(AE_BLOCK));
1446 29 newCONSTSUB(stash, "AE_OTHER", newSViv(AE_OTHER));
1447 }
1448 #if PERL_VERSION_GE(5,14,0)
1449 /* Register custom-op replacements for Entry method dispatch.
1450 * After this, `$entry->name` etc. compiles into a direct AV-slot
1451 * lookup with no @ISA walk and no XSUB call frame. */
1452 29 XopENTRY_set(&entry_accessor_xop, xop_name, "entry_accessor");
1453 29 XopENTRY_set(&entry_accessor_xop, xop_desc, "Entry method accessor");
1454 29 XopENTRY_set(&entry_accessor_xop, xop_class, OA_UNOP);
1455 29 Perl_custom_op_register(aTHX_ pp_entry_accessor, &entry_accessor_xop);
1456  
1457 29 XopENTRY_set(&entry_predicate_xop, xop_name, "entry_predicate");
1458 29 XopENTRY_set(&entry_predicate_xop, xop_desc, "Entry type predicate");
1459 29 XopENTRY_set(&entry_predicate_xop, xop_class, OA_UNOP);
1460 29 Perl_custom_op_register(aTHX_ pp_entry_predicate, &entry_predicate_xop);
1461  
1462 /* Slot accessors. */
1463 29 install_entry_method_checker(aTHX_ "name", M_NAME, 0);
1464 29 install_entry_method_checker(aTHX_ "size", M_SIZE, 0);
1465 29 install_entry_method_checker(aTHX_ "mode", M_MODE, 0);
1466 29 install_entry_method_checker(aTHX_ "mtime", M_MTIME, 0);
1467 29 install_entry_method_checker(aTHX_ "mtime_ns", M_MTIME_NS, 0);
1468 29 install_entry_method_checker(aTHX_ "uid", M_UID, 0);
1469 29 install_entry_method_checker(aTHX_ "gid", M_GID, 0);
1470 29 install_entry_method_checker(aTHX_ "type", M_TYPE, 0);
1471 29 install_entry_method_checker(aTHX_ "link_target", M_LINK_TARGET, 0);
1472 29 install_entry_method_checker(aTHX_ "xattrs", M_XATTRS, 0);
1473 29 install_entry_method_checker(aTHX_ "is_sparse", M_IS_SPARSE, 0);
1474  
1475 /* Type predicates: kind=1..4 keyed to the switch in pp_entry_predicate. */
1476 29 install_entry_method_checker(aTHX_ "is_file", 1, 1);
1477 29 install_entry_method_checker(aTHX_ "is_dir", 2, 1);
1478 29 install_entry_method_checker(aTHX_ "is_symlink", 3, 1);
1479 29 install_entry_method_checker(aTHX_ "is_link", 4, 1);
1480 #endif
1481  
1482 SV *
1483 _open_read(path_sv, opts_sv)
1484 SV *path_sv
1485 SV *opts_sv
1486 PREINIT:
1487 0 HV *opts = NULL;
1488 0 archive_handle_t *h = NULL;
1489 int gz, fd;
1490 const ArchivePlugin *plugin;
1491 archive_pull_fn pull;
1492 void *src;
1493 CODE:
1494 0 ▶ if (!SvOK(path_sv)) croak("File::Raw::Archive::_open_read: undef path");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(path_sv)) croak("File::Raw::Archive::_open_read: undef path"); 0 0 0
1495 0 ▶ if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 0 0 0
if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 0 0 0
1496 0 opts = (HV *)SvRV(opts_sv);
1497  
1498 0 fd = open(SvPV_nolen(path_sv), O_RDONLY);
1499 0 ▶ if (fd < 0) croak("File::Raw::Archive: cannot open '%s': %s",
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) croak("File::Raw::Archive: cannot open '%s': %s", 0 0 0
1500 SvPV_nolen(path_sv), strerror(errno));
1501  
1502 0 gz = resolve_compression(aTHX_ fd, opts);
1503 0 plugin = resolve_plugin(aTHX_ opts, fd, gz);
1504 0 ▶ if (!plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if (!plugin) { 0 0 0
1505 0 close(fd);
1506 0 croak("File::Raw::Archive: no plugin selected (auto-detection failed)");
1507 }
1508  
1509 0 h = make_handle();
1510 0 ▶ if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); } 0 0 0
1511 0 h->fd = fd;
1512 0 h->plugin = plugin;
1513  
1514 0 ▶ if (gz) {
Branch: 1 branch
BranchTrueFalseTotal
if (gz) { 0 0 0
1515 0 h->gz_src = archive_gz_src_new(fd, ARCHIVE_DEFAULT_CHUNK);
1516 0 ▶ if (!h->gz_src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!h->gz_src) { 0 0 0
1517 0 free_handle(aTHX_ h);
1518 0 croak("File::Raw::Archive: gzip stream init failed");
1519 }
1520 0 pull = archive_pull_gz;
1521 0 src = h->gz_src;
1522 } else {
1523 0 h->fd_state.fd = fd;
1524 0 pull = archive_pull_fd;
1525 0 src = &h->fd_state;
1526 }
1527  
1528 0 ▶ if (plugin->read_open(aTHX_ plugin, pull, src, opts, &h->cursor) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plugin->read_open(aTHX_ plugin, pull, src, opts, &h->cursor) < 0) { 0 0 0
1529 0 free_handle(aTHX_ h);
1530 0 croak("File::Raw::Archive: read_open failed");
1531 }
1532  
1533 0 RETVAL = new_handle_obj(aTHX_ h, "File::Raw::Archive::Reader");
1534 OUTPUT:
1535 RETVAL
1536  
1537 SV *
1538 _read_next(handle_sv)
1539 SV *handle_sv
1540 PREINIT:
1541 archive_handle_t *h;
1542 ArchiveEntry e;
1543 int rc;
1544 CODE:
1545 0 h = unwrap_handle(aTHX_ handle_sv);
1546 0 ▶ if (!h || h->closed || h->is_writer)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->closed || h->is_writer) 0 0 0
if (!h || h->closed || h->is_writer) 0 0 0
if (!h || h->closed || h->is_writer) 0 0 0
1547 0 croak("File::Raw::Archive::_read_next: invalid handle");
1548 0 rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
1549 0 ▶ if (rc < 0) croak("File::Raw::Archive: malformed archive");
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) croak("File::Raw::Archive: malformed archive"); 0 0 0
1550 0 ▶ if (rc == 0) RETVAL = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) RETVAL = &PL_sv_undef; 0 0 0
1551 0 else RETVAL = entry_to_av(aTHX_ &e);
1552 OUTPUT:
1553 RETVAL
1554  
1555 SV *
1556 _read_data(handle_sv, max_bytes_sv = NULL)
1557 SV *handle_sv
1558 SV *max_bytes_sv
1559 PREINIT:
1560 archive_handle_t *h;
1561 0 ▶ UV want = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV want = 0; 0 0 0
1562 SV *out;
1563 char buf[64 * 1024];
1564 int n;
1565 CODE:
1566 0 h = unwrap_handle(aTHX_ handle_sv);
1567 0 ▶ if (!h || h->closed || h->is_writer)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->closed || h->is_writer) 0 0 0
if (!h || h->closed || h->is_writer) 0 0 0
if (!h || h->closed || h->is_writer) 0 0 0
1568 0 croak("File::Raw::Archive::_read_data: invalid handle");
1569 0 ▶ if (max_bytes_sv && SvOK(max_bytes_sv)) want = SvUV(max_bytes_sv);
Branch: 2 branches
BranchTrueFalseTotal
if (max_bytes_sv && SvOK(max_bytes_sv)) want = SvUV(max_bytes_sv); 0 0 0
if (max_bytes_sv && SvOK(max_bytes_sv)) want = SvUV(max_bytes_sv); 0 0 0
1570 0 out = newSVpvn("", 0);
1571 /* If `want` set, read exactly that many. Otherwise drain the entry. */
1572 0 while (1) {
1573 0 size_t take = sizeof buf;
1574 0 ▶ if (want > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (want > 0) { 0 0 0
1575 0 UV remain = want - (UV)SvCUR(out);
1576 0 ▶ if (remain == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (remain == 0) break; 0 0 0
1577 0 ▶ if (remain < take) take = (size_t)remain;
Branch: 1 branch
BranchTrueFalseTotal
if (remain < take) take = (size_t)remain; 0 0 0
1578 }
1579 0 n = h->plugin->read_data(aTHX_ h->plugin, h->cursor, buf, take);
1580 0 ▶ if (n < 0) { SvREFCNT_dec(out); croak("File::Raw::Archive: read_data failed"); }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { SvREFCNT_dec(out); croak("File::Raw::Archive: read_data failed"); } 0 0 0
1581 0 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 0 0
1582 0 sv_catpvn(out, buf, (STRLEN)n);
1583 0 ▶ if (want == 0 && (size_t)n < take) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == 0 && (size_t)n < take) { 0 0 0
1584 /* Either entry done or short read; if entry has more,
1585 * the next iteration will return more. read_data returns
1586 * 0 only at end-of-entry. */
1587 }
1588 }
1589 0 RETVAL = out;
1590 OUTPUT:
1591 RETVAL
1592  
1593 void
1594 _read_close(handle_sv)
1595 SV *handle_sv
1596 PREINIT:
1597 archive_handle_t *h;
1598 CODE:
1599 0 h = unwrap_handle(aTHX_ handle_sv);
1600 0 ▶ if (h) free_handle(aTHX_ h);
Branch: 1 branch
BranchTrueFalseTotal
if (h) free_handle(aTHX_ h); 0 0 0
1601  
1602 SV *
1603 _open_write(path_sv, opts_sv)
1604 SV *path_sv
1605 SV *opts_sv
1606 PREINIT:
1607 0 HV *opts = NULL;
1608 0 archive_handle_t *h = NULL;
1609 0 int gz = 0, fd;
1610 const ArchivePlugin *plugin;
1611 archive_push_fn push;
1612 void *sink;
1613 CODE:
1614 0 ▶ if (!SvOK(path_sv)) croak("File::Raw::Archive::_open_write: undef path");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(path_sv)) croak("File::Raw::Archive::_open_write: undef path"); 0 0 0
1615 0 ▶ if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 0 0 0
if (SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 0 0 0
1616 0 opts = (HV *)SvRV(opts_sv);
1617  
1618 {
1619 0 ▶ SV **sv = opts ? hv_fetchs(opts, "compression", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = opts ? hv_fetchs(opts, "compression", 0) : NULL; 0 0 0
1620 0 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 0 0 0
if (sv && *sv && SvOK(*sv)) { 0 0 0
if (sv && *sv && SvOK(*sv)) { 0 0 0
1621 STRLEN nn;
1622 0 const char *pp = SvPV(*sv, nn);
1623 0 ▶ if ((nn == 4 && memcmp(pp, "gzip", 4) == 0)) gz = 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((nn == 4 && memcmp(pp, "gzip", 4) == 0)) gz = 1; 0 0 0
if ((nn == 4 && memcmp(pp, "gzip", 4) == 0)) gz = 1; 0 0 0
1624 0 ▶ else if (nn == 4 && memcmp(pp, "auto", 4) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (nn == 4 && memcmp(pp, "auto", 4) == 0) { 0 0 0
else if (nn == 4 && memcmp(pp, "auto", 4) == 0) { 0 0 0
1625 /* On write, "auto" means look at suffix; if .gz, gzip. */
1626 STRLEN pl;
1627 0 const char *pp2 = SvPV(path_sv, pl);
1628 0 ▶ if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1; 0 0 0
if (pl >= 3 && memcmp(pp2 + pl - 3, ".gz", 3) == 0) gz = 1; 0 0 0
1629 }
1630 }
1631 }
1632  
1633 0 fd = open(SvPV_nolen(path_sv), O_WRONLY | O_CREAT | O_TRUNC, 0644);
1634 0 ▶ if (fd < 0) croak("File::Raw::Archive: cannot open '%s' for write: %s",
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) croak("File::Raw::Archive: cannot open '%s' for write: %s", 0 0 0
1635 SvPV_nolen(path_sv), strerror(errno));
1636  
1637 {
1638 0 ▶ SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = opts ? hv_fetchs(opts, "plugin", 0) : NULL; 0 0 0
1639 0 ▶ if (sv && *sv && SvOK(*sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && *sv && SvOK(*sv)) { 0 0 0
if (sv && *sv && SvOK(*sv)) { 0 0 0
if (sv && *sv && SvOK(*sv)) { 0 0 0
1640 STRLEN nn;
1641 0 const char *pp = SvPV(*sv, nn);
1642 char namebuf[64];
1643 0 ▶ if (nn >= sizeof namebuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (nn >= sizeof namebuf) { 0 0 0
1644 0 close(fd);
1645 0 croak("File::Raw::Archive: plugin name too long");
1646 }
1647 0 memcpy(namebuf, pp, nn);
1648 0 namebuf[nn] = '\0';
1649 0 plugin = archive_lookup_plugin(aTHX_ namebuf);
1650 } else {
1651 0 plugin = archive_lookup_plugin(aTHX_ "tar");
1652 }
1653 }
1654 0 ▶ if (!plugin) { close(fd); croak("File::Raw::Archive: unknown plugin"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!plugin) { close(fd); croak("File::Raw::Archive: unknown plugin"); } 0 0 0
1655  
1656 0 h = make_handle();
1657 0 ▶ if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { close(fd); croak("File::Raw::Archive: out of memory"); } 0 0 0
1658 0 h->fd = fd;
1659 0 h->plugin = plugin;
1660 0 h->is_writer = 1;
1661  
1662 0 ▶ if (gz) {
Branch: 1 branch
BranchTrueFalseTotal
if (gz) { 0 0 0
1663 0 int level = 6;
1664 0 ▶ if (opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts) { 0 0 0
1665 0 SV **lsv = hv_fetchs(opts, "level", 0);
1666 0 ▶ if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv);
Branch: 3 branches
BranchTrueFalseTotal
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 0 0 0
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 0 0 0
if (lsv && *lsv && SvOK(*lsv)) level = (int)SvIV(*lsv); 0 0 0
1667 }
1668 0 h->gz_sink = archive_gz_sink_new(fd, ARCHIVE_DEFAULT_CHUNK, level);
1669 0 ▶ if (!h->gz_sink) { free_handle(aTHX_ h); croak("File::Raw::Archive: gzip sink init failed"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!h->gz_sink) { free_handle(aTHX_ h); croak("File::Raw::Archive: gzip sink init failed"); } 0 0 0
1670 0 push = archive_push_gz;
1671 0 sink = h->gz_sink;
1672 } else {
1673 0 h->fd_state.fd = fd;
1674 0 push = archive_push_fd;
1675 0 sink = &h->fd_state;
1676 }
1677  
1678 0 ▶ if (plugin->write_open(aTHX_ plugin, push, sink, opts, &h->cursor) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plugin->write_open(aTHX_ plugin, push, sink, opts, &h->cursor) < 0) { 0 0 0
1679 0 free_handle(aTHX_ h);
1680 0 croak("File::Raw::Archive: write_open failed (bad format option?)");
1681 }
1682  
1683 0 RETVAL = new_handle_obj(aTHX_ h, "File::Raw::Archive::Writer");
1684 OUTPUT:
1685 RETVAL
1686  
1687 void
1688 _write_add(handle_sv, entry_sv, content_sv)
1689 SV *handle_sv
1690 SV *entry_sv
1691 SV *content_sv
1692 PREINIT:
1693 archive_handle_t *h;
1694 HV *eh;
1695 ArchiveEntry e;
1696 ArchiveXattr xbuf[64];
1697 0 size_t xused = 0;
1698 0 STRLEN clen = 0;
1699 0 const char *cp = NULL;
1700 CODE:
1701 0 h = unwrap_handle(aTHX_ handle_sv);
1702 0 ▶ if (!h || h->closed || !h->is_writer)
Branch: 3 branches
BranchTrueFalseTotal
if (!h || h->closed || !h->is_writer) 0 0 0
if (!h || h->closed || !h->is_writer) 0 0 0
if (!h || h->closed || !h->is_writer) 0 0 0
1703 0 croak("File::Raw::Archive::_write_add: invalid handle");
1704 0 ▶ if (!SvROK(entry_sv) || SvTYPE(SvRV(entry_sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(entry_sv) || SvTYPE(SvRV(entry_sv)) != SVt_PVHV) 0 0 0
if (!SvROK(entry_sv) || SvTYPE(SvRV(entry_sv)) != SVt_PVHV) 0 0 0
1705 0 croak("File::Raw::Archive::_write_add: entry must be a hashref");
1706 0 eh = (HV *)SvRV(entry_sv);
1707 0 hv_to_entry(aTHX_ eh, &e, xbuf, sizeof xbuf / sizeof xbuf[0], &xused);
1708  
1709 0 ▶ if (SvOK(content_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(content_sv)) { 0 0 0
1710 0 cp = SvPV(content_sv, clen);
1711 /* If caller didn't set entry size, use the content length. */
1712 0 ▶ if (e.size == 0 && clen > 0) e.size = clen;
Branch: 2 branches
BranchTrueFalseTotal
if (e.size == 0 && clen > 0) e.size = clen; 0 0 0
if (e.size == 0 && clen > 0) e.size = clen; 0 0 0
1713 }
1714  
1715 0 ▶ if (h->plugin->write_add(aTHX_ h->plugin, h->cursor, &e, cp, clen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (h->plugin->write_add(aTHX_ h->plugin, h->cursor, &e, cp, clen) < 0) 0 0 0
1716 0 croak("File::Raw::Archive::_write_add: write failed (entry exceeds format limits?)");
1717  
1718 void
1719 _write_close(handle_sv)
1720 SV *handle_sv
1721 PREINIT:
1722 archive_handle_t *h;
1723 CODE:
1724 0 h = unwrap_handle(aTHX_ handle_sv);
1725 0 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 0 0
1726 0 ▶ if (!h->closed && h->plugin && h->cursor && h->is_writer) {
Branch: 4 branches
BranchTrueFalseTotal
if (!h->closed && h->plugin && h->cursor && h->is_writer) { 0 0 0
if (!h->closed && h->plugin && h->cursor && h->is_writer) { 0 0 0
if (!h->closed && h->plugin && h->cursor && h->is_writer) { 0 0 0
if (!h->closed && h->plugin && h->cursor && h->is_writer) { 0 0 0
1727 0 ▶ if (h->plugin->write_close(aTHX_ h->plugin, h->cursor) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (h->plugin->write_close(aTHX_ h->plugin, h->cursor) < 0) 0 0 0
1728 0 croak("File::Raw::Archive::_write_close: failed");
1729 0 h->cursor = NULL; /* prevent double-close */
1730 }
1731 /* gz_sink finish is handled in free_handle. */
1732 0 ▶ if (h->gz_sink) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->gz_sink) { 0 0 0
1733 0 ▶ if (archive_gz_sink_finish(h->gz_sink) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (archive_gz_sink_finish(h->gz_sink) < 0) 0 0 0
1734 0 croak("File::Raw::Archive::_write_close: gzip finalise failed");
1735 }
1736 0 free_handle(aTHX_ h);
1737  
1738 int
1739 _apply_xattrs(fd_iv, xattrs_sv)
1740 IV fd_iv
1741 SV *xattrs_sv
1742 PREINIT:
1743 HV *xh;
1744 ArchiveXattr buf[64];
1745 0 ▶ size_t n = 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t n = 0; 0 0 0
1746 int rc;
1747 CODE:
1748 0 ▶ if (!SvROK(xattrs_sv) || SvTYPE(SvRV(xattrs_sv)) != SVt_PVHV) XSRETURN_IV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(xattrs_sv) || SvTYPE(SvRV(xattrs_sv)) != SVt_PVHV) XSRETURN_IV(0); 0 0 0
if (!SvROK(xattrs_sv) || SvTYPE(SvRV(xattrs_sv)) != SVt_PVHV) XSRETURN_IV(0); 0 0 0
1749 0 xh = (HV *)SvRV(xattrs_sv);
1750 0 hv_iterinit(xh);
1751 HE *he;
1752 0 ▶ while ((he = hv_iternext(xh)) && n < sizeof buf / sizeof buf[0]) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(xh)) && n < sizeof buf / sizeof buf[0]) { 0 0 0
while ((he = hv_iternext(xh)) && n < sizeof buf / sizeof buf[0]) { 0 0 0
1753 I32 klen_i;
1754 0 const char *k = hv_iterkey(he, &klen_i);
1755 0 SV *v = hv_iterval(xh, he);
1756 STRLEN vlen;
1757 0 const char *vp = SvPV(v, vlen);
1758 0 buf[n].key = k;
1759 0 buf[n].key_len = (size_t)klen_i;
1760 0 buf[n].value = vp;
1761 0 buf[n].value_len = vlen;
1762 0 n++;
1763 }
1764 0 rc = archive_apply_xattrs((int)fd_iv, buf, n);
1765 0 ▶ RETVAL = rc;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = rc; 0 0 0
1766 OUTPUT:
1767 RETVAL
1768  
1769 SV *
1770 _list_plugins()
1771 PREINIT:
1772 AV *av;
1773 int i;
1774 CODE:
1775 0 av = newAV();
1776 /* We don't expose iteration in the public API, so do a small
1777 * lookup-by-name dance. Currently only "tar" is built-in; sister
1778 * dists register more. */
1779 static const char *known[] = { "tar", "zip", "cpio", "ar", NULL };
1780 0 ▶ for (i = 0; known[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) { 0 0 0
1781 0 ▶ if (archive_lookup_plugin(aTHX_ known[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_lookup_plugin(aTHX_ known[i])) { 0 0 0
1782 0 av_push(av, newSVpv(known[i], 0));
1783 }
1784 }
1785 0 RETVAL = newRV_noinc((SV *)av);
1786 OUTPUT:
1787 RETVAL
1788  
1789 void
1790 _extract_all_xs(handle_sv, dest_sv, apply_xattrs_iv, unsafe_paths_iv, filter_sv)
1791 SV *handle_sv
1792 SV *dest_sv
1793 IV apply_xattrs_iv
1794 IV unsafe_paths_iv
1795 SV *filter_sv
1796 PREINIT:
1797 archive_handle_t *h;
1798 const char *dest;
1799 STRLEN dest_len;
1800 CODE:
1801 0 h = unwrap_handle(aTHX_ handle_sv);
1802 0 ▶ if (!SvOK(dest_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(dest_sv)) 0 0 0
1803 0 croak("File::Raw::Archive::_extract_all_xs: dest required");
1804 0 dest = SvPV(dest_sv, dest_len);
1805 0 do_extract_all_seq(aTHX_ h, dest, dest_len,
1806 (int)apply_xattrs_iv, (int)unsafe_paths_iv,
1807 filter_sv);
1808  
1809 SV *
1810 _list_xs(handle_sv)
1811 SV *handle_sv
1812 CODE:
1813 0 RETVAL = do_list(aTHX_ unwrap_handle(aTHX_ handle_sv));
1814 OUTPUT:
1815 RETVAL
1816  
1817 IV
1818 _extract_one_xs(handle_sv, name_sv, dest_sv, apply_xattrs_iv)
1819 SV *handle_sv
1820 SV *name_sv
1821 SV *dest_sv
1822 IV apply_xattrs_iv
1823 PREINIT:
1824 archive_handle_t *h;
1825 STRLEN match_len;
1826 const char *match_name;
1827 const char *dest_path;
1828 CODE:
1829 0 h = unwrap_handle(aTHX_ handle_sv);
1830 0 ▶ if (!SvOK(name_sv) || !SvOK(dest_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(name_sv) || !SvOK(dest_sv)) 0 0 0
if (!SvOK(name_sv) || !SvOK(dest_sv)) 0 0 0
1831 0 croak("File::Raw::Archive::_extract_one_xs: name and dest required");
1832 0 ▶ match_name = SvPV(name_sv, match_len);
Branch: 1 branch
BranchTrueFalseTotal
match_name = SvPV(name_sv, match_len); 0 0 0
1833 0 dest_path = SvPV_nolen(dest_sv);
1834 0 RETVAL = do_extract_one(aTHX_ h, match_name, match_len,
1835 dest_path, (int)apply_xattrs_iv);
1836 OUTPUT:
1837 RETVAL
1838  
1839 void
1840 _send_job_xs(fd_iv, path_sv, content_sv, mode_iv, mtime_iv, mtime_ns_iv, uid_iv, gid_iv, apply_xattrs_iv, xattrs_sv)
1841 IV fd_iv
1842 SV *path_sv
1843 SV *content_sv
1844 IV mode_iv
1845 IV mtime_iv
1846 IV mtime_ns_iv
1847 IV uid_iv
1848 IV gid_iv
1849 IV apply_xattrs_iv
1850 SV *xattrs_sv
1851 PREINIT:
1852 int fd;
1853 3 STRLEN path_len = 0;
1854 3 STRLEN content_len = 0;
1855 const char *path;
1856 3 const char *content = "";
1857 3 char *buf = NULL;
1858 3 size_t buf_len = 0;
1859 ArchiveXattr xbuf[64];
1860 3 size_t xn = 0;
1861 CODE:
1862 3 fd = (int)fd_iv;
1863 3 ▶ if (!SvOK(path_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(path_sv)) 0 3 3
1864 0 croak("File::Raw::Archive::_send_job_xs: path required");
1865 3 path = SvPV(path_sv, path_len);
1866 3 ▶ if (SvOK(content_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(content_sv)) { 3 0 3
1867 3 content = SvPV(content_sv, content_len);
1868 }
1869 3 ▶ if (SvROK(xattrs_sv) && SvTYPE(SvRV(xattrs_sv)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(xattrs_sv) && SvTYPE(SvRV(xattrs_sv)) == SVt_PVHV) { 1 2 3
if (SvROK(xattrs_sv) && SvTYPE(SvRV(xattrs_sv)) == SVt_PVHV) { 1 0 1
1870 1 HV *xh = (HV *)SvRV(xattrs_sv);
1871 1 hv_iterinit(xh);
1872 HE *he;
1873 3 ▶ while ((he = hv_iternext(xh)) && xn < sizeof xbuf / sizeof xbuf[0]) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(xh)) && xn < sizeof xbuf / sizeof xbuf[0]) { 2 1 3
while ((he = hv_iternext(xh)) && xn < sizeof xbuf / sizeof xbuf[0]) { 2 0 2
1874 I32 klen_i;
1875 2 const char *k = hv_iterkey(he, &klen_i);
1876 2 SV *v = hv_iterval(xh, he);
1877 STRLEN vlen;
1878 2 const char *vp = SvPV(v, vlen);
1879 2 xbuf[xn].key = k;
1880 2 xbuf[xn].key_len = (size_t)klen_i;
1881 2 xbuf[xn].value = vp;
1882 2 xbuf[xn].value_len = vlen;
1883 2 xn++;
1884 }
1885 }
1886  
1887 3 ▶ if (marshal_job(path, path_len,
Branch: 1 branch
BranchTrueFalseTotal
if (marshal_job(path, path_len, 0 3 3
1888 content, content_len,
1889 (uint32_t)mode_iv,
1890 (uint64_t)mtime_iv, (uint32_t)mtime_ns_iv,
1891 (uint32_t)uid_iv, (uint32_t)gid_iv,
1892 (int)apply_xattrs_iv,
1893 xbuf, xn,
1894 &buf, &buf_len) < 0) {
1895 0 croak("File::Raw::Archive::_send_job_xs: out of memory");
1896 }
1897 3 ▶ if (marshal_send(fd, buf, buf_len) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (marshal_send(fd, buf, buf_len) < 0) { 0 3 3
1898 0 int saved = errno;
1899 0 free(buf);
1900 0 errno = saved;
1901 0 croak("File::Raw::Archive::_send_job_xs: pipe write: %s",
1902 strerror(saved));
1903 }
1904 3 free(buf);
1905  
1906 void
1907 _worker_loop_xs(job_fd_iv, err_fd_iv)
1908 IV job_fd_iv
1909 IV err_fd_iv
1910 CODE:
1911 0 do_worker_loop((int)job_fd_iv, (int)err_fd_iv);
1912  
1913 # ====================================================================
1914 # Public top-level class methods. Each one owns the open + work + close
1915 # lifecycle: the user never sees a Reader/Writer handle for these. The
1916 # handle is freed via SAVEDESTRUCTOR_X so a croak inside the work
1917 # helper cleanly closes the fd and frees the cursor.
1918 # ====================================================================
1919  
1920 void
1921 list(...)
1922 PPCODE:
1923 {
1924 13 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 13 13
1925 0 croak("Usage: \\$class->list(\\$path, %%opts)");
1926 13 ▶ if ((items - 2) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2 != 0) 1 12 13
1927 1 croak("File::Raw::Archive::list: odd number of options");
1928 12 HV *opts = build_opts_from_args(aTHX_ &ST(2), items - 2);
1929 12 archive_handle_t *h = open_reader(aTHX_ ST(1), opts);
1930 12 SAVEDESTRUCTOR_X(free_handle_destructor, h);
1931 12 SV *result = do_list(aTHX_ h);
1932 9 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 9 9
1933 9 XSRETURN(1);
1934 }
1935  
1936 IV
1937 extract(...)
1938 PPCODE:
1939 {
1940 5 ▶ if (items < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 4) 1 4 5
1941 1 croak("Usage: \\$class->extract(\\$path, \\$name, \\$dest, %%opts)");
1942 4 ▶ if ((items - 4) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 4) % 2 != 0) 0 4 4
1943 0 croak("File::Raw::Archive::extract: odd number of options");
1944 4 HV *opts = build_opts_from_args(aTHX_ &ST(4), items - 4);
1945 4 int apply_xattrs = 1;
1946 4 SV **xv = hv_fetchs(opts, "xattrs", 0);
1947 4 ▶ if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 4 4
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
1948  
1949 4 archive_handle_t *h = open_reader(aTHX_ ST(1), opts);
1950 4 SAVEDESTRUCTOR_X(free_handle_destructor, h);
1951  
1952 STRLEN match_len;
1953 4 const char *match_name = SvPV(ST(2), match_len);
1954 4 ▶ const char *dest_path = SvPV_nolen(ST(3));
Branch: 1 branch
BranchTrueFalseTotal
const char *dest_path = SvPV_nolen(ST(3)); 4 0 4
1955 4 IV rc = do_extract_one(aTHX_ h, match_name, match_len, dest_path,
1956 apply_xattrs);
1957 4 ▶ XPUSHs(sv_2mortal(newSViv(rc)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(rc))); 0 4 4
1958 4 XSRETURN(1);
1959 }
1960  
1961 IV
1962 extract_all(...)
1963 PPCODE:
1964 {
1965 12 ▶ if (items < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) 1 11 12
1966 1 croak("Usage: \\$class->extract_all(\\$path, \\$dest, %%opts)");
1967 11 ▶ if ((items - 3) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2 != 0) 0 11 11
1968 0 croak("File::Raw::Archive::extract_all: odd number of options");
1969 11 HV *opts = build_opts_from_args(aTHX_ &ST(3), items - 3);
1970 11 int apply_xattrs = 1;
1971 11 int unsafe_paths = 0;
1972 11 int parallel = 1;
1973 11 SV *filter = NULL;
1974 SV **xv;
1975  
1976 11 xv = hv_fetchs(opts, "xattrs", 0);
1977 11 ▶ if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 11 11
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
1978 11 xv = hv_fetchs(opts, "unsafe_paths", 0);
1979 11 ▶ if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 11 11
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 0 0
1980 11 xv = hv_fetchs(opts, "entry_filter", 0);
1981 11 ▶ if (xv && *xv && SvOK(*xv)) filter = *xv;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 11 11
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
1982 11 xv = hv_fetchs(opts, "parallel", 0);
1983 11 ▶ if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv);
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 3 8 11
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 3 0 3
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 3 0 3
1984  
1985 11 ▶ if (parallel > 1 && !parallel_supported()) {
Branch: 2 branches
BranchTrueFalseTotal
if (parallel > 1 && !parallel_supported()) { 2 9 11
if (parallel > 1 && !parallel_supported()) { 0 2 2
1986 0 warn("File::Raw::Archive: parallel extract not supported on this "
1987 "platform; falling back to sequential\n");
1988 0 parallel = 1;
1989 }
1990  
1991 11 archive_handle_t *h = open_reader(aTHX_ ST(1), opts);
1992 11 SAVEDESTRUCTOR_X(free_handle_destructor, h);
1993  
1994 STRLEN dest_len;
1995 11 const char *dest = SvPV(ST(2), dest_len);
1996 11 ▶ if (parallel > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (parallel > 1) { 2 9 11
1997 2 do_extract_all_parallel(aTHX_ h, dest, dest_len,
1998 parallel, apply_xattrs,
1999 filter, unsafe_paths);
2000 } else {
2001 9 do_extract_all_seq(aTHX_ h, dest, dest_len,
2002 apply_xattrs, unsafe_paths, filter);
2003 }
2004 8 ▶ XPUSHs(sv_2mortal(newSViv(1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(1))); 0 8 8
2005 8 XSRETURN(1);
2006 }
2007  
2008 void
2009 each(...)
2010 PPCODE:
2011 {
2012 5 ▶ if (items < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) 1 4 5
2013 1 croak("Usage: \\$class->each(\\$path, %%opts, sub { ... })");
2014 4 SV *cb_sv = ST(items - 1);
2015 4 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 3 1 4
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 3 3
2016 1 croak("File::Raw::Archive::each: last arg must be a coderef");
2017 3 int opts_end = items - 1;
2018 3 ▶ if ((opts_end - 2) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((opts_end - 2) % 2 != 0) 0 3 3
2019 0 croak("File::Raw::Archive::each: odd number of options");
2020 3 HV *opts = build_opts_from_args(aTHX_ &ST(2), opts_end - 2);
2021 3 SV *filter = NULL;
2022 3 SV **xv = hv_fetchs(opts, "entry_filter", 0);
2023 3 ▶ if (xv && *xv && SvOK(*xv)) filter = *xv;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) filter = *xv; 1 2 3
if (xv && *xv && SvOK(*xv)) filter = *xv; 1 0 1
if (xv && *xv && SvOK(*xv)) filter = *xv; 1 0 1
2024  
2025 3 archive_handle_t *h = open_reader(aTHX_ ST(1), opts);
2026  
2027 /* Build a Reader AV equivalent to what File::Raw::Archive->open
2028 * returns. The Entry objects we hand to the callback link back to
2029 * this so $entry->slurp / $entry->_skip work the same as on the
2030 * iterator API. The Reader's DESTROY (XSUB) frees `h` via
2031 * maybe_free_handle when the AV refcount hits zero - either at
2032 * XSUB exit (if no Entry stashed it) or whenever the last stashed
2033 * Entry is collected. */
2034 3 AV *reader_av = newAV();
2035 3 av_extend(reader_av, R_SLOT_COUNT - 1);
2036 3 av_store(reader_av, R_HANDLE,
2037 new_handle_obj(aTHX_ h, "File::Raw::Archive::Reader"));
2038 3 av_store(reader_av, R_CONSUMED, newSViv(1));
2039 3 av_store(reader_av, R_CLOSED, newSViv(0));
2040 3 SV *reader_sv = sv_2mortal(newRV_noinc((SV *)reader_av));
2041 3 sv_bless(reader_sv, gv_stashpv("File::Raw::Archive::Reader", GV_ADD));
2042  
2043 3 do_each(aTHX_ h, reader_sv, cb_sv, filter);
2044 3 XSRETURN_EMPTY;
2045 }
2046  
2047 SV *
2048 open(...)
2049 PPCODE:
2050 {
2051 36 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 36 36
2052 0 croak("Usage: \\$class->open(\\$path, %%opts)");
2053 36 ▶ if ((items - 2) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2 != 0) 0 36 36
2054 0 croak("File::Raw::Archive::open: odd number of options");
2055 36 HV *opts = build_opts_from_args(aTHX_ &ST(2), items - 2);
2056 36 archive_handle_t *h = open_reader(aTHX_ ST(1), opts);
2057 35 SV *handle_sv = new_handle_obj(aTHX_ h, "File::Raw::Archive::Reader");
2058  
2059 35 AV *self = newAV();
2060 35 av_extend(self, R_SLOT_COUNT - 1);
2061 35 av_store(self, R_HANDLE, handle_sv); /* takes ownership */
2062 35 av_store(self, R_CONSUMED, newSViv(1));
2063 35 av_store(self, R_CLOSED, newSViv(0));
2064 /* R_CUR_ENTRY left empty until the first ->next. */
2065 35 SV *obj = sv_bless(newRV_noinc((SV *)self),
2066 gv_stashpv("File::Raw::Archive::Reader", GV_ADD));
2067 35 ▶ XPUSHs(sv_2mortal(obj));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(obj)); 0 35 35
2068 35 XSRETURN(1);
2069 }
2070  
2071 SV *
2072 create(...)
2073 PPCODE:
2074 {
2075 40 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 40 40
2076 0 croak("Usage: \\$class->create(\\$path, %%opts)");
2077 40 ▶ if ((items - 2) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2 != 0) 0 40 40
2078 0 croak("File::Raw::Archive::create: odd number of options");
2079 40 HV *opts = build_opts_from_args(aTHX_ &ST(2), items - 2);
2080 40 archive_handle_t *h = open_writer(aTHX_ ST(1), opts);
2081 39 SV *handle_sv = new_handle_obj(aTHX_ h, "File::Raw::Archive::Writer");
2082  
2083 39 AV *self = newAV();
2084 39 av_extend(self, W_SLOT_COUNT - 1);
2085 39 av_store(self, W_HANDLE, handle_sv);
2086 39 av_store(self, W_CLOSED, newSViv(0));
2087 39 SV *obj = sv_bless(newRV_noinc((SV *)self),
2088 gv_stashpv("File::Raw::Archive::Writer", GV_ADD));
2089 39 ▶ XPUSHs(sv_2mortal(obj));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(obj)); 0 39 39
2090 39 XSRETURN(1);
2091 }
2092  
2093 # ====================================================================
2094 MODULE = File::Raw::Archive PACKAGE = File::Raw::Archive::Reader
2095 # ====================================================================
2096  
2097 SV *
2098 next(self_sv)
2099 SV *self_sv
2100 PREINIT:
2101 AV *self;
2102 SV *handle_sv;
2103 archive_handle_t *h;
2104 SV **handle_ref, **closed_ref, **cur_entry_ref, **consumed_ref;
2105 CODE:
2106 77 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 77 0 77
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 77 77
2107 0 croak("File::Raw::Archive::Reader::next: invalid invocant");
2108 77 self = (AV *)SvRV(self_sv);
2109  
2110 77 closed_ref = av_fetch(self, R_CLOSED, 0);
2111 77 ▶ if (closed_ref && *closed_ref && SvTRUE(*closed_ref))
Branch: 3 branches
BranchTrueFalseTotal
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) 77 0 77
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) 77 0 77
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) 1 76 77
2112 1 croak("File::Raw::Archive::Reader::next: reader is closed");
2113  
2114 76 handle_ref = av_fetch(self, R_HANDLE, 0);
2115 76 ▶ if (!handle_ref || !*handle_ref)
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) 76 0 76
if (!handle_ref || !*handle_ref) 0 76 76
2116 0 croak("File::Raw::Archive::Reader::next: missing handle");
2117 76 handle_sv = *handle_ref;
2118 76 h = unwrap_handle(aTHX_ handle_sv);
2119 76 ▶ if (!h || h->is_writer)
Branch: 2 branches
BranchTrueFalseTotal
if (!h || h->is_writer) 76 0 76
if (!h || h->is_writer) 0 76 76
2120 0 croak("File::Raw::Archive::Reader::next: invalid handle");
2121  
2122 /* Drain previous entry's payload if not consumed. */
2123 76 cur_entry_ref = av_fetch(self, R_CUR_ENTRY, 0);
2124 76 consumed_ref = av_fetch(self, R_CONSUMED, 0);
2125 76 ▶ if (cur_entry_ref && *cur_entry_ref && SvOK(*cur_entry_ref) &&
Branch: 4 branches
BranchTrueFalseTotal
if (cur_entry_ref && *cur_entry_ref && SvOK(*cur_entry_ref) && 42 34 76
if (cur_entry_ref && *cur_entry_ref && SvOK(*cur_entry_ref) && 42 0 42
if (cur_entry_ref && *cur_entry_ref && SvOK(*cur_entry_ref) && 42 0 42
if (cur_entry_ref && *cur_entry_ref && SvOK(*cur_entry_ref) && 42 0 42
2126 42 ▶ (!consumed_ref || !*consumed_ref || !SvTRUE(*consumed_ref))) {
Branch: 2 branches
BranchTrueFalseTotal
(!consumed_ref || !*consumed_ref || !SvTRUE(*consumed_ref))) { 42 0 42
(!consumed_ref || !*consumed_ref || !SvTRUE(*consumed_ref))) { 13 29 42
2127 char buf[16 * 1024];
2128 int n;
2129 22 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
2130 22 ▶ buf, sizeof buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
buf, sizeof buf)) > 0) {} 9 13 22
2131 13 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 13 13
2132 0 croak("File::Raw::Archive::Reader::next: read_data failed");
2133 }
2134  
2135 {
2136 ArchiveEntry e;
2137 76 int rc = h->plugin->read_next(aTHX_ h->plugin, h->cursor, &e);
2138 76 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 76 76
2139 0 croak("File::Raw::Archive: malformed archive");
2140 76 ▶ if (rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) { 11 65 76
2141 11 av_store(self, R_CUR_ENTRY, &PL_sv_undef);
2142 11 av_store(self, R_CONSUMED, newSViv(1));
2143 11 XSRETURN_UNDEF;
2144 }
2145 65 SV *entry_rv = entry_to_av(aTHX_ &e);
2146 65 SV *entry_obj = build_entry_obj(aTHX_ self_sv, entry_rv);
2147 65 av_store(self, R_CUR_ENTRY, SvREFCNT_inc(entry_obj));
2148 65 av_store(self, R_CONSUMED, newSViv(0));
2149 65 RETVAL = entry_obj;
2150 }
2151 OUTPUT:
2152 RETVAL
2153  
2154 SV *
2155 _handle(self_sv)
2156 SV *self_sv
2157 PREINIT:
2158 AV *self;
2159 SV **handle_ref;
2160 CODE:
2161 0 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 0 0
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 0 0
2162 0 XSRETURN_UNDEF;
2163 0 self = (AV *)SvRV(self_sv);
2164 0 handle_ref = av_fetch(self, R_HANDLE, 0);
2165 0 ▶ if (!handle_ref || !*handle_ref) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) XSRETURN_UNDEF; 0 0 0
if (!handle_ref || !*handle_ref) XSRETURN_UNDEF; 0 0 0
2166 0 RETVAL = SvREFCNT_inc(*handle_ref);
2167 OUTPUT:
2168 RETVAL
2169  
2170 void
2171 _mark_consumed(self_sv)
2172 SV *self_sv
2173 PREINIT:
2174 AV *self;
2175 CODE:
2176 0 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return; 0 0 0
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return; 0 0 0
2177 0 self = (AV *)SvRV(self_sv);
2178 0 av_store(self, R_CONSUMED, newSViv(1));
2179  
2180 void
2181 close(self_sv)
2182 SV *self_sv
2183 PREINIT:
2184 AV *self;
2185 SV **closed_ref, **handle_ref;
2186 CODE:
2187 34 ▶ if (!SvROK(self_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) return; 0 34 34
2188 34 ▶ if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) { 0 34 34
2189 /* Bare blessed-IV-ref form: close it directly. */
2190 0 maybe_free_handle(aTHX_ self_sv);
2191 0 return;
2192 }
2193 34 self = (AV *)SvRV(self_sv);
2194 34 closed_ref = av_fetch(self, R_CLOSED, 0);
2195 34 ▶ if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 34 0 34
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 34 0 34
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 1 33 34
2196 33 av_store(self, R_CLOSED, newSViv(1));
2197 /* Clear cur_entry so any stashed entry's slurp fails cleanly
2198 * rather than reading from a freed handle. */
2199 33 av_store(self, R_CUR_ENTRY, &PL_sv_undef);
2200 33 handle_ref = av_fetch(self, R_HANDLE, 0);
2201 33 ▶ if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref);
Branch: 2 branches
BranchTrueFalseTotal
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 33 0 33
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 33 0 33
2202 33 av_store(self, R_HANDLE, &PL_sv_undef);
2203  
2204 void
2205 DESTROY(self_sv)
2206 SV *self_sv
2207 PREINIT:
2208 AV *self;
2209 SV **closed_ref, **handle_ref;
2210 CODE:
2211 80 ▶ if (!SvROK(self_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) return; 0 80 80
2212 80 ▶ if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) { 40 40 80
2213 /* Bare blessed-IV-ref handle: free underlying C struct now. */
2214 40 maybe_free_handle(aTHX_ self_sv);
2215 40 return;
2216 }
2217 40 self = (AV *)SvRV(self_sv);
2218 40 closed_ref = av_fetch(self, R_CLOSED, 0);
2219 40 ▶ if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 33 7 40
2220 7 handle_ref = av_fetch(self, R_HANDLE, 0);
2221 7 ▶ if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref);
Branch: 2 branches
BranchTrueFalseTotal
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 7 0 7
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 7 0 7
2222  
2223 # ====================================================================
2224 MODULE = File::Raw::Archive PACKAGE = File::Raw::Archive::Writer
2225 # ====================================================================
2226  
2227 void
2228 add(...)
2229 PPCODE:
2230 {
2231 679 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 679 679
2232 0 croak("Usage: \\$writer->add(name => ..., content => ..., ...)");
2233 679 SV *self_sv = ST(0);
2234 679 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 679 0 679
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 679 679
2235 0 croak("File::Raw::Archive::Writer::add: invalid invocant");
2236 679 AV *self = (AV *)SvRV(self_sv);
2237 679 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 679 679
2238 0 croak("File::Raw::Archive::Writer::add: odd number of fields");
2239  
2240 679 HV *fields = newHV();
2241 679 sv_2mortal((SV *)fields);
2242 679 SV *content_sv = NULL;
2243 int i;
2244 2638 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 1959 679 2638
2245 STRLEN klen;
2246 1959 const char *kp = SvPV(ST(i), klen);
2247 1959 ▶ if (klen == 7 && memcmp(kp, "content", 7) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memcmp(kp, "content", 7) == 0) { 670 1289 1959
if (klen == 7 && memcmp(kp, "content", 7) == 0) { 670 0 670
2248 670 content_sv = ST(i + 1);
2249 } else {
2250 1289 hv_store(fields, kp, klen, newSVsv(ST(i + 1)), 0);
2251 }
2252 }
2253  
2254 /* Default type by name suffix or link_target presence. */
2255 {
2256 679 SV **type_ref = hv_fetchs(fields, "type", 0);
2257 679 ▶ if (!type_ref || !*type_ref || !SvOK(*type_ref)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!type_ref || !*type_ref || !SvOK(*type_ref)) { 1 678 679
if (!type_ref || !*type_ref || !SvOK(*type_ref)) { 1 0 1
if (!type_ref || !*type_ref || !SvOK(*type_ref)) { 0 1 1
2258 678 int type = AE_FILE;
2259 678 SV **name_ref = hv_fetchs(fields, "name", 0);
2260 678 SV **link_ref = hv_fetchs(fields, "link_target", 0);
2261 678 ▶ int has_link = (link_ref && *link_ref && SvOK(*link_ref));
Branch: 3 branches
BranchTrueFalseTotal
int has_link = (link_ref && *link_ref && SvOK(*link_ref)); 3 675 678
int has_link = (link_ref && *link_ref && SvOK(*link_ref)); 3 0 3
int has_link = (link_ref && *link_ref && SvOK(*link_ref)); 3 0 3
2262 1356 ▶ if (name_ref && *name_ref && SvOK(*name_ref)) {
Branch: 3 branches
BranchTrueFalseTotal
if (name_ref && *name_ref && SvOK(*name_ref)) { 678 0 678
if (name_ref && *name_ref && SvOK(*name_ref)) { 678 0 678
if (name_ref && *name_ref && SvOK(*name_ref)) { 678 0 678
2263 STRLEN nl;
2264 678 const char *np = SvPV(*name_ref, nl);
2265 678 ▶ if (nl > 0 && np[nl - 1] == '/') type = AE_DIR;
Branch: 2 branches
BranchTrueFalseTotal
if (nl > 0 && np[nl - 1] == '/') type = AE_DIR; 678 0 678
if (nl > 0 && np[nl - 1] == '/') type = AE_DIR; 5 673 678
2266 673 ▶ else if (has_link) type = AE_SYMLINK;
Branch: 1 branch
BranchTrueFalseTotal
else if (has_link) type = AE_SYMLINK; 3 670 673
2267 670 else type = AE_FILE;
2268 0 ▶ } else if (has_link) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_link) { 0 0 0
2269 0 type = AE_SYMLINK;
2270 }
2271 678 hv_stores(fields, "type", newSViv(type));
2272 }
2273 }
2274 /* Default mode by type. */
2275 {
2276 679 SV **mode_ref = hv_fetchs(fields, "mode", 0);
2277 679 ▶ if (!mode_ref || !*mode_ref || !SvOK(*mode_ref)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!mode_ref || !*mode_ref || !SvOK(*mode_ref)) { 576 103 679
if (!mode_ref || !*mode_ref || !SvOK(*mode_ref)) { 576 0 576
if (!mode_ref || !*mode_ref || !SvOK(*mode_ref)) { 0 576 576
2278 103 SV **t = hv_fetchs(fields, "type", 0);
2279 103 ▶ int type = (t && *t) ? (int)SvIV(*t) : AE_FILE;
Branch: 2 branches
BranchTrueFalseTotal
int type = (t && *t) ? (int)SvIV(*t) : AE_FILE; 103 0 103
int type = (t && *t) ? (int)SvIV(*t) : AE_FILE; 103 0 103
2280 103 IV mode = (type == AE_DIR ? 0755
2281 103 ▶ : type == AE_SYMLINK ? 0777
Branch: 2 branches
BranchTrueFalseTotal
: type == AE_SYMLINK ? 0777 99 4 103
: type == AE_SYMLINK ? 0777 3 96 99
2282 : 0644);
2283 103 hv_stores(fields, "mode", newSViv(mode));
2284 }
2285 }
2286  
2287 679 SV **handle_ref = av_fetch(self, W_HANDLE, 0);
2288 679 ▶ if (!handle_ref || !*handle_ref)
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) 679 0 679
if (!handle_ref || !*handle_ref) 0 679 679
2289 0 croak("File::Raw::Archive::Writer::add: missing handle");
2290 679 archive_handle_t *h = unwrap_handle(aTHX_ *handle_ref);
2291 679 ▶ if (!h || !h->is_writer)
Branch: 2 branches
BranchTrueFalseTotal
if (!h || !h->is_writer) 679 0 679
if (!h || !h->is_writer) 0 679 679
2292 0 croak("File::Raw::Archive::Writer::add: invalid handle");
2293  
2294 ArchiveEntry e;
2295 ArchiveXattr xbuf[64];
2296 679 size_t xused = 0;
2297 679 hv_to_entry(aTHX_ fields, &e, xbuf, sizeof xbuf / sizeof xbuf[0], &xused);
2298  
2299 679 STRLEN clen = 0;
2300 679 const char *cp = NULL;
2301 679 ▶ if (content_sv && SvOK(content_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (content_sv && SvOK(content_sv)) { 670 9 679
if (content_sv && SvOK(content_sv)) { 670 0 670
2302 670 cp = SvPV(content_sv, clen);
2303 670 ▶ if (e.size == 0 && clen > 0) e.size = clen;
Branch: 2 branches
BranchTrueFalseTotal
if (e.size == 0 && clen > 0) e.size = clen; 670 0 670
if (e.size == 0 && clen > 0) e.size = clen; 670 0 670
2304 }
2305  
2306 679 ▶ if (h->plugin->write_add(aTHX_ h->plugin, h->cursor, &e, cp, clen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (h->plugin->write_add(aTHX_ h->plugin, h->cursor, &e, cp, clen) < 0) 5 674 679
2307 5 croak("File::Raw::Archive::Writer::add: write failed (entry exceeds format limits?)");
2308  
2309 674 XSRETURN_YES;
2310 }
2311  
2312 void
2313 close(self_sv)
2314 SV *self_sv
2315 PREINIT:
2316 AV *self;
2317 SV **closed_ref, **handle_ref;
2318 CODE:
2319 40 ▶ if (!SvROK(self_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) return; 0 40 40
2320 40 ▶ if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) { 0 40 40
2321 0 maybe_free_handle(aTHX_ self_sv);
2322 0 return;
2323 }
2324 40 self = (AV *)SvRV(self_sv);
2325 40 closed_ref = av_fetch(self, W_CLOSED, 0);
2326 40 ▶ if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 0 40 40
2327 40 av_store(self, W_CLOSED, newSViv(1));
2328 40 handle_ref = av_fetch(self, W_HANDLE, 0);
2329 40 ▶ if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref);
Branch: 2 branches
BranchTrueFalseTotal
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 40 0 40
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 40 0 40
2330 40 av_store(self, W_HANDLE, &PL_sv_undef);
2331  
2332 void
2333 DESTROY(self_sv)
2334 SV *self_sv
2335 PREINIT:
2336 AV *self;
2337 SV **closed_ref, **handle_ref;
2338 CODE:
2339 80 ▶ if (!SvROK(self_sv)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self_sv)) return; 0 80 80
2340 80 ▶ if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(self_sv)) != SVt_PVAV) { 40 40 80
2341 40 maybe_free_handle(aTHX_ self_sv);
2342 40 return;
2343 }
2344 40 self = (AV *)SvRV(self_sv);
2345 40 closed_ref = av_fetch(self, W_CLOSED, 0);
2346 40 ▶ if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
if (closed_ref && *closed_ref && SvTRUE(*closed_ref)) return; 40 0 40
2347 0 handle_ref = av_fetch(self, W_HANDLE, 0);
2348 0 ▶ if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref);
Branch: 2 branches
BranchTrueFalseTotal
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 0 0 0
if (handle_ref && *handle_ref) maybe_free_handle(aTHX_ *handle_ref); 0 0 0
2349  
2350 # ====================================================================
2351 MODULE = File::Raw::Archive PACKAGE = File::Raw::Archive::Entry
2352 # ====================================================================
2353 #
2354 # Entry is a blessed hashref with:
2355 # reader => Reader hashref (back-link, carries the C handle)
2356 # meta => entry-metadata hashref (name, size, mode, ...)
2357 # slurped => undef OR the cached payload SV
2358 #
2359 # Accessors fetch from `meta`. slurp pulls payload via the reader's
2360 # handle and caches it so a second slurp returns the same bytes.
2361  
2362 # Macro-style helper: read a key from $self->{meta} and return it,
2363 # or undef. Used by every accessor.
2364  
2365 SV *
2366 name(self_sv)
2367 SV *self_sv
2368 ALIAS:
2369 File::Raw::Archive::Entry::name = M_NAME
2370 File::Raw::Archive::Entry::size = M_SIZE
2371 File::Raw::Archive::Entry::mode = M_MODE
2372 File::Raw::Archive::Entry::mtime = M_MTIME
2373 File::Raw::Archive::Entry::mtime_ns = M_MTIME_NS
2374 File::Raw::Archive::Entry::uid = M_UID
2375 File::Raw::Archive::Entry::gid = M_GID
2376 File::Raw::Archive::Entry::type = M_TYPE
2377 File::Raw::Archive::Entry::link_target = M_LINK_TARGET
2378 File::Raw::Archive::Entry::xattrs = M_XATTRS
2379 File::Raw::Archive::Entry::is_sparse = M_IS_SPARSE
2380 PREINIT:
2381 AV *self, *meta;
2382 SV **meta_ref, **val;
2383 CODE:
2384 204 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 204 0 204
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 204 204
2385 0 XSRETURN_UNDEF;
2386 204 self = (AV *)SvRV(self_sv);
2387 204 meta_ref = av_fetch(self, E_META, 0);
2388 204 ▶ if (!meta_ref || !*meta_ref || !SvROK(*meta_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 203 1 204
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 203 0 203
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 203 0 203
2389 203 ▶ || SvTYPE(SvRV(*meta_ref)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*meta_ref)) != SVt_PVAV) 0 203 203
2390 1 XSRETURN_UNDEF;
2391 203 meta = (AV *)SvRV(*meta_ref);
2392 203 val = av_fetch(meta, ix, 0);
2393 203 ▶ if (!val || !*val || !SvOK(*val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!val || !*val || !SvOK(*val)) { 200 3 203
if (!val || !*val || !SvOK(*val)) { 200 0 200
if (!val || !*val || !SvOK(*val)) { 0 200 200
2394 /* Numeric defaults for the integer-shaped accessors. */
2395 3 ▶ switch (ix) {
Branch: 1 branch
BranchTrueFalseTotal
switch (ix) { 0 3 3
2396 0 case M_SIZE: case M_MODE: case M_MTIME: case M_MTIME_NS:
2397 case M_UID: case M_GID: case M_TYPE: case M_IS_SPARSE:
2398 0 RETVAL = newSViv(0);
2399 0 break;
2400 3 default:
2401 3 XSRETURN_UNDEF;
2402 }
2403 } else {
2404 200 RETVAL = SvREFCNT_inc(*val);
2405 }
2406 OUTPUT:
2407 RETVAL
2408  
2409 IV
2410 is_file(self_sv)
2411 SV *self_sv
2412 ALIAS:
2413 File::Raw::Archive::Entry::is_file = 1
2414 File::Raw::Archive::Entry::is_dir = 2
2415 File::Raw::Archive::Entry::is_symlink = 3
2416 File::Raw::Archive::Entry::is_link = 4
2417 PREINIT:
2418 AV *self, *meta;
2419 SV **meta_ref, **type_ref;
2420 IV t;
2421 CODE:
2422 35 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 35 0 35
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 35 35
2423 0 XSRETURN_IV(0);
2424 35 self = (AV *)SvRV(self_sv);
2425 35 meta_ref = av_fetch(self, E_META, 0);
2426 35 ▶ if (!meta_ref || !*meta_ref || !SvROK(*meta_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 35 0 35
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 35 0 35
if (!meta_ref || !*meta_ref || !SvROK(*meta_ref) 35 0 35
2427 35 ▶ || SvTYPE(SvRV(*meta_ref)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*meta_ref)) != SVt_PVAV) 0 35 35
2428 0 XSRETURN_IV(0);
2429 35 meta = (AV *)SvRV(*meta_ref);
2430 35 type_ref = av_fetch(meta, M_TYPE, 0);
2431 35 ▶ t = (type_ref && *type_ref && SvOK(*type_ref))
Branch: 2 branches
BranchTrueFalseTotal
t = (type_ref && *type_ref && SvOK(*type_ref)) 35 0 35
t = (type_ref && *type_ref && SvOK(*type_ref)) 35 0 35
2432 70 ▶ ? SvIV(*type_ref) : (IV)AE_FILE;
Branch: 1 branch
BranchTrueFalseTotal
? SvIV(*type_ref) : (IV)AE_FILE; 35 0 35
2433 35 switch (ix) {
2434 12 case 1: RETVAL = (t == AE_FILE) ? 1 : 0; break;
2435 7 case 2: RETVAL = (t == AE_DIR) ? 1 : 0; break;
2436 9 case 3: RETVAL = (t == AE_SYMLINK) ? 1 : 0; break;
2437 7 ▶ case 4: RETVAL = (t == AE_SYMLINK || t == AE_HARDLINK) ? 1 : 0; break;
Branch: 2 branches
BranchTrueFalseTotal
case 4: RETVAL = (t == AE_SYMLINK || t == AE_HARDLINK) ? 1 : 0; break; 5 2 7
case 4: RETVAL = (t == AE_SYMLINK || t == AE_HARDLINK) ? 1 : 0; break; 0 5 5
2438 0 default: RETVAL = 0;
2439 }
2440 OUTPUT:
2441 RETVAL
2442  
2443 # slurp: pull the entry's payload bytes via the reader's plugin/cursor.
2444 # Memoised in $self->{slurped} so a second call returns the same SV.
2445  
2446 SV *
2447 slurp(self_sv)
2448 SV *self_sv
2449 PREINIT:
2450 AV *self, *reader_av;
2451 SV **slurped_ref, **reader_ref, **handle_ref;
2452 archive_handle_t *h;
2453 char buf[64 * 1024];
2454 int n;
2455 SV *result;
2456 CODE:
2457 40 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 40 0 40
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 40 40
2458 0 croak("File::Raw::Archive::Entry::slurp: invalid invocant");
2459 40 self = (AV *)SvRV(self_sv);
2460 40 slurped_ref = av_fetch(self, E_SLURPED, 0);
2461 40 ▶ if (slurped_ref && *slurped_ref && SvOK(*slurped_ref)) {
Branch: 3 branches
BranchTrueFalseTotal
if (slurped_ref && *slurped_ref && SvOK(*slurped_ref)) { 3 37 40
if (slurped_ref && *slurped_ref && SvOK(*slurped_ref)) { 3 0 3
if (slurped_ref && *slurped_ref && SvOK(*slurped_ref)) { 3 0 3
2462 3 RETVAL = SvREFCNT_inc(*slurped_ref);
2463 } else {
2464 37 reader_ref = av_fetch(self, E_READER, 0);
2465 37 ▶ if (!reader_ref || !*reader_ref || !SvROK(*reader_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 37 0 37
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 37 0 37
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 37 0 37
2466 37 ▶ || SvTYPE(SvRV(*reader_ref)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*reader_ref)) != SVt_PVAV) 0 37 37
2467 0 croak("File::Raw::Archive::Entry::slurp: missing reader");
2468 37 reader_av = (AV *)SvRV(*reader_ref);
2469 37 handle_ref = av_fetch(reader_av, R_HANDLE, 0);
2470 37 ▶ if (!handle_ref || !*handle_ref)
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) 37 0 37
if (!handle_ref || !*handle_ref) 0 37 37
2471 0 croak("File::Raw::Archive::Entry::slurp: missing handle");
2472 37 h = unwrap_handle(aTHX_ *handle_ref);
2473 37 ▶ if (!h || h->is_writer)
Branch: 2 branches
BranchTrueFalseTotal
if (!h || h->is_writer) 36 1 37
if (!h || h->is_writer) 0 36 36
2474 1 croak("File::Raw::Archive::Entry::slurp: invalid handle");
2475 36 result = newSVpvn("", 0);
2476 230 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
2477 230 ▶ buf, sizeof buf)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
buf, sizeof buf)) > 0) { 194 36 230
2478 194 sv_catpvn(result, buf, n);
2479 }
2480 36 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 36 36
2481 0 SvREFCNT_dec(result);
2482 0 croak("File::Raw::Archive::Entry::slurp: read_data failed");
2483 }
2484 /* Memoise + mark reader's cur_entry as consumed. */
2485 36 av_store(self, E_SLURPED, SvREFCNT_inc(result));
2486 36 av_store(reader_av, R_CONSUMED, newSViv(1));
2487 36 RETVAL = result;
2488 }
2489 OUTPUT:
2490 RETVAL
2491  
2492 SV *
2493 read(self_sv, n_sv)
2494 SV *self_sv
2495 SV *n_sv
2496 PREINIT:
2497 AV *self, *reader_av;
2498 SV **reader_ref, **handle_ref;
2499 archive_handle_t *h;
2500 UV want;
2501 SV *result;
2502 char buf[64 * 1024];
2503 int got;
2504 CODE:
2505 45 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 45 0 45
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) 0 45 45
2506 0 croak("File::Raw::Archive::Entry::read: invalid invocant");
2507 45 self = (AV *)SvRV(self_sv);
2508 45 reader_ref = av_fetch(self, E_READER, 0);
2509 45 ▶ if (!reader_ref || !*reader_ref || !SvROK(*reader_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 45 0 45
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 45 0 45
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 45 0 45
2510 45 ▶ || SvTYPE(SvRV(*reader_ref)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*reader_ref)) != SVt_PVAV) 0 45 45
2511 0 croak("File::Raw::Archive::Entry::read: missing reader");
2512 45 reader_av = (AV *)SvRV(*reader_ref);
2513 45 handle_ref = av_fetch(reader_av, R_HANDLE, 0);
2514 45 ▶ if (!handle_ref || !*handle_ref)
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) 45 0 45
if (!handle_ref || !*handle_ref) 0 45 45
2515 0 croak("File::Raw::Archive::Entry::read: missing handle");
2516 45 h = unwrap_handle(aTHX_ *handle_ref);
2517 45 ▶ if (!h || h->is_writer)
Branch: 2 branches
BranchTrueFalseTotal
if (!h || h->is_writer) 45 0 45
if (!h || h->is_writer) 0 45 45
2518 0 croak("File::Raw::Archive::Entry::read: invalid handle");
2519 45 ▶ if (!n_sv || !SvOK(n_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!n_sv || !SvOK(n_sv)) 45 0 45
if (!n_sv || !SvOK(n_sv)) 0 45 45
2520 0 croak("File::Raw::Archive::Entry::read: byte count required");
2521 45 want = SvUV(n_sv);
2522 45 result = newSVpvn("", 0);
2523 128 ▶ while ((UV)SvCUR(result) < want) {
Branch: 1 branch
BranchTrueFalseTotal
while ((UV)SvCUR(result) < want) { 86 42 128
2524 86 size_t chunk = sizeof buf;
2525 86 UV remain = want - (UV)SvCUR(result);
2526 86 ▶ if (remain < chunk) chunk = (size_t)remain;
Branch: 1 branch
BranchTrueFalseTotal
if (remain < chunk) chunk = (size_t)remain; 3 83 86
2527 86 got = h->plugin->read_data(aTHX_ h->plugin, h->cursor, buf, chunk);
2528 86 ▶ if (got < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { 0 86 86
2529 0 SvREFCNT_dec(result);
2530 0 croak("File::Raw::Archive::Entry::read: read_data failed");
2531 }
2532 86 ▶ if (got == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (got == 0) break; 3 83 86
2533 83 sv_catpvn(result, buf, got);
2534 }
2535 45 RETVAL = result;
2536 OUTPUT:
2537 RETVAL
2538  
2539 void
2540 _skip(self_sv)
2541 SV *self_sv
2542 ALIAS:
2543 File::Raw::Archive::Entry::_skip = 0
2544 File::Raw::Archive::Entry::_drain = 1
2545 PREINIT:
2546 AV *self, *reader_av;
2547 SV **reader_ref, **handle_ref, **consumed_ref;
2548 archive_handle_t *h;
2549 char buf[16 * 1024];
2550 int n;
2551 CODE:
2552 PERL_UNUSED_VAR(ix);
2553 2 ▶ if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return; 2 0 2
if (!SvROK(self_sv) || SvTYPE(SvRV(self_sv)) != SVt_PVAV) return; 0 2 2
2554 2 self = (AV *)SvRV(self_sv);
2555 2 reader_ref = av_fetch(self, E_READER, 0);
2556 2 ▶ if (!reader_ref || !*reader_ref || !SvROK(*reader_ref)
Branch: 3 branches
BranchTrueFalseTotal
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 2 0 2
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 2 0 2
if (!reader_ref || !*reader_ref || !SvROK(*reader_ref) 2 0 2
2557 2 ▶ || SvTYPE(SvRV(*reader_ref)) != SVt_PVAV) return;
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*reader_ref)) != SVt_PVAV) return; 0 2 2
2558 2 reader_av = (AV *)SvRV(*reader_ref);
2559 2 consumed_ref = av_fetch(reader_av, R_CONSUMED, 0);
2560 2 ▶ if (consumed_ref && *consumed_ref && SvTRUE(*consumed_ref)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (consumed_ref && *consumed_ref && SvTRUE(*consumed_ref)) return; 2 0 2
if (consumed_ref && *consumed_ref && SvTRUE(*consumed_ref)) return; 2 0 2
if (consumed_ref && *consumed_ref && SvTRUE(*consumed_ref)) return; 0 2 2
2561 2 handle_ref = av_fetch(reader_av, R_HANDLE, 0);
2562 2 ▶ if (!handle_ref || !*handle_ref) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!handle_ref || !*handle_ref) return; 2 0 2
if (!handle_ref || !*handle_ref) return; 0 2 2
2563 2 h = unwrap_handle(aTHX_ *handle_ref);
2564 2 ▶ if (!h) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!h) return; 0 2 2
2565 4 while ((n = h->plugin->read_data(aTHX_ h->plugin, h->cursor,
2566 4 ▶ buf, sizeof buf)) > 0) {}
Branch: 1 branch
BranchTrueFalseTotal
buf, sizeof buf)) > 0) {} 2 2 4
2567 2 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 2 2
2568 0 croak("File::Raw::Archive::Entry::_skip: read_data failed");
2569 2 av_store(reader_av, R_CONSUMED, newSViv(1));
2570  
2571 SV *
2572 _new(class_sv, reader_sv, meta_sv)
2573 SV *class_sv
2574 SV *reader_sv
2575 SV *meta_sv
2576 PREINIT:
2577 AV *self;
2578 const char *cls;
2579 CODE:
2580 0 cls = SvPV_nolen(class_sv);
2581 0 self = newAV();
2582 0 av_extend(self, E_SLOT_COUNT - 1);
2583 0 av_store(self, E_READER, SvREFCNT_inc(reader_sv));
2584 0 av_store(self, E_META, SvREFCNT_inc(meta_sv));
2585 0 RETVAL = sv_bless(newRV_noinc((SV *)self), gv_stashpv(cls, GV_ADD));
2586 OUTPUT:
2587 RETVAL
2588  
2589 # ====================================================================
2590 MODULE = File::Raw::Archive PACKAGE = File::Raw::Archive
2591 # ====================================================================
2592 #
2593 # Function-style entry points: callable as plain functions instead of
2594 # class methods, so they slot into File::Raw's `file_<verb>` family.
2595 # Same semantics as the matching class methods minus the leading
2596 # `$class` arg. Imported into the caller's package via `import` below.
2597  
2598 SV *
2599 file_archive_open(...)
2600 PPCODE:
2601 {
2602 1 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 1 1
2603 0 croak("Usage: file_archive_open(\\$path, %%opts)");
2604 1 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 1 1
2605 0 croak("file_archive_open: odd number of options");
2606 1 HV *opts = build_opts_from_args(aTHX_ &ST(1), items - 1);
2607 1 archive_handle_t *h = open_reader(aTHX_ ST(0), opts);
2608 1 SV *handle_sv = new_handle_obj(aTHX_ h, "File::Raw::Archive::Reader");
2609  
2610 1 AV *self = newAV();
2611 1 av_extend(self, R_SLOT_COUNT - 1);
2612 1 av_store(self, R_HANDLE, handle_sv);
2613 1 av_store(self, R_CONSUMED, newSViv(1));
2614 1 av_store(self, R_CLOSED, newSViv(0));
2615 1 SV *obj = sv_bless(newRV_noinc((SV *)self),
2616 gv_stashpv("File::Raw::Archive::Reader", GV_ADD));
2617 1 ▶ XPUSHs(sv_2mortal(obj));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(obj)); 0 1 1
2618 1 XSRETURN(1);
2619 }
2620  
2621 SV *
2622 file_archive_create(...)
2623 PPCODE:
2624 {
2625 1 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 1 1
2626 0 croak("Usage: file_archive_create(\\$path, %%opts)");
2627 1 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 1 1
2628 0 croak("file_archive_create: odd number of options");
2629 1 HV *opts = build_opts_from_args(aTHX_ &ST(1), items - 1);
2630 1 archive_handle_t *h = open_writer(aTHX_ ST(0), opts);
2631 1 SV *handle_sv = new_handle_obj(aTHX_ h, "File::Raw::Archive::Writer");
2632  
2633 1 AV *self = newAV();
2634 1 av_extend(self, W_SLOT_COUNT - 1);
2635 1 av_store(self, W_HANDLE, handle_sv);
2636 1 av_store(self, W_CLOSED, newSViv(0));
2637 1 SV *obj = sv_bless(newRV_noinc((SV *)self),
2638 gv_stashpv("File::Raw::Archive::Writer", GV_ADD));
2639 1 ▶ XPUSHs(sv_2mortal(obj));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(obj)); 0 1 1
2640 1 XSRETURN(1);
2641 }
2642  
2643 SV *
2644 file_archive_list(...)
2645 PPCODE:
2646 {
2647 1 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 1 1
2648 0 croak("Usage: file_archive_list(\\$path, %%opts)");
2649 1 ▶ if ((items - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) 0 1 1
2650 0 croak("file_archive_list: odd number of options");
2651 1 HV *opts = build_opts_from_args(aTHX_ &ST(1), items - 1);
2652 1 archive_handle_t *h = open_reader(aTHX_ ST(0), opts);
2653 1 SAVEDESTRUCTOR_X(free_handle_destructor, h);
2654 1 SV *result = do_list(aTHX_ h);
2655 1 ▶ XPUSHs(sv_2mortal(result));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(result)); 0 1 1
2656 1 XSRETURN(1);
2657 }
2658  
2659 IV
2660 file_archive_extract(...)
2661 PPCODE:
2662 {
2663 1 ▶ if (items < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) 0 1 1
2664 0 croak("Usage: file_archive_extract(\\$path, \\$name, \\$dest, %%opts)");
2665 1 ▶ if ((items - 3) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2 != 0) 0 1 1
2666 0 croak("file_archive_extract: odd number of options");
2667 1 HV *opts = build_opts_from_args(aTHX_ &ST(3), items - 3);
2668 1 int apply_xattrs = 1;
2669 1 SV **xv = hv_fetchs(opts, "xattrs", 0);
2670 1 ▶ if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 1 1
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
2671  
2672 1 archive_handle_t *h = open_reader(aTHX_ ST(0), opts);
2673 1 SAVEDESTRUCTOR_X(free_handle_destructor, h);
2674  
2675 STRLEN match_len;
2676 1 const char *match_name = SvPV(ST(1), match_len);
2677 1 ▶ const char *dest_path = SvPV_nolen(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
const char *dest_path = SvPV_nolen(ST(2)); 1 0 1
2678 1 IV rc = do_extract_one(aTHX_ h, match_name, match_len, dest_path,
2679 apply_xattrs);
2680 1 ▶ XPUSHs(sv_2mortal(newSViv(rc)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(rc))); 0 1 1
2681 1 XSRETURN(1);
2682 }
2683  
2684 IV
2685 file_archive_extract_all(...)
2686 PPCODE:
2687 {
2688 1 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 1 1
2689 0 croak("Usage: file_archive_extract_all(\\$path, \\$dest, %%opts)");
2690 1 ▶ if ((items - 2) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2 != 0) 0 1 1
2691 0 croak("file_archive_extract_all: odd number of options");
2692 1 HV *opts = build_opts_from_args(aTHX_ &ST(2), items - 2);
2693 1 int apply_xattrs = 1;
2694 1 int unsafe_paths = 0;
2695 1 int parallel = 1;
2696 1 SV *filter = NULL;
2697 SV **xv;
2698  
2699 1 xv = hv_fetchs(opts, "xattrs", 0);
2700 1 ▶ if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 1 1
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) apply_xattrs = SvTRUE(*xv) ? 1 : 0; 0 0 0
2701 1 xv = hv_fetchs(opts, "unsafe_paths", 0);
2702 1 ▶ if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 1 1
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 0 0
if (xv && *xv && SvOK(*xv)) unsafe_paths = SvTRUE(*xv) ? 1 : 0; 0 0 0
2703 1 xv = hv_fetchs(opts, "entry_filter", 0);
2704 1 ▶ if (xv && *xv && SvOK(*xv)) filter = *xv;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 1 1
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
2705 1 xv = hv_fetchs(opts, "parallel", 0);
2706 1 ▶ if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv);
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 0 1 1
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 0 0 0
if (xv && *xv && SvOK(*xv)) parallel = (int)SvIV(*xv); 0 0 0
2707  
2708 1 ▶ if (parallel > 1 && !parallel_supported()) {
Branch: 2 branches
BranchTrueFalseTotal
if (parallel > 1 && !parallel_supported()) { 0 1 1
if (parallel > 1 && !parallel_supported()) { 0 0 0
2709 0 warn("File::Raw::Archive: parallel extract not supported on this "
2710 "platform; falling back to sequential\n");
2711 0 parallel = 1;
2712 }
2713  
2714 1 archive_handle_t *h = open_reader(aTHX_ ST(0), opts);
2715 1 SAVEDESTRUCTOR_X(free_handle_destructor, h);
2716  
2717 STRLEN dest_len;
2718 1 const char *dest = SvPV(ST(1), dest_len);
2719 1 ▶ if (parallel > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (parallel > 1) { 0 1 1
2720 0 do_extract_all_parallel(aTHX_ h, dest, dest_len,
2721 parallel, apply_xattrs,
2722 filter, unsafe_paths);
2723 } else {
2724 1 do_extract_all_seq(aTHX_ h, dest, dest_len,
2725 apply_xattrs, unsafe_paths, filter);
2726 }
2727 1 ▶ XPUSHs(sv_2mortal(newSViv(1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(1))); 0 1 1
2728 1 XSRETURN(1);
2729 }
2730  
2731 void
2732 file_archive_each(...)
2733 PPCODE:
2734 {
2735 1 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 1 1
2736 0 croak("Usage: file_archive_each(\\$path, %%opts, sub { ... })");
2737 1 SV *cb_sv = ST(items - 1);
2738 1 ▶ if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 1 0 1
if (!SvROK(cb_sv) || SvTYPE(SvRV(cb_sv)) != SVt_PVCV) 0 1 1
2739 0 croak("file_archive_each: last arg must be a coderef");
2740 1 int opts_end = items - 1;
2741 1 ▶ if ((opts_end - 1) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((opts_end - 1) % 2 != 0) 0 1 1
2742 0 croak("file_archive_each: odd number of options");
2743 1 HV *opts = build_opts_from_args(aTHX_ &ST(1), opts_end - 1);
2744 1 SV *filter = NULL;
2745 1 SV **xv = hv_fetchs(opts, "entry_filter", 0);
2746 1 ▶ if (xv && *xv && SvOK(*xv)) filter = *xv;
Branch: 3 branches
BranchTrueFalseTotal
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 1 1
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
if (xv && *xv && SvOK(*xv)) filter = *xv; 0 0 0
2747  
2748 1 archive_handle_t *h = open_reader(aTHX_ ST(0), opts);
2749  
2750 /* Build a Reader AV equivalent so Entry objects can call back. */
2751 1 AV *reader_av = newAV();
2752 1 av_extend(reader_av, R_SLOT_COUNT - 1);
2753 1 av_store(reader_av, R_HANDLE,
2754 new_handle_obj(aTHX_ h, "File::Raw::Archive::Reader"));
2755 1 av_store(reader_av, R_CONSUMED, newSViv(1));
2756 1 av_store(reader_av, R_CLOSED, newSViv(0));
2757 1 SV *reader_sv = sv_2mortal(newRV_noinc((SV *)reader_av));
2758 1 sv_bless(reader_sv, gv_stashpv("File::Raw::Archive::Reader", GV_ADD));
2759  
2760 1 do_each(aTHX_ h, reader_sv, cb_sv, filter);
2761 1 XSRETURN_EMPTY;
2762 }
2763  
2764 # Public surface installer. Called as
2765 # use File::Raw::Archive qw(import); # all six
2766 # or use File::Raw::Archive qw(each list); # specific subset
2767 # Walks the requested name list and aliases each into the caller's
2768 # stash as `file_archive_<name>`.
2769  
2770 void
2771 import(...)
2772 PPCODE:
2773 {
2774 /* ST(0) is the class. The rest are export-tag style names. */
2775 static const char * const known_names[] = {
2776 "open", "create", "list", "each", "extract", "extract_all", NULL
2777 };
2778 HV *caller_stash;
2779 {
2780 30 const char *caller_pkg = NULL;
2781 #if PERL_VERSION_GE(5, 14, 0)
2782 /* caller_cx available since 5.13.5; tie to 5.14 (first stable release) */
2783 30 const PERL_CONTEXT *cx = caller_cx(0, NULL);
2784 30 ▶ if (cx && CxTYPE(cx) == CXt_SUB) {
Branch: 2 branches
BranchTrueFalseTotal
if (cx && CxTYPE(cx) == CXt_SUB) { 30 0 30
if (cx && CxTYPE(cx) == CXt_SUB) { 30 0 30
2785 30 ▶ caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop));
Branch: 6 branches
BranchTrueFalseTotal
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 30 0 30
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 30 0 30
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 0 30 30
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 0 0 0
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 30 0 30
caller_pkg = HvNAME_get(CopSTASH(cx->blk_oldcop)); 0 30 30
2786 }
2787 #else
2788 /* On older Perls, PL_curcop inside an XSUB is still the caller's cop */
2789 if (PL_curcop)
2790 caller_pkg = HvNAME(CopSTASH(PL_curcop));
2791 #endif
2792 30 ▶ if (!caller_pkg) caller_pkg = "main";
Branch: 1 branch
BranchTrueFalseTotal
if (!caller_pkg) caller_pkg = "main"; 0 30 30
2793 30 caller_stash = gv_stashpv(caller_pkg, GV_ADD);
2794 }
2795  
2796 /* If only the bareword "import" is requested, install all known
2797 * names. Otherwise install just the requested subset. */
2798 30 int install_all = 0;
2799 int i;
2800 30 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 1 29 30
2801 1 ▶ if (!SvOK(ST(i))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(i))) continue; 0 1 1
2802 STRLEN nl;
2803 1 const char *np = SvPV(ST(i), nl);
2804 1 ▶ if ((nl == 6 && memcmp(np, "import", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ((nl == 6 && memcmp(np, "import", 6) == 0) 1 0 1
if ((nl == 6 && memcmp(np, "import", 6) == 0) 0 1 1
2805 0 ▶ || (nl == 4 && memcmp(np, ":all", 4) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (nl == 4 && memcmp(np, ":all", 4) == 0)) { 0 0 0
|| (nl == 4 && memcmp(np, ":all", 4) == 0)) { 0 0 0
2806 1 install_all = 1;
2807 1 break;
2808 }
2809 }
2810  
2811 int n;
2812 210 ▶ for (n = 0; known_names[n]; n++) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; known_names[n]; n++) { 180 30 210
2813 180 const char *name = known_names[n];
2814 180 int wanted = install_all;
2815 180 ▶ if (!wanted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wanted) { 174 6 180
2816 int j;
2817 174 ▶ for (j = 1; j < items; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < items; j++) { 0 174 174
2818 0 ▶ if (!SvOK(ST(j))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(j))) continue; 0 0 0
2819 STRLEN nl;
2820 0 const char *np = SvPV(ST(j), nl);
2821 0 ▶ if (nl == strlen(name) && memcmp(np, name, nl) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (nl == strlen(name) && memcmp(np, name, nl) == 0) { 0 0 0
if (nl == strlen(name) && memcmp(np, name, nl) == 0) { 0 0 0
2822 0 wanted = 1;
2823 0 break;
2824 }
2825 }
2826 }
2827 180 ▶ if (!wanted) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!wanted) continue; 174 6 180
2828  
2829 /* Source CV: File::Raw::Archive::file_archive_<name> */
2830 char src_full[128];
2831 6 snprintf(src_full, sizeof src_full,
2832 "File::Raw::Archive::file_archive_%s", name);
2833 6 CV *src_cv = get_cv(src_full, 0);
2834 6 ▶ if (!src_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!src_cv) { 0 6 6
2835 0 warn("File::Raw::Archive::import: %s not installed at BOOT",
2836 src_full);
2837 0 continue;
2838 }
2839  
2840 /* Destination CV name: file_archive_<name> in caller's stash. */
2841 char dst_short[64];
2842 6 snprintf(dst_short, sizeof dst_short, "file_archive_%s", name);
2843 6 GV *dst_gv = (GV *)*hv_fetch(caller_stash, dst_short,
2844 (I32)strlen(dst_short), 1);
2845 6 ▶ if (!isGV(dst_gv)) gv_init(dst_gv, caller_stash, dst_short,
Branch: 1 branch
BranchTrueFalseTotal
if (!isGV(dst_gv)) gv_init(dst_gv, caller_stash, dst_short, 6 0 6
2846 strlen(dst_short), GV_ADDMULTI);
2847 6 GvCV_set(dst_gv, src_cv);
2848 6 SvREFCNT_inc((SV *)src_cv);
2849 }
2850 30 XSRETURN_EMPTY;
2851 }
2852  
2853  
2854  
2855  
« arch_io.c ↑ Summary tar.c »