file.c

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