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