file.c

filter: Statementstmt 78.1% 2636 / 3373 Branchbran 58.4% 1146 / 1960 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.9% 3782 / 5333 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/File/Raw/Install/Files.pm ↑ Summary blib/lib/File/Raw.pm »
1 /*
2 * file.c - Fast IO operations using direct system calls
3 *
4 * Features:
5 * - slurp/spew with minimal overhead
6 * - Memory-mapped file access (mmap)
7 * - Efficient line iteration
8 * - Direct stat access
9 * - Windows and POSIX support
10 */
11  
12 #define PERL_NO_GET_CONTEXT
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16 #include "include/file_compat.h"
17 #include "include/file_plugin.h"
18 #include "include/file_chunk.h"
19  
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23 #include <string.h>
24  
25 #ifdef _WIN32
26 #include <io.h>
27 #include <windows.h>
28 #include <direct.h>
29 /*
30 * Windows compatibility - use Perl's wrapper functions
31 * We DON'T redefine open/read/write/close/stat/fstat/access here
32 * because Perl's XSUB.h already defines them to work correctly.
33 * Just define the flags and other missing bits.
34 */
35 #define O_RDONLY _O_RDONLY
36 #define O_WRONLY _O_WRONLY
37 #define O_RDWR _O_RDWR
38 #define O_CREAT _O_CREAT
39 #define O_TRUNC _O_TRUNC
40 #define O_APPEND _O_APPEND
41 #define O_BINARY _O_BINARY
42 #ifndef S_ISREG
43 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
44 #endif
45 #ifndef S_ISDIR
46 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
47 #endif
48 #define R_OK 4
49 #define W_OK 2
50 /* ssize_t for Windows */
51 #ifndef ssize_t
52 #ifdef _WIN64
53 typedef __int64 ssize_t;
54 #else
55 typedef int ssize_t;
56 #endif
57 #endif
58 /* Windows doesn't have real uid/gid - use dummy values */
59 #define FILE_FAKE_UID 1000
60 #define FILE_FAKE_GID 1000
61 /*
62 * On Windows with PERL_IMPLICIT_SYS, Perl redefines open() to
63 * PerlLIO_open() which only accepts 2 args. Use _open() directly
64 * for the 3-arg form (path, flags, mode).
65 */
66 #define file_open3(path, flags, mode) _open(path, flags, mode)
67 #else
68 #define file_open3(path, flags, mode) open(path, flags, mode)
69 #include <unistd.h>
70 #include <sys/mman.h>
71 #include <utime.h> /* For utime - more portable than utimes */
72 #include <dirent.h> /* For readdir */
73 #if defined(__linux__)
74 #include <sys/sendfile.h> /* Zero-copy file transfer */
75 #endif
76 #if defined(__APPLE__)
77 #include <copyfile.h> /* macOS native file copy */
78 #endif
79 #endif
80  
81 /* Default buffer size for reads - 64KB is optimal for most systems */
82 #define FILE_BUFFER_SIZE 65536
83  
84 /* Larger buffer for bulk operations */
85 #define FILE_BULK_BUFFER_SIZE 262144
86  
87 /* Threshold for mmap-based slurp (4MB) */
88 #define MMAP_SLURP_THRESHOLD (4 * 1024 * 1024)
89  
90 /* Branch prediction hints */
91 #ifndef LIKELY
92 #if defined(__GNUC__) || defined(__clang__)
93 #define LIKELY(x) __builtin_expect(!!(x), 1)
94 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
95 #else
96 #define LIKELY(x) (x)
97 #define UNLIKELY(x) (x)
98 #endif
99 #endif
100  
101 /* posix_fadvise hints for kernel optimization */
102 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
103 #define HAVE_POSIX_FADVISE 1
104 #define advise_sequential(fd, len) posix_fadvise(fd, 0, len, POSIX_FADV_SEQUENTIAL)
105 #define advise_dontneed(fd, len) posix_fadvise(fd, 0, len, POSIX_FADV_DONTNEED)
106 #else
107 #define HAVE_POSIX_FADVISE 0
108 #define advise_sequential(fd, len) ((void)0)
109 #define advise_dontneed(fd, len) ((void)0)
110 #endif
111  
112 /* ============================================
113 Plugin registry + dispatch helpers.
114 Storage and definitions live here in the Raw.so TU; downstream XS
115 modules see only file_plugin.h and resolve the function symbols at
116 load time via RTLD_GLOBAL.
117 ============================================ */
118  
119 /* Registry: name (PV) -> FilePlugin* (stored via PTR2IV in the SV). */
120 static HV *g_file_plugin_registry = NULL;
121  
122 /* One-pointer cache: short-circuits HV lookup when the same plugin is
123 * used in tight loops (e.g. each_line). */
124 static const FilePlugin *g_file_last_plugin = NULL;
125  
126 58 static void file_plugin_registry_init(pTHX) {
127 58 ▶ if (!g_file_plugin_registry)
Branch: 1 branch
BranchTrueFalseTotal
if (!g_file_plugin_registry) 29 29 58
128 29 g_file_plugin_registry = newHV();
129 58 }
130  
131 58 int file_register_plugin(pTHX_ const FilePlugin *plugin) {
132 SV *entry;
133 STRLEN name_len;
134  
135 58 ▶ if (!plugin || !plugin->name || !*plugin->name) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!plugin || !plugin->name || !*plugin->name) return -1; 58 0 58
if (!plugin || !plugin->name || !*plugin->name) return -1; 58 0 58
if (!plugin || !plugin->name || !*plugin->name) return -1; 0 58 58
136  
137 58 file_plugin_registry_init(aTHX);
138 58 name_len = strlen(plugin->name);
139 58 ▶ if (hv_exists(g_file_plugin_registry, plugin->name, name_len))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(g_file_plugin_registry, plugin->name, name_len)) 1 57 58
140 1 return 0;
141  
142 57 entry = newSViv(PTR2IV(plugin));
143 57 ▶ if (!hv_store(g_file_plugin_registry, plugin->name, name_len, entry, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(g_file_plugin_registry, plugin->name, name_len, entry, 0)) { 0 57 57
144 0 SvREFCNT_dec(entry);
145 0 return -1;
146 }
147 57 return 1;
148 }
149  
150 14 int file_unregister_plugin(pTHX_ const char *name) {
151 14 ▶ if (!g_file_plugin_registry || !name) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!g_file_plugin_registry || !name) return 0; 14 0 14
if (!g_file_plugin_registry || !name) return 0; 0 14 14
152 14 ▶ if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0) 8 6 14
if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0) 8 0 8
153 8 g_file_last_plugin = NULL;
154 14 return hv_delete(g_file_plugin_registry, name, strlen(name), G_DISCARD)
155 14 ? 1 : 0;
156 }
157  
158 67 const FilePlugin *file_lookup_plugin(pTHX_ const char *name) {
159 SV **svp;
160 const FilePlugin *p;
161  
162 67 ▶ if (!name) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return NULL; 0 67 67
163 67 ▶ if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0) 49 18 67
if (g_file_last_plugin && strcmp(g_file_last_plugin->name, name) == 0) 21 28 49
164 21 return g_file_last_plugin;
165 46 ▶ if (!g_file_plugin_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_file_plugin_registry) return NULL; 0 46 46
166  
167 46 svp = hv_fetch(g_file_plugin_registry, name, strlen(name), 0);
168 46 ▶ if (!svp || !*svp) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !*svp) return NULL; 41 5 46
if (!svp || !*svp) return NULL; 0 41 41
169 41 p = INT2PTR(const FilePlugin*, SvIV(*svp));
170 41 g_file_last_plugin = p;
171 41 return p;
172 }
173  
174 /* ---- option-HV builder ---- */
175  
176 61 HV* file_plugin_build_opts(pTHX_ SV **stack, int start, int items,
177 const char *fn_name) {
178 HV *opts;
179 int i;
180 61 int has_plugin = 0;
181  
182 61 ▶ if (start >= items) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (start >= items) return NULL; 0 61 61
183 61 ▶ if ((items - start) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - start) % 2 != 0) 2 59 61
184 2 croak("File::Raw::%s: odd number of options (expected key => value pairs)",
185 fn_name);
186  
187 59 opts = newHV();
188 133 ▶ for (i = start; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < items; i += 2) { 74 59 133
189 74 SV *key_sv = stack[i];
190 74 SV *val_sv = stack[i + 1];
191 STRLEN key_len;
192 const char *key;
193  
194 74 ▶ if (!SvOK(key_sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(key_sv)) 0 74 74
195 0 croak("File::Raw::%s: option key at position %d is undef",
196 fn_name, i);
197 74 key = SvPV(key_sv, key_len);
198 74 ▶ if (!hv_store(opts, key, (I32)key_len, SvREFCNT_inc(val_sv), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(opts, key, (I32)key_len, SvREFCNT_inc(val_sv), 0)) { 0 74 74
199 0 SvREFCNT_dec(val_sv);
200 0 SvREFCNT_dec((SV*)opts);
201 0 croak("File::Raw::%s: failed to store option '%s'", fn_name, key);
202 }
203 74 ▶ if (key_len == 6 && memcmp(key, "plugin", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (key_len == 6 && memcmp(key, "plugin", 6) == 0) 56 18 74
if (key_len == 6 && memcmp(key, "plugin", 6) == 0) 56 0 56
204 56 has_plugin = 1;
205 }
206  
207 59 ▶ if (!has_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_plugin) { 3 56 59
208 3 SvREFCNT_dec((SV*)opts);
209 3 croak("File::Raw::%s: options passed without 'plugin' key", fn_name);
210 }
211 56 return opts;
212 }
213  
214 /* ---- dispatch helpers ---- */
215  
216 /* FilePluginChain — internal-only resolution result for a single dispatch
217 * call.
218 *
219 * Two shapes:
220 *
221 * Single-plugin (fast path, count == 1, shared == NULL):
222 * plugins[0] is the resolved plugin; opts is passed to it directly
223 * (no per-plugin slicing). This is the only path used when the
224 * caller passed `plugin => 'name'` as a scalar string — preserves
225 * today's behaviour byte-for-byte.
226 *
227 * Chain (count >= 1, shared != NULL):
228 * `plugin => [a, b, c]` (arrayref). `shared` holds top-level keys
229 * that did not match any plugin name's per-plugin sub-hash;
230 * `per_plugin[i]` (may be NULL) holds the sub-hashref the user
231 * gave for plugins[i]. `file_plugin_chain_iter_opts` builds a
232 * fresh per-iteration HV from these. */
233 typedef struct {
234 const FilePlugin **plugins; /* count slots */
235 int count;
236 HV *shared; /* NULL on single-plugin fast path */
237 HV **per_plugin; /* count slots, each may be NULL */
238 } FilePluginChain;
239  
240 static void
241 52 file_plugin_chain_init(FilePluginChain *chain) {
242 52 memset(chain, 0, sizeof *chain);
243 52 }
244  
245 static void
246 47 file_plugin_chain_free(pTHX_ FilePluginChain *chain) {
247 int i;
248 47 ▶ if (chain->per_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain->per_plugin) { 16 31 47
249 44 ▶ for (i = 0; i < chain->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < chain->count; i++) { 28 16 44
250 28 ▶ if (chain->per_plugin[i])
Branch: 1 branch
BranchTrueFalseTotal
if (chain->per_plugin[i]) 3 25 28
251 3 SvREFCNT_dec((SV *)chain->per_plugin[i]);
252 }
253 16 Safefree(chain->per_plugin);
254 16 chain->per_plugin = NULL;
255 }
256 47 ▶ if (chain->shared) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain->shared) { 16 31 47
257 16 SvREFCNT_dec((SV *)chain->shared);
258 16 chain->shared = NULL;
259 }
260 47 ▶ if (chain->plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain->plugins) { 47 0 47
261 47 Safefree(chain->plugins);
262 47 chain->plugins = NULL;
263 }
264 47 chain->count = 0;
265 47 }
266  
267 /* Resolve plugin chain. Accepts scalar (single-plugin fast path) or
268 * arrayref (chain). Croaks on undef, empty arrayref, unknown plugin
269 * name, or wrong-shape value. Caller must call file_plugin_chain_free
270 * on `out` before returning. */
271 static void
272 52 file_plugin_resolve_chain(pTHX_ HV *opts, const char *fn_name,
273 FilePluginChain *out)
274 {
275 SV **slot;
276 SV *plugin_sv;
277 AV *plugins_av;
278 SSize_t n;
279 SSize_t i;
280  
281 52 file_plugin_chain_init(out);
282  
283 52 slot = hv_fetchs(opts, "plugin", 0);
284 52 ▶ if (!slot || !*slot || !SvOK(*slot))
Branch: 3 branches
BranchTrueFalseTotal
if (!slot || !*slot || !SvOK(*slot)) 52 0 52
if (!slot || !*slot || !SvOK(*slot)) 52 0 52
if (!slot || !*slot || !SvOK(*slot)) 0 52 52
285 0 croak("File::Raw::%s: missing 'plugin' option", fn_name);
286 52 plugin_sv = *slot;
287  
288 /* Scalar fast path — single-plugin call, opts passed straight through. */
289 52 ▶ if (!SvROK(plugin_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(plugin_sv)) { 35 17 52
290 35 const char *name = SvPV_nolen(plugin_sv);
291 35 const FilePlugin *p = file_lookup_plugin(aTHX_ name);
292 35 ▶ if (!p) croak("File::Raw::%s: unknown plugin '%s'", fn_name, name);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("File::Raw::%s: unknown plugin '%s'", fn_name, name); 4 31 35
293 31 Newx(out->plugins, 1, const FilePlugin *);
294 31 out->plugins[0] = p;
295 31 out->count = 1;
296 31 return;
297 }
298  
299 17 ▶ if (SvTYPE(SvRV(plugin_sv)) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(plugin_sv)) != SVt_PVAV) 0 17 17
300 0 croak("File::Raw::%s: 'plugin' must be a string or arrayref of "
301 "plugin names", fn_name);
302  
303 17 plugins_av = (AV *)SvRV(plugin_sv);
304 17 n = av_len(plugins_av) + 1;
305 17 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 17 17
306 0 croak("File::Raw::%s: empty plugin chain", fn_name);
307  
308 17 ▶ Newx(out->plugins, n, const FilePlugin *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(out->plugins, n, const FilePlugin *); 0 17 17
309 17 out->count = (int)n;
310  
311 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 30 17 47
312 30 SV **np = av_fetch(plugins_av, i, 0);
313 const char *name;
314 const FilePlugin *p;
315 30 ▶ if (!np || !*np || !SvOK(*np))
Branch: 3 branches
BranchTrueFalseTotal
if (!np || !*np || !SvOK(*np)) 30 0 30
if (!np || !*np || !SvOK(*np)) 30 0 30
if (!np || !*np || !SvOK(*np)) 0 30 30
316 0 croak("File::Raw::%s: undef plugin name at chain index %ld",
317 fn_name, (long)i);
318 30 ▶ if (SvROK(*np))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(*np)) 0 30 30
319 0 croak("File::Raw::%s: plugin name at chain index %ld must "
320 "be a string", fn_name, (long)i);
321 30 name = SvPV_nolen(*np);
322 30 p = file_lookup_plugin(aTHX_ name);
323 30 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 30 30
324 0 croak("File::Raw::%s: unknown plugin '%s' (chain index %ld)",
325 fn_name, name, (long)i);
326 30 out->plugins[i] = p;
327 }
328  
329 /* Build shared HV + per-plugin slots. Walk every key in opts:
330 * - 'plugin' → skip (already consumed)
331 * - matches a plugin name AND is a hashref → per-plugin sub-hash
332 * - otherwise → shared bag, visible to every iteration */
333 17 out->shared = newHV();
334 17 ▶ Newxz(out->per_plugin, n, HV *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(out->per_plugin, n, HV *); 0 17 17
335  
336 {
337 HE *he;
338 17 hv_iterinit(opts);
339 39 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 22 17 39
340 I32 klen_i;
341 22 const char *key = hv_iterkey(he, &klen_i);
342 22 STRLEN klen = (STRLEN)klen_i;
343 22 SV *val = hv_iterval(opts, he);
344 22 int matched = -1;
345  
346 22 ▶ if (klen == 6 && memcmp(key, "plugin", 6) == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(key, "plugin", 6) == 0) continue; 17 5 22
if (klen == 6 && memcmp(key, "plugin", 6) == 0) continue; 17 0 17
347  
348 5 ▶ if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 3 2 5
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { 3 0 3
349 int j;
350 4 ▶ for (j = 0; j < (int)n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < (int)n; j++) { 4 0 4
351 4 const char *pn = out->plugins[j]->name;
352 4 STRLEN plen = strlen(pn);
353 4 ▶ if (plen == klen && memcmp(key, pn, klen) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (plen == klen && memcmp(key, pn, klen) == 0) { 3 1 4
if (plen == klen && memcmp(key, pn, klen) == 0) { 3 0 3
354 3 matched = j;
355 3 break;
356 }
357 }
358 }
359  
360 5 ▶ if (matched >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (matched >= 0) { 3 2 5
361 3 out->per_plugin[matched] = (HV *)SvRV(val);
362 3 SvREFCNT_inc((SV *)out->per_plugin[matched]);
363 } else {
364 2 (void)hv_store(out->shared, key, klen,
365 SvREFCNT_inc(val), 0);
366 }
367 }
368 }
369 }
370  
371 /* Resolve for phases that don't support chaining (RECORD, STREAM).
372 * Croaks if the user passed an arrayref. */
373 static const FilePlugin *
374 4 file_plugin_resolve_single(pTHX_ HV *opts, const char *fn_name)
375 {
376 4 SV **slot = hv_fetchs(opts, "plugin", 0);
377 const char *name;
378 const FilePlugin *p;
379 4 ▶ if (!slot || !*slot || !SvOK(*slot))
Branch: 3 branches
BranchTrueFalseTotal
if (!slot || !*slot || !SvOK(*slot)) 4 0 4
if (!slot || !*slot || !SvOK(*slot)) 4 0 4
if (!slot || !*slot || !SvOK(*slot)) 0 4 4
380 0 croak("File::Raw::%s: missing 'plugin' option", fn_name);
381 4 ▶ if (SvROK(*slot))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(*slot)) 2 2 4
382 2 croak("File::Raw::%s: plugin chains are not supported for the "
383 "'%s' phase (record/stream); pass a single plugin name "
384 "instead", fn_name, fn_name);
385 2 name = SvPV_nolen(*slot);
386 2 p = file_lookup_plugin(aTHX_ name);
387 2 ▶ if (!p) croak("File::Raw::%s: unknown plugin '%s'", fn_name, name);
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("File::Raw::%s: unknown plugin '%s'", fn_name, name); 1 1 2
388 1 return p;
389 }
390  
391 /* Build the per-iteration ctx->options HV for chain mode: shared bag
392 * with the indexed plugin's sub-hash overlaid on top (sub-hash wins on
393 * conflict). The 'plugin' key is set to the iterating plugin's own
394 * name, so plugins that read it (e.g. Separated's known_opt list)
395 * don't see something stale or array-shaped. Returns a fresh HV the
396 * dispatcher must SvREFCNT_dec after the iteration. */
397 static HV *
398 28 file_plugin_chain_iter_opts(pTHX_ FilePluginChain *chain, int idx)
399 {
400 28 HV *iter = newHV();
401 HE *he;
402  
403 28 ▶ if (chain->shared) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain->shared) { 28 0 28
404 28 hv_iterinit(chain->shared);
405 32 ▶ while ((he = hv_iternext(chain->shared))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(chain->shared))) { 4 28 32
406 I32 klen_i;
407 4 const char *key = hv_iterkey(he, &klen_i);
408 4 SV *val = hv_iterval(chain->shared, he);
409 4 (void)hv_store(iter, key, klen_i, SvREFCNT_inc(val), 0);
410 }
411 }
412 28 ▶ if (chain->per_plugin && chain->per_plugin[idx]) {
Branch: 2 branches
BranchTrueFalseTotal
if (chain->per_plugin && chain->per_plugin[idx]) { 28 0 28
if (chain->per_plugin && chain->per_plugin[idx]) { 3 25 28
413 3 HV *pp = chain->per_plugin[idx];
414 3 hv_iterinit(pp);
415 7 ▶ while ((he = hv_iternext(pp))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(pp))) { 4 3 7
416 I32 klen_i;
417 4 const char *key = hv_iterkey(he, &klen_i);
418 4 SV *val = hv_iterval(pp, he);
419 4 (void)hv_store(iter, key, klen_i, SvREFCNT_inc(val), 0);
420 }
421 }
422 28 (void)hv_store(iter, "plugin", 6,
423 newSVpv(chain->plugins[idx]->name, 0), 0);
424 28 return iter;
425 }
426  
427 /* READ dispatcher.
428 *
429 * Single-plugin scalar path: identical to today — opts goes straight
430 * through, plugin's return SV is returned bare, caller manages
431 * `if (out != bytes)` decref.
432 *
433 * Chain path: walks the resolved plugin list left-to-right, threading
434 * each plugin's return SV into the next call's ctx->data. Refcount
435 * discipline tracks "do we own the current SV?" so that whether the
436 * chain returns the input bytes unchanged or a fresh SV, the contract
437 * the caller sees is identical to the single-plugin path. */
438 37 SV* file_plugin_dispatch_read(pTHX_ HV *opts, const char *path, SV *bytes) {
439 FilePluginChain chain;
440 SV *current;
441 int we_own;
442 int i;
443  
444 37 file_plugin_resolve_chain(aTHX_ opts, "slurp", &chain);
445  
446 /* Scalar fast path: zero allocation overhead vs. today. */
447 34 ▶ if (chain.shared == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain.shared == NULL) { 23 11 34
448 23 const FilePlugin *p = chain.plugins[0];
449 FilePluginContext ctx;
450 SV *out;
451 23 ▶ if (!p->read_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->read_fn) { 0 23 23
452 0 file_plugin_chain_free(aTHX_ &chain);
453 0 croak("File::Raw: plugin '%s' has no read phase", p->name);
454 }
455 23 ctx.path = path;
456 23 ctx.data = bytes;
457 23 ctx.callback = NULL;
458 23 ctx.options = opts;
459 23 ctx.phase = FILE_PLUGIN_PHASE_READ;
460 23 ctx.cancel = 0;
461 23 ctx.plugin_state = p->state;
462 23 ctx.call_state = NULL;
463 23 out = p->read_fn(aTHX_ &ctx);
464 23 file_plugin_chain_free(aTHX_ &chain);
465 23 ▶ if (ctx.cancel) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.cancel) return NULL; 1 22 23
466 22 return out;
467 }
468  
469 /* Chain path. */
470 11 current = bytes;
471 11 we_own = 0;
472  
473 29 ▶ for (i = 0; i < chain.count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < chain.count; i++) { 20 9 29
474 20 const FilePlugin *p = chain.plugins[i];
475 FilePluginContext ctx;
476 HV *iter_opts;
477 SV *next;
478  
479 20 ▶ if (!p->read_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->read_fn) { 0 20 20
480 0 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 0 0 0
481 0 file_plugin_chain_free(aTHX_ &chain);
482 0 croak("File::Raw: plugin '%s' has no read phase "
483 "(chain index %d)", p->name, i);
484 }
485  
486 20 iter_opts = file_plugin_chain_iter_opts(aTHX_ &chain, i);
487 20 ctx.path = path;
488 20 ctx.data = current;
489 20 ctx.callback = NULL;
490 20 ctx.options = iter_opts;
491 20 ctx.phase = FILE_PLUGIN_PHASE_READ;
492 20 ctx.cancel = 0;
493 20 ctx.plugin_state = p->state;
494 20 ctx.call_state = NULL;
495  
496 20 next = p->read_fn(aTHX_ &ctx);
497 19 SvREFCNT_dec((SV *)iter_opts);
498  
499 19 ▶ if (ctx.cancel || !next) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx.cancel || !next) { 18 1 19
if (ctx.cancel || !next) { 0 18 18
500 1 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 1 0 1
501 1 file_plugin_chain_free(aTHX_ &chain);
502 1 return NULL;
503 }
504  
505 18 ▶ if (next != current) {
Branch: 1 branch
BranchTrueFalseTotal
if (next != current) { 18 0 18
506 18 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 7 11 18
507 18 current = next;
508 18 we_own = 1; /* per existing convention, plugin gives us +1 */
509 }
510 /* else: plugin returned the same SV; ownership unchanged. */
511 }
512  
513 9 file_plugin_chain_free(aTHX_ &chain);
514 /* Contract matches today's: if we never replaced bytes, we return
515 * bytes (caller still owns the +1 we never touched). If we replaced
516 * it, we return the fresh SV with +1 from the last plugin, exactly
517 * as today's single-plugin path would. */
518 9 return current;
519 }
520  
521 /* WRITE dispatcher. Mirror image of READ — same chain mechanics, but
522 * iterates RIGHT TO LEFT. The user's payload (which can be structured —
523 * AoA, AoH, etc.) flows into the *last* plugin first; that plugin emits
524 * bytes; subsequent (earlier-listed) plugins wrap those bytes. The byte
525 * stream produced by the FIRST plugin is what gets written to disk.
526 *
527 * Mnemonic: same array spelling for read and write — the array describes
528 * the encoding stack from outermost wrapper to innermost format. */
529 15 SV* file_plugin_dispatch_write(pTHX_ HV *opts, const char *path, SV *payload) {
530 FilePluginChain chain;
531 SV *current;
532 int we_own;
533 int i;
534  
535 15 file_plugin_resolve_chain(aTHX_ opts, "spew", &chain);
536  
537 14 ▶ if (chain.shared == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (chain.shared == NULL) { 8 6 14
538 8 const FilePlugin *p = chain.plugins[0];
539 FilePluginContext ctx;
540 SV *out;
541 8 ▶ if (!p->write_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->write_fn) { 1 7 8
542 1 file_plugin_chain_free(aTHX_ &chain);
543 1 croak("File::Raw: plugin '%s' has no write phase", p->name);
544 }
545 7 ctx.path = path;
546 7 ctx.data = payload;
547 7 ctx.callback = NULL;
548 7 ctx.options = opts;
549 7 ctx.phase = FILE_PLUGIN_PHASE_WRITE;
550 7 ctx.cancel = 0;
551 7 ctx.plugin_state = p->state;
552 7 ctx.call_state = NULL;
553 7 out = p->write_fn(aTHX_ &ctx);
554 7 file_plugin_chain_free(aTHX_ &chain);
555 7 ▶ if (ctx.cancel) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.cancel) return NULL; 1 6 7
556 6 return out;
557 }
558  
559 6 current = payload;
560 6 we_own = 0;
561  
562 13 ▶ for (i = chain.count - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = chain.count - 1; i >= 0; i--) { 8 5 13
563 8 const FilePlugin *p = chain.plugins[i];
564 FilePluginContext ctx;
565 HV *iter_opts;
566 SV *next;
567  
568 8 ▶ if (!p->write_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p->write_fn) { 0 8 8
569 0 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 0 0 0
570 0 file_plugin_chain_free(aTHX_ &chain);
571 0 croak("File::Raw: plugin '%s' has no write phase "
572 "(chain index %d)", p->name, i);
573 }
574  
575 8 iter_opts = file_plugin_chain_iter_opts(aTHX_ &chain, i);
576 8 ctx.path = path;
577 8 ctx.data = current;
578 8 ctx.callback = NULL;
579 8 ctx.options = iter_opts;
580 8 ctx.phase = FILE_PLUGIN_PHASE_WRITE;
581 8 ctx.cancel = 0;
582 8 ctx.plugin_state = p->state;
583 8 ctx.call_state = NULL;
584  
585 8 next = p->write_fn(aTHX_ &ctx);
586 8 SvREFCNT_dec((SV *)iter_opts);
587  
588 8 ▶ if (ctx.cancel || !next) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx.cancel || !next) { 7 1 8
if (ctx.cancel || !next) { 0 7 7
589 1 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 1 0 1
590 1 file_plugin_chain_free(aTHX_ &chain);
591 1 return NULL;
592 }
593  
594 7 ▶ if (next != current) {
Branch: 1 branch
BranchTrueFalseTotal
if (next != current) { 7 0 7
595 7 ▶ if (we_own) SvREFCNT_dec(current);
Branch: 1 branch
BranchTrueFalseTotal
if (we_own) SvREFCNT_dec(current); 1 6 7
596 7 current = next;
597 7 we_own = 1;
598 }
599 }
600  
601 5 file_plugin_chain_free(aTHX_ &chain);
602 5 return current;
603 }
604  
605 /* RECORD dispatcher — single-plugin only. Chains are rejected because
606 * a "record" is one already-parsed unit; threading it through multiple
607 * record fns would require the records to remain the same shape across
608 * links, which collapses the abstraction. */
609 0 SV* file_plugin_dispatch_record(pTHX_ HV *opts, const char *path, SV *record) {
610 0 const FilePlugin *p = file_plugin_resolve_single(aTHX_ opts, "record");
611 FilePluginContext ctx;
612 SV *out;
613  
614 0 ▶ if (!p->record_fn)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->record_fn) 0 0 0
615 0 croak("File::Raw: plugin '%s' has no record phase", p->name);
616  
617 0 ctx.path = path;
618 0 ctx.data = NULL;
619 0 ctx.callback = NULL;
620 0 ctx.options = opts;
621 0 ctx.phase = FILE_PLUGIN_PHASE_RECORD;
622 0 ctx.cancel = 0;
623 0 ctx.plugin_state = p->state;
624 0 ctx.call_state = NULL;
625  
626 0 out = p->record_fn(aTHX_ &ctx, record);
627 0 ▶ if (ctx.cancel) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.cancel) return NULL; 0 0 0
628 0 return out;
629 }
630  
631 /* the save stack closes the descriptor, so a die in the plugin or in the
632 * per-record callback does not leak it; on Windows a leaked descriptor
633 * also keeps the file locked against unlink */
634 0 static void file_close_fd(pTHX_ void *p) {
635 PERL_UNUSED_CONTEXT;
636 0 close((int)(IV)p);
637 0 }
638  
639 /* file_plugin_dispatch_stream is defined below - it relies on
640 * FILE_BUFFER_SIZE and the platform open()/read() wrappers. */
641 4 SV* file_plugin_dispatch_stream(pTHX_ HV *opts, const char *path, SV *cb) {
642 4 const FilePlugin *p = file_plugin_resolve_single(aTHX_ opts, "each_line");
643 FilePluginContext ctx;
644 char buf[FILE_BUFFER_SIZE];
645 int fd;
646 ssize_t n;
647 1 int cancelled = 0;
648  
649 1 ▶ if (!p->stream_fn)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->stream_fn) 1 0 1
650 1 croak("File::Raw: plugin '%s' has no stream phase", p->name);
651  
652 /* O_BINARY on Windows: without it the CRT puts the descriptor in
653 * text mode and strips \r from any \r\n in the read buffer before
654 * the plugin's stream hook sees it. Hash-style plugins (and any
655 * other binary consumer) then see a different byte stream than
656 * the one-shot read path, which already sets O_BINARY. No-op on
657 * Unix where O_BINARY is defined to 0. */
658 {
659 0 int open_flags = O_RDONLY;
660 #ifdef _WIN32
661 open_flags |= O_BINARY;
662 #endif
663 0 fd = file_open3(path, open_flags, 0);
664 }
665 0 ▶ if (fd < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return NULL; 0 0 0
666 0 ENTER;
667 0 SAVEDESTRUCTOR_X(file_close_fd, INT2PTR(void *, (IV)fd));
668  
669 0 ctx.path = path;
670 0 ctx.data = NULL;
671 0 ctx.callback = cb;
672 0 ctx.options = opts;
673 0 ctx.phase = FILE_PLUGIN_PHASE_STREAM;
674 0 ctx.cancel = 0;
675 0 ctx.plugin_state = p->state;
676 0 ctx.call_state = NULL;
677  
678 0 ▶ while ((n = read(fd, buf, sizeof(buf))) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((n = read(fd, buf, sizeof(buf))) > 0) { 0 0 0
679 0 ▶ if (p->stream_fn(aTHX_ &ctx, buf, (size_t)n, 0) || ctx.cancel) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->stream_fn(aTHX_ &ctx, buf, (size_t)n, 0) || ctx.cancel) { 0 0 0
if (p->stream_fn(aTHX_ &ctx, buf, (size_t)n, 0) || ctx.cancel) { 0 0 0
680 0 cancelled = 1;
681 0 break;
682 }
683 }
684 0 ▶ if (!cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cancelled) { 0 0 0
685 /* EOF flush so the plugin can emit any buffered final record. */
686 0 p->stream_fn(aTHX_ &ctx, NULL, 0, 1);
687 }
688 0 LEAVE; /* closes fd */
689 0 ▶ return (cancelled || ctx.cancel) ? NULL : &PL_sv_yes;
Branch: 2 branches
BranchTrueFalseTotal
return (cancelled || ctx.cancel) ? NULL : &PL_sv_yes; 0 0 0
return (cancelled || ctx.cancel) ? NULL : &PL_sv_yes; 0 0 0
690 }
691  
692 /* ============================================
693 Stat cache - like Perl's _ special filehandle
694 ============================================ */
695 #define STAT_CACHE_PATH_MAX 1024
696  
697 static struct {
698 char path[STAT_CACHE_PATH_MAX];
699 Stat_t st;
700 int valid;
701 #ifdef _WIN32
702 int uid;
703 int gid;
704 #else
705 uid_t uid;
706 gid_t gid;
707 #endif
708 } g_stat_cache = { "", {0}, 0, 0, 0 };
709  
710 /* Get cached stat or perform new stat */
711 393 static int cached_stat(const char *path, Stat_t *st) {
712 dTHX;
713 393 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 337 56 393
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 149 188 337
714 149 *st = g_stat_cache.st;
715 149 return 0;
716 }
717
718 244 ▶ if (stat(path, st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, st) < 0) { 32 212 244
719 32 g_stat_cache.valid = 0;
720 32 return -1;
721 }
722
723 /* Cache the result */
724 212 size_t len = strlen(path);
725 212 ▶ if (len < STAT_CACHE_PATH_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < STAT_CACHE_PATH_MAX) { 212 0 212
726 212 memcpy(g_stat_cache.path, path, len + 1);
727 212 g_stat_cache.st = *st;
728 #ifdef _WIN32
729 /* Windows doesn't have real uid/gid concepts */
730 g_stat_cache.uid = FILE_FAKE_UID;
731 g_stat_cache.gid = FILE_FAKE_GID;
732 #else
733 212 g_stat_cache.uid = geteuid();
734 212 g_stat_cache.gid = getegid();
735 #endif
736 212 g_stat_cache.valid = 1;
737 }
738
739 212 return 0;
740 }
741  
742 /* Invalidate cache (call after write operations) */
743 4 static void invalidate_stat_cache(void) {
744 4 g_stat_cache.valid = 0;
745 4 }
746  
747 /* Invalidate cache for specific path */
748 1 static void invalidate_stat_cache_path(const char *path) {
749 1 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 1 0 1
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 1 0 1
750 1 g_stat_cache.valid = 0;
751 }
752 1 }
753  
754 /* Check readable using cached stat */
755 9 static int file_is_readable_cached(const char *path) {
756 dTHX;
757 #ifdef _WIN32
758 return access(path, R_OK) == 0;
759 #else
760 Stat_t st;
761 9 ▶ if (cached_stat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) return 0; 1 8 9
762
763 8 ▶ if (g_stat_cache.uid == 0) return 1; /* root can read anything */
Branch: 1 branch
BranchTrueFalseTotal
if (g_stat_cache.uid == 0) return 1; /* root can read anything */ 0 8 8
764
765 8 ▶ if (st.st_uid == g_stat_cache.uid) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_uid == g_stat_cache.uid) { 8 0 8
766 8 return (st.st_mode & S_IRUSR) != 0;
767 0 ▶ } else if (st.st_gid == g_stat_cache.gid) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st.st_gid == g_stat_cache.gid) { 0 0 0
768 0 return (st.st_mode & S_IRGRP) != 0;
769 } else {
770 0 return (st.st_mode & S_IROTH) != 0;
771 }
772 #endif
773 }
774  
775 /* Check writable using cached stat */
776 9 static int file_is_writable_cached(const char *path) {
777 dTHX;
778 #ifdef _WIN32
779 return access(path, W_OK) == 0;
780 #else
781 Stat_t st;
782 9 ▶ if (cached_stat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) return 0; 1 8 9
783
784 8 ▶ if (g_stat_cache.uid == 0) return 1; /* root can write anything */
Branch: 1 branch
BranchTrueFalseTotal
if (g_stat_cache.uid == 0) return 1; /* root can write anything */ 0 8 8
785
786 8 ▶ if (st.st_uid == g_stat_cache.uid) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_uid == g_stat_cache.uid) { 8 0 8
787 8 return (st.st_mode & S_IWUSR) != 0;
788 0 ▶ } else if (st.st_gid == g_stat_cache.gid) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st.st_gid == g_stat_cache.gid) { 0 0 0
789 0 return (st.st_mode & S_IWGRP) != 0;
790 } else {
791 0 return (st.st_mode & S_IWOTH) != 0;
792 }
793 #endif
794 }
795  
796 /* Check executable using cached stat */
797 10 static int file_is_executable_cached(const char *path) {
798 dTHX;
799 #ifdef _WIN32
800 /* Windows: check file extension for executability */
801 const char *ext = strrchr(path, '.');
802 if (ext) {
803 if (_stricmp(ext, ".exe") == 0 || _stricmp(ext, ".bat") == 0 ||
804 _stricmp(ext, ".cmd") == 0 || _stricmp(ext, ".com") == 0) {
805 return access(path, R_OK) == 0;
806 }
807 }
808 return 0;
809 #else
810 Stat_t st;
811 10 ▶ if (cached_stat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) return 0; 0 10 10
812
813 10 ▶ if (g_stat_cache.uid == 0) return 1; /* root can execute anything */
Branch: 1 branch
BranchTrueFalseTotal
if (g_stat_cache.uid == 0) return 1; /* root can execute anything */ 0 10 10
814
815 10 ▶ if (st.st_uid == g_stat_cache.uid) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_uid == g_stat_cache.uid) { 10 0 10
816 10 return (st.st_mode & S_IXUSR) != 0;
817 0 ▶ } else if (st.st_gid == g_stat_cache.gid) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st.st_gid == g_stat_cache.gid) { 0 0 0
818 0 return (st.st_mode & S_IXGRP) != 0;
819 } else {
820 0 return (st.st_mode & S_IXOTH) != 0;
821 }
822 #endif
823 }
824  
825  
826 /* ============================================
827 Custom op support for compile-time optimization
828 ============================================ */
829  
830 /* Custom op registrations */
831 static XOP file_slurp_xop;
832 static XOP file_spew_xop;
833 static XOP file_exists_xop;
834 static XOP file_size_xop;
835 static XOP file_is_file_xop;
836 static XOP file_is_dir_xop;
837 static XOP file_lines_xop;
838 static XOP file_unlink_xop;
839 static XOP file_mkdir_xop;
840 static XOP file_rmdir_xop;
841 static XOP file_basename_xop;
842 static XOP file_dirname_xop;
843 static XOP file_extname_xop;
844 static XOP file_touch_xop;
845 static XOP file_clear_stat_cache_xop;
846 static XOP file_mtime_xop;
847 static XOP file_atime_xop;
848 static XOP file_ctime_xop;
849 static XOP file_mode_xop;
850 static XOP file_is_link_xop;
851 static XOP file_is_readable_xop;
852 static XOP file_is_writable_xop;
853 static XOP file_is_executable_xop;
854 static XOP file_readdir_xop;
855 static XOP file_slurp_raw_xop;
856 static XOP file_copy_xop;
857 static XOP file_move_xop;
858 static XOP file_chmod_xop;
859 static XOP file_append_xop;
860 static XOP file_atomic_spew_xop;
861  
862 /* Forward declarations for internal functions */
863 static SV* file_slurp_internal(pTHX_ const char *path);
864 static SV* file_slurp_raw_internal(pTHX_ const char *path);
865 static int file_spew_internal(pTHX_ const char *path, SV *data);
866 static int file_append_internal(pTHX_ const char *path, SV *data);
867 static IV file_size_internal(const char *path);
868 static IV file_mtime_internal(const char *path);
869 static IV file_atime_internal(const char *path);
870 static IV file_ctime_internal(const char *path);
871 static IV file_mode_internal(const char *path);
872 static int file_exists_internal(const char *path);
873 static int file_is_file_internal(const char *path);
874 static int file_is_dir_internal(const char *path);
875 static int file_is_link_internal(const char *path);
876 static int file_is_readable_internal(const char *path);
877 static int file_is_writable_internal(const char *path);
878 static int file_is_executable_internal(const char *path);
879 static AV* file_split_lines(pTHX_ SV *content);
880 static int file_unlink_internal(const char *path);
881 static int file_copy_internal(pTHX_ const char *src, const char *dst);
882 static int file_move_internal(pTHX_ const char *src, const char *dst);
883 static int file_mkdir_internal(const char *path, int mode);
884 static int file_rmdir_internal(const char *path);
885 static int file_touch_internal(const char *path);
886 static int file_chmod_internal(const char *path, int mode);
887 static AV* file_readdir_internal(pTHX_ const char *path);
888 static int file_atomic_spew_internal(pTHX_ const char *path, SV *data);
889 static SV* file_basename_internal(pTHX_ const char *path);
890 static SV* file_dirname_internal(pTHX_ const char *path);
891 static SV* file_extname_internal(pTHX_ const char *path);
892  
893 /* Typedef for pp functions */
894 typedef OP* (*file_ppfunc)(pTHX);
895  
896 /* ============================================
897 Custom OP implementations - fastest path
898 ============================================ */
899  
900 /* pp_file_slurp: single path arg on stack - OPTIMIZED HOT PATH */
901 76 static OP* pp_file_slurp(pTHX) {
902 76 dSP;
903 76 SV *path_sv = POPs;
904 76 const char *path = SvPV_nolen(path_sv);
905 int fd;
906 Stat_t st;
907 SV *result;
908 char *buf;
909 ssize_t n, total;
910  
911 /* Fast path: direct syscalls. The custom op only fires when the
912 * call-checker accepted exactly one arg, so there is no plugin tail
913 * to dispatch here - the variadic XSUB owns that path. */
914 #ifdef _WIN32
915 fd = open(path, O_RDONLY | O_BINARY);
916 #else
917 76 fd = open(path, O_RDONLY);
918 #endif
919 76 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 76 76
920 0 PUSHs(&PL_sv_undef);
921 0 PUTBACK;
922 0 return NORMAL;
923 }
924  
925 76 ▶ if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) { 76 0 76
if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) { 0 76 76
926 0 close(fd);
927 0 PUSHs(&PL_sv_undef);
928 0 PUTBACK;
929 0 return NORMAL;
930 }
931  
932 /* Empty file */
933 76 ▶ if (st.st_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size == 0) { 1 75 76
934 1 close(fd);
935 1 result = newSVpvs("");
936 1 PUSHs(sv_2mortal(result));
937 1 PUTBACK;
938 1 return NORMAL;
939 }
940  
941 /* Hint to kernel: sequential read */
942 75 advise_sequential(fd, st.st_size);
943  
944 /* Pre-allocate exact size */
945 75 result = newSV(st.st_size + 1);
946 75 SvPOK_on(result);
947 75 buf = SvPVX(result);
948  
949 #ifndef _WIN32
950 /* Large files: use mmap for zero-copy */
951 75 ▶ if (st.st_size >= MMAP_SLURP_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size >= MMAP_SLURP_THRESHOLD) { 0 75 75
952 0 void *map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
953 0 ▶ if (map != MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (map != MAP_FAILED) { 0 0 0
954 #ifdef MADV_SEQUENTIAL
955 0 madvise(map, st.st_size, MADV_SEQUENTIAL);
956 #endif
957 0 memcpy(buf, map, st.st_size);
958 0 buf[st.st_size] = '\0';
959 0 SvCUR_set(result, st.st_size);
960 0 munmap(map, st.st_size);
961 0 close(fd);
962 0 PUSHs(sv_2mortal(result));
963 0 PUTBACK;
964 0 return NORMAL;
965 }
966 /* mmap failed, fall through to read */
967 }
968 #endif
969  
970 /* Single read - common case for small/medium files */
971 75 n = read(fd, buf, st.st_size);
972 75 ▶ if (n == st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == st.st_size) { 75 0 75
973 /* Got everything in one read - fast path */
974 75 close(fd);
975 75 buf[n] = '\0';
976 75 SvCUR_set(result, n);
977 75 PUSHs(sv_2mortal(result));
978 75 PUTBACK;
979 75 return NORMAL;
980 }
981
982 /* Short read or error - need loop */
983 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
984 0 ▶ if (errno == EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) { 0 0 0
985 0 n = 0; /* Start from beginning */
986 } else {
987 0 close(fd);
988 0 SvREFCNT_dec(result);
989 0 PUSHs(&PL_sv_undef);
990 0 PUTBACK;
991 0 return NORMAL;
992 }
993 }
994
995 0 total = n;
996 0 ▶ while (total < st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (total < st.st_size) { 0 0 0
997 0 n = read(fd, buf + total, st.st_size - total);
998 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
999 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1000 0 close(fd);
1001 0 SvREFCNT_dec(result);
1002 0 PUSHs(&PL_sv_undef);
1003 0 PUTBACK;
1004 0 return NORMAL;
1005 }
1006 0 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 0 0
1007 0 total += n;
1008 }
1009
1010 0 close(fd);
1011 0 buf[total] = '\0';
1012 0 SvCUR_set(result, total);
1013
1014 0 PUSHs(sv_2mortal(result));
1015 0 PUTBACK;
1016 0 return NORMAL;
1017 }
1018  
1019 /* pp_file_spew: path and data on stack */
1020 86 static OP* pp_file_spew(pTHX) {
1021 86 dSP;
1022 86 SV *data = POPs;
1023 86 SV *path_sv = POPs;
1024 86 const char *path = SvPV_nolen(path_sv);
1025 86 ▶ PUSHs(file_spew_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_spew_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no); 86 0 86
1026 86 PUTBACK;
1027 86 return NORMAL;
1028 }
1029  
1030 /* pp_file_exists: single path arg on stack */
1031 147 static OP* pp_file_exists(pTHX) {
1032 147 dSP;
1033 147 SV *path_sv = POPs;
1034 147 const char *path = SvPV_nolen(path_sv);
1035 147 ▶ PUSHs(file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no); 140 7 147
1036 147 PUTBACK;
1037 147 return NORMAL;
1038 }
1039  
1040 /* pp_file_size: single path arg on stack */
1041 54 static OP* pp_file_size(pTHX) {
1042 54 dSP;
1043 54 SV *path_sv = POPs;
1044 54 const char *path = SvPV_nolen(path_sv);
1045 54 PUSHs(sv_2mortal(newSViv(file_size_internal(path))));
1046 54 PUTBACK;
1047 54 return NORMAL;
1048 }
1049  
1050 /* pp_file_is_file: single path arg on stack */
1051 29 static OP* pp_file_is_file(pTHX) {
1052 29 dSP;
1053 29 SV *path_sv = POPs;
1054 29 const char *path = SvPV_nolen(path_sv);
1055 29 ▶ PUSHs(file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no); 17 12 29
1056 29 PUTBACK;
1057 29 return NORMAL;
1058 }
1059  
1060 /* pp_file_is_dir: single path arg on stack */
1061 24 static OP* pp_file_is_dir(pTHX) {
1062 24 dSP;
1063 24 SV *path_sv = POPs;
1064 24 const char *path = SvPV_nolen(path_sv);
1065 24 ▶ PUSHs(file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no); 10 14 24
1066 24 PUTBACK;
1067 24 return NORMAL;
1068 }
1069  
1070 /* pp_file_lines: single path arg on stack */
1071 6 static OP* pp_file_lines(pTHX) {
1072 6 dSP;
1073 6 SV *path_sv = POPs;
1074 6 const char *path = SvPV_nolen(path_sv);
1075 6 SV *content = file_slurp_internal(aTHX_ path);
1076 AV *lines;
1077  
1078 6 ▶ if (content == &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (content == &PL_sv_undef) { 0 6 6
1079 0 lines = newAV();
1080 } else {
1081 6 lines = file_split_lines(aTHX_ content);
1082 6 SvREFCNT_dec(content);
1083 }
1084  
1085 6 PUSHs(sv_2mortal(newRV_noinc((SV*)lines)));
1086 6 PUTBACK;
1087 6 return NORMAL;
1088 }
1089  
1090 /* pp_file_unlink: single path arg on stack */
1091 2 static OP* pp_file_unlink(pTHX) {
1092 2 dSP;
1093 2 SV *path_sv = POPs;
1094 2 const char *path = SvPV_nolen(path_sv);
1095 2 ▶ PUSHs(file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no); 1 1 2
1096 2 PUTBACK;
1097 2 return NORMAL;
1098 }
1099  
1100 /* pp_file_clear_stat_cache: optional path arg - clears stat cache */
1101 1 static OP* pp_file_clear_stat_cache(pTHX) {
1102 1 dSP;
1103 1 SV *path_sv = POPs;
1104
1105 1 ▶ if (SvOK(path_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(path_sv)) { 1 0 1
1106 /* Clear cache for specific path */
1107 1 const char *path = SvPV_nolen(path_sv);
1108 1 invalidate_stat_cache_path(path);
1109 } else {
1110 /* Clear entire cache */
1111 0 invalidate_stat_cache();
1112 }
1113
1114 1 PUSHs(&PL_sv_yes);
1115 1 PUTBACK;
1116 1 return NORMAL;
1117 }
1118  
1119 /* pp_file_mkdir: single path arg on stack (mode defaults to 0755) */
1120 4 static OP* pp_file_mkdir(pTHX) {
1121 4 dSP;
1122 4 SV *path_sv = POPs;
1123 4 const char *path = SvPV_nolen(path_sv);
1124 4 ▶ PUSHs(file_mkdir_internal(path, 0755) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_mkdir_internal(path, 0755) ? &PL_sv_yes : &PL_sv_no); 4 0 4
1125 4 PUTBACK;
1126 4 return NORMAL;
1127 }
1128  
1129 /* pp_file_rmdir: single path arg on stack */
1130 1 static OP* pp_file_rmdir(pTHX) {
1131 1 dSP;
1132 1 SV *path_sv = POPs;
1133 1 const char *path = SvPV_nolen(path_sv);
1134 1 ▶ PUSHs(file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no); 1 0 1
1135 1 PUTBACK;
1136 1 return NORMAL;
1137 }
1138  
1139 /* pp_file_touch: single path arg on stack */
1140 1 static OP* pp_file_touch(pTHX) {
1141 1 dSP;
1142 1 SV *path_sv = POPs;
1143 1 const char *path = SvPV_nolen(path_sv);
1144 1 ▶ PUSHs(file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no); 1 0 1
1145 1 PUTBACK;
1146 1 return NORMAL;
1147 }
1148  
1149 /* pp_file_basename: single path arg on stack */
1150 71 static OP* pp_file_basename(pTHX) {
1151 71 dSP;
1152 71 SV *path_sv = POPs;
1153 71 const char *path = SvPV_nolen(path_sv);
1154 71 PUSHs(sv_2mortal(file_basename_internal(aTHX_ path)));
1155 71 PUTBACK;
1156 71 return NORMAL;
1157 }
1158  
1159 /* pp_file_dirname: single path arg on stack */
1160 10 static OP* pp_file_dirname(pTHX) {
1161 10 dSP;
1162 10 SV *path_sv = POPs;
1163 10 const char *path = SvPV_nolen(path_sv);
1164 10 PUSHs(sv_2mortal(file_dirname_internal(aTHX_ path)));
1165 10 PUTBACK;
1166 10 return NORMAL;
1167 }
1168  
1169 /* pp_file_extname: single path arg on stack */
1170 36 static OP* pp_file_extname(pTHX) {
1171 36 dSP;
1172 36 SV *path_sv = POPs;
1173 36 const char *path = SvPV_nolen(path_sv);
1174 36 PUSHs(sv_2mortal(file_extname_internal(aTHX_ path)));
1175 36 PUTBACK;
1176 36 return NORMAL;
1177 }
1178  
1179 /* pp_file_mtime: single path arg on stack */
1180 27 static OP* pp_file_mtime(pTHX) {
1181 27 dSP;
1182 27 SV *path_sv = POPs;
1183 27 const char *path = SvPV_nolen(path_sv);
1184 27 PUSHs(sv_2mortal(newSViv(file_mtime_internal(path))));
1185 27 PUTBACK;
1186 27 return NORMAL;
1187 }
1188  
1189 /* pp_file_atime: single path arg on stack */
1190 6 static OP* pp_file_atime(pTHX) {
1191 6 dSP;
1192 6 SV *path_sv = POPs;
1193 6 const char *path = SvPV_nolen(path_sv);
1194 6 PUSHs(sv_2mortal(newSViv(file_atime_internal(path))));
1195 6 PUTBACK;
1196 6 return NORMAL;
1197 }
1198  
1199 /* pp_file_ctime: single path arg on stack */
1200 6 static OP* pp_file_ctime(pTHX) {
1201 6 dSP;
1202 6 SV *path_sv = POPs;
1203 6 const char *path = SvPV_nolen(path_sv);
1204 6 PUSHs(sv_2mortal(newSViv(file_ctime_internal(path))));
1205 6 PUTBACK;
1206 6 return NORMAL;
1207 }
1208  
1209 /* pp_file_mode: single path arg on stack */
1210 3 static OP* pp_file_mode(pTHX) {
1211 3 dSP;
1212 3 SV *path_sv = POPs;
1213 3 const char *path = SvPV_nolen(path_sv);
1214 3 PUSHs(sv_2mortal(newSViv(file_mode_internal(path))));
1215 3 PUTBACK;
1216 3 return NORMAL;
1217 }
1218  
1219 /* pp_file_is_link: single path arg on stack */
1220 6 static OP* pp_file_is_link(pTHX) {
1221 6 dSP;
1222 6 SV *path_sv = POPs;
1223 6 const char *path = SvPV_nolen(path_sv);
1224 6 ▶ PUSHs(file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no); 2 4 6
1225 6 PUTBACK;
1226 6 return NORMAL;
1227 }
1228  
1229 /* pp_file_is_readable: single path arg on stack */
1230 7 static OP* pp_file_is_readable(pTHX) {
1231 7 dSP;
1232 7 SV *path_sv = POPs;
1233 7 const char *path = SvPV_nolen(path_sv);
1234 7 ▶ PUSHs(file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no); 7 0 7
1235 7 PUTBACK;
1236 7 return NORMAL;
1237 }
1238  
1239 /* pp_file_is_writable: single path arg on stack */
1240 6 static OP* pp_file_is_writable(pTHX) {
1241 6 dSP;
1242 6 SV *path_sv = POPs;
1243 6 const char *path = SvPV_nolen(path_sv);
1244 6 ▶ PUSHs(file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no); 5 1 6
1245 6 PUTBACK;
1246 6 return NORMAL;
1247 }
1248  
1249 /* pp_file_is_executable: single path arg on stack */
1250 8 static OP* pp_file_is_executable(pTHX) {
1251 8 dSP;
1252 8 SV *path_sv = POPs;
1253 8 const char *path = SvPV_nolen(path_sv);
1254 8 ▶ PUSHs(file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no); 2 6 8
1255 8 PUTBACK;
1256 8 return NORMAL;
1257 }
1258  
1259 /* pp_file_readdir: single path arg on stack */
1260 0 static OP* pp_file_readdir(pTHX) {
1261 0 dSP;
1262 0 SV *path_sv = POPs;
1263 0 const char *path = SvPV_nolen(path_sv);
1264 0 AV *result = file_readdir_internal(aTHX_ path);
1265 0 PUSHs(sv_2mortal(newRV_noinc((SV*)result)));
1266 0 PUTBACK;
1267 0 return NORMAL;
1268 }
1269  
1270 /* pp_file_slurp_raw: single path arg on stack (bypasses hooks) */
1271 0 static OP* pp_file_slurp_raw(pTHX) {
1272 0 dSP;
1273 0 SV *path_sv = POPs;
1274 0 const char *path = SvPV_nolen(path_sv);
1275 0 SV *result = file_slurp_raw_internal(aTHX_ path);
1276 0 PUSHs(sv_2mortal(result));
1277 0 PUTBACK;
1278 0 return NORMAL;
1279 }
1280  
1281 /* pp_file_copy: src and dst on stack */
1282 1 static OP* pp_file_copy(pTHX) {
1283 1 dSP;
1284 1 SV *dst_sv = POPs;
1285 1 SV *src_sv = POPs;
1286 1 ▶ const char *src = SvPV_nolen(src_sv);
Branch: 1 branch
BranchTrueFalseTotal
const char *src = SvPV_nolen(src_sv); 1 0 1
1287 1 const char *dst = SvPV_nolen(dst_sv);
1288 1 ▶ PUSHs(file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no); 1 0 1
1289 1 PUTBACK;
1290 1 return NORMAL;
1291 }
1292  
1293 /* pp_file_move: src and dst on stack */
1294 0 static OP* pp_file_move(pTHX) {
1295 0 dSP;
1296 0 SV *dst_sv = POPs;
1297 0 SV *src_sv = POPs;
1298 0 ▶ const char *src = SvPV_nolen(src_sv);
Branch: 1 branch
BranchTrueFalseTotal
const char *src = SvPV_nolen(src_sv); 0 0 0
1299 0 const char *dst = SvPV_nolen(dst_sv);
1300 0 ▶ PUSHs(file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no); 0 0 0
1301 0 PUTBACK;
1302 0 return NORMAL;
1303 }
1304  
1305 /* pp_file_chmod: path and mode on stack */
1306 0 static OP* pp_file_chmod(pTHX) {
1307 0 dSP;
1308 0 SV *mode_sv = POPs;
1309 0 SV *path_sv = POPs;
1310 0 const char *path = SvPV_nolen(path_sv);
1311 0 int mode = SvIV(mode_sv);
1312 0 ▶ PUSHs(file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no); 0 0 0
1313 0 PUTBACK;
1314 0 return NORMAL;
1315 }
1316  
1317 /* pp_file_append: path and data on stack */
1318 2 static OP* pp_file_append(pTHX) {
1319 2 dSP;
1320 2 SV *data = POPs;
1321 2 SV *path_sv = POPs;
1322 2 const char *path = SvPV_nolen(path_sv);
1323 2 ▶ PUSHs(file_append_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_append_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no); 2 0 2
1324 2 PUTBACK;
1325 2 return NORMAL;
1326 }
1327  
1328 /* pp_file_atomic_spew: path and data on stack */
1329 0 static OP* pp_file_atomic_spew(pTHX) {
1330 0 dSP;
1331 0 SV *data = POPs;
1332 0 SV *path_sv = POPs;
1333 0 const char *path = SvPV_nolen(path_sv);
1334 0 ▶ PUSHs(file_atomic_spew_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(file_atomic_spew_internal(aTHX_ path, data) ? &PL_sv_yes : &PL_sv_no); 0 0 0
1335 0 PUTBACK;
1336 0 return NORMAL;
1337 }
1338  
1339 /* ============================================
1340 Call checkers for compile-time optimization
1341 ============================================ */
1342  
1343 /* Count args between pushop's first sibling and the trailing cv op.
1344 * The last sibling in the chain is the cv (we don't replace it), so the
1345 * arg count is (total siblings) - 1. Returns -1 if the chain is shorter
1346 * than expected (no args at all). */
1347 268 static int file_count_call_args(OP *pushop) {
1348 268 ▶ OP *o = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
OP *o = OpSIBLING(pushop); 268 0 268
1349 268 int n = 0;
1350 955 ▶ while (o) {
Branch: 1 branch
BranchTrueFalseTotal
while (o) { 687 268 955
1351 687 n++;
1352 687 ▶ o = OpSIBLING(o);
Branch: 1 branch
BranchTrueFalseTotal
o = OpSIBLING(o); 419 268 687
1353 }
1354 268 return n > 0 ? n - 1 : -1;
1355 }
1356  
1357 /* 1-arg call checker (slurp, exists, size, is_file, is_dir, lines).
1358 * Bails when items != 1 so the regular XSUB sees the full arg list -
1359 * critical for the plugin tail (slurp($p, plugin => ..., key => val)). */
1360 192 static OP* file_call_checker_1arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
1361 192 file_ppfunc ppfunc = (file_ppfunc)SvIVX(ckobj);
1362 OP *pushop, *cvop, *argop;
1363 OP *newop;
1364  
1365 PERL_UNUSED_ARG(namegv);
1366  
1367 /* Navigate to first child */
1368 192 pushop = cUNOPx(entersubop)->op_first;
1369 192 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 192 0 192
1370 192 pushop = cUNOPx(pushop)->op_first;
1371 }
1372  
1373 192 ▶ if (file_count_call_args(pushop) != 1) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (file_count_call_args(pushop) != 1) return entersubop; 23 169 192
1374  
1375 /* Get the args: pushmark -> arg -> cv */
1376 169 ▶ argop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
argop = OpSIBLING(pushop); 169 0 169
1377 169 ▶ if (!argop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!argop) return entersubop; 0 169 169
1378  
1379 169 ▶ cvop = OpSIBLING(argop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(argop); 169 0 169
1380 169 ▶ if (!cvop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!cvop) return entersubop; 0 169 169
1381  
1382 /* Detach arg from tree */
1383 169 OpMORESIB_set(pushop, cvop);
1384 169 OpLASTSIB_set(argop, NULL);
1385  
1386 /* Force scalar context so function calls return exactly one value */
1387 169 argop = op_contextualize(argop, G_SCALAR);
1388  
1389 /* Create as OP_NULL first to avoid -DDEBUGGING assertion in newUNOP,
1390 then convert to OP_CUSTOM */
1391 169 newop = newUNOP(OP_NULL, 0, argop);
1392 169 newop->op_type = OP_CUSTOM;
1393 169 newop->op_ppaddr = ppfunc;
1394  
1395 169 op_free(entersubop);
1396 169 return newop;
1397 }
1398  
1399 /* 2-arg call checker (spew, append).
1400 * Bails when items != 2 so the regular XSUB sees the full arg list -
1401 * critical for the plugin tail (spew($p, $data, plugin => ..., ...)). */
1402 76 static OP* file_call_checker_2arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
1403 76 file_ppfunc ppfunc = (file_ppfunc)SvIVX(ckobj);
1404 OP *pushop, *cvop, *pathop, *dataop;
1405 OP *newop;
1406  
1407 PERL_UNUSED_ARG(namegv);
1408  
1409 /* Navigate to first child */
1410 76 pushop = cUNOPx(entersubop)->op_first;
1411 76 ▶ if (!OpHAS_SIBLING(pushop)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) { 76 0 76
1412 76 pushop = cUNOPx(pushop)->op_first;
1413 }
1414  
1415 76 ▶ if (file_count_call_args(pushop) != 2) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (file_count_call_args(pushop) != 2) return entersubop; 15 61 76
1416  
1417 /* Get the args: pushmark -> path -> data -> cv */
1418 61 ▶ pathop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
pathop = OpSIBLING(pushop); 61 0 61
1419 61 ▶ if (!pathop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!pathop) return entersubop; 0 61 61
1420  
1421 61 ▶ dataop = OpSIBLING(pathop);
Branch: 1 branch
BranchTrueFalseTotal
dataop = OpSIBLING(pathop); 61 0 61
1422 61 ▶ if (!dataop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!dataop) return entersubop; 0 61 61
1423  
1424 61 ▶ cvop = OpSIBLING(dataop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(dataop); 61 0 61
1425 61 ▶ if (!cvop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!cvop) return entersubop; 0 61 61
1426  
1427 /* Detach args from tree */
1428 61 OpMORESIB_set(pushop, cvop);
1429 61 OpLASTSIB_set(pathop, NULL);
1430 61 OpLASTSIB_set(dataop, NULL);
1431  
1432 /* Force scalar context on both args so function calls
1433 return exactly one value on the stack */
1434 61 pathop = op_contextualize(pathop, G_SCALAR);
1435 61 dataop = op_contextualize(dataop, G_SCALAR);
1436  
1437 /* Create as OP_NULL first to avoid -DDEBUGGING assertion in newBINOP,
1438 then convert to OP_CUSTOM */
1439 61 newop = newBINOP(OP_NULL, 0, pathop, dataop);
1440 61 newop->op_type = OP_CUSTOM;
1441 61 newop->op_ppaddr = ppfunc;
1442  
1443 61 op_free(entersubop);
1444 61 return newop;
1445 }
1446  
1447 /* Install 1-arg function with call checker */
1448 105 static void install_file_func_1arg(pTHX_ const char *pkg, const char *name,
1449 XSUBADDR_t xsub, file_ppfunc ppfunc) {
1450 char full_name[256];
1451 CV *cv;
1452 SV *ckobj;
1453  
1454 105 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, name);
1455 105 cv = newXS(full_name, xsub, __FILE__);
1456  
1457 105 ckobj = newSViv(PTR2IV(ppfunc));
1458 105 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
1459 105 }
1460  
1461 /* Install 2-arg function with call checker */
1462 30 static void install_file_func_2arg(pTHX_ const char *pkg, const char *name,
1463 XSUBADDR_t xsub, file_ppfunc ppfunc) {
1464 char full_name[256];
1465 CV *cv;
1466 SV *ckobj;
1467  
1468 30 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, name);
1469 30 cv = newXS(full_name, xsub, __FILE__);
1470  
1471 30 ckobj = newSViv(PTR2IV(ppfunc));
1472 30 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
1473 30 }
1474  
1475 /* ============================================
1476 Memory-mapped file registry
1477 ============================================ */
1478  
1479 typedef struct {
1480 void *addr; /* Mapped address */
1481 size_t len; /* Mapped length */
1482 int refcount; /* Reference count */
1483 #ifdef _WIN32
1484 HANDLE file_handle; /* Windows file handle */
1485 HANDLE map_handle; /* Windows mapping handle */
1486 #else
1487 int fd; /* File descriptor (POSIX) */
1488 #endif
1489 } MmapEntry;
1490  
1491 static MmapEntry *g_mmaps = NULL;
1492 static IV g_mmaps_size = 0;
1493 static IV g_mmaps_count = 0;
1494  
1495 /* Free list for mmap reuse */
1496 static IV *g_free_mmaps = NULL;
1497 static IV g_free_mmaps_size = 0;
1498 static IV g_free_mmaps_count = 0;
1499  
1500 /* ============================================
1501 Line iterator registry
1502 ============================================ */
1503  
1504 typedef struct {
1505 int fd; /* File descriptor (-1 in record-iter mode) */
1506 char *buffer; /* Read buffer (NULL in record-iter mode) */
1507 size_t buf_size; /* Buffer size */
1508 size_t buf_pos; /* Current position in buffer */
1509 size_t buf_len; /* Valid data length in buffer */
1510 int eof; /* End of file reached */
1511 int refcount; /* Reference count */
1512 char *path; /* File path (for reopening) */
1513 /* Record-iterator mode (set when lines_iter was called with a
1514 * plugin tail). When records is non-NULL, next/eof/close walk the
1515 * AoA instead of reading bytes from fd. */
1516 AV *records;
1517 SSize_t records_idx;
1518 /* Chunk-iterator mode (set by chunk_iter). Non-zero is the size the
1519 * caller asked for: next fills the buffer to it rather than looking
1520 * for a newline, and the buffer never grows past it. Zero is line
1521 * mode. The three modes share this registry and its free list, so
1522 * every field an open path does not set has to be cleared on the
1523 * way out - see free_iter_slot and ensure_iters_capacity. */
1524 size_t chunk_size;
1525 } LineIterEntry;
1526  
1527 static LineIterEntry *g_iters = NULL;
1528 static IV g_iters_size = 0;
1529 static IV g_iters_count = 0;
1530  
1531 static IV *g_free_iters = NULL;
1532 static IV g_free_iters_size = 0;
1533 static IV g_free_iters_count = 0;
1534  
1535 /* ============================================
1536 Initialization
1537 ============================================ */
1538  
1539 static int file_initialized = 0;
1540  
1541 /* Forward declaration for callback registry init */
1542 static void file_init_callback_registry(pTHX);
1543  
1544 29 static void file_init(pTHX) {
1545 29 ▶ if (file_initialized) return;
Branch: 1 branch
BranchTrueFalseTotal
if (file_initialized) return; 0 29 29
1546  
1547 29 g_mmaps_size = 16;
1548 29 ▶ Newxz(g_mmaps, g_mmaps_size, MmapEntry);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(g_mmaps, g_mmaps_size, MmapEntry); 0 29 29
1549 29 g_free_mmaps_size = 16;
1550 29 ▶ Newxz(g_free_mmaps, g_free_mmaps_size, IV);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(g_free_mmaps, g_free_mmaps_size, IV); 0 29 29
1551  
1552 29 g_iters_size = 16;
1553 29 ▶ Newxz(g_iters, g_iters_size, LineIterEntry);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(g_iters, g_iters_size, LineIterEntry); 0 29 29
1554 29 g_free_iters_size = 16;
1555 29 ▶ Newxz(g_free_iters, g_free_iters_size, IV);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(g_free_iters, g_free_iters_size, IV); 0 29 29
1556  
1557 /* Initialize callback registry with built-in predicates */
1558 29 file_init_callback_registry(aTHX);
1559  
1560 29 file_initialized = 1;
1561 }
1562  
1563 /* ============================================
1564 Fast slurp - read entire file into SV
1565 ============================================ */
1566  
1567 80 static SV* file_slurp_internal(pTHX_ const char *path) {
1568 int fd;
1569 Stat_t st;
1570 SV *result;
1571 char *buf;
1572 80 ssize_t total = 0, n;
1573 #ifdef _WIN32
1574 int open_flags = O_RDONLY | O_BINARY;
1575 #else
1576 /* O_NOATIME avoids updating access time - reduces disk writes */
1577 #ifdef __linux__
1578 80 int open_flags = O_RDONLY | O_NOATIME;
1579 #else
1580 int open_flags = O_RDONLY;
1581 #endif
1582 #endif
1583  
1584 80 fd = open(path, open_flags);
1585 #ifdef __linux__
1586 /* Fallback if O_NOATIME fails (not owner) */
1587 80 ▶ if (fd < 0 && errno == EPERM) {
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EPERM) { 3 77 80
if (fd < 0 && errno == EPERM) { 0 3 3
1588 0 fd = open(path, O_RDONLY);
1589 }
1590 #endif
1591 80 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 3 77 80
1592 3 return &PL_sv_undef;
1593 }
1594  
1595 77 ▶ if (fstat(fd, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &st) < 0) { 0 77 77
1596 0 close(fd);
1597 0 return &PL_sv_undef;
1598 }
1599  
1600 /* Hint to kernel: sequential read pattern */
1601 77 advise_sequential(fd, st.st_size);
1602  
1603 /* Pre-allocate exact size for regular files */
1604 77 ▶ if (S_ISREG(st.st_mode) && st.st_size > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISREG(st.st_mode) && st.st_size > 0) { 77 0 77
if (S_ISREG(st.st_mode) && st.st_size > 0) { 72 5 77
1605 #ifndef _WIN32
1606 /* For large files, use mmap + memcpy - faster than read() syscalls */
1607 72 ▶ if (st.st_size >= MMAP_SLURP_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size >= MMAP_SLURP_THRESHOLD) { 0 72 72
1608 0 void *map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1609 0 ▶ if (map != MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (map != MAP_FAILED) { 0 0 0
1610 /* Hint: we'll read sequentially */
1611 #ifdef MADV_SEQUENTIAL
1612 0 madvise(map, st.st_size, MADV_SEQUENTIAL);
1613 #endif
1614
1615 0 result = newSV(st.st_size + 1);
1616 0 SvPOK_on(result);
1617 0 buf = SvPVX(result);
1618 0 memcpy(buf, map, st.st_size);
1619 0 buf[st.st_size] = '\0';
1620 0 SvCUR_set(result, st.st_size);
1621
1622 0 munmap(map, st.st_size);
1623 0 close(fd);
1624 0 goto done;
1625 }
1626 /* mmap failed, fall through to read() */
1627 }
1628 #endif
1629 72 result = newSV(st.st_size + 1);
1630 72 SvPOK_on(result);
1631 72 buf = SvPVX(result);
1632  
1633 /* Read in one shot if possible */
1634 144 ▶ while (total < st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (total < st.st_size) { 72 72 144
1635 72 n = read(fd, buf + total, st.st_size - total);
1636 72 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 72 72
1637 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1638 0 close(fd);
1639 0 SvREFCNT_dec(result);
1640 0 return &PL_sv_undef;
1641 }
1642 72 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 72 72
1643 72 total += n;
1644 }
1645  
1646 72 buf[total] = '\0';
1647 72 SvCUR_set(result, total);
1648 } else {
1649 /* Stream or unknown size - read in chunks */
1650 5 size_t capacity = FILE_BUFFER_SIZE;
1651 5 result = newSV(capacity);
1652 5 SvPOK_on(result);
1653 5 buf = SvPVX(result);
1654  
1655 while (1) {
1656 5 ▶ if (total >= (ssize_t)capacity - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (total >= (ssize_t)capacity - 1) { 0 5 5
1657 0 capacity *= 2;
1658 0 ▶ SvGROW(result, capacity);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(result, capacity); 0 0 0
SvGROW(result, capacity); 0 0 0
1659 0 buf = SvPVX(result);
1660 }
1661  
1662 5 n = read(fd, buf + total, capacity - total - 1);
1663 5 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 5 5
1664 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1665 0 close(fd);
1666 0 SvREFCNT_dec(result);
1667 0 return &PL_sv_undef;
1668 }
1669 5 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 5 0 5
1670 0 total += n;
1671 }
1672  
1673 5 buf[total] = '\0';
1674 5 SvCUR_set(result, total);
1675 }
1676  
1677 77 close(fd);
1678  
1679 77 done:
1680 77 return result;
1681 }
1682  
1683 /* ============================================
1684 Fast slurp binary - same as slurp but explicit
1685 (bypasses hooks - for raw binary data)
1686 ============================================ */
1687  
1688 2 static SV* file_slurp_raw_internal(pTHX_ const char *path) {
1689 int fd;
1690 Stat_t st;
1691 SV *result;
1692 char *buf;
1693 2 ssize_t total = 0, n;
1694 #ifdef _WIN32
1695 int open_flags = O_RDONLY | O_BINARY;
1696 #else
1697 #ifdef __linux__
1698 2 int open_flags = O_RDONLY | O_NOATIME;
1699 #else
1700 int open_flags = O_RDONLY;
1701 #endif
1702 #endif
1703  
1704 2 fd = open(path, open_flags);
1705 #ifdef __linux__
1706 2 ▶ if (fd < 0 && errno == EPERM) {
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 && errno == EPERM) { 0 2 2
if (fd < 0 && errno == EPERM) { 0 0 0
1707 0 fd = open(path, O_RDONLY);
1708 }
1709 #endif
1710 2 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 2 2
1711 0 return &PL_sv_undef;
1712 }
1713  
1714 2 ▶ if (fstat(fd, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &st) < 0) { 0 2 2
1715 0 close(fd);
1716 0 return &PL_sv_undef;
1717 }
1718  
1719 /* Hint to kernel: sequential read pattern */
1720 2 advise_sequential(fd, st.st_size);
1721  
1722 2 ▶ if (S_ISREG(st.st_mode) && st.st_size > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISREG(st.st_mode) && st.st_size > 0) { 2 0 2
if (S_ISREG(st.st_mode) && st.st_size > 0) { 2 0 2
1723 #ifndef _WIN32
1724 /* For large files, use mmap + memcpy */
1725 2 ▶ if (st.st_size >= MMAP_SLURP_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size >= MMAP_SLURP_THRESHOLD) { 0 2 2
1726 0 void *map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1727 0 ▶ if (map != MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (map != MAP_FAILED) { 0 0 0
1728 #ifdef MADV_SEQUENTIAL
1729 0 madvise(map, st.st_size, MADV_SEQUENTIAL);
1730 #endif
1731
1732 0 result = newSV(st.st_size + 1);
1733 0 SvPOK_on(result);
1734 0 buf = SvPVX(result);
1735 0 memcpy(buf, map, st.st_size);
1736 0 buf[st.st_size] = '\0';
1737 0 SvCUR_set(result, st.st_size);
1738
1739 0 munmap(map, st.st_size);
1740 0 close(fd);
1741 0 return result;
1742 }
1743 }
1744 #endif
1745 2 result = newSV(st.st_size + 1);
1746 2 SvPOK_on(result);
1747 2 buf = SvPVX(result);
1748  
1749 4 ▶ while (total < st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (total < st.st_size) { 2 2 4
1750 2 n = read(fd, buf + total, st.st_size - total);
1751 2 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 2 2
1752 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1753 0 close(fd);
1754 0 SvREFCNT_dec(result);
1755 0 return &PL_sv_undef;
1756 }
1757 2 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 2 2
1758 2 total += n;
1759 }
1760  
1761 2 buf[total] = '\0';
1762 2 SvCUR_set(result, total);
1763 } else {
1764 0 size_t capacity = FILE_BUFFER_SIZE;
1765 0 result = newSV(capacity);
1766 0 SvPOK_on(result);
1767 0 buf = SvPVX(result);
1768  
1769 while (1) {
1770 0 ▶ if (total >= (ssize_t)capacity - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (total >= (ssize_t)capacity - 1) { 0 0 0
1771 0 capacity *= 2;
1772 0 ▶ SvGROW(result, capacity);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(result, capacity); 0 0 0
SvGROW(result, capacity); 0 0 0
1773 0 buf = SvPVX(result);
1774 }
1775  
1776 0 n = read(fd, buf + total, capacity - total - 1);
1777 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1778 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1779 0 close(fd);
1780 0 SvREFCNT_dec(result);
1781 0 return &PL_sv_undef;
1782 }
1783 0 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 0 0
1784 0 total += n;
1785 }
1786  
1787 0 buf[total] = '\0';
1788 0 SvCUR_set(result, total);
1789 }
1790  
1791 2 close(fd);
1792 2 return result; /* No hooks for raw */
1793 }
1794  
1795 2 static SV* file_slurp_raw(pTHX_ const char *path) {
1796 2 return file_slurp_raw_internal(aTHX_ path);
1797 }
1798  
1799 /* ============================================
1800 Fast spew - write SV to file
1801 ============================================ */
1802  
1803 178 static int file_spew_internal(pTHX_ const char *path, SV *data) {
1804 int fd;
1805 const char *buf;
1806 STRLEN len;
1807 ssize_t n;
1808 178 SV *write_data = data;
1809 178 int free_write_data = 0;
1810 #ifdef _WIN32
1811 int open_flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
1812 #else
1813 178 int open_flags = O_WRONLY | O_CREAT | O_TRUNC;
1814 #endif
1815  
1816 178 buf = SvPV(write_data, len);
1817  
1818 178 fd = file_open3(path, open_flags, 0644);
1819 178 ▶ if (UNLIKELY(fd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(fd < 0)) { 0 178 178
1820 0 ▶ if (free_write_data) SvREFCNT_dec(write_data);
Branch: 1 branch
BranchTrueFalseTotal
if (free_write_data) SvREFCNT_dec(write_data); 0 0 0
1821 0 return 0;
1822 }
1823  
1824 #if defined(__linux__)
1825 /* Pre-allocate space for large files to avoid fragmentation */
1826 178 ▶ if (len >= 65536) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 65536) { 4 174 178
1827 4 posix_fallocate(fd, 0, len);
1828 }
1829 #endif
1830  
1831 /* Fast path: single write for common case */
1832 178 n = write(fd, buf, len);
1833 178 ▶ if (LIKELY(n == (ssize_t)len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY(n == (ssize_t)len)) { 178 0 178
1834 178 close(fd);
1835 178 ▶ if (free_write_data) SvREFCNT_dec(write_data);
Branch: 1 branch
BranchTrueFalseTotal
if (free_write_data) SvREFCNT_dec(write_data); 0 178 178
1836 /* Invalidate cache for this path */
1837 178 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 63 115 178
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 61 63
1838 2 g_stat_cache.valid = 0;
1839 }
1840 178 return 1;
1841 }
1842  
1843 /* Handle partial write or error */
1844 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1845 0 ▶ if (errno != EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINTR) { 0 0 0
1846 0 close(fd);
1847 0 ▶ if (free_write_data) SvREFCNT_dec(write_data);
Branch: 1 branch
BranchTrueFalseTotal
if (free_write_data) SvREFCNT_dec(write_data); 0 0 0
1848 0 return 0;
1849 }
1850 0 n = 0;
1851 }
1852  
1853 /* Loop for remaining data (rare) */
1854 {
1855 0 ssize_t written = n;
1856 0 ▶ while ((size_t)written < len) {
Branch: 1 branch
BranchTrueFalseTotal
while ((size_t)written < len) { 0 0 0
1857 0 n = write(fd, buf + written, len - written);
1858 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1859 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1860 0 close(fd);
1861 0 ▶ if (free_write_data) SvREFCNT_dec(write_data);
Branch: 1 branch
BranchTrueFalseTotal
if (free_write_data) SvREFCNT_dec(write_data); 0 0 0
1862 0 return 0;
1863 }
1864 0 written += n;
1865 }
1866 }
1867  
1868 0 close(fd);
1869 0 ▶ if (free_write_data) SvREFCNT_dec(write_data);
Branch: 1 branch
BranchTrueFalseTotal
if (free_write_data) SvREFCNT_dec(write_data); 0 0 0
1870 /* Invalidate cache for this path */
1871 0 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 0 0 0
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 0 0 0
1872 0 g_stat_cache.valid = 0;
1873 }
1874 0 return 1;
1875 }
1876  
1877 /* ============================================
1878 Fast append - append SV to file
1879 ============================================ */
1880  
1881 7 static int file_append_internal(pTHX_ const char *path, SV *data) {
1882 int fd;
1883 const char *buf;
1884 STRLEN len;
1885 ssize_t n;
1886 #ifdef _WIN32
1887 int open_flags = O_WRONLY | O_CREAT | O_APPEND | O_BINARY;
1888 #else
1889 7 int open_flags = O_WRONLY | O_CREAT | O_APPEND;
1890 #endif
1891  
1892 7 buf = SvPV(data, len);
1893  
1894 7 fd = file_open3(path, open_flags, 0644);
1895 7 ▶ if (UNLIKELY(fd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(fd < 0)) { 0 7 7
1896 0 return 0;
1897 }
1898  
1899 /* Fast path: single write for common case */
1900 7 n = write(fd, buf, len);
1901 7 ▶ if (LIKELY(n == (ssize_t)len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY(n == (ssize_t)len)) { 7 0 7
1902 7 close(fd);
1903 /* Invalidate cache for this path */
1904 7 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 4 3 7
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 1 3 4
1905 1 g_stat_cache.valid = 0;
1906 }
1907 7 return 1;
1908 }
1909  
1910 /* Handle partial write or error */
1911 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1912 0 ▶ if (errno != EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINTR) { 0 0 0
1913 0 close(fd);
1914 0 return 0;
1915 }
1916 0 n = 0;
1917 }
1918  
1919 /* Loop for remaining data (rare) */
1920 {
1921 0 ssize_t written = n;
1922 0 ▶ while ((size_t)written < len) {
Branch: 1 branch
BranchTrueFalseTotal
while ((size_t)written < len) { 0 0 0
1923 0 n = write(fd, buf + written, len - written);
1924 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1925 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
1926 0 close(fd);
1927 0 return 0;
1928 }
1929 0 written += n;
1930 }
1931 }
1932  
1933 0 close(fd);
1934 /* Invalidate cache for this path */
1935 0 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 0 0 0
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 0 0 0
1936 0 g_stat_cache.valid = 0;
1937 }
1938 0 return 1;
1939 }
1940  
1941 /* ============================================
1942 Memory-mapped file operations
1943 ============================================ */
1944  
1945 4 static void ensure_mmaps_capacity(IV needed) {
1946 4 ▶ if (needed >= g_mmaps_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed >= g_mmaps_size) { 0 4 4
1947 0 ▶ IV new_size = g_mmaps_size ? g_mmaps_size * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
IV new_size = g_mmaps_size ? g_mmaps_size * 2 : 16; 0 0 0
1948 IV i;
1949 0 ▶ while (new_size <= needed) new_size *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_size <= needed) new_size *= 2; 0 0 0
1950 0 ▶ Renew(g_mmaps, new_size, MmapEntry);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_mmaps, new_size, MmapEntry); 0 0 0
1951 0 ▶ for (i = g_mmaps_size; i < new_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = g_mmaps_size; i < new_size; i++) { 0 0 0
1952 0 g_mmaps[i].addr = NULL;
1953 0 g_mmaps[i].len = 0;
1954 0 g_mmaps[i].refcount = 0;
1955 #ifdef _WIN32
1956 g_mmaps[i].file_handle = INVALID_HANDLE_VALUE;
1957 g_mmaps[i].map_handle = INVALID_HANDLE_VALUE;
1958 #else
1959 0 g_mmaps[i].fd = -1;
1960 #endif
1961 }
1962 0 g_mmaps_size = new_size;
1963 }
1964 4 }
1965  
1966 14 static IV alloc_mmap_slot(void) {
1967 IV idx;
1968  
1969 14 ▶ if (g_free_mmaps_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_free_mmaps_count > 0) { 10 4 14
1970 10 return g_free_mmaps[--g_free_mmaps_count];
1971 }
1972  
1973 4 ensure_mmaps_capacity(g_mmaps_count);
1974 4 idx = g_mmaps_count++;
1975 4 return idx;
1976 }
1977  
1978 14 static void free_mmap_slot(IV idx) {
1979 dTHX;
1980 MmapEntry *entry;
1981  
1982 14 ▶ if (idx < 0 || idx >= g_mmaps_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_mmaps_count) return; 14 0 14
if (idx < 0 || idx >= g_mmaps_count) return; 0 14 14
1983  
1984 14 entry = &g_mmaps[idx];
1985 #ifdef _WIN32
1986 if (entry->addr) {
1987 UnmapViewOfFile(entry->addr);
1988 }
1989 if (entry->map_handle != INVALID_HANDLE_VALUE) {
1990 CloseHandle(entry->map_handle);
1991 }
1992 if (entry->file_handle != INVALID_HANDLE_VALUE) {
1993 CloseHandle(entry->file_handle);
1994 }
1995 entry->file_handle = INVALID_HANDLE_VALUE;
1996 entry->map_handle = INVALID_HANDLE_VALUE;
1997 #else
1998 14 ▶ if (entry->addr && entry->addr != MAP_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry->addr && entry->addr != MAP_FAILED) { 14 0 14
if (entry->addr && entry->addr != MAP_FAILED) { 14 0 14
1999 14 munmap(entry->addr, entry->len);
2000 }
2001 14 ▶ if (entry->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->fd >= 0) { 14 0 14
2002 14 close(entry->fd);
2003 }
2004 14 entry->fd = -1;
2005 #endif
2006 14 entry->addr = NULL;
2007 14 entry->len = 0;
2008 14 entry->refcount = 0;
2009  
2010 14 ▶ if (g_free_mmaps_count >= g_free_mmaps_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_free_mmaps_count >= g_free_mmaps_size) { 0 14 14
2011 0 g_free_mmaps_size *= 2;
2012 0 ▶ Renew(g_free_mmaps, g_free_mmaps_size, IV);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_free_mmaps, g_free_mmaps_size, IV); 0 0 0
2013 }
2014 14 g_free_mmaps[g_free_mmaps_count++] = idx;
2015 }
2016  
2017 16 static IV file_mmap_open(pTHX_ const char *path, int writable) {
2018 IV idx;
2019 void *addr;
2020 size_t file_size;
2021  
2022 #ifdef _WIN32
2023 HANDLE file_handle;
2024 HANDLE map_handle;
2025 LARGE_INTEGER size;
2026 DWORD access = writable ? GENERIC_READ | GENERIC_WRITE : GENERIC_READ;
2027 DWORD share = FILE_SHARE_READ;
2028 DWORD protect = writable ? PAGE_READWRITE : PAGE_READONLY;
2029 DWORD map_access = writable ? FILE_MAP_WRITE : FILE_MAP_READ;
2030  
2031 file_handle = CreateFileA(path, access, share, NULL, OPEN_EXISTING,
2032 FILE_ATTRIBUTE_NORMAL, NULL);
2033 if (file_handle == INVALID_HANDLE_VALUE) {
2034 return -1;
2035 }
2036  
2037 if (!GetFileSizeEx(file_handle, &size)) {
2038 CloseHandle(file_handle);
2039 return -1;
2040 }
2041  
2042 if (size.QuadPart == 0) {
2043 CloseHandle(file_handle);
2044 return -1;
2045 }
2046  
2047 file_size = (size_t)size.QuadPart;
2048  
2049 map_handle = CreateFileMappingA(file_handle, NULL, protect, 0, 0, NULL);
2050 if (map_handle == NULL) {
2051 CloseHandle(file_handle);
2052 return -1;
2053 }
2054  
2055 addr = MapViewOfFile(map_handle, map_access, 0, 0, 0);
2056 if (addr == NULL) {
2057 CloseHandle(map_handle);
2058 CloseHandle(file_handle);
2059 return -1;
2060 }
2061  
2062 idx = alloc_mmap_slot();
2063 g_mmaps[idx].addr = addr;
2064 g_mmaps[idx].len = file_size;
2065 g_mmaps[idx].file_handle = file_handle;
2066 g_mmaps[idx].map_handle = map_handle;
2067 g_mmaps[idx].refcount = 1;
2068  
2069 #else
2070 int fd;
2071 Stat_t st;
2072 16 ▶ int flags = writable ? O_RDWR : O_RDONLY;
Branch: 1 branch
BranchTrueFalseTotal
int flags = writable ? O_RDWR : O_RDONLY; 1 15 16
2073 16 ▶ int prot = writable ? (PROT_READ | PROT_WRITE) : PROT_READ;
Branch: 1 branch
BranchTrueFalseTotal
int prot = writable ? (PROT_READ | PROT_WRITE) : PROT_READ; 1 15 16
2074  
2075 16 fd = open(path, flags);
2076 16 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 1 15 16
2077 1 return -1;
2078 }
2079  
2080 15 ▶ if (fstat(fd, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &st) < 0) { 0 15 15
2081 0 close(fd);
2082 0 return -1;
2083 }
2084  
2085 15 ▶ if (st.st_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.st_size == 0) { 1 14 15
2086 /* Can't mmap empty file */
2087 1 close(fd);
2088 1 return -1;
2089 }
2090  
2091 14 file_size = st.st_size;
2092  
2093 14 addr = mmap(NULL, st.st_size, prot, MAP_SHARED, fd, 0);
2094 14 ▶ if (addr == MAP_FAILED) {
Branch: 1 branch
BranchTrueFalseTotal
if (addr == MAP_FAILED) { 0 14 14
2095 0 close(fd);
2096 0 return -1;
2097 }
2098  
2099 14 idx = alloc_mmap_slot();
2100 14 g_mmaps[idx].addr = addr;
2101 14 g_mmaps[idx].len = file_size;
2102 14 g_mmaps[idx].fd = fd;
2103 14 g_mmaps[idx].refcount = 1;
2104 #endif
2105  
2106 14 return idx;
2107 }
2108  
2109 14 static SV* file_mmap_get_sv(pTHX_ IV idx) {
2110 MmapEntry *entry;
2111 SV *sv;
2112  
2113 14 ▶ if (idx < 0 || idx >= g_mmaps_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_mmaps_count) { 14 0 14
if (idx < 0 || idx >= g_mmaps_count) { 0 14 14
2114 0 return &PL_sv_undef;
2115 }
2116  
2117 14 entry = &g_mmaps[idx];
2118 #ifdef _WIN32
2119 if (!entry->addr) {
2120 return &PL_sv_undef;
2121 }
2122 #else
2123 14 ▶ if (!entry->addr || entry->addr == MAP_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (!entry->addr || entry->addr == MAP_FAILED) { 14 0 14
if (!entry->addr || entry->addr == MAP_FAILED) { 0 14 14
2124 0 return &PL_sv_undef;
2125 }
2126 #endif
2127  
2128 /* Create an SV that points directly to the mapped memory */
2129 14 sv = newSV(0);
2130 14 ▶ SvUPGRADE(sv, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(sv, SVt_PV); 14 0 14
2131 14 SvPV_set(sv, (char*)entry->addr);
2132 14 SvCUR_set(sv, entry->len);
2133 14 SvLEN_set(sv, 0); /* Don't free this memory! */
2134 14 SvPOK_on(sv);
2135 14 SvREADONLY_on(sv);
2136  
2137 14 return sv;
2138 }
2139  
2140 16 static void file_mmap_close(IV idx) {
2141 dTHX;
2142 16 ▶ if (idx < 0 || idx >= g_mmaps_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_mmaps_count) return; 14 2 16
if (idx < 0 || idx >= g_mmaps_count) return; 0 14 14
2143  
2144 14 MmapEntry *entry = &g_mmaps[idx];
2145 14 entry->refcount--;
2146 14 ▶ if (entry->refcount <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->refcount <= 0) { 14 0 14
2147 14 free_mmap_slot(idx);
2148 }
2149 }
2150  
2151 2 static void file_mmap_sync(IV idx) {
2152 dTHX;
2153 MmapEntry *entry;
2154  
2155 2 ▶ if (idx < 0 || idx >= g_mmaps_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_mmaps_count) return; 2 0 2
if (idx < 0 || idx >= g_mmaps_count) return; 0 2 2
2156  
2157 2 entry = &g_mmaps[idx];
2158 #ifdef _WIN32
2159 if (entry->addr) {
2160 FlushViewOfFile(entry->addr, entry->len);
2161 }
2162 #else
2163 2 ▶ if (entry->addr && entry->addr != MAP_FAILED) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry->addr && entry->addr != MAP_FAILED) { 2 0 2
if (entry->addr && entry->addr != MAP_FAILED) { 2 0 2
2164 2 msync(entry->addr, entry->len, MS_SYNC);
2165 }
2166 #endif
2167 }
2168  
2169 /* ============================================
2170 Line iterator operations
2171 ============================================ */
2172  
2173 44 static void ensure_iters_capacity(IV needed) {
2174 44 ▶ if (needed >= g_iters_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed >= g_iters_size) { 1 43 44
2175 1 ▶ IV new_size = g_iters_size ? g_iters_size * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
IV new_size = g_iters_size ? g_iters_size * 2 : 16; 1 0 1
2176 IV i;
2177 1 ▶ while (new_size <= needed) new_size *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_size <= needed) new_size *= 2; 0 1 1
2178 1 ▶ Renew(g_iters, new_size, LineIterEntry);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_iters, new_size, LineIterEntry); 0 1 1
2179 17 ▶ for (i = g_iters_size; i < new_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = g_iters_size; i < new_size; i++) { 16 1 17
2180 16 g_iters[i].fd = -1;
2181 16 g_iters[i].buffer = NULL;
2182 16 g_iters[i].buf_size = 0;
2183 16 g_iters[i].buf_pos = 0;
2184 16 g_iters[i].buf_len = 0;
2185 16 g_iters[i].eof = 0;
2186 16 g_iters[i].refcount = 0;
2187 16 g_iters[i].path = NULL;
2188 /* Renew does not zero what it grows into, so the mode fields
2189 * have to be cleared here too: an open path that forgets one
2190 * would inherit the garbage rather than a previous owner's
2191 * value, which is the harder version of the same bug. */
2192 16 g_iters[i].records = NULL;
2193 16 g_iters[i].records_idx = 0;
2194 16 g_iters[i].chunk_size = 0;
2195 }
2196 1 g_iters_size = new_size;
2197 }
2198 44 }
2199  
2200 646 static IV alloc_iter_slot(void) {
2201 IV idx;
2202  
2203 646 ▶ if (g_free_iters_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_free_iters_count > 0) { 602 44 646
2204 602 return g_free_iters[--g_free_iters_count];
2205 }
2206  
2207 44 ensure_iters_capacity(g_iters_count);
2208 44 idx = g_iters_count++;
2209 44 return idx;
2210 }
2211  
2212 645 static void free_iter_slot(IV idx) {
2213 dTHX;
2214 LineIterEntry *entry;
2215  
2216 645 ▶ if (idx < 0 || idx >= g_iters_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_iters_count) return; 645 0 645
if (idx < 0 || idx >= g_iters_count) return; 0 645 645
2217  
2218 645 entry = &g_iters[idx];
2219 645 ▶ if (entry->fd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->fd >= 0) { 642 3 645
2220 642 close(entry->fd);
2221 }
2222 645 ▶ if (entry->buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buffer) { 642 3 645
2223 642 Safefree(entry->buffer);
2224 }
2225 645 ▶ if (entry->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->path) { 642 3 645
2226 642 Safefree(entry->path);
2227 }
2228 645 ▶ if (entry->records) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->records) { 3 642 645
2229 3 SvREFCNT_dec((SV *)entry->records);
2230 }
2231  
2232 645 entry->fd = -1;
2233 645 entry->buffer = NULL;
2234 645 entry->buf_size = 0;
2235 645 entry->buf_pos = 0;
2236 645 entry->buf_len = 0;
2237 645 entry->eof = 0;
2238 645 entry->refcount = 0;
2239 645 entry->path = NULL;
2240 645 entry->records = NULL;
2241 645 entry->records_idx = 0;
2242 645 entry->chunk_size = 0;
2243  
2244 645 ▶ if (g_free_iters_count >= g_free_iters_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_free_iters_count >= g_free_iters_size) { 1 644 645
2245 1 g_free_iters_size *= 2;
2246 1 ▶ Renew(g_free_iters, g_free_iters_size, IV);
Branch: 1 branch
BranchTrueFalseTotal
Renew(g_free_iters, g_free_iters_size, IV); 0 1 1
2247 }
2248 645 g_free_iters[g_free_iters_count++] = idx;
2249 }
2250  
2251 84 static IV file_lines_open(pTHX_ const char *path) {
2252 int fd;
2253 IV idx;
2254 LineIterEntry *entry;
2255 size_t path_len;
2256 #ifdef _WIN32
2257 int open_flags = O_RDONLY | O_BINARY;
2258 #else
2259 84 int open_flags = O_RDONLY;
2260 #endif
2261  
2262 84 fd = open(path, open_flags);
2263 84 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 4 80 84
2264 4 return -1;
2265 }
2266  
2267 80 idx = alloc_iter_slot();
2268 80 entry = &g_iters[idx];
2269  
2270 80 entry->fd = fd;
2271 80 entry->buf_size = FILE_BUFFER_SIZE;
2272 80 Newx(entry->buffer, entry->buf_size, char);
2273 80 entry->buf_pos = 0;
2274 80 entry->buf_len = 0;
2275 80 entry->eof = 0;
2276 80 entry->refcount = 1;
2277  
2278 80 path_len = strlen(path);
2279 80 Newx(entry->path, path_len + 1, char);
2280 80 memcpy(entry->path, path, path_len + 1);
2281  
2282 /* Byte-line mode: ensure record-mode fields stay NULL even if
2283 * alloc_iter_slot reused a slot whose previous owner was a
2284 * record-iter (free_iter_slot already clears them, but be explicit
2285 * - this was overlooked before the field was added). */
2286 80 entry->records = NULL;
2287 80 entry->records_idx = 0;
2288 80 entry->chunk_size = 0;
2289  
2290 80 return idx;
2291 }
2292  
2293 /* ============================================
2294 Chunk iterator: the pull half of the streaming surface.
2295  
2296 These three are the public C API declared in include/file_chunk.h, so
2297 an XS consumer can hold the fd and pull bytes into its own loop
2298 without a Perl round trip. The Perl surface (chunk_iter) is a thin
2299 XSUB over the same functions - there is one implementation, and the
2300 header does not promise a second.
2301 ============================================ */
2302  
2303 static void file_lines_close(IV idx); /* defined below; the free path
2304 * is shared with the line and
2305 * record iterators */
2306  
2307 /* The same open as file_lines_open: the same flags, the same registry.
2308 * The buffer is the size the caller asked for and never grows, because
2309 * that bound is the whole point of asking. */
2310 564 IV file_chunk_open(pTHX_ const char *path, size_t size) {
2311 int fd;
2312 IV idx;
2313 LineIterEntry *entry;
2314 size_t path_len;
2315 #ifdef _WIN32
2316 int open_flags = O_RDONLY | O_BINARY;
2317 #else
2318 564 int open_flags = O_RDONLY;
2319 #endif
2320  
2321 564 fd = open(path, open_flags);
2322 564 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 1 563 564
2323 1 return -1;
2324 }
2325  
2326 563 idx = alloc_iter_slot();
2327 563 entry = &g_iters[idx];
2328  
2329 563 entry->fd = fd;
2330 563 entry->buf_size = size;
2331 563 Newx(entry->buffer, entry->buf_size, char);
2332 563 entry->buf_pos = 0;
2333 563 entry->buf_len = 0;
2334 563 entry->eof = 0;
2335 563 entry->refcount = 1;
2336  
2337 563 path_len = strlen(path);
2338 563 Newx(entry->path, path_len + 1, char);
2339 563 memcpy(entry->path, path, path_len + 1);
2340  
2341 563 entry->records = NULL;
2342 563 entry->records_idx = 0;
2343 563 entry->chunk_size = size;
2344  
2345 563 return idx;
2346 }
2347  
2348 /* Fill to chunk_size or to end of file, whichever comes first, so a
2349 * caller splitting a file into fixed blocks gets fixed blocks: a short
2350 * read is retried rather than handed on. Returns the byte count with
2351 * *buf pointing into the iterator's own buffer (valid until the next
2352 * call on this handle), 0 at end of file, -1 with errno set on a read
2353 * error. It does not croak: a C consumer wants the errno, and the Perl
2354 * surface is where a refusal is worded. */
2355 11566 IV file_chunk_read(pTHX_ IV handle, const char **buf) {
2356 LineIterEntry *entry;
2357 ssize_t n;
2358  
2359 PERL_UNUSED_CONTEXT;
2360  
2361 11566 ▶ if (buf) *buf = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (buf) *buf = NULL; 11566 0 11566
2362 11566 ▶ if (handle < 0 || handle >= g_iters_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (handle < 0 || handle >= g_iters_count) { 11566 0 11566
if (handle < 0 || handle >= g_iters_count) { 0 11566 11566
2363 0 errno = EBADF;
2364 0 return -1;
2365 }
2366  
2367 11566 entry = &g_iters[handle];
2368 11566 ▶ if (entry->fd < 0 || entry->chunk_size == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry->fd < 0 || entry->chunk_size == 0) { 11566 0 11566
if (entry->fd < 0 || entry->chunk_size == 0) { 0 11566 11566
2369 0 errno = EBADF;
2370 0 return -1;
2371 }
2372  
2373 11566 entry->buf_pos = 0;
2374 11566 entry->buf_len = 0;
2375  
2376 23114 ▶ while (entry->buf_len < entry->chunk_size && !entry->eof) {
Branch: 2 branches
BranchTrueFalseTotal
while (entry->buf_len < entry->chunk_size && !entry->eof) { 11576 11538 23114
while (entry->buf_len < entry->chunk_size && !entry->eof) { 11565 11 11576
2377 11565 n = read(entry->fd, entry->buffer + entry->buf_len,
2378 11565 entry->chunk_size - entry->buf_len);
2379 11565 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 1 11564 11565
2380 1 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 1 1
2381 1 return -1;
2382 }
2383 11564 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 16 11548 11564
2384 16 entry->eof = 1;
2385 16 break;
2386 }
2387 11548 entry->buf_len += (size_t)n;
2388 }
2389  
2390 11565 entry->buf_pos = entry->buf_len;
2391 11565 ▶ if (buf) *buf = entry->buffer;
Branch: 1 branch
BranchTrueFalseTotal
if (buf) *buf = entry->buffer; 11565 0 11565
2392 11565 return (IV)entry->buf_len;
2393 }
2394  
2395 /* Close is by handle and is safe to call once. A slot whose refcount has
2396 * already reached zero is left alone rather than pushed onto the free
2397 * list twice, which would hand the same slot out to two owners. */
2398 0 void file_chunk_close(pTHX_ IV handle) {
2399 LineIterEntry *entry;
2400  
2401 PERL_UNUSED_CONTEXT;
2402  
2403 0 ▶ if (handle < 0 || handle >= g_iters_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (handle < 0 || handle >= g_iters_count) return; 0 0 0
if (handle < 0 || handle >= g_iters_count) return; 0 0 0
2404 0 entry = &g_iters[handle];
2405 0 ▶ if (entry->refcount <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (entry->refcount <= 0) return; 0 0 0
2406 0 file_lines_close(handle);
2407 }
2408  
2409 1395 static SV* file_lines_next(pTHX_ IV idx) {
2410 LineIterEntry *entry;
2411 char *line_start;
2412 char *newline;
2413 size_t line_len;
2414 SV *result;
2415 ssize_t n;
2416  
2417 1395 ▶ if (idx < 0 || idx >= g_iters_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_iters_count) { 1395 0 1395
if (idx < 0 || idx >= g_iters_count) { 0 1395 1395
2418 0 return &PL_sv_undef;
2419 }
2420  
2421 1395 entry = &g_iters[idx];
2422 1395 ▶ if (entry->fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->fd < 0) { 0 1395 1395
2423 0 return &PL_sv_undef;
2424 }
2425  
2426 while (1) {
2427 /* Look for newline in current buffer */
2428 1474 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 1365 109 1474
2429 1365 line_start = entry->buffer + entry->buf_pos;
2430 1365 newline = memchr(line_start, '\n', entry->buf_len - entry->buf_pos);
2431  
2432 1365 ▶ if (newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (newline) { 1353 12 1365
2433 1353 line_len = newline - line_start;
2434 1353 result = newSVpvn(line_start, line_len);
2435 1353 entry->buf_pos += line_len + 1;
2436 1353 return result;
2437 }
2438 }
2439  
2440 /* No newline found, need more data */
2441 121 ▶ if (entry->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->eof) { 42 79 121
2442 /* Return remaining data if any */
2443 42 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 6 36 42
2444 6 line_len = entry->buf_len - entry->buf_pos;
2445 6 result = newSVpvn(entry->buffer + entry->buf_pos, line_len);
2446 6 entry->buf_pos = entry->buf_len;
2447 6 return result;
2448 }
2449 36 return &PL_sv_undef;
2450 }
2451  
2452 /* Move remaining data to start of buffer */
2453 79 ▶ if (entry->buf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos > 0) { 33 46 79
2454 33 size_t remaining = entry->buf_len - entry->buf_pos;
2455 33 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 6 27 33
2456 6 memmove(entry->buffer, entry->buffer + entry->buf_pos, remaining);
2457 }
2458 33 entry->buf_len = remaining;
2459 33 entry->buf_pos = 0;
2460 }
2461  
2462 /* Expand buffer if needed */
2463 79 ▶ if (entry->buf_len >= entry->buf_size - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_len >= entry->buf_size - 1) { 0 79 79
2464 0 entry->buf_size *= 2;
2465 0 Renew(entry->buffer, entry->buf_size, char);
2466 }
2467  
2468 /* Read more data */
2469 79 n = read(entry->fd, entry->buffer + entry->buf_len,
2470 79 entry->buf_size - entry->buf_len - 1);
2471 79 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 79 79
2472 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
2473 0 return &PL_sv_undef;
2474 }
2475 79 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 36 43 79
2476 36 entry->eof = 1;
2477 } else {
2478 43 entry->buf_len += n;
2479 }
2480 }
2481 }
2482  
2483 0 static int file_lines_eof(IV idx) {
2484 dTHX;
2485 LineIterEntry *entry;
2486  
2487 0 ▶ if (idx < 0 || idx >= g_iters_count) {
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_iters_count) { 0 0 0
if (idx < 0 || idx >= g_iters_count) { 0 0 0
2488 0 return 1;
2489 }
2490  
2491 0 entry = &g_iters[idx];
2492 0 ▶ return entry->eof && entry->buf_pos >= entry->buf_len;
Branch: 2 branches
BranchTrueFalseTotal
return entry->eof && entry->buf_pos >= entry->buf_len; 0 0 0
return entry->eof && entry->buf_pos >= entry->buf_len; 0 0 0
2493 }
2494  
2495 647 static void file_lines_close(IV idx) {
2496 dTHX;
2497 647 ▶ if (idx < 0 || idx >= g_iters_count) return;
Branch: 2 branches
BranchTrueFalseTotal
if (idx < 0 || idx >= g_iters_count) return; 645 2 647
if (idx < 0 || idx >= g_iters_count) return; 0 645 645
2498  
2499 645 LineIterEntry *entry = &g_iters[idx];
2500 645 entry->refcount--;
2501 645 ▶ if (entry->refcount <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->refcount <= 0) { 645 0 645
2502 645 free_iter_slot(idx);
2503 }
2504 }
2505  
2506 /* ============================================
2507 Fast stat operations
2508 ============================================ */
2509  
2510 65 static IV file_size_internal(const char *path) {
2511 dTHX;
2512 Stat_t st;
2513 65 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 2 63 65
2514 2 return -1;
2515 }
2516 63 return st.st_size;
2517 }
2518  
2519 168 static int file_exists_internal(const char *path) {
2520 dTHX;
2521 Stat_t st;
2522 168 return cached_stat(path, &st) == 0;
2523 }
2524  
2525 33 static int file_is_file_internal(const char *path) {
2526 dTHX;
2527 Stat_t st;
2528 33 ▶ if (cached_stat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) return 0; 2 31 33
2529 31 return S_ISREG(st.st_mode);
2530 }
2531  
2532 38 static int file_is_dir_internal(const char *path) {
2533 dTHX;
2534 Stat_t st;
2535 38 ▶ if (cached_stat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) return 0; 7 31 38
2536 31 return S_ISDIR(st.st_mode);
2537 }
2538  
2539 9 static int file_is_readable_internal(const char *path) {
2540 dTHX;
2541 9 return file_is_readable_cached(path);
2542 }
2543  
2544 9 static int file_is_writable_internal(const char *path) {
2545 dTHX;
2546 9 return file_is_writable_cached(path);
2547 }
2548  
2549 31 static IV file_mtime_internal(const char *path) {
2550 dTHX;
2551 Stat_t st;
2552 31 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 1 30 31
2553 1 return -1;
2554 }
2555 30 return st.st_mtime;
2556 }
2557  
2558 8 static IV file_atime_internal(const char *path) {
2559 dTHX;
2560 Stat_t st;
2561 8 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 1 7 8
2562 1 return -1;
2563 }
2564 7 return st.st_atime;
2565 }
2566  
2567 8 static IV file_ctime_internal(const char *path) {
2568 dTHX;
2569 Stat_t st;
2570 8 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 1 7 8
2571 1 return -1;
2572 }
2573 7 return st.st_ctime;
2574 }
2575  
2576 7 static IV file_mode_internal(const char *path) {
2577 dTHX;
2578 Stat_t st;
2579 7 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 1 6 7
2580 1 return -1;
2581 }
2582 6 return st.st_mode & 07777; /* Return permission bits only */
2583 }
2584  
2585 #ifdef _WIN32
2586 /* The CRT's stat() reports st_ino as 0 and st_dev as the drive index, so
2587 * the pair cannot tell two files apart on Windows. The volume serial
2588 * number and the 64-bit file index are the real identity. They need an
2589 * open handle, which is why this is called only from the full stat()
2590 * hash, where ino is actually published, and never from the single-field
2591 * accessors. FILE_FLAG_BACKUP_SEMANTICS is what lets a directory be
2592 * opened at all; FILE_READ_ATTRIBUTES opens a file others hold open. */
2593 static int file_win32_identity(const char *path, DWORD *dev,
2594 DWORD *ino_hi, DWORD *ino_lo)
2595 {
2596 BY_HANDLE_FILE_INFORMATION bhfi;
2597 HANDLE h;
2598 int ok = 0;
2599  
2600 h = CreateFileA(path, FILE_READ_ATTRIBUTES,
2601 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2602 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2603 if (h == INVALID_HANDLE_VALUE) {
2604 return 0;
2605 }
2606 if (GetFileInformationByHandle(h, &bhfi)) {
2607 *dev = bhfi.dwVolumeSerialNumber;
2608 *ino_hi = bhfi.nFileIndexHigh;
2609 *ino_lo = bhfi.nFileIndexLow;
2610 ok = 1;
2611 }
2612 CloseHandle(h);
2613 return ok;
2614 }
2615  
2616 /* A 32-bit UV cannot hold a file index, so fall back to an NV rather than
2617 * truncate. The indexes a real volume hands out stay far below 2**53,
2618 * where a double is still exact. */
2619 static SV *file_ino_sv(pTHX_ DWORD hi, DWORD lo)
2620 {
2621 #if UVSIZE >= 8
2622 return newSVuv(((UV)hi << 32) | (UV)lo);
2623 #else
2624 if (hi == 0) {
2625 return newSVuv((UV)lo);
2626 }
2627 return newSVnv((NV)hi * 4294967296.0 + (NV)lo);
2628 #endif
2629 }
2630 #else
2631 /* st_ino is 64 bits on filesystems an ivsize=4 perl still has to read. */
2632 6 static SV *file_ino_sv(pTHX_ Stat_t *st)
2633 {
2634 6 ▶ if ((NV)st->st_ino <= (NV)UV_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if ((NV)st->st_ino <= (NV)UV_MAX) { 6 0 6
2635 6 return newSVuv((UV)st->st_ino);
2636 }
2637 0 return newSVnv((NV)st->st_ino);
2638 }
2639 #endif
2640  
2641 /* Combined stat - returns all attributes in one syscall */
2642 7 static HV* file_stat_all_internal(pTHX_ const char *path) {
2643 Stat_t st;
2644 HV *result;
2645 SV *ino_sv;
2646 UV dev;
2647  
2648 7 ▶ if (cached_stat(path, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached_stat(path, &st) < 0) { 1 6 7
2649 1 return NULL;
2650 }
2651  
2652 6 dev = (UV)st.st_dev;
2653 #ifdef _WIN32
2654 {
2655 DWORD wdev, whi, wlo;
2656 if (file_win32_identity(path, &wdev, &whi, &wlo)) {
2657 dev = (UV)wdev;
2658 ino_sv = file_ino_sv(aTHX_ whi, wlo);
2659 } else {
2660 ino_sv = newSVuv(0);
2661 }
2662 }
2663 #else
2664 6 ino_sv = file_ino_sv(aTHX_ &st);
2665 #endif
2666  
2667 6 result = newHV();
2668 6 hv_store(result, "size", 4, newSViv(st.st_size), 0);
2669 6 hv_store(result, "mtime", 5, newSViv(st.st_mtime), 0);
2670 6 hv_store(result, "atime", 5, newSViv(st.st_atime), 0);
2671 6 hv_store(result, "ctime", 5, newSViv(st.st_ctime), 0);
2672 6 hv_store(result, "mode", 4, newSViv(st.st_mode & 07777), 0);
2673 6 ▶ hv_store(result, "is_file", 7, S_ISREG(st.st_mode) ? &PL_sv_yes : &PL_sv_no, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(result, "is_file", 7, S_ISREG(st.st_mode) ? &PL_sv_yes : &PL_sv_no, 0); 5 1 6
2674 6 ▶ hv_store(result, "is_dir", 6, S_ISDIR(st.st_mode) ? &PL_sv_yes : &PL_sv_no, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(result, "is_dir", 6, S_ISDIR(st.st_mode) ? &PL_sv_yes : &PL_sv_no, 0); 1 5 6
2675 6 hv_store(result, "dev", 3, newSVuv(dev), 0);
2676 6 hv_store(result, "ino", 3, ino_sv, 0);
2677 6 hv_store(result, "nlink", 5, newSViv(st.st_nlink), 0);
2678 6 hv_store(result, "uid", 3, newSViv(st.st_uid), 0);
2679 6 hv_store(result, "gid", 3, newSViv(st.st_gid), 0);
2680  
2681 6 return result;
2682 }
2683  
2684 10 static int file_is_link_internal(const char *path) {
2685 dTHX;
2686 #ifdef _WIN32
2687 /* Windows: check for reparse point */
2688 DWORD attrs = GetFileAttributesA(path);
2689 if (attrs == INVALID_FILE_ATTRIBUTES) return 0;
2690 return (attrs & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
2691 #else
2692 Stat_t st;
2693 10 ▶ if (lstat(path, &st) < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lstat(path, &st) < 0) return 0; 1 9 10
2694 9 return S_ISLNK(st.st_mode);
2695 #endif
2696 }
2697  
2698 10 static int file_is_executable_internal(const char *path) {
2699 dTHX;
2700 #ifdef _WIN32
2701 /* Windows: check file extension */
2702 const char *ext = strrchr(path, '.');
2703 if (ext) {
2704 if (_stricmp(ext, ".exe") == 0 || _stricmp(ext, ".bat") == 0 ||
2705 _stricmp(ext, ".cmd") == 0 || _stricmp(ext, ".com") == 0) {
2706 return 1;
2707 }
2708 }
2709 return 0;
2710 #else
2711 10 return file_is_executable_cached(path);
2712 #endif
2713 }
2714  
2715 /* ============================================
2716 File manipulation operations
2717 ============================================ */
2718  
2719 6 static int file_unlink_internal(const char *path) {
2720 dTHX;
2721 int result;
2722 #ifdef _WIN32
2723 result = _unlink(path) == 0;
2724 #else
2725 6 result = unlink(path) == 0;
2726 #endif
2727 /* Invalidate cache if this path was cached */
2728 6 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 4 6
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 0 2
2729 2 g_stat_cache.valid = 0;
2730 }
2731 6 return result;
2732 }
2733  
2734 4 static int file_copy_internal(pTHX_ const char *src, const char *dst) {
2735 #if defined(__APPLE__)
2736 /* macOS: Use native copyfile() for best performance and metadata */
2737 Stat_t st;
2738 int result;
2739 if (stat(src, &st) < 0) return 0;
2740 result = copyfile(src, dst, NULL, COPYFILE_DATA) == 0;
2741 if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) {
2742 g_stat_cache.valid = 0;
2743 }
2744 return result;
2745 #elif defined(__linux__)
2746 /* Linux: Use sendfile() for zero-copy transfer */
2747 int fd_src, fd_dst;
2748 Stat_t st;
2749 4 off_t offset = 0;
2750 ssize_t sent;
2751  
2752 4 fd_src = open(src, O_RDONLY);
2753 4 ▶ if (fd_src < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fd_src < 0) return 0; 1 3 4
2754  
2755 3 ▶ if (fstat(fd_src, &st) < 0 || !S_ISREG(st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fstat(fd_src, &st) < 0 || !S_ISREG(st.st_mode)) { 3 0 3
if (fstat(fd_src, &st) < 0 || !S_ISREG(st.st_mode)) { 0 3 3
2756 0 close(fd_src);
2757 0 return 0;
2758 }
2759  
2760 3 fd_dst = file_open3(dst, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode & 07777);
2761 3 ▶ if (fd_dst < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd_dst < 0) { 0 3 3
2762 0 close(fd_src);
2763 0 return 0;
2764 }
2765  
2766 /* sendfile() for zero-copy - much faster than read/write */
2767 5 ▶ while (offset < st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < st.st_size) { 2 3 5
2768 2 sent = sendfile(fd_dst, fd_src, &offset, st.st_size - offset);
2769 2 ▶ if (sent < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sent < 0) { 0 2 2
2770 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
2771 0 ▶ if (errno == EINVAL || errno == ENOSYS) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno == EINVAL || errno == ENOSYS) { 0 0 0
if (errno == EINVAL || errno == ENOSYS) { 0 0 0
2772 /* sendfile not supported, fallback to read/write */
2773 char *buffer;
2774 ssize_t n_read, n_written, written;
2775 0 int result = 0;
2776  
2777 /* Reposition to where we left off */
2778 0 lseek(fd_src, offset, SEEK_SET);
2779  
2780 0 Newx(buffer, FILE_BULK_BUFFER_SIZE, char);
2781 while (1) {
2782 0 n_read = read(fd_src, buffer, FILE_BULK_BUFFER_SIZE);
2783 0 ▶ if (n_read < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_read < 0) { 0 0 0
2784 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
2785 0 break;
2786 }
2787 0 ▶ if (n_read == 0) { result = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (n_read == 0) { result = 1; break; } 0 0 0
2788  
2789 0 written = 0;
2790 0 ▶ while (written < n_read) {
Branch: 1 branch
BranchTrueFalseTotal
while (written < n_read) { 0 0 0
2791 0 n_written = write(fd_dst, buffer + written, n_read - written);
2792 0 ▶ if (n_written < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_written < 0) { 0 0 0
2793 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
2794 0 goto fallback_cleanup;
2795 }
2796 0 written += n_written;
2797 }
2798 }
2799 0 fallback_cleanup:
2800 0 Safefree(buffer);
2801 0 close(fd_src);
2802 0 close(fd_dst);
2803 0 ▶ if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) { 0 0 0
if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) { 0 0 0
if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) { 0 0 0
2804 0 g_stat_cache.valid = 0;
2805 }
2806 0 return result;
2807 }
2808 0 close(fd_src);
2809 0 close(fd_dst);
2810 0 return 0;
2811 }
2812 2 ▶ if (sent == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sent == 0) break; 0 2 2
2813 }
2814  
2815 3 close(fd_src);
2816 3 close(fd_dst);
2817 /* Invalidate cache for dst */
2818 3 ▶ if (g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) { 1 2 3
if (g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) { 0 1 1
2819 0 g_stat_cache.valid = 0;
2820 }
2821 3 return 1;
2822 #else
2823 /* Portable fallback: read/write loop */
2824 int fd_src, fd_dst;
2825 char *buffer;
2826 ssize_t n_read, n_written, written;
2827 Stat_t st;
2828 int result = 0;
2829 #ifdef _WIN32
2830 int open_flags_r = O_RDONLY | O_BINARY;
2831 int open_flags_w = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
2832 #else
2833 int open_flags_r = O_RDONLY;
2834 int open_flags_w = O_WRONLY | O_CREAT | O_TRUNC;
2835 #endif
2836  
2837 fd_src = open(src, open_flags_r);
2838 if (fd_src < 0) return 0;
2839  
2840 if (fstat(fd_src, &st) < 0) {
2841 close(fd_src);
2842 return 0;
2843 }
2844  
2845 fd_dst = file_open3(dst, open_flags_w, st.st_mode & 07777);
2846 if (fd_dst < 0) {
2847 close(fd_src);
2848 return 0;
2849 }
2850  
2851 Newx(buffer, FILE_BULK_BUFFER_SIZE, char);
2852  
2853 while (1) {
2854 n_read = read(fd_src, buffer, FILE_BULK_BUFFER_SIZE);
2855 if (n_read < 0) {
2856 if (errno == EINTR) continue;
2857 goto cleanup;
2858 }
2859 if (n_read == 0) break;
2860  
2861 written = 0;
2862 while (written < n_read) {
2863 n_written = write(fd_dst, buffer + written, n_read - written);
2864 if (n_written < 0) {
2865 if (errno == EINTR) continue;
2866 goto cleanup;
2867 }
2868 written += n_written;
2869 }
2870 }
2871  
2872 result = 1;
2873  
2874 cleanup:
2875 Safefree(buffer);
2876 close(fd_src);
2877 close(fd_dst);
2878 if (result && g_stat_cache.valid && strcmp(dst, g_stat_cache.path) == 0) {
2879 g_stat_cache.valid = 0;
2880 }
2881 return result;
2882 #endif
2883 }
2884  
2885 3 static int file_move_internal(pTHX_ const char *src, const char *dst) {
2886 int result;
2887
2888 /* Try rename first (fast path for same filesystem) */
2889 3 ▶ if (rename(src, dst) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename(src, dst) == 0) { 2 1 3
2890 2 result = 1;
2891 }
2892 /* If EXDEV, copy then delete (cross-device move) */
2893 1 ▶ else if (errno == EXDEV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (errno == EXDEV) { 0 1 1
2894 0 ▶ if (file_copy_internal(aTHX_ src, dst)) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_copy_internal(aTHX_ src, dst)) { 0 0 0
2895 0 result = file_unlink_internal(src);
2896 } else {
2897 0 return 0;
2898 }
2899 } else {
2900 1 return 0;
2901 }
2902
2903 /* Invalidate cache for both paths */
2904 2 ▶ if (g_stat_cache.valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_stat_cache.valid) { 1 1 2
2905 1 ▶ if (strcmp(src, g_stat_cache.path) == 0 || strcmp(dst, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(src, g_stat_cache.path) == 0 || strcmp(dst, g_stat_cache.path) == 0) { 1 0 1
if (strcmp(src, g_stat_cache.path) == 0 || strcmp(dst, g_stat_cache.path) == 0) { 0 1 1
2906 0 g_stat_cache.valid = 0;
2907 }
2908 }
2909 2 return result;
2910 }
2911  
2912 3 static int file_touch_internal(const char *path) {
2913 dTHX;
2914 int result;
2915 #ifdef _WIN32
2916 HANDLE h;
2917 FILETIME ft;
2918 SYSTEMTIME st;
2919 result = 0;
2920  
2921 /* Try to open existing file */
2922 h = CreateFileA(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
2923 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2924 if (h == INVALID_HANDLE_VALUE) {
2925 return 0;
2926 }
2927  
2928 GetSystemTime(&st);
2929 SystemTimeToFileTime(&st, &ft);
2930 result = SetFileTime(h, NULL, &ft, &ft) != 0;
2931 CloseHandle(h);
2932 #else
2933 int fd;
2934 /* Try to update times on existing file - utime(path, NULL) sets to current time */
2935 3 ▶ if (utime(path, NULL) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (utime(path, NULL) == 0) { 1 2 3
2936 1 result = 1;
2937 } else {
2938 /* File doesn't exist, create it */
2939 2 fd = file_open3(path, O_WRONLY | O_CREAT, 0644);
2940 2 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 2 2
2941 0 return 0;
2942 }
2943 2 close(fd);
2944 2 result = 1;
2945 }
2946 #endif
2947 /* Invalidate cache if this path was cached */
2948 3 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 1 3
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 1 1 2
2949 1 g_stat_cache.valid = 0;
2950 }
2951 3 return result;
2952 }
2953  
2954 4 static int file_chmod_internal(const char *path, int mode) {
2955 dTHX;
2956 int result;
2957 #ifdef _WIN32
2958 result = _chmod(path, mode) == 0;
2959 #else
2960 4 result = chmod(path, mode) == 0;
2961 #endif
2962 /* Invalidate cache if this path was cached */
2963 4 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 4 0 4
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 2 4
2964 2 g_stat_cache.valid = 0;
2965 }
2966 4 return result;
2967 }
2968  
2969 18 static int file_mkdir_internal(const char *path, int mode) {
2970 dTHX;
2971 int result;
2972 #ifdef _WIN32
2973 PERL_UNUSED_VAR(mode);
2974 result = _mkdir(path) == 0;
2975 #else
2976 18 result = mkdir(path, mode) == 0;
2977 #endif
2978 /* Invalidate cache if this path was cached */
2979 18 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 6 12 18
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 1 5 6
2980 1 g_stat_cache.valid = 0;
2981 }
2982 18 return result;
2983 }
2984  
2985 9 static int file_rmdir_internal(const char *path) {
2986 dTHX;
2987 int result;
2988 #ifdef _WIN32
2989 result = _rmdir(path) == 0;
2990 #else
2991 9 result = rmdir(path) == 0;
2992 #endif
2993 /* Invalidate cache if this path was cached */
2994 9 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 7 9
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 2 0 2
2995 2 g_stat_cache.valid = 0;
2996 }
2997 9 return result;
2998 }
2999  
3000 /* ============================================
3001 Directory listing
3002 ============================================ */
3003  
3004 7 static AV* file_readdir_internal(pTHX_ const char *path) {
3005 7 AV *result = newAV();
3006  
3007 #ifdef _WIN32
3008 WIN32_FIND_DATAA fd;
3009 HANDLE h;
3010 char pattern[MAX_PATH];
3011 size_t len = strlen(path);
3012  
3013 if (len + 3 > MAX_PATH) return result;
3014  
3015 memcpy(pattern, path, len);
3016 if (len > 0 && path[len-1] != '\\' && path[len-1] != '/') {
3017 pattern[len++] = '\\';
3018 }
3019 pattern[len++] = '*';
3020 pattern[len] = '\0';
3021  
3022 h = FindFirstFileA(pattern, &fd);
3023 if (h == INVALID_HANDLE_VALUE) return result;
3024  
3025 do {
3026 /* Skip . and .. */
3027 if (strcmp(fd.cFileName, ".") != 0 && strcmp(fd.cFileName, "..") != 0) {
3028 av_push(result, newSVpv(fd.cFileName, 0));
3029 }
3030 } while (FindNextFileA(h, &fd));
3031  
3032 FindClose(h);
3033 #else
3034 DIR *dir;
3035 struct dirent *entry;
3036  
3037 7 dir = opendir(path);
3038 7 ▶ if (!dir) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (!dir) return result; 2 5 7
3039  
3040 33 ▶ while ((entry = readdir(dir)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = readdir(dir)) != NULL) { 28 5 33
3041 /* Skip . and .. */
3042 28 ▶ if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 23 5 28
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 18 5 23
3043 18 av_push(result, newSVpv(entry->d_name, 0));
3044 }
3045 }
3046  
3047 5 closedir(dir);
3048 #endif
3049  
3050 5 return result;
3051 }
3052  
3053 /* ============================================
3054 Path manipulation
3055 ============================================ */
3056  
3057 83 static SV* file_basename_internal(pTHX_ const char *path) {
3058 const char *p;
3059 83 size_t len = strlen(path);
3060  
3061 83 ▶ if (len == 0) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return newSVpvs(""); 2 81 83
3062  
3063 /* Skip trailing slashes */
3064 88 ▶ while (len > 0 && (path[len-1] == '/' || path[len-1] == '\\')) {
Branch: 3 branches
BranchTrueFalseTotal
while (len > 0 && (path[len-1] == '/' || path[len-1] == '\\')) { 85 3 88
while (len > 0 && (path[len-1] == '/' || path[len-1] == '\\')) { 7 78 85
while (len > 0 && (path[len-1] == '/' || path[len-1] == '\\')) { 0 78 78
3065 7 len--;
3066 }
3067 81 ▶ if (len == 0) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return newSVpvs(""); 3 78 81
3068  
3069 /* Find last separator */
3070 78 p = path + len - 1;
3071 735 ▶ while (p > path && *p != '/' && *p != '\\') {
Branch: 3 branches
BranchTrueFalseTotal
while (p > path && *p != '/' && *p != '\\') { 732 3 735
while (p > path && *p != '/' && *p != '\\') { 657 75 732
while (p > path && *p != '/' && *p != '\\') { 657 0 657
3072 657 p--;
3073 }
3074 78 ▶ if (*p == '/' || *p == '\\') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '/' || *p == '\\') p++; 3 75 78
if (*p == '/' || *p == '\\') p++; 0 3 3
3075  
3076 78 return newSVpvn(p, (path + len) - p);
3077 }
3078  
3079 20 static SV* file_dirname_internal(pTHX_ const char *path) {
3080 const char *end;
3081 20 size_t len = strlen(path);
3082  
3083 20 ▶ if (len == 0) return newSVpvs(".");
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return newSVpvs("."); 1 19 20
3084  
3085 /* Skip trailing slashes */
3086 19 end = path + len - 1;
3087 21 ▶ while (end > path && (*end == '/' || *end == '\\')) {
Branch: 3 branches
BranchTrueFalseTotal
while (end > path && (*end == '/' || *end == '\\')) { 19 2 21
while (end > path && (*end == '/' || *end == '\\')) { 2 17 19
while (end > path && (*end == '/' || *end == '\\')) { 0 17 17
3088 2 end--;
3089 }
3090  
3091 /* Find last separator */
3092 137 ▶ while (end > path && *end != '/' && *end != '\\') {
Branch: 3 branches
BranchTrueFalseTotal
while (end > path && *end != '/' && *end != '\\') { 131 6 137
while (end > path && *end != '/' && *end != '\\') { 118 13 131
while (end > path && *end != '/' && *end != '\\') { 118 0 118
3093 118 end--;
3094 }
3095  
3096 19 ▶ if (end == path) {
Branch: 1 branch
BranchTrueFalseTotal
if (end == path) { 6 13 19
3097 6 ▶ if (*end == '/' || *end == '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if (*end == '/' || *end == '\\') { 2 4 6
if (*end == '/' || *end == '\\') { 0 2 2
3098 4 return newSVpvn(path, 1);
3099 }
3100 2 return newSVpvs(".");
3101 }
3102  
3103 /* Skip multiple trailing slashes in dirname */
3104 14 ▶ while (end > path && (*(end-1) == '/' || *(end-1) == '\\')) {
Branch: 3 branches
BranchTrueFalseTotal
while (end > path && (*(end-1) == '/' || *(end-1) == '\\')) { 14 0 14
while (end > path && (*(end-1) == '/' || *(end-1) == '\\')) { 1 13 14
while (end > path && (*(end-1) == '/' || *(end-1) == '\\')) { 0 13 13
3105 1 end--;
3106 }
3107  
3108 13 return newSVpvn(path, end - path);
3109 }
3110  
3111 49 static SV* file_extname_internal(pTHX_ const char *path) {
3112 const char *dot;
3113 const char *basename;
3114 49 size_t len = strlen(path);
3115  
3116 49 ▶ if (len == 0) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return newSVpvs(""); 1 48 49
3117  
3118 /* Find basename first */
3119 48 basename = path + len - 1;
3120 452 ▶ while (basename > path && *basename != '/' && *basename != '\\') {
Branch: 3 branches
BranchTrueFalseTotal
while (basename > path && *basename != '/' && *basename != '\\') { 440 12 452
while (basename > path && *basename != '/' && *basename != '\\') { 404 36 440
while (basename > path && *basename != '/' && *basename != '\\') { 404 0 404
3121 404 basename--;
3122 }
3123 48 ▶ if (*basename == '/' || *basename == '\\') basename++;
Branch: 2 branches
BranchTrueFalseTotal
if (*basename == '/' || *basename == '\\') basename++; 12 36 48
if (*basename == '/' || *basename == '\\') basename++; 0 12 12
3124  
3125 /* Find last dot in basename */
3126 48 dot = strrchr(basename, '.');
3127 48 ▶ if (!dot || dot == basename) return newSVpvs("");
Branch: 2 branches
BranchTrueFalseTotal
if (!dot || dot == basename) return newSVpvs(""); 44 4 48
if (!dot || dot == basename) return newSVpvs(""); 2 42 44
3128  
3129 42 return newSVpv(dot, 0);
3130 }
3131  
3132 12 static SV* file_join_internal(pTHX_ AV *parts) {
3133 SV *result;
3134 SSize_t i, len;
3135 12 STRLEN total_len = 0;
3136 char *buf, *p;
3137 int need_sep;
3138  
3139 12 len = av_len(parts) + 1;
3140 12 ▶ if (len == 0) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) return newSVpvs(""); 0 12 12
3141  
3142 /* Calculate total length */
3143 40 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 28 12 40
3144 28 SV **sv = av_fetch(parts, i, 0);
3145 28 ▶ if (sv && SvPOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvPOK(*sv)) { 28 0 28
if (sv && SvPOK(*sv)) { 28 0 28
3146 28 total_len += SvCUR(*sv) + 1; /* +1 for separator */
3147 }
3148 }
3149  
3150 12 result = newSV(total_len + 1);
3151 12 SvPOK_on(result);
3152 12 buf = SvPVX(result);
3153 12 p = buf;
3154 12 need_sep = 0;
3155  
3156 40 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 28 12 40
3157 28 SV **sv = av_fetch(parts, i, 0);
3158 28 ▶ if (sv && SvPOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvPOK(*sv)) { 28 0 28
if (sv && SvPOK(*sv)) { 28 0 28
3159 STRLEN part_len;
3160 28 const char *part = SvPV(*sv, part_len);
3161  
3162 28 ▶ if (part_len == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (part_len == 0) continue; 1 27 28
3163  
3164 /* Skip leading separator if we already have one */
3165 29 ▶ while (part_len > 0 && (*part == '/' || *part == '\\')) {
Branch: 3 branches
BranchTrueFalseTotal
while (part_len > 0 && (*part == '/' || *part == '\\')) { 29 0 29
while (part_len > 0 && (*part == '/' || *part == '\\')) { 9 20 29
while (part_len > 0 && (*part == '/' || *part == '\\')) { 0 20 20
3166 9 ▶ if (!need_sep && p == buf) break; /* Keep root slash */
Branch: 2 branches
BranchTrueFalseTotal
if (!need_sep && p == buf) break; /* Keep root slash */ 9 0 9
if (!need_sep && p == buf) break; /* Keep root slash */ 7 2 9
3167 2 part++;
3168 2 part_len--;
3169 }
3170  
3171 27 ▶ if (need_sep && part_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (need_sep && part_len > 0) { 12 15 27
if (need_sep && part_len > 0) { 12 0 12
3172 #ifdef _WIN32
3173 *p++ = '\\';
3174 #else
3175 12 *p++ = '/';
3176 #endif
3177 }
3178  
3179 27 ▶ if (part_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (part_len > 0) { 27 0 27
3180 27 memcpy(p, part, part_len);
3181 27 p += part_len;
3182  
3183 /* Check if ends with separator */
3184 27 ▶ need_sep = (*(p-1) != '/' && *(p-1) != '\\');
Branch: 2 branches
BranchTrueFalseTotal
need_sep = (*(p-1) != '/' && *(p-1) != '\\'); 24 3 27
need_sep = (*(p-1) != '/' && *(p-1) != '\\'); 24 0 24
3185 }
3186 }
3187 }
3188  
3189 12 *p = '\0';
3190 12 SvCUR_set(result, p - buf);
3191 12 return result;
3192 }
3193  
3194 /* ============================================
3195 Head and Tail operations
3196 ============================================ */
3197  
3198 6 static AV* file_head_internal(pTHX_ const char *path, IV n) {
3199 6 AV *result = newAV();
3200 IV idx;
3201 SV *line;
3202 6 IV count = 0;
3203  
3204 6 ▶ if (n <= 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return result; 1 5 6
3205  
3206 5 idx = file_lines_open(aTHX_ path);
3207 5 ▶ if (idx < 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) return result; 1 4 5
3208  
3209 22 ▶ while (count < n && (line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 2 branches
BranchTrueFalseTotal
while (count < n && (line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 20 2 22
while (count < n && (line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 18 2 20
3210 18 av_push(result, line);
3211 18 count++;
3212 }
3213  
3214 4 file_lines_close(idx);
3215 4 return result;
3216 }
3217  
3218 6 static AV* file_tail_internal(pTHX_ const char *path, IV n) {
3219 6 AV *result = newAV();
3220 AV *buffer;
3221 SV *line;
3222 IV idx;
3223 SSize_t i, buf_len;
3224  
3225 6 ▶ if (n <= 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return result; 1 5 6
3226  
3227 5 idx = file_lines_open(aTHX_ path);
3228 5 ▶ if (idx < 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) return result; 1 4 5
3229  
3230 /* Use circular buffer to keep last N lines */
3231 4 buffer = newAV();
3232 4 av_extend(buffer, n - 1);
3233  
3234 47 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 43 4 47
3235 43 ▶ if (av_len(buffer) + 1 >= n) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(buffer) + 1 >= n) { 25 18 43
3236 25 SV *old = av_shift(buffer);
3237 25 SvREFCNT_dec(old);
3238 }
3239 43 av_push(buffer, line);
3240 }
3241  
3242 4 file_lines_close(idx);
3243  
3244 /* Copy buffer to result */
3245 4 buf_len = av_len(buffer) + 1;
3246 22 ▶ for (i = 0; i < buf_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < buf_len; i++) { 18 4 22
3247 18 SV **sv = av_fetch(buffer, i, 0);
3248 18 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 18 0 18
3249 18 av_push(result, newSVsv(*sv));
3250 }
3251 }
3252  
3253 4 SvREFCNT_dec((SV*)buffer);
3254 4 return result;
3255 }
3256  
3257 /* range_lines: 1-based, half-open in count style.
3258 * range_lines($p, 1, 10) -> first 10 lines (same as head($p, 10))
3259 * range_lines($p, 5, 3) -> lines 5, 6, 7
3260 * If `from` is past EOF or `count <= 0`, returns an empty AV (no error).
3261 * `from < 1` is also treated as empty (caller error - documented).
3262 *
3263 * Implementation: skip-then-take using the existing line iterator. SVs
3264 * for the skipped lines are allocated and immediately freed; that's
3265 * O(skip) cheap work but bounded by line size, not file size. For very
3266 * large skips (millions of lines) a buffer-scan-without-allocation
3267 * variant would help; deferred until benchmarks demand it. */
3268 9 static AV* file_range_internal(pTHX_ const char *path, IV from, IV count) {
3269 9 AV *result = newAV();
3270 IV idx, i;
3271 SV *line;
3272  
3273 9 ▶ if (count <= 0 || from < 1) return result;
Branch: 2 branches
BranchTrueFalseTotal
if (count <= 0 || from < 1) return result; 7 2 9
if (count <= 0 || from < 1) return result; 2 5 7
3274  
3275 5 idx = file_lines_open(aTHX_ path);
3276 5 ▶ if (idx < 0) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) return result; 0 5 5
3277  
3278 /* Skip lines 1 .. from-1 */
3279 60 ▶ for (i = 0; i < from - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < from - 1; i++) { 56 4 60
3280 56 line = file_lines_next(aTHX_ idx);
3281 56 ▶ if (line == &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (line == &PL_sv_undef) { 1 55 56
3282 1 file_lines_close(idx);
3283 1 return result;
3284 }
3285 55 SvREFCNT_dec(line);
3286 }
3287  
3288 /* Take `count` lines starting at line `from` */
3289 4 av_extend(result, count - 1);
3290 19 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 16 3 19
3291 16 line = file_lines_next(aTHX_ idx);
3292 16 ▶ if (line == &PL_sv_undef) break;
Branch: 1 branch
BranchTrueFalseTotal
if (line == &PL_sv_undef) break; 1 15 16
3293 15 av_push(result, line);
3294 }
3295  
3296 4 file_lines_close(idx);
3297 4 return result;
3298 }
3299  
3300 /* ============================================
3301 Atomic spew - write to temp file then rename
3302 ============================================ */
3303  
3304 17 static int file_atomic_spew_internal(pTHX_ const char *path, SV *data) {
3305 char temp_path[4096];
3306 int fd;
3307 const char *buf;
3308 STRLEN len;
3309 17 ssize_t written = 0, n;
3310 static int counter = 0;
3311 #ifdef _WIN32
3312 int open_flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
3313 int pid = (int)GetCurrentProcessId();
3314 #else
3315 17 int open_flags = O_WRONLY | O_CREAT | O_TRUNC;
3316 17 int pid = (int)getpid();
3317 #endif
3318  
3319 /* Create temp file name in same directory */
3320 17 snprintf(temp_path, sizeof(temp_path), "%s.tmp.%d.%d", path, pid, counter++);
3321  
3322 17 buf = SvPV(data, len);
3323  
3324 17 fd = file_open3(temp_path, open_flags, 0644);
3325 17 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 17 17
3326 0 return 0;
3327 }
3328  
3329 33 ▶ while ((size_t)written < len) {
Branch: 1 branch
BranchTrueFalseTotal
while ((size_t)written < len) { 16 17 33
3330 16 n = write(fd, buf + written, len - written);
3331 16 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 16 16
3332 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
3333 0 close(fd);
3334 0 file_unlink_internal(temp_path);
3335 0 return 0;
3336 }
3337 16 written += n;
3338 }
3339  
3340 #ifdef _WIN32
3341 /* Sync to disk on Windows */
3342 _commit(fd);
3343 #else
3344 /* Sync to disk on POSIX */
3345 17 fsync(fd);
3346 #endif
3347  
3348 17 close(fd);
3349  
3350 /* Atomic rename */
3351 17 ▶ if (rename(temp_path, path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename(temp_path, path) != 0) { 0 17 17
3352 0 file_unlink_internal(temp_path);
3353 0 return 0;
3354 }
3355  
3356 /* Invalidate cache for this path */
3357 17 ▶ if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 10 7 17
if (g_stat_cache.valid && strcmp(path, g_stat_cache.path) == 0) { 0 10 10
3358 0 g_stat_cache.valid = 0;
3359 }
3360 17 return 1;
3361 }
3362  
3363 /* ============================================
3364 Split lines utility
3365 ============================================ */
3366  
3367 6 static AV* file_split_lines(pTHX_ SV *content) {
3368 AV *lines;
3369 const char *start, *end, *p;
3370 STRLEN len;
3371  
3372 6 start = SvPV(content, len);
3373 6 end = start + len;
3374 6 lines = newAV();
3375  
3376 16 ▶ while (start < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (start < end) { 14 2 16
3377 14 p = memchr(start, '\n', end - start);
3378 14 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 10 4 14
3379 10 av_push(lines, newSVpvn(start, p - start));
3380 10 start = p + 1;
3381 } else {
3382 4 ▶ if (start < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (start < end) { 4 0 4
3383 4 av_push(lines, newSVpvn(start, end - start));
3384 }
3385 4 break;
3386 }
3387 }
3388  
3389 6 return lines;
3390 }
3391  
3392 /* ============================================
3393 XS Functions
3394 ============================================ */
3395  
3396 52 XS_INTERNAL(xs_slurp) {
3397 52 dXSARGS;
3398 const char *path;
3399 SV *bytes;
3400  
3401 52 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 52 52
3402 0 croak("Usage: file::slurp(path [, plugin => ..., key => value ...])");
3403  
3404 52 path = SvPV_nolen(ST(0));
3405 52 bytes = file_slurp_internal(aTHX_ path);
3406  
3407 52 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 17 35 52
3408 17 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 1, items, "slurp");
3409 15 SV *out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
3410 13 SvREFCNT_dec((SV *)opts);
3411 13 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 2 11 13
3412 2 SvREFCNT_dec(bytes);
3413 2 ST(0) = &PL_sv_undef;
3414 2 XSRETURN(1);
3415 }
3416 11 ▶ if (out != bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) { 11 0 11
3417 11 SvREFCNT_dec(bytes);
3418 11 bytes = out;
3419 }
3420 }
3421  
3422 46 ST(0) = sv_2mortal(bytes);
3423 46 XSRETURN(1);
3424 }
3425  
3426 2 XS_INTERNAL(xs_slurp_raw) {
3427 2 dXSARGS;
3428 const char *path;
3429  
3430 2 ▶ if (items != 1) croak("Usage: file::slurp_raw(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::slurp_raw(path)"); 0 2 2
3431  
3432 2 path = SvPV_nolen(ST(0));
3433 2 ST(0) = sv_2mortal(file_slurp_raw(aTHX_ path));
3434 2 XSRETURN(1);
3435 }
3436  
3437 95 XS_INTERNAL(xs_spew) {
3438 95 dXSARGS;
3439 const char *path;
3440 SV *payload;
3441 95 SV *bytes_to_write = NULL;
3442  
3443 95 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 95 95
3444 0 croak("Usage: file::spew(path, data [, plugin => ..., key => value ...])");
3445  
3446 95 path = SvPV_nolen(ST(0));
3447 95 payload = ST(1);
3448  
3449 95 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 9 86 95
3450 9 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "spew");
3451 9 bytes_to_write = file_plugin_dispatch_write(aTHX_ opts, path, payload);
3452 7 SvREFCNT_dec((SV *)opts);
3453 7 ▶ if (!bytes_to_write) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes_to_write) { 2 5 7
3454 2 ST(0) = &PL_sv_no;
3455 2 XSRETURN(1);
3456 }
3457 5 payload = sv_2mortal(bytes_to_write);
3458 }
3459  
3460 91 ▶ ST(0) = file_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 91 0 91
3461 91 XSRETURN(1);
3462 }
3463  
3464 5 XS_INTERNAL(xs_append) {
3465 5 dXSARGS;
3466 const char *path;
3467 SV *payload;
3468 5 SV *bytes_to_write = NULL;
3469  
3470 5 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 5 5
3471 0 croak("Usage: file::append(path, data [, plugin => ..., key => value ...])");
3472  
3473 5 path = SvPV_nolen(ST(0));
3474 5 payload = ST(1);
3475  
3476 5 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 3 2 5
3477 3 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "append");
3478 3 bytes_to_write = file_plugin_dispatch_write(aTHX_ opts, path, payload);
3479 3 SvREFCNT_dec((SV *)opts);
3480 3 ▶ if (!bytes_to_write) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes_to_write) { 0 3 3
3481 0 ST(0) = &PL_sv_no;
3482 0 XSRETURN(1);
3483 }
3484 3 payload = sv_2mortal(bytes_to_write);
3485 }
3486  
3487 5 ▶ ST(0) = file_append_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_append_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 5 0 5
3488 5 XSRETURN(1);
3489 }
3490  
3491 11 XS_INTERNAL(xs_size) {
3492 11 dXSARGS;
3493 const char *path;
3494 IV size;
3495  
3496 11 ▶ if (items != 1) croak("Usage: file::size(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::size(path)"); 0 11 11
3497  
3498 11 path = SvPV_nolen(ST(0));
3499 11 size = file_size_internal(path);
3500 11 ST(0) = sv_2mortal(newSViv(size));
3501 11 XSRETURN(1);
3502 }
3503  
3504 4 XS_INTERNAL(xs_mtime) {
3505 4 dXSARGS;
3506 const char *path;
3507 IV mtime;
3508  
3509 4 ▶ if (items != 1) croak("Usage: file::mtime(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::mtime(path)"); 0 4 4
3510  
3511 4 path = SvPV_nolen(ST(0));
3512 4 mtime = file_mtime_internal(path);
3513 4 ST(0) = sv_2mortal(newSViv(mtime));
3514 4 XSRETURN(1);
3515 }
3516  
3517 21 XS_INTERNAL(xs_exists) {
3518 21 dXSARGS;
3519 const char *path;
3520  
3521 21 ▶ if (items != 1) croak("Usage: file::exists(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::exists(path)"); 0 21 21
3522  
3523 21 path = SvPV_nolen(ST(0));
3524 21 ▶ ST(0) = file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no; 14 7 21
3525 21 XSRETURN(1);
3526 }
3527  
3528 4 XS_INTERNAL(xs_is_file) {
3529 4 dXSARGS;
3530 const char *path;
3531  
3532 4 ▶ if (items != 1) croak("Usage: file::is_file(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_file(path)"); 0 4 4
3533  
3534 4 path = SvPV_nolen(ST(0));
3535 4 ▶ ST(0) = file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no; 3 1 4
3536 4 XSRETURN(1);
3537 }
3538  
3539 7 XS_INTERNAL(xs_is_dir) {
3540 7 dXSARGS;
3541 const char *path;
3542  
3543 7 ▶ if (items != 1) croak("Usage: file::is_dir(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_dir(path)"); 0 7 7
3544  
3545 7 path = SvPV_nolen(ST(0));
3546 7 ▶ ST(0) = file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no; 4 3 7
3547 7 XSRETURN(1);
3548 }
3549  
3550 2 XS_INTERNAL(xs_is_readable) {
3551 2 dXSARGS;
3552 const char *path;
3553  
3554 2 ▶ if (items != 1) croak("Usage: file::is_readable(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_readable(path)"); 0 2 2
3555  
3556 2 path = SvPV_nolen(ST(0));
3557 2 ▶ ST(0) = file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no; 1 1 2
3558 2 XSRETURN(1);
3559 }
3560  
3561 3 XS_INTERNAL(xs_is_writable) {
3562 3 dXSARGS;
3563 const char *path;
3564  
3565 3 ▶ if (items != 1) croak("Usage: file::is_writable(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_writable(path)"); 0 3 3
3566  
3567 3 path = SvPV_nolen(ST(0));
3568 3 ▶ ST(0) = file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no; 2 1 3
3569 3 XSRETURN(1);
3570 }
3571  
3572 9 XS_INTERNAL(xs_lines) {
3573 9 dXSARGS;
3574 const char *path;
3575 AV *lines;
3576 int fd;
3577 Stat_t st;
3578 char *buffer;
3579 char *p, *end, *line_start;
3580 size_t file_size;
3581 ssize_t total_read, n;
3582 #ifdef _WIN32
3583 int open_flags = O_RDONLY | O_BINARY;
3584 #else
3585 9 int open_flags = O_RDONLY;
3586 #endif
3587  
3588 9 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 9 9
3589 0 croak("Usage: file::lines(path [, plugin => ..., key => value ...])");
3590  
3591 9 path = SvPV_nolen(ST(0));
3592  
3593 /* Plugin path: route the slurp through plugin READ. If the plugin
3594 * returns an arrayref we hand that back unchanged (each element is
3595 * a record). If it returns bytes we fall through to the byte-split
3596 * helper below by stashing them in `buffer`. */
3597 9 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 2 7 9
3598 2 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 1, items, "lines");
3599 2 SV *bytes = file_slurp_internal(aTHX_ path);
3600 2 SV *out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
3601 2 SvREFCNT_dec((SV *)opts);
3602 2 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 2 2
3603 0 SvREFCNT_dec(bytes);
3604 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
3605 0 XSRETURN(1);
3606 }
3607 2 ▶ if (out != bytes) SvREFCNT_dec(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) SvREFCNT_dec(bytes); 2 0 2
3608 2 ▶ if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 1 1 2
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 1 0 1
3609 1 ST(0) = sv_2mortal(out);
3610 1 XSRETURN(1);
3611 }
3612 /* Plugin returned bytes - reuse the byte-split path below. */
3613 {
3614 STRLEN len;
3615 1 const char *pv = SvPV(out, len);
3616 1 AV *result = newAV();
3617 1 const char *cursor = pv;
3618 1 const char *bend = pv + len;
3619 const char *nl;
3620 1 av_extend(result, len / 40);
3621 3 ▶ while (cursor < bend) {
Branch: 1 branch
BranchTrueFalseTotal
while (cursor < bend) { 3 0 3
3622 3 nl = (const char *)memchr(cursor, '\n', bend - cursor);
3623 3 ▶ if (nl) {
Branch: 1 branch
BranchTrueFalseTotal
if (nl) { 2 1 3
3624 2 av_push(result, newSVpvn(cursor, nl - cursor));
3625 2 cursor = nl + 1;
3626 } else {
3627 1 ▶ if (cursor < bend)
Branch: 1 branch
BranchTrueFalseTotal
if (cursor < bend) 1 0 1
3628 1 av_push(result, newSVpvn(cursor, bend - cursor));
3629 1 break;
3630 }
3631 }
3632 1 SvREFCNT_dec(out);
3633 1 ST(0) = sv_2mortal(newRV_noinc((SV *)result));
3634 1 XSRETURN(1);
3635 }
3636 }
3637  
3638 7 fd = open(path, open_flags);
3639 7 ▶ if (UNLIKELY(fd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(fd < 0)) { 1 6 7
3640 1 ST(0) = sv_2mortal(newRV_noinc((SV*)newAV()));
3641 1 XSRETURN(1);
3642 }
3643  
3644 /* Get file size for single-read optimization */
3645 6 ▶ if (UNLIKELY(fstat(fd, &st) < 0 || st.st_size == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(fstat(fd, &st) < 0 || st.st_size == 0)) { 6 0 6
if (UNLIKELY(fstat(fd, &st) < 0 || st.st_size == 0)) { 1 5 6
3646 1 close(fd);
3647 1 ST(0) = sv_2mortal(newRV_noinc((SV*)newAV()));
3648 1 XSRETURN(1);
3649 }
3650  
3651 5 file_size = st.st_size;
3652 5 Newx(buffer, file_size + 1, char);
3653  
3654 /* Read entire file in one syscall when possible */
3655 5 total_read = 0;
3656 10 ▶ while ((size_t)total_read < file_size) {
Branch: 1 branch
BranchTrueFalseTotal
while ((size_t)total_read < file_size) { 5 5 10
3657 5 n = read(fd, buffer + total_read, file_size - total_read);
3658 5 ▶ if (UNLIKELY(n < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(n < 0)) { 0 5 5
3659 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
3660 0 break;
3661 }
3662 5 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 5 5
3663 5 total_read += n;
3664 }
3665 5 close(fd);
3666  
3667 5 ▶ if (UNLIKELY(total_read == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(total_read == 0)) { 0 5 5
3668 0 Safefree(buffer);
3669 0 ST(0) = sv_2mortal(newRV_noinc((SV*)newAV()));
3670 0 XSRETURN(1);
3671 }
3672  
3673 5 lines = newAV();
3674 /* Pre-extend array based on estimated lines */
3675 5 av_extend(lines, total_read / 40);
3676  
3677 /* Single scan through buffer - no memmove, no buffer resize */
3678 5 line_start = buffer;
3679 5 end = buffer + total_read;
3680 5 p = buffer;
3681  
3682 15 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 15 0 15
3683 15 p = memchr(p, '\n', end - p);
3684 15 ▶ if (LIKELY(p != NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (LIKELY(p != NULL)) { 10 5 15
3685 10 av_push(lines, newSVpvn(line_start, p - line_start));
3686 10 p++;
3687 10 line_start = p;
3688 } else {
3689 /* Last line without trailing newline */
3690 5 ▶ if (line_start < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (line_start < end) { 5 0 5
3691 5 av_push(lines, newSVpvn(line_start, end - line_start));
3692 }
3693 5 break;
3694 }
3695 }
3696  
3697 5 Safefree(buffer);
3698  
3699 5 ST(0) = sv_2mortal(newRV_noinc((SV*)lines));
3700 5 XSRETURN(1);
3701 }
3702  
3703 16 XS_INTERNAL(xs_mmap_open) {
3704 16 dXSARGS;
3705 const char *path;
3706 int writable;
3707 IV idx;
3708 HV *hash;
3709  
3710 16 ▶ if (items < 1 || items > 2) croak("Usage: file::mmap_open(path, [writable])");
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || items > 2) croak("Usage: file::mmap_open(path, [writable])"); 16 0 16
if (items < 1 || items > 2) croak("Usage: file::mmap_open(path, [writable])"); 0 16 16
3711  
3712 16 path = SvPV_nolen(ST(0));
3713 16 ▶ writable = (items > 1 && SvTRUE(ST(1))) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
writable = (items > 1 && SvTRUE(ST(1))) ? 1 : 0; 1 15 16
writable = (items > 1 && SvTRUE(ST(1))) ? 1 : 0; 1 0 1
3714  
3715 16 idx = file_mmap_open(aTHX_ path, writable);
3716 16 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 2 14 16
3717 2 ST(0) = &PL_sv_undef;
3718 2 XSRETURN(1);
3719 }
3720  
3721 14 hash = newHV();
3722 14 hv_store(hash, "_idx", 4, newSViv(idx), 0);
3723 14 hv_store(hash, "_writable", 9, newSViv(writable), 0);
3724  
3725 14 ST(0) = sv_2mortal(sv_bless(newRV_noinc((SV*)hash), gv_stashpv("File::Raw::mmap", GV_ADD)));
3726 14 XSRETURN(1);
3727 }
3728  
3729 14 XS_INTERNAL(xs_mmap_data) {
3730 14 dXSARGS;
3731 HV *hash;
3732 SV **idx_sv;
3733 IV idx;
3734  
3735 14 ▶ if (items != 1) croak("Usage: $mmap->data");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $mmap->data"); 0 14 14
3736  
3737 14 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 14 0 14
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 0 14 14
3738 0 croak("Invalid mmap object");
3739 }
3740  
3741 14 hash = (HV*)SvRV(ST(0));
3742 14 idx_sv = hv_fetch(hash, "_idx", 4, 0);
3743 14 ▶ idx = idx_sv ? SvIV(*idx_sv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
idx = idx_sv ? SvIV(*idx_sv) : -1; 14 0 14
3744  
3745 14 ST(0) = sv_2mortal(file_mmap_get_sv(aTHX_ idx));
3746 14 XSRETURN(1);
3747 }
3748  
3749 2 XS_INTERNAL(xs_mmap_sync) {
3750 2 dXSARGS;
3751 HV *hash;
3752 SV **idx_sv;
3753 IV idx;
3754  
3755 2 ▶ if (items != 1) croak("Usage: $mmap->sync");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $mmap->sync"); 0 2 2
3756  
3757 2 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 2 0 2
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 0 2 2
3758 0 croak("Invalid mmap object");
3759 }
3760  
3761 2 hash = (HV*)SvRV(ST(0));
3762 2 idx_sv = hv_fetch(hash, "_idx", 4, 0);
3763 2 ▶ idx = idx_sv ? SvIV(*idx_sv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
idx = idx_sv ? SvIV(*idx_sv) : -1; 2 0 2
3764  
3765 2 file_mmap_sync(idx);
3766 2 XSRETURN_EMPTY;
3767 }
3768  
3769 16 XS_INTERNAL(xs_mmap_close) {
3770 16 dXSARGS;
3771 HV *hash;
3772 SV **idx_sv;
3773 IV idx;
3774  
3775 16 ▶ if (items != 1) croak("Usage: $mmap->close");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $mmap->close"); 0 16 16
3776  
3777 16 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 16 0 16
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 0 16 16
3778 0 croak("Invalid mmap object");
3779 }
3780  
3781 16 hash = (HV*)SvRV(ST(0));
3782 16 idx_sv = hv_fetch(hash, "_idx", 4, 0);
3783 16 ▶ idx = idx_sv ? SvIV(*idx_sv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
idx = idx_sv ? SvIV(*idx_sv) : -1; 16 0 16
3784  
3785 16 file_mmap_close(idx);
3786 16 hv_store(hash, "_idx", 4, newSViv(-1), 0);
3787 16 XSRETURN_EMPTY;
3788 }
3789  
3790 14 XS_INTERNAL(xs_mmap_DESTROY) {
3791 14 dXSARGS;
3792 HV *hash;
3793 SV **idx_sv;
3794 IV idx;
3795  
3796 PERL_UNUSED_VAR(items);
3797  
3798 14 ▶ if (PL_dirty) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) XSRETURN_EMPTY; 0 14 14
3799  
3800 14 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 14 0 14
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) { 0 14 14
3801 0 XSRETURN_EMPTY;
3802 }
3803  
3804 14 hash = (HV*)SvRV(ST(0));
3805 14 idx_sv = hv_fetch(hash, "_idx", 4, 0);
3806 14 ▶ idx = idx_sv ? SvIV(*idx_sv) : -1;
Branch: 1 branch
BranchTrueFalseTotal
idx = idx_sv ? SvIV(*idx_sv) : -1; 14 0 14
3807  
3808 14 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 0 14 14
3809 0 file_mmap_close(idx);
3810 }
3811 14 XSRETURN_EMPTY;
3812 }
3813  
3814 34 XS_INTERNAL(xs_lines_iter) {
3815 34 dXSARGS;
3816 const char *path;
3817 IV idx;
3818 SV *idx_sv;
3819  
3820 34 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 34 34
3821 0 croak("Usage: file::lines_iter(path [, plugin => ..., key => value ...])");
3822  
3823 34 path = SvPV_nolen(ST(0));
3824  
3825 /* Plugin path: slurp + dispatch READ, wrap the resulting AoA in an
3826 * iterator that walks records in order. This is eager (whole AoA
3827 * held in memory) - for true streaming use each_line($p, $cb,
3828 * plugin => ...). The iterator interface itself is preserved so
3829 * code that stores the iterator handle still composes. */
3830 34 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 6 28 34
3831 HV *opts;
3832 SV *bytes;
3833 SV *out;
3834 AV *records;
3835 LineIterEntry *entry;
3836  
3837 6 opts = file_plugin_build_opts(aTHX_ &ST(0), 1, items, "lines_iter");
3838 5 bytes = file_slurp_internal(aTHX_ path);
3839 5 out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
3840 4 SvREFCNT_dec((SV *)opts);
3841 4 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 4 4
3842 0 SvREFCNT_dec(bytes);
3843 0 ST(0) = &PL_sv_undef;
3844 0 XSRETURN(1);
3845 }
3846 4 ▶ if (out != bytes) SvREFCNT_dec(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) SvREFCNT_dec(bytes); 4 0 4
3847 4 ▶ if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) { 3 1 4
if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) { 0 3 3
3848 1 SvREFCNT_dec(out);
3849 1 croak("File::Raw::lines_iter: plugin must return an arrayref of records");
3850 }
3851 3 records = (AV *)SvRV(out);
3852 3 SvREFCNT_inc(records); /* keep the AV alive on its own */
3853 3 SvREFCNT_dec(out); /* drop the RV wrapper */
3854  
3855 3 idx = alloc_iter_slot();
3856 3 entry = &g_iters[idx];
3857 3 entry->fd = -1; /* sentinel: no file behind us */
3858 3 entry->buffer = NULL;
3859 3 entry->buf_size = 0;
3860 3 entry->buf_pos = 0;
3861 3 entry->buf_len = 0;
3862 3 entry->eof = 0;
3863 3 entry->refcount = 1;
3864 3 entry->path = NULL;
3865 3 entry->records = records;
3866 3 entry->records_idx = 0;
3867  
3868 3 idx_sv = newSViv(idx);
3869 3 ST(0) = sv_2mortal(sv_bless(newRV_noinc(idx_sv),
3870 gv_stashpv("File::Raw::lines", GV_ADD)));
3871 3 XSRETURN(1);
3872 }
3873  
3874 28 idx = file_lines_open(aTHX_ path);
3875  
3876 28 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 1 27 28
3877 1 ST(0) = &PL_sv_undef;
3878 1 XSRETURN(1);
3879 }
3880  
3881 /* Use simple IV reference - much faster than hash */
3882 27 idx_sv = newSViv(idx);
3883 27 ST(0) = sv_2mortal(sv_bless(newRV_noinc(idx_sv), gv_stashpv("File::Raw::lines", GV_ADD)));
3884 27 XSRETURN(1);
3885 }
3886  
3887 174 XS_INTERNAL(xs_lines_iter_next) {
3888 174 dXSARGS;
3889 SV *rv;
3890 IV idx;
3891 LineIterEntry *entry;
3892 char *line_start;
3893 char *newline;
3894 size_t line_len;
3895 SV *result;
3896 ssize_t n;
3897  
3898 174 ▶ if (items != 1) croak("Usage: $iter->next");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $iter->next"); 0 174 174
3899  
3900 174 rv = ST(0);
3901 174 ▶ if (UNLIKELY(!SvROK(rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(!SvROK(rv))) { 0 174 174
3902 0 croak("Invalid lines iterator object");
3903 }
3904  
3905 /* Direct IV access - no hash lookup */
3906 174 idx = SvIV(SvRV(rv));
3907  
3908 174 ▶ if (UNLIKELY(idx < 0 || idx >= g_iters_count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(idx < 0 || idx >= g_iters_count)) { 174 0 174
if (UNLIKELY(idx < 0 || idx >= g_iters_count)) { 0 174 174
3909 0 ST(0) = &PL_sv_undef;
3910 0 XSRETURN(1);
3911 }
3912  
3913 174 entry = &g_iters[idx];
3914  
3915 /* Record-iter mode: walk the AoA we collected at lines_iter() time. */
3916 174 ▶ if (entry->records) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->records) { 5 169 174
3917 5 SSize_t total = av_len(entry->records) + 1;
3918 5 ▶ if (entry->records_idx >= total) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->records_idx >= total) { 0 5 5
3919 0 ST(0) = &PL_sv_undef;
3920 0 XSRETURN(1);
3921 }
3922 5 SV **rp = av_fetch(entry->records, entry->records_idx++, 0);
3923 5 ▶ ST(0) = (rp && *rp) ? sv_2mortal(newSVsv(*rp)) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (rp && *rp) ? sv_2mortal(newSVsv(*rp)) : &PL_sv_undef; 5 0 5
ST(0) = (rp && *rp) ? sv_2mortal(newSVsv(*rp)) : &PL_sv_undef; 5 0 5
3924 5 XSRETURN(1);
3925 }
3926  
3927 169 ▶ if (UNLIKELY(entry->fd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(entry->fd < 0)) { 0 169 169
3928 0 ST(0) = &PL_sv_undef;
3929 0 XSRETURN(1);
3930 }
3931  
3932 /* Inline buffer parsing for speed */
3933 while (1) {
3934 /* Look for newline in current buffer */
3935 213 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 177 36 213
3936 177 line_start = entry->buffer + entry->buf_pos;
3937 177 newline = memchr(line_start, '\n', entry->buf_len - entry->buf_pos);
3938  
3939 177 ▶ if (newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (newline) { 153 24 177
3940 153 line_len = newline - line_start;
3941 153 result = newSVpvn(line_start, line_len);
3942 153 entry->buf_pos += line_len + 1;
3943 153 ST(0) = sv_2mortal(result);
3944 153 XSRETURN(1);
3945 }
3946 }
3947  
3948 /* No newline found, need more data */
3949 60 ▶ if (entry->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->eof) { 16 44 60
3950 /* Return remaining data if any */
3951 16 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 11 5 16
3952 11 line_len = entry->buf_len - entry->buf_pos;
3953 11 result = newSVpvn(entry->buffer + entry->buf_pos, line_len);
3954 11 entry->buf_pos = entry->buf_len;
3955 11 ST(0) = sv_2mortal(result);
3956 11 XSRETURN(1);
3957 }
3958 5 ST(0) = &PL_sv_undef;
3959 5 XSRETURN(1);
3960 }
3961  
3962 /* Move remaining data to start of buffer */
3963 44 ▶ if (entry->buf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos > 0) { 14 30 44
3964 14 size_t remaining = entry->buf_len - entry->buf_pos;
3965 14 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 10 4 14
3966 10 memmove(entry->buffer, entry->buffer + entry->buf_pos, remaining);
3967 }
3968 14 entry->buf_len = remaining;
3969 14 entry->buf_pos = 0;
3970 }
3971  
3972 /* Expand buffer if needed */
3973 44 ▶ if (entry->buf_len >= entry->buf_size - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_len >= entry->buf_size - 1) { 1 43 44
3974 1 entry->buf_size *= 2;
3975 1 Renew(entry->buffer, entry->buf_size, char);
3976 }
3977  
3978 /* Read more data */
3979 44 n = read(entry->fd, entry->buffer + entry->buf_len,
3980 44 entry->buf_size - entry->buf_len - 1);
3981 44 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 44 44
3982 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
3983 0 ST(0) = &PL_sv_undef;
3984 0 XSRETURN(1);
3985 }
3986 44 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 16 28 44
3987 16 entry->eof = 1;
3988 } else {
3989 28 entry->buf_len += n;
3990 }
3991 }
3992 }
3993  
3994 137 XS_INTERNAL(xs_lines_iter_eof) {
3995 137 dXSARGS;
3996 SV *rv;
3997 IV idx;
3998 LineIterEntry *entry;
3999  
4000 137 ▶ if (items != 1) croak("Usage: $iter->eof");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $iter->eof"); 0 137 137
4001  
4002 137 rv = ST(0);
4003 137 ▶ if (UNLIKELY(!SvROK(rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(!SvROK(rv))) { 0 137 137
4004 0 croak("Invalid lines iterator object");
4005 }
4006  
4007 /* Direct IV access and inline eof check */
4008 137 idx = SvIV(SvRV(rv));
4009  
4010 137 ▶ if (UNLIKELY(idx < 0 || idx >= g_iters_count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY(idx < 0 || idx >= g_iters_count)) { 137 0 137
if (UNLIKELY(idx < 0 || idx >= g_iters_count)) { 0 137 137
4011 0 ST(0) = &PL_sv_yes;
4012 0 XSRETURN(1);
4013 }
4014  
4015 137 entry = &g_iters[idx];
4016 137 ▶ if (entry->records) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->records) { 4 133 137
4017 4 ST(0) = (entry->records_idx >= (av_len(entry->records) + 1))
4018 4 ▶ ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
? &PL_sv_yes : &PL_sv_no; 1 3 4
4019 4 XSRETURN(1);
4020 }
4021 133 ▶ ST(0) = (entry->eof && entry->buf_pos >= entry->buf_len) ? &PL_sv_yes : &PL_sv_no;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (entry->eof && entry->buf_pos >= entry->buf_len) ? &PL_sv_yes : &PL_sv_no; 14 119 133
ST(0) = (entry->eof && entry->buf_pos >= entry->buf_len) ? &PL_sv_yes : &PL_sv_no; 14 0 14
4022 133 XSRETURN(1);
4023 }
4024  
4025 93 XS_INTERNAL(xs_lines_iter_close) {
4026 93 dXSARGS;
4027 SV *rv, *inner;
4028 IV idx;
4029  
4030 93 ▶ if (items != 1) croak("Usage: $iter->close");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $iter->close"); 0 93 93
4031  
4032 93 rv = ST(0);
4033 93 ▶ if (UNLIKELY(!SvROK(rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(!SvROK(rv))) { 0 93 93
4034 0 croak("Invalid lines iterator object");
4035 }
4036  
4037 93 inner = SvRV(rv);
4038 93 idx = SvIV(inner);
4039  
4040 93 file_lines_close(idx);
4041 93 sv_setiv(inner, -1); /* Mark as closed */
4042 93 XSRETURN_EMPTY;
4043 }
4044  
4045 593 XS_INTERNAL(xs_lines_iter_DESTROY) {
4046 593 dXSARGS;
4047 SV *rv;
4048 IV idx;
4049  
4050 PERL_UNUSED_VAR(items);
4051  
4052 593 ▶ if (PL_dirty) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) XSRETURN_EMPTY; 0 593 593
4053  
4054 593 rv = ST(0);
4055 593 ▶ if (UNLIKELY(!SvROK(rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(!SvROK(rv))) { 0 593 593
4056 0 XSRETURN_EMPTY;
4057 }
4058  
4059 593 idx = SvIV(SvRV(rv));
4060  
4061 593 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 502 91 593
4062 502 file_lines_close(idx);
4063 }
4064 593 XSRETURN_EMPTY;
4065 }
4066  
4067 /* ============================================
4068 Chunk iterator XSUBs
4069  
4070 next is its own function; eof, close and DESTROY are the line
4071 iterator's, which read the same fields and free the same slot.
4072 ============================================ */
4073  
4074 /* The largest chunk we will allocate on a caller's say-so. A size is a
4075 * buffer this module allocates and holds, so an unchecked one is an
4076 * allocation of whatever arrived from a config file. */
4077 #define FILE_CHUNK_MAX_SIZE ((UV)16 * 1024 * 1024)
4078  
4079 576 XS_INTERNAL(xs_chunk_iter) {
4080 576 dXSARGS;
4081 const char *path;
4082 IV idx;
4083 576 UV size = (UV)FILE_BUFFER_SIZE;
4084 SV *idx_sv;
4085 int i;
4086  
4087 576 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 576 576
4088 0 croak("Usage: file::chunk_iter(path [, size => $bytes])");
4089  
4090 576 path = SvPV_nolen(ST(0));
4091  
4092 576 ▶ if (((items - 1) % 2) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (((items - 1) % 2) != 0) 1 575 576
4093 1 croak("File::Raw::chunk_iter: odd number of options (expected key => value pairs)");
4094  
4095 1134 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 570 564 1134
4096 STRLEN klen;
4097 const char *key;
4098 SV *val;
4099  
4100 570 ▶ if (!SvOK(ST(i)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ST(i))) 1 569 570
4101 1 croak("File::Raw::chunk_iter: option key at position %d is undef", i);
4102  
4103 569 key = SvPV(ST(i), klen);
4104 569 val = ST(i + 1);
4105  
4106 569 ▶ if (klen == 4 && strEQ(key, "size")) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 4 && strEQ(key, "size")) { 567 2 569
if (klen == 4 && strEQ(key, "size")) { 567 0 567
4107 IV iv;
4108 567 ▶ if (!SvOK(val) || !looks_like_number(val))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val) || !looks_like_number(val)) 566 1 567
if (!SvOK(val) || !looks_like_number(val)) 2 564 566
4109 3 croak("File::Raw::chunk_iter: 'size' must be a positive integer");
4110 564 iv = SvIV(val);
4111 /* IV to NV is always defined; NV to IV is not, so compare in
4112 * the direction that cannot trap. A value too large for an IV
4113 * fails the equality and lands on the same message. */
4114 564 ▶ if ((NV)iv != SvNV(val) || iv < 1)
Branch: 2 branches
BranchTrueFalseTotal
if ((NV)iv != SvNV(val) || iv < 1) 563 1 564
if ((NV)iv != SvNV(val) || iv < 1) 3 560 563
4115 4 croak("File::Raw::chunk_iter: 'size' must be a positive integer");
4116 560 ▶ if ((UV)iv > FILE_CHUNK_MAX_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)iv > FILE_CHUNK_MAX_SIZE) 1 559 560
4117 1 croak("File::Raw::chunk_iter: 'size' of %" IVdf
4118 " exceeds the %" UVuf " byte maximum", iv,
4119 FILE_CHUNK_MAX_SIZE);
4120 559 size = (UV)iv;
4121 }
4122 2 ▶ else if (klen == 6 && strEQ(key, "plugin")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && strEQ(key, "plugin")) { 2 0 2
else if (klen == 6 && strEQ(key, "plugin")) { 1 1 2
4123 1 croak("File::Raw::chunk_iter: a plugin tail is not accepted; "
4124 "transforming chunks is the stream phase's work, so pass "
4125 "plugin => ... to each_line instead");
4126 }
4127 else {
4128 1 croak("File::Raw::chunk_iter: unknown option '%s'", key);
4129 }
4130 }
4131  
4132 564 idx = file_chunk_open(aTHX_ path, (size_t)size);
4133 564 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 1 563 564
4134 1 ST(0) = &PL_sv_undef; /* errno is the caller's to read */
4135 1 XSRETURN(1);
4136 }
4137  
4138 563 idx_sv = newSViv(idx);
4139 563 ST(0) = sv_2mortal(sv_bless(newRV_noinc(idx_sv),
4140 gv_stashpv("File::Raw::chunks", GV_ADD)));
4141 563 XSRETURN(1);
4142 }
4143  
4144 /* $iter->next - the chunk, or undef at end of file
4145 * $iter->next($buffer) - fills $buffer, returns the byte count, 0 at
4146 * end of file: the shape of core read(), which
4147 * is what it replaces
4148 *
4149 * The two-argument form exists because a fresh SV per chunk is the one
4150 * place this loses to PerlIO: at 4 KiB chunks the allocation dominates
4151 * and reusing the caller's scalar removes it. At 64 KiB and above it
4152 * makes little difference, so the one-argument form stays the readable
4153 * default. See bench/chunks.pl. */
4154 11569 XS_INTERNAL(xs_chunk_iter_next) {
4155 11569 dXSARGS;
4156 SV *rv;
4157 IV idx, n;
4158 const char *buf;
4159  
4160 11569 ▶ if (items < 1 || items > 2) croak("Usage: $iter->next([$buffer])");
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || items > 2) croak("Usage: $iter->next([$buffer])"); 11569 0 11569
if (items < 1 || items > 2) croak("Usage: $iter->next([$buffer])"); 1 11568 11569
4161  
4162 11568 rv = ST(0);
4163 11568 ▶ if (UNLIKELY(!SvROK(rv))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(!SvROK(rv))) { 0 11568 11568
4164 0 croak("Invalid chunk iterator object");
4165 }
4166  
4167 11568 idx = SvIV(SvRV(rv));
4168 11568 ▶ if (UNLIKELY(idx < 0)) { /* closed */
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(idx < 0)) { /* closed */ 2 11566 11568
4169 2 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 1 1 2
4170 1 sv_setpvn(ST(1), "", 0);
4171 1 SvUTF8_off(ST(1));
4172 1 ▶ SvSETMAGIC(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(ST(1)); 0 1 1
4173 1 ST(0) = sv_2mortal(newSViv(0));
4174 }
4175 else {
4176 1 ST(0) = &PL_sv_undef;
4177 }
4178 2 XSRETURN(1);
4179 }
4180  
4181 11566 n = file_chunk_read(aTHX_ idx, &buf);
4182 11566 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 1 11565 11566
4183 1 croak("File::Raw::chunk_iter: read failed: %s", Strerror(errno));
4184  
4185 11565 ▶ if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) { 7 11558 11565
4186 7 SV *dst = ST(1);
4187 7 ▶ sv_setpvn(dst, n ? buf : "", (STRLEN)n);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(dst, n ? buf : "", (STRLEN)n); 5 2 7
4188 7 SvUTF8_off(dst); /* bytes, whatever the scalar held before */
4189 7 ▶ SvSETMAGIC(dst);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(dst); 0 7 7
4190 7 ST(0) = sv_2mortal(newSViv(n));
4191 7 XSRETURN(1);
4192 }
4193  
4194 /* 0 is end of file, and the empty string is never a chunk */
4195 11558 ▶ ST(0) = n ? sv_2mortal(newSVpvn(buf, (STRLEN)n)) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = n ? sv_2mortal(newSVpvn(buf, (STRLEN)n)) : &PL_sv_undef; 11543 15 11558
4196 11558 XSRETURN(1);
4197 }
4198  
4199 /* ============================================
4200 Callback registry for line processing
4201 Allows C-level predicates for maximum speed
4202 ============================================ */
4203  
4204 /* Predicate function type for line processing */
4205 typedef bool (*file_line_predicate)(pTHX_ SV *line);
4206  
4207 /* Registered callback entry */
4208 typedef struct {
4209 file_line_predicate predicate; /* C function pointer (NULL for Perl-only) */
4210 SV *perl_callback; /* Perl callback (for fallback or custom) */
4211 } FileLineCallback;
4212  
4213 /* Global callback registry */
4214 static HV *g_file_callback_registry = NULL;
4215  
4216 /* Built-in C predicates */
4217 59 static bool pred_is_blank(pTHX_ SV *line) {
4218 STRLEN len;
4219 59 const char *s = SvPV(line, len);
4220 STRLEN i;
4221 80 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 64 16 80
4222 64 ▶ if (s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n') {
Branch: 4 branches
BranchTrueFalseTotal
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n') { 43 21 64
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n') { 43 0 43
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n') { 43 0 43
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\r' && s[i] != '\n') { 43 0 43
4223 43 return FALSE;
4224 }
4225 }
4226 16 return TRUE;
4227 }
4228  
4229 41 static bool pred_is_not_blank(pTHX_ SV *line) {
4230 41 return !pred_is_blank(aTHX_ line);
4231 }
4232  
4233 9 static bool pred_is_empty(pTHX_ SV *line) {
4234 9 return SvCUR(line) == 0;
4235 }
4236  
4237 9 static bool pred_is_not_empty(pTHX_ SV *line) {
4238 9 return SvCUR(line) > 0;
4239 }
4240  
4241 29 static bool pred_is_comment(pTHX_ SV *line) {
4242 STRLEN len;
4243 29 const char *s = SvPV(line, len);
4244 /* Skip leading whitespace */
4245 40 ▶ while (len > 0 && (*s == ' ' || *s == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
while (len > 0 && (*s == ' ' || *s == '\t')) { 32 8 40
while (len > 0 && (*s == ' ' || *s == '\t')) { 11 21 32
while (len > 0 && (*s == ' ' || *s == '\t')) { 0 21 21
4246 11 s++;
4247 11 len--;
4248 }
4249 29 ▶ return len > 0 && *s == '#';
Branch: 2 branches
BranchTrueFalseTotal
return len > 0 && *s == '#'; 21 8 29
return len > 0 && *s == '#'; 6 15 21
4250 }
4251  
4252 9 static bool pred_is_not_comment(pTHX_ SV *line) {
4253 9 return !pred_is_comment(aTHX_ line);
4254 }
4255  
4256 /* Cleanup callback registry during global destruction */
4257 29 static void file_cleanup_callback_registry(pTHX_ void *data) {
4258 PERL_UNUSED_ARG(data);
4259  
4260 /* During global destruction, just NULL out pointers.
4261 * Perl handles SV cleanup; trying to free them ourselves
4262 * can cause crashes due to destruction order. */
4263 29 ▶ if (PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_dirty) { 29 0 29
4264 29 g_file_callback_registry = NULL;
4265 29 return;
4266 }
4267  
4268 /* Normal cleanup - not during global destruction */
4269 0 g_file_callback_registry = NULL;
4270 }
4271  
4272 63 static void file_init_callback_registry(pTHX) {
4273 SV *sv;
4274 FileLineCallback *cb;
4275  
4276 63 ▶ if (g_file_callback_registry) return;
Branch: 1 branch
BranchTrueFalseTotal
if (g_file_callback_registry) return; 34 29 63
4277 29 g_file_callback_registry = newHV();
4278  
4279 /* Register built-in predicates with both naming conventions */
4280 /* blank / is_blank */
4281 29 Newxz(cb, 1, FileLineCallback);
4282 29 cb->predicate = pred_is_blank;
4283 29 cb->perl_callback = NULL;
4284 29 sv = newSViv(PTR2IV(cb));
4285 29 hv_store(g_file_callback_registry, "blank", 5, sv, 0);
4286 29 hv_store(g_file_callback_registry, "is_blank", 8, SvREFCNT_inc(sv), 0);
4287  
4288 /* not_blank / is_not_blank */
4289 29 Newxz(cb, 1, FileLineCallback);
4290 29 cb->predicate = pred_is_not_blank;
4291 29 cb->perl_callback = NULL;
4292 29 sv = newSViv(PTR2IV(cb));
4293 29 hv_store(g_file_callback_registry, "not_blank", 9, sv, 0);
4294 29 hv_store(g_file_callback_registry, "is_not_blank", 12, SvREFCNT_inc(sv), 0);
4295  
4296 /* empty / is_empty */
4297 29 Newxz(cb, 1, FileLineCallback);
4298 29 cb->predicate = pred_is_empty;
4299 29 cb->perl_callback = NULL;
4300 29 sv = newSViv(PTR2IV(cb));
4301 29 hv_store(g_file_callback_registry, "empty", 5, sv, 0);
4302 29 hv_store(g_file_callback_registry, "is_empty", 8, SvREFCNT_inc(sv), 0);
4303  
4304 /* not_empty / is_not_empty */
4305 29 Newxz(cb, 1, FileLineCallback);
4306 29 cb->predicate = pred_is_not_empty;
4307 29 cb->perl_callback = NULL;
4308 29 sv = newSViv(PTR2IV(cb));
4309 29 hv_store(g_file_callback_registry, "not_empty", 9, sv, 0);
4310 29 hv_store(g_file_callback_registry, "is_not_empty", 12, SvREFCNT_inc(sv), 0);
4311  
4312 /* comment / is_comment */
4313 29 Newxz(cb, 1, FileLineCallback);
4314 29 cb->predicate = pred_is_comment;
4315 29 cb->perl_callback = NULL;
4316 29 sv = newSViv(PTR2IV(cb));
4317 29 hv_store(g_file_callback_registry, "comment", 7, sv, 0);
4318 29 hv_store(g_file_callback_registry, "is_comment", 10, SvREFCNT_inc(sv), 0);
4319  
4320 /* not_comment / is_not_comment */
4321 29 Newxz(cb, 1, FileLineCallback);
4322 29 cb->predicate = pred_is_not_comment;
4323 29 cb->perl_callback = NULL;
4324 29 sv = newSViv(PTR2IV(cb));
4325 29 hv_store(g_file_callback_registry, "not_comment", 11, sv, 0);
4326 29 hv_store(g_file_callback_registry, "is_not_comment", 14, SvREFCNT_inc(sv), 0);
4327 }
4328  
4329 23 static FileLineCallback* file_get_callback(pTHX_ const char *name) {
4330 SV **svp;
4331 23 ▶ if (!g_file_callback_registry) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g_file_callback_registry) return NULL; 0 23 23
4332 23 svp = hv_fetch(g_file_callback_registry, name, strlen(name), 0);
4333 23 ▶ if (svp && SvIOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvIOK(*svp)) { 19 4 23
if (svp && SvIOK(*svp)) { 19 0 19
4334 19 return INT2PTR(FileLineCallback*, SvIVX(*svp));
4335 }
4336 4 return NULL;
4337 }
4338  
4339 /* Process lines with callback - MULTICALL optimized (Perl >= 5.14 only) */
4340 14 XS_INTERNAL(xs_each_line) {
4341 14 dXSARGS;
4342 #if PERL_VERSION >= 14
4343 dMULTICALL;
4344 #endif
4345 const char *path;
4346 SV *callback;
4347 IV idx;
4348 CV *block_cv;
4349 SV *old_defsv;
4350 SV *line_sv;
4351 LineIterEntry *entry;
4352 char *line_start;
4353 char *newline;
4354 size_t line_len;
4355 ssize_t n;
4356 #if PERL_VERSION >= 14
4357 14 U8 gimme = G_VOID;
4358 #endif
4359  
4360 14 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 1 13 14
4361 1 croak("Usage: file::each_line(path, callback [, plugin => ..., key => value ...])");
4362  
4363 13 path = SvPV_nolen(ST(0));
4364 13 callback = ST(1);
4365  
4366 13 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) { 12 1 13
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) { 0 12 12
4367 1 croak("Second argument must be a code reference");
4368 }
4369  
4370 /* Plugin path: route through streaming dispatch. The plugin's
4371 * stream fn owns the record emission and calls back to `callback`
4372 * per record (typically once for each parsed CSV row, etc.). */
4373 12 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 4 8 12
4374 4 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "each_line");
4375 4 (void)file_plugin_dispatch_stream(aTHX_ opts, path, callback);
4376 0 SvREFCNT_dec((SV *)opts);
4377 0 XSRETURN_EMPTY;
4378 }
4379  
4380 8 block_cv = (CV*)SvRV(callback);
4381 8 idx = file_lines_open(aTHX_ path);
4382 8 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 1 7 8
4383 1 XSRETURN_EMPTY;
4384 }
4385  
4386 7 entry = &g_iters[idx];
4387  
4388 7 ▶ old_defsv = DEFSV;
Branch: 1 branch
BranchTrueFalseTotal
old_defsv = DEFSV; 7 0 7
4389 7 line_sv = newSV(256);
4390 7 ▶ DEFSV = line_sv;
Branch: 1 branch
BranchTrueFalseTotal
DEFSV = line_sv; 7 0 7
4391  
4392 #if PERL_VERSION >= 14
4393 7 ▶ PUSH_MULTICALL(block_cv);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_MULTICALL(block_cv); 0 7 7
4394 #endif
4395  
4396 while (1) {
4397 /* Look for newline in current buffer */
4398 1039 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 1027 12 1039
4399 1027 line_start = entry->buffer + entry->buf_pos;
4400 1027 newline = memchr(line_start, '\n', entry->buf_len - entry->buf_pos);
4401  
4402 1027 ▶ if (newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (newline) { 1021 6 1027
4403 1021 line_len = newline - line_start;
4404 1021 sv_setpvn(line_sv, line_start, line_len);
4405 1021 entry->buf_pos += line_len + 1;
4406 #if PERL_VERSION >= 14
4407 1021 MULTICALL;
4408 #else
4409 { dSP; PUSHMARK(SP); call_sv((SV*)block_cv, G_VOID|G_DISCARD); }
4410 #endif
4411 1020 continue;
4412 }
4413 }
4414  
4415 /* No newline found, need more data */
4416 18 ▶ if (entry->eof) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->eof) { 6 12 18
4417 /* Return remaining data if any */
4418 6 ▶ if (entry->buf_pos < entry->buf_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos < entry->buf_len) { 3 3 6
4419 3 line_len = entry->buf_len - entry->buf_pos;
4420 3 sv_setpvn(line_sv, entry->buffer + entry->buf_pos, line_len);
4421 3 entry->buf_pos = entry->buf_len;
4422 #if PERL_VERSION >= 14
4423 3 MULTICALL;
4424 #else
4425 { dSP; PUSHMARK(SP); call_sv((SV*)block_cv, G_VOID|G_DISCARD); }
4426 #endif
4427 }
4428 6 break;
4429 }
4430  
4431 /* Move remaining data to start of buffer */
4432 12 ▶ if (entry->buf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_pos > 0) { 5 7 12
4433 5 size_t remaining = entry->buf_len - entry->buf_pos;
4434 5 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 3 2 5
4435 3 memmove(entry->buffer, entry->buffer + entry->buf_pos, remaining);
4436 }
4437 5 entry->buf_len = remaining;
4438 5 entry->buf_pos = 0;
4439 }
4440  
4441 /* Expand buffer if needed */
4442 12 ▶ if (entry->buf_len >= entry->buf_size - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry->buf_len >= entry->buf_size - 1) { 0 12 12
4443 0 entry->buf_size *= 2;
4444 0 Renew(entry->buffer, entry->buf_size, char);
4445 }
4446  
4447 /* Read more data */
4448 12 n = read(entry->fd, entry->buffer + entry->buf_len,
4449 12 entry->buf_size - entry->buf_len - 1);
4450 12 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 12 12
4451 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
4452 0 break;
4453 }
4454 12 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 6 6 12
4455 6 entry->eof = 1;
4456 } else {
4457 6 entry->buf_len += n;
4458 }
4459 }
4460  
4461 #if PERL_VERSION >= 14
4462 6 ▶ POP_MULTICALL;
Branch: 1 branch
BranchTrueFalseTotal
POP_MULTICALL; 6 0 6
4463 #endif
4464 6 SvREFCNT_dec(line_sv);
4465 6 ▶ DEFSV = old_defsv;
Branch: 1 branch
BranchTrueFalseTotal
DEFSV = old_defsv; 6 0 6
4466 6 file_lines_close(idx);
4467 6 XSRETURN_EMPTY;
4468 }
4469  
4470 /* Run plugin READ, expect arrayref result. Returns the underlying AV*
4471 * (whose RV is stored in *out_holder for the caller to refcount-dec).
4472 * Returns NULL if the plugin cancelled. Croaks if the plugin returned
4473 * a non-arrayref (predicate-style XSUBs need an iterable). */
4474 14 static AV *file_records_via_plugin(pTHX_ HV *opts, const char *path,
4475 SV **out_holder)
4476 {
4477 14 SV *bytes = file_slurp_internal(aTHX_ path);
4478 14 SV *out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
4479 13 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 13 13
4480 0 SvREFCNT_dec(bytes);
4481 0 return NULL;
4482 }
4483 13 ▶ if (out != bytes) SvREFCNT_dec(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) SvREFCNT_dec(bytes); 13 0 13
4484 13 ▶ if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) { 12 1 13
if (!SvROK(out) || SvTYPE(SvRV(out)) != SVt_PVAV) { 0 12 12
4485 1 SvREFCNT_dec(out);
4486 1 croak("File::Raw: plugin must return an arrayref of records "
4487 "for predicate-style operations");
4488 }
4489 12 *out_holder = out;
4490 12 return (AV *)SvRV(out);
4491 }
4492  
4493 /* Apply a coderef predicate to one record. Returns 1 if matched, 0 otherwise.
4494 * Used by the plugin path of grep/count/find. */
4495 21 static int file_call_predicate_cv(pTHX_ CV *cv, SV *record) {
4496 21 int matches = 0;
4497 int n;
4498 SV *r;
4499 21 dSP;
4500 21 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 21 21
4501 21 ▶ XPUSHs(record);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(record); 0 21 21
4502 21 PUTBACK;
4503 21 n = call_sv((SV *)cv, G_SCALAR);
4504 21 SPAGAIN;
4505 21 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 21 0 21
4506 21 r = POPs;
4507 21 matches = SvTRUE(r) ? 1 : 0;
4508 }
4509 21 PUTBACK;
4510 21 return matches;
4511 }
4512  
4513 /* Grep lines with callback or registered predicate name */
4514 25 XS_INTERNAL(xs_grep_lines) {
4515 25 dXSARGS;
4516 const char *path;
4517 SV *predicate;
4518 IV idx;
4519 SV *line;
4520 AV *result;
4521 25 CV *block_cv = NULL;
4522 25 FileLineCallback *fcb = NULL;
4523  
4524 25 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 25 25
4525 0 croak("Usage: file::grep_lines(path, &predicate or $name [, plugin => ..., key => value ...])");
4526  
4527 25 path = SvPV_nolen(ST(0));
4528 25 predicate = ST(1);
4529  
4530 /* Plugin path: records come from plugin READ; predicate must be a coderef. */
4531 25 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 4 21 25
4532 HV *opts;
4533 4 SV *holder = NULL;
4534 AV *records;
4535 SSize_t i, n;
4536 AV *matched;
4537  
4538 4 ▶ if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV) 3 1 4
if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV) 0 3 3
4539 1 croak("File::Raw::grep_lines: predicate must be a coderef when "
4540 "a plugin is in use (predicate-name sugar is legacy 2-arg only)");
4541  
4542 3 opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "grep_lines");
4543 3 records = file_records_via_plugin(aTHX_ opts, path, &holder);
4544 2 SvREFCNT_dec((SV *)opts);
4545 2 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 2 2
4546 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
4547 0 XSRETURN(1);
4548 }
4549  
4550 2 matched = newAV();
4551 2 n = av_len(records) + 1;
4552 10 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 2 10
4553 8 SV **rp = av_fetch(records, i, 0);
4554 8 ▶ SV *rec = (rp && *rp) ? *rp : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 8 0 8
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 8 0 8
4555 8 ▶ if (file_call_predicate_cv(aTHX_ (CV *)SvRV(predicate), rec))
Branch: 1 branch
BranchTrueFalseTotal
if (file_call_predicate_cv(aTHX_ (CV *)SvRV(predicate), rec)) 7 1 8
4556 7 av_push(matched, SvREFCNT_inc(rec));
4557 }
4558 2 SvREFCNT_dec(holder);
4559 2 ST(0) = sv_2mortal(newRV_noinc((SV *)matched));
4560 2 XSRETURN(1);
4561 }
4562 21 result = newAV();
4563  
4564 /* Check if predicate is a name or coderef */
4565 21 ▶ if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 5 16 21
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 5 0 5
4566 5 block_cv = (CV*)SvRV(predicate);
4567 } else {
4568 16 const char *name = SvPV_nolen(predicate);
4569 16 fcb = file_get_callback(aTHX_ name);
4570 16 ▶ if (!fcb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fcb) { 1 15 16
4571 1 croak("File::Raw::grep_lines: unknown predicate '%s'", name);
4572 }
4573 }
4574  
4575 20 idx = file_lines_open(aTHX_ path);
4576 20 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 0 20 20
4577 0 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
4578 0 XSRETURN(1);
4579 }
4580  
4581 /* C predicate path - fastest */
4582 20 ▶ if (fcb && fcb->predicate) {
Branch: 2 branches
BranchTrueFalseTotal
if (fcb && fcb->predicate) { 15 5 20
if (fcb && fcb->predicate) { 12 3 15
4583 103 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 91 12 103
4584 91 ▶ if (fcb->predicate(aTHX_ line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcb->predicate(aTHX_ line)) { 49 42 91
4585 49 av_push(result, line);
4586 } else {
4587 42 SvREFCNT_dec(line);
4588 }
4589 }
4590 12 file_lines_close(idx);
4591 12 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
4592 12 XSRETURN(1);
4593 }
4594  
4595 /* Call Perl callback */
4596 {
4597 8 ▶ SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv; 3 5 8
4598 1065 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 1057 8 1065
4599 1057 dSP;
4600 IV count;
4601 SV *result_sv;
4602 1057 bool matches = FALSE;
4603 1057 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1057 1057
4604 1057 ▶ XPUSHs(line);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(line); 0 1057 1057
4605 1057 PUTBACK;
4606 1057 count = call_sv(cb_sv, G_SCALAR);
4607 1057 SPAGAIN;
4608 1057 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 1057 0 1057
4609 1057 result_sv = POPs;
4610 1057 matches = SvTRUE(result_sv);
4611 }
4612 1057 PUTBACK;
4613 1057 ▶ if (matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (matches) { 26 1031 1057
4614 26 av_push(result, line);
4615 } else {
4616 1031 SvREFCNT_dec(line);
4617 }
4618 }
4619 }
4620  
4621 8 file_lines_close(idx);
4622 8 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
4623 8 XSRETURN(1);
4624 }
4625  
4626 /* Count lines matching predicate */
4627 9 XS_INTERNAL(xs_count_lines) {
4628 9 dXSARGS;
4629 const char *path;
4630 9 SV *predicate = NULL;
4631 IV idx;
4632 SV *line;
4633 9 IV count = 0;
4634 9 CV *block_cv = NULL;
4635 9 FileLineCallback *fcb = NULL;
4636  
4637 9 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 9 9
4638 0 croak("Usage: file::count_lines(path [, &predicate or $name] [, plugin => ..., key => value ...])");
4639  
4640 9 path = SvPV_nolen(ST(0));
4641  
4642 /* Plugin path: if items > 2 we definitely have a plugin tail. Otherwise
4643 * if items == 2 and ST(1) looks like the start of options (a string key
4644 * with a value missing - i.e. items == 2 but ST(1) is a known options
4645 * key like "plugin"), users use the explicit form by passing a coderef
4646 * or undef as the predicate slot first. Keep it strict: plugin tail
4647 * begins at position 2, so items must be >= 3 (predicate may be undef). */
4648 9 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 2 7 9
4649 HV *opts;
4650 2 SV *holder = NULL;
4651 AV *records;
4652 SSize_t i, n;
4653 2 IV matched = 0;
4654 2 int has_pred = SvOK(ST(1));
4655  
4656 2 ▶ if (has_pred && (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (has_pred && (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV)) 1 1 2
if (has_pred && (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV)) 1 0 1
if (has_pred && (!SvROK(ST(1)) || SvTYPE(SvRV(ST(1))) != SVt_PVCV)) 0 1 1
4657 0 croak("File::Raw::count_lines: predicate must be a coderef or undef "
4658 "when a plugin is in use");
4659  
4660 2 opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "count_lines");
4661 2 records = file_records_via_plugin(aTHX_ opts, path, &holder);
4662 2 SvREFCNT_dec((SV *)opts);
4663 2 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 2 2
4664 0 ST(0) = sv_2mortal(newSViv(0));
4665 0 XSRETURN(1);
4666 }
4667 2 n = av_len(records) + 1;
4668 2 ▶ if (!has_pred) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_pred) { 1 1 2
4669 1 matched = n;
4670 } else {
4671 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 1 6
4672 5 SV **rp = av_fetch(records, i, 0);
4673 5 ▶ SV *rec = (rp && *rp) ? *rp : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 5 0 5
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 5 0 5
4674 5 ▶ if (file_call_predicate_cv(aTHX_ (CV *)SvRV(ST(1)), rec))
Branch: 1 branch
BranchTrueFalseTotal
if (file_call_predicate_cv(aTHX_ (CV *)SvRV(ST(1)), rec)) 4 1 5
4675 4 matched++;
4676 }
4677 }
4678 2 SvREFCNT_dec(holder);
4679 2 ST(0) = sv_2mortal(newSViv(matched));
4680 2 XSRETURN(1);
4681 }
4682  
4683 /* If no predicate, just count newlines - no SV creation needed */
4684 7 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 4 3 7
4685 int fd;
4686 char *buffer;
4687 4 ssize_t n, total_read = 0;
4688 char *p, *end;
4689 4 char last_char = '\n'; /* Assume last char is newline (handles empty file) */
4690 #ifdef _WIN32
4691 int open_flags = O_RDONLY | O_BINARY;
4692 #else
4693 4 int open_flags = O_RDONLY;
4694 #endif
4695 4 fd = open(path, open_flags);
4696 4 ▶ if (UNLIKELY(fd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(fd < 0)) { 1 3 4
4697 1 ST(0) = sv_2mortal(newSViv(0));
4698 1 XSRETURN(1);
4699 }
4700  
4701 3 Newx(buffer, FILE_BUFFER_SIZE, char);
4702 3 count = 0;
4703  
4704 5 ▶ while ((n = read(fd, buffer, FILE_BUFFER_SIZE)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((n = read(fd, buffer, FILE_BUFFER_SIZE)) > 0) { 2 3 5
4705 2 p = buffer;
4706 2 end = buffer + n;
4707 1010 ▶ while ((p = memchr(p, '\n', end - p)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((p = memchr(p, '\n', end - p)) != NULL) { 1008 2 1010
4708 1008 count++;
4709 1008 p++;
4710 }
4711 2 total_read += n;
4712 2 last_char = buffer[n - 1];
4713 }
4714 3 close(fd);
4715 3 Safefree(buffer);
4716  
4717 /* If file doesn't end with newline, count the last line */
4718 3 ▶ if (total_read > 0 && last_char != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (total_read > 0 && last_char != '\n') { 2 1 3
if (total_read > 0 && last_char != '\n') { 1 1 2
4719 1 count++;
4720 }
4721  
4722 3 ST(0) = sv_2mortal(newSViv(count));
4723 3 XSRETURN(1);
4724 }
4725  
4726 3 predicate = ST(1);
4727  
4728 /* Check if predicate is a name or coderef */
4729 3 ▶ if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 2 1 3
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 2 0 2
4730 2 block_cv = (CV*)SvRV(predicate);
4731 } else {
4732 1 const char *name = SvPV_nolen(predicate);
4733 1 fcb = file_get_callback(aTHX_ name);
4734 1 ▶ if (!fcb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fcb) { 0 1 1
4735 0 croak("File::Raw::count_lines: unknown predicate '%s'", name);
4736 }
4737 }
4738  
4739 3 idx = file_lines_open(aTHX_ path);
4740 3 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 0 3 3
4741 0 ST(0) = sv_2mortal(newSViv(0));
4742 0 XSRETURN(1);
4743 }
4744  
4745 /* C predicate path - fastest */
4746 3 ▶ if (fcb && fcb->predicate) {
Branch: 2 branches
BranchTrueFalseTotal
if (fcb && fcb->predicate) { 1 2 3
if (fcb && fcb->predicate) { 1 0 1
4747 10 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 9 1 10
4748 9 ▶ if (fcb->predicate(aTHX_ line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcb->predicate(aTHX_ line)) { 7 2 9
4749 7 count++;
4750 }
4751 9 SvREFCNT_dec(line);
4752 }
4753 1 file_lines_close(idx);
4754 1 ST(0) = sv_2mortal(newSViv(count));
4755 1 XSRETURN(1);
4756 }
4757  
4758 /* Call Perl callback */
4759 {
4760 2 ▶ SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv; 0 2 2
4761 15 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 13 2 15
4762 13 dSP;
4763 IV n;
4764 SV *result_sv;
4765 13 bool matches = FALSE;
4766 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
4767 13 ▶ XPUSHs(line);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(line); 0 13 13
4768 13 PUTBACK;
4769 13 n = call_sv(cb_sv, G_SCALAR);
4770 13 SPAGAIN;
4771 13 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 13 0 13
4772 13 result_sv = POPs;
4773 13 matches = SvTRUE(result_sv);
4774 }
4775 13 PUTBACK;
4776 13 ▶ if (matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (matches) { 10 3 13
4777 10 count++;
4778 }
4779 13 SvREFCNT_dec(line);
4780 }
4781 }
4782  
4783 2 file_lines_close(idx);
4784 2 ST(0) = sv_2mortal(newSViv(count));
4785 2 XSRETURN(1);
4786 }
4787  
4788 /* Find first line matching predicate */
4789 8 XS_INTERNAL(xs_find_line) {
4790 8 dXSARGS;
4791 const char *path;
4792 SV *predicate;
4793 IV idx;
4794 SV *line;
4795 8 CV *block_cv = NULL;
4796 8 FileLineCallback *fcb = NULL;
4797  
4798 8 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 8 8
4799 0 croak("Usage: file::find_line(path, &predicate or $name [, plugin => ..., key => value ...])");
4800  
4801 8 path = SvPV_nolen(ST(0));
4802 8 predicate = ST(1);
4803  
4804 8 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 2 6 8
4805 HV *opts;
4806 2 SV *holder = NULL;
4807 AV *records;
4808 SSize_t i, n;
4809  
4810 2 ▶ if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV) 2 0 2
if (!SvROK(predicate) || SvTYPE(SvRV(predicate)) != SVt_PVCV) 0 2 2
4811 0 croak("File::Raw::find_line: predicate must be a coderef when "
4812 "a plugin is in use");
4813  
4814 2 opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "find_line");
4815 2 records = file_records_via_plugin(aTHX_ opts, path, &holder);
4816 2 SvREFCNT_dec((SV *)opts);
4817 2 ▶ if (!records) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!records) XSRETURN_UNDEF; 0 2 2
4818  
4819 2 n = av_len(records) + 1;
4820 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 1 9
4821 8 SV **rp = av_fetch(records, i, 0);
4822 8 ▶ SV *rec = (rp && *rp) ? *rp : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 8 0 8
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 8 0 8
4823 8 ▶ if (file_call_predicate_cv(aTHX_ (CV *)SvRV(predicate), rec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_call_predicate_cv(aTHX_ (CV *)SvRV(predicate), rec)) { 1 7 8
4824 1 SV *winner = newSVsv(rec);
4825 1 SvREFCNT_dec(holder);
4826 1 ST(0) = sv_2mortal(winner);
4827 1 XSRETURN(1);
4828 }
4829 }
4830 1 SvREFCNT_dec(holder);
4831 1 XSRETURN_UNDEF;
4832 }
4833  
4834 /* Check if predicate is a name or coderef */
4835 6 ▶ if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 5 1 6
if (SvROK(predicate) && SvTYPE(SvRV(predicate)) == SVt_PVCV) { 5 0 5
4836 5 block_cv = (CV*)SvRV(predicate);
4837 } else {
4838 1 const char *name = SvPV_nolen(predicate);
4839 1 fcb = file_get_callback(aTHX_ name);
4840 1 ▶ if (!fcb) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fcb) { 0 1 1
4841 0 croak("File::Raw::find_line: unknown predicate '%s'", name);
4842 }
4843 }
4844  
4845 6 idx = file_lines_open(aTHX_ path);
4846 6 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 0 6 6
4847 0 XSRETURN_UNDEF;
4848 }
4849  
4850 /* C predicate path - fastest */
4851 6 ▶ if (fcb && fcb->predicate) {
Branch: 2 branches
BranchTrueFalseTotal
if (fcb && fcb->predicate) { 1 5 6
if (fcb && fcb->predicate) { 1 0 1
4852 6 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 6 0 6
4853 6 ▶ if (fcb->predicate(aTHX_ line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcb->predicate(aTHX_ line)) { 1 5 6
4854 1 file_lines_close(idx);
4855 1 ST(0) = sv_2mortal(line);
4856 1 XSRETURN(1);
4857 }
4858 5 SvREFCNT_dec(line);
4859 }
4860 0 file_lines_close(idx);
4861 0 XSRETURN_UNDEF;
4862 }
4863  
4864 /* Call Perl callback */
4865 {
4866 5 ▶ SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv;
Branch: 1 branch
BranchTrueFalseTotal
SV *cb_sv = fcb ? fcb->perl_callback : (SV*)block_cv; 0 5 5
4867 26 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 25 1 26
4868 25 dSP;
4869 IV n;
4870 SV *result_sv;
4871 25 bool matches = FALSE;
4872 25 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 25 25
4873 25 ▶ XPUSHs(line);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(line); 0 25 25
4874 25 PUTBACK;
4875 25 n = call_sv(cb_sv, G_SCALAR);
4876 25 SPAGAIN;
4877 25 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 25 0 25
4878 25 result_sv = POPs;
4879 25 matches = SvTRUE(result_sv);
4880 }
4881 25 PUTBACK;
4882 25 ▶ if (matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (matches) { 4 21 25
4883 4 file_lines_close(idx);
4884 4 ST(0) = sv_2mortal(line);
4885 4 XSRETURN(1);
4886 }
4887 21 SvREFCNT_dec(line);
4888 }
4889 }
4890  
4891 1 file_lines_close(idx);
4892 1 XSRETURN_UNDEF;
4893 }
4894  
4895 /* Map lines with callback */
4896 5 XS_INTERNAL(xs_map_lines) {
4897 5 dXSARGS;
4898 const char *path;
4899 SV *callback;
4900 IV idx;
4901 SV *line;
4902 AV *result;
4903 5 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 5 5
4904 0 croak("Usage: file::map_lines(path, &callback [, plugin => ..., key => value ...])");
4905  
4906 5 path = SvPV_nolen(ST(0));
4907 5 callback = ST(1);
4908 5 result = newAV();
4909  
4910 5 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) { 5 0 5
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) { 0 5 5
4911 0 croak("Second argument must be a code reference");
4912 }
4913  
4914 5 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 1 4 5
4915 HV *opts;
4916 1 SV *holder = NULL;
4917 AV *records;
4918 SSize_t i, n;
4919 AV *out;
4920  
4921 1 SvREFCNT_dec((SV *)result);
4922 1 opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "map_lines");
4923 1 records = file_records_via_plugin(aTHX_ opts, path, &holder);
4924 1 SvREFCNT_dec((SV *)opts);
4925 1 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 1 1
4926 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
4927 0 XSRETURN(1);
4928 }
4929 1 out = newAV();
4930 1 n = av_len(records) + 1;
4931 1 av_extend(out, n);
4932 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 1 6
4933 5 SV **rp = av_fetch(records, i, 0);
4934 5 ▶ SV *rec = (rp && *rp) ? *rp : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 5 0 5
SV *rec = (rp && *rp) ? *rp : &PL_sv_undef; 5 0 5
4935 int rn;
4936 SV *rv;
4937 5 dSP;
4938 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
4939 5 ▶ XPUSHs(rec);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(rec); 0 5 5
4940 5 PUTBACK;
4941 5 rn = call_sv(callback, G_SCALAR);
4942 5 SPAGAIN;
4943 5 ▶ if (rn > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rn > 0) { 5 0 5
4944 5 rv = POPs;
4945 5 av_push(out, SvREFCNT_inc(rv));
4946 }
4947 5 PUTBACK;
4948 }
4949 1 SvREFCNT_dec(holder);
4950 1 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
4951 1 XSRETURN(1);
4952 }
4953  
4954 4 idx = file_lines_open(aTHX_ path);
4955 4 ▶ if (idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) { 0 4 4
4956 0 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
4957 0 XSRETURN(1);
4958 }
4959  
4960 /* Call Perl callback */
4961 {
4962 31 ▶ while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
while ((line = file_lines_next(aTHX_ idx)) != &PL_sv_undef) { 27 4 31
4963 27 dSP;
4964 IV count;
4965 SV *result_sv;
4966 27 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 27 27
4967 27 ▶ XPUSHs(sv_2mortal(line));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(line)); 0 27 27
4968 27 PUTBACK;
4969 27 count = call_sv(callback, G_SCALAR);
4970 27 SPAGAIN;
4971 27 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 27 0 27
4972 27 result_sv = POPs;
4973 27 av_push(result, SvREFCNT_inc(result_sv));
4974 }
4975 27 PUTBACK;
4976 }
4977 }
4978  
4979 4 file_lines_close(idx);
4980 4 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
4981 4 XSRETURN(1);
4982 }
4983  
4984 /* ============================================
4985 Perl bridge for the plugin API.
4986  
4987 Perl plugins are registered as a hashref of phase coderefs:
4988  
4989 File::Raw::register_plugin('csv', {
4990 read => sub { my ($path, $bytes, $opts) = @_; ... },
4991 write => sub { my ($path, $rows, $opts) = @_; ... },
4992 record => sub { my ($path, $record, $opts) = @_; ... },
4993 });
4994  
4995 The bridge allocates a PerlPluginBridge holding the coderef SVs plus
4996 a FilePlugin block whose function pointers are static C thunks. The
4997 thunks recover the bridge from FilePluginContext::plugin_state and
4998 call the appropriate coderef. The bridge is pinned in
4999 g_perl_plugins so we can free it on unregister.
5000  
5001 The 'stream' phase is intentionally not supported from Perl: a Perl
5002 stream plugin would be invoked once per chunk by file.c's read loop,
5003 and the per-call call_sv overhead defeats the point of streaming.
5004 Perl plugins that need record-by-record callbacks should implement
5005 the 'record' phase instead - File::Raw drives the iteration.
5006 ============================================ */
5007  
5008 typedef struct PerlPluginBridge {
5009 char *name; /* strdup'd; pointer is stored in plugin.name */
5010 SV *read_cv;
5011 SV *write_cv;
5012 SV *record_cv;
5013 FilePlugin plugin;
5014 } PerlPluginBridge;
5015  
5016 static HV *g_perl_plugins = NULL;
5017  
5018 14 static void perl_plugin_bridge_free(pTHX_ PerlPluginBridge *b) {
5019 14 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 14 14
5020 14 ▶ if (b->read_cv) SvREFCNT_dec(b->read_cv);
Branch: 1 branch
BranchTrueFalseTotal
if (b->read_cv) SvREFCNT_dec(b->read_cv); 11 3 14
5021 14 ▶ if (b->write_cv) SvREFCNT_dec(b->write_cv);
Branch: 1 branch
BranchTrueFalseTotal
if (b->write_cv) SvREFCNT_dec(b->write_cv); 1 13 14
5022 14 ▶ if (b->record_cv) SvREFCNT_dec(b->record_cv);
Branch: 1 branch
BranchTrueFalseTotal
if (b->record_cv) SvREFCNT_dec(b->record_cv); 1 13 14
5023 14 ▶ if (b->name) Safefree(b->name);
Branch: 1 branch
BranchTrueFalseTotal
if (b->name) Safefree(b->name); 14 0 14
5024 14 Safefree(b);
5025 }
5026  
5027 43 static SV *perl_plugin_thunk_read(pTHX_ FilePluginContext *ctx) {
5028 43 PerlPluginBridge *b = (PerlPluginBridge *)ctx->plugin_state;
5029 SV *result;
5030 int count;
5031 43 dSP;
5032  
5033 43 ENTER;
5034 43 SAVETMPS;
5035 43 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 43 43
5036 43 ▶ XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 0 43 43
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 43 0 43
5037 43 ▶ XPUSHs(sv_2mortal(newSVsv(ctx->data)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(ctx->data))); 0 43 43
5038 43 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options))); 0 43 43
5039 43 PUTBACK;
5040  
5041 43 count = call_sv(b->read_cv, G_SCALAR | G_EVAL);
5042  
5043 43 SPAGAIN;
5044 43 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 43 0 43
if (SvTRUE(ERRSV)) { 1 42 43
5045 1 ▶ SV *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVsv(ERRSV); 1 0 1
5046 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
5047 1 LEAVE;
5048 1 croak_sv(err);
5049 }
5050 42 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 42 0 42
5051 42 SV *ret = POPs;
5052 42 ▶ if (SvOK(ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ret)) { 40 2 42
5053 40 result = newSVsv(ret);
5054 } else {
5055 2 ctx->cancel = 1;
5056 2 result = NULL;
5057 }
5058 } else {
5059 0 ctx->cancel = 1;
5060 0 result = NULL;
5061 }
5062 42 PUTBACK;
5063 42 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 42 0 42
5064 42 LEAVE;
5065 42 return result;
5066 }
5067  
5068 15 static SV *perl_plugin_thunk_write(pTHX_ FilePluginContext *ctx) {
5069 15 PerlPluginBridge *b = (PerlPluginBridge *)ctx->plugin_state;
5070 SV *result;
5071 int count;
5072 15 dSP;
5073  
5074 15 ENTER;
5075 15 SAVETMPS;
5076 15 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15 15
5077 15 ▶ XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 0 15 15
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 15 0 15
5078 15 ▶ XPUSHs(sv_2mortal(newSVsv(ctx->data)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(ctx->data))); 0 15 15
5079 15 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options))); 0 15 15
5080 15 PUTBACK;
5081  
5082 15 count = call_sv(b->write_cv, G_SCALAR | G_EVAL);
5083  
5084 15 SPAGAIN;
5085 15 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 15 0 15
if (SvTRUE(ERRSV)) { 0 15 15
5086 0 ▶ SV *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVsv(ERRSV); 0 0 0
5087 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
5088 0 LEAVE;
5089 0 croak_sv(err);
5090 }
5091 15 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 15 0 15
5092 15 SV *ret = POPs;
5093 15 ▶ if (SvOK(ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ret)) { 13 2 15
5094 13 result = newSVsv(ret);
5095 } else {
5096 2 ctx->cancel = 1;
5097 2 result = NULL;
5098 }
5099 } else {
5100 0 ctx->cancel = 1;
5101 0 result = NULL;
5102 }
5103 15 PUTBACK;
5104 15 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 15 0 15
5105 15 LEAVE;
5106 15 return result;
5107 }
5108  
5109 0 static SV *perl_plugin_thunk_record(pTHX_ FilePluginContext *ctx, SV *record) {
5110 0 PerlPluginBridge *b = (PerlPluginBridge *)ctx->plugin_state;
5111 SV *result;
5112 int count;
5113 0 dSP;
5114  
5115 0 ENTER;
5116 0 SAVETMPS;
5117 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
5118 0 ▶ XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 0 0 0
XPUSHs(sv_2mortal(newSVpv(ctx->path ? ctx->path : "", 0))); 0 0 0
5119 0 ▶ XPUSHs(sv_2mortal(newSVsv(record)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(record))); 0 0 0
5120 0 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)ctx->options))); 0 0 0
5121 0 PUTBACK;
5122  
5123 0 count = call_sv(b->record_cv, G_SCALAR | G_EVAL);
5124  
5125 0 SPAGAIN;
5126 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
5127 0 ▶ SV *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVsv(ERRSV); 0 0 0
5128 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
5129 0 LEAVE;
5130 0 croak_sv(err);
5131 }
5132 0 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 0 0 0
5133 0 SV *ret = POPs;
5134 0 ▶ if (SvOK(ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ret)) { 0 0 0
5135 0 result = newSVsv(ret);
5136 } else {
5137 0 result = &PL_sv_undef; /* exclude */
5138 }
5139 } else {
5140 0 result = &PL_sv_undef;
5141 }
5142 0 PUTBACK;
5143 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
5144 0 LEAVE;
5145 0 return result;
5146 }
5147  
5148 /* ============================================
5149 Built-in 'predicate' plugin.
5150  
5151 Records flow through plugin->record_fn(), which looks up a predicate by
5152 name in g_file_callback_registry. The record fn returns the record SV
5153 when the predicate matches, or &PL_sv_undef when it does not -
5154 callers (grep/count/find) interpret this via truthiness.
5155  
5156 The 'name' option is required:
5157  
5158 File::Raw::grep_lines($p, plugin => 'predicate', name => 'is_blank');
5159  
5160 Predicates registered as Perl coderefs are invoked with the record in
5161 $_; the same convention as grep_lines's per-record callbacks.
5162 ============================================ */
5163  
5164 0 static SV *predicate_plugin_record(pTHX_ FilePluginContext *ctx, SV *record) {
5165 SV **svp;
5166 const char *pred_name;
5167 STRLEN pred_name_len;
5168 FileLineCallback *cb;
5169  
5170 0 svp = hv_fetchs(ctx->options, "name", 0);
5171 0 ▶ if (!svp || !*svp || !SvOK(*svp))
Branch: 3 branches
BranchTrueFalseTotal
if (!svp || !*svp || !SvOK(*svp)) 0 0 0
if (!svp || !*svp || !SvOK(*svp)) 0 0 0
if (!svp || !*svp || !SvOK(*svp)) 0 0 0
5172 0 croak("File::Raw plugin 'predicate': missing 'name' option");
5173 0 pred_name = SvPV(*svp, pred_name_len);
5174  
5175 0 ▶ if (!g_file_callback_registry)
Branch: 1 branch
BranchTrueFalseTotal
if (!g_file_callback_registry) 0 0 0
5176 0 file_init_callback_registry(aTHX);
5177  
5178 0 svp = hv_fetch(g_file_callback_registry, pred_name, pred_name_len, 0);
5179 0 ▶ if (!svp || !*svp)
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !*svp) 0 0 0
if (!svp || !*svp) 0 0 0
5180 0 croak("File::Raw plugin 'predicate': unknown predicate '%s'", pred_name);
5181 0 cb = INT2PTR(FileLineCallback *, SvIV(*svp));
5182  
5183 0 ▶ if (cb->predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (cb->predicate) { 0 0 0
5184 0 ▶ return cb->predicate(aTHX_ record) ? record : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return cb->predicate(aTHX_ record) ? record : &PL_sv_undef; 0 0 0
5185 0 ▶ } else if (cb->perl_callback) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cb->perl_callback) { 0 0 0
5186 0 ▶ SV *old_defsv = DEFSV;
Branch: 1 branch
BranchTrueFalseTotal
SV *old_defsv = DEFSV; 0 0 0
5187 SV *result_sv;
5188 int count;
5189 0 bool matched = FALSE;
5190 0 dSP;
5191  
5192 0 ▶ DEFSV = record;
Branch: 1 branch
BranchTrueFalseTotal
DEFSV = record; 0 0 0
5193 0 ENTER;
5194 0 SAVETMPS;
5195 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
5196 0 PUTBACK;
5197 0 count = call_sv(cb->perl_callback, G_SCALAR);
5198 0 SPAGAIN;
5199 0 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 0 0 0
5200 0 result_sv = POPs;
5201 0 matched = SvTRUE(result_sv);
5202 }
5203 0 PUTBACK;
5204 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
5205 0 LEAVE;
5206 0 ▶ DEFSV = old_defsv;
Branch: 1 branch
BranchTrueFalseTotal
DEFSV = old_defsv; 0 0 0
5207 0 ▶ return matched ? record : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return matched ? record : &PL_sv_undef; 0 0 0
5208 }
5209 0 return &PL_sv_undef;
5210 }
5211  
5212 static FilePlugin g_predicate_plugin = {
5213 "predicate",
5214 NULL, /* read */
5215 NULL, /* write */
5216 predicate_plugin_record, /* record */
5217 NULL, /* stream */
5218 NULL /* state - the global registry is consulted directly */
5219 };
5220  
5221 /* Public XSUBs for the predicate registry that backs the 'predicate'
5222 * plugin. Adding here also makes the entry visible to the legacy 2-arg
5223 * grep_lines($p, $name) sugar, which looks names up in the same HV. */
5224  
5225 8 XS_INTERNAL(xs_register_predicate) {
5226 8 dXSARGS;
5227 const char *name;
5228 STRLEN name_len;
5229 SV *coderef;
5230 FileLineCallback *cb;
5231 FileLineCallback *existing;
5232 SV *sv;
5233  
5234 8 ▶ if (items != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) 1 7 8
5235 1 croak("Usage: File::Raw::register_predicate($name, \\&coderef)");
5236  
5237 7 name = SvPV(ST(0), name_len);
5238 7 coderef = ST(1);
5239  
5240 7 ▶ if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) 5 2 7
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) 0 5 5
5241 2 croak("File::Raw::register_predicate: second arg must be a coderef");
5242  
5243 5 file_init_callback_registry(aTHX);
5244  
5245 5 existing = file_get_callback(aTHX_ name);
5246 5 ▶ if (existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing) { 2 3 5
5247 2 ▶ if (existing->perl_callback) SvREFCNT_dec(existing->perl_callback);
Branch: 1 branch
BranchTrueFalseTotal
if (existing->perl_callback) SvREFCNT_dec(existing->perl_callback); 2 0 2
5248 2 existing->perl_callback = newSVsv(coderef);
5249 2 existing->predicate = NULL;
5250 2 XSRETURN_YES;
5251 }
5252  
5253 3 Newxz(cb, 1, FileLineCallback);
5254 3 cb->predicate = NULL;
5255 3 cb->perl_callback = newSVsv(coderef);
5256 3 sv = newSViv(PTR2IV(cb));
5257 3 hv_store(g_file_callback_registry, name, name_len, sv, 0);
5258  
5259 3 XSRETURN_YES;
5260 }
5261  
5262 3 XS_INTERNAL(xs_list_predicates) {
5263 3 dXSARGS;
5264 3 AV *result = newAV();
5265 HE *he;
5266  
5267 PERL_UNUSED_VAR(items);
5268  
5269 3 ▶ if (g_file_callback_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_file_callback_registry) { 3 0 3
5270 3 hv_iterinit(g_file_callback_registry);
5271 42 ▶ while ((he = hv_iternext(g_file_callback_registry))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(g_file_callback_registry))) { 39 3 42
5272 I32 klen;
5273 39 const char *kname = hv_iterkey(he, &klen);
5274 39 av_push(result, newSVpvn(kname, klen));
5275 }
5276 }
5277  
5278 3 ST(0) = sv_2mortal(newRV_noinc((SV *)result));
5279 3 XSRETURN(1);
5280 }
5281  
5282 34 XS_INTERNAL(xs_register_plugin) {
5283 34 dXSARGS;
5284 const char *name;
5285 STRLEN name_len;
5286 SV *spec;
5287 HV *spec_hv;
5288 SV **svp;
5289 PerlPluginBridge *b;
5290 int rc;
5291 34 int override = 0;
5292  
5293 34 ▶ if (items < 2 || items > 3)
Branch: 2 branches
BranchTrueFalseTotal
if (items < 2 || items > 3) 33 1 34
if (items < 2 || items > 3) 0 33 33
5294 1 croak("Usage: File::Raw::register_plugin($name, \\%%phases [, $override])");
5295  
5296 33 name = SvPV(ST(0), name_len);
5297 33 ▶ if (name_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) 0 33 33
5298 0 croak("File::Raw::register_plugin: name must be non-empty");
5299  
5300 33 spec = ST(1);
5301 33 ▶ if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV) 32 1 33
if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV) 0 32 32
5302 1 croak("File::Raw::register_plugin: second arg must be a hashref");
5303 32 spec_hv = (HV *)SvRV(spec);
5304  
5305 32 ▶ if (items == 3) override = SvTRUE(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items == 3) override = SvTRUE(ST(2)); 2 30 32
5306  
5307 32 ▶ if (override) (void)file_unregister_plugin(aTHX_ name);
Branch: 1 branch
BranchTrueFalseTotal
if (override) (void)file_unregister_plugin(aTHX_ name); 2 30 32
5308  
5309 32 Newxz(b, 1, PerlPluginBridge);
5310 32 b->name = savepv(name);
5311  
5312 32 svp = hv_fetchs(spec_hv, "read", 0);
5313 32 ▶ if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 29 3 32
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 29 0 29
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 28 1 29
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 28 0 28
5314 28 b->read_cv = newSVsv(*svp);
5315 28 b->plugin.read_fn = perl_plugin_thunk_read;
5316 }
5317 32 svp = hv_fetchs(spec_hv, "write", 0);
5318 32 ▶ if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 8 24 32
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 8 0 8
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 8 0 8
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 8 0 8
5319 8 b->write_cv = newSVsv(*svp);
5320 8 b->plugin.write_fn = perl_plugin_thunk_write;
5321 }
5322 32 svp = hv_fetchs(spec_hv, "record", 0);
5323 32 ▶ if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 1 31 32
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 1 0 1
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 1 0 1
if (svp && *svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 1 0 1
5324 1 b->record_cv = newSVsv(*svp);
5325 1 b->plugin.record_fn = perl_plugin_thunk_record;
5326 }
5327 /* hv_existss is 5.36+. Use hv_exists for portability. */
5328 32 ▶ if (hv_exists(spec_hv, "stream", 6))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(spec_hv, "stream", 6)) 1 31 32
5329 1 croak("File::Raw::register_plugin: 'stream' phase not supported "
5330 "from Perl - implement 'record' instead, or write a C plugin");
5331  
5332 31 ▶ if (!b->plugin.read_fn && !b->plugin.write_fn && !b->plugin.record_fn) {
Branch: 3 branches
BranchTrueFalseTotal
if (!b->plugin.read_fn && !b->plugin.write_fn && !b->plugin.record_fn) { 3 28 31
if (!b->plugin.read_fn && !b->plugin.write_fn && !b->plugin.record_fn) { 3 0 3
if (!b->plugin.read_fn && !b->plugin.write_fn && !b->plugin.record_fn) { 2 1 3
5333 2 perl_plugin_bridge_free(aTHX_ b);
5334 2 croak("File::Raw::register_plugin: at least one of read/write/record "
5335 "must be a coderef");
5336 }
5337  
5338 29 b->plugin.name = b->name;
5339 29 b->plugin.state = b;
5340  
5341 29 rc = file_register_plugin(aTHX_ &b->plugin);
5342 29 ▶ if (rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 1) { 1 28 29
5343 1 perl_plugin_bridge_free(aTHX_ b);
5344 1 ▶ if (rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) 1 0 1
5345 1 croak("File::Raw::register_plugin: plugin '%s' is already registered",
5346 name);
5347 0 croak("File::Raw::register_plugin: invalid plugin spec for '%s'", name);
5348 }
5349  
5350 28 ▶ if (!g_perl_plugins) g_perl_plugins = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!g_perl_plugins) g_perl_plugins = newHV(); 13 15 28
5351 28 hv_store(g_perl_plugins, name, name_len, newSViv(PTR2IV(b)), 0);
5352  
5353 28 XSRETURN_YES;
5354 }
5355  
5356 12 XS_INTERNAL(xs_unregister_plugin) {
5357 12 dXSARGS;
5358 const char *name;
5359 STRLEN name_len;
5360 SV **svp;
5361 12 int removed_perl = 0;
5362  
5363 12 ▶ if (items != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) 0 12 12
5364 0 croak("Usage: File::Raw::unregister_plugin($name)");
5365  
5366 12 name = SvPV(ST(0), name_len);
5367  
5368 12 ▶ if (g_perl_plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_perl_plugins) { 12 0 12
5369 12 svp = hv_fetch(g_perl_plugins, name, name_len, 0);
5370 12 ▶ if (svp && *svp) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && *svp) { 11 1 12
if (svp && *svp) { 11 0 11
5371 11 PerlPluginBridge *b = INT2PTR(PerlPluginBridge *, SvIV(*svp));
5372 11 (void)hv_delete(g_perl_plugins, name, name_len, G_DISCARD);
5373 11 (void)file_unregister_plugin(aTHX_ name);
5374 11 perl_plugin_bridge_free(aTHX_ b);
5375 11 removed_perl = 1;
5376 }
5377 }
5378 12 ▶ if (!removed_perl)
Branch: 1 branch
BranchTrueFalseTotal
if (!removed_perl) 1 11 12
5379 1 (void)file_unregister_plugin(aTHX_ name);
5380  
5381 12 XSRETURN_YES;
5382 }
5383  
5384 3 XS_INTERNAL(xs_list_plugins) {
5385 3 dXSARGS;
5386 3 AV *result = newAV();
5387 HE *he;
5388  
5389 PERL_UNUSED_VAR(items);
5390  
5391 3 ▶ if (g_file_plugin_registry) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_file_plugin_registry) { 3 0 3
5392 3 hv_iterinit(g_file_plugin_registry);
5393 7 ▶ while ((he = hv_iternext(g_file_plugin_registry))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(g_file_plugin_registry))) { 4 3 7
5394 I32 klen;
5395 4 const char *kname = hv_iterkey(he, &klen);
5396 4 av_push(result, newSVpvn(kname, klen));
5397 }
5398 }
5399  
5400 3 ST(0) = sv_2mortal(newRV_noinc((SV *)result));
5401 3 XSRETURN(1);
5402 }
5403  
5404 /* New stat functions */
5405 2 XS_INTERNAL(xs_atime) {
5406 2 dXSARGS;
5407 const char *path;
5408 2 ▶ if (items != 1) croak("Usage: file::atime(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::atime(path)"); 0 2 2
5409 2 path = SvPV_nolen(ST(0));
5410 2 ST(0) = sv_2mortal(newSViv(file_atime_internal(path)));
5411 2 XSRETURN(1);
5412 }
5413  
5414 2 XS_INTERNAL(xs_ctime) {
5415 2 dXSARGS;
5416 const char *path;
5417 2 ▶ if (items != 1) croak("Usage: file::ctime(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::ctime(path)"); 0 2 2
5418 2 path = SvPV_nolen(ST(0));
5419 2 ST(0) = sv_2mortal(newSViv(file_ctime_internal(path)));
5420 2 XSRETURN(1);
5421 }
5422  
5423 4 XS_INTERNAL(xs_mode) {
5424 4 dXSARGS;
5425 const char *path;
5426 4 ▶ if (items != 1) croak("Usage: file::mode(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::mode(path)"); 0 4 4
5427 4 path = SvPV_nolen(ST(0));
5428 4 ST(0) = sv_2mortal(newSViv(file_mode_internal(path)));
5429 4 XSRETURN(1);
5430 }
5431  
5432 /* Combined stat - all attributes in one syscall */
5433 7 XS_INTERNAL(xs_stat_all) {
5434 7 dXSARGS;
5435 const char *path;
5436 HV *result;
5437 7 ▶ if (items != 1) croak("Usage: File::Raw::stat(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: File::Raw::stat(path)"); 0 7 7
5438 7 path = SvPV_nolen(ST(0));
5439 7 result = file_stat_all_internal(aTHX_ path);
5440 7 ▶ if (result == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result == NULL) { 1 6 7
5441 1 ST(0) = &PL_sv_undef;
5442 } else {
5443 6 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
5444 }
5445 7 XSRETURN(1);
5446 }
5447  
5448 4 XS_INTERNAL(xs_is_link) {
5449 4 dXSARGS;
5450 const char *path;
5451 4 ▶ if (items != 1) croak("Usage: file::is_link(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_link(path)"); 0 4 4
5452 4 path = SvPV_nolen(ST(0));
5453 4 ▶ ST(0) = file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no; 1 3 4
5454 4 XSRETURN(1);
5455 }
5456  
5457 2 XS_INTERNAL(xs_is_executable) {
5458 2 dXSARGS;
5459 const char *path;
5460 2 ▶ if (items != 1) croak("Usage: file::is_executable(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::is_executable(path)"); 0 2 2
5461 2 path = SvPV_nolen(ST(0));
5462 2 ▶ ST(0) = file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no; 1 1 2
5463 2 XSRETURN(1);
5464 }
5465  
5466 /* File manipulation functions */
5467 4 XS_INTERNAL(xs_unlink) {
5468 4 dXSARGS;
5469 const char *path;
5470 4 ▶ if (items != 1) croak("Usage: file::unlink(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::unlink(path)"); 0 4 4
5471 4 path = SvPV_nolen(ST(0));
5472 4 ▶ ST(0) = file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no; 2 2 4
5473 4 XSRETURN(1);
5474 }
5475  
5476 3 XS_INTERNAL(xs_copy) {
5477 3 dXSARGS;
5478 const char *src;
5479 const char *dst;
5480 3 ▶ if (items != 2) croak("Usage: file::copy(src, dst)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file::copy(src, dst)"); 0 3 3
5481 3 src = SvPV_nolen(ST(0));
5482 3 ▶ dst = SvPV_nolen(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
dst = SvPV_nolen(ST(1)); 3 0 3
5483 3 ▶ ST(0) = file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no; 2 1 3
5484 3 XSRETURN(1);
5485 }
5486  
5487 3 XS_INTERNAL(xs_move) {
5488 3 dXSARGS;
5489 const char *src;
5490 const char *dst;
5491 3 ▶ if (items != 2) croak("Usage: file::move(src, dst)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file::move(src, dst)"); 0 3 3
5492 3 src = SvPV_nolen(ST(0));
5493 3 ▶ dst = SvPV_nolen(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
dst = SvPV_nolen(ST(1)); 3 0 3
5494 3 ▶ ST(0) = file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no; 2 1 3
5495 3 XSRETURN(1);
5496 }
5497  
5498 2 XS_INTERNAL(xs_touch) {
5499 2 dXSARGS;
5500 const char *path;
5501 2 ▶ if (items != 1) croak("Usage: file::touch(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::touch(path)"); 0 2 2
5502 2 path = SvPV_nolen(ST(0));
5503 2 ▶ ST(0) = file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no; 2 0 2
5504 2 XSRETURN(1);
5505 }
5506  
5507 4 XS_INTERNAL(xs_clear_stat_cache) {
5508 4 dXSARGS;
5509 4 ▶ if (items > 1) croak("Usage: file::clear_stat_cache() or file::clear_stat_cache(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) croak("Usage: file::clear_stat_cache() or file::clear_stat_cache(path)"); 0 4 4
5510
5511 4 ▶ if (items == 1 && SvOK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items == 1 && SvOK(ST(0))) { 0 4 4
if (items == 1 && SvOK(ST(0))) { 0 0 0
5512 0 const char *path = SvPV_nolen(ST(0));
5513 0 invalidate_stat_cache_path(path);
5514 } else {
5515 4 invalidate_stat_cache();
5516 }
5517
5518 4 ST(0) = &PL_sv_yes;
5519 4 XSRETURN(1);
5520 }
5521  
5522 4 XS_INTERNAL(xs_chmod) {
5523 4 dXSARGS;
5524 const char *path;
5525 int mode;
5526 4 ▶ if (items != 2) croak("Usage: file::chmod(path, mode)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file::chmod(path, mode)"); 0 4 4
5527 4 path = SvPV_nolen(ST(0));
5528 4 mode = SvIV(ST(1));
5529 4 ▶ ST(0) = file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no; 4 0 4
5530 4 XSRETURN(1);
5531 }
5532  
5533 12 XS_INTERNAL(xs_mkdir) {
5534 12 dXSARGS;
5535 const char *path;
5536 12 int mode = 0755;
5537 12 ▶ if (items < 1 || items > 2) croak("Usage: file::mkdir(path, [mode])");
Branch: 2 branches
BranchTrueFalseTotal
if (items < 1 || items > 2) croak("Usage: file::mkdir(path, [mode])"); 12 0 12
if (items < 1 || items > 2) croak("Usage: file::mkdir(path, [mode])"); 0 12 12
5538 12 path = SvPV_nolen(ST(0));
5539 12 ▶ if (items > 1) mode = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) mode = SvIV(ST(1)); 0 12 12
5540 12 ▶ ST(0) = file_mkdir_internal(path, mode) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_mkdir_internal(path, mode) ? &PL_sv_yes : &PL_sv_no; 8 4 12
5541 12 XSRETURN(1);
5542 }
5543  
5544 6 XS_INTERNAL(xs_rmdir) {
5545 6 dXSARGS;
5546 const char *path;
5547 6 ▶ if (items != 1) croak("Usage: file::rmdir(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::rmdir(path)"); 0 6 6
5548 6 path = SvPV_nolen(ST(0));
5549 6 ▶ ST(0) = file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no; 2 4 6
5550 6 XSRETURN(1);
5551 }
5552  
5553 5 XS_INTERNAL(xs_readdir) {
5554 5 dXSARGS;
5555 const char *path;
5556 AV *result;
5557 5 ▶ if (items != 1) croak("Usage: file::readdir(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::readdir(path)"); 0 5 5
5558 5 path = SvPV_nolen(ST(0));
5559 5 result = file_readdir_internal(aTHX_ path);
5560 5 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
5561 5 XSRETURN(1);
5562 }
5563  
5564 /* Path manipulation functions */
5565 12 XS_INTERNAL(xs_basename) {
5566 12 dXSARGS;
5567 const char *path;
5568 12 ▶ if (items != 1) croak("Usage: file::basename(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::basename(path)"); 0 12 12
5569 12 path = SvPV_nolen(ST(0));
5570 12 ST(0) = sv_2mortal(file_basename_internal(aTHX_ path));
5571 12 XSRETURN(1);
5572 }
5573  
5574 10 XS_INTERNAL(xs_dirname) {
5575 10 dXSARGS;
5576 const char *path;
5577 10 ▶ if (items != 1) croak("Usage: file::dirname(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::dirname(path)"); 0 10 10
5578 10 path = SvPV_nolen(ST(0));
5579 10 ST(0) = sv_2mortal(file_dirname_internal(aTHX_ path));
5580 10 XSRETURN(1);
5581 }
5582  
5583 13 XS_INTERNAL(xs_extname) {
5584 13 dXSARGS;
5585 const char *path;
5586 13 ▶ if (items != 1) croak("Usage: file::extname(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file::extname(path)"); 0 13 13
5587 13 path = SvPV_nolen(ST(0));
5588 13 ST(0) = sv_2mortal(file_extname_internal(aTHX_ path));
5589 13 XSRETURN(1);
5590 }
5591  
5592 11 XS_INTERNAL(xs_join) {
5593 11 dXSARGS;
5594 AV *parts;
5595 SSize_t i;
5596  
5597 11 ▶ if (items < 1) croak("Usage: file::join(part1, part2, ...)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: file::join(part1, part2, ...)"); 0 11 11
5598  
5599 11 parts = newAV();
5600 37 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 26 11 37
5601 26 av_push(parts, newSVsv(ST(i)));
5602 }
5603  
5604 11 ST(0) = sv_2mortal(file_join_internal(aTHX_ parts));
5605 11 SvREFCNT_dec((SV*)parts);
5606 11 XSRETURN(1);
5607 }
5608  
5609 /* mkpath: recursive mkdir */
5610 1 XS_INTERNAL(xs_mkpath) {
5611 1 dXSARGS;
5612 const char *path;
5613 STRLEN path_len;
5614 char buf[4096];
5615 STRLEN i;
5616 1 int created = 0;
5617  
5618 1 ▶ if (items != 1) croak("Usage: file_mkpath(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_mkpath(path)"); 0 1 1
5619 1 path = SvPV(ST(0), path_len);
5620 1 ▶ if (path_len >= sizeof(buf)) croak("Path too long");
Branch: 1 branch
BranchTrueFalseTotal
if (path_len >= sizeof(buf)) croak("Path too long"); 0 1 1
5621  
5622 36 ▶ for (i = 0; i <= path_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= path_len; i++) { 35 1 36
5623 35 ▶ if (i == path_len || path[i] == '/' || path[i] == '\\') {
Branch: 3 branches
BranchTrueFalseTotal
if (i == path_len || path[i] == '/' || path[i] == '\\') { 34 1 35
if (i == path_len || path[i] == '/' || path[i] == '\\') { 30 4 34
if (i == path_len || path[i] == '/' || path[i] == '\\') { 0 30 30
5624 5 ▶ if (i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) { 1 4 5
5625 /* Root / or drive-relative */
5626 1 buf[0] = path[0];
5627 1 buf[1] = '\0';
5628 1 continue;
5629 }
5630 4 memcpy(buf, path, i);
5631 4 buf[i] = '\0';
5632  
5633 /* Skip drive letter portion like C: */
5634 4 ▶ if (i == 2 && buf[1] == ':') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (i == 2 && buf[1] == ':') continue; 0 4 4
if (i == 2 && buf[1] == ':') continue; 0 0 0
5635  
5636 4 ▶ if (!file_is_dir_internal(buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file_is_dir_internal(buf)) { 2 2 4
5637 2 ▶ if (file_mkdir_internal(buf, 0755))
Branch: 1 branch
BranchTrueFalseTotal
if (file_mkdir_internal(buf, 0755)) 2 0 2
5638 2 created = 1;
5639 }
5640 }
5641 }
5642  
5643 1 ▶ ST(0) = created || file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = created || file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 1 1
ST(0) = created || file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
5644 1 XSRETURN(1);
5645 }
5646  
5647 /* rm_rf: recursive remove directory */
5648 2 static void file_rm_rf_internal(pTHX_ const char *path) {
5649 AV *entries;
5650 SSize_t i, len;
5651  
5652 2 ▶ if (!file_is_dir_internal(path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file_is_dir_internal(path)) { 0 2 2
5653 0 file_unlink_internal(path);
5654 0 return;
5655 }
5656  
5657 2 entries = file_readdir_internal(aTHX_ path);
5658 2 len = av_len(entries) + 1;
5659 3 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 1 2 3
5660 1 SV **sv = av_fetch(entries, i, 0);
5661 1 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 1 0 1
5662 1 AV *join_parts = newAV();
5663 SV *child_sv;
5664 const char *child;
5665  
5666 1 av_push(join_parts, newSVpv(path, 0));
5667 1 av_push(join_parts, newSVsv(*sv));
5668 1 child_sv = file_join_internal(aTHX_ join_parts);
5669 1 child = SvPV_nolen(child_sv);
5670  
5671 1 ▶ if (file_is_dir_internal(child)) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_is_dir_internal(child)) { 1 0 1
5672 1 file_rm_rf_internal(aTHX_ child);
5673 } else {
5674 0 file_unlink_internal(child);
5675 }
5676  
5677 1 SvREFCNT_dec(child_sv);
5678 1 SvREFCNT_dec((SV*)join_parts);
5679 }
5680 }
5681 2 SvREFCNT_dec((SV*)entries);
5682 2 file_rmdir_internal(path);
5683 }
5684  
5685 1 XS_INTERNAL(xs_rm_rf) {
5686 1 dXSARGS;
5687 const char *path;
5688  
5689 1 ▶ if (items != 1) croak("Usage: file_rm_rf(path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_rm_rf(path)"); 0 1 1
5690 1 path = SvPV_nolen(ST(0));
5691 1 file_rm_rf_internal(aTHX_ path);
5692 1 ST(0) = &PL_sv_yes;
5693 1 XSRETURN(1);
5694 }
5695  
5696 /* Head and tail */
5697 /* head/tail share an arg-parsing convention: even items means $n is at
5698 * ST(1) and the plugin tail (if any) begins at ST(2); odd items means
5699 * $n is omitted and the plugin tail begins at ST(1). This avoids the
5700 * ambiguity of "is ST(1) a count or the first option key?". */
5701 8 XS_INTERNAL(xs_head) {
5702 8 dXSARGS;
5703 const char *path;
5704 AV *result;
5705 8 IV n = 10;
5706 int n_positional;
5707  
5708 8 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 8 8
5709 0 croak("Usage: file::head(path [, n] [, plugin => ..., key => value ...])");
5710  
5711 8 path = SvPV_nolen(ST(0));
5712 8 ▶ n_positional = (items % 2 == 0) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
n_positional = (items % 2 == 0) ? 2 : 1; 5 3 8
5713 8 ▶ if (n_positional == 2) n = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (n_positional == 2) n = SvIV(ST(1)); 5 3 8
5714  
5715 8 ▶ if (items > n_positional) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > n_positional) { 2 6 8
5716 HV *opts;
5717 2 SV *holder = NULL;
5718 AV *records;
5719 AV *out;
5720 SSize_t i, total, take;
5721  
5722 2 opts = file_plugin_build_opts(aTHX_ &ST(0), n_positional, items, "head");
5723 2 records = file_records_via_plugin(aTHX_ opts, path, &holder);
5724 2 SvREFCNT_dec((SV *)opts);
5725 2 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 2 2
5726 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
5727 0 XSRETURN(1);
5728 }
5729 2 out = newAV();
5730 2 total = av_len(records) + 1;
5731 2 take = (n < (IV)total) ? n : total;
5732 2 ▶ if (take < 0) take = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (take < 0) take = 0; 0 2 2
5733 2 av_extend(out, take);
5734 9 ▶ for (i = 0; i < take; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < take; i++) { 7 2 9
5735 7 SV **rp = av_fetch(records, i, 0);
5736 7 ▶ av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 7 0 7
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 7 0 7
5737 }
5738 2 SvREFCNT_dec(holder);
5739 2 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
5740 2 XSRETURN(1);
5741 }
5742  
5743 6 result = file_head_internal(aTHX_ path, n);
5744 6 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
5745 6 XSRETURN(1);
5746 }
5747  
5748 7 XS_INTERNAL(xs_tail) {
5749 7 dXSARGS;
5750 const char *path;
5751 AV *result;
5752 7 IV n = 10;
5753 int n_positional;
5754  
5755 7 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 7 7
5756 0 croak("Usage: file::tail(path [, n] [, plugin => ..., key => value ...])");
5757  
5758 7 path = SvPV_nolen(ST(0));
5759 7 ▶ n_positional = (items % 2 == 0) ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
n_positional = (items % 2 == 0) ? 2 : 1; 5 2 7
5760 7 ▶ if (n_positional == 2) n = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (n_positional == 2) n = SvIV(ST(1)); 5 2 7
5761  
5762 7 ▶ if (items > n_positional) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > n_positional) { 1 6 7
5763 HV *opts;
5764 1 SV *holder = NULL;
5765 AV *records;
5766 AV *out;
5767 SSize_t i, total, start, take;
5768  
5769 1 opts = file_plugin_build_opts(aTHX_ &ST(0), n_positional, items, "tail");
5770 1 records = file_records_via_plugin(aTHX_ opts, path, &holder);
5771 1 SvREFCNT_dec((SV *)opts);
5772 1 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 1 1
5773 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
5774 0 XSRETURN(1);
5775 }
5776 1 out = newAV();
5777 1 total = av_len(records) + 1;
5778 1 take = (n < (IV)total) ? n : total;
5779 1 ▶ if (take < 0) take = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (take < 0) take = 0; 0 1 1
5780 1 start = total - take;
5781 1 av_extend(out, take);
5782 3 ▶ for (i = start; i < total; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < total; i++) { 2 1 3
5783 2 SV **rp = av_fetch(records, i, 0);
5784 2 ▶ av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 2 0 2
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 2 0 2
5785 }
5786 1 SvREFCNT_dec(holder);
5787 1 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
5788 1 XSRETURN(1);
5789 }
5790  
5791 6 result = file_tail_internal(aTHX_ path, n);
5792 6 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
5793 6 XSRETURN(1);
5794 }
5795  
5796 /* range_lines(path, from, count [, plugin => ..., key => value ...])
5797 *
5798 * 1-based, half-open in count style: range_lines($p, 5, 3) returns
5799 * lines 5, 6, 7 (or fewer if EOF arrives first). Symmetric with
5800 * head/tail in shape and plugin behaviour. */
5801 16 XS_INTERNAL(xs_range_lines) {
5802 16 dXSARGS;
5803 const char *path;
5804 IV from, count;
5805 AV *result;
5806  
5807 16 ▶ if (items < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) 2 14 16
5808 2 croak("Usage: file::range_lines(path, from, count "
5809 "[, plugin => ..., key => value ...])");
5810  
5811 14 path = SvPV_nolen(ST(0));
5812 14 from = SvIV(ST(1));
5813 14 count = SvIV(ST(2));
5814  
5815 /* Plugin path: slice the plugin's record AoA. Same eager trade-off
5816 * as head/tail/lines under a plugin tail. */
5817 14 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 5 9 14
5818 HV *opts;
5819 5 SV *holder = NULL;
5820 AV *records;
5821 AV *out;
5822 SSize_t i, total, start, end;
5823  
5824 5 opts = file_plugin_build_opts(aTHX_ &ST(0), 3, items, "range_lines");
5825 3 records = file_records_via_plugin(aTHX_ opts, path, &holder);
5826 2 SvREFCNT_dec((SV *)opts);
5827 2 ▶ if (!records) {
Branch: 1 branch
BranchTrueFalseTotal
if (!records) { 0 2 2
5828 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
5829 0 XSRETURN(1);
5830 }
5831 2 out = newAV();
5832 2 ▶ if (from < 1 || count <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (from < 1 || count <= 0) { 2 0 2
if (from < 1 || count <= 0) { 0 2 2
5833 0 SvREFCNT_dec(holder);
5834 0 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
5835 0 XSRETURN(1);
5836 }
5837 2 total = av_len(records) + 1;
5838 2 start = from - 1; /* 1-based -> 0-based */
5839 2 ▶ if (start >= total) {
Branch: 1 branch
BranchTrueFalseTotal
if (start >= total) { 0 2 2
5840 0 SvREFCNT_dec(holder);
5841 0 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
5842 0 XSRETURN(1);
5843 }
5844 2 end = start + count;
5845 2 ▶ if (end > total) end = total;
Branch: 1 branch
BranchTrueFalseTotal
if (end > total) end = total; 1 1 2
5846 2 av_extend(out, end - start - 1);
5847 7 ▶ for (i = start; i < end; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start; i < end; i++) { 5 2 7
5848 5 SV **rp = av_fetch(records, i, 0);
5849 5 ▶ av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 5 0 5
av_push(out, SvREFCNT_inc(rp && *rp ? *rp : &PL_sv_undef)); 5 0 5
5850 }
5851 2 SvREFCNT_dec(holder);
5852 2 ST(0) = sv_2mortal(newRV_noinc((SV *)out));
5853 2 XSRETURN(1);
5854 }
5855  
5856 9 result = file_range_internal(aTHX_ path, from, count);
5857 9 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
5858 9 XSRETURN(1);
5859 }
5860  
5861 /* Atomic spew */
5862 17 XS_INTERNAL(xs_atomic_spew) {
5863 17 dXSARGS;
5864 const char *path;
5865 SV *payload;
5866 17 SV *bytes_to_write = NULL;
5867  
5868 17 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 17 17
5869 0 croak("Usage: file::atomic_spew(path, data [, plugin => ..., key => value ...])");
5870  
5871 17 path = SvPV_nolen(ST(0));
5872 17 payload = ST(1);
5873  
5874 17 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 3 14 17
5875 3 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "atomic_spew");
5876 3 bytes_to_write = file_plugin_dispatch_write(aTHX_ opts, path, payload);
5877 3 SvREFCNT_dec((SV *)opts);
5878 3 ▶ if (!bytes_to_write) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes_to_write) { 0 3 3
5879 0 ST(0) = &PL_sv_no;
5880 0 XSRETURN(1);
5881 }
5882 3 payload = sv_2mortal(bytes_to_write);
5883 }
5884  
5885 17 ▶ ST(0) = file_atomic_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_atomic_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 17 0 17
5886 17 XSRETURN(1);
5887 }
5888  
5889 /* ============================================
5890 Function-style XS (for import)
5891 ============================================ */
5892  
5893 1 XS_EXTERNAL(XS_file_func_slurp) {
5894 1 dXSARGS;
5895 const char *path;
5896 SV *bytes;
5897  
5898 1 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 1 1
5899 0 croak("Usage: file_slurp($path [, plugin => ..., key => value ...])");
5900  
5901 1 path = SvPV_nolen(ST(0));
5902 1 bytes = file_slurp_internal(aTHX_ path);
5903  
5904 1 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 1 0 1
5905 1 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 1, items, "slurp");
5906 1 SV *out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
5907 1 SvREFCNT_dec((SV *)opts);
5908 1 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 1 1
5909 0 SvREFCNT_dec(bytes);
5910 0 ST(0) = &PL_sv_undef;
5911 0 XSRETURN(1);
5912 }
5913 1 ▶ if (out != bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) { 1 0 1
5914 1 SvREFCNT_dec(bytes);
5915 1 bytes = out;
5916 }
5917 }
5918 1 ST(0) = sv_2mortal(bytes);
5919 1 XSRETURN(1);
5920 }
5921  
5922 1 XS_EXTERNAL(XS_file_func_spew) {
5923 1 dXSARGS;
5924 const char *path;
5925 SV *payload;
5926  
5927 1 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 1 1
5928 0 croak("Usage: file_spew($path, $data [, plugin => ..., key => value ...])");
5929  
5930 1 path = SvPV_nolen(ST(0));
5931 1 payload = ST(1);
5932  
5933 1 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 1 1
5934 0 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "spew");
5935 0 SV *out = file_plugin_dispatch_write(aTHX_ opts, path, payload);
5936 0 SvREFCNT_dec((SV *)opts);
5937 0 ▶ if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); } 0 0 0
5938 0 payload = sv_2mortal(out);
5939 }
5940 1 ▶ ST(0) = file_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 1 0 1
5941 1 XSRETURN(1);
5942 }
5943  
5944 0 XS_EXTERNAL(XS_file_func_exists) {
5945 0 dXSARGS;
5946 const char *path;
5947 0 ▶ if (items != 1) croak("Usage: file_exists($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_exists($path)"); 0 0 0
5948 0 path = SvPV_nolen(ST(0));
5949 0 ▶ ST(0) = file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_exists_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
5950 0 XSRETURN(1);
5951 }
5952  
5953 0 XS_EXTERNAL(XS_file_func_size) {
5954 0 dXSARGS;
5955 const char *path;
5956 0 ▶ if (items != 1) croak("Usage: file_size($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_size($path)"); 0 0 0
5957 0 path = SvPV_nolen(ST(0));
5958 0 ST(0) = sv_2mortal(newSViv(file_size_internal(path)));
5959 0 XSRETURN(1);
5960 }
5961  
5962 0 XS_EXTERNAL(XS_file_func_is_file) {
5963 0 dXSARGS;
5964 const char *path;
5965 0 ▶ if (items != 1) croak("Usage: file_is_file($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_file($path)"); 0 0 0
5966 0 path = SvPV_nolen(ST(0));
5967 0 ▶ ST(0) = file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_file_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
5968 0 XSRETURN(1);
5969 }
5970  
5971 0 XS_EXTERNAL(XS_file_func_is_dir) {
5972 0 dXSARGS;
5973 const char *path;
5974 0 ▶ if (items != 1) croak("Usage: file_is_dir($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_dir($path)"); 0 0 0
5975 0 path = SvPV_nolen(ST(0));
5976 0 ▶ ST(0) = file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_dir_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
5977 0 XSRETURN(1);
5978 }
5979  
5980 0 XS_EXTERNAL(XS_file_func_lines) {
5981 0 dXSARGS;
5982 const char *path;
5983 SV *content;
5984 AV *lines;
5985  
5986 0 ▶ if (items < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) 0 0 0
5987 0 croak("Usage: file_lines($path [, plugin => ..., key => value ...])");
5988  
5989 0 path = SvPV_nolen(ST(0));
5990  
5991 0 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 0 0
5992 0 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 1, items, "lines");
5993 0 SV *bytes = file_slurp_internal(aTHX_ path);
5994 0 SV *out = file_plugin_dispatch_read(aTHX_ opts, path, bytes);
5995 0 SvREFCNT_dec((SV *)opts);
5996 0 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 0 0 0
5997 0 SvREFCNT_dec(bytes);
5998 0 ST(0) = sv_2mortal(newRV_noinc((SV *)newAV()));
5999 0 XSRETURN(1);
6000 }
6001 0 ▶ if (out != bytes) SvREFCNT_dec(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (out != bytes) SvREFCNT_dec(bytes); 0 0 0
6002 0 ▶ if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 0 0 0
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV) { 0 0 0
6003 0 ST(0) = sv_2mortal(out);
6004 0 XSRETURN(1);
6005 }
6006 0 lines = file_split_lines(aTHX_ out);
6007 0 SvREFCNT_dec(out);
6008 0 ST(0) = sv_2mortal(newRV_noinc((SV *)lines));
6009 0 XSRETURN(1);
6010 }
6011  
6012 0 content = file_slurp_internal(aTHX_ path);
6013 0 ▶ if (content == &PL_sv_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (content == &PL_sv_undef) { 0 0 0
6014 0 lines = newAV();
6015 } else {
6016 0 lines = file_split_lines(aTHX_ content);
6017 0 SvREFCNT_dec(content);
6018 }
6019 0 ST(0) = sv_2mortal(newRV_noinc((SV*)lines));
6020 0 XSRETURN(1);
6021 }
6022  
6023 0 XS_EXTERNAL(XS_file_func_unlink) {
6024 0 dXSARGS;
6025 const char *path;
6026 0 ▶ if (items != 1) croak("Usage: file_unlink($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_unlink($path)"); 0 0 0
6027 0 path = SvPV_nolen(ST(0));
6028 0 ▶ ST(0) = file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_unlink_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6029 0 XSRETURN(1);
6030 }
6031  
6032 0 XS_EXTERNAL(XS_file_func_mkdir) {
6033 0 dXSARGS;
6034 const char *path;
6035 0 ▶ if (items != 1) croak("Usage: file_mkdir($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_mkdir($path)"); 0 0 0
6036 0 path = SvPV_nolen(ST(0));
6037 0 ▶ ST(0) = file_mkdir_internal(path, 0755) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_mkdir_internal(path, 0755) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6038 0 XSRETURN(1);
6039 }
6040  
6041 0 XS_EXTERNAL(XS_file_func_rmdir) {
6042 0 dXSARGS;
6043 const char *path;
6044 0 ▶ if (items != 1) croak("Usage: file_rmdir($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_rmdir($path)"); 0 0 0
6045 0 path = SvPV_nolen(ST(0));
6046 0 ▶ ST(0) = file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_rmdir_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6047 0 XSRETURN(1);
6048 }
6049  
6050 0 XS_EXTERNAL(XS_file_func_touch) {
6051 0 dXSARGS;
6052 const char *path;
6053 0 ▶ if (items != 1) croak("Usage: file_touch($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_touch($path)"); 0 0 0
6054 0 path = SvPV_nolen(ST(0));
6055 0 ▶ ST(0) = file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_touch_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6056 0 XSRETURN(1);
6057 }
6058  
6059 0 XS_EXTERNAL(XS_file_func_clear_stat_cache) {
6060 0 dXSARGS;
6061 0 ▶ if (items > 1) croak("Usage: file_clear_stat_cache() or file_clear_stat_cache($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) croak("Usage: file_clear_stat_cache() or file_clear_stat_cache($path)"); 0 0 0
6062
6063 0 ▶ if (items == 1 && SvOK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items == 1 && SvOK(ST(0))) { 0 0 0
if (items == 1 && SvOK(ST(0))) { 0 0 0
6064 0 const char *path = SvPV_nolen(ST(0));
6065 0 invalidate_stat_cache_path(path);
6066 } else {
6067 0 invalidate_stat_cache();
6068 }
6069
6070 0 ST(0) = &PL_sv_yes;
6071 0 XSRETURN(1);
6072 }
6073  
6074 0 XS_EXTERNAL(XS_file_func_basename) {
6075 0 dXSARGS;
6076 const char *path;
6077 0 ▶ if (items != 1) croak("Usage: file_basename($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_basename($path)"); 0 0 0
6078 0 path = SvPV_nolen(ST(0));
6079 0 ST(0) = sv_2mortal(file_basename_internal(aTHX_ path));
6080 0 XSRETURN(1);
6081 }
6082  
6083 0 XS_EXTERNAL(XS_file_func_dirname) {
6084 0 dXSARGS;
6085 const char *path;
6086 0 ▶ if (items != 1) croak("Usage: file_dirname($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_dirname($path)"); 0 0 0
6087 0 path = SvPV_nolen(ST(0));
6088 0 ST(0) = sv_2mortal(file_dirname_internal(aTHX_ path));
6089 0 XSRETURN(1);
6090 }
6091  
6092 0 XS_EXTERNAL(XS_file_func_extname) {
6093 0 dXSARGS;
6094 const char *path;
6095 0 ▶ if (items != 1) croak("Usage: file_extname($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_extname($path)"); 0 0 0
6096 0 path = SvPV_nolen(ST(0));
6097 0 ST(0) = sv_2mortal(file_extname_internal(aTHX_ path));
6098 0 XSRETURN(1);
6099 }
6100  
6101 0 XS_EXTERNAL(XS_file_func_mtime) {
6102 0 dXSARGS;
6103 const char *path;
6104 0 ▶ if (items != 1) croak("Usage: file_mtime($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_mtime($path)"); 0 0 0
6105 0 path = SvPV_nolen(ST(0));
6106 0 ST(0) = sv_2mortal(newSViv(file_mtime_internal(path)));
6107 0 XSRETURN(1);
6108 }
6109  
6110 0 XS_EXTERNAL(XS_file_func_atime) {
6111 0 dXSARGS;
6112 const char *path;
6113 0 ▶ if (items != 1) croak("Usage: file_atime($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_atime($path)"); 0 0 0
6114 0 path = SvPV_nolen(ST(0));
6115 0 ST(0) = sv_2mortal(newSViv(file_atime_internal(path)));
6116 0 XSRETURN(1);
6117 }
6118  
6119 0 XS_EXTERNAL(XS_file_func_ctime) {
6120 0 dXSARGS;
6121 const char *path;
6122 0 ▶ if (items != 1) croak("Usage: file_ctime($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_ctime($path)"); 0 0 0
6123 0 path = SvPV_nolen(ST(0));
6124 0 ST(0) = sv_2mortal(newSViv(file_ctime_internal(path)));
6125 0 XSRETURN(1);
6126 }
6127  
6128 0 XS_EXTERNAL(XS_file_func_mode) {
6129 0 dXSARGS;
6130 const char *path;
6131 0 ▶ if (items != 1) croak("Usage: file_mode($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_mode($path)"); 0 0 0
6132 0 path = SvPV_nolen(ST(0));
6133 0 ST(0) = sv_2mortal(newSViv(file_mode_internal(path)));
6134 0 XSRETURN(1);
6135 }
6136  
6137 0 XS_EXTERNAL(XS_file_func_is_link) {
6138 0 dXSARGS;
6139 const char *path;
6140 0 ▶ if (items != 1) croak("Usage: file_is_link($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_link($path)"); 0 0 0
6141 0 path = SvPV_nolen(ST(0));
6142 0 ▶ ST(0) = file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_link_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6143 0 XSRETURN(1);
6144 }
6145  
6146 0 XS_EXTERNAL(XS_file_func_is_readable) {
6147 0 dXSARGS;
6148 const char *path;
6149 0 ▶ if (items != 1) croak("Usage: file_is_readable($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_readable($path)"); 0 0 0
6150 0 path = SvPV_nolen(ST(0));
6151 0 ▶ ST(0) = file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_readable_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6152 0 XSRETURN(1);
6153 }
6154  
6155 0 XS_EXTERNAL(XS_file_func_is_writable) {
6156 0 dXSARGS;
6157 const char *path;
6158 0 ▶ if (items != 1) croak("Usage: file_is_writable($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_writable($path)"); 0 0 0
6159 0 path = SvPV_nolen(ST(0));
6160 0 ▶ ST(0) = file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_writable_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6161 0 XSRETURN(1);
6162 }
6163  
6164 0 XS_EXTERNAL(XS_file_func_is_executable) {
6165 0 dXSARGS;
6166 const char *path;
6167 0 ▶ if (items != 1) croak("Usage: file_is_executable($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_is_executable($path)"); 0 0 0
6168 0 path = SvPV_nolen(ST(0));
6169 0 ▶ ST(0) = file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_is_executable_internal(path) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6170 0 XSRETURN(1);
6171 }
6172  
6173 0 XS_EXTERNAL(XS_file_func_readdir) {
6174 0 dXSARGS;
6175 const char *path;
6176 AV *result;
6177 0 ▶ if (items != 1) croak("Usage: file_readdir($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_readdir($path)"); 0 0 0
6178 0 path = SvPV_nolen(ST(0));
6179 0 result = file_readdir_internal(aTHX_ path);
6180 0 ST(0) = sv_2mortal(newRV_noinc((SV*)result));
6181 0 XSRETURN(1);
6182 }
6183  
6184 0 XS_EXTERNAL(XS_file_func_slurp_raw) {
6185 0 dXSARGS;
6186 const char *path;
6187 0 ▶ if (items != 1) croak("Usage: file_slurp_raw($path)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: file_slurp_raw($path)"); 0 0 0
6188 0 path = SvPV_nolen(ST(0));
6189 0 ST(0) = sv_2mortal(file_slurp_raw_internal(aTHX_ path));
6190 0 XSRETURN(1);
6191 }
6192  
6193 0 XS_EXTERNAL(XS_file_func_copy) {
6194 0 dXSARGS;
6195 const char *src;
6196 const char *dst;
6197 0 ▶ if (items != 2) croak("Usage: file_copy($src, $dst)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file_copy($src, $dst)"); 0 0 0
6198 0 src = SvPV_nolen(ST(0));
6199 0 ▶ dst = SvPV_nolen(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
dst = SvPV_nolen(ST(1)); 0 0 0
6200 0 ▶ ST(0) = file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_copy_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6201 0 XSRETURN(1);
6202 }
6203  
6204 0 XS_EXTERNAL(XS_file_func_move) {
6205 0 dXSARGS;
6206 const char *src;
6207 const char *dst;
6208 0 ▶ if (items != 2) croak("Usage: file_move($src, $dst)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file_move($src, $dst)"); 0 0 0
6209 0 src = SvPV_nolen(ST(0));
6210 0 ▶ dst = SvPV_nolen(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
dst = SvPV_nolen(ST(1)); 0 0 0
6211 0 ▶ ST(0) = file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_move_internal(aTHX_ src, dst) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6212 0 XSRETURN(1);
6213 }
6214  
6215 0 XS_EXTERNAL(XS_file_func_chmod) {
6216 0 dXSARGS;
6217 const char *path;
6218 int mode;
6219 0 ▶ if (items != 2) croak("Usage: file_chmod($path, $mode)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: file_chmod($path, $mode)"); 0 0 0
6220 0 path = SvPV_nolen(ST(0));
6221 0 mode = SvIV(ST(1));
6222 0 ▶ ST(0) = file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_chmod_internal(path, mode) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6223 0 XSRETURN(1);
6224 }
6225  
6226 0 XS_EXTERNAL(XS_file_func_append) {
6227 0 dXSARGS;
6228 const char *path;
6229 SV *payload;
6230  
6231 0 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 0 0
6232 0 croak("Usage: file_append($path, $data [, plugin => ..., key => value ...])");
6233  
6234 0 path = SvPV_nolen(ST(0));
6235 0 payload = ST(1);
6236  
6237 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
6238 0 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "append");
6239 0 SV *out = file_plugin_dispatch_write(aTHX_ opts, path, payload);
6240 0 SvREFCNT_dec((SV *)opts);
6241 0 ▶ if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); } 0 0 0
6242 0 payload = sv_2mortal(out);
6243 }
6244 0 ▶ ST(0) = file_append_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_append_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6245 0 XSRETURN(1);
6246 }
6247  
6248 0 XS_EXTERNAL(XS_file_func_atomic_spew) {
6249 0 dXSARGS;
6250 const char *path;
6251 SV *payload;
6252  
6253 0 ▶ if (items < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) 0 0 0
6254 0 croak("Usage: file_atomic_spew($path, $data [, plugin => ..., key => value ...])");
6255  
6256 0 path = SvPV_nolen(ST(0));
6257 0 payload = ST(1);
6258  
6259 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
6260 0 HV *opts = file_plugin_build_opts(aTHX_ &ST(0), 2, items, "atomic_spew");
6261 0 SV *out = file_plugin_dispatch_write(aTHX_ opts, path, payload);
6262 0 SvREFCNT_dec((SV *)opts);
6263 0 ▶ if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { ST(0) = &PL_sv_no; XSRETURN(1); } 0 0 0
6264 0 payload = sv_2mortal(out);
6265 }
6266 0 ▶ ST(0) = file_atomic_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = file_atomic_spew_internal(aTHX_ path, payload) ? &PL_sv_yes : &PL_sv_no; 0 0 0
6267 0 XSRETURN(1);
6268 }
6269  
6270 /* Function entry for selective import */
6271 typedef struct {
6272 const char *name; /* Name without file_ prefix (e.g., "slurp") */
6273 int args; /* 1 or 2 arguments */
6274 void (*xs_func)(pTHX_ CV*);
6275 Perl_ppaddr_t pp_func;
6276 } ImportEntry;
6277  
6278 static const ImportEntry import_funcs[] = {
6279 /* 1-arg functions */
6280 {"slurp", 1, XS_file_func_slurp, pp_file_slurp},
6281 {"slurp_raw", 1, XS_file_func_slurp_raw, pp_file_slurp_raw},
6282 {"exists", 1, XS_file_func_exists, pp_file_exists},
6283 {"size", 1, XS_file_func_size, pp_file_size},
6284 {"is_file", 1, XS_file_func_is_file, pp_file_is_file},
6285 {"is_dir", 1, XS_file_func_is_dir, pp_file_is_dir},
6286 {"lines", 1, XS_file_func_lines, pp_file_lines},
6287 {"unlink", 1, XS_file_func_unlink, pp_file_unlink},
6288 {"mkdir", 1, XS_file_func_mkdir, pp_file_mkdir},
6289 {"rmdir", 1, XS_file_func_rmdir, pp_file_rmdir},
6290 {"touch", 1, XS_file_func_touch, pp_file_touch},
6291 {"clear_stat_cache", 1, XS_file_func_clear_stat_cache, pp_file_clear_stat_cache},
6292 {"basename", 1, XS_file_func_basename, pp_file_basename},
6293 {"dirname", 1, XS_file_func_dirname, pp_file_dirname},
6294 {"extname", 1, XS_file_func_extname, pp_file_extname},
6295 {"mtime", 1, XS_file_func_mtime, pp_file_mtime},
6296 {"atime", 1, XS_file_func_atime, pp_file_atime},
6297 {"ctime", 1, XS_file_func_ctime, pp_file_ctime},
6298 {"mode", 1, XS_file_func_mode, pp_file_mode},
6299 {"is_link", 1, XS_file_func_is_link, pp_file_is_link},
6300 {"is_readable", 1, XS_file_func_is_readable, pp_file_is_readable},
6301 {"is_writable", 1, XS_file_func_is_writable, pp_file_is_writable},
6302 {"is_executable", 1, XS_file_func_is_executable, pp_file_is_executable},
6303 {"readdir", 1, XS_file_func_readdir, pp_file_readdir},
6304 /* 2-arg functions */
6305 {"spew", 2, XS_file_func_spew, pp_file_spew},
6306 {"copy", 2, XS_file_func_copy, pp_file_copy},
6307 {"move", 2, XS_file_func_move, pp_file_move},
6308 {"chmod", 2, XS_file_func_chmod, pp_file_chmod},
6309 {"append", 2, XS_file_func_append, pp_file_append},
6310 {"atomic_spew", 2, XS_file_func_atomic_spew, pp_file_atomic_spew},
6311 /* variadic functions (args=0 means plain newXS, no custom op) */
6312 {"join", 0, xs_join, NULL},
6313 {"mkpath", 0, xs_mkpath, NULL},
6314 {"rm_rf", 0, xs_rm_rf, NULL},
6315 {"range_lines", 0, xs_range_lines, NULL},
6316 {NULL, 0, NULL, NULL}
6317 };
6318  
6319 #define IMPORT_FUNCS_COUNT (sizeof(import_funcs) / sizeof(import_funcs[0]) - 1)
6320  
6321 155 static void install_import_entry(pTHX_ const char *pkg, const ImportEntry *e) {
6322 char short_name[256];
6323 155 snprintf(short_name, sizeof(short_name), "file_%s", e->name);
6324 155 ▶ if (e->args == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->args == 0) { 20 135 155
6325 /* Variadic: plain newXS, no custom op */
6326 char full_name[256];
6327 20 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, short_name);
6328 20 newXS(full_name, e->xs_func, __FILE__);
6329 135 ▶ } else if (e->args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (e->args == 1) { 105 30 135
6330 105 install_file_func_1arg(aTHX_ pkg, short_name, e->xs_func, e->pp_func);
6331 } else {
6332 30 install_file_func_2arg(aTHX_ pkg, short_name, e->xs_func, e->pp_func);
6333 }
6334 155 }
6335  
6336 4 static void install_all_imports(pTHX_ const char *pkg) {
6337 int i;
6338 140 ▶ for (i = 0; import_funcs[i].name != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; import_funcs[i].name != NULL; i++) { 136 4 140
6339 136 install_import_entry(aTHX_ pkg, &import_funcs[i]);
6340 }
6341 4 }
6342  
6343 /* file::import - import function-style accessors with custom ops */
6344 37 XS_EXTERNAL(XS_file_import) {
6345 37 dXSARGS;
6346 const char *pkg;
6347 int i, j;
6348  
6349 /* Get caller's package */
6350 37 ▶ pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 0 37 37
pkg = CopSTASHPV(PL_curcop); 0 0 0
pkg = CopSTASHPV(PL_curcop); 37 0 37
pkg = CopSTASHPV(PL_curcop); 0 37 37
6351  
6352 /* No args after package name = no imports */
6353 37 ▶ if (items <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items <= 1) { 27 10 37
6354 27 XSRETURN_EMPTY;
6355 }
6356  
6357 /* Process each requested import */
6358 29 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 23 6 29
6359 STRLEN len;
6360 23 const char *arg = SvPV(ST(i), len);
6361  
6362 /* Check for :all or import (both mean import everything) */
6363 23 ▶ if ((len == 4 && strEQ(arg, ":all")) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((len == 4 && strEQ(arg, ":all")) || 8 15 23
if ((len == 4 && strEQ(arg, ":all")) || 6 2 8
6364 21 ▶ (len == 6 && strEQ(arg, "import"))) {
Branch: 2 branches
BranchTrueFalseTotal
(len == 6 && strEQ(arg, "import"))) { 6 15 21
(len == 6 && strEQ(arg, "import"))) { 2 4 6
6365 4 install_all_imports(aTHX_ pkg);
6366 4 XSRETURN_EMPTY; /* :all means we're done */
6367 }
6368  
6369 /* Look up the requested function */
6370 357 ▶ for (j = 0; import_funcs[j].name != NULL; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; import_funcs[j].name != NULL; j++) { 357 0 357
6371 357 ▶ if (strEQ(arg, import_funcs[j].name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(arg, import_funcs[j].name)) { 19 338 357
6372 19 install_import_entry(aTHX_ pkg, &import_funcs[j]);
6373 19 break;
6374 }
6375 }
6376  
6377 /* If not found, warn but don't die */
6378 19 ▶ if (import_funcs[j].name == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (import_funcs[j].name == NULL) { 0 19 19
6379 0 warn("File::Raw: '%s' is not exported", arg);
6380 }
6381 }
6382  
6383 6 XSRETURN_EMPTY;
6384 }
6385  
6386 /* ============================================
6387 Boot
6388 ============================================ */
6389  
6390 29 XS_EXTERNAL(boot_File__Raw) {
6391 29 dXSBOOTARGSXSAPIVERCHK;
6392 PERL_UNUSED_VAR(items);
6393  
6394 29 file_init(aTHX);
6395  
6396 /* Register custom ops */
6397 29 XopENTRY_set(&file_slurp_xop, xop_name, "file_slurp");
6398 29 XopENTRY_set(&file_slurp_xop, xop_desc, "file slurp");
6399 29 XopENTRY_set(&file_slurp_xop, xop_class, OA_UNOP);
6400 29 Perl_custom_op_register(aTHX_ pp_file_slurp, &file_slurp_xop);
6401  
6402 29 XopENTRY_set(&file_spew_xop, xop_name, "file_spew");
6403 29 XopENTRY_set(&file_spew_xop, xop_desc, "file spew");
6404 29 XopENTRY_set(&file_spew_xop, xop_class, OA_BINOP);
6405 29 Perl_custom_op_register(aTHX_ pp_file_spew, &file_spew_xop);
6406  
6407 29 XopENTRY_set(&file_exists_xop, xop_name, "file_exists");
6408 29 XopENTRY_set(&file_exists_xop, xop_desc, "file exists");
6409 29 XopENTRY_set(&file_exists_xop, xop_class, OA_UNOP);
6410 29 Perl_custom_op_register(aTHX_ pp_file_exists, &file_exists_xop);
6411  
6412 29 XopENTRY_set(&file_size_xop, xop_name, "file_size");
6413 29 XopENTRY_set(&file_size_xop, xop_desc, "file size");
6414 29 XopENTRY_set(&file_size_xop, xop_class, OA_UNOP);
6415 29 Perl_custom_op_register(aTHX_ pp_file_size, &file_size_xop);
6416  
6417 29 XopENTRY_set(&file_is_file_xop, xop_name, "file_is_file");
6418 29 XopENTRY_set(&file_is_file_xop, xop_desc, "file is_file");
6419 29 XopENTRY_set(&file_is_file_xop, xop_class, OA_UNOP);
6420 29 Perl_custom_op_register(aTHX_ pp_file_is_file, &file_is_file_xop);
6421  
6422 29 XopENTRY_set(&file_is_dir_xop, xop_name, "file_is_dir");
6423 29 XopENTRY_set(&file_is_dir_xop, xop_desc, "file is_dir");
6424 29 XopENTRY_set(&file_is_dir_xop, xop_class, OA_UNOP);
6425 29 Perl_custom_op_register(aTHX_ pp_file_is_dir, &file_is_dir_xop);
6426  
6427 29 XopENTRY_set(&file_lines_xop, xop_name, "file_lines");
6428 29 XopENTRY_set(&file_lines_xop, xop_desc, "file lines");
6429 29 XopENTRY_set(&file_lines_xop, xop_class, OA_UNOP);
6430 29 Perl_custom_op_register(aTHX_ pp_file_lines, &file_lines_xop);
6431  
6432 29 XopENTRY_set(&file_unlink_xop, xop_name, "file_unlink");
6433 29 XopENTRY_set(&file_unlink_xop, xop_desc, "file unlink");
6434 29 XopENTRY_set(&file_unlink_xop, xop_class, OA_UNOP);
6435 29 Perl_custom_op_register(aTHX_ pp_file_unlink, &file_unlink_xop);
6436  
6437 29 XopENTRY_set(&file_mkdir_xop, xop_name, "file_mkdir");
6438 29 XopENTRY_set(&file_mkdir_xop, xop_desc, "file mkdir");
6439 29 XopENTRY_set(&file_mkdir_xop, xop_class, OA_UNOP);
6440 29 Perl_custom_op_register(aTHX_ pp_file_mkdir, &file_mkdir_xop);
6441  
6442 29 XopENTRY_set(&file_rmdir_xop, xop_name, "file_rmdir");
6443 29 XopENTRY_set(&file_rmdir_xop, xop_desc, "file rmdir");
6444 29 XopENTRY_set(&file_rmdir_xop, xop_class, OA_UNOP);
6445 29 Perl_custom_op_register(aTHX_ pp_file_rmdir, &file_rmdir_xop);
6446  
6447 29 XopENTRY_set(&file_touch_xop, xop_name, "file_touch");
6448 29 XopENTRY_set(&file_touch_xop, xop_desc, "file touch");
6449 29 XopENTRY_set(&file_touch_xop, xop_class, OA_UNOP);
6450 29 Perl_custom_op_register(aTHX_ pp_file_touch, &file_touch_xop);
6451  
6452 29 XopENTRY_set(&file_clear_stat_cache_xop, xop_name, "file_clear_stat_cache");
6453 29 XopENTRY_set(&file_clear_stat_cache_xop, xop_desc, "clear stat cache");
6454 29 XopENTRY_set(&file_clear_stat_cache_xop, xop_class, OA_BASEOP);
6455 29 Perl_custom_op_register(aTHX_ pp_file_clear_stat_cache, &file_clear_stat_cache_xop);
6456  
6457 29 XopENTRY_set(&file_basename_xop, xop_name, "file_basename");
6458 29 XopENTRY_set(&file_basename_xop, xop_desc, "file basename");
6459 29 XopENTRY_set(&file_basename_xop, xop_class, OA_UNOP);
6460 29 Perl_custom_op_register(aTHX_ pp_file_basename, &file_basename_xop);
6461  
6462 29 XopENTRY_set(&file_dirname_xop, xop_name, "file_dirname");
6463 29 XopENTRY_set(&file_dirname_xop, xop_desc, "file dirname");
6464 29 XopENTRY_set(&file_dirname_xop, xop_class, OA_UNOP);
6465 29 Perl_custom_op_register(aTHX_ pp_file_dirname, &file_dirname_xop);
6466  
6467 29 XopENTRY_set(&file_extname_xop, xop_name, "file_extname");
6468 29 XopENTRY_set(&file_extname_xop, xop_desc, "file extname");
6469 29 XopENTRY_set(&file_extname_xop, xop_class, OA_UNOP);
6470 29 Perl_custom_op_register(aTHX_ pp_file_extname, &file_extname_xop);
6471  
6472 29 XopENTRY_set(&file_mtime_xop, xop_name, "file_mtime");
6473 29 XopENTRY_set(&file_mtime_xop, xop_desc, "file mtime");
6474 29 XopENTRY_set(&file_mtime_xop, xop_class, OA_UNOP);
6475 29 Perl_custom_op_register(aTHX_ pp_file_mtime, &file_mtime_xop);
6476  
6477 29 XopENTRY_set(&file_atime_xop, xop_name, "file_atime");
6478 29 XopENTRY_set(&file_atime_xop, xop_desc, "file atime");
6479 29 XopENTRY_set(&file_atime_xop, xop_class, OA_UNOP);
6480 29 Perl_custom_op_register(aTHX_ pp_file_atime, &file_atime_xop);
6481  
6482 29 XopENTRY_set(&file_ctime_xop, xop_name, "file_ctime");
6483 29 XopENTRY_set(&file_ctime_xop, xop_desc, "file ctime");
6484 29 XopENTRY_set(&file_ctime_xop, xop_class, OA_UNOP);
6485 29 Perl_custom_op_register(aTHX_ pp_file_ctime, &file_ctime_xop);
6486  
6487 29 XopENTRY_set(&file_mode_xop, xop_name, "file_mode");
6488 29 XopENTRY_set(&file_mode_xop, xop_desc, "file mode");
6489 29 XopENTRY_set(&file_mode_xop, xop_class, OA_UNOP);
6490 29 Perl_custom_op_register(aTHX_ pp_file_mode, &file_mode_xop);
6491  
6492 29 XopENTRY_set(&file_is_link_xop, xop_name, "file_is_link");
6493 29 XopENTRY_set(&file_is_link_xop, xop_desc, "file is_link");
6494 29 XopENTRY_set(&file_is_link_xop, xop_class, OA_UNOP);
6495 29 Perl_custom_op_register(aTHX_ pp_file_is_link, &file_is_link_xop);
6496  
6497 29 XopENTRY_set(&file_is_readable_xop, xop_name, "file_is_readable");
6498 29 XopENTRY_set(&file_is_readable_xop, xop_desc, "file is_readable");
6499 29 XopENTRY_set(&file_is_readable_xop, xop_class, OA_UNOP);
6500 29 Perl_custom_op_register(aTHX_ pp_file_is_readable, &file_is_readable_xop);
6501  
6502 29 XopENTRY_set(&file_is_writable_xop, xop_name, "file_is_writable");
6503 29 XopENTRY_set(&file_is_writable_xop, xop_desc, "file is_writable");
6504 29 XopENTRY_set(&file_is_writable_xop, xop_class, OA_UNOP);
6505 29 Perl_custom_op_register(aTHX_ pp_file_is_writable, &file_is_writable_xop);
6506  
6507 29 XopENTRY_set(&file_is_executable_xop, xop_name, "file_is_executable");
6508 29 XopENTRY_set(&file_is_executable_xop, xop_desc, "file is_executable");
6509 29 XopENTRY_set(&file_is_executable_xop, xop_class, OA_UNOP);
6510 29 Perl_custom_op_register(aTHX_ pp_file_is_executable, &file_is_executable_xop);
6511  
6512 29 XopENTRY_set(&file_readdir_xop, xop_name, "file_readdir");
6513 29 XopENTRY_set(&file_readdir_xop, xop_desc, "file readdir");
6514 29 XopENTRY_set(&file_readdir_xop, xop_class, OA_UNOP);
6515 29 Perl_custom_op_register(aTHX_ pp_file_readdir, &file_readdir_xop);
6516  
6517 29 XopENTRY_set(&file_slurp_raw_xop, xop_name, "file_slurp_raw");
6518 29 XopENTRY_set(&file_slurp_raw_xop, xop_desc, "file slurp_raw");
6519 29 XopENTRY_set(&file_slurp_raw_xop, xop_class, OA_UNOP);
6520 29 Perl_custom_op_register(aTHX_ pp_file_slurp_raw, &file_slurp_raw_xop);
6521  
6522 29 XopENTRY_set(&file_copy_xop, xop_name, "file_copy");
6523 29 XopENTRY_set(&file_copy_xop, xop_desc, "file copy");
6524 29 XopENTRY_set(&file_copy_xop, xop_class, OA_BINOP);
6525 29 Perl_custom_op_register(aTHX_ pp_file_copy, &file_copy_xop);
6526  
6527 29 XopENTRY_set(&file_move_xop, xop_name, "file_move");
6528 29 XopENTRY_set(&file_move_xop, xop_desc, "file move");
6529 29 XopENTRY_set(&file_move_xop, xop_class, OA_BINOP);
6530 29 Perl_custom_op_register(aTHX_ pp_file_move, &file_move_xop);
6531  
6532 29 XopENTRY_set(&file_chmod_xop, xop_name, "file_chmod");
6533 29 XopENTRY_set(&file_chmod_xop, xop_desc, "file chmod");
6534 29 XopENTRY_set(&file_chmod_xop, xop_class, OA_BINOP);
6535 29 Perl_custom_op_register(aTHX_ pp_file_chmod, &file_chmod_xop);
6536  
6537 29 XopENTRY_set(&file_append_xop, xop_name, "file_append");
6538 29 XopENTRY_set(&file_append_xop, xop_desc, "file append");
6539 29 XopENTRY_set(&file_append_xop, xop_class, OA_BINOP);
6540 29 Perl_custom_op_register(aTHX_ pp_file_append, &file_append_xop);
6541  
6542 29 XopENTRY_set(&file_atomic_spew_xop, xop_name, "file_atomic_spew");
6543 29 XopENTRY_set(&file_atomic_spew_xop, xop_desc, "file atomic_spew");
6544 29 XopENTRY_set(&file_atomic_spew_xop, xop_class, OA_BINOP);
6545 29 Perl_custom_op_register(aTHX_ pp_file_atomic_spew, &file_atomic_spew_xop);
6546  
6547 /* Install functions with call checker for custom op optimization */
6548 {
6549 CV *cv;
6550 SV *ckobj;
6551  
6552 /* 1-arg functions with call checker */
6553 29 cv = newXS("File::Raw::size", xs_size, __FILE__);
6554 29 ckobj = newSViv(PTR2IV(pp_file_size));
6555 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6556  
6557 29 cv = newXS("File::Raw::mtime", xs_mtime, __FILE__);
6558 29 ckobj = newSViv(PTR2IV(pp_file_mtime));
6559 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6560  
6561 29 cv = newXS("File::Raw::atime", xs_atime, __FILE__);
6562 29 ckobj = newSViv(PTR2IV(pp_file_atime));
6563 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6564  
6565 29 cv = newXS("File::Raw::ctime", xs_ctime, __FILE__);
6566 29 ckobj = newSViv(PTR2IV(pp_file_ctime));
6567 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6568  
6569 29 cv = newXS("File::Raw::mode", xs_mode, __FILE__);
6570 29 ckobj = newSViv(PTR2IV(pp_file_mode));
6571 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6572  
6573 29 cv = newXS("File::Raw::exists", xs_exists, __FILE__);
6574 29 ckobj = newSViv(PTR2IV(pp_file_exists));
6575 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6576  
6577 29 cv = newXS("File::Raw::is_file", xs_is_file, __FILE__);
6578 29 ckobj = newSViv(PTR2IV(pp_file_is_file));
6579 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6580  
6581 29 cv = newXS("File::Raw::is_dir", xs_is_dir, __FILE__);
6582 29 ckobj = newSViv(PTR2IV(pp_file_is_dir));
6583 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6584  
6585 29 cv = newXS("File::Raw::is_link", xs_is_link, __FILE__);
6586 29 ckobj = newSViv(PTR2IV(pp_file_is_link));
6587 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6588  
6589 29 cv = newXS("File::Raw::is_readable", xs_is_readable, __FILE__);
6590 29 ckobj = newSViv(PTR2IV(pp_file_is_readable));
6591 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6592  
6593 29 cv = newXS("File::Raw::is_writable", xs_is_writable, __FILE__);
6594 29 ckobj = newSViv(PTR2IV(pp_file_is_writable));
6595 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6596  
6597 29 cv = newXS("File::Raw::is_executable", xs_is_executable, __FILE__);
6598 29 ckobj = newSViv(PTR2IV(pp_file_is_executable));
6599 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6600  
6601 /* File manipulation - 1-arg */
6602 29 cv = newXS("File::Raw::unlink", xs_unlink, __FILE__);
6603 29 ckobj = newSViv(PTR2IV(pp_file_unlink));
6604 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6605  
6606 29 cv = newXS("File::Raw::mkdir", xs_mkdir, __FILE__);
6607 29 ckobj = newSViv(PTR2IV(pp_file_mkdir));
6608 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6609  
6610 29 cv = newXS("File::Raw::rmdir", xs_rmdir, __FILE__);
6611 29 ckobj = newSViv(PTR2IV(pp_file_rmdir));
6612 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6613  
6614 29 cv = newXS("File::Raw::touch", xs_touch, __FILE__);
6615 29 ckobj = newSViv(PTR2IV(pp_file_touch));
6616 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6617  
6618 29 cv = newXS("File::Raw::clear_stat_cache", xs_clear_stat_cache, __FILE__);
6619 29 ckobj = newSViv(PTR2IV(pp_file_clear_stat_cache));
6620 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6621  
6622 29 cv = newXS("File::Raw::basename", xs_basename, __FILE__);
6623 29 ckobj = newSViv(PTR2IV(pp_file_basename));
6624 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6625  
6626 29 cv = newXS("File::Raw::dirname", xs_dirname, __FILE__);
6627 29 ckobj = newSViv(PTR2IV(pp_file_dirname));
6628 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6629  
6630 29 cv = newXS("File::Raw::extname", xs_extname, __FILE__);
6631 29 ckobj = newSViv(PTR2IV(pp_file_extname));
6632 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6633  
6634 29 cv = newXS("File::Raw::slurp", xs_slurp, __FILE__);
6635 29 ckobj = newSViv(PTR2IV(pp_file_slurp));
6636 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6637  
6638 29 cv = newXS("File::Raw::slurp_raw", xs_slurp_raw, __FILE__);
6639 29 ckobj = newSViv(PTR2IV(pp_file_slurp_raw));
6640 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6641  
6642 29 cv = newXS("File::Raw::lines", xs_lines, __FILE__);
6643 29 ckobj = newSViv(PTR2IV(pp_file_lines));
6644 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6645  
6646 29 cv = newXS("File::Raw::readdir", xs_readdir, __FILE__);
6647 29 ckobj = newSViv(PTR2IV(pp_file_readdir));
6648 29 cv_set_call_checker(cv, file_call_checker_1arg, ckobj);
6649  
6650 /* 2-arg functions with call checker */
6651 29 cv = newXS("File::Raw::spew", xs_spew, __FILE__);
6652 29 ckobj = newSViv(PTR2IV(pp_file_spew));
6653 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6654  
6655 29 cv = newXS("File::Raw::append", xs_append, __FILE__);
6656 29 ckobj = newSViv(PTR2IV(pp_file_append));
6657 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6658  
6659 29 cv = newXS("File::Raw::copy", xs_copy, __FILE__);
6660 29 ckobj = newSViv(PTR2IV(pp_file_copy));
6661 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6662  
6663 29 cv = newXS("File::Raw::move", xs_move, __FILE__);
6664 29 ckobj = newSViv(PTR2IV(pp_file_move));
6665 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6666  
6667 29 cv = newXS("File::Raw::chmod", xs_chmod, __FILE__);
6668 29 ckobj = newSViv(PTR2IV(pp_file_chmod));
6669 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6670  
6671 29 cv = newXS("File::Raw::atomic_spew", xs_atomic_spew, __FILE__);
6672 29 ckobj = newSViv(PTR2IV(pp_file_atomic_spew));
6673 29 cv_set_call_checker(cv, file_call_checker_2arg, ckobj);
6674 }
6675  
6676 /* Functions without custom op optimization */
6677 29 newXS("File::Raw::join", xs_join, __FILE__);
6678 29 newXS("File::Raw::mkpath", xs_mkpath, __FILE__);
6679 29 newXS("File::Raw::rm_rf", xs_rm_rf, __FILE__);
6680 29 newXS("File::Raw::each_line", xs_each_line, __FILE__);
6681 29 newXS("File::Raw::grep_lines", xs_grep_lines, __FILE__);
6682 29 newXS("File::Raw::count_lines", xs_count_lines, __FILE__);
6683 29 newXS("File::Raw::find_line", xs_find_line, __FILE__);
6684 29 newXS("File::Raw::map_lines", xs_map_lines, __FILE__);
6685  
6686 /* Plugin API */
6687 29 newXS("File::Raw::register_plugin", xs_register_plugin, __FILE__);
6688 29 newXS("File::Raw::unregister_plugin", xs_unregister_plugin, __FILE__);
6689 29 newXS("File::Raw::list_plugins", xs_list_plugins, __FILE__);
6690 29 newXS("File::Raw::register_predicate", xs_register_predicate, __FILE__);
6691 29 newXS("File::Raw::list_predicates", xs_list_predicates, __FILE__);
6692  
6693 /* Built-in 'predicate' plugin: routes the eight built-in line
6694 * predicates and any user-registered ones through plugin dispatch.
6695 * Initialise the predicate storage first so the plugin's record fn
6696 * always sees a populated registry. */
6697 29 file_init_callback_registry(aTHX);
6698 29 ▶ if (file_register_plugin(aTHX_ &g_predicate_plugin) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_register_plugin(aTHX_ &g_predicate_plugin) != 1) { 0 29 29
6699 0 croak("File::Raw boot: failed to register built-in 'predicate' plugin");
6700 }
6701  
6702 /* Combined stat - all attributes in one syscall */
6703 29 newXS("File::Raw::stat", xs_stat_all, __FILE__);
6704  
6705 /* Head and tail */
6706 29 newXS("File::Raw::head", xs_head, __FILE__);
6707 29 newXS("File::Raw::tail", xs_tail, __FILE__);
6708 29 newXS("File::Raw::range_lines", xs_range_lines, __FILE__);
6709  
6710 /* Import function */
6711 29 newXS("File::Raw::import", XS_file_import, __FILE__);
6712  
6713 /* Memory-mapped files */
6714 29 newXS("File::Raw::mmap_open", xs_mmap_open, __FILE__);
6715 29 newXS("File::Raw::mmap::data", xs_mmap_data, __FILE__);
6716 29 newXS("File::Raw::mmap::sync", xs_mmap_sync, __FILE__);
6717 29 newXS("File::Raw::mmap::close", xs_mmap_close, __FILE__);
6718 29 newXS("File::Raw::mmap::DESTROY", xs_mmap_DESTROY, __FILE__);
6719  
6720 /* Line iterators */
6721 29 newXS("File::Raw::lines_iter", xs_lines_iter, __FILE__);
6722 29 newXS("File::Raw::lines::next", xs_lines_iter_next, __FILE__);
6723 29 newXS("File::Raw::lines::eof", xs_lines_iter_eof, __FILE__);
6724 29 newXS("File::Raw::lines::close", xs_lines_iter_close, __FILE__);
6725 29 newXS("File::Raw::lines::DESTROY", xs_lines_iter_DESTROY, __FILE__);
6726  
6727 /* Chunk iterators: pull reads. eof, close and DESTROY are the line
6728 * iterator's - same fields, same slot, same free path. */
6729 29 newXS("File::Raw::chunk_iter", xs_chunk_iter, __FILE__);
6730 29 newXS("File::Raw::chunks::next", xs_chunk_iter_next, __FILE__);
6731 29 newXS("File::Raw::chunks::eof", xs_lines_iter_eof, __FILE__);
6732 29 newXS("File::Raw::chunks::close", xs_lines_iter_close, __FILE__);
6733 29 newXS("File::Raw::chunks::DESTROY", xs_lines_iter_DESTROY, __FILE__);
6734  
6735 /* Register cleanup for global destruction */
6736 29 Perl_call_atexit(aTHX_ file_cleanup_callback_registry, NULL);
6737  
6738 #if PERL_REVISION > 5 || (PERL_REVISION == 5 && PERL_VERSION >= 22)
6739 29 Perl_xs_boot_epilog(aTHX_ ax);
6740 #else
6741 XSRETURN_YES;
6742 #endif
6743 29 }
« blib/arch/File/Raw/Install/Files.pm ↑ Summary blib/lib/File/Raw.pm »