include/punk/punk_blob.h

filter: Statementstmt 92.9% 372 / 409 (8 uncoverable) Branchbran 74.0% 261 / 416 (73 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 633 / 825 (81 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_lang.h ↑ Summary xs/config.xs »
1 /* punk_blob.h - content-addressed storage for uploads, on Apophis.
2 *
3 * Punk::Upload answers what an upload costs and stops there. Where the file
4 * then lives is left to the application, so every application invents a
5 * directory layout, a naming scheme and a deduplication story - and the
6 * naming scheme is usually the user's filename, which is how a path traversal
7 * gets written.
8 *
9 * Apophis does the storage half: deterministic UUID v5 identifiers, a sharded
10 * tree, atomic temp-and-rename writes, and dedup that needs no locking
11 * because content addressing is idempotent. This is the tier above it - the
12 * part that decides what an application may hand it and what it may hand
13 * back.
14 *
15 * WHY THE CONTENT IS COMPARED ON A DEDUP HIT.
16 *
17 * Apophis identifies content with UUID v5, which is SHA-1, and its store()
18 * returns immediately when the id already exists without comparing the bytes.
19 * That is the right optimisation under the assumption content addressing
20 * usually carries, that one id means one set of bytes.
21 *
22 * SHA-1 has not supported that assumption since 2017. The attack is three
23 * steps: craft two colliding files; upload A, which is stored and gets
24 * whatever review or sharing the application does; then anyone who later
25 * uploads B is handed a reference that serves A, because nothing was written.
26 *
27 * So a dedup hit READS THE STORED BLOB AND COMPARES IT. Equal costs one read.
28 * Unequal croaks, because it cannot be resolved: the store holds one of the
29 * two files and either answer is wrong for somebody.
30 *
31 * HOW APOPHIS IS REACHED.
32 *
33 * Through its C ABI (ap_abi.h, via ExtUtils::Depends), not through method
34 * calls. Identifying content, sharding it and writing it are all snprintf and
35 * memcpy underneath, and paying a Perl method call for each of them is most
36 * of what blob_send costs. Apophis 0.05+ is a hard prerequisite, so a missing
37 * or too-old table is a BOOT error raised by register, not a per-request
38 * fallback - there is no second implementation to fall back to, and quietly
39 * having one would be worse than the croak.
40 *
41 * What stays on the Perl API: remove, verify, meta. None of them is on the
42 * request path, and remove has a metadata sidecar to unlink as well as the
43 * blob. A second copy of those semantics, exercised once a month by a
44 * maintenance job, is how a rewrite quietly stops deleting something.
45 *
46 * Must be included after punk_context.h (pcx_call_meth), punk_app.h (app_hv)
47 * and punk_static.h (punk_closure).
48 */
49  
50 #ifndef PUNK_BLOB_H
51 #define PUNK_BLOB_H
52  
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <dirent.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58  
59 #include "ap_abi.h" /* Apophis's public ABI, via ExtUtils::Depends */
60  
61 #define PB_PATH_MAX 4096
62  
63 static const ap_abi *PUNK_AP = NULL;
64 static int PUNK_AP_TRIED = 0;
65  
66 /* Resolve (once) Apophis's ABI table, or NULL. PUNK_FAKE_AP_BAD simulates a
67 * version mismatch for the guard test. */
68 918 static const ap_abi *punk_ap_try(pTHX) {
69 918 ▶ if (!PUNK_AP_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_AP_TRIED) { 6 912 918
70 6 dSP; int count; UV p = 0;
71 6 PUNK_AP_TRIED = 1;
72 /*
73 # uncoverable branch false
74 # uncoverable condition right count:1 class:ignore_covered_err */
75 6 ▶ if (pk_require_once(aTHX_ "Apophis", FALSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pk_require_once(aTHX_ "Apophis", FALSE)) { 6 0 6
76 6 SPAGAIN; /* the require may have reallocated the value stack */
77 6 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 6 6
78 6 count = call_pv("Apophis::_abi_ptr", G_SCALAR | G_EVAL);
79 6 SPAGAIN;
80 /* SvUV, not SvIV: the address is unsigned, and a .so mapped with
81 * the top bit set reads back negative through PTR2IV/SvIV.
82 *
83 * And popped ONCE, into an SV*, because SvUV is a macro that
84 * mentions its argument more than once: SvUV(POPs) pops twice and
85 * reads the address off the wrong slot, so the table came back
86 * as zero and every perl older than the one this was written on
87 * failed the version guard with "upgrade Apophis to 0.05+"
88 * against an Apophis 0.05 that was sitting right there. */
89 /*
90 # uncoverable branch false
91 # uncoverable condition right count:1 class:ignore_covered_err */
92 6 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 6 0 6
93 6 SV *sv = POPs;
94 /*
95 # uncoverable branch false
96 # uncoverable condition right count:1 class:ignore_covered_err
97 # uncoverable condition right count:2 class:ignore_covered_err */
98 6 ▶ if (!SvTRUE(ERRSV)) p = SvUV(sv);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) p = SvUV(sv); 6 0 6
if (!SvTRUE(ERRSV)) p = SvUV(sv); 6 0 6
99 }
100 6 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 6 0 6
101 /*
102 # uncoverable branch false
103 # uncoverable condition right count:1 class:ignore_covered_err */
104 6 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 6 0 6
105 6 const ap_abi *a = INT2PTR(const ap_abi *, p);
106 /* >= and never ==: the table is append-only, so a later one
107 * is a superset whose prefix stays valid. An equality check
108 * would make every Apophis release break this plugin. */
109 /*
110 # uncoverable condition right count:1 class:ignore_covered_err
111 # uncoverable condition right count:3 class:ignore_covered_err */
112 6 ▶ if (a && !getenv("PUNK_FAKE_AP_BAD")
Branch: 2 branches
BranchTrueFalseTotal
if (a && !getenv("PUNK_FAKE_AP_BAD") 6 0 6
if (a && !getenv("PUNK_FAKE_AP_BAD") 5 1 6
113 5 ▶ && a->abi_version >= AP_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
&& a->abi_version >= AP_ABI_VERSION) 5 0 5
114 5 PUNK_AP = a;
115 }
116 }
117 }
118 918 return PUNK_AP;
119 }
120  
121 /* The table, croaking if it is missing or too old. Called by register, so
122 * this is a boot-environment error rather than a surprise mid-request. */
123 918 static const ap_abi *punk_ap(pTHX) {
124 918 const ap_abi *a = punk_ap_try(aTHX);
125 918 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 1 917 918
126 1 croak("Punk::Plugin::Blob needs Apophis with a compatible C ABI "
127 "(AP_ABI_VERSION %d); upgrade Apophis to 0.05+", AP_ABI_VERSION);
128 917 return a;
129 }
130  
131 /* Was an option actually given? Defined, and not the empty string.
132 *
133 * SvCUR is only legal on a PV, and `namespace` is documented to take a
134 * CODEREF - a reference is an IV-bodied SV with no PVX at all. Reading one
135 * silently returns whatever sits at that offset on an ordinary perl and trips
136 * `PL_valid_types_PVX` on a DEBUGGING one, which is an abort rather than a
137 * croak. A reference is by definition not empty, so it answers before the
138 * length is ever looked at. */
139 53 static int pb_given(pTHX_ SV *sv) {
140 STRLEN len;
141 /*
142 # uncoverable branch true
143 # uncoverable condition left count:1 class:ignore_covered_err
144 # uncoverable condition left count:2 class:ignore_covered_err */
145 53 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 53 0 53
if (!sv || !SvOK(sv)) return 0; 0 53 53
146 53 ▶ if (SvROK(sv)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) return 1; 10 43 53
147 (void)SvPV_const(sv, len);
148 43 return len != 0;
149 }
150  
151 /* The store's namespace bytes and directory, or croak. */
152 367 static void pb_unpack(pTHX_ SV *store, const unsigned char **ns,
153 const char **dir, STRLEN *dirlen) {
154 367 const ap_abi *A = punk_ap(aTHX);
155 /*
156 # uncoverable branch true
157 # uncoverable condition left count:1 class:ignore_covered_err */
158 367 ▶ if (!A->store_of(aTHX_ store, ns, dir, dirlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->store_of(aTHX_ store, ns, dir, dirlen)) 0 367 367
159 0 croak("Punk::Plugin::Blob: the store is not a usable Apophis object " /* # uncoverable statement */
160 "- it needs both a namespace and a store_dir");
161 367 }
162  
163 /* The on-disk path for an id, into a caller's buffer. Returns its length.
164 *
165 * build_path comes from the ABI rather than being two lines of snprintf here,
166 * and that is the point: the sharded layout is Apophis's to change, and a
167 * second copy of the rule would mean that the day it changes, every blob
168 * already on disk becomes unreachable through Punk while staying perfectly
169 * findable through Apophis. */
170 218 static int pb_path(pTHX_ SV *store, const char *id, STRLEN idlen,
171 char *out, size_t outsz) {
172 218 const ap_abi *A = punk_ap(aTHX);
173 const unsigned char *ns; const char *dir; STRLEN dirlen;
174 int n;
175 218 pb_unpack(aTHX_ store, &ns, &dir, &dirlen);
176 218 n = A->build_path(out, outsz, dir, dirlen, id, idlen);
177 /* snprintf semantics: n is the length it WOULD have needed. Apophis's own
178 * callers do not check this, so a store root long enough to overrun
179 * truncates silently - and a truncated path is a different file. */
180 /*
181 # uncoverable branch true
182 # uncoverable condition left count:1 class:ignore_covered_err
183 # uncoverable condition left count:2 class:ignore_covered_err */
184 218 ▶ if (n < 0 || (size_t)n >= outsz)
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= outsz) 218 0 218
if (n < 0 || (size_t)n >= outsz) 0 218 218
185 0 croak("Punk::Plugin::Blob: the blob path does not fit in %d bytes " /* # uncoverable statement */
186 "- the store root is too long", (int)outsz); /* # uncoverable statement */
187 218 return n;
188 }
189  
190 /* Do the bytes already at `path` match what is being offered?
191 *
192 * Compared in chunks rather than by reading the blob into a scalar: the whole
193 * point of this read is a memcmp, and a mismatched first block ends it. -1
194 * means the file could not be read at all. */
195 38 static int pb_same(const char *path, const char *want, STRLEN wantlen) {
196 char buf[65536];
197 struct stat st;
198 38 STRLEN off = 0;
199 38 int fd = open(path, O_RDONLY);
200 /*
201 # uncoverable branch true
202 # uncoverable condition left count:1 class:ignore_covered_err */
203 38 ▶ if (fd < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) return -1; 0 38 38
204 /*
205 # uncoverable branch true
206 # uncoverable condition left count:1 class:ignore_covered_err */
207 38 ▶ if (fstat(fd, &st) != 0) { close(fd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fd, &st) != 0) { close(fd); return -1; } 0 38 38
208 38 ▶ if ((STRLEN)st.st_size != wantlen) { close(fd); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)st.st_size != wantlen) { close(fd); return 0; } 5 33 38
209 81 ▶ while (off < wantlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < wantlen) { 48 33 81
210 48 size_t chunk = wantlen - off;
211 ssize_t got;
212 48 ▶ if (chunk > sizeof buf) chunk = sizeof buf;
Branch: 1 branch
BranchTrueFalseTotal
if (chunk > sizeof buf) chunk = sizeof buf; 20 28 48
213 48 got = read(fd, buf, chunk);
214 /*
215 # uncoverable branch true
216 # uncoverable condition left count:1 class:ignore_covered_err */
217 48 ▶ if (got <= 0) { close(fd); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) { close(fd); return -1; } 0 48 48
218 /*
219 # uncoverable branch true
220 # uncoverable condition left count:1 class:ignore_covered_err */
221 48 ▶ if (memNE(buf, want + off, (size_t)got)) { close(fd); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (memNE(buf, want + off, (size_t)got)) { close(fd); return 0; } 0 48 48
222 48 off += (STRLEN)got;
223 }
224 33 close(fd);
225 33 return 1;
226 }
227  
228 /* Is this a UUID, exactly?
229 *
230 * 8-4-4-4-12 lowercase hex and nothing else. It matters because an id usually
231 * came out of a URL and is about to become a filesystem path through
232 * Apophis's path_for - and send_file says so plainly: "the path is served as
233 * given - if any part of it came from the request, the traversal guard is
234 * yours".
235 *
236 * A whole-shape check rather than a scan for '..' or '/': a rule that
237 * enumerates the bad shapes misses one, and there is exactly one good shape. */
238 564 static int pb_id_ok(const char *s, STRLEN l) {
239 static const int dash[5] = { 8, 13, 18, 23, -1 };
240 STRLEN i;
241 564 int d = 0;
242 564 ▶ if (!s || l != 36) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!s || l != 36) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 564 0 564
if (!s || l != 36) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 360 204 564
243 7188 ▶ for (i = 0; i < 36; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 36; i++) { 6994 194 7188
244 6994 ▶ if (dash[d] >= 0 && i == (STRLEN)dash[d]) {
Branch: 2 branches
BranchTrueFalseTotal
if (dash[d] >= 0 && i == (STRLEN)dash[d]) { 4666 2328 6994
if (dash[d] >= 0 && i == (STRLEN)dash[d]) { 776 3890 4666
245 /*
246 # uncoverable branch true
247 # uncoverable condition left count:1 class:ignore_covered_err */
248 776 ▶ if (s[i] != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] != '-') return 0; 0 776 776
249 776 d++;
250 776 continue;
251 }
252 6218 ▶ if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 4 branches
BranchTrueFalseTotal
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 6218 0 6218
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2036 4182 6218
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2031 5 2036
if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 5 2026 2031
253 10 return 0;
254 }
255 194 return 1;
256 }
257  
258 /* The types a blob may be served INLINE as, and nothing else.
259 *
260 * Raster images. The list is short on purpose: every entry is a format a
261 * browser will render in this application's origin, so every entry is a
262 * decision about what an uploader may make it render.
263 *
264 * SVG IS NOT ON IT, which is the whole reason there is a list rather than an
265 * /^image\// test. An SVG is a document that can carry script, so serving one
266 * inline from your own origin is exactly the cross-site scripting this
267 * defends against - and image/svg+xml passes every naive image check ever
268 * written. PDF is not on it either: browsers render PDFs inline and a PDF is
269 * a far larger attack surface than a raster decoder. */
270 35 static const char *pb_inline_ok(const char *s, STRLEN l) {
271 static const char *ok[] = { "image/png", "image/jpeg", "image/gif",
272 "image/webp", "image/avif", NULL };
273 char buf[64];
274 35 STRLEN i, n = 0;
275 int j;
276 /*
277 # uncoverable branch true
278 # uncoverable condition left count:1 class:ignore_covered_err
279 # uncoverable condition left count:2 class:ignore_covered_err */
280 35 ▶ if (!s || !l) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !l) return NULL; 35 0 35
if (!s || !l) return NULL; 0 35 35
281 400 ▶ for (i = 0; i < l && n < sizeof buf - 1; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l && n < sizeof buf - 1; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 370 30 400
for (i = 0; i < l && n < sizeof buf - 1; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 370 0 370
282 370 char c = s[i];
283 370 ▶ if (c == ';') break; /* drop any parameters */
Branch: 1 branch
BranchTrueFalseTotal
if (c == ';') break; /* drop any parameters */ 5 365 370
284 /*
285 # uncoverable branch true
286 # uncoverable condition left count:1 class:ignore_covered_err
287 # uncoverable condition left count:2 class:ignore_covered_err */
288 365 ▶ if (c == ' ' || c == '\t') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t') continue; 365 0 365
if (c == ' ' || c == '\t') continue; 0 365 365
289 365 ▶ buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c; 325 40 365
buf[n++] = (c >= 'A' && c <= 'Z') ? (char)(c | 32) : c; 40 285 325
290 }
291 35 buf[n] = '\0';
292 135 ▶ for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j];
Branch: 2 branches
BranchTrueFalseTotal
for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j]; 15 100 115
for (j = 0; ok[j]; j++) if (strEQ(buf, ok[j])) return ok[j]; 115 20 135
293 20 return NULL;
294 }
295  
296 /* Store bytes, comparing on a dedup hit. Returns the id (+1).
297 *
298 * The whole of Apophis's store() in C: identify, format, shard, stat, and
299 * either compare or write. No Perl frame, and the collision check is now a
300 * chunked compare rather than a fetch into a scalar. */
301 /* Create the directories leading to `path`.
302 *
303 * Apophis's write_atomic does this itself, but the cross-filesystem copy
304 * below cannot use write_atomic - that takes the content as a buffer, which
305 * is the one thing this whole path exists to avoid holding. */
306 5 static void pb_mkparent(pTHX_ const char *path) {
307 char buf[PB_PATH_MAX];
308 char *p;
309 5 STRLEN n = strlen(path);
310 /*
311 # uncoverable branch true
312 # uncoverable condition left count:1 class:ignore_covered_err */
313 5 ▶ if (n >= sizeof buf) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof buf) return; 0 5 5
314 5 Copy(path, buf, n + 1, char);
315 5 p = strrchr(buf, '/');
316 /*
317 # uncoverable branch true
318 # uncoverable condition left count:1 class:ignore_covered_err */
319 5 ▶ if (!p) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return; 0 5 5
320 5 *p = '\0';
321 105 ▶ for (p = buf + 1; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = buf + 1; *p; p++) { 100 5 105
322 100 ▶ if (*p != '/') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '/') continue; 85 15 100
323 15 *p = '\0';
324 15 (void)PerlDir_mkdir(buf, 0700);
325 15 *p = '/';
326 }
327 5 (void)PerlDir_mkdir(buf, 0700);
328 }
329  
330 /* An upload that is already a file: is there a path, and can it be read? */
331 149 static SV *pb_upload_path(pTHX_ SV *what) {
332 SV *p;
333 /*
334 # uncoverable condition left count:1 class:ignore_covered_err
335 # uncoverable condition left count:2 class:ignore_covered_err */
336 149 ▶ if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 149 0 149
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 149 0 149
if (!(what && SvROK(what) && SvOBJECT(SvRV(what)))) return NULL; 129 20 149
337 /*
338 # uncoverable branch true
339 # uncoverable condition left count:1 class:ignore_covered_err */
340 20 ▶ if (!pcx_can(aTHX_ what, "path")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!pcx_can(aTHX_ what, "path")) return NULL; 0 20 20
341 20 p = pcx_call_meth(aTHX_ what, "path", NULL, 0, 1);
342 /*
343 # uncoverable branch false
344 # uncoverable condition right count:1 class:ignore_covered_err
345 # uncoverable condition right count:2 class:ignore_covered_err
346 # uncoverable condition right count:3 class:ignore_covered_err */
347 20 ▶ if (p && SvOK(p) && SvCUR(p)) return p;
Branch: 3 branches
BranchTrueFalseTotal
if (p && SvOK(p) && SvCUR(p)) return p; 20 0 20
if (p && SvOK(p) && SvCUR(p)) return p; 20 0 20
if (p && SvOK(p) && SvCUR(p)) return p; 20 0 20
348 /*
349 # uncoverable statement
350 # uncoverable branch true
351 # uncoverable branch false
352 # uncoverable condition left count:1
353 # uncoverable condition right count:1 */
354 0 ▶ if (p) SvREFCNT_dec(p);
Branch: 1 branch
BranchTrueFalseTotal
if (p) SvREFCNT_dec(p); 0 0 0
355 0 return NULL; /* # uncoverable statement */
356 }
357  
358 /* Do two files hold the same bytes? Used for the deduplication check when
359 * neither side is in memory. -1 when one of them cannot be read. */
360 15 static int pb_same_file(pTHX_ const char *a, const char *b) {
361 15 PerlIO *fa = PerlIO_open(a, "rb");
362 PerlIO *fb;
363 15 int same = 1;
364 /*
365 # uncoverable branch true
366 # uncoverable condition left count:1 class:ignore_covered_err */
367 15 ▶ if (!fa) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!fa) return -1; 0 15 15
368 15 fb = PerlIO_open(b, "rb");
369 /*
370 # uncoverable branch true
371 # uncoverable condition left count:1 class:ignore_covered_err */
372 15 ▶ if (!fb) { PerlIO_close(fa); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fb) { PerlIO_close(fa); return -1; } 0 15 15
373 40 for (;;) {
374 char ba[65536], bb[65536];
375 55 SSize_t na = PerlIO_read(fa, ba, sizeof ba);
376 55 SSize_t nb = PerlIO_read(fb, bb, sizeof bb);
377 55 ▶ if (na != nb) { same = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (na != nb) { same = 0; break; } 5 50 55
378 50 ▶ if (na <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (na <= 0) break; 10 40 50
379 /*
380 # uncoverable branch true
381 # uncoverable condition left count:1 class:ignore_covered_err */
382 40 ▶ if (memNE(ba, bb, (size_t)na)) { same = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (memNE(ba, bb, (size_t)na)) { same = 0; break; } 0 40 40
383 }
384 15 PerlIO_close(fa);
385 15 PerlIO_close(fb);
386 15 return same;
387 }
388  
389 /* Store an upload that is already on disk, without ever holding it.
390 *
391 * The upload path gets a large file from the wire to a temp file without it
392 * being resident anywhere. Storing it through ->content would undo all of
393 * that at the last step: the whole point of an id derived
394 * from the contents is that the contents must be READ, and reading them into
395 * a scalar to hash them is the copy that was just removed.
396 *
397 * Apophis ships `identify_fh` for exactly this - the same id the in-memory
398 * path produces, computed in 64KB chunks - and it says why a consumer must
399 * not reimplement it: the chunking and the namespace prefix are easy to get
400 * subtly wrong, and wrong here means ids that disagree with every other path.
401 *
402 * Returns NULL when this is not an on-disk upload, so the caller falls back.
403 */
404 149 static SV *pb_store_file(pTHX_ SV *ca, SV *what) {
405 149 const ap_abi *A = punk_ap(aTHX);
406 const unsigned char *ns; const char *dir; STRLEN dirlen;
407 unsigned char idb[16];
408 char id_str[37], path[PB_PATH_MAX];
409 149 SV *src = pb_upload_path(aTHX_ what);
410 PerlIO *fh;
411 struct stat st;
412  
413 149 ▶ if (!src) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return NULL; 129 20 149
414 20 fh = PerlIO_open(SvPVX(src), "rb");
415 /*
416 # uncoverable branch true
417 # uncoverable condition left count:1 class:ignore_covered_err */
418 20 ▶ if (!fh) { SvREFCNT_dec(src); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) { SvREFCNT_dec(src); return NULL; } 0 20 20
419  
420 20 pb_unpack(aTHX_ ca, &ns, &dir, &dirlen);
421 20 A->identify_fh(aTHX_ idb, ns, fh);
422 20 PerlIO_close(fh);
423 20 A->format_id(id_str, idb);
424 20 (void)pb_path(aTHX_ ca, id_str, 36, path, sizeof path);
425  
426 20 ▶ if (stat(path, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, &st) == 0) { 15 5 20
427 15 int same = pb_same_file(aTHX_ path, SvPVX(src));
428 15 ▶ if (same == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (same == 0) { 5 10 15
429 5 SvREFCNT_dec(src);
430 5 croak("Punk::Plugin::Blob: refusing to store - two DIFFERENT "
431 "files share the id %s. Apophis identifies content with "
432 "SHA-1, so this is either a deliberate collision or a "
433 "corrupted store, and either way the bytes already held "
434 "are not the bytes offered", id_str);
435 }
436 /*
437 # uncoverable branch false
438 # uncoverable condition right count:1 class:ignore_covered_err */
439 10 ▶ if (same == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (same == 1) { 10 0 10
440 /* The dedup case, and the one this is most obviously worth
441 * having: a file uploaded by a hundred users is stored once, and
442 * the ninety-nine after the first move nothing at all. */
443 10 SvREFCNT_dec(src);
444 10 return newSVpvn(id_str, 36);
445 }
446 /* -1: there but unreadable. Fall through and write. */
447 }
448  
449 { /* Same filesystem: a rename, which is free. Otherwise a copy, still
450 * without the bytes passing through a scalar. Either way the store
451 * only ever sees a COMPLETE file at a content address - a partial
452 * object at an address that claims to be its hash is a lie that
453 * survives every later integrity check. */
454 5 SV *tmp = newSVpv(path, 0);
455 5 sv_catpvs(tmp, ".part");
456 /* BEFORE the rename, not only in the copy branch below: rename fails
457 * with ENOENT when the destination's directory does not exist, which
458 * is every first store into a shard. Without this the rename always
459 * failed and the copy path always ran - so the move was never a move,
460 * on the filesystem where it was supposed to be free. */
461 5 (void)pb_mkparent(aTHX_ path);
462 /*
463 # uncoverable branch true
464 # uncoverable condition left count:1 class:ignore_covered_err */
465 5 ▶ if (PerlLIO_rename(SvPVX(src), path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_rename(SvPVX(src), path) != 0) { 0 5 5
466 0 PerlIO *in = PerlIO_open(SvPVX(src), "rb"); /* # uncoverable statement */
467 PerlIO *out;
468 /*
469 # uncoverable statement
470 # uncoverable branch true
471 # uncoverable branch false
472 # uncoverable condition left count:1
473 # uncoverable condition right count:1 */
474 0 ▶ if (!in) { SvREFCNT_dec(tmp); SvREFCNT_dec(src); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { SvREFCNT_dec(tmp); SvREFCNT_dec(src); return NULL; } 0 0 0
475 0 out = PerlIO_open(SvPVX(tmp), "wb"); /* # uncoverable statement */
476 /*
477 # uncoverable statement
478 # uncoverable branch true
479 # uncoverable branch false
480 # uncoverable condition left count:1
481 # uncoverable condition right count:1 */
482 0 ▶ if (!out) { PerlIO_close(in); SvREFCNT_dec(tmp);
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { PerlIO_close(in); SvREFCNT_dec(tmp); 0 0 0
483 0 SvREFCNT_dec(src); return NULL; } /* # uncoverable statement */
484 0 for (;;) { /* # uncoverable statement */
485 char buf[65536];
486 0 SSize_t n = PerlIO_read(in, buf, sizeof buf); /* # uncoverable statement */
487 /*
488 # uncoverable statement
489 # uncoverable branch true
490 # uncoverable branch false
491 # uncoverable branch true count:2
492 # uncoverable branch false count:2
493 # uncoverable branch true count:3
494 # uncoverable branch false count:3
495 # uncoverable condition left count:1
496 # uncoverable condition right count:1 */
497 0 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 0 0
498 /*
499 # uncoverable statement
500 # uncoverable branch true
501 # uncoverable branch false
502 # uncoverable condition left count:1
503 # uncoverable condition right count:1 */
504 0 ▶ if (PerlIO_write(out, buf, n) != n) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlIO_write(out, buf, n) != n) { 0 0 0
505 0 PerlIO_close(in); PerlIO_close(out); /* # uncoverable statement */
506 0 (void)PerlLIO_unlink(SvPVX(tmp)); /* # uncoverable statement */
507 0 SvREFCNT_dec(tmp); SvREFCNT_dec(src); /* # uncoverable statement */
508 0 croak("Punk::Plugin::Blob: short write storing %s", id_str); /* # uncoverable statement */
509 }
510 }
511 0 PerlIO_close(in); /* # uncoverable statement */
512 0 PerlIO_close(out); /* # uncoverable statement */
513 /*
514 # uncoverable statement
515 # uncoverable branch true
516 # uncoverable branch false
517 # uncoverable condition left count:1
518 # uncoverable condition right count:1 */
519 0 ▶ if (PerlLIO_rename(SvPVX(tmp), path) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PerlLIO_rename(SvPVX(tmp), path) != 0) { 0 0 0
520 0 (void)PerlLIO_unlink(SvPVX(tmp)); /* # uncoverable statement */
521 0 SvREFCNT_dec(tmp); SvREFCNT_dec(src); /* # uncoverable statement */
522 0 croak("Punk::Plugin::Blob: cannot place %s", id_str); /* # uncoverable statement */
523 }
524 }
525 5 SvREFCNT_dec(tmp);
526 }
527  
528 /* It has been moved into the store; the upload's cleanup must not chase
529 * it, exactly as Punk::Upload::save does after its own rename. */
530 /*
531 # uncoverable branch false
532 # uncoverable condition right count:1 class:ignore_covered_err */
533 5 ▶ if (pcx_can(aTHX_ what, "_disown"))
Branch: 1 branch
BranchTrueFalseTotal
if (pcx_can(aTHX_ what, "_disown")) 5 0 5
534 5 { SV *r = pcx_call_meth(aTHX_ what, "_disown", NULL, 0, 1);
535 /*
536 # uncoverable branch false class:ignore_covered_err
537 # uncoverable condition right count:1 class:ignore_covered_err */
538 5 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 5 0 5
539  
540 5 SvREFCNT_dec(src);
541 5 return newSVpvn(id_str, 36);
542 }
543  
544 149 static SV *pb_store(pTHX_ SV *ca, SV *what) {
545 149 const ap_abi *A = punk_ap(aTHX);
546 { /* an upload already on disk never becomes a scalar */
547 149 SV *id = pb_store_file(aTHX_ ca, what);
548 144 ▶ if (id) return id;
Branch: 1 branch
BranchTrueFalseTotal
if (id) return id; 15 129 144
549 }
550 {
551 const unsigned char *ns; const char *dir; STRLEN dirlen;
552 unsigned char idb[16];
553 char id_str[37], path[PB_PATH_MAX];
554 const char *content;
555 STRLEN clen;
556 struct stat st;
557  
558 129 pb_unpack(aTHX_ ca, &ns, &dir, &dirlen);
559 129 ▶ content = SvPV_const(SvROK(what) ? SvRV(what) : what, clen); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
content = SvPV_const(SvROK(what) ? SvRV(what) : what, clen); /* # uncoverable condition right count:1 class:ignore_covered_err */ 129 0 129
560  
561 129 A->identify(idb, ns, content, clen);
562 129 A->format_id(id_str, idb);
563 129 (void)pb_path(aTHX_ ca, id_str, 36, path, sizeof path);
564  
565 129 ▶ if (stat(path, &st) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, &st) == 0) { 38 91 129
566 38 int same = pb_same(path, content, clen);
567 38 ▶ if (same == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (same == 0) 5 33 38
568 5 croak("Punk::Plugin::Blob: refusing to store - two DIFFERENT "
569 "files share the id %s. Apophis identifies content with "
570 "SHA-1, so this is either a deliberate collision or a "
571 "corrupted store, and either way the bytes already held "
572 "are not the bytes offered", id_str);
573 /*
574 # uncoverable branch false
575 # uncoverable condition right count:1 class:ignore_covered_err */
576 33 ▶ if (same == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (same == 1) 33 0 33
577 33 return newSVpvn(id_str, 36); /* the ordinary dedup case */
578 /* -1: it is there but unreadable, so the store changed underneath
579 * us. Fall through and write. */
580 }
581  
582 91 A->write_atomic(aTHX_ path, content, clen);
583 91 return newSVpvn(id_str, 36);
584 }
585 }
586  
587 /* What the caller handed over, as the scalar ref Apophis takes (+1).
588 *
589 * A Punk::Upload, a scalar ref, or a string of bytes. NOT a filename: a
590 * string that happens to name a file is content, and guessing otherwise is
591 * how an application stores the path instead of the bytes. */
592 24 static SV *pb_content_ref(pTHX_ SV *what) {
593 /*
594 # uncoverable branch true
595 # uncoverable condition left count:1 class:ignore_covered_err
596 # uncoverable condition left count:2 class:ignore_covered_err */
597 24 ▶ if (!(what && SvOK(what)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(what && SvOK(what))) 24 0 24
if (!(what && SvOK(what))) 0 24 24
598 0 croak("Punk::Plugin::Blob: blob_put needs content"); /* # uncoverable statement */
599 /*
600 # uncoverable branch true
601 # uncoverable condition left count:1 class:ignore_covered_err
602 # uncoverable condition left count:2 class:ignore_covered_err
603 # uncoverable condition right count:2 class:ignore_covered_err
604 # uncoverable condition left count:3 class:ignore_covered_err
605 # uncoverable condition right count:3 class:ignore_covered_err */
606 24 ▶ if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 24 24
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 0 0
if (SvROK(what) && SvTYPE(SvRV(what)) == SVt_PVMG && SvOBJECT(SvRV(what))) { 0 0 0
607 /* something blessed: an upload, if it can hand over its bytes */
608 0 SV *bytes = pcx_call_meth(aTHX_ what, "content", NULL, 0, 1); /* # uncoverable statement */
609 /*
610 # uncoverable statement
611 # uncoverable branch true
612 # uncoverable branch false
613 # uncoverable condition left count:1
614 # uncoverable condition right count:1 */
615 0 ▶ if (bytes) return newRV_noinc(bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (bytes) return newRV_noinc(bytes); 0 0 0
616 0 croak("Punk::Plugin::Blob: blob_put takes a Punk::Upload, a scalar " /* # uncoverable statement */
617 "reference, or a string of bytes");
618 }
619 /*
620 # uncoverable branch true
621 # uncoverable condition left count:1 class:ignore_covered_err */
622 24 ▶ if (SvROK(what)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(what)) { 0 24 24
623 /*
624 # uncoverable statement
625 # uncoverable branch true
626 # uncoverable branch false
627 # uncoverable condition left count:1
628 # uncoverable condition right count:1 */
629 0 ▶ if (SvTYPE(SvRV(what)) > SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(what)) > SVt_PVMG) 0 0 0
630 0 croak("Punk::Plugin::Blob: blob_put takes a Punk::Upload, a " /* # uncoverable statement */
631 "scalar reference, or a string of bytes");
632 0 return newRV_inc(SvRV(what)); /* already a scalar ref */ /* # uncoverable statement */
633 }
634 24 return newRV_noinc(newSVsv(what));
635 }
636  
637 /* ---- the sweep --------------------------------------------------------------
638 *
639 * Deduplication means a blob may have several references, so deleting one
640 * cannot mean unlinking the bytes: two users uploaded the same file, one
641 * deletes their copy, and the other must still have theirs. Unlinking on the
642 * first delete is data loss that looks like a successful operation.
643 *
644 * Refcounting would work and puts an atomic read-modify-write on the upload
645 * path - a lock, in a component whose whole appeal is that content addressing
646 * needs none. So nothing unlinks during a request, and a sweep asks the
647 * application which ids are still referenced and removes the rest. Unswept
648 * orphans cost disk, and disk is cheaper than a support ticket about a
649 * missing file.
650 *
651 * THE DANGEROUS PART IS THE ANSWER, NOT THE WALK. A `live` callback that dies
652 * or returns nothing is indistinguishable from "no blobs are referenced", and
653 * acting on that unlinks the whole store.
654 */
655  
656 155 static void pb_sweep_dir(pTHX_ const char *dir, HV *live, double now,
657 double grace, int dry, UV *removed, UV *kept) {
658 155 DIR *d = opendir(dir);
659 struct dirent *e;
660 /*
661 # uncoverable branch true
662 # uncoverable condition left count:1 class:ignore_covered_err */
663 155 ▶ if (!d) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return; 0 155 155
664 530 ▶ while ((e = readdir(d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = readdir(d))) { 375 155 530
665 char path[2048];
666 struct stat st;
667 375 STRLEN nl = strlen(e->d_name);
668  
669 /* Only things shaped like a blob. A temp file from an interrupted
670 * write, or anything a human left here, is left alone: a sweep that
671 * removes what it does not recognise eventually removes something
672 * that mattered. */
673 405 ▶ if (!pb_id_ok(e->d_name, nl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(e->d_name, nl)) continue; 315 60 375
674 /*
675 # uncoverable branch true
676 # uncoverable condition left count:1 class:ignore_covered_err */
677 60 ▶ if ((size_t)snprintf(path, sizeof path, "%s/%s", dir, e->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(path, sizeof path, "%s/%s", dir, e->d_name) 0 60 60
678 0 >= sizeof path) continue; /* # uncoverable statement class:ignore_covered_err */
679 /*
680 # uncoverable branch true
681 # uncoverable condition left count:1 class:ignore_covered_err
682 # uncoverable condition left count:2 class:ignore_covered_err */
683 60 ▶ if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue; 60 0 60
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) continue; 0 60 60
684  
685 60 ▶ if (hv_exists(live, e->d_name, (I32)nl)) { (*kept)++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(live, e->d_name, (I32)nl)) { (*kept)++; continue; } 25 35 60
686  
687 /* A blob written between the caller's snapshot of live ids and this
688 * walk is in neither, and is not an orphan. The grace period is what
689 * stops that race collecting a file whose row was committed a moment
690 * later. */
691 35 ▶ if (grace > 0 && now - (double)st.st_mtime < grace) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (grace > 0 && now - (double)st.st_mtime < grace) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 30 35
if (grace > 0 && now - (double)st.st_mtime < grace) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
692 5 (*kept)++;
693 5 continue;
694 }
695 /*
696 # uncoverable branch true
697 # uncoverable condition left count:1 class:ignore_covered_err
698 # uncoverable condition left count:2 class:ignore_covered_err */
699 30 ▶ if (!dry && unlink(path) != 0) { (*kept)++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!dry && unlink(path) != 0) { (*kept)++; continue; } 25 5 30
if (!dry && unlink(path) != 0) { (*kept)++; continue; } 0 25 25
700 30 (*removed)++;
701 }
702 155 closedir(d);
703 }
704  
705 /* The whole store: two shard levels, then the blob. */
706 40 static void pb_sweep(pTHX_ const char *root, HV *live, double grace, int dry,
707 UV *removed, UV *kept) {
708 40 DIR *d1 = opendir(root);
709 struct dirent *e1;
710 40 double now = (double)time(NULL);
711 /*
712 # uncoverable branch true
713 # uncoverable condition left count:1 class:ignore_covered_err */
714 40 ▶ if (!d1) croak("Punk::Plugin::Blob: cannot read the store at '%s'", root);
Branch: 1 branch
BranchTrueFalseTotal
if (!d1) croak("Punk::Plugin::Blob: cannot read the store at '%s'", root); 0 40 40
715 275 ▶ while ((e1 = readdir(d1))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e1 = readdir(d1))) { 235 40 275
716 char l1[2048];
717 DIR *d2;
718 struct dirent *e2;
719 235 ▶ if (e1->d_name[0] == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e1->d_name[0] == '.') continue; 80 155 235
720 /*
721 # uncoverable branch true
722 # uncoverable condition left count:1 class:ignore_covered_err */
723 155 ▶ if ((size_t)snprintf(l1, sizeof l1, "%s/%s", root, e1->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(l1, sizeof l1, "%s/%s", root, e1->d_name) 0 155 155
724 0 >= sizeof l1) continue; /* # uncoverable statement class:ignore_covered_err */
725 155 d2 = opendir(l1);
726 /*
727 # uncoverable branch true
728 # uncoverable condition left count:1 class:ignore_covered_err */
729 155 ▶ if (!d2) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!d2) continue; 0 155 155
730 620 ▶ while ((e2 = readdir(d2))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e2 = readdir(d2))) { 465 155 620
731 char l2[2048];
732 465 ▶ if (e2->d_name[0] == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (e2->d_name[0] == '.') continue; 310 155 465
733 /*
734 # uncoverable branch true
735 # uncoverable condition left count:1 class:ignore_covered_err */
736 155 ▶ if ((size_t)snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name)
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name) 0 155 155
737 0 >= sizeof l2) continue; /* # uncoverable statement class:ignore_covered_err */
738 155 pb_sweep_dir(aTHX_ l2, live, now, grace, dry, removed, kept);
739 }
740 155 closedir(d2);
741 }
742 40 closedir(d1);
743 40 }
744  
745 /* Ask the application which ids are still referenced, and refuse to act on an
746 * answer that might not be one. Returns a hash of live ids (+1), or croaks. */
747 55 static HV *pb_live_set(pTHX_ SV *cb, int allow_empty) {
748 55 dSP;
749 55 HV *live = newHV();
750 55 AV *got = (AV *)sv_2mortal((SV *)newAV());
751 55 AV *items = got;
752 int count, i;
753 55 IV n = 0;
754  
755 55 ENTER; SAVETMPS;
756 55 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 55 55
757 55 PUTBACK;
758 55 count = call_sv(cb, G_ARRAY | G_EVAL);
759 55 SPAGAIN;
760 100 ▶ for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs));
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) av_store(got, (SSize_t)i, newSVsv(POPs)); 45 55 100
761 55 PUTBACK;
762 55 ▶ if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 55 0 55
if (SvTRUE(ERRSV)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 50 55
763 /* croak BEFORE the FREETMPS rather than after it holding a pointer
764 * into what it just freed: an SV mortalised inside this scope is gone
765 * the moment its temporaries are released, and reading the message
766 * out of it afterwards is a use-after-free that usually still says
767 * the right thing. perl unwinds the scope. */
768 5 SvREFCNT_dec((SV *)live);
769 10 ▶ croak("Punk::Plugin::Blob: the sweep's `live` callback died, so "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Plugin::Blob: the sweep's `live` callback died, so " 5 0 5
770 "NOTHING was removed - an answer that failed is not an answer "
771 "that there are no live blobs: %s", SvPV_nolen(ERRSV));
772 }
773 50 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 40 10 50
774  
775 /* One arrayref, or a plain list - a model returns the first and a
776 * hand-written callback the second.
777 *
778 * BORROWED, not copied back into `got`: emptying `got` to refill it would
779 * free the arrayref SV, which holds the only reference to the array being
780 * read from, and the loop below would then walk freed memory. */
781 50 ▶ if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) { 35 15 50
782 35 SV **only = av_fetch(got, 0, 0);
783 /*
784 # uncoverable condition right count:1 class:ignore_covered_err
785 # uncoverable condition right count:2 class:ignore_covered_err
786 # uncoverable condition right count:4 class:ignore_covered_err */
787 35 ▶ if (only && *only && SvROK(*only)
Branch: 3 branches
BranchTrueFalseTotal
if (only && *only && SvROK(*only) 35 0 35
if (only && *only && SvROK(*only) 35 0 35
if (only && *only && SvROK(*only) 5 30 35
788 5 ▶ && SvTYPE(SvRV(*only)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*only)) == SVt_PVAV) { 5 0 5
789 5 items = (AV *)SvRV(*only);
790 5 count = (int)(av_len(items) + 1);
791 }
792 }
793  
794 95 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 45 50 95
795 45 SV **x = av_fetch(items, (SSize_t)i, 0);
796 STRLEN l;
797 const char *s;
798 /*
799 # uncoverable condition left count:1 class:ignore_covered_err
800 # uncoverable condition left count:2 class:ignore_covered_err */
801 45 ▶ if (!(x && *x && SvOK(*x))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(x && *x && SvOK(*x))) continue; 45 0 45
if (!(x && *x && SvOK(*x))) continue; 45 0 45
if (!(x && *x && SvOK(*x))) continue; 5 40 45
802 40 s = SvPV_const(*x, l);
803 /*
804 # uncoverable branch true
805 # uncoverable condition left count:1 class:ignore_covered_err */
806 40 ▶ if (!pb_id_ok(s, l)) continue; /* not an id: cannot be live */
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) continue; /* not an id: cannot be live */ 0 40 40
807 40 (void)hv_store(live, s, (I32)l, newSViv(1), 0);
808 40 n++;
809 }
810  
811 50 ▶ if (n == 0 && !allow_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 && !allow_empty) { 15 35 50
if (n == 0 && !allow_empty) { 10 5 15
812 10 SvREFCNT_dec((SV *)live);
813 10 croak("Punk::Plugin::Blob: the sweep's `live` callback returned no "
814 "blob ids, so NOTHING was removed. An empty answer is "
815 "indistinguishable from a query that failed, and acting on it "
816 "would unlink the whole store. Pass allow_empty => 1 if the "
817 "application really does reference no blobs");
818 }
819 40 return live;
820 }
821  
822 /* Call a plain coderef, returning its scalar result (+1) or NULL. */
823 50 static SV *pb_call_cv(pTHX_ SV *cv, SV **argv, int n) {
824 50 dSP;
825 50 SV *r = NULL;
826 int count, i;
827 50 ENTER; SAVETMPS;
828 50 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 50 50
829 50 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 50 0 50
EXTEND(SP, n); 0 50 50
830 100 ▶ for (i = 0; i < n; i++) PUSHs(argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(argv[i]); 50 50 100
831 50 PUTBACK;
832 50 count = call_sv(cv, G_SCALAR);
833 50 SPAGAIN;
834 /*
835 # uncoverable branch false
836 # uncoverable condition right count:1 class:ignore_covered_err */
837 50 ▶ if (count > 0) r = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) r = SvREFCNT_inc(POPs); 50 0 50
838 50 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 50 0 50
839 50 return r;
840 }
841  
842 /* ---- the helpers, as C closures ---------------------------------------------
843 *
844 * Each captures the Apophis store built at registration, so a request never
845 * constructs one. cap = [store, root].
846 */
847  
848 /* The store this request belongs to, borrowed.
849 *
850 * cap = [store-or-undef, root, namespace, cache].
851 *
852 * A STRING namespace is one store, built at registration: every tenant shares
853 * ids, and a file held by two of them is stored once.
854 *
855 * A CODEREF namespace is one store per tenant. It cannot be built at
856 * registration because it depends on the request, so it is built on first use
857 * and cached per worker - the cost is one Apophis object per tenant this
858 * worker has served, which is a handful of scalars.
859 *
860 * Which of the two an application wants is the trade in "Deduplication
861 * crosses tenants", and it is deliberately not defaulted. */
862 133 static SV *pb_store_for(pTHX_ CV *cv, SV *c) {
863 133 AV *cap = punk_clos_cap(aTHX_ cv);
864 SV **s, **ns, **cache, **root;
865 SV *nsv;
866 HV *seen;
867 HE *have;
868  
869 /*
870 # uncoverable branch true
871 # uncoverable condition left count:1 class:ignore_covered_err */
872 133 ▶ if (!cap) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) return NULL; 0 133 133
873 133 s = av_fetch(cap, 0, 0);
874 /*
875 # uncoverable condition right count:1 class:ignore_covered_err
876 # uncoverable condition right count:2 class:ignore_covered_err */
877 133 ▶ if (s && *s && SvROK(*s)) return *s; /* one store for everyone */
Branch: 3 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 133 0 133
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 133 0 133
if (s && *s && SvROK(*s)) return *s; /* one store for everyone */ 83 50 133
878  
879 50 ns = av_fetch(cap, 2, 0);
880 50 root = av_fetch(cap, 1, 0);
881 50 cache = av_fetch(cap, 3, 0);
882 /*
883 # uncoverable branch true
884 # uncoverable condition left count:1 class:ignore_covered_err
885 # uncoverable condition left count:2 class:ignore_covered_err
886 # uncoverable condition left count:3 class:ignore_covered_err
887 # uncoverable condition left count:4 class:ignore_covered_err */
888 50 ▶ if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 50 0 50
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 50 0 50
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 50 0 50
if (!(ns && *ns && SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV)) 0 50 50
889 0 return NULL; /* # uncoverable statement */
890 /*
891 # uncoverable branch true
892 # uncoverable condition left count:1 class:ignore_covered_err
893 # uncoverable condition left count:2 class:ignore_covered_err
894 # uncoverable condition left count:3 class:ignore_covered_err */
895 50 ▶ if (!(cache && *cache && SvROK(*cache))) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cache && *cache && SvROK(*cache))) return NULL; 50 0 50
if (!(cache && *cache && SvROK(*cache))) return NULL; 50 0 50
if (!(cache && *cache && SvROK(*cache))) return NULL; 0 50 50
896 50 seen = (HV *)SvRV(*cache);
897  
898 { /* $namespace->($c) */
899 SV *argv[1];
900 50 ▶ argv[0] = c ? c : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = c ? c : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 50 0 50
901 50 nsv = pb_call_cv(aTHX_ *ns, argv, c ? 1 : 0);
902 }
903 /*
904 # uncoverable condition left count:1 class:ignore_covered_err
905 # uncoverable condition left count:2 class:ignore_covered_err */
906 50 ▶ if (!(nsv && SvOK(nsv) && SvCUR(nsv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 50 0 50
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 50 0 50
if (!(nsv && SvOK(nsv) && SvCUR(nsv))) { 5 45 50
907 /*
908 # uncoverable branch false
909 # uncoverable condition right count:1 class:ignore_covered_err */
910 5 ▶ if (nsv) SvREFCNT_dec(nsv);
Branch: 1 branch
BranchTrueFalseTotal
if (nsv) SvREFCNT_dec(nsv); 5 0 5
911 5 croak("Punk::Plugin::Blob: the `namespace` callback gave back "
912 "nothing - it decides which tenant's ids these are, so an "
913 "empty answer would silently put this request in somebody "
914 "else's namespace");
915 }
916 45 sv_2mortal(nsv);
917  
918 45 have = hv_fetch_ent(seen, nsv, 0, 0);
919 45 ▶ if (have) return HeVAL(have);
Branch: 1 branch
BranchTrueFalseTotal
if (have) return HeVAL(have); 35 10 45
920  
921 {
922 SV *argv[4], *store;
923 10 argv[0] = sv_2mortal(newSVpvs("namespace"));
924 10 argv[1] = nsv;
925 10 argv[2] = sv_2mortal(newSVpvs("store_dir"));
926 /*
927 # uncoverable condition right count:1 class:ignore_covered_err
928 # uncoverable condition right count:2 class:ignore_covered_err */
929 10 ▶ argv[3] = (root && *root) ? *root : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[3] = (root && *root) ? *root : &PL_sv_undef; 10 0 10
argv[3] = (root && *root) ? *root : &PL_sv_undef; 10 0 10
930 10 store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Apophis")), "new",
931 argv, 4, 1);
932 /*
933 # uncoverable branch true
934 # uncoverable condition left count:1 class:ignore_covered_err
935 # uncoverable condition left count:2 class:ignore_covered_err */
936 10 ▶ if (!(store && SvROK(store)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && SvROK(store))) 10 0 10
if (!(store && SvROK(store))) 0 10 10
937 0 croak("Punk::Plugin::Blob: Apophis->new gave back no store for " /* # uncoverable statement */
938 "namespace '%" SVf "'", SVfARG(nsv)); /* # uncoverable statement */
939 10 (void)hv_store_ent(seen, nsv, store, 0);
940 10 return store;
941 }
942 }
943  
944 XS_INTERNAL(pb_put_cb);
945 29 XS_INTERNAL(pb_put_cb) {
946 29 dXSARGS;
947 29 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 29 0 29
948 24 ▶ SV *what = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *what = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 24 0 24
949 SV *ref;
950 /*
951 # uncoverable branch true
952 # uncoverable condition left count:1 class:ignore_covered_err */
953 24 ▶ if (!store) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!store) XSRETURN_EMPTY; 0 24 24
954 24 ref = sv_2mortal(pb_content_ref(aTHX_ what));
955 24 ST(0) = sv_2mortal(pb_store(aTHX_ store, ref));
956 24 XSRETURN(1);
957 }
958  
959 XS_INTERNAL(pb_exists_cb);
960 10 XS_INTERNAL(pb_exists_cb) {
961 10 dXSARGS;
962 10 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
963 10 ▶ SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
964 10 STRLEN l = 0;
965 /*
966 # uncoverable condition right count:1 class:ignore_covered_err
967 # uncoverable condition right count:2 class:ignore_covered_err */
968 20 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
969 char path[PB_PATH_MAX];
970 struct stat st;
971 /*
972 # uncoverable branch true
973 # uncoverable condition left count:1 class:ignore_covered_err
974 # uncoverable condition left count:2 class:ignore_covered_err */
975 10 ▶ if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 10 0 10
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 10 10
976 10 (void)pb_path(aTHX_ store, s, l, path, sizeof path);
977 10 ▶ ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode))); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 5 10
ST(0) = sv_2mortal(newSViv(stat(path, &st) == 0 && S_ISREG(st.st_mode))); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
978 10 XSRETURN(1);
979 }
980  
981 XS_INTERNAL(pb_path_cb);
982 10 XS_INTERNAL(pb_path_cb) {
983 10 dXSARGS;
984 10 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
985 10 ▶ SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
986 10 STRLEN l = 0;
987 /*
988 # uncoverable condition right count:1 class:ignore_covered_err
989 # uncoverable condition right count:2 class:ignore_covered_err */
990 20 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
991 /*
992 # uncoverable branch true
993 # uncoverable condition left count:1 class:ignore_covered_err */
994 10 ▶ if (!store) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!store) XSRETURN_EMPTY; 0 10 10
995 /* blob_path croaks where blob_send answers 404: a program calls this one
996 * deliberately, so a bad id here is a bug rather than a stray request. */
997 10 ▶ if (!pb_id_ok(s, l))
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) 5 5 10
998 5 ▶ croak("Punk::Plugin::Blob: '%s' is not a blob id", s ? s : "(undef)"); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Plugin::Blob: '%s' is not a blob id", s ? s : "(undef)"); /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
999 {
1000 char path[PB_PATH_MAX];
1001 5 int n = pb_path(aTHX_ store, s, l, path, sizeof path);
1002 5 ST(0) = sv_2mortal(newSVpvn(path, n));
1003 }
1004 5 XSRETURN(1);
1005 }
1006  
1007 XS_INTERNAL(pb_remove_cb);
1008 10 XS_INTERNAL(pb_remove_cb) {
1009 10 dXSARGS;
1010 10 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
1011 10 ▶ SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
1012 10 STRLEN l = 0;
1013 /*
1014 # uncoverable condition right count:1 class:ignore_covered_err
1015 # uncoverable condition right count:2 class:ignore_covered_err */
1016 20 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 10 0 10
1017 SV *r;
1018 /*
1019 # uncoverable branch true
1020 # uncoverable condition left count:1 class:ignore_covered_err
1021 # uncoverable condition left count:2 class:ignore_covered_err */
1022 10 ▶ if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 10 0 10
if (!store || !pb_id_ok(s, l)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 10 10
1023 10 r = pcx_call_meth(aTHX_ store, "remove", &id, 1, 1);
1024 10 ▶ ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
ST(0) = sv_2mortal(newSViv((r && SvTRUE(r)) ? 1 : 0)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 5 10
1025 /*
1026 # uncoverable branch false
1027 # uncoverable condition right count:1 class:ignore_covered_err */
1028 10 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 10 0 10
1029 10 XSRETURN(1);
1030 }
1031  
1032 XS_INTERNAL(pb_store_cb);
1033 15 XS_INTERNAL(pb_store_cb) {
1034 15 dXSARGS;
1035 15 ▶ SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *store = pb_store_for(aTHX_ cv, items > 0 ? ST(0) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
1036 15 ▶ ST(0) = store ? sv_2mortal(newSVsv(store)) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = store ? sv_2mortal(newSVsv(store)) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
1037 15 XSRETURN(1);
1038 }
1039  
1040 XS_INTERNAL(pb_root_cb);
1041 15 XS_INTERNAL(pb_root_cb) {
1042 15 dXSARGS;
1043 15 AV *cap = punk_clos_cap(aTHX_ cv);
1044 15 ▶ SV **r = cap ? av_fetch(cap, 1, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **r = cap ? av_fetch(cap, 1, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 0 15
1045 PERL_UNUSED_VAR(items);
1046 /*
1047 # uncoverable condition right count:1 class:ignore_covered_err
1048 # uncoverable condition right count:2 class:ignore_covered_err */
1049 15 ▶ ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef; 15 0 15
ST(0) = (r && *r) ? sv_2mortal(newSVsv(*r)) : &PL_sv_undef; 15 0 15
1050 15 XSRETURN(1);
1051 }
1052  
1053 XS_INTERNAL(pb_safe_type_cb);
1054 5 XS_INTERNAL(pb_safe_type_cb) {
1055 5 dXSARGS;
1056 5 ▶ SV *claimed = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *claimed = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
1057 5 STRLEN l = 0;
1058 /*
1059 # uncoverable condition right count:1 class:ignore_covered_err
1060 # uncoverable condition right count:2 class:ignore_covered_err */
1061 10 ▶ const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL; 5 0 5
const char *s = (claimed && SvOK(claimed)) ? SvPV_const(claimed, l) : NULL; 5 0 5
1062 5 const char *ok = pb_inline_ok(s, l);
1063 5 ▶ ST(0) = ok ? sv_2mortal(newSVpv(ok, 0)) : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = ok ? sv_2mortal(newSVpv(ok, 0)) : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 5 5
1064 5 XSRETURN(1);
1065 }
1066  
1067 /* blob_send: the unfriendly defaults, and why.
1068 *
1069 * An uploader controls the bytes AND claims the type, so serving those bytes
1070 * back as the claimed type is stored cross-site scripting with this
1071 * application's origin on it - the commonest way a file-upload feature
1072 * becomes a vulnerability.
1073 *
1074 * Content-Type: application/octet-stream
1075 * Content-Disposition: attachment
1076 * X-Content-Type-Options: nosniff
1077 *
1078 * nosniff matters as much as the type: without it a browser may sniff HTML
1079 * out of a response labelled octet-stream and render it, which puts the whole
1080 * defence back where it started. It is set always and this helper offers no
1081 * way to turn it off. */
1082 XS_INTERNAL(pb_send_cb);
1083 59 XS_INTERNAL(pb_send_cb) {
1084 59 dXSARGS;
1085 59 ▶ SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 59 0 59
1086 59 SV *store = pb_store_for(aTHX_ cv, c);
1087 59 ▶ SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV *id = items > 1 ? ST(1) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 59 0 59
1088 59 STRLEN l = 0;
1089 /*
1090 # uncoverable condition right count:1 class:ignore_covered_err
1091 # uncoverable condition right count:2 class:ignore_covered_err */
1092 118 ▶ const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 59 0 59
const char *s = (id && SvOK(id)) ? SvPV_const(id, l) : NULL; 59 0 59
1093 SV *path, **argv;
1094 59 int n = 1, i, have_type = 0, have_cc = 0, have_fn = 0, inline_ok = 0;
1095 59 int public_ = 0;
1096  
1097 /*
1098 # uncoverable branch true
1099 # uncoverable condition left count:1 class:ignore_covered_err
1100 # uncoverable condition left count:2 class:ignore_covered_err */
1101 59 ▶ if (!(store && c)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(store && c)) XSRETURN_EMPTY; 59 0 59
if (!(store && c)) XSRETURN_EMPTY; 0 59 59
1102  
1103 /* An id that is not an id is a 404, not an error: it came out of a URL,
1104 * so something probing /blob/../.. should be told there is nothing there
1105 * rather than fill the error log with 500s. */
1106 59 ▶ if (!pb_id_ok(s, l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pb_id_ok(s, l)) { 5 54 59
1107 5 ST(0) = sv_2mortal(pcx_call_meth(aTHX_ c, "not_found", NULL, 0, 1));
1108 5 XSRETURN(1);
1109 }
1110  
1111 /* Always, and before send_file, which folds set headers in. */
1112 {
1113 SV *hv[2];
1114 SV *r;
1115 54 hv[0] = sv_2mortal(newSVpvs("X-Content-Type-Options"));
1116 54 hv[1] = sv_2mortal(newSVpvs("nosniff"));
1117 54 r = pcx_call_meth(aTHX_ c, "header", hv, 2, 1);
1118 /*
1119 # uncoverable branch false
1120 # uncoverable condition right count:1 class:ignore_covered_err */
1121 54 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 54 0 54
1122 }
1123  
1124 /* send_file's arguments: the path, every option the caller gave, and
1125 * the ten at most that are added below. Sized from the call, because a
1126 * fixed array is a stack overflow the caller's argument count chooses. */
1127 54 Newx(argv, items + 12, SV *);
1128 54 SAVEFREEPV(argv);
1129  
1130 146 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 92 54 146
1131 92 const char *k = SvPV_nolen(ST(i));
1132 92 ▶ if (strEQ(k, "type")) have_type = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "type")) have_type = 1; 9 83 92
1133 /*
1134 # uncoverable branch true
1135 # uncoverable condition left count:1 class:ignore_covered_err */
1136 83 ▶ else if (strEQ(k, "cache_control")) have_cc = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "cache_control")) have_cc = 1; 0 83 83
1137 83 ▶ else if (strEQ(k, "filename")) have_fn = 1;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "filename")) have_fn = 1; 4 79 83
1138 79 ▶ else if (strEQ(k, "inline")) inline_ok = SvTRUE(ST(i + 1)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "inline")) inline_ok = SvTRUE(ST(i + 1)) ? 1 : 0; 74 5 79
1139 /*
1140 # uncoverable branch false
1141 # uncoverable condition right count:1 class:ignore_covered_err */
1142 5 ▶ else if (strEQ(k, "public")) { public_ = SvTRUE(ST(i + 1)) ? 1 : 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "public")) { public_ = SvTRUE(ST(i + 1)) ? 1 : 0; continue; } 5 0 5
1143 87 argv[n++] = ST(i);
1144 87 argv[n++] = ST(i + 1);
1145 }
1146  
1147 54 ▶ if (!have_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_type) { 45 9 54
1148 /* nothing unknown is displayed inline */
1149 45 argv[n++] = sv_2mortal(newSVpvs("type"));
1150 45 argv[n++] = sv_2mortal(newSVpvs("application/octet-stream"));
1151 /*
1152 # uncoverable branch true
1153 # uncoverable condition left count:1 class:ignore_covered_err */
1154 45 ▶ if (inline_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (inline_ok) { 0 45 45
1155 0 argv[n++] = sv_2mortal(newSVpvs("inline")); /* # uncoverable statement */
1156 0 argv[n++] = sv_2mortal(newSViv(0)); /* # uncoverable statement */
1157 }
1158 /*
1159 # uncoverable branch false
1160 # uncoverable condition right count:1 class:ignore_covered_err */
1161 45 ▶ if (!have_fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_fn) { 45 0 45
1162 45 argv[n++] = sv_2mortal(newSVpvs("filename"));
1163 45 argv[n++] = sv_2mortal(newSVpvs("blob"));
1164 45 have_fn = 1;
1165 }
1166 }
1167 /*
1168 # uncoverable branch false
1169 # uncoverable condition right count:1 class:ignore_covered_err */
1170 54 ▶ if (!have_cc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!have_cc) { 54 0 54
1171 /* A content-addressed URL cannot come to mean anything else, so a
1172 * long lifetime is right - but `public` on a blob behind
1173 * authorisation tells a shared cache it may hand somebody's document
1174 * to the next person who asks. */
1175 54 argv[n++] = sv_2mortal(newSVpvs("cache_control"));
1176 54 ▶ argv[n++] = sv_2mortal(newSVpvf("%s, max-age=31536000, immutable",
Branch: 1 branch
BranchTrueFalseTotal
argv[n++] = sv_2mortal(newSVpvf("%s, max-age=31536000, immutable", 5 49 54
1177 public_ ? "public" : "private"));
1178 }
1179 /* A well-formed id naming no blob is a reference the sweep collected. */
1180 54 argv[n++] = sv_2mortal(newSVpvs("missing"));
1181 54 argv[n++] = sv_2mortal(newSVpvs("not_found"));
1182  
1183 { /* the request path's one remaining Perl call was this snprintf */
1184 char buf[PB_PATH_MAX];
1185 54 STRLEN il = 0;
1186 54 const char *is = SvPV_const(id, il);
1187 54 int pn = pb_path(aTHX_ store, is, il, buf, sizeof buf);
1188 54 path = sv_2mortal(newSVpvn(buf, pn));
1189 }
1190 54 argv[0] = path;
1191 54 ST(0) = sv_2mortal(pcx_call_meth(aTHX_ c, "send_file", argv, n, 1));
1192 54 XSRETURN(1);
1193 }
1194  
1195 #endif /* PUNK_BLOB_H */
« include/punk/punk_lang.h ↑ Summary xs/config.xs »