xs/blob.xs

filter: Statementstmt 93.0% 67 / 72 Branchbran 66.2% 48 / 74 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.4% 115 / 146 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_afterres.h ↑ Summary include/punk/punk_cors.h »
1 MODULE = Punk PACKAGE = Punk::Plugin::Blob
2  
3 PROTOTYPES: DISABLE
4  
5 # Content-addressed storage for uploads, on Apophis. punk_blob.h holds the
6 # rules and says why each is what it is - in particular why a deduplication
7 # hit reads and compares rather than trusting the id.
8  
9 SV *
10 new(class)
11 SV *class
12 CODE:
13 35 RETVAL = sv_bless(newRV_noinc(newSViv(0)),
14 gv_stashpv(SvPV_nolen(class), GV_ADD));
15 OUTPUT:
16 RETVAL
17  
18 # register($app, \%opts)
19 #
20 # `namespace` is required and is not boilerplate: deduplication crosses
21 # tenants, and a dedup hit is measurably faster than a write - so an account
22 # can upload a file, time the response, and learn whether another account
23 # already holds it. A namespace per tenant makes the ids disjoint. The cost is
24 # the deduplication between them, which is the application's trade to make and
25 # the reason there is no default.
26 void
27 register(self, app, opts = &PL_sv_undef)
28 SV *self
29 SV *app
30 SV *opts
31 CODE:
32 {
33 35 ▶ HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 35 0 35
34 70 ▶ ? (HV *)SvRV(opts) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : NULL; 35 0 35
35 35 ▶ SV **root = o ? hv_fetchs(o, "root", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **root = o ? hv_fetchs(o, "root", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 35 0 35
36 35 ▶ SV **ns = o ? hv_fetchs(o, "namespace", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **ns = o ? hv_fetchs(o, "namespace", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 35 0 35
37 SV *store, *cache, *argv[4];
38 AV *cap;
39 int i;
40 static const struct { const char *name; XSUBADDR_t body; } H[] = {
41 { "blob_put", pb_put_cb },
42 { "blob_send", pb_send_cb },
43 { "blob_exists", pb_exists_cb },
44 { "blob_path", pb_path_cb },
45 { "blob_remove", pb_remove_cb },
46 { "blob_store", pb_store_cb },
47 { "blob_root", pb_root_cb },
48 { "blob_safe_type", pb_safe_type_cb }
49 };
50  
51 PERL_UNUSED_VAR(self);
52  
53 /* Apophis has to be LOADED, and nothing above here has done it.
54 *
55 * `plugin 'Blob'` skips its require when the class already has a
56 * `register` method - and this XS defines one in
57 * Punk::Plugin::Blob the moment Punk itself loads, so
58 * lib/Punk/Plugin/Blob.pm is never read and its `use Apophis`
59 * never runs. Being a prerequisite means Apophis is installed,
60 * not that it is in memory.
61 *
62 * The same is true of every plugin that ships inside this dist and
63 * provides `register` from C. Sitemap gets away with it by needing
64 * nothing. */
65 35 (void)pk_require_once(aTHX_ "Apophis", TRUE);
66  
67 /* Resolve the C ABI here, so a missing or too-old Apophis is a BOOT
68 * error naming what to upgrade, rather than a croak out of the first
69 * request that happens to touch a blob. There is no Perl fallback:
70 * a second implementation of the sharding and the id derivation is
71 * exactly what the ABI exists to prevent. */
72 35 (void)punk_ap(aTHX);
73  
74 34 ▶ if (!(root && pb_given(aTHX_ *root))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(root && pb_given(aTHX_ *root))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 29 5 34
if (!(root && pb_given(aTHX_ *root))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 29 29
75 5 croak("Punk::Plugin::Blob: `root` is required - it is where the "
76 "blobs live (plugin 'Blob' => { root => '/var/...' })");
77 29 ▶ if (!(ns && pb_given(aTHX_ *ns))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(ns && pb_given(aTHX_ *ns))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 24 5 29
if (!(ns && pb_given(aTHX_ *ns))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 24 24
78 5 croak("Punk::Plugin::Blob: `namespace` is required - "
79 "deduplication crosses tenants, and a namespace per tenant "
80 "is what stops one account learning that another already "
81 "holds a file. See 'Deduplication crosses tenants'");
82  
83 /* A CODEREF namespace is one store per tenant, and cannot be built
84 * here: it depends on the request. It is built on first use and
85 * cached per worker instead - see pb_store_for. */
86 24 ▶ if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 14 24
if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
87 10 store = NULL;
88 10 cache = newRV_noinc((SV *)newHV());
89 }
90 else {
91 14 argv[0] = sv_2mortal(newSVpvs("namespace"));
92 14 argv[1] = *ns;
93 14 argv[2] = sv_2mortal(newSVpvs("store_dir"));
94 14 argv[3] = *root;
95 14 store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Apophis")), "new",
96 argv, 4, 1);
97 14 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 14 0 14
# uncoverable branch true 0 14 14
98 0 # uncoverable condition left count:1 class:ignore_covered_err
99 14 # uncoverable condition left count:2 class:ignore_covered_err
100 14 if (!(store && SvROK(store)))
101 croak("Punk::Plugin::Blob: Apophis->new gave back no store"); /* # uncoverable statement */
102 24 sv_2mortal(store);
103 cache = newRV_noinc((SV *)newHV());
104 216 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 192 24 216
105 sv_2mortal(cache);
106  
107 192 for (i = 0; i < (int)(sizeof H / sizeof H[0]); i++) {
108 192 ▶ SV *h[3];
Branch: 1 branch
BranchTrueFalseTotal
SV *h[3]; 112 80 192
109 192 SV *r;
110 192 cap = newAV();
111 av_push(cap, store ? newSVsv(store) : newSV(0));
112 av_push(cap, newSVsv(*root));
113 192 av_push(cap, newSVsv(*ns));
114 192 /* ONE cache across every helper, so blob_put and blob_send in the
115 192 * same request reach the same store rather than building two. */
116 192 av_push(cap, newSVsv(cache));
117 192 h[0] = sv_2mortal(newSVpv(H[i].name, 0));
118 192 ▶ h[1] = sv_2mortal(punk_closure(aTHX_ H[i].body, cap));
Branch: 1 branch
BranchTrueFalseTotal
h[1] = sv_2mortal(punk_closure(aTHX_ H[i].body, cap)); 192 0 192
119 h[2] = sv_2mortal(newSVpvs("Punk::Plugin::Blob"));
120 r = pcx_call_meth(aTHX_ app, "helper", h, 3, 1);
121 # uncoverable branch false
122 # uncoverable condition right count:1 class:ignore_covered_err
123 if (r) SvREFCNT_dec(r);
124 }
125 }
126  
127 # sweep($root_or_app, live => sub {...}, %opts) -> (removed, kept)
128 #
129 # Nothing unlinks during a request; this is where bytes actually go. The
130 # refusals come before the work: the callback is called first, and its failure
131 # or emptiness aborts before a directory is opened.
132 void
133 sweep(class, where, ...)
134 SV *class
135 SV *where
136 PPCODE:
137 {
138 55 SV *cb = NULL, *root = NULL;
139 55 double grace = 3600.0;
140 55 int dry = 0, allow_empty = 0, i;
141 HV *live;
142 55 UV removed = 0, kept = 0;
143  
144 PERL_UNUSED_VAR(class);
145 180 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 125 55 180
146 125 const char *k = SvPV_nolen(ST(i));
147 125 SV *v = ST(i + 1);
148 125 ▶ if (strEQ(k, "live")) cb = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "live")) cb = v; 55 70 125
149 70 ▶ else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 55 15 70
else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 55 0 55
150 15 ▶ else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0; 5 10 15
151 10 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 10 0 10
152 # uncoverable condition right count:1 class:ignore_covered_err
153 55 ▶ else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0; 55 0 55
else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0; 55 0 55
else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0; 0 55 55
154 0 }
155 # uncoverable branch true
156 # uncoverable condition left count:1 class:ignore_covered_err
157 # uncoverable condition left count:2 class:ignore_covered_err
158 55 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 55 55
159 if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV))
160 55 croak("Punk::Plugin::Blob: sweep needs live => sub { ... } " /* # uncoverable statement */
161 55 ▶ "returning the blob ids still referenced");
Branch: 3 branches
BranchTrueFalseTotal
"returning the blob ids still referenced"); 55 0 55
"returning the blob ids still referenced"); 55 0 55
"returning the blob ids still referenced"); 0 55 55
162 0  
163 /* an app, or the root itself */
164 55 # uncoverable branch true
165 40 # uncoverable condition left count:1 class:ignore_covered_err
166 40 if (SvROK(where)) root = sv_2mortal(pcx_call_meth(aTHX_ where,
167 "blob_root", NULL, 0, 1));
168 40 else root = where;
169 40 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 40 40
170 40 # uncoverable condition left count:1 class:ignore_covered_err
171 40 # uncoverable condition left count:2 class:ignore_covered_err
172 # uncoverable condition left count:3 class:ignore_covered_err
173 if (!(root && SvOK(root) && SvCUR(root)))
174 croak("Punk::Plugin::Blob: sweep needs the store root"); /* # uncoverable statement */
175  
176 live = pb_live_set(aTHX_ cb, allow_empty);
177 sv_2mortal((SV *)live);
178 pb_sweep(aTHX_ SvPV_nolen(root), live, grace, dry, &removed, &kept);
179  
180 SP = PL_stack_base + ax - 1; /* the live callback ran */
181 EXTEND(SP, 2);
182 mPUSHu(removed);
183 mPUSHu(kept);
184 }
185  
186 # The type a blob may be served inline as, or undef. Raster images only; see
187 # punk_blob.h for why SVG and PDF are not on the list.
188 SV *
189 safe_inline_type(class, claimed)
190 SV *class
191 SV *claimed
192 CODE:
193 {
194 30 STRLEN l = 0;
195 60 ▶ const char *s = SvOK(claimed) ? SvPV_const(claimed, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvOK(claimed) ? SvPV_const(claimed, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 30 0 30
196 30 const char *ok = pb_inline_ok(s, l);
197 PERL_UNUSED_VAR(class);
198 30 ▶ if (!ok) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) XSRETURN_UNDEF; 15 15 30
199 15 RETVAL = newSVpv(ok, 0);
200 }
201 OUTPUT:
202 RETVAL
203  
204 # Is this a blob id? The guard between a request and a filesystem path.
205 IV
206 _id_ok(class, id)
207 SV *class
208 SV *id
209 CODE:
210 {
211 60 STRLEN l = 0;
212 120 ▶ const char *s = SvOK(id) ? SvPV_const(id, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *s = SvOK(id) ? SvPV_const(id, l) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 60 0 60
213 PERL_UNUSED_VAR(class);
214 60 ▶ RETVAL = pb_id_ok(s, l);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = pb_id_ok(s, l); 40 20 60
215 }
216 OUTPUT:
217 RETVAL
218  
219 # The store seam the tests assert through.
220 SV *
221 _store(class, ca, content)
222 SV *class
223 SV *ca
224 SV *content
225 CODE:
226 {
227 PERL_UNUSED_VAR(class);
228 125 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 125 0 125
# uncoverable branch true 0 125 125
229 0 # uncoverable condition left count:1 class:ignore_covered_err
230 125 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 125 0 125
# uncoverable condition left count:2 class:ignore_covered_err 0 125 125
231 0 if (!(ca && SvROK(ca)))
232 125 croak("Punk::Plugin::Blob: need an Apophis store"); /* # uncoverable statement */
233 # uncoverable branch true
234 # uncoverable condition left count:1 class:ignore_covered_err
235 # uncoverable condition left count:2 class:ignore_covered_err
236 if (!(content && SvROK(content)))
237 croak("Punk::Plugin::Blob: content must be a scalar reference"); /* # uncoverable statement */
238 RETVAL = pb_store(aTHX_ ca, content);
239 }
240 OUTPUT:
241 RETVAL
« include/punk/punk_afterres.h ↑ Summary include/punk/punk_cors.h »