FileCheck.xs

filter: Statementstmt 93.8% 199 / 212 Branchbran 47.1% 100 / 212 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.5% 299 / 424 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Overload/FileCheck.pm ↑ Summary
1 /*
2 *
3 * Copyright (c) 2018, cPanel, LLC.
4 * All rights reserved.
5 * http://cpanel.net
6 *
7 * This is free software; you can redistribute it and/or modify it under the
8 * same terms as Perl itself.
9 *
10 */
11  
12 #include <EXTERN.h>
13 #include <perl.h>
14 #include <XSUB.h>
15 #include <embed.h>
16  
17 #define NEED_sv_2pv_flags
18 #include "ppport.h"
19  
20 #include "FileCheck.h"
21  
22 /*
23 * Macro to make the moking process easier
24 * for now keep them there, so we can hack them in the same file
25 */
26  
27 /* generic macro with args */
28 #define _CALL_REAL_PP(zOP) (* ( gl_overload_ft->op[zOP].real_pp ) )(aTHX)
29 #define _RETURN_CALL_REAL_PP_IF_UNMOCK(zOP) if (!gl_overload_ft->op[zOP].is_mocked) return _CALL_REAL_PP(zOP);
30  
31 /* simplified versions for our custom usage */
32 #define CALL_REAL_OP() _CALL_REAL_PP(PL_op->op_type)
33 #define RETURN_CALL_REAL_OP_IF_UNMOCK() _RETURN_CALL_REAL_PP_IF_UNMOCK(PL_op->op_type)
34  
35 #define INIT_FILECHECK_MOCK(op_name, op_type, f) \
36 newCONSTSUB(stash, op_name, newSViv(op_type) ); \
37 gl_overload_ft->op[op_type].real_pp = PL_ppaddr[op_type]; \
38 PL_ppaddr[op_type] = f;
39  
40 /* a Stat_t struct has 13 elements */
41 #define STAT_T_MAX 13
42  
43 /* ----------- start there --------------- */
44  
45 OverloadFTOps *gl_overload_ft = 0;
46  
47 /*
48 * common helper to callback the pure perl function Overload::FileCheck::_check
49 * and get the mocked value for the -X check
50 *
51 * 1 check is true -> OP returns Yes
52 * 0 check is false -> OP returns No
53 * TODO: -> OP returns undef
54 * -1 fallback to the original OP
55 */
56 1094 int _overload_ft_ops() {
57 1094 SV *const arg = *PL_stack_sp;
58 1094 int optype = PL_op->op_type; /* this is the current op_type we are mocking */
59 1094 int check_status = -1; /* 1 -> YES ; 0 -> FALSE ; -1 -> delegate */
60  
61 1094 dSP;
62 int count;
63  
64 1094 ENTER;
65 1094 SAVETMPS;
66  
67 1094 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1094 1094
68 1094 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1094 1094
69 1094 PUSHs(sv_2mortal(newSViv(optype)));
70 1094 PUSHs(arg);
71 1094 PUTBACK;
72  
73 1094 count = call_pv("Overload::FileCheck::_check", G_SCALAR);
74  
75 1094 SPAGAIN;
76  
77 1094 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 1094 1094
78 0 croak("No return value from Overload::FileCheck::_check for OP #%d\n", optype);
79  
80 1094 check_status = POPi; /* TOOO pop on SV* for true / false & co */
81  
82 /* printf ("######## The result is %d /// OPTYPE is %d\n", check_status, optype); */
83  
84 1094 PUTBACK;
85 1094 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1094 0 1094
86 1094 LEAVE;
87  
88 1094 return check_status;
89 }
90  
91 109 SV* _overload_ft_ops_sv() {
92 109 SV *const arg = *PL_stack_sp;
93 109 int optype = PL_op->op_type; /* this is the current op_type we are mocking */
94 SV *status; /* 1 -> YES ; 0 -> FALSE ; -1 -> delegate */
95  
96 109 dSP;
97 int count;
98  
99 109 ENTER;
100 109 SAVETMPS;
101  
102 109 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 109 109
103 109 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 109 109
104 109 PUSHs(sv_2mortal(newSViv(optype)));
105 109 PUSHs(arg);
106 109 PUTBACK;
107  
108 109 count = call_pv("Overload::FileCheck::_check", G_SCALAR);
109  
110 109 SPAGAIN;
111  
112 109 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 109 109
113 0 croak("No return value from Overload::FileCheck::_check for OP #%d\n", optype);
114  
115 109 status = POPs;
116 109 SvREFCNT_inc( status );
117  
118 /* printf ("######## The result is %d /// OPTYPE is %d\n", check_status, optype); */
119  
120 109 PUTBACK;
121 109 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 109 0 109
122 109 LEAVE;
123  
124 109 return status;
125 }
126  
127 /*
128 * view perldoc to call SVs, method, ...
129 *
130 * https://perldoc.perl.org/perlcall.html
131 *
132 * but also https://perldoc.perl.org/perlguts.html
133 */
134  
135 #define set_stat_from_aryix(st, ix) \
136 rsv = ary[ix]; \
137 if (SvROK(rsv)) croak("Overload::FileCheck - Item %d should not be one RV\n", ix); \
138 if (SvIOK(rsv)) st = SvIV( rsv ); \
139 else if (SvUOK(rsv)) st = SvUV( rsv ); \
140 else if (SvNOK(rsv)) st = SvNV( rsv ); \
141 else croak("Overload::FileCheck - Item %d is not numeric...\n", ix);
142  
143  
144 /*
145 * similar to _overload_ft_ops but expect more args from _check
146 * which returns values for a fake stat
147 *
148 * Note: we could also call a dedicated function as _check_stat
149 */
150 676 int _overload_ft_stat(Stat_t *stat, int *size) {
151 676 SV *const arg = *PL_stack_sp;
152 676 int optype = PL_op->op_type; /* this is the current op_type we are mocking */
153 676 int check_status = -1; /* 1 -> YES ; 0 -> FALSE ; -1 -> delegate */
154  
155 676 dSP;
156 int count;
157 SV *sv;
158  
159 676 ENTER;
160 676 SAVETMPS;
161  
162 676 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 676 676
163 676 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 676 676
164 676 PUSHs(sv_2mortal(newSViv(optype)));
165 676 PUSHs(arg);
166 676 PUTBACK;
167  
168 676 count = call_pv("Overload::FileCheck::_check", G_ARRAY);
169  
170 668 SPAGAIN;
171  
172 668 ▶ if (count < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count < 1) 0 668 668
173 0 croak("Overload::FileCheck::_check for stat OP #%d should return at least one SV.\n", optype);
174 668 ▶ if (count > 2)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 2) 0 668 668
175 0 croak("Overload::FileCheck::_check for stat OP #%d should return no more than two SVs.\n", optype);
176  
177 /* popping the stack from last entry to first */
178 668 ▶ if (count == 2) sv = POPs; /* RvAV */
Branch: 1 branch
BranchTrueFalseTotal
if (count == 2) sv = POPs; /* RvAV */ 665 3 668
179 668 check_status = POPi; /* TOOO pop on SV* for true / false & co */
180  
181 668 *size = 0; /* by default it fails */
182  
183 668 ▶ if ( check_status == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( check_status == 1 ) { 665 3 668
184 AV *stat_array;
185 SV **ary;
186 SV *rsv;
187 int av_size;
188  
189 665 ▶ if (count != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 2) 0 665 665
190 0 croak("Overload::FileCheck::_check for stat OP #%d should return two SVs on success.\n", optype);
191  
192 665 ▶ if ( ! SvROK(sv) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvROK(sv) ) 0 665 665
193 0 croak( "Overload::FileCheck::_check need to return an array ref" );
194  
195 665 stat_array = MUTABLE_AV( SvRV( sv ) );
196 665 ▶ if ( SvTYPE(stat_array) != SVt_PVAV )
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE(stat_array) != SVt_PVAV ) 0 665 665
197 0 croak( "Overload::FileCheck::_check need to return an array ref" );
198  
199 665 av_size = AvFILL(stat_array);
200 665 ▶ if ( av_size > 0 && av_size != ( STAT_T_MAX - 1 ) )
Branch: 2 branches
BranchTrueFalseTotal
if ( av_size > 0 && av_size != ( STAT_T_MAX - 1 ) ) 590 75 665
if ( av_size > 0 && av_size != ( STAT_T_MAX - 1 ) ) 0 590 590
201 0 croak( "Overload::FileCheck::_check: Array should contain 13 elements" );
202  
203 665 ▶ if ( av_size > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( av_size > 0 ) { 590 75 665
204 590 *size = av_size; /* store the av_size */
205  
206 590 ary = AvARRAY(stat_array);
207  
208 /* fill the stat struct */
209 590 ▶ set_stat_from_aryix( stat->st_dev, 0 ); /* IV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_dev, 0 ); /* IV */ 0 590 590
set_stat_from_aryix( stat->st_dev, 0 ); /* IV */ 586 4 590
set_stat_from_aryix( stat->st_dev, 0 ); /* IV */ 0 4 4
set_stat_from_aryix( stat->st_dev, 0 ); /* IV */ 0 4 4
210 586 ▶ set_stat_from_aryix( stat->st_ino, 1 ); /* IV or UV : neg = PL_statcache.st_ino < 0 */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_ino, 1 ); /* IV or UV : neg = PL_statcache.st_ino < 0 */ 0 586 586
set_stat_from_aryix( stat->st_ino, 1 ); /* IV or UV : neg = PL_statcache.st_ino < 0 */ 586 0 586
set_stat_from_aryix( stat->st_ino, 1 ); /* IV or UV : neg = PL_statcache.st_ino < 0 */ 0 0 0
set_stat_from_aryix( stat->st_ino, 1 ); /* IV or UV : neg = PL_statcache.st_ino < 0 */ 0 0 0
211 586 ▶ set_stat_from_aryix( stat->st_mode, 2 ); /* UV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_mode, 2 ); /* UV */ 0 586 586
set_stat_from_aryix( stat->st_mode, 2 ); /* UV */ 586 0 586
set_stat_from_aryix( stat->st_mode, 2 ); /* UV */ 0 0 0
set_stat_from_aryix( stat->st_mode, 2 ); /* UV */ 0 0 0
212 586 ▶ set_stat_from_aryix( stat->st_nlink, 3 ); /* UV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_nlink, 3 ); /* UV */ 0 586 586
set_stat_from_aryix( stat->st_nlink, 3 ); /* UV */ 586 0 586
set_stat_from_aryix( stat->st_nlink, 3 ); /* UV */ 0 0 0
set_stat_from_aryix( stat->st_nlink, 3 ); /* UV */ 0 0 0
213 586 ▶ set_stat_from_aryix( stat->st_uid, 4 ); /* IV ? */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_uid, 4 ); /* IV ? */ 0 586 586
set_stat_from_aryix( stat->st_uid, 4 ); /* IV ? */ 586 0 586
set_stat_from_aryix( stat->st_uid, 4 ); /* IV ? */ 0 0 0
set_stat_from_aryix( stat->st_uid, 4 ); /* IV ? */ 0 0 0
214 586 ▶ set_stat_from_aryix( stat->st_gid, 5 ); /* IV ? */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_gid, 5 ); /* IV ? */ 0 586 586
set_stat_from_aryix( stat->st_gid, 5 ); /* IV ? */ 586 0 586
set_stat_from_aryix( stat->st_gid, 5 ); /* IV ? */ 0 0 0
set_stat_from_aryix( stat->st_gid, 5 ); /* IV ? */ 0 0 0
215 586 ▶ set_stat_from_aryix( stat->st_rdev, 6 ); /* IV or PV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_rdev, 6 ); /* IV or PV */ 0 586 586
set_stat_from_aryix( stat->st_rdev, 6 ); /* IV or PV */ 586 0 586
set_stat_from_aryix( stat->st_rdev, 6 ); /* IV or PV */ 0 0 0
set_stat_from_aryix( stat->st_rdev, 6 ); /* IV or PV */ 0 0 0
216 586 ▶ set_stat_from_aryix( stat->st_size, 7 ); /* NV or IV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_size, 7 ); /* NV or IV */ 0 586 586
set_stat_from_aryix( stat->st_size, 7 ); /* NV or IV */ 586 0 586
set_stat_from_aryix( stat->st_size, 7 ); /* NV or IV */ 0 0 0
set_stat_from_aryix( stat->st_size, 7 ); /* NV or IV */ 0 0 0
217 586 ▶ set_stat_from_aryix( stat->st_atime, 8 ); /* NV or IV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_atime, 8 ); /* NV or IV */ 0 586 586
set_stat_from_aryix( stat->st_atime, 8 ); /* NV or IV */ 586 0 586
set_stat_from_aryix( stat->st_atime, 8 ); /* NV or IV */ 0 0 0
set_stat_from_aryix( stat->st_atime, 8 ); /* NV or IV */ 0 0 0
218 586 ▶ set_stat_from_aryix( stat->st_mtime, 9 ); /* NV or IV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_mtime, 9 ); /* NV or IV */ 0 586 586
set_stat_from_aryix( stat->st_mtime, 9 ); /* NV or IV */ 586 0 586
set_stat_from_aryix( stat->st_mtime, 9 ); /* NV or IV */ 0 0 0
set_stat_from_aryix( stat->st_mtime, 9 ); /* NV or IV */ 0 0 0
219 586 ▶ set_stat_from_aryix( stat->st_ctime, 10 ); /* NV or IV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_ctime, 10 ); /* NV or IV */ 0 586 586
set_stat_from_aryix( stat->st_ctime, 10 ); /* NV or IV */ 586 0 586
set_stat_from_aryix( stat->st_ctime, 10 ); /* NV or IV */ 0 0 0
set_stat_from_aryix( stat->st_ctime, 10 ); /* NV or IV */ 0 0 0
220 586 ▶ set_stat_from_aryix( stat->st_blksize, 11 ); /* UV or PV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_blksize, 11 ); /* UV or PV */ 0 586 586
set_stat_from_aryix( stat->st_blksize, 11 ); /* UV or PV */ 586 0 586
set_stat_from_aryix( stat->st_blksize, 11 ); /* UV or PV */ 0 0 0
set_stat_from_aryix( stat->st_blksize, 11 ); /* UV or PV */ 0 0 0
221 586 ▶ set_stat_from_aryix( stat->st_blocks, 12 ); /* UV or PV */
Branch: 4 branches
BranchTrueFalseTotal
set_stat_from_aryix( stat->st_blocks, 12 ); /* UV or PV */ 0 586 586
set_stat_from_aryix( stat->st_blocks, 12 ); /* UV or PV */ 584 2 586
set_stat_from_aryix( stat->st_blocks, 12 ); /* UV or PV */ 0 2 2
set_stat_from_aryix( stat->st_blocks, 12 ); /* UV or PV */ 0 2 2
222 }
223  
224 }
225  
226 662 PUTBACK;
227 662 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 662 0 662
228 662 LEAVE;
229  
230 662 return check_status;
231 }
232  
233 /* a generic OP to overload the FT OPs returning yes or no */
234 /* FIXME also need to handle undef */
235 2042 PP(pp_overload_ft_yes_no) {
236 int check_status;
237  
238 assert( gl_overload_ft );
239  
240 /* not currently mocked */
241 2042 ▶ RETURN_CALL_REAL_OP_IF_UNMOCK();
Branch: 1 branch
BranchTrueFalseTotal
RETURN_CALL_REAL_OP_IF_UNMOCK(); 995 1047 2042
242  
243 //Perl_warn(".... continue" );
244  
245 1047 check_status = _overload_ft_ops();
246  
247 {
248 FT_SETUP_dSP_IF_NEEDED;
249  
250 1047 ▶ if ( check_status == 1 ) FT_RETURNYES;
Branch: 1 branch
BranchTrueFalseTotal
if ( check_status == 1 ) FT_RETURNYES; 367 680 1047
251 680 ▶ if ( check_status == 0 ) FT_RETURNNO;
Branch: 1 branch
BranchTrueFalseTotal
if ( check_status == 0 ) FT_RETURNNO; 516 164 680
252 /* if ( check_status == -1 ) FT_RETURNUNDEF; */ /* TODO */
253 }
254  
255 /* fallback */
256 164 return CALL_REAL_OP();
257 }
258  
259 77 PP(pp_overload_ft_int) {
260 int check_status;
261  
262 assert( gl_overload_ft );
263  
264 /* not currently mocked */
265 77 ▶ RETURN_CALL_REAL_OP_IF_UNMOCK();
Branch: 1 branch
BranchTrueFalseTotal
RETURN_CALL_REAL_OP_IF_UNMOCK(); 30 47 77
266 47 check_status = _overload_ft_ops();
267  
268 /* SETERRNO(EEXIST,RMS_FEX); */ /* TODO */
269 47 ▶ if ( check_status == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_status == -1 ) 11 36 47
270 11 return CALL_REAL_OP();
271  
272 {
273 36 dTARGET;
274 FT_SETUP_dSP_IF_NEEDED;
275  
276 /* TODO this is over simplistic some OPs can return one NV instead of IV */
277 36 sv_setiv(TARG, (IV) check_status);
278 36 FT_RETURN_TARG;
279 }
280 }
281  
282 190 PP(pp_overload_ft_nv) {
283 SV *status;
284  
285 assert( gl_overload_ft );
286  
287 /* not currently mocked */
288 190 ▶ RETURN_CALL_REAL_OP_IF_UNMOCK();
Branch: 1 branch
BranchTrueFalseTotal
RETURN_CALL_REAL_OP_IF_UNMOCK(); 81 109 190
289 109 status = _overload_ft_ops_sv();
290  
291 109 ▶ if ( SvIOK(status) && SvIV(status) == -1 )
Branch: 2 branches
BranchTrueFalseTotal
if ( SvIOK(status) && SvIV(status) == -1 ) 39 70 109
if ( SvIOK(status) && SvIV(status) == -1 ) 0 39 39
292 0 return CALL_REAL_OP();
293  
294 109 ▶ if ( SvNOK(status) && SvNV(status) == -1 )
Branch: 2 branches
BranchTrueFalseTotal
if ( SvNOK(status) && SvNV(status) == -1 ) 97 12 109
if ( SvNOK(status) && SvNV(status) == -1 ) 16 81 97
295 16 return CALL_REAL_OP();
296  
297 {
298 93 dTARGET;
299 FT_SETUP_dSP_IF_NEEDED;
300  
301 93 ▶ if ( SvNOK(status) )
Branch: 1 branch
BranchTrueFalseTotal
if ( SvNOK(status) ) 81 12 93
302 81 sv_setnv(TARG, (NV) SvNV(status) );
303 12 ▶ else if ( SvIOK(status) )
Branch: 1 branch
BranchTrueFalseTotal
else if ( SvIOK(status) ) 12 0 12
304 12 sv_setiv(TARG, (IV) SvIV(status) );
305  
306 93 FT_RETURN_TARG;
307 }
308 }
309  
310 774 PP(pp_overload_stat) { /* stat & lstat */
311 774 Stat_t mocked_stat = { 0 }; /* fake stats */
312 774 int check_status = 0;
313 int size;
314  
315  
316 assert( gl_overload_ft );
317  
318 /* not currently mocked */
319 774 ▶ RETURN_CALL_REAL_OP_IF_UNMOCK();
Branch: 1 branch
BranchTrueFalseTotal
RETURN_CALL_REAL_OP_IF_UNMOCK(); 91 683 774
320  
321  
322 { /* return earlier if the arg is PL_defgv as we can use cache from the previous call */
323 683 SV *const arg = *PL_stack_sp;
324 683 ▶ GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */
Branch: 9 branches
BranchTrueFalseTotal
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 2 681 683
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 2 2
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 681 681
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
GV *gv = MAYBE_DEREF_GV(arg); /* incomplete but should do most of the work */ 0 0 0
325 //printf ("### arg %d %p vs GV %p vs defgv %p \n", SvFLAGS(arg), *PL_stack_sp, gv, PL_defgv );
326 /* get the GV from the arg if it s not a GV */
327 683 ▶ if ( SvTYPE(arg) == SVt_NULL || gv == PL_defgv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvTYPE(arg) == SVt_NULL || gv == PL_defgv ) { 678 5 683
if ( SvTYPE(arg) == SVt_NULL || gv == PL_defgv ) { 2 676 678
328 7 return CALL_REAL_OP();
329 }
330 }
331  
332 /* calling with our own tmp stat struct, instead of passing directly PL_statcache: more control */
333 676 check_status = _overload_ft_stat(&mocked_stat, &size);
334  
335 /* explicit ask for fallback */
336 662 ▶ if ( check_status == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_status == -1 ) 3 659 662
337 3 return CALL_REAL_OP();
338  
339 /*
340 * The idea is too fool the stat function
341 * like if it was called by passing _ or *_
342 *
343 * We are setting these values as if stat was previously called
344 * - PL_laststype
345 * - PL_statcache
346 * - PL_laststatval
347 * - PL_statname
348 *
349 */
350  
351 {
352 659 dSP;
353  
354 /* drop & replace our stack first element with *_ */
355 /* Unexpected warning: Attempt to free unreferenced scalar: SV 0x119bd80. */
356 //SV *previous_stack = sv_2mortal(POPs); /* what do we want to do with this ? */
357 659 SV *previous_stack = POPs;
358 659 PUSHs( MUTABLE_SV( PL_defgv ) ); /* add *_ to the stack */
359  
360 /* copy the content of mocked_stat to PL_statcache */
361 659 memcpy(&PL_statcache, &mocked_stat, sizeof(PL_statcache));
362  
363 659 ▶ if ( size ) { /* yes it succeeds */
Branch: 1 branch
BranchTrueFalseTotal
if ( size ) { /* yes it succeeds */ 584 75 659
364 584 PL_laststatval = 0;
365 } else { /* the stat call fails */
366 75 PL_laststatval = -1;
367 }
368  
369 659 PL_laststype = PL_op->op_type; /* this was for our OP */
370  
371 /* probably not real necesseary, make warning messages nicer */
372 659 ▶ if ( previous_stack && SvPOK(previous_stack) )
Branch: 2 branches
BranchTrueFalseTotal
if ( previous_stack && SvPOK(previous_stack) ) 659 0 659
if ( previous_stack && SvPOK(previous_stack) ) 659 0 659
373 659 sv_setpv(PL_statname, SvPV_nolen(previous_stack) );
374  
375 659 return CALL_REAL_OP();
376 }
377  
378 }
379  
380 /*
381 * extract from https://perldoc.perl.org/functions/-X.html
382 *
383 * -r File is readable by effective uid/gid.
384 * -w File is writable by effective uid/gid.
385 * -x File is executable by effective uid/gid.
386 * -o File is owned by effective uid.
387 * -R File is readable by real uid/gid.
388 * -W File is writable by real uid/gid.
389 * -X File is executable by real uid/gid.
390 * -O File is owned by real uid.
391 * -e File exists.
392 * -z File has zero size (is empty).
393 * -s File has nonzero size (returns size in bytes).
394 * -f File is a plain file.
395 * -d File is a directory.
396 * -l File is a symbolic link (false if symlinks aren't
397 * supported by the file system).
398 * -p File is a named pipe (FIFO), or Filehandle is a pipe.
399 * -S File is a socket.
400 * -b File is a block special file.
401 * -c File is a character special file.
402 * -t Filehandle is opened to a tty.
403 * -u File has setuid bit set.
404 * -g File has setgid bit set.
405 * -k File has sticky bit set.
406 * -T File is an ASCII or UTF-8 text file (heuristic guess).
407 * -B File is a "binary" file (opposite of -T).
408 * -M Script start time minus file modification time, in days.
409 * -A Same for access time.
410 * -C Same for inode change time
411 */
412  
413 MODULE = Overload__FileCheck PACKAGE = Overload::FileCheck
414  
415 SV*
416 mock_op(optype)
417 SV* optype;
418 ALIAS:
419 Overload::FileCheck::_xs_mock_op = 1
420 Overload::FileCheck::_xs_unmock_op = 2
421 CODE:
422 {
423 /* mylogger = INT2PTR(MyLogger*, SvIV(SvRV(self))); */
424 1323 int opid = 0;
425  
426 1323 ▶ if ( ! SvIOK(optype) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvIOK(optype) ) 0 1323 1323
427 0 croak("first argument to _xs_mock_op / _xs_unmock_op must be one integer");
428  
429 1323 opid = SvIV( optype );
430 1323 ▶ if ( !opid || opid < 0 || opid >= OP_MAX )
Branch: 3 branches
BranchTrueFalseTotal
if ( !opid || opid < 0 || opid >= OP_MAX ) 1323 0 1323
if ( !opid || opid < 0 || opid >= OP_MAX ) 1323 0 1323
if ( !opid || opid < 0 || opid >= OP_MAX ) 0 1323 1323
431 0 croak( "Invalid opid value %d", opid );
432  
433 1323 switch (ix) {
434 903 case 1: /* _xs_mock_op */
435 903 gl_overload_ft->op[opid].is_mocked = 1;
436 903 break;
437 420 case 2: /* _xs_unmock_op */
438 420 gl_overload_ft->op[opid].is_mocked = 0;
439 420 break;
440 0 default:
441 0 croak("Unsupported function at index %d", ix);
442 XSRETURN_EMPTY;
443 }
444  
445 1323 XSRETURN_EMPTY;
446 }
447 OUTPUT:
448 RETVAL
449  
450  
451 SV*
452 get_basetime()
453 CODE:
454 54 RETVAL = newSViv(PL_basetime);
455 OUTPUT:
456 RETVAL
457  
458  
459 BOOT:
460 47 ▶if (!gl_overload_ft) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gl_overload_ft) { 47 0 47
461 HV *stash;
462 SV *sv;
463 47 int ix = 0;
464  
465 47 Newxz( gl_overload_ft, 1, OverloadFTOps);
466  
467 47 stash = gv_stashpvn("Overload::FileCheck", 19, TRUE);
468  
469 47 newCONSTSUB(stash, "_loaded", newSViv(1) );
470  
471 /* provide constants to standardize return values from mocked functions */
472 47 newCONSTSUB(stash, "CHECK_IS_TRUE", &PL_sv_yes ); /* could use newSViv(1) or &PL_sv_yes */
473 47 newCONSTSUB(stash, "CHECK_IS_FALSE", &PL_sv_no ); /* could use newSViv(0) or &PL_sv_no */
474 47 newCONSTSUB(stash, "CHECK_IS_NULL", &PL_sv_undef ); /* FIXME: need to handle this as a valid answer */
475 47 newCONSTSUB(stash, "FALLBACK_TO_REAL_OP", newSVnv(-1) );
476  
477 /* provide constants to add entry in a fake stat array */
478  
479 47 newCONSTSUB(stash, "ST_DEV", newSViv(ix++) );
480 47 newCONSTSUB(stash, "ST_INO", newSViv(ix++) );
481 47 newCONSTSUB(stash, "ST_MODE", newSViv(ix++) );
482 47 newCONSTSUB(stash, "ST_NLINK", newSViv(ix++) );
483 47 newCONSTSUB(stash, "ST_UID", newSViv(ix++) );
484 47 newCONSTSUB(stash, "ST_GID", newSViv(ix++) );
485 47 newCONSTSUB(stash, "ST_RDEV", newSViv(ix++) );
486 47 newCONSTSUB(stash, "ST_SIZE", newSViv(ix++) );
487 47 newCONSTSUB(stash, "ST_ATIME", newSViv(ix++) );
488 47 newCONSTSUB(stash, "ST_MTIME", newSViv(ix++) );
489 47 newCONSTSUB(stash, "ST_CTIME", newSViv(ix++) );
490 47 newCONSTSUB(stash, "ST_BLKSIZE", newSViv(ix++) );
491 47 newCONSTSUB(stash, "ST_BLOCKS", newSViv(ix++) );
492 assert(STAT_T_MAX == ix);
493 47 newCONSTSUB(stash, "STAT_T_MAX", newSViv(STAT_T_MAX) );
494  
495 /* copy the original OP then plug our own custom OP function */
496 /* view pp_sys.c for complete list */
497  
498 /* PP(pp_ftrread) - yes/no/undef */
499 47 INIT_FILECHECK_MOCK( "OP_FTRREAD", OP_FTRREAD, &Perl_pp_overload_ft_yes_no); /* -R */
500 47 INIT_FILECHECK_MOCK( "OP_FTRWRITE", OP_FTRWRITE, &Perl_pp_overload_ft_yes_no); /* -W */
501 47 INIT_FILECHECK_MOCK( "OP_FTREXEC", OP_FTREXEC, &Perl_pp_overload_ft_yes_no); /* -X */
502 47 INIT_FILECHECK_MOCK( "OP_FTEREAD", OP_FTEREAD, &Perl_pp_overload_ft_yes_no); /* -r */
503 47 INIT_FILECHECK_MOCK( "OP_FTEWRITE", OP_FTEWRITE, &Perl_pp_overload_ft_yes_no); /* -w */
504 47 INIT_FILECHECK_MOCK( "OP_FTEEXEC", OP_FTEEXEC, &Perl_pp_overload_ft_yes_no); /* -x */
505  
506 /* PP(pp_ftis) - yes/undef/true/false */
507 47 INIT_FILECHECK_MOCK( "OP_FTIS", OP_FTIS, &Perl_pp_overload_ft_yes_no); /* -e */
508 47 INIT_FILECHECK_MOCK( "OP_FTSIZE", OP_FTSIZE, &Perl_pp_overload_ft_int); /* -s */
509 47 INIT_FILECHECK_MOCK( "OP_FTMTIME", OP_FTMTIME, &Perl_pp_overload_ft_nv); /* -M */
510 47 INIT_FILECHECK_MOCK( "OP_FTCTIME", OP_FTCTIME, &Perl_pp_overload_ft_nv); /* -C */
511 47 INIT_FILECHECK_MOCK( "OP_FTATIME", OP_FTATIME, &Perl_pp_overload_ft_nv); /* -A */
512  
513 /* PP(pp_ftrowned) yes/no/undef */
514 47 INIT_FILECHECK_MOCK( "OP_FTROWNED", OP_FTROWNED, &Perl_pp_overload_ft_yes_no); /* -O */
515 47 INIT_FILECHECK_MOCK( "OP_FTEOWNED", OP_FTEOWNED, &Perl_pp_overload_ft_yes_no); /* -o */
516 47 INIT_FILECHECK_MOCK( "OP_FTZERO", OP_FTZERO, &Perl_pp_overload_ft_yes_no); /* -z */
517 47 INIT_FILECHECK_MOCK( "OP_FTSOCK", OP_FTSOCK, &Perl_pp_overload_ft_yes_no); /* -S */
518 47 INIT_FILECHECK_MOCK( "OP_FTCHR", OP_FTCHR, &Perl_pp_overload_ft_yes_no); /* -c */
519 47 INIT_FILECHECK_MOCK( "OP_FTBLK", OP_FTBLK, &Perl_pp_overload_ft_yes_no); /* -b */
520 47 INIT_FILECHECK_MOCK( "OP_FTFILE", OP_FTFILE, &Perl_pp_overload_ft_yes_no); /* -f */
521 47 INIT_FILECHECK_MOCK( "OP_FTDIR", OP_FTDIR, &Perl_pp_overload_ft_yes_no); /* -d */
522 47 INIT_FILECHECK_MOCK( "OP_FTPIPE", OP_FTPIPE, &Perl_pp_overload_ft_yes_no); /* -p */
523 47 INIT_FILECHECK_MOCK( "OP_FTSUID", OP_FTSUID, &Perl_pp_overload_ft_yes_no); /* -u */
524 47 INIT_FILECHECK_MOCK( "OP_FTSGID", OP_FTSGID, &Perl_pp_overload_ft_yes_no); /* -g */
525 47 INIT_FILECHECK_MOCK( "OP_FTSVTX", OP_FTSVTX, &Perl_pp_overload_ft_yes_no); /* -k */
526  
527 /* PP(pp_ftlink) - yes/no/undef */
528 47 INIT_FILECHECK_MOCK( "OP_FTLINK", OP_FTLINK, &Perl_pp_overload_ft_yes_no); /* -l */
529  
530 /* PP(pp_fttty) - yes/no/undef */
531 47 INIT_FILECHECK_MOCK( "OP_FTTTY", OP_FTTTY, &Perl_pp_overload_ft_yes_no); /* -t */
532  
533 /* PP(pp_fttext) - yes/no/undef */
534 47 INIT_FILECHECK_MOCK( "OP_FTTEXT", OP_FTTEXT, &Perl_pp_overload_ft_yes_no); /* -T */
535 47 INIT_FILECHECK_MOCK( "OP_FTBINARY", OP_FTBINARY, &Perl_pp_overload_ft_yes_no); /* -B */
536  
537 /* PP(pp_stat) also used for: pp_lstat() */
538 47 INIT_FILECHECK_MOCK( "OP_STAT", OP_STAT, &Perl_pp_overload_stat); /* stat */
539 47 INIT_FILECHECK_MOCK( "OP_LSTAT", OP_LSTAT, &Perl_pp_overload_stat); /* lstat */
540  
541 1;
542 }
« blib/lib/Overload/FileCheck.pm ↑ Summary