| 1 |
|
| /* |
| 2 |
|
| * Apophis.xs - Content-addressable storage with deterministic UUID v5 |
| 3 |
|
| * |
| 4 |
|
| * Named after Apophis, the Egyptian serpent of chaos — here tamed |
| 5 |
|
| * to bring order to content through deterministic hashing. |
| 6 |
|
| * |
| 7 |
|
| * 100% XS: all logic in C, Perl layer is just XSLoader. |
| 8 |
|
| * Uses Horus library for RFC 9562 UUID v5 (SHA-1 namespace) generation. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #define PERL_NO_GET_CONTEXT |
| 12 |
|
| #include "EXTERN.h" |
| 13 |
|
| #include "perl.h" |
| 14 |
|
| #include "XSUB.h" |
| 15 |
|
| |
| 16 |
|
| #include "ppport.h" |
| 17 |
|
| #include "xop_compat.h" |
| 18 |
|
| |
| 19 |
|
| #include <sys/stat.h> |
| 20 |
|
| #include <errno.h> |
| 21 |
|
| #ifndef _WIN32 |
| 22 |
|
| # include <unistd.h> |
| 23 |
|
| #else |
| 24 |
|
| # include <io.h> |
| 25 |
|
| # include <direct.h> |
| 26 |
|
| # include <windows.h> /* MoveFileEx; perl.h has it already, this is a guard */ |
| 27 |
|
| #endif |
| 28 |
|
| |
| 29 |
|
| /* |
| 30 |
|
| * Windows compatibility: |
| 31 |
|
| * perl.h on threaded Windows redefines mkdir, stat, unlink etc. as macros |
| 32 |
|
| * requiring the interpreter context (my_perl). Our static C helper functions |
| 33 |
|
| * don't have pTHX, so we undo those overrides and use the real libc calls. |
| 34 |
|
| */ |
| 35 |
|
| #ifdef WIN32 |
| 36 |
|
| # undef mkdir |
| 37 |
|
| # undef stat |
| 38 |
|
| # undef unlink |
| 39 |
|
| # undef rename |
| 40 |
|
| # undef open |
| 41 |
|
| # undef close |
| 42 |
|
| # undef read |
| 43 |
|
| # undef write |
| 44 |
|
| /* Windows mkdir takes only one arg */ |
| 45 |
|
| # define apophis_mkdir(p, m) _mkdir(p) |
| 46 |
|
| /* |
| 47 |
|
| * Stat_t is struct w32_stat, the layout perl's own stat() fills in - but |
| 48 |
|
| * that stat() is the macro undef'd just above, so what is left is the CRT |
| 49 |
|
| * one, which writes a differently shaped struct. Pair the call and the |
| 50 |
|
| * type instead: _stat64 keeps st_size 64 bit, which the CRT's struct stat |
| 51 |
|
| * does not on Win64. |
| 52 |
|
| */ |
| 53 |
|
| # define apophis_stat_t struct _stat64 |
| 54 |
|
| # define apophis_stat(p, b) _stat64((p), (b)) |
| 55 |
|
| #else |
| 56 |
|
| # define apophis_mkdir(p, m) mkdir(p, m) |
| 57 |
|
| # define apophis_stat_t struct stat |
| 58 |
|
| # define apophis_stat(p, b) stat((p), (b)) |
| 59 |
|
| #endif |
| 60 |
|
| |
| 61 |
|
| /* |
| 62 |
|
| * The CRT rename() refuses a destination that already exists, so the second |
| 63 |
|
| * store of the same content with metadata could not replace its sidecar. |
| 64 |
|
| * MoveFileEx replaces, and stays atomic. |
| 65 |
|
| */ |
| 66 |
|
| static int |
| 67 |
33 |
| apophis_rename(const char *from, const char *to) |
| 68 |
|
| { |
| 69 |
|
| #ifdef WIN32 |
| 70 |
|
| if (MoveFileExA(from, to, MOVEFILE_REPLACE_EXISTING)) |
| 71 |
|
| return 0; |
| 72 |
|
| errno = EACCES; |
| 73 |
|
| return -1; |
| 74 |
|
| #else |
| 75 |
33 |
| return rename(from, to); |
| 76 |
|
| #endif |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* Horus UUID library - pure C, no Perl deps */ |
| 80 |
|
| #define HORUS_FATAL(msg) croak("%s", (msg)) |
| 81 |
|
| #include "horus_core.h" |
| 82 |
|
| |
| 83 |
|
| /* ------------------------------------------------------------------ */ |
| 84 |
|
| /* Constants */ |
| 85 |
|
| /* ------------------------------------------------------------------ */ |
| 86 |
|
| |
| 87 |
|
| #define APOPHIS_STREAM_BUF 65536 /* 64KB read chunks for streaming */ |
| 88 |
|
| #define APOPHIS_PATH_MAX 4096 |
| 89 |
|
| |
| 90 |
|
| /* ------------------------------------------------------------------ */ |
| 91 |
|
| /* Internal: namespace UUID generation */ |
| 92 |
|
| /* ------------------------------------------------------------------ */ |
| 93 |
|
| |
| 94 |
|
| /* Derive a namespace UUID from a human-readable string via v5(DNS, name) */ |
| 95 |
|
| static void |
| 96 |
37 |
| apophis_derive_namespace(unsigned char *ns_out, |
| 97 |
|
| const char *name, STRLEN name_len) |
| 98 |
|
| { |
| 99 |
37 |
| horus_uuid_v5(ns_out, HORUS_NS_DNS, |
| 100 |
|
| (const unsigned char *)name, (size_t)name_len); |
| 101 |
37 |
| } |
| 102 |
|
| |
| 103 |
|
| /* Format 16-byte UUID binary to 36-char string SV */ |
| 104 |
|
| static SV * |
| 105 |
38 |
| apophis_uuid_to_sv(pTHX_ const unsigned char *uuid) |
| 106 |
|
| { |
| 107 |
|
| char buf[HORUS_FMT_STR_LEN + 1]; |
| 108 |
38 |
| horus_format_uuid(buf, uuid, HORUS_FMT_STR); |
| 109 |
38 |
| return newSVpvn(buf, HORUS_FMT_STR_LEN); |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* ------------------------------------------------------------------ */ |
| 113 |
|
| /* Internal: content identification */ |
| 114 |
|
| /* ------------------------------------------------------------------ */ |
| 115 |
|
| |
| 116 |
|
| /* Identify in-memory content: v5(namespace, content) */ |
| 117 |
|
| static void |
| 118 |
75 |
| apophis_identify_content(unsigned char *uuid_out, |
| 119 |
|
| const unsigned char *ns_bytes, |
| 120 |
|
| const char *content, STRLEN content_len) |
| 121 |
|
| { |
| 122 |
75 |
| horus_uuid_v5(uuid_out, ns_bytes, |
| 123 |
|
| (const unsigned char *)content, (size_t)content_len); |
| 124 |
75 |
| } |
| 125 |
|
| |
| 126 |
|
| /* Identify via streaming SHA-1 — O(1) memory */ |
| 127 |
|
| static void |
| 128 |
17 |
| apophis_identify_stream(pTHX_ unsigned char *uuid_out, |
| 129 |
|
| const unsigned char *ns_bytes, |
| 130 |
|
| PerlIO *fh) |
| 131 |
|
| { |
| 132 |
|
| horus_sha1_ctx ctx; |
| 133 |
|
| unsigned char buf[APOPHIS_STREAM_BUF]; |
| 134 |
|
| unsigned char digest[20]; |
| 135 |
|
| SSize_t nread; |
| 136 |
|
| |
| 137 |
17 |
| horus_sha1_init(&ctx); |
| 138 |
17 |
| horus_sha1_update(&ctx, ns_bytes, 16); /* namespace first per RFC */ |
| 139 |
|
| |
| 140 |
37 |
▶ | while ((nread = PerlIO_read(fh, buf, sizeof(buf))) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((nread = PerlIO_read(fh, buf, sizeof(buf))) > 0) { |
20 |
17 |
37 |
|
| 141 |
20 |
| horus_sha1_update(&ctx, (const unsigned char *)buf, (size_t)nread); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
17 |
| horus_sha1_final(digest, &ctx); |
| 145 |
17 |
| memcpy(uuid_out, digest, 16); |
| 146 |
17 |
| horus_stamp_version_variant(uuid_out, 5); |
| 147 |
17 |
| } |
| 148 |
|
| |
| 149 |
|
| /* ------------------------------------------------------------------ */ |
| 150 |
|
| /* Internal: path computation */ |
| 151 |
|
| /* ------------------------------------------------------------------ */ |
| 152 |
|
| |
| 153 |
|
| /* Build 2-level sharded path: store_dir/a3/bb/a3bb189e-...-1e3a |
| 154 |
|
| * Returns length written (excluding NUL). */ |
| 155 |
|
| static int |
| 156 |
134 |
| apophis_build_path(char *out, size_t out_size, |
| 157 |
|
| const char *store_dir, STRLEN store_len, |
| 158 |
|
| const char *id, STRLEN id_len) |
| 159 |
|
| { |
| 160 |
|
| /* UUID is 36 chars: a3bb189e-8bf9-5f18-b3f6-1b2f5f5c1e3a |
| 161 |
|
| * Shard on first 2 and chars 3-4 (skipping no hyphens needed, |
| 162 |
|
| * first 4 hex chars are positions 0-3 of the UUID string) */ |
| 163 |
134 |
▶ | if (id_len < 5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (id_len < 5) |
1 |
133 |
134 |
|
| 164 |
1 |
| croak("Apophis: invalid UUID id"); |
| 165 |
|
| |
| 166 |
266 |
| return snprintf(out, out_size, "%.*s/%c%c/%c%c/%.*s", |
| 167 |
|
| (int)store_len, store_dir, |
| 168 |
133 |
| id[0], id[1], /* first shard level */ |
| 169 |
133 |
| id[2], id[3], /* second shard level */ |
| 170 |
|
| (int)id_len, id); |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
|
| /* The same for the XSUBs, which have nowhere to report a truncated path: a |
| 174 |
|
| * path that does not fit, with room left for the .tmp.<pid> and .meta |
| 175 |
|
| * suffixes written beside it, is a different file, so it croaks. */ |
| 176 |
|
| #define APOPHIS_PATH_ROOM 24 |
| 177 |
|
| |
| 178 |
|
| static int |
| 179 |
129 |
| apophis_path(char *out, size_t out_size, |
| 180 |
|
| const char *store_dir, STRLEN store_len, |
| 181 |
|
| const char *id, STRLEN id_len) |
| 182 |
|
| { |
| 183 |
129 |
| int len = apophis_build_path(out, out_size, store_dir, store_len, id, id_len); |
| 184 |
128 |
▶ | if (len < 0 || (size_t)len + APOPHIS_PATH_ROOM >= out_size) /* # uncoverable condition left count:1 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 0 || (size_t)len + APOPHIS_PATH_ROOM >= out_size) /* # uncoverable condition left count:1 */ |
128 |
0 |
128 |
| if (len < 0 || (size_t)len + APOPHIS_PATH_ROOM >= out_size) /* # uncoverable condition left count:1 */ |
1 |
127 |
128 |
|
| 185 |
1 |
| croak("Apophis: path too long"); |
| 186 |
127 |
| return len; |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| /* Build path for .meta sidecar */ |
| 190 |
|
| static int |
| 191 |
23 |
| apophis_build_meta_path(char *out, size_t out_size, |
| 192 |
|
| const char *content_path, int content_path_len) |
| 193 |
|
| { |
| 194 |
23 |
| return snprintf(out, out_size, "%.*s.meta", |
| 195 |
|
| content_path_len, content_path); |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
|
| /* ------------------------------------------------------------------ */ |
| 199 |
|
| /* Internal: recursive mkdir */ |
| 200 |
|
| /* ------------------------------------------------------------------ */ |
| 201 |
|
| |
| 202 |
|
| static void |
| 203 |
33 |
| apophis_mkdir_p(const char *path) |
| 204 |
|
| { |
| 205 |
|
| char buf[APOPHIS_PATH_MAX]; |
| 206 |
|
| char *p; |
| 207 |
|
| size_t len; |
| 208 |
|
| |
| 209 |
33 |
| len = strlen(path); |
| 210 |
33 |
| memcpy(buf, path, len + 1); |
| 211 |
|
| |
| 212 |
730 |
▶ | for (p = buf + 1; *p; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = buf + 1; *p; p++) { |
698 |
32 |
730 |
|
| 213 |
698 |
▶ | if (*p == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '/') { |
106 |
592 |
698 |
|
| 214 |
106 |
| *p = '\0'; |
| 215 |
106 |
▶ | if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
79 |
27 |
106 |
| if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
1 |
78 |
79 |
|
| 216 |
1 |
| croak("Apophis: cannot create directory '%s': %s", |
| 217 |
|
| buf, strerror(errno)); |
| 218 |
|
| } |
| 219 |
105 |
| *p = '/'; |
| 220 |
|
| } |
| 221 |
|
| } |
| 222 |
|
| /* Final component */ |
| 223 |
32 |
▶ | if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
7 |
25 |
32 |
| if (apophis_mkdir(buf, 0777) != 0 && errno != EEXIST) { |
1 |
6 |
7 |
|
| 224 |
1 |
| croak("Apophis: cannot create directory '%s': %s", |
| 225 |
|
| buf, strerror(errno)); |
| 226 |
|
| } |
| 227 |
31 |
| } |
| 228 |
|
| |
| 229 |
|
| /* Ensure parent directory of a file path exists */ |
| 230 |
|
| static void |
| 231 |
35 |
| apophis_ensure_parent_dir(const char *file_path) |
| 232 |
|
| { |
| 233 |
|
| char buf[APOPHIS_PATH_MAX]; |
| 234 |
|
| char *last_slash; |
| 235 |
|
| size_t len; |
| 236 |
|
| |
| 237 |
35 |
| len = strlen(file_path); |
| 238 |
35 |
▶ | if (len >= sizeof(buf)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= sizeof(buf)) |
1 |
34 |
35 |
|
| 239 |
1 |
| croak("Apophis: path too long"); |
| 240 |
|
| |
| 241 |
34 |
| memcpy(buf, file_path, len + 1); |
| 242 |
34 |
| last_slash = strrchr(buf, '/'); |
| 243 |
34 |
▶ | if (last_slash) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_slash) { |
33 |
1 |
34 |
|
| 244 |
33 |
| *last_slash = '\0'; |
| 245 |
33 |
| apophis_mkdir_p(buf); |
| 246 |
|
| } |
| 247 |
32 |
| } |
| 248 |
|
| |
| 249 |
|
| /* ------------------------------------------------------------------ */ |
| 250 |
|
| /* Internal: atomic file write (temp + rename) */ |
| 251 |
|
| /* ------------------------------------------------------------------ */ |
| 252 |
|
| |
| 253 |
|
| static void |
| 254 |
32 |
| apophis_atomic_write(pTHX_ const char *path, |
| 255 |
|
| const char *content, STRLEN content_len) |
| 256 |
|
| { |
| 257 |
|
| char tmp_path[APOPHIS_PATH_MAX]; |
| 258 |
|
| PerlIO *fh; |
| 259 |
|
| SSize_t written; |
| 260 |
|
| |
| 261 |
32 |
| snprintf(tmp_path, sizeof(tmp_path), "%s.tmp.%d", |
| 262 |
32 |
| path, (int)getpid()); |
| 263 |
|
| |
| 264 |
32 |
| fh = PerlIO_open(tmp_path, "wb"); |
| 265 |
32 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
1 |
31 |
32 |
|
| 266 |
1 |
| croak("Apophis: cannot write '%s': %s", tmp_path, strerror(errno)); |
| 267 |
|
| |
| 268 |
31 |
| written = PerlIO_write(fh, content, content_len); |
| 269 |
31 |
| PerlIO_close(fh); |
| 270 |
|
| |
| 271 |
|
| /* |
| 272 |
|
| # uncoverable branch true |
| 273 |
|
| # uncoverable condition left count:1 */ |
| 274 |
31 |
▶ | if (written != (SSize_t)content_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (written != (SSize_t)content_len) { |
0 |
31 |
31 |
|
| 275 |
0 |
| unlink(tmp_path); /* # uncoverable statement */ |
| 276 |
0 |
| croak("Apophis: short write to '%s'", tmp_path); /* # uncoverable statement */ |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
31 |
▶ | if (apophis_rename(tmp_path, path) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_rename(tmp_path, path) != 0) { |
1 |
30 |
31 |
|
| 280 |
1 |
| unlink(tmp_path); |
| 281 |
1 |
| croak("Apophis: cannot rename '%s' -> '%s': %s", |
| 282 |
|
| tmp_path, path, strerror(errno)); |
| 283 |
|
| } |
| 284 |
30 |
| } |
| 285 |
|
| |
| 286 |
|
| /* ------------------------------------------------------------------ */ |
| 287 |
|
| /* Internal: metadata sidecar (key=value\n format) */ |
| 288 |
|
| /* ------------------------------------------------------------------ */ |
| 289 |
|
| |
| 290 |
|
| static void |
| 291 |
3 |
| apophis_meta_write(pTHX_ const char *meta_path, HV *meta) |
| 292 |
|
| { |
| 293 |
|
| PerlIO *fh; |
| 294 |
|
| HE *entry; |
| 295 |
|
| char tmp_path[APOPHIS_PATH_MAX]; |
| 296 |
|
| |
| 297 |
3 |
| snprintf(tmp_path, sizeof(tmp_path), "%s.tmp.%d", |
| 298 |
3 |
| meta_path, (int)getpid()); |
| 299 |
|
| |
| 300 |
3 |
| fh = PerlIO_open(tmp_path, "w"); |
| 301 |
3 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
1 |
2 |
3 |
|
| 302 |
1 |
| croak("Apophis: cannot write metadata '%s': %s", |
| 303 |
|
| tmp_path, strerror(errno)); |
| 304 |
|
| |
| 305 |
2 |
| hv_iterinit(meta); |
| 306 |
5 |
▶ | while ((entry = hv_iternext(meta))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((entry = hv_iternext(meta))) { |
3 |
2 |
5 |
|
| 307 |
3 |
| SV *val = hv_iterval(meta, entry); |
| 308 |
|
| I32 klen; |
| 309 |
3 |
| const char *key = hv_iterkey(entry, &klen); |
| 310 |
|
| STRLEN vlen; |
| 311 |
3 |
| const char *vstr = SvPV(val, vlen); |
| 312 |
3 |
| PerlIO_write(fh, key, (SSize_t)klen); |
| 313 |
3 |
| PerlIO_write(fh, "=", 1); |
| 314 |
3 |
| PerlIO_write(fh, vstr, (SSize_t)vlen); |
| 315 |
3 |
| PerlIO_write(fh, "\n", 1); |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
2 |
| PerlIO_close(fh); |
| 319 |
|
| |
| 320 |
2 |
▶ | if (apophis_rename(tmp_path, meta_path) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_rename(tmp_path, meta_path) != 0) { |
1 |
1 |
2 |
|
| 321 |
1 |
| unlink(tmp_path); |
| 322 |
1 |
| croak("Apophis: cannot rename metadata '%s': %s", |
| 323 |
|
| tmp_path, strerror(errno)); |
| 324 |
|
| } |
| 325 |
1 |
| } |
| 326 |
|
| |
| 327 |
|
| static HV * |
| 328 |
9 |
| apophis_meta_read(pTHX_ const char *meta_path) |
| 329 |
|
| { |
| 330 |
|
| PerlIO *fh; |
| 331 |
|
| HV *meta; |
| 332 |
|
| apophis_stat_t st; |
| 333 |
|
| char *buf, *p, *end; |
| 334 |
|
| SSize_t nread; |
| 335 |
|
| |
| 336 |
|
| /* A directory opens, and on the BSDs, Cygwin and the Hurd reads |
| 337 |
|
| * too, so it is refused by its mode rather than by the read. */ |
| 338 |
9 |
▶ | if (apophis_stat(meta_path, &st) != 0 || S_ISDIR(st.st_mode)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (apophis_stat(meta_path, &st) != 0 || S_ISDIR(st.st_mode)) return NULL; |
4 |
5 |
9 |
| if (apophis_stat(meta_path, &st) != 0 || S_ISDIR(st.st_mode)) return NULL; |
1 |
3 |
4 |
|
| 339 |
|
| |
| 340 |
3 |
| fh = PerlIO_open(meta_path, "r"); |
| 341 |
3 |
▶ | if (!fh) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) return NULL; |
1 |
2 |
3 |
|
| 342 |
|
| |
| 343 |
2 |
| buf = (char *)malloc((size_t)st.st_size + 1); |
| 344 |
|
| /* |
| 345 |
|
| # uncoverable branch true |
| 346 |
|
| # uncoverable condition left count:1 */ |
| 347 |
2 |
▶ | if (!buf) { PerlIO_close(fh); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) { PerlIO_close(fh); return NULL; } |
0 |
2 |
2 |
|
| 348 |
|
| |
| 349 |
2 |
| nread = PerlIO_read(fh, buf, (Size_t)st.st_size); |
| 350 |
|
| /* A read that fails after the open and the stat succeeded is |
| 351 |
|
| * nothing a test can arrange on a file. |
| 352 |
|
| # uncoverable branch true |
| 353 |
|
| # uncoverable condition left count:1 |
| 354 |
|
| # uncoverable condition left count:2 */ |
| 355 |
2 |
▶ | if (nread < 0 || PerlIO_error(fh)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nread < 0 || PerlIO_error(fh)) { |
2 |
0 |
2 |
| if (nread < 0 || PerlIO_error(fh)) { |
0 |
2 |
2 |
|
| 356 |
0 |
| PerlIO_close(fh); /* # uncoverable statement */ |
| 357 |
0 |
| free(buf); /* # uncoverable statement */ |
| 358 |
0 |
| return NULL; /* # uncoverable statement */ |
| 359 |
|
| } |
| 360 |
2 |
| PerlIO_close(fh); |
| 361 |
2 |
| buf[nread] = '\0'; |
| 362 |
|
| |
| 363 |
2 |
| meta = newHV(); |
| 364 |
2 |
| p = buf; |
| 365 |
2 |
| end = buf + nread; |
| 366 |
|
| |
| 367 |
7 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
5 |
2 |
7 |
|
| 368 |
5 |
| char *line_end = strchr(p, '\n'); |
| 369 |
|
| char *eq; |
| 370 |
5 |
▶ | if (!line_end) line_end = end; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!line_end) line_end = end; |
1 |
4 |
5 |
|
| 371 |
|
| |
| 372 |
5 |
| eq = (char *)memchr(p, '=', (size_t)(line_end - p)); |
| 373 |
5 |
▶ | if (eq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eq) { |
4 |
1 |
5 |
|
| 374 |
4 |
| hv_store(meta, p, (I32)(eq - p), |
| 375 |
|
| newSVpvn(eq + 1, (STRLEN)(line_end - eq - 1)), 0); |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
5 |
| p = line_end + 1; |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
2 |
| free(buf); |
| 382 |
2 |
| return meta; |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| /* ------------------------------------------------------------------ */ |
| 386 |
|
| /* Internal: object field accessors */ |
| 387 |
|
| /* ------------------------------------------------------------------ */ |
| 388 |
|
| |
| 389 |
|
| /* Get the 16-byte namespace bytes from the object */ |
| 390 |
|
| static const unsigned char * |
| 391 |
100 |
| apophis_get_ns(pTHX_ HV *self) |
| 392 |
|
| { |
| 393 |
100 |
| SV **svp = hv_fetchs(self, "_ns_bytes", 0); |
| 394 |
100 |
▶ | if (!svp || !SvOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!svp || !SvOK(*svp)) |
99 |
1 |
100 |
| if (!svp || !SvOK(*svp)) |
1 |
98 |
99 |
|
| 395 |
2 |
| croak("Apophis: object has no namespace (not properly constructed)"); |
| 396 |
196 |
| return (const unsigned char *)SvPV_nolen(*svp); |
| 397 |
|
| } |
| 398 |
|
| |
| 399 |
|
| /* Get store_dir from object, or from opts, or croak */ |
| 400 |
|
| static const char * |
| 401 |
128 |
| apophis_get_store_dir(pTHX_ HV *self, HV *opts, STRLEN *len_out) |
| 402 |
|
| { |
| 403 |
|
| SV **svp; |
| 404 |
|
| |
| 405 |
|
| /* Check opts first */ |
| 406 |
128 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
20 |
108 |
128 |
|
| 407 |
20 |
| svp = hv_fetchs(opts, "store_dir", 0); |
| 408 |
20 |
▶ | if (svp && SvOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) |
14 |
6 |
20 |
| if (svp && SvOK(*svp)) |
13 |
1 |
14 |
|
| 409 |
26 |
| return SvPV(*svp, *len_out); |
| 410 |
|
| } |
| 411 |
|
| |
| 412 |
|
| /* Fall back to object */ |
| 413 |
115 |
| svp = hv_fetchs(self, "store_dir", 0); |
| 414 |
115 |
▶ | if (svp && SvOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) |
112 |
3 |
115 |
| if (svp && SvOK(*svp)) |
111 |
1 |
112 |
|
| 415 |
222 |
| return SvPV(*svp, *len_out); |
| 416 |
|
| |
| 417 |
4 |
| croak("Apophis: no store_dir specified"); |
| 418 |
|
| return NULL; /* not reached */ |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
|
| |
| 422 |
|
| /* ================================================================== */ |
| 423 |
|
| /* The six fused bodies */ |
| 424 |
|
| /* */ |
| 425 |
|
| /* identify, store, exists, fetch, verify and remove each have one */ |
| 426 |
|
| /* body here. The XSUB of that name runs it, and so does the custom op */ |
| 427 |
|
| /* a call to it compiles to under `use Apophis`. `who` is the name a */ |
| 428 |
|
| /* croak reports. `args` and `items` are the XSUB's stack, for the */ |
| 429 |
|
| /* key/value options after the positional arguments; the op passes */ |
| 430 |
|
| /* NULL and 2, since it only stands for a call with none. */ |
| 431 |
|
| /* ================================================================== */ |
| 432 |
|
| |
| 433 |
|
| static HV * |
| 434 |
158 |
| apophis_self_hv(pTHX_ const char *who, SV *self) |
| 435 |
|
| { |
| 436 |
158 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
13 |
145 |
158 |
|
| 437 |
13 |
| croak("%s: not an object", who); |
| 438 |
145 |
| return (HV *)SvRV(self); |
| 439 |
|
| } |
| 440 |
|
| |
| 441 |
|
| /* The trailing key/value pairs as a mortal HV, or NULL when there are |
| 442 |
|
| * none. When `meta` is given, a `meta` hashref goes there instead. */ |
| 443 |
|
| static HV * |
| 444 |
107 |
| apophis_opts(pTHX_ const char *who, SV **args, I32 items, HV **meta) |
| 445 |
|
| { |
| 446 |
|
| HV *opts; |
| 447 |
|
| I32 i; |
| 448 |
|
| |
| 449 |
107 |
▶ | if (items <= 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items <= 2) |
84 |
23 |
107 |
|
| 450 |
84 |
| return NULL; |
| 451 |
23 |
▶ | if ((items - 2) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2 != 0) |
7 |
16 |
23 |
|
| 452 |
7 |
| croak("%s: odd number of optional arguments", who); |
| 453 |
16 |
| opts = (HV *)sv_2mortal((SV *)newHV()); |
| 454 |
32 |
▶ | for (i = 2; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i < items; i += 2) { |
16 |
16 |
32 |
|
| 455 |
|
| STRLEN klen; |
| 456 |
16 |
| const char *k = SvPV(args[i], klen); |
| 457 |
16 |
| SV *v = args[i + 1]; |
| 458 |
16 |
▶ | if (meta && strEQ(k, "meta") && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (meta && strEQ(k, "meta") && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
8 |
8 |
16 |
| if (meta && strEQ(k, "meta") && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
6 |
2 |
8 |
| if (meta && strEQ(k, "meta") && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
5 |
1 |
6 |
| if (meta && strEQ(k, "meta") && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
3 |
2 |
5 |
|
| 459 |
3 |
| *meta = (HV *)SvRV(v); |
| 460 |
|
| else |
| 461 |
13 |
| (void)hv_store(opts, k, klen, SvREFCNT_inc(v), 0); |
| 462 |
|
| } |
| 463 |
16 |
| return opts; |
| 464 |
|
| } |
| 465 |
|
| |
| 466 |
|
| static const char * |
| 467 |
71 |
| apophis_content_of(pTHX_ const char *who, SV *content_ref, STRLEN *len) |
| 468 |
|
| { |
| 469 |
71 |
▶ | if (!SvROK(content_ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(content_ref)) |
6 |
65 |
71 |
|
| 470 |
6 |
| croak("%s: argument must be a scalar reference", who); |
| 471 |
130 |
| return SvPV(SvRV(content_ref), *len); |
| 472 |
|
| } |
| 473 |
|
| |
| 474 |
|
| static void |
| 475 |
39 |
| apophis_do_identify(pTHX_ const char *who, SV *self, SV *content_ref, unsigned char *uuid) |
| 476 |
|
| { |
| 477 |
39 |
| HV *hv = apophis_self_hv(aTHX_ who, self); |
| 478 |
36 |
| const unsigned char *ns = apophis_get_ns(aTHX_ hv); |
| 479 |
|
| STRLEN content_len; |
| 480 |
34 |
| const char *content = apophis_content_of(aTHX_ who, content_ref, &content_len); |
| 481 |
|
| |
| 482 |
30 |
| apophis_identify_content(uuid, ns, content, content_len); |
| 483 |
30 |
| } |
| 484 |
|
| |
| 485 |
|
| /* Writes the id, HORUS_FMT_STR_LEN bytes and a NUL, into id_str. */ |
| 486 |
|
| static void |
| 487 |
39 |
| apophis_do_store(pTHX_ const char *who, SV *self, SV *content_ref, SV **args, I32 items, char *id_str) |
| 488 |
|
| { |
| 489 |
39 |
| HV *hv = apophis_self_hv(aTHX_ who, self); |
| 490 |
37 |
| const unsigned char *ns = apophis_get_ns(aTHX_ hv); |
| 491 |
|
| STRLEN content_len, store_dir_len; |
| 492 |
37 |
| const char *content = apophis_content_of(aTHX_ who, content_ref, &content_len); |
| 493 |
35 |
| HV *meta = NULL; |
| 494 |
35 |
| HV *opts = apophis_opts(aTHX_ who, args, items, &meta); |
| 495 |
|
| const char *store_dir; |
| 496 |
|
| unsigned char uuid[16]; |
| 497 |
|
| char path[APOPHIS_PATH_MAX]; |
| 498 |
|
| int path_len; |
| 499 |
|
| apophis_stat_t st; |
| 500 |
|
| |
| 501 |
33 |
| apophis_identify_content(uuid, ns, content, content_len); |
| 502 |
33 |
| horus_format_uuid(id_str, uuid, HORUS_FMT_STR); |
| 503 |
|
| |
| 504 |
33 |
| store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 505 |
33 |
| path_len = apophis_path(path, sizeof(path), store_dir, store_dir_len, id_str, HORUS_FMT_STR_LEN); |
| 506 |
|
| |
| 507 |
32 |
▶ | if (apophis_stat(path, &st) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_stat(path, &st) != 0) { |
23 |
9 |
32 |
|
| 508 |
23 |
| apophis_ensure_parent_dir(path); |
| 509 |
21 |
| apophis_atomic_write(aTHX_ path, content, content_len); |
| 510 |
|
| } |
| 511 |
|
| |
| 512 |
29 |
▶ | if (meta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (meta) { |
3 |
26 |
29 |
|
| 513 |
|
| char meta_path[APOPHIS_PATH_MAX]; |
| 514 |
3 |
| apophis_build_meta_path(meta_path, sizeof(meta_path), path, path_len); |
| 515 |
3 |
| apophis_meta_write(aTHX_ meta_path, meta); |
| 516 |
|
| } |
| 517 |
27 |
| } |
| 518 |
|
| |
| 519 |
|
| /* The sharded path of an id under the store, for the four methods that |
| 520 |
|
| * take an id and options. Gives the id's bytes back for verify. */ |
| 521 |
|
| static int |
| 522 |
72 |
| apophis_id_path(pTHX_ const char *who, HV *hv, SV *id, SV **args, I32 items, |
| 523 |
|
| char *path, size_t path_size, const char **id_str, STRLEN *id_len) |
| 524 |
|
| { |
| 525 |
72 |
| HV *opts = apophis_opts(aTHX_ who, args, items, NULL); |
| 526 |
|
| STRLEN store_dir_len; |
| 527 |
67 |
| const char *store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 528 |
|
| |
| 529 |
63 |
| *id_str = SvPV(id, *id_len); |
| 530 |
63 |
| return apophis_path(path, path_size, store_dir, store_dir_len, *id_str, *id_len); |
| 531 |
|
| } |
| 532 |
|
| |
| 533 |
|
| static bool |
| 534 |
28 |
| apophis_do_exists(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items) |
| 535 |
|
| { |
| 536 |
|
| const char *id_str; |
| 537 |
|
| STRLEN id_len; |
| 538 |
|
| char path[APOPHIS_PATH_MAX]; |
| 539 |
|
| apophis_stat_t st; |
| 540 |
|
| |
| 541 |
28 |
| apophis_id_path(aTHX_ who, apophis_self_hv(aTHX_ who, self), id, args, items, path, sizeof(path), &id_str, &id_len); |
| 542 |
20 |
| return apophis_stat(path, &st) == 0; |
| 543 |
|
| } |
| 544 |
|
| |
| 545 |
|
| /* A new reference to the content, or &PL_sv_undef when there is none. */ |
| 546 |
|
| static SV * |
| 547 |
23 |
| apophis_do_fetch(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items) |
| 548 |
|
| { |
| 549 |
|
| const char *id_str; |
| 550 |
|
| STRLEN id_len; |
| 551 |
|
| char path[APOPHIS_PATH_MAX]; |
| 552 |
|
| apophis_stat_t st; |
| 553 |
|
| PerlIO *fh; |
| 554 |
|
| SV *content; |
| 555 |
|
| SSize_t nread; |
| 556 |
|
| |
| 557 |
23 |
| apophis_id_path(aTHX_ who, apophis_self_hv(aTHX_ who, self), id, args, items, path, sizeof(path), &id_str, &id_len); |
| 558 |
20 |
▶ | if (apophis_stat(path, &st) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_stat(path, &st) != 0) |
3 |
17 |
20 |
|
| 559 |
3 |
| return &PL_sv_undef; |
| 560 |
17 |
▶ | if (S_ISDIR(st.st_mode)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (S_ISDIR(st.st_mode)) |
2 |
15 |
17 |
|
| 561 |
2 |
| croak("%s: read error on '%s': %s", who, path, strerror(EISDIR)); |
| 562 |
|
| |
| 563 |
15 |
| fh = PerlIO_open(path, "rb"); |
| 564 |
15 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
2 |
13 |
15 |
|
| 565 |
2 |
| croak("%s: cannot open '%s': %s", who, path, strerror(errno)); |
| 566 |
|
| |
| 567 |
13 |
| content = newSV((STRLEN)st.st_size + 1); |
| 568 |
13 |
| SvPOK_on(content); |
| 569 |
13 |
| nread = PerlIO_read(fh, SvPVX(content), (Size_t)st.st_size); |
| 570 |
|
| /* A read that fails after the open and the stat succeeded is |
| 571 |
|
| * nothing a test can arrange on a file. |
| 572 |
|
| # uncoverable branch true |
| 573 |
|
| # uncoverable condition left count:1 |
| 574 |
|
| # uncoverable condition left count:2 */ |
| 575 |
13 |
▶ | if (nread < 0 || PerlIO_error(fh)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nread < 0 || PerlIO_error(fh)) { |
13 |
0 |
13 |
| if (nread < 0 || PerlIO_error(fh)) { |
0 |
13 |
13 |
|
| 576 |
0 |
| PerlIO_close(fh); /* # uncoverable statement */ |
| 577 |
0 |
| SvREFCNT_dec(content); /* # uncoverable statement */ |
| 578 |
0 |
| croak("%s: read error on '%s'", who, path); /* # uncoverable statement */ |
| 579 |
|
| } |
| 580 |
13 |
| PerlIO_close(fh); |
| 581 |
13 |
| SvCUR_set(content, (STRLEN)nread); |
| 582 |
13 |
| *SvEND(content) = '\0'; |
| 583 |
13 |
| return newRV_noinc(content); |
| 584 |
|
| } |
| 585 |
|
| |
| 586 |
|
| static bool |
| 587 |
15 |
| apophis_do_verify(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items) |
| 588 |
|
| { |
| 589 |
|
| const char *id_str; |
| 590 |
|
| STRLEN id_len; |
| 591 |
|
| char path[APOPHIS_PATH_MAX]; |
| 592 |
|
| PerlIO *fh; |
| 593 |
|
| unsigned char uuid[16]; |
| 594 |
|
| char recomputed[HORUS_FMT_STR_LEN + 1]; |
| 595 |
15 |
| HV *hv = apophis_self_hv(aTHX_ who, self); |
| 596 |
13 |
| const unsigned char *ns = apophis_get_ns(aTHX_ hv); |
| 597 |
|
| |
| 598 |
13 |
| apophis_id_path(aTHX_ who, hv, id, args, items, path, sizeof(path), &id_str, &id_len); |
| 599 |
12 |
| fh = PerlIO_open(path, "rb"); |
| 600 |
12 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
3 |
9 |
12 |
|
| 601 |
3 |
| return FALSE; |
| 602 |
|
| |
| 603 |
9 |
| apophis_identify_stream(aTHX_ uuid, ns, fh); |
| 604 |
9 |
| PerlIO_close(fh); |
| 605 |
9 |
| horus_format_uuid(recomputed, uuid, HORUS_FMT_STR); |
| 606 |
9 |
▶ | return id_len == HORUS_FMT_STR_LEN && memcmp(id_str, recomputed, HORUS_FMT_STR_LEN) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return id_len == HORUS_FMT_STR_LEN && memcmp(id_str, recomputed, HORUS_FMT_STR_LEN) == 0; |
8 |
1 |
9 |
| return id_len == HORUS_FMT_STR_LEN && memcmp(id_str, recomputed, HORUS_FMT_STR_LEN) == 0; |
6 |
2 |
8 |
|
| 607 |
|
| } |
| 608 |
|
| |
| 609 |
|
| static bool |
| 610 |
14 |
| apophis_do_remove(pTHX_ const char *who, SV *self, SV *id, SV **args, I32 items) |
| 611 |
|
| { |
| 612 |
|
| const char *id_str; |
| 613 |
|
| STRLEN id_len; |
| 614 |
|
| char path[APOPHIS_PATH_MAX]; |
| 615 |
|
| char meta_path[APOPHIS_PATH_MAX]; |
| 616 |
|
| int path_len, removed; |
| 617 |
|
| |
| 618 |
14 |
| path_len = apophis_id_path(aTHX_ who, apophis_self_hv(aTHX_ who, self), id, args, items, path, sizeof(path), &id_str, &id_len); |
| 619 |
11 |
| removed = unlink(path) == 0; |
| 620 |
|
| |
| 621 |
|
| /* The sidecar goes too. It need not be there. */ |
| 622 |
11 |
| apophis_build_meta_path(meta_path, sizeof(meta_path), path, path_len); |
| 623 |
11 |
| unlink(meta_path); |
| 624 |
11 |
| return removed ? TRUE : FALSE; |
| 625 |
|
| } |
| 626 |
|
| |
| 627 |
|
| |
| 628 |
|
| /* ================================================================== */ |
| 629 |
|
| /* Custom ops */ |
| 630 |
|
| /* */ |
| 631 |
|
| /* Under `use Apophis`, a call to one of the six methods above with */ |
| 632 |
|
| /* exactly its arguments compiles to a custom op in place of the */ |
| 633 |
|
| /* entersub. The op keeps entersub's kids, so the method is still */ |
| 634 |
|
| /* looked up at run time and the invocant decides. When it resolves */ |
| 635 |
|
| /* to this module's own XSUB, the op runs the body straight off the */ |
| 636 |
|
| /* stack: no sub call, no @_, and a result written into the pad slot */ |
| 637 |
|
| /* the call site owns. Anything else, a method a subclass overrode, a */ |
| 638 |
|
| /* wrapped sub, a call that turns out to carry options, runs */ |
| 639 |
|
| /* pp_entersub itself, so it behaves exactly as it would have. */ |
| 640 |
|
| /* ================================================================== */ |
| 641 |
|
| |
| 642 |
|
| #define AP_HINT_KEY "Apophis/ops" |
| 643 |
|
| |
| 644 |
|
| enum { AP_IDENTIFY, AP_STORE, AP_EXISTS, AP_FETCH, AP_VERIFY, AP_REMOVE, AP_NOPS }; |
| 645 |
|
| |
| 646 |
|
| static OP *pp_apophis_identify(pTHX); |
| 647 |
|
| static OP *pp_apophis_store(pTHX); |
| 648 |
|
| static OP *pp_apophis_exists(pTHX); |
| 649 |
|
| static OP *pp_apophis_fetch(pTHX); |
| 650 |
|
| static OP *pp_apophis_verify(pTHX); |
| 651 |
|
| static OP *pp_apophis_remove(pTHX); |
| 652 |
|
| |
| 653 |
|
| typedef struct { |
| 654 |
|
| const char *name; |
| 655 |
|
| STRLEN len; |
| 656 |
|
| int which; |
| 657 |
|
| } ap_name_t; |
| 658 |
|
| |
| 659 |
|
| /* The method names a call site may use, and the op each stands for. The |
| 660 |
|
| * op_ names are the aliases below, kept from when they were the only way |
| 661 |
|
| * to the fused bodies. */ |
| 662 |
|
| static const ap_name_t ap_names[] = { |
| 663 |
|
| { "identify", 8, AP_IDENTIFY }, { "op_identify", 11, AP_IDENTIFY }, |
| 664 |
|
| { "store", 5, AP_STORE }, { "op_store", 8, AP_STORE }, |
| 665 |
|
| { "exists", 6, AP_EXISTS }, { "op_exists", 9, AP_EXISTS }, |
| 666 |
|
| { "fetch", 5, AP_FETCH }, { "op_fetch", 8, AP_FETCH }, |
| 667 |
|
| { "verify", 6, AP_VERIFY }, { "op_verify", 9, AP_VERIFY }, |
| 668 |
|
| { "remove", 6, AP_REMOVE }, { "op_remove", 9, AP_REMOVE }, |
| 669 |
|
| }; |
| 670 |
|
| |
| 671 |
|
| static const char *const ap_method[AP_NOPS] = { |
| 672 |
|
| "identify", "store", "exists", "fetch", "verify", "remove", |
| 673 |
|
| }; |
| 674 |
|
| |
| 675 |
|
| /* The name a croak reports, by op and by whether the call used the op_ |
| 676 |
|
| * alias, which is the XSUB's ix. */ |
| 677 |
|
| static const char *const ap_who[AP_NOPS][2] = { |
| 678 |
|
| { "Apophis::identify", "Apophis::op_identify" }, |
| 679 |
|
| { "Apophis::store", "Apophis::op_store" }, |
| 680 |
|
| { "Apophis::exists", "Apophis::op_exists" }, |
| 681 |
|
| { "Apophis::fetch", "Apophis::op_fetch" }, |
| 682 |
|
| { "Apophis::verify", "Apophis::op_verify" }, |
| 683 |
|
| { "Apophis::remove", "Apophis::op_remove" }, |
| 684 |
|
| }; |
| 685 |
|
| |
| 686 |
|
| static OP *(*const ap_pp[AP_NOPS])(pTHX) = { |
| 687 |
|
| pp_apophis_identify, pp_apophis_store, pp_apophis_exists, |
| 688 |
|
| pp_apophis_fetch, pp_apophis_verify, pp_apophis_remove, |
| 689 |
|
| }; |
| 690 |
|
| |
| 691 |
|
| static const char *const ap_xop_name[AP_NOPS] = { |
| 692 |
|
| "apophis_identify", "apophis_store", "apophis_exists", |
| 693 |
|
| "apophis_fetch", "apophis_verify", "apophis_remove", |
| 694 |
|
| }; |
| 695 |
|
| |
| 696 |
|
| static const char *const ap_xop_desc[AP_NOPS] = { |
| 697 |
|
| "Apophis fused identify (SHA-1, UUID v5)", |
| 698 |
|
| "Apophis fused store (identify, mkdir, atomic write)", |
| 699 |
|
| "Apophis fused exists (path, stat)", |
| 700 |
|
| "Apophis fused fetch (path, stat, read)", |
| 701 |
|
| "Apophis fused verify (read, re-hash, compare)", |
| 702 |
|
| "Apophis fused remove (path, unlink, sidecar)", |
| 703 |
|
| }; |
| 704 |
|
| |
| 705 |
|
| /* The C function behind each method's XSUB, taken at boot. A CV on the |
| 706 |
|
| * stack is ours when it runs one of these, which holds in every thread |
| 707 |
|
| * and survives nothing but a redefinition. */ |
| 708 |
|
| static XSUBADDR_t ap_xsub[AP_NOPS]; |
| 709 |
|
| static XOP ap_xop[AP_NOPS]; |
| 710 |
|
| |
| 711 |
|
| static bool |
| 712 |
13589 |
| ap_ops_in_scope(pTHX) |
| 713 |
|
| { |
| 714 |
13589 |
| SV **ent = hv_fetchs(GvHV(PL_hintgv), AP_HINT_KEY, 0); |
| 715 |
13589 |
▶ | return ent && SvTRUE(*ent); |
Branch: 2 branches
| Branch | True | False | Total |
| return ent && SvTRUE(*ent); |
548 |
13041 |
13589 |
| return ent && SvTRUE(*ent); |
547 |
1 |
548 |
|
| 716 |
|
| } |
| 717 |
|
| |
| 718 |
|
| /* entersub's own check has run. A method call to one of the names, with |
| 719 |
|
| * no more kids than the invocant and the one argument, becomes the op. */ |
| 720 |
|
| static Perl_check_t ap_next_ck_entersub; |
| 721 |
|
| |
| 722 |
|
| static OP * |
| 723 |
21779 |
| ap_ck_entersub(pTHX_ OP *o) |
| 724 |
|
| { |
| 725 |
21779 |
| OP *kid, *last = NULL; |
| 726 |
|
| SV *meth; |
| 727 |
|
| const char *name; |
| 728 |
|
| STRLEN len; |
| 729 |
21779 |
| int nkids = 0; |
| 730 |
|
| size_t i; |
| 731 |
|
| |
| 732 |
21779 |
| o = ap_next_ck_entersub(aTHX_ o); |
| 733 |
21779 |
▶ | if (o->op_type != OP_ENTERSUB |
Branch: 1 branch
| Branch | True | False | Total |
| if (o->op_type != OP_ENTERSUB |
21519 |
260 |
21779 |
|
| 734 |
21519 |
▶ | || (o->op_private & (OPpENTERSUB_AMPER | OPpENTERSUB_DB)) |
Branch: 1 branch
| Branch | True | False | Total |
| || (o->op_private & (OPpENTERSUB_AMPER | OPpENTERSUB_DB)) |
13589 |
7930 |
21519 |
|
| 735 |
13589 |
▶ | || !ap_ops_in_scope(aTHX)) |
Branch: 1 branch
| Branch | True | False | Total |
| || !ap_ops_in_scope(aTHX)) |
13042 |
547 |
13589 |
|
| 736 |
21232 |
| return o; |
| 737 |
|
| |
| 738 |
|
| /* The kids, or the nulled list that holds them, as ck_subr reads them. */ |
| 739 |
547 |
| kid = cUNOPo->op_first; |
| 740 |
547 |
▶ | if (!OpHAS_SIBLING(kid)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!OpHAS_SIBLING(kid)) |
329 |
218 |
547 |
|
| 741 |
329 |
| kid = cUNOPx(kid)->op_first; |
| 742 |
2930 |
▶ | for (; kid; kid = OpSIBLING(kid)) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; kid; kid = OpSIBLING(kid)) { |
2383 |
547 |
2930 |
|
| 743 |
2383 |
| last = kid; |
| 744 |
2383 |
▶ | nkids++; |
Branch: 1 branch
| Branch | True | False | Total |
| nkids++; |
1836 |
547 |
2383 |
|
| 745 |
|
| } |
| 746 |
|
| /* pushmark, the invocant, the argument, the method */ |
| 747 |
547 |
▶ | if (last->op_type != OP_METHOD_NAMED || nkids > 4) |
Branch: 2 branches
| Branch | True | False | Total |
| if (last->op_type != OP_METHOD_NAMED || nkids > 4) |
212 |
335 |
547 |
| if (last->op_type != OP_METHOD_NAMED || nkids > 4) |
64 |
148 |
212 |
|
| 748 |
399 |
| return o; |
| 749 |
|
| |
| 750 |
|
| #if PERL_VERSION_GE(5,22,0) |
| 751 |
148 |
| meth = cMETHOPx_meth(last); |
| 752 |
|
| #else |
| 753 |
|
| meth = cSVOPx_sv(last); |
| 754 |
|
| #endif |
| 755 |
148 |
| name = SvPV(meth, len); |
| 756 |
1127 |
▶ | for (i = 0; i < sizeof(ap_names) / sizeof(*ap_names); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < sizeof(ap_names) / sizeof(*ap_names); i++) { |
1076 |
51 |
1127 |
|
| 757 |
1076 |
▶ | if (len == ap_names[i].len && memEQ(name, ap_names[i].name, len)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len == ap_names[i].len && memEQ(name, ap_names[i].name, len)) { |
208 |
868 |
1076 |
| if (len == ap_names[i].len && memEQ(name, ap_names[i].name, len)) { |
97 |
111 |
208 |
|
| 758 |
97 |
| o->op_type = OP_CUSTOM; |
| 759 |
97 |
| o->op_ppaddr = ap_pp[ap_names[i].which]; |
| 760 |
|
| /* A perl that gives entersub a pad slot of its own, as 5.40 |
| 761 |
|
| * and 5.42 do, never reaches the allocation. One that does |
| 762 |
|
| * not, every perl below 5.16 among them, would otherwise |
| 763 |
|
| * leave the op writing to pad slot 0. Perl_pad_alloc is |
| 764 |
|
| * the name every perl exports, pad_alloc the API only |
| 765 |
|
| * since 5.16. |
| 766 |
|
| # uncoverable branch true |
| 767 |
|
| # uncoverable condition left count:1 */ |
| 768 |
97 |
▶ | if (!o->op_targ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!o->op_targ) |
0 |
97 |
97 |
|
| 769 |
0 |
| o->op_targ = Perl_pad_alloc(aTHX_ OP_ENTERSUB, SVs_PADTMP); /* # uncoverable statement */ |
| 770 |
97 |
| break; |
| 771 |
|
| } |
| 772 |
|
| } |
| 773 |
148 |
| return o; |
| 774 |
|
| } |
| 775 |
|
| |
| 776 |
|
| /* Above the mark sit the invocant, the argument and the CV the method |
| 777 |
|
| * resolved to. The op takes the call when that CV is the XSUB it stands |
| 778 |
|
| * for, and then only. */ |
| 779 |
|
| static bool |
| 780 |
117 |
| ap_op_mine(pTHX_ SV **mark, SV **sp, int which) |
| 781 |
|
| { |
| 782 |
117 |
| SV *cv = *sp; |
| 783 |
117 |
| return sp - mark == 3 |
| 784 |
115 |
▶ | && SvTYPE(cv) == SVt_PVCV |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(cv) == SVt_PVCV |
115 |
0 |
115 |
|
| 785 |
115 |
▶ | && CvISXSUB((CV *)cv) |
Branch: 1 branch
| Branch | True | False | Total |
| && CvISXSUB((CV *)cv) |
113 |
2 |
115 |
|
| 786 |
232 |
▶ | && CvXSUB((CV *)cv) == ap_xsub[which]; |
Branch: 2 branches
| Branch | True | False | Total |
| && CvXSUB((CV *)cv) == ap_xsub[which]; |
115 |
2 |
117 |
| && CvXSUB((CV *)cv) == ap_xsub[which]; |
113 |
0 |
113 |
|
| 787 |
|
| } |
| 788 |
|
| |
| 789 |
|
| static const char * |
| 790 |
113 |
| ap_op_who(SV *cv, int which) |
| 791 |
|
| { |
| 792 |
113 |
| return ap_who[which][CvXSUBANY((CV *)cv).any_i32 ? 1 : 0]; |
| 793 |
|
| } |
| 794 |
|
| |
| 795 |
|
| /* The call site's pad slot, which the check above saw to. */ |
| 796 |
|
| static SV * |
| 797 |
48 |
| ap_op_targ(pTHX) |
| 798 |
|
| { |
| 799 |
48 |
| return PAD_SV(PL_op->op_targ); |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
|
| /* The call is over. Its three values go, and the result takes their |
| 803 |
|
| * place unless nothing wanted one. */ |
| 804 |
|
| static OP * |
| 805 |
97 |
| ap_op_leave(pTHX_ I32 markix, SV *result) |
| 806 |
|
| { |
| 807 |
97 |
| dSP; |
| 808 |
97 |
| const I32 gimme = GIMME_V; |
| 809 |
|
| |
| 810 |
97 |
| SP = PL_stack_base + markix; |
| 811 |
97 |
▶ | if (gimme != G_VOID) |
Branch: 1 branch
| Branch | True | False | Total |
| if (gimme != G_VOID) |
95 |
2 |
97 |
|
| 812 |
95 |
| *++SP = result; |
| 813 |
97 |
| PUTBACK; |
| 814 |
97 |
| return NORMAL; |
| 815 |
|
| } |
| 816 |
|
| |
| 817 |
|
| /* The mark is kept as an index: a body may read a tied or overloaded |
| 818 |
|
| * argument, and that can move the stack. */ |
| 819 |
|
| #define AP_OP_PROLOGUE(which) \ |
| 820 |
|
| dSP; \ |
| 821 |
|
| const I32 markix = TOPMARK; \ |
| 822 |
|
| SV **mark = PL_stack_base + markix; \ |
| 823 |
|
| const char *who; \ |
| 824 |
|
| if (!ap_op_mine(aTHX_ mark, SP, (which))) \ |
| 825 |
|
| return PL_ppaddr[OP_ENTERSUB](aTHX); \ |
| 826 |
|
| who = ap_op_who(*SP, (which)); \ |
| 827 |
|
| (void)POPMARK |
| 828 |
|
| |
| 829 |
|
| static OP * |
| 830 |
33 |
| pp_apophis_identify(pTHX) |
| 831 |
|
| { |
| 832 |
|
| unsigned char uuid[16]; |
| 833 |
|
| char buf[HORUS_FMT_STR_LEN + 1]; |
| 834 |
|
| SV *result; |
| 835 |
33 |
▶ | AP_OP_PROLOGUE(AP_IDENTIFY); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_IDENTIFY); |
0 |
33 |
33 |
|
| 836 |
|
| |
| 837 |
33 |
| apophis_do_identify(aTHX_ who, mark[1], mark[2], uuid); |
| 838 |
28 |
| horus_format_uuid(buf, uuid, HORUS_FMT_STR); |
| 839 |
28 |
| result = ap_op_targ(aTHX); |
| 840 |
28 |
| sv_setpvn(result, buf, HORUS_FMT_STR_LEN); |
| 841 |
28 |
| return ap_op_leave(aTHX_ markix, result); |
| 842 |
|
| } |
| 843 |
|
| |
| 844 |
|
| static OP * |
| 845 |
25 |
| pp_apophis_store(pTHX) |
| 846 |
|
| { |
| 847 |
|
| char id_str[HORUS_FMT_STR_LEN + 1]; |
| 848 |
|
| SV *result; |
| 849 |
25 |
▶ | AP_OP_PROLOGUE(AP_STORE); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_STORE); |
1 |
24 |
25 |
|
| 850 |
|
| |
| 851 |
24 |
| apophis_do_store(aTHX_ who, mark[1], mark[2], NULL, 2, id_str); |
| 852 |
20 |
| result = ap_op_targ(aTHX); |
| 853 |
20 |
| sv_setpvn(result, id_str, HORUS_FMT_STR_LEN); |
| 854 |
20 |
| return ap_op_leave(aTHX_ markix, result); |
| 855 |
|
| } |
| 856 |
|
| |
| 857 |
|
| static OP * |
| 858 |
22 |
| pp_apophis_exists(pTHX) |
| 859 |
|
| { |
| 860 |
22 |
▶ | AP_OP_PROLOGUE(AP_EXISTS); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_EXISTS); |
3 |
19 |
22 |
|
| 861 |
16 |
▶ | return ap_op_leave(aTHX_ markix, |
Branch: 1 branch
| Branch | True | False | Total |
| return ap_op_leave(aTHX_ markix, |
9 |
7 |
16 |
|
| 862 |
19 |
| apophis_do_exists(aTHX_ who, mark[1], mark[2], NULL, 2) ? &PL_sv_yes : &PL_sv_no); |
| 863 |
|
| } |
| 864 |
|
| |
| 865 |
|
| static OP * |
| 866 |
17 |
| pp_apophis_fetch(pTHX) |
| 867 |
|
| { |
| 868 |
17 |
▶ | AP_OP_PROLOGUE(AP_FETCH); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_FETCH); |
0 |
17 |
17 |
|
| 869 |
13 |
| return ap_op_leave(aTHX_ markix, |
| 870 |
17 |
| sv_2mortal(apophis_do_fetch(aTHX_ who, mark[1], mark[2], NULL, 2))); |
| 871 |
|
| } |
| 872 |
|
| |
| 873 |
|
| static OP * |
| 874 |
10 |
| pp_apophis_verify(pTHX) |
| 875 |
|
| { |
| 876 |
10 |
▶ | AP_OP_PROLOGUE(AP_VERIFY); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_VERIFY); |
0 |
10 |
10 |
|
| 877 |
10 |
▶ | return ap_op_leave(aTHX_ markix, |
Branch: 1 branch
| Branch | True | False | Total |
| return ap_op_leave(aTHX_ markix, |
4 |
6 |
10 |
|
| 878 |
10 |
| apophis_do_verify(aTHX_ who, mark[1], mark[2], NULL, 2) ? &PL_sv_yes : &PL_sv_no); |
| 879 |
|
| } |
| 880 |
|
| |
| 881 |
|
| static OP * |
| 882 |
10 |
| pp_apophis_remove(pTHX) |
| 883 |
|
| { |
| 884 |
10 |
▶ | AP_OP_PROLOGUE(AP_REMOVE); |
Branch: 1 branch
| Branch | True | False | Total |
| AP_OP_PROLOGUE(AP_REMOVE); |
0 |
10 |
10 |
|
| 885 |
10 |
▶ | return ap_op_leave(aTHX_ markix, |
Branch: 1 branch
| Branch | True | False | Total |
| return ap_op_leave(aTHX_ markix, |
7 |
3 |
10 |
|
| 886 |
10 |
| apophis_do_remove(aTHX_ who, mark[1], mark[2], NULL, 2) ? &PL_sv_yes : &PL_sv_no); |
| 887 |
|
| } |
| 888 |
|
| |
| 889 |
|
| /* Once for the process: the XSUBs behind the names, the ops' names for |
| 890 |
|
| * B and the debugger, and the hook on entersub's check. */ |
| 891 |
|
| static void |
| 892 |
15 |
| ap_boot_ops(pTHX) |
| 893 |
|
| { |
| 894 |
|
| int i; |
| 895 |
|
| |
| 896 |
105 |
▶ | for (i = 0; i < AP_NOPS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < AP_NOPS; i++) { |
90 |
15 |
105 |
|
| 897 |
90 |
| CV *cv = get_cv(form("Apophis::%s", ap_method[i]), 0); |
| 898 |
90 |
| ap_xsub[i] = CvXSUB(cv); |
| 899 |
90 |
| XopENTRY_set(&ap_xop[i], xop_name, ap_xop_name[i]); |
| 900 |
90 |
| XopENTRY_set(&ap_xop[i], xop_desc, ap_xop_desc[i]); |
| 901 |
90 |
| XopENTRY_set(&ap_xop[i], xop_class, OA_UNOP); |
| 902 |
90 |
| Perl_custom_op_register(aTHX_ ap_pp[i], &ap_xop[i]); |
| 903 |
|
| } |
| 904 |
|
| /* A perl before 5.14 has no XOP, so B cannot tell the shape of a |
| 905 |
|
| * custom op and its walkers, B::Deparse's among them, die at the |
| 906 |
|
| * kids. There the calls stay entersubs. */ |
| 907 |
|
| #if PERL_VERSION_GE(5,16,0) |
| 908 |
15 |
| wrap_op_checker(OP_ENTERSUB, ap_ck_entersub, &ap_next_ck_entersub); |
| 909 |
|
| #elif PERL_VERSION_GE(5,14,0) |
| 910 |
|
| if (!ap_next_ck_entersub) { |
| 911 |
|
| ap_next_ck_entersub = PL_check[OP_ENTERSUB]; |
| 912 |
|
| PL_check[OP_ENTERSUB] = ap_ck_entersub; |
| 913 |
|
| } |
| 914 |
|
| #else |
| 915 |
|
| PERL_UNUSED_VAR(ap_next_ck_entersub); |
| 916 |
|
| #endif |
| 917 |
15 |
| } |
| 918 |
|
| |
| 919 |
|
| |
| 920 |
|
| /* ================================================================== */ |
| 921 |
|
| /* The public C ABI */ |
| 922 |
|
| /* ================================================================== */ |
| 923 |
|
| |
| 924 |
|
| /* Wraps the static core above into the table consumers reach through |
| 925 |
|
| * Apophis::_abi_ptr. Included here, after everything it wraps. */ |
| 926 |
|
| #include "ap_abi_impl.h" |
| 927 |
|
| |
| 928 |
|
| |
| 929 |
|
| /* ================================================================== */ |
| 930 |
|
| /* XSUBs */ |
| 931 |
|
| /* ================================================================== */ |
| 932 |
|
| |
| 933 |
|
| MODULE = Apophis PACKAGE = Apophis |
| 934 |
|
| |
| 935 |
|
| BOOT: |
| 936 |
15 |
| ap_boot_ops(aTHX); |
| 937 |
|
| |
| 938 |
|
| # ------------------------------------------------------------------ # |
| 939 |
|
| # new(class, %args) -> blessed object # |
| 940 |
|
| # ------------------------------------------------------------------ # |
| 941 |
|
| |
| 942 |
|
| SV * |
| 943 |
|
| new(class, ...) |
| 944 |
|
| const char *class |
| 945 |
|
| PREINIT: |
| 946 |
|
| HV *self; |
| 947 |
|
| SV *self_ref; |
| 948 |
|
| int i; |
| 949 |
35 |
| const char *namespace_str = NULL; |
| 950 |
35 |
| STRLEN namespace_len = 0; |
| 951 |
35 |
| const char *store_dir = NULL; |
| 952 |
35 |
| STRLEN store_dir_len = 0; |
| 953 |
|
| unsigned char ns_bytes[16]; |
| 954 |
|
| CODE: |
| 955 |
35 |
▶ | if ((items - 1) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 1) % 2 != 0) |
1 |
34 |
35 |
|
| 956 |
1 |
| croak("Apophis->new: odd number of arguments"); |
| 957 |
|
| |
| 958 |
|
| /* Parse args */ |
| 959 |
87 |
▶ | for (i = 1; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i += 2) { |
53 |
34 |
87 |
|
| 960 |
53 |
| const char *key = SvPV_nolen(ST(i)); |
| 961 |
53 |
▶ | if (strEQ(key, "namespace")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "namespace")) { |
32 |
21 |
53 |
|
| 962 |
64 |
| namespace_str = SvPV(ST(i+1), namespace_len); |
| 963 |
21 |
▶ | } else if (strEQ(key, "store_dir")) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strEQ(key, "store_dir")) { |
20 |
1 |
21 |
|
| 964 |
40 |
| store_dir = SvPV(ST(i+1), store_dir_len); |
| 965 |
|
| } |
| 966 |
|
| } |
| 967 |
|
| |
| 968 |
34 |
▶ | if (!namespace_str) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!namespace_str) |
2 |
32 |
34 |
|
| 969 |
2 |
| croak("Apophis->new: 'namespace' is required"); |
| 970 |
|
| |
| 971 |
|
| /* Derive namespace UUID */ |
| 972 |
32 |
| apophis_derive_namespace(ns_bytes, namespace_str, namespace_len); |
| 973 |
|
| |
| 974 |
|
| /* Build object */ |
| 975 |
32 |
| self = newHV(); |
| 976 |
32 |
| hv_stores(self, "_ns_bytes", newSVpvn((const char *)ns_bytes, 16)); |
| 977 |
32 |
| hv_stores(self, "_ns_str", apophis_uuid_to_sv(aTHX_ ns_bytes)); |
| 978 |
|
| |
| 979 |
32 |
▶ | if (store_dir) |
Branch: 1 branch
| Branch | True | False | Total |
| if (store_dir) |
19 |
13 |
32 |
|
| 980 |
19 |
| hv_stores(self, "store_dir", newSVpvn(store_dir, store_dir_len)); |
| 981 |
|
| |
| 982 |
32 |
| self_ref = newRV_noinc((SV *)self); |
| 983 |
32 |
| sv_bless(self_ref, gv_stashpv(class, GV_ADD)); |
| 984 |
32 |
| RETVAL = self_ref; |
| 985 |
|
| OUTPUT: |
| 986 |
|
| RETVAL |
| 987 |
|
| |
| 988 |
|
| # ------------------------------------------------------------------ # |
| 989 |
|
| # namespace() -> UUID string # |
| 990 |
|
| # ------------------------------------------------------------------ # |
| 991 |
|
| |
| 992 |
|
| SV * |
| 993 |
|
| namespace(self) |
| 994 |
|
| SV *self |
| 995 |
|
| PREINIT: |
| 996 |
|
| HV *hv; |
| 997 |
|
| SV **svp; |
| 998 |
|
| CODE: |
| 999 |
9 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
8 |
9 |
|
| 1000 |
1 |
| croak("Apophis::namespace: not an object"); |
| 1001 |
8 |
| hv = (HV *)SvRV(self); |
| 1002 |
8 |
| svp = hv_fetchs(hv, "_ns_str", 0); |
| 1003 |
8 |
▶ | if (!svp || !SvOK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!svp || !SvOK(*svp)) |
7 |
1 |
8 |
| if (!svp || !SvOK(*svp)) |
1 |
6 |
7 |
|
| 1004 |
2 |
| croak("Apophis: object has no namespace"); |
| 1005 |
6 |
| RETVAL = newSVsv(*svp); |
| 1006 |
|
| OUTPUT: |
| 1007 |
|
| RETVAL |
| 1008 |
|
| |
| 1009 |
|
| # ------------------------------------------------------------------ # |
| 1010 |
|
| # identify(\$content) -> UUID string # |
| 1011 |
|
| # ------------------------------------------------------------------ # |
| 1012 |
|
| |
| 1013 |
|
| SV * |
| 1014 |
|
| identify(self, content_ref) |
| 1015 |
|
| SV *self |
| 1016 |
|
| SV *content_ref |
| 1017 |
|
| ALIAS: |
| 1018 |
|
| op_identify = 1 |
| 1019 |
|
| PREINIT: |
| 1020 |
|
| unsigned char uuid[16]; |
| 1021 |
|
| CODE: |
| 1022 |
6 |
| apophis_do_identify(aTHX_ ap_who[AP_IDENTIFY][ix], self, content_ref, uuid); |
| 1023 |
2 |
| RETVAL = apophis_uuid_to_sv(aTHX_ uuid); |
| 1024 |
|
| OUTPUT: |
| 1025 |
|
| RETVAL |
| 1026 |
|
| |
| 1027 |
|
| # ------------------------------------------------------------------ # |
| 1028 |
|
| # identify_file($path) -> UUID string # |
| 1029 |
|
| # ------------------------------------------------------------------ # |
| 1030 |
|
| |
| 1031 |
|
| SV * |
| 1032 |
|
| identify_file(self, path) |
| 1033 |
|
| SV *self |
| 1034 |
|
| const char *path |
| 1035 |
|
| PREINIT: |
| 1036 |
|
| HV *hv; |
| 1037 |
|
| const unsigned char *ns; |
| 1038 |
|
| unsigned char uuid[16]; |
| 1039 |
|
| PerlIO *fh; |
| 1040 |
|
| CODE: |
| 1041 |
6 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
5 |
6 |
|
| 1042 |
1 |
| croak("Apophis::identify_file: not an object"); |
| 1043 |
5 |
| hv = (HV *)SvRV(self); |
| 1044 |
5 |
| ns = apophis_get_ns(aTHX_ hv); |
| 1045 |
|
| |
| 1046 |
5 |
| fh = PerlIO_open(path, "rb"); |
| 1047 |
5 |
▶ | if (!fh) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) |
1 |
4 |
5 |
|
| 1048 |
1 |
| croak("Apophis::identify_file: cannot open '%s': %s", |
| 1049 |
|
| path, strerror(errno)); |
| 1050 |
|
| |
| 1051 |
4 |
| apophis_identify_stream(aTHX_ uuid, ns, fh); |
| 1052 |
4 |
| PerlIO_close(fh); |
| 1053 |
|
| |
| 1054 |
4 |
| RETVAL = apophis_uuid_to_sv(aTHX_ uuid); |
| 1055 |
|
| OUTPUT: |
| 1056 |
|
| RETVAL |
| 1057 |
|
| |
| 1058 |
|
| # ------------------------------------------------------------------ # |
| 1059 |
|
| # path_for($id, %opts) -> path string # |
| 1060 |
|
| # ------------------------------------------------------------------ # |
| 1061 |
|
| |
| 1062 |
|
| SV * |
| 1063 |
|
| path_for(self, id, ...) |
| 1064 |
|
| SV *self |
| 1065 |
|
| SV *id |
| 1066 |
|
| PREINIT: |
| 1067 |
|
| HV *hv; |
| 1068 |
12 |
| HV *opts = NULL; |
| 1069 |
|
| const char *store_dir; |
| 1070 |
|
| STRLEN store_dir_len; |
| 1071 |
|
| const char *id_str; |
| 1072 |
|
| STRLEN id_len; |
| 1073 |
|
| char path[APOPHIS_PATH_MAX]; |
| 1074 |
|
| int path_len; |
| 1075 |
|
| CODE: |
| 1076 |
12 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
11 |
12 |
|
| 1077 |
1 |
| croak("Apophis::path_for: not an object"); |
| 1078 |
11 |
| hv = (HV *)SvRV(self); |
| 1079 |
|
| |
| 1080 |
|
| /* Parse optional key-value pairs into opts HV */ |
| 1081 |
11 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
2 |
9 |
11 |
|
| 1082 |
|
| int i; |
| 1083 |
2 |
▶ | if ((items - 2) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2 != 0) |
1 |
1 |
2 |
|
| 1084 |
1 |
| croak("Apophis::path_for: odd number of optional arguments"); |
| 1085 |
1 |
| opts = newHV(); |
| 1086 |
1 |
| sv_2mortal((SV *)opts); |
| 1087 |
2 |
▶ | for (i = 2; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i < items; i += 2) { |
1 |
1 |
2 |
|
| 1088 |
|
| STRLEN klen; |
| 1089 |
1 |
| const char *k = SvPV(ST(i), klen); |
| 1090 |
1 |
| hv_store(opts, k, klen, SvREFCNT_inc(ST(i+1)), 0); |
| 1091 |
|
| } |
| 1092 |
|
| } |
| 1093 |
|
| |
| 1094 |
10 |
| store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 1095 |
10 |
| id_str = SvPV(id, id_len); |
| 1096 |
|
| |
| 1097 |
10 |
| path_len = apophis_path(path, sizeof(path), |
| 1098 |
|
| store_dir, store_dir_len, |
| 1099 |
|
| id_str, id_len); |
| 1100 |
9 |
| RETVAL = newSVpvn(path, path_len); |
| 1101 |
|
| OUTPUT: |
| 1102 |
|
| RETVAL |
| 1103 |
|
| |
| 1104 |
|
| # ------------------------------------------------------------------ # |
| 1105 |
|
| # store(\$content, %opts) -> UUID string # |
| 1106 |
|
| # ------------------------------------------------------------------ # |
| 1107 |
|
| |
| 1108 |
|
| SV * |
| 1109 |
|
| store(self, content_ref, ...) |
| 1110 |
|
| SV *self |
| 1111 |
|
| SV *content_ref |
| 1112 |
|
| ALIAS: |
| 1113 |
|
| op_store = 1 |
| 1114 |
|
| PREINIT: |
| 1115 |
|
| char id_str[HORUS_FMT_STR_LEN + 1]; |
| 1116 |
|
| CODE: |
| 1117 |
15 |
| apophis_do_store(aTHX_ ap_who[AP_STORE][ix], self, content_ref, &ST(0), items, id_str); |
| 1118 |
7 |
| RETVAL = newSVpvn(id_str, HORUS_FMT_STR_LEN); |
| 1119 |
|
| OUTPUT: |
| 1120 |
|
| RETVAL |
| 1121 |
|
| |
| 1122 |
|
| # ------------------------------------------------------------------ # |
| 1123 |
|
| # fetch($id, %opts) -> \$content or undef # |
| 1124 |
|
| # ------------------------------------------------------------------ # |
| 1125 |
|
| |
| 1126 |
|
| SV * |
| 1127 |
|
| fetch(self, id, ...) |
| 1128 |
|
| SV *self |
| 1129 |
|
| SV *id |
| 1130 |
|
| ALIAS: |
| 1131 |
|
| op_fetch = 1 |
| 1132 |
|
| CODE: |
| 1133 |
6 |
| RETVAL = apophis_do_fetch(aTHX_ ap_who[AP_FETCH][ix], self, id, &ST(0), items); |
| 1134 |
|
| OUTPUT: |
| 1135 |
|
| RETVAL |
| 1136 |
|
| |
| 1137 |
|
| # ------------------------------------------------------------------ # |
| 1138 |
|
| # exists($id, %opts) -> bool # |
| 1139 |
|
| # ------------------------------------------------------------------ # |
| 1140 |
|
| |
| 1141 |
|
| bool |
| 1142 |
|
| exists(self, id, ...) |
| 1143 |
|
| SV *self |
| 1144 |
|
| SV *id |
| 1145 |
|
| ALIAS: |
| 1146 |
|
| op_exists = 1 |
| 1147 |
|
| CODE: |
| 1148 |
9 |
| RETVAL = apophis_do_exists(aTHX_ ap_who[AP_EXISTS][ix], self, id, &ST(0), items); |
| 1149 |
|
| OUTPUT: |
| 1150 |
|
| RETVAL |
| 1151 |
|
| |
| 1152 |
|
| # ------------------------------------------------------------------ # |
| 1153 |
|
| # remove($id, %opts) -> bool # |
| 1154 |
|
| # ------------------------------------------------------------------ # |
| 1155 |
|
| |
| 1156 |
|
| bool |
| 1157 |
|
| remove(self, id, ...) |
| 1158 |
|
| SV *self |
| 1159 |
|
| SV *id |
| 1160 |
|
| ALIAS: |
| 1161 |
|
| op_remove = 1 |
| 1162 |
|
| CODE: |
| 1163 |
4 |
| RETVAL = apophis_do_remove(aTHX_ ap_who[AP_REMOVE][ix], self, id, &ST(0), items); |
| 1164 |
|
| OUTPUT: |
| 1165 |
|
| RETVAL |
| 1166 |
|
| |
| 1167 |
|
| # ------------------------------------------------------------------ # |
| 1168 |
|
| # verify($id, %opts) -> bool # |
| 1169 |
|
| # ------------------------------------------------------------------ # |
| 1170 |
|
| |
| 1171 |
|
| bool |
| 1172 |
|
| verify(self, id, ...) |
| 1173 |
|
| SV *self |
| 1174 |
|
| SV *id |
| 1175 |
|
| ALIAS: |
| 1176 |
|
| op_verify = 1 |
| 1177 |
|
| CODE: |
| 1178 |
5 |
| RETVAL = apophis_do_verify(aTHX_ ap_who[AP_VERIFY][ix], self, id, &ST(0), items); |
| 1179 |
|
| OUTPUT: |
| 1180 |
|
| RETVAL |
| 1181 |
|
| |
| 1182 |
|
| # ------------------------------------------------------------------ # |
| 1183 |
|
| # store_many(\@refs, %opts) -> @ids # |
| 1184 |
|
| # ------------------------------------------------------------------ # |
| 1185 |
|
| |
| 1186 |
|
| void |
| 1187 |
|
| store_many(self, refs, ...) |
| 1188 |
|
| SV *self |
| 1189 |
|
| SV *refs |
| 1190 |
|
| PREINIT: |
| 1191 |
|
| HV *hv; |
| 1192 |
10 |
| HV *opts = NULL; |
| 1193 |
|
| const unsigned char *ns; |
| 1194 |
|
| const char *store_dir; |
| 1195 |
|
| STRLEN store_dir_len; |
| 1196 |
|
| AV *av; |
| 1197 |
|
| I32 len, i; |
| 1198 |
|
| PPCODE: |
| 1199 |
10 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
9 |
10 |
|
| 1200 |
1 |
| croak("Apophis::store_many: not an object"); |
| 1201 |
9 |
| hv = (HV *)SvRV(self); |
| 1202 |
9 |
| ns = apophis_get_ns(aTHX_ hv); |
| 1203 |
|
| |
| 1204 |
9 |
▶ | if (!SvROK(refs) || SvTYPE(SvRV(refs)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(refs) || SvTYPE(SvRV(refs)) != SVt_PVAV) |
8 |
1 |
9 |
| if (!SvROK(refs) || SvTYPE(SvRV(refs)) != SVt_PVAV) |
2 |
6 |
8 |
|
| 1205 |
3 |
| croak("Apophis::store_many: first argument must be an array ref"); |
| 1206 |
6 |
| av = (AV *)SvRV(refs); |
| 1207 |
6 |
| len = av_len(av) + 1; |
| 1208 |
|
| |
| 1209 |
6 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
2 |
4 |
6 |
|
| 1210 |
|
| int j; |
| 1211 |
2 |
▶ | if ((items - 2) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2 != 0) |
1 |
1 |
2 |
|
| 1212 |
1 |
| croak("Apophis::store_many: odd number of optional arguments"); |
| 1213 |
1 |
| opts = newHV(); |
| 1214 |
1 |
| sv_2mortal((SV *)opts); |
| 1215 |
2 |
▶ | for (j = 2; j < items; j += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 2; j < items; j += 2) { |
1 |
1 |
2 |
|
| 1216 |
|
| STRLEN klen; |
| 1217 |
1 |
| const char *k = SvPV(ST(j), klen); |
| 1218 |
1 |
| hv_store(opts, k, klen, SvREFCNT_inc(ST(j+1)), 0); |
| 1219 |
|
| } |
| 1220 |
|
| } |
| 1221 |
|
| |
| 1222 |
5 |
| store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 1223 |
|
| |
| 1224 |
5 |
▶ | EXTEND(SP, len); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, len); |
5 |
0 |
5 |
| EXTEND(SP, len); |
0 |
5 |
5 |
|
| 1225 |
12 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
9 |
3 |
12 |
|
| 1226 |
9 |
| SV **svp = av_fetch(av, i, 0); |
| 1227 |
|
| SV *content_sv; |
| 1228 |
|
| const char *content; |
| 1229 |
|
| STRLEN content_len; |
| 1230 |
|
| unsigned char uuid[16]; |
| 1231 |
|
| char id_str[HORUS_FMT_STR_LEN + 1]; |
| 1232 |
|
| char path[APOPHIS_PATH_MAX]; |
| 1233 |
|
| apophis_stat_t st; |
| 1234 |
|
| |
| 1235 |
9 |
▶ | if (!svp || !SvROK(*svp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!svp || !SvROK(*svp)) |
8 |
1 |
9 |
| if (!svp || !SvROK(*svp)) |
1 |
7 |
8 |
|
| 1236 |
2 |
| croak("Apophis::store_many: element %d must be a scalar ref", |
| 1237 |
|
| (int)i); |
| 1238 |
|
| |
| 1239 |
7 |
| content_sv = SvRV(*svp); |
| 1240 |
7 |
| content = SvPV(content_sv, content_len); |
| 1241 |
|
| |
| 1242 |
7 |
| apophis_identify_content(uuid, ns, content, content_len); |
| 1243 |
7 |
| horus_format_uuid(id_str, uuid, HORUS_FMT_STR); |
| 1244 |
|
| |
| 1245 |
7 |
| apophis_path(path, sizeof(path), |
| 1246 |
|
| store_dir, store_dir_len, |
| 1247 |
|
| id_str, HORUS_FMT_STR_LEN); |
| 1248 |
|
| |
| 1249 |
7 |
▶ | if (apophis_stat(path, &st) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_stat(path, &st) != 0) { |
3 |
4 |
7 |
|
| 1250 |
3 |
| apophis_ensure_parent_dir(path); |
| 1251 |
3 |
| apophis_atomic_write(aTHX_ path, content, content_len); |
| 1252 |
|
| } |
| 1253 |
|
| |
| 1254 |
7 |
| PUSHs(sv_2mortal(newSVpvn(id_str, HORUS_FMT_STR_LEN))); |
| 1255 |
|
| } |
| 1256 |
|
| |
| 1257 |
|
| # ------------------------------------------------------------------ # |
| 1258 |
|
| # find_missing(\@ids, %opts) -> @missing_ids # |
| 1259 |
|
| # ------------------------------------------------------------------ # |
| 1260 |
|
| |
| 1261 |
|
| void |
| 1262 |
|
| find_missing(self, ids, ...) |
| 1263 |
|
| SV *self |
| 1264 |
|
| SV *ids |
| 1265 |
|
| PREINIT: |
| 1266 |
|
| HV *hv; |
| 1267 |
9 |
| HV *opts = NULL; |
| 1268 |
|
| const char *store_dir; |
| 1269 |
|
| STRLEN store_dir_len; |
| 1270 |
|
| AV *av; |
| 1271 |
|
| I32 len, i; |
| 1272 |
|
| PPCODE: |
| 1273 |
9 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
8 |
9 |
|
| 1274 |
1 |
| croak("Apophis::find_missing: not an object"); |
| 1275 |
8 |
| hv = (HV *)SvRV(self); |
| 1276 |
|
| |
| 1277 |
8 |
▶ | if (!SvROK(ids) || SvTYPE(SvRV(ids)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(ids) || SvTYPE(SvRV(ids)) != SVt_PVAV) |
7 |
1 |
8 |
| if (!SvROK(ids) || SvTYPE(SvRV(ids)) != SVt_PVAV) |
2 |
5 |
7 |
|
| 1278 |
3 |
| croak("Apophis::find_missing: first argument must be an array ref"); |
| 1279 |
5 |
| av = (AV *)SvRV(ids); |
| 1280 |
5 |
| len = av_len(av) + 1; |
| 1281 |
|
| |
| 1282 |
5 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
2 |
3 |
5 |
|
| 1283 |
|
| int j; |
| 1284 |
2 |
▶ | if ((items - 2) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2 != 0) |
1 |
1 |
2 |
|
| 1285 |
1 |
| croak("Apophis::find_missing: odd number of optional arguments"); |
| 1286 |
1 |
| opts = newHV(); |
| 1287 |
1 |
| sv_2mortal((SV *)opts); |
| 1288 |
2 |
▶ | for (j = 2; j < items; j += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 2; j < items; j += 2) { |
1 |
1 |
2 |
|
| 1289 |
|
| STRLEN klen; |
| 1290 |
1 |
| const char *k = SvPV(ST(j), klen); |
| 1291 |
1 |
| hv_store(opts, k, klen, SvREFCNT_inc(ST(j+1)), 0); |
| 1292 |
|
| } |
| 1293 |
|
| } |
| 1294 |
|
| |
| 1295 |
4 |
| store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 1296 |
|
| |
| 1297 |
13 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
9 |
4 |
13 |
|
| 1298 |
9 |
| SV **svp = av_fetch(av, i, 0); |
| 1299 |
|
| const char *id_str; |
| 1300 |
|
| STRLEN id_len; |
| 1301 |
|
| char path[APOPHIS_PATH_MAX]; |
| 1302 |
|
| apophis_stat_t st; |
| 1303 |
|
| |
| 1304 |
9 |
▶ | if (!svp || !SvOK(*svp)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!svp || !SvOK(*svp)) continue; |
8 |
1 |
9 |
| if (!svp || !SvOK(*svp)) continue; |
1 |
7 |
8 |
|
| 1305 |
|
| |
| 1306 |
7 |
| id_str = SvPV(*svp, id_len); |
| 1307 |
7 |
| apophis_path(path, sizeof(path), |
| 1308 |
|
| store_dir, store_dir_len, id_str, id_len); |
| 1309 |
|
| |
| 1310 |
7 |
▶ | if (apophis_stat(path, &st) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (apophis_stat(path, &st) != 0) { |
4 |
3 |
7 |
|
| 1311 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvn(id_str, id_len))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(id_str, id_len))); |
0 |
4 |
4 |
|
| 1312 |
|
| } |
| 1313 |
|
| } |
| 1314 |
|
| |
| 1315 |
|
| # ------------------------------------------------------------------ # |
| 1316 |
|
| # meta($id, %opts) -> \%meta or undef # |
| 1317 |
|
| # ------------------------------------------------------------------ # |
| 1318 |
|
| |
| 1319 |
|
| SV * |
| 1320 |
|
| meta(self, id, ...) |
| 1321 |
|
| SV *self |
| 1322 |
|
| SV *id |
| 1323 |
|
| PREINIT: |
| 1324 |
|
| HV *hv; |
| 1325 |
11 |
| HV *opts = NULL; |
| 1326 |
|
| const char *store_dir; |
| 1327 |
|
| STRLEN store_dir_len; |
| 1328 |
|
| const char *id_str; |
| 1329 |
|
| STRLEN id_len; |
| 1330 |
|
| char path[APOPHIS_PATH_MAX]; |
| 1331 |
|
| int path_len; |
| 1332 |
|
| char meta_path[APOPHIS_PATH_MAX]; |
| 1333 |
|
| HV *meta; |
| 1334 |
|
| CODE: |
| 1335 |
11 |
▶ | if (!sv_isobject(self)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self)) |
1 |
10 |
11 |
|
| 1336 |
1 |
| croak("Apophis::meta: not an object"); |
| 1337 |
10 |
| hv = (HV *)SvRV(self); |
| 1338 |
|
| |
| 1339 |
10 |
▶ | if (items > 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) { |
2 |
8 |
10 |
|
| 1340 |
|
| int i; |
| 1341 |
2 |
▶ | if ((items - 2) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2 != 0) |
1 |
1 |
2 |
|
| 1342 |
1 |
| croak("Apophis::meta: odd number of optional arguments"); |
| 1343 |
1 |
| opts = newHV(); |
| 1344 |
1 |
| sv_2mortal((SV *)opts); |
| 1345 |
2 |
▶ | for (i = 2; i < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i < items; i += 2) { |
1 |
1 |
2 |
|
| 1346 |
|
| STRLEN klen; |
| 1347 |
1 |
| const char *k = SvPV(ST(i), klen); |
| 1348 |
1 |
| hv_store(opts, k, klen, SvREFCNT_inc(ST(i+1)), 0); |
| 1349 |
|
| } |
| 1350 |
|
| } |
| 1351 |
|
| |
| 1352 |
9 |
| store_dir = apophis_get_store_dir(aTHX_ hv, opts, &store_dir_len); |
| 1353 |
9 |
| id_str = SvPV(id, id_len); |
| 1354 |
9 |
| path_len = apophis_path(path, sizeof(path), |
| 1355 |
|
| store_dir, store_dir_len, |
| 1356 |
|
| id_str, id_len); |
| 1357 |
9 |
| apophis_build_meta_path(meta_path, sizeof(meta_path), |
| 1358 |
|
| path, path_len); |
| 1359 |
|
| |
| 1360 |
9 |
| meta = apophis_meta_read(aTHX_ meta_path); |
| 1361 |
9 |
▶ | if (meta) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (meta) { |
2 |
7 |
9 |
|
| 1362 |
2 |
| RETVAL = newRV_noinc((SV *)meta); |
| 1363 |
|
| } else { |
| 1364 |
7 |
| RETVAL = &PL_sv_undef; |
| 1365 |
|
| } |
| 1366 |
|
| OUTPUT: |
| 1367 |
|
| RETVAL |
| 1368 |
|
| |
| 1369 |
|
| # ------------------------------------------------------------------ # |
| 1370 |
|
| # The public C ABI (ap_abi.h) # |
| 1371 |
|
| # ------------------------------------------------------------------ # |
| 1372 |
|
| |
| 1373 |
|
| # _abi_ptr() -> the address of the process-wide table, as a UV. |
| 1374 |
|
| # |
| 1375 |
|
| # Private. UV and PTR2UV rather than IV and PTR2IV because an address is |
| 1376 |
|
| # unsigned: PTR2IV hands back a NEGATIVE integer wherever the .so maps with |
| 1377 |
|
| # the top bit set - Solaris/illumos amd64 loads libraries high, and so does |
| 1378 |
|
| # any 32-bit build in the upper 2GB - which makes a consumer's `$ptr > 0` |
| 1379 |
|
| # sanity check fail on a perfectly usable pointer. |
| 1380 |
|
| |
| 1381 |
|
| UV |
| 1382 |
|
| _abi_ptr() |
| 1383 |
|
| CODE: |
| 1384 |
2 |
▶ | RETVAL = PTR2UV(&AP_ABI); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(&AP_ABI); |
0 |
2 |
2 |
|
| 1385 |
|
| OUTPUT: |
| 1386 |
|
| RETVAL |
| 1387 |
|
| |
| 1388 |
|
| # _abi_selftest($store, \$content, $name, $fh) |
| 1389 |
|
| # -> ($id, $path, $ns_of_name, $id_of_fh, $short_id_rc, $probe_rc, $probe_dirlen) |
| 1390 |
|
| # |
| 1391 |
|
| # Drives the table through its own function pointers, so the suite exercises |
| 1392 |
|
| # the ABI the way a consumer does rather than the way the XSUBs do. Writes the |
| 1393 |
|
| # blob, exactly as store() would. The test compares both answers against the |
| 1394 |
|
| # Perl API - two implementations of one algorithm agreeing is the assertion, |
| 1395 |
|
| # not an assumption. The rest of the table rides along: the namespace derived |
| 1396 |
|
| # from $name, the id of what $fh reads, build_path's refusal of a short id, and |
| 1397 |
|
| # store_of asked for part of the store, as a consumer probing with NULLs does. |
| 1398 |
|
| |
| 1399 |
|
| void |
| 1400 |
|
| _abi_selftest(self, content_ref, name, fh_sv) |
| 1401 |
|
| SV *self |
| 1402 |
|
| SV *content_ref |
| 1403 |
|
| SV *name |
| 1404 |
|
| SV *fh_sv |
| 1405 |
|
| PREINIT: |
| 1406 |
18 |
| const ap_abi *A = &AP_ABI; |
| 1407 |
18 |
| const unsigned char *ns = NULL; |
| 1408 |
18 |
| const unsigned char *ns_only = NULL; |
| 1409 |
18 |
| const char *dir = NULL; |
| 1410 |
18 |
| const char *dir_only = NULL; |
| 1411 |
18 |
| STRLEN dirlen = 0; |
| 1412 |
18 |
| STRLEN only_len = 0; |
| 1413 |
|
| const char *content; |
| 1414 |
|
| STRLEN content_len; |
| 1415 |
|
| const char *name_str; |
| 1416 |
|
| STRLEN name_len; |
| 1417 |
|
| unsigned char id[16]; |
| 1418 |
|
| unsigned char ns_bytes[16]; |
| 1419 |
|
| unsigned char fh_id[16]; |
| 1420 |
|
| char id_str[HORUS_FMT_STR_LEN + 1]; |
| 1421 |
|
| char ns_str[HORUS_FMT_STR_LEN + 1]; |
| 1422 |
|
| char fh_str[HORUS_FMT_STR_LEN + 1]; |
| 1423 |
|
| char path[APOPHIS_PATH_MAX]; |
| 1424 |
|
| int path_len; |
| 1425 |
|
| int short_rc; |
| 1426 |
|
| int probe_rc; |
| 1427 |
|
| int null_rc; |
| 1428 |
|
| PPCODE: |
| 1429 |
18 |
▶ | if (!A->store_of(aTHX_ self, &ns, &dir, &dirlen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!A->store_of(aTHX_ self, &ns, &dir, &dirlen)) |
12 |
6 |
18 |
|
| 1430 |
12 |
| croak("Apophis::_abi_selftest: not a store"); |
| 1431 |
6 |
▶ | if (!SvROK(content_ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(content_ref)) |
1 |
5 |
6 |
|
| 1432 |
1 |
| croak("Apophis::_abi_selftest: content must be a scalar reference"); |
| 1433 |
10 |
▶ | content = SvPV(SvRV(content_ref), content_len); |
Branch: 1 branch
| Branch | True | False | Total |
| content = SvPV(SvRV(content_ref), content_len); |
5 |
0 |
5 |
|
| 1434 |
5 |
| name_str = SvPV(name, name_len); |
| 1435 |
|
| |
| 1436 |
5 |
| probe_rc = A->store_of(aTHX_ self, &ns_only, NULL, NULL) |
| 1437 |
5 |
| + A->store_of(aTHX_ self, NULL, &dir_only, NULL) |
| 1438 |
5 |
| + A->store_of(aTHX_ self, NULL, NULL, &only_len); |
| 1439 |
5 |
| null_rc = A->store_of(aTHX_ NULL, &ns_only, &dir_only, &only_len); |
| 1440 |
|
| |
| 1441 |
5 |
| A->derive_ns(ns_bytes, name_str, name_len); |
| 1442 |
5 |
| A->format_id(ns_str, ns_bytes); |
| 1443 |
|
| |
| 1444 |
5 |
| A->identify(id, ns, content, content_len); |
| 1445 |
5 |
| A->format_id(id_str, id); |
| 1446 |
|
| |
| 1447 |
5 |
| short_rc = A->build_path(path, sizeof(path), dir, dirlen, id_str, 3); |
| 1448 |
5 |
| path_len = A->build_path(path, sizeof(path), dir, dirlen, |
| 1449 |
|
| id_str, HORUS_FMT_STR_LEN); |
| 1450 |
5 |
▶ | if (path_len < 0 || (size_t)path_len >= sizeof(path)) /* # uncoverable condition left count:1 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (path_len < 0 || (size_t)path_len >= sizeof(path)) /* # uncoverable condition left count:1 */ |
5 |
0 |
5 |
| if (path_len < 0 || (size_t)path_len >= sizeof(path)) /* # uncoverable condition left count:1 */ |
1 |
4 |
5 |
|
| 1451 |
1 |
| croak("Apophis::_abi_selftest: path too long"); |
| 1452 |
|
| |
| 1453 |
4 |
| A->write_atomic(aTHX_ path, content, content_len); |
| 1454 |
|
| |
| 1455 |
4 |
| A->identify_fh(aTHX_ fh_id, ns, IoIFP(sv_2io(fh_sv))); |
| 1456 |
4 |
| A->format_id(fh_str, fh_id); |
| 1457 |
|
| |
| 1458 |
4 |
▶ | EXTEND(SP, 8); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 8); |
0 |
4 |
4 |
|
| 1459 |
4 |
| PUSHs(sv_2mortal(newSVpvn(id_str, HORUS_FMT_STR_LEN))); |
| 1460 |
4 |
| PUSHs(sv_2mortal(newSVpvn(path, path_len))); |
| 1461 |
4 |
| PUSHs(sv_2mortal(newSVpvn(ns_str, HORUS_FMT_STR_LEN))); |
| 1462 |
4 |
| PUSHs(sv_2mortal(newSVpvn(fh_str, HORUS_FMT_STR_LEN))); |
| 1463 |
4 |
| PUSHs(sv_2mortal(newSViv(short_rc))); |
| 1464 |
4 |
| PUSHs(sv_2mortal(newSViv(probe_rc))); |
| 1465 |
4 |
| PUSHs(sv_2mortal(newSVuv(only_len))); |
| 1466 |
4 |
| PUSHs(sv_2mortal(newSViv(null_rc))); |
| 1467 |
|
| |
| 1468 |
|
| # _abi_write($path, \$content) |
| 1469 |
|
| # |
| 1470 |
|
| # The table's write_atomic on a path of the caller's choosing, as a consumer |
| 1471 |
|
| # that built its own path would call it. |
| 1472 |
|
| |
| 1473 |
|
| void |
| 1474 |
|
| _abi_write(path, content_ref) |
| 1475 |
|
| const char *path |
| 1476 |
|
| SV *content_ref |
| 1477 |
|
| PREINIT: |
| 1478 |
|
| const char *content; |
| 1479 |
|
| STRLEN content_len; |
| 1480 |
|
| PPCODE: |
| 1481 |
6 |
▶ | if (!SvROK(content_ref)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(content_ref)) |
1 |
5 |
6 |
|
| 1482 |
1 |
| croak("Apophis::_abi_write: content must be a scalar reference"); |
| 1483 |
5 |
| content = SvPV(SvRV(content_ref), content_len); |
| 1484 |
5 |
| AP_ABI.write_atomic(aTHX_ path, content, content_len); |
| 1485 |
3 |
| XSRETURN_EMPTY; |