Archive.xs

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