| 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 |
7 |
| 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 |
7 |
▶ | HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| HV *o = (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
7 |
0 |
7 |
|
| 34 |
14 |
▶ | ? (HV *)SvRV(opts) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(opts) : NULL; |
7 |
0 |
7 |
|
| 35 |
7 |
▶ | SV **root = o ? hv_fetchs(o, "root", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **root = o ? hv_fetchs(o, "root", 0) : NULL; |
7 |
0 |
7 |
|
| 36 |
7 |
▶ | SV **ns = o ? hv_fetchs(o, "namespace", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **ns = o ? hv_fetchs(o, "namespace", 0) : NULL; |
7 |
0 |
7 |
|
| 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 |
7 |
| (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 |
7 |
| (void)punk_ap(aTHX); |
| 73 |
|
| |
| 74 |
6 |
▶ | if (!(root && pb_given(aTHX_ *root))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(root && pb_given(aTHX_ *root))) |
5 |
1 |
6 |
| if (!(root && pb_given(aTHX_ *root))) |
0 |
5 |
5 |
|
| 75 |
1 |
| croak("Punk::Plugin::Blob: `root` is required - it is where the " |
| 76 |
|
| "blobs live (plugin 'Blob' => { root => '/var/...' })"); |
| 77 |
5 |
▶ | if (!(ns && pb_given(aTHX_ *ns))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(ns && pb_given(aTHX_ *ns))) |
4 |
1 |
5 |
| if (!(ns && pb_given(aTHX_ *ns))) |
0 |
4 |
4 |
|
| 78 |
1 |
| 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 |
4 |
▶ | if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { |
2 |
2 |
4 |
| if (SvROK(*ns) && SvTYPE(SvRV(*ns)) == SVt_PVCV) { |
2 |
0 |
2 |
|
| 87 |
2 |
| store = NULL; |
| 88 |
2 |
| cache = newRV_noinc((SV *)newHV()); |
| 89 |
|
| } |
| 90 |
|
| else { |
| 91 |
2 |
| argv[0] = sv_2mortal(newSVpvs("namespace")); |
| 92 |
2 |
| argv[1] = *ns; |
| 93 |
2 |
| argv[2] = sv_2mortal(newSVpvs("store_dir")); |
| 94 |
2 |
| argv[3] = *root; |
| 95 |
2 |
| store = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Apophis")), "new", |
| 96 |
|
| argv, 4, 1); |
| 97 |
2 |
▶ | if (!(store && SvROK(store))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(store && SvROK(store))) |
2 |
0 |
2 |
| if (!(store && SvROK(store))) |
0 |
2 |
2 |
|
| 98 |
0 |
| croak("Punk::Plugin::Blob: Apophis->new gave back no store"); |
| 99 |
2 |
| sv_2mortal(store); |
| 100 |
2 |
| cache = newRV_noinc((SV *)newHV()); |
| 101 |
|
| } |
| 102 |
4 |
| sv_2mortal(cache); |
| 103 |
|
| |
| 104 |
36 |
▶ | for (i = 0; i < (int)(sizeof H / sizeof H[0]); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (int)(sizeof H / sizeof H[0]); i++) { |
32 |
4 |
36 |
|
| 105 |
|
| SV *h[3]; |
| 106 |
|
| SV *r; |
| 107 |
32 |
| cap = newAV(); |
| 108 |
32 |
▶ | av_push(cap, store ? newSVsv(store) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push(cap, store ? newSVsv(store) : newSV(0)); |
16 |
16 |
32 |
|
| 109 |
32 |
| av_push(cap, newSVsv(*root)); |
| 110 |
32 |
| av_push(cap, newSVsv(*ns)); |
| 111 |
|
| /* ONE cache across every helper, so blob_put and blob_send in the |
| 112 |
|
| * same request reach the same store rather than building two. */ |
| 113 |
32 |
| av_push(cap, newSVsv(cache)); |
| 114 |
32 |
| h[0] = sv_2mortal(newSVpv(H[i].name, 0)); |
| 115 |
32 |
| h[1] = sv_2mortal(punk_closure(aTHX_ H[i].body, cap)); |
| 116 |
32 |
| h[2] = sv_2mortal(newSVpvs("Punk::Plugin::Blob")); |
| 117 |
32 |
| r = pcx_call_meth(aTHX_ app, "helper", h, 3, 1); |
| 118 |
32 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
32 |
0 |
32 |
|
| 119 |
|
| } |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| # sweep($root_or_app, live => sub {...}, %opts) -> (removed, kept) |
| 123 |
|
| # |
| 124 |
|
| # Nothing unlinks during a request; this is where bytes actually go. The |
| 125 |
|
| # refusals come before the work: the callback is called first, and its failure |
| 126 |
|
| # or emptiness aborts before a directory is opened. |
| 127 |
|
| void |
| 128 |
|
| sweep(class, where, ...) |
| 129 |
|
| SV *class |
| 130 |
|
| SV *where |
| 131 |
|
| PPCODE: |
| 132 |
|
| { |
| 133 |
11 |
| SV *cb = NULL, *root = NULL; |
| 134 |
11 |
| double grace = 3600.0; |
| 135 |
11 |
| int dry = 0, allow_empty = 0, i; |
| 136 |
|
| HV *live; |
| 137 |
11 |
| UV removed = 0, kept = 0; |
| 138 |
|
| |
| 139 |
|
| PERL_UNUSED_VAR(class); |
| 140 |
36 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
25 |
11 |
36 |
|
| 141 |
25 |
| const char *k = SvPV_nolen(ST(i)); |
| 142 |
25 |
| SV *v = ST(i + 1); |
| 143 |
25 |
▶ | if (strEQ(k, "live")) cb = v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(k, "live")) cb = v; |
11 |
14 |
25 |
|
| 144 |
14 |
▶ | else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; |
11 |
3 |
14 |
| else if (strEQ(k, "grace")) grace = SvOK(v) ? SvNV(v) : 0.0; |
11 |
0 |
11 |
|
| 145 |
3 |
▶ | else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0; |
1 |
2 |
3 |
|
| 146 |
2 |
▶ | else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(k, "allow_empty")) allow_empty = SvTRUE(v) ? 1 : 0; |
2 |
0 |
2 |
|
| 147 |
|
| } |
| 148 |
11 |
▶ | if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
11 |
0 |
11 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
11 |
0 |
11 |
| if (!(cb && SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV)) |
0 |
11 |
11 |
|
| 149 |
0 |
| croak("Punk::Plugin::Blob: sweep needs live => sub { ... } " |
| 150 |
|
| "returning the blob ids still referenced"); |
| 151 |
|
| |
| 152 |
|
| /* an app, or the root itself */ |
| 153 |
11 |
▶ | if (SvROK(where)) root = sv_2mortal(pcx_call_meth(aTHX_ where, |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(where)) root = sv_2mortal(pcx_call_meth(aTHX_ where, |
0 |
11 |
11 |
|
| 154 |
|
| "blob_root", NULL, 0, 1)); |
| 155 |
11 |
| else root = where; |
| 156 |
11 |
▶ | if (!(root && SvOK(root) && SvCUR(root))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(root && SvOK(root) && SvCUR(root))) |
11 |
0 |
11 |
| if (!(root && SvOK(root) && SvCUR(root))) |
11 |
0 |
11 |
| if (!(root && SvOK(root) && SvCUR(root))) |
0 |
11 |
11 |
|
| 157 |
0 |
| croak("Punk::Plugin::Blob: sweep needs the store root"); |
| 158 |
|
| |
| 159 |
11 |
| live = pb_live_set(aTHX_ cb, allow_empty); |
| 160 |
8 |
| sv_2mortal((SV *)live); |
| 161 |
8 |
| pb_sweep(aTHX_ SvPV_nolen(root), live, grace, dry, &removed, &kept); |
| 162 |
|
| |
| 163 |
8 |
| SP = PL_stack_base + ax - 1; /* the live callback ran */ |
| 164 |
8 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
8 |
8 |
|
| 165 |
8 |
| mPUSHu(removed); |
| 166 |
8 |
| mPUSHu(kept); |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| # The type a blob may be served inline as, or undef. Raster images only; see |
| 170 |
|
| # punk_blob.h for why SVG and PDF are not on the list. |
| 171 |
|
| SV * |
| 172 |
|
| safe_inline_type(class, claimed) |
| 173 |
|
| SV *class |
| 174 |
|
| SV *claimed |
| 175 |
|
| CODE: |
| 176 |
|
| { |
| 177 |
6 |
| STRLEN l = 0; |
| 178 |
12 |
▶ | const char *s = SvOK(claimed) ? SvPV_const(claimed, l) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *s = SvOK(claimed) ? SvPV_const(claimed, l) : NULL; |
6 |
0 |
6 |
|
| 179 |
6 |
| const char *ok = pb_inline_ok(s, l); |
| 180 |
|
| PERL_UNUSED_VAR(class); |
| 181 |
6 |
▶ | if (!ok) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) XSRETURN_UNDEF; |
3 |
3 |
6 |
|
| 182 |
3 |
| RETVAL = newSVpv(ok, 0); |
| 183 |
|
| } |
| 184 |
|
| OUTPUT: |
| 185 |
|
| RETVAL |
| 186 |
|
| |
| 187 |
|
| # Is this a blob id? The guard between a request and a filesystem path. |
| 188 |
|
| IV |
| 189 |
|
| _id_ok(class, id) |
| 190 |
|
| SV *class |
| 191 |
|
| SV *id |
| 192 |
|
| CODE: |
| 193 |
|
| { |
| 194 |
12 |
| STRLEN l = 0; |
| 195 |
24 |
▶ | const char *s = SvOK(id) ? SvPV_const(id, l) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *s = SvOK(id) ? SvPV_const(id, l) : NULL; |
12 |
0 |
12 |
|
| 196 |
|
| PERL_UNUSED_VAR(class); |
| 197 |
12 |
▶ | RETVAL = pb_id_ok(s, l); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = pb_id_ok(s, l); |
8 |
4 |
12 |
|
| 198 |
|
| } |
| 199 |
|
| OUTPUT: |
| 200 |
|
| RETVAL |
| 201 |
|
| |
| 202 |
|
| # The store seam the tests assert through. |
| 203 |
|
| SV * |
| 204 |
|
| _store(class, ca, content) |
| 205 |
|
| SV *class |
| 206 |
|
| SV *ca |
| 207 |
|
| SV *content |
| 208 |
|
| CODE: |
| 209 |
|
| { |
| 210 |
|
| PERL_UNUSED_VAR(class); |
| 211 |
25 |
▶ | if (!(ca && SvROK(ca))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(ca && SvROK(ca))) |
25 |
0 |
25 |
| if (!(ca && SvROK(ca))) |
0 |
25 |
25 |
|
| 212 |
0 |
| croak("Punk::Plugin::Blob: need an Apophis store"); |
| 213 |
25 |
▶ | if (!(content && SvROK(content))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(content && SvROK(content))) |
25 |
0 |
25 |
| if (!(content && SvROK(content))) |
0 |
25 |
25 |
|
| 214 |
0 |
| croak("Punk::Plugin::Blob: content must be a scalar reference"); |
| 215 |
25 |
| RETVAL = pb_store(aTHX_ ca, content); |
| 216 |
|
| } |
| 217 |
|
| OUTPUT: |
| 218 |
|
| RETVAL |