file.c

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