spark-connector
aien-dev/spark-connector
Brought by drakestapleton
spark-connector / runner / 00base.om
| 1 | note runner/00base.om -- interplane-runner: state cells, texts and helpers (seeded Omega, OM-2). |
| 2 | note Composed after the om/tls units, seed/net/http.om and seed/net/https.om (see build.sh). |
| 3 | note Every name here starts with r: http.om reserves h*, H*; om/tls reserves its module prefixes. |
| 4 | note A global is never passed as an argument (omc error 19), so helpers that fill a global copy |
| 5 | note from a local array in a loop. |
| 6 | |
| 7 | note rgv: scalar cells, indexed by the constants below |
| 8 | global rgv ulong 64 |
| 9 | note rph: the last path built by r_path / r_jpath; rhm: the state directory |
| 10 | global rph uchar 4096 |
| 11 | global rhm uchar 4096 |
| 12 | note rhub: the hub URL as given; rhost: its host name |
| 13 | global rhub uchar 512 |
| 14 | global rhost uchar 256 |
| 15 | note rmsg: the message being built; rrsp: the last response body. rrsp holds the largest salvage |
| 16 | note record the hub can serve (the record format caps each field: 16 KiB source, 4 KiB stdin, |
| 17 | note 4 KiB expected, and the text fields; under 32 KiB in all) with room to spare; a longer body is |
| 18 | note refused as -413, never cut. |
| 19 | global rmsg uchar 8192 |
| 20 | global rrsp uchar 65536 |
| 21 | note rsk: the private scalar; rpub: the public point 04 || X || Y; rpubh / rkid: their hex forms |
| 22 | global rsk uchar 32 |
| 23 | global rpub uchar 65 |
| 24 | global rpubh uchar 130 |
| 25 | global rkid uchar 64 |
| 26 | note rjd: the current job directory; rjf: the job's text fields at the offsets rf* below |
| 27 | global rjd uchar 4096 |
| 28 | global rjf uchar 512 |
| 29 | |
| 30 | const rhml ulong 0 |
| 31 | const rphl ulong 1 |
| 32 | const rhstl ulong 2 |
| 33 | const rport ulong 3 |
| 34 | const rtls ulong 4 |
| 35 | const rmsgl ulong 5 |
| 36 | const rrspl ulong 6 |
| 37 | const rrate ulong 7 |
| 38 | const rwhy ulong 8 |
| 39 | const rcode ulong 9 |
| 40 | const rhubl ulong 10 |
| 41 | const rpost ulong 11 |
| 42 | const rjdl ulong 12 |
| 43 | const rexl ulong 13 |
| 44 | const rsrco ulong 14 |
| 45 | const rsrcn ulong 15 |
| 46 | const rstio ulong 16 |
| 47 | const rstin ulong 17 |
| 48 | |
| 49 | note rjf offsets: job id 32, part 64, compiler 64, binary 64, stdout 64, exit text (rgv rexl bytes) |
| 50 | const rfjob ulong 0 |
| 51 | const rfpart ulong 32 |
| 52 | const rfcomp ulong 96 |
| 53 | const rfbin ulong 160 |
| 54 | const rfout ulong 224 |
| 55 | const rfexit ulong 288 |
| 56 | |
| 57 | note open flags (arm64): rdonly|nofollow|cloexec; wronly|creat|excl|nofollow|cloexec; |
| 58 | note wronly|creat|trunc|nofollow|cloexec; wronly|creat|append|nofollow|cloexec; |
| 59 | note rdonly|directory|nofollow|cloexec |
| 60 | const rford int 557056 |
| 61 | const rfonew int 557249 |
| 62 | const rfotr int 557633 |
| 63 | const rfoapp int 558145 |
| 64 | const rfodir int 573440 |
| 65 | |
| 66 | note the compiler every job must name and every check uses: omc of omega-mv f4df8239, the pin |
| 67 | note of the interplane-forge salvage collector (run.sh PIN) |
| 68 | text rtpin "3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194" |
| 69 | text rhex "0123456789abcdef" |
| 70 | text rtnl "\n" |
| 71 | text rtsp " " |
| 72 | text renvh "HOME=" |
| 73 | text renvr "INTERPLANE_RUNNER_HOME=" |
| 74 | text rdotd "/.interplane-runner" |
| 75 | |
| 76 | fn r_say t view uchar * to void |
| 77 | w = write 1 t 0 len t |
| 78 | end |
| 79 | |
| 80 | note r_dec: v in decimal into d at o; returns the offset after it |
| 81 | fn r_dec d edit uchar * o ulong v ulong to ulong |
| 82 | t = new uchar 20 0 |
| 83 | i = ulong 20 |
| 84 | x = v |
| 85 | for it ulong 20 |
| 86 | i = sub i 1 |
| 87 | put t i uchar add rem x 10 48 |
| 88 | x = div x 10 |
| 89 | if eq x 0 |
| 90 | break |
| 91 | end |
| 92 | end |
| 93 | k = o |
| 94 | for j ulong sub 20 i bound 20 |
| 95 | put d k get t add i j |
| 96 | k = add k 1 |
| 97 | end |
| 98 | ret k |
| 99 | end |
| 100 | |
| 101 | fn r_num v ulong to void |
| 102 | d = new uchar 20 0 |
| 103 | n = r_dec d 0 v |
| 104 | w = write 1 d 0 n |
| 105 | end |
| 106 | |
| 107 | note r_undec: a decimal of 1..19 digits without a leading zero (or "0"); 2^64-1 when malformed |
| 108 | fn r_undec s view uchar * to ulong |
| 109 | bad = ulong 18446744073709551615 |
| 110 | if or eq len s 0 gt len s 19 |
| 111 | ret bad |
| 112 | end |
| 113 | if and gt len s 1 eq get s 0 48 |
| 114 | ret bad |
| 115 | end |
| 116 | v = ulong 0 |
| 117 | for i ulong len s bound 19 |
| 118 | c = get s i |
| 119 | if or lt c 48 gt c 57 |
| 120 | ret bad |
| 121 | end |
| 122 | v = add mul v 10 sub ulong c 48 |
| 123 | end |
| 124 | ret v |
| 125 | end |
| 126 | |
| 127 | note r_hex: s as lowercase hex into d at o; returns the offset after it |
| 128 | fn r_hex s view uchar * d edit uchar * o ulong to ulong |
| 129 | for i ulong len s bound 8388608 |
| 130 | put d add o mul i 2 get rhex ulong shr get s i 4 |
| 131 | put d add add o mul i 2 1 get rhex ulong and get s i 15 |
| 132 | end |
| 133 | ret add o mul len s 2 |
| 134 | end |
| 135 | |
| 136 | fn r_hv c uchar to ulong |
| 137 | if and ge c 48 le c 57 |
| 138 | ret sub ulong c 48 |
| 139 | end |
| 140 | if and ge c 97 le c 102 |
| 141 | ret sub ulong c 87 |
| 142 | end |
| 143 | ret 16 |
| 144 | end |
| 145 | |
| 146 | note r_unhex: exactly 2 * len d lowercase hex characters into d |
| 147 | fn r_unhex s view uchar * d edit uchar * to bool |
| 148 | if ne len s mul len d 2 |
| 149 | ret false |
| 150 | end |
| 151 | for i ulong len d bound 4194304 |
| 152 | a = r_hv get s mul i 2 |
| 153 | b = r_hv get s add mul i 2 1 |
| 154 | if or gt a 15 gt b 15 |
| 155 | ret false |
| 156 | end |
| 157 | put d i uchar or shl a 4 b |
| 158 | end |
| 159 | ret true |
| 160 | end |
| 161 | |
| 162 | note r_ishex: s is lowercase hex only |
| 163 | fn r_ishex s view uchar * to bool |
| 164 | for i ulong len s bound 16777216 |
| 165 | if gt r_hv get s i 15 |
| 166 | ret false |
| 167 | end |
| 168 | end |
| 169 | ret true |
| 170 | end |
| 171 | |
| 172 | fn r_eq a view uchar * b view uchar * to bool |
| 173 | if ne len a len b |
| 174 | ret false |
| 175 | end |
| 176 | for i ulong len a bound 16777216 |
| 177 | if ne get a i get b i |
| 178 | ret false |
| 179 | end |
| 180 | end |
| 181 | ret true |
| 182 | end |
| 183 | |
| 184 | fn r_wipe b edit uchar * to void |
| 185 | for i ulong len b bound 16777216 |
| 186 | put b i 0 |
| 187 | end |
| 188 | end |
| 189 | |
| 190 | fn r_sha m view uchar * h edit uchar * to void |
| 191 | c = new ulong 40 0 |
| 192 | sha256_init c |
| 193 | sha256_update c m |
| 194 | sha256_final c h |
| 195 | end |
| 196 | |
| 197 | note r_envget: the value of the first environment entry that starts with pre, into d; -1 when absent |
| 198 | fn r_envget pre view uchar * d edit uchar * to long |
| 199 | b = new uchar 4096 0 |
| 200 | for i ulong ulong envcnt bound 1000000 |
| 201 | n = envread i b 0 4096 |
| 202 | if gt n long len pre |
| 203 | if r_envcp part b 0 ulong n pre d |
| 204 | ret sub n long len pre |
| 205 | end |
| 206 | end |
| 207 | end |
| 208 | ret -1 |
| 209 | end |
| 210 | |
| 211 | note r_envcp: b starts with pre and the rest fits d: copy the rest into d |
| 212 | fn r_envcp b view uchar * pre view uchar * d edit uchar * to bool |
| 213 | if not r_eq part b 0 len pre pre |
| 214 | ret false |
| 215 | end |
| 216 | m = sub len b len pre |
| 217 | if gt m len d |
| 218 | ret false |
| 219 | end |
| 220 | for j ulong m bound 4096 |
| 221 | put d j get b add len pre j |
| 222 | end |
| 223 | ret true |
| 224 | end |
| 225 | |
| 226 | note r_home: the state directory, $INTERPLANE_RUNNER_HOME when set, else $HOME/.interplane-runner; |
| 227 | note it must be absolute and free of control bytes |
| 228 | fn r_home to bool |
| 229 | b = new uchar 4096 0 |
| 230 | n = r_envget renvr b |
| 231 | if lt n 0 |
| 232 | n = r_envget renvh b |
| 233 | if lt n 0 |
| 234 | ret false |
| 235 | end |
| 236 | if gt add ulong n len rdotd 4000 |
| 237 | ret false |
| 238 | end |
| 239 | for i ulong len rdotd |
| 240 | put b add ulong n i get rdotd i |
| 241 | end |
| 242 | n = add n long len rdotd |
| 243 | end |
| 244 | if or lt n 2 gt n 4000 |
| 245 | ret false |
| 246 | end |
| 247 | if ne get b 0 47 |
| 248 | ret false |
| 249 | end |
| 250 | note the folder's own name must be .interplane-runner (also for INTERPLANE_RUNNER_HOME, which tests |
| 251 | note use): everything the runner removes is inside a folder of that name |
| 252 | if lt ulong n len rdotd |
| 253 | ret false |
| 254 | end |
| 255 | if not r_eq part b sub ulong n len rdotd len rdotd rdotd |
| 256 | ret false |
| 257 | end |
| 258 | for i ulong ulong n bound 4096 |
| 259 | c = get b i |
| 260 | if or lt c 32 eq c 127 |
| 261 | ret false |
| 262 | end |
| 263 | put rhm i c |
| 264 | end |
| 265 | put rgv rhml ulong n |
| 266 | ret true |
| 267 | end |
| 268 | |
| 269 | note r_path: rph = state directory + "/" + nm (nm empty: the directory itself); returns the length |
| 270 | fn r_path nm view uchar * to ulong |
| 271 | n = get rgv rhml |
| 272 | for i ulong n bound 4096 |
| 273 | put rph i get rhm i |
| 274 | end |
| 275 | if gt len nm 0 |
| 276 | put rph n 47 |
| 277 | for i ulong len nm bound 4000 |
| 278 | put rph add add n 1 i get nm i |
| 279 | end |
| 280 | n = add add n 1 len nm |
| 281 | end |
| 282 | put rgv rphl n |
| 283 | ret n |
| 284 | end |
| 285 | |
| 286 | note r_jpath: rph = job directory + "/" + nm |
| 287 | fn r_jpath nm view uchar * to ulong |
| 288 | n = get rgv rjdl |
| 289 | for i ulong n bound 4096 |
| 290 | put rph i get rjd i |
| 291 | end |
| 292 | put rph n 47 |
| 293 | for i ulong len nm bound 4000 |
| 294 | put rph add add n 1 i get nm i |
| 295 | end |
| 296 | n = add add n 1 len nm |
| 297 | put rgv rphl n |
| 298 | ret n |
| 299 | end |
| 300 | |
| 301 | note r_phcp: a copy of rph into d; returns its length |
| 302 | fn r_phcp d edit uchar * to ulong |
| 303 | n = get rgv rphl |
| 304 | for i ulong n bound 4096 |
| 305 | put d i get rph i |
| 306 | end |
| 307 | ret n |
| 308 | end |
| 309 | |
| 310 | note r_rd: the whole file at rph into b; its length, -27 when it is longer than b, or -errno |
| 311 | fn r_rd b edit uchar * to long |
| 312 | one = new uchar 1 0 |
| 313 | fd = fopen rph 0 get rgv rphl rford 0 |
| 314 | if lt fd 0 |
| 315 | ret fd |
| 316 | end |
| 317 | n = ulong 0 |
| 318 | rc = long 0 |
| 319 | while true bound 9223372036854775807 |
| 320 | if eq n len b |
| 321 | r = read int fd one 0 1 |
| 322 | if ne r 0 |
| 323 | rc = -27 |
| 324 | end |
| 325 | break |
| 326 | end |
| 327 | r = read int fd b n sub len b n |
| 328 | if lt r 0 |
| 329 | rc = r |
| 330 | break |
| 331 | end |
| 332 | if eq r 0 |
| 333 | break |
| 334 | end |
| 335 | n = add n ulong r |
| 336 | end |
| 337 | x = fclose int fd |
| 338 | if lt rc 0 |
| 339 | ret rc |
| 340 | end |
| 341 | ret long n |
| 342 | end |
| 343 | |
| 344 | note r_wr: write d to the file at rph opened with flags fl and mode md, then fsync |
| 345 | fn r_wr d view uchar * fl int md int to bool |
| 346 | fd = fopen rph 0 get rgv rphl fl md |
| 347 | if lt fd 0 |
| 348 | ret false |
| 349 | end |
| 350 | n = ulong 0 |
| 351 | ok = true |
| 352 | while lt n len d bound 9223372036854775807 |
| 353 | r = write int fd d n sub len d n |
| 354 | if le r 0 |
| 355 | ok = false |
| 356 | break |
| 357 | end |
| 358 | n = add n ulong r |
| 359 | end |
| 360 | s = fsync int fd |
| 361 | x = fclose int fd |
| 362 | ret and ok eq s 0 |
| 363 | end |
| 364 | |
| 365 | note r_pk: one packed-list record (u16 length, bytes) at o; returns the offset after it |
| 366 | fn r_pk p edit uchar * o ulong s view uchar * to ulong |
| 367 | put p o uchar and len s 255 |
| 368 | put p add o 1 uchar shr len s 8 |
| 369 | for i ulong len s bound 65536 |
| 370 | put p add add o 2 i get s i |
| 371 | end |
| 372 | ret add add o 2 len s |
| 373 | end |
| 374 | |
| 375 | text rtnew ".new" |
| 376 | note r_replace: write d to "<rph>.new" (0600), then rename it over rph |
| 377 | fn r_replace d view uchar * to bool |
| 378 | a = new uchar 4100 0 |
| 379 | b = new uchar 4100 0 |
| 380 | p = new uchar 8300 0 |
| 381 | n = r_phcp a |
| 382 | for i ulong 4 |
| 383 | put b add n i get rtnew i |
| 384 | end |
| 385 | for i ulong n bound 4096 |
| 386 | put b i get a i |
| 387 | end |
| 388 | for i ulong add n 4 bound 4100 |
| 389 | put rph i get b i |
| 390 | end |
| 391 | put rgv rphl add n 4 |
| 392 | if not r_wr d rfotr 384 |
| 393 | ret false |
| 394 | end |
| 395 | put p 0 2 |
| 396 | put p 1 0 |
| 397 | k = r_pk p 2 part b 0 add n 4 |
| 398 | k = r_pk p k part a 0 n |
| 399 | r = renames p 0 k 0 |
| 400 | ret eq r 0 |
| 401 | end |
| 402 | |
| 403 | note r_join: q = p + "/" + nm; returns its length (0 when it would not fit) |
| 404 | fn r_join p view uchar * nm view uchar * q edit uchar * to ulong |
| 405 | if gt add add len p 1 len nm len q |
| 406 | ret 0 |
| 407 | end |
| 408 | for i ulong len p bound 4096 |
| 409 | put q i get p i |
| 410 | end |
| 411 | put q len p 47 |
| 412 | for i ulong len nm bound 4096 |
| 413 | put q add add len p 1 i get nm i |
| 414 | end |
| 415 | ret add add len p 1 len nm |
| 416 | end |
| 417 | |
| 418 | note r_dotdot: nm is "." or ".." |
| 419 | fn r_dotdot nm view uchar * to bool |
| 420 | if eq len nm 1 |
| 421 | ret eq get nm 0 46 |
| 422 | end |
| 423 | if eq len nm 2 |
| 424 | ret and eq get nm 0 46 eq get nm 1 46 |
| 425 | end |
| 426 | ret false |
| 427 | end |
| 428 | |
| 429 | note r_rmtree: remove p and everything below it, never following a symbolic link (a link is removed |
| 430 | note as an entry). True when p is gone afterwards. |
| 431 | fn r_rmtree p view uchar * to bool |
| 432 | b = new uchar 8192 0 |
| 433 | q = new uchar 4096 0 |
| 434 | fd = fopen p 0 len p rfodir 0 |
| 435 | if lt fd 0 |
| 436 | r = unlink p 0 len p |
| 437 | ret or eq r 0 eq r -2 |
| 438 | end |
| 439 | while true bound 9223372036854775807 |
| 440 | n = getdnts int fd b 0 8192 |
| 441 | if le n 0 |
| 442 | break |
| 443 | end |
| 444 | o = ulong 0 |
| 445 | while lt o ulong n bound 8192 |
| 446 | o = add o r_rment p part b o sub ulong n o q |
| 447 | end |
| 448 | end |
| 449 | x = fclose int fd |
| 450 | r = rmdir p 0 len p |
| 451 | ret or eq r 0 eq r -2 |
| 452 | end |
| 453 | |
| 454 | note r_rment: one getdents64 record at the start of d below the directory p; returns the record |
| 455 | note length (a removal that fails leaves the directory non-empty, so the caller's rmdir fails) |
| 456 | fn r_rment p view uchar * d view uchar * q edit uchar * to ulong |
| 457 | rl = or ulong get d 16 shl ulong get d 17 8 |
| 458 | ty = get d 18 |
| 459 | nl = ulong 0 |
| 460 | while lt add 19 nl rl bound 65536 |
| 461 | if eq get d add 19 nl 0 |
| 462 | break |
| 463 | end |
| 464 | nl = add nl 1 |
| 465 | end |
| 466 | if r_dotdot part d 19 nl |
| 467 | ret rl |
| 468 | end |
| 469 | k = r_join p part d 19 nl q |
| 470 | if eq k 0 |
| 471 | ret rl |
| 472 | end |
| 473 | if or eq ty 4 eq ty 0 |
| 474 | x = r_rmtree part q 0 k |
| 475 | ret rl |
| 476 | end |
| 477 | r = unlink q 0 k |
| 478 | ret rl |
| 479 | end |