interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / server / util.om
| 1 | note forged server/util.om: the shared request / response globals and helpers of docs/CONTRACT.md. |
| 2 | note Every name here starts with f (functions, globals) or tf (texts). Composed after http.om. |
| 3 | note ---- request (set by server/main.om before dispatch) |
| 4 | global fmeth ulong 1 |
| 5 | global fpath uchar 4096 |
| 6 | global fpathl ulong 1 |
| 7 | global fqs uchar 4096 |
| 8 | global fqsl ulong 1 |
| 9 | global fbody uchar 65536 |
| 10 | global fbodyl ulong 1 |
| 11 | note fbig: the body of a POST whose path ends in /patches/new (git patch intake, docs/API.md), at most |
| 12 | note 3 MiB (3145728 bytes: a 2 MiB mbox as base64url is 2796203 bytes, a 60000-byte description |
| 13 | note percent-encoded at most 180000, the rest under 20000). server/main.om freqbig fills it and leaves |
| 14 | note fbody empty; every other POST keeps the 64 KiB fbody and has fbigl 0. |
| 15 | global fbig uchar 3145728 |
| 16 | global fbigl ulong 1 |
| 17 | global fck uchar 4096 |
| 18 | global fckl ulong 1 |
| 19 | global fip uchar 64 |
| 20 | global fipl ulong 1 |
| 21 | global fbase uchar 64 |
| 22 | global fbasel ulong 1 |
| 23 | global fridx ulong 1 |
| 24 | global frid uchar 64 |
| 25 | global fridl ulong 1 |
| 26 | global fown uchar 64 |
| 27 | global fownl ulong 1 |
| 28 | global frepo uchar 128 |
| 29 | global frepol ulong 1 |
| 30 | global frest uchar 4096 |
| 31 | global frestl ulong 1 |
| 32 | global fuser uchar 64 |
| 33 | global fuserl ulong 1 |
| 34 | global fcsrf uchar 64 |
| 35 | global fcsrfl ulong 1 |
| 36 | global finm uchar 256 |
| 37 | global finml ulong 1 |
| 38 | global forig uchar 256 |
| 39 | global forigl ulong 1 |
| 40 | global fag uchar 256 |
| 41 | global fagl ulong 1 |
| 42 | global frf uchar 256 |
| 43 | global frfl ulong 1 |
| 44 | note ---- response |
| 45 | global fob uchar 8388608 |
| 46 | global fobl ulong 1 |
| 47 | global fobtr ulong 1 |
| 48 | global ftxmore ulong 1 |
| 49 | global fkind ulong 1 |
| 50 | global fct uchar 128 |
| 51 | global fctl ulong 1 |
| 52 | global fcd uchar 512 |
| 53 | global fcdl ulong 1 |
| 54 | global fsck uchar 512 |
| 55 | global fsckl ulong 1 |
| 56 | global floc uchar 4096 |
| 57 | global flocl ulong 1 |
| 58 | global fti uchar 256 |
| 59 | global ftil ulong 1 |
| 60 | global ffile uchar 4096 |
| 61 | global ffilel ulong 1 |
| 62 | global fetag uchar 128 |
| 63 | global fetagl ulong 1 |
| 64 | global fcc uchar 128 |
| 65 | global fccl ulong 1 |
| 66 | global fval uchar 4096 |
| 67 | note ---- allow-list (server/repos.om fills it). FAMAX rows: every table below indexed by the row has |
| 68 | note FAMAX entries, and every loop over the rows is bound FAMAX. The pools hold FAMAX rows at the |
| 69 | note longest each row may be (name 100 + slack, id 64, branch 60, description and last commit as |
| 70 | note before, times four). Sized 2026-11-14 from 64 to 256 for the native store (105 public projects |
| 71 | note in it); store/tests/capacity.sh fails loudly when a store or RIDS.md table passes 192 rows. |
| 72 | const FAMAX ulong 256 |
| 73 | global fan ulong 1 |
| 74 | global fanm uchar 32768 |
| 75 | global fano ulong 256 |
| 76 | global fanl ulong 256 |
| 77 | global faid uchar 16384 |
| 78 | global faio ulong 256 |
| 79 | global fail ulong 256 |
| 80 | global fabr uchar 16384 |
| 81 | global fabo ulong 256 |
| 82 | global fabl ulong 256 |
| 83 | global fadesc uchar 262144 |
| 84 | global fado ulong 256 |
| 85 | global fadl ulong 256 |
| 86 | global falc uchar 262144 |
| 87 | global falco ulong 256 |
| 88 | global falcl ulong 256 |
| 89 | global famt ulong 1 |
| 90 | note ---- exec |
| 91 | global fpk uchar 8192 |
| 92 | global fpkl ulong 1 |
| 93 | global fpkr ulong 1 |
| 94 | global fxo uchar 16777216 |
| 95 | global fxol ulong 1 |
| 96 | global fxotr ulong 1 |
| 97 | note streaming exec (fsopen / fsnext / fsclose): 0 read fd, 1 pid, 2 deadline, 3 state (0 open, |
| 98 | note 1 end of output, 2 failed); fsp the first unread byte of fxo, fsq the first byte not yet scanned |
| 99 | note for the separator, fsrs the start of the record fsnext returned last |
| 100 | global fsst long 4 |
| 101 | global fsp ulong 1 |
| 102 | global fsq ulong 1 |
| 103 | global fsrs ulong 1 |
| 104 | global fpfd uchar 8 |
| 105 | global fwst uchar 4 |
| 106 | global fjunk uchar 4096 |
| 107 | global fpth uchar 4096 |
| 108 | global fpthl ulong 1 |
| 109 | global fstb uchar 128 |
| 110 | note ---- write rate limit: 256 slots keyed by the 64-bit FNV-1a hash of the client IP, 30 writes per |
| 111 | note 60 s window; an unknown IP takes a free slot, else the slot whose window started first |
| 112 | global frlk ulong 256 |
| 113 | global frlt ulong 256 |
| 114 | global frlc ulong 256 |
| 115 | note ---- per-response hooks (cleared per request by fresetr in main.om) |
| 116 | note fretry: nonzero = send "Retry-After: <fretry>" (seconds) with the response (dl 503) |
| 117 | global fretry ulong 1 |
| 118 | note fcspsb: nonzero = send the sandbox CSP instead of the page CSP (browse /raw/), see fsetsbx |
| 119 | global fcspsb ulong 1 |
| 120 | note fmvng: 1 = this 503 is the "moving" answer of a part that is moving to the new Interplane store |
| 121 | note (store/moving.om, store build only): ferrti, ferrmsg and fyerr then give its title, its text and |
| 122 | note "reason":"moving"; every other 503 (door fail-closed, dl Retry-After) keeps its own words |
| 123 | global fmvng ulong 1 |
| 124 | note fsrcst: 1 when the repositories are a native Interplane store (store/repos.om sets it when it |
| 125 | note loads; the default build never does): fkgit / fkgiti then name <fsrt>projects/<name>/repo.git, |
| 126 | note fsrt being the store root with its final "/". 0: <tfstore><RID>, Radicle storage. |
| 127 | global fsrcst ulong 1 |
| 128 | global fsrt uchar 4096 |
| 129 | global fsrtl ulong 1 |
| 130 | note fpin: the commit a repository page is pinned to, 40 lower-case hex, or fpin[0] = 0 for none. |
| 131 | note browse (brpinhd) sets it around frepohd on the repo home and on /tree/ pages, so the Files tab |
| 132 | note and the clone panel (store/clone.om) name the commit the page shows, not a branch that may move |
| 133 | note between the page and the download. fputpin writes it. |
| 134 | global fpin uchar 40 |
| 135 | text tfsprj "projects/" |
| 136 | text tfsrg "/repo.git" |
| 137 | text tfstore "/home/drakestapleton/.radicle/storage/" |
| 138 | text tfgit "/usr/bin/git" |
| 139 | text tfdata "/home/drakestapleton/forge-data/" |
| 140 | text tfowner "drakestapleton" |
| 141 | text tfnid "z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK" |
| 142 | text tfdevnul "/dev/null" |
| 143 | text tfgitdir "--git-dir=" |
| 144 | text tfamp "&" |
| 145 | text tflt "<" |
| 146 | text tfgt ">" |
| 147 | text tfquot """ |
| 148 | text tfapos "'" |
| 149 | note ---- body builder fob |
| 150 | note fputc: a byte that does not fit fob is dropped and fobtr is set. fsend (server/main.om) reads |
| 151 | note fobtr before any header goes out and then sends a whole 500 instead of the cut body. |
| 152 | fn fputc c uchar to void |
| 153 | n = get fobl 0 |
| 154 | if lt n len fob |
| 155 | put fob n c |
| 156 | put fobl 0 add n 1 |
| 157 | ret |
| 158 | end |
| 159 | put fobtr 0 1 |
| 160 | end |
| 161 | fn fput s view uchar * to void |
| 162 | for i ulong len s bound 16777216 |
| 163 | fputc get s i |
| 164 | end |
| 165 | end |
| 166 | fn fescc c uchar to void |
| 167 | if eq c 38 |
| 168 | fput tfamp |
| 169 | ret |
| 170 | end |
| 171 | if eq c 60 |
| 172 | fput tflt |
| 173 | ret |
| 174 | end |
| 175 | if eq c 62 |
| 176 | fput tfgt |
| 177 | ret |
| 178 | end |
| 179 | if eq c 34 |
| 180 | fput tfquot |
| 181 | ret |
| 182 | end |
| 183 | if eq c 39 |
| 184 | fput tfapos |
| 185 | ret |
| 186 | end |
| 187 | fputc c |
| 188 | end |
| 189 | fn fesc s view uchar * to void |
| 190 | for i ulong len s bound 16777216 |
| 191 | fescc get s i |
| 192 | end |
| 193 | end |
| 194 | fn fnum v ulong to void |
| 195 | d = new uchar 20 0 |
| 196 | i = ulong 20 |
| 197 | x = v |
| 198 | for it ulong 20 |
| 199 | i = sub i 1 |
| 200 | put d i uchar add rem x 10 48 |
| 201 | x = div x 10 |
| 202 | if eq x 0 |
| 203 | break |
| 204 | end |
| 205 | end |
| 206 | for k ulong sub 20 i bound 20 |
| 207 | fputc get d add i k |
| 208 | end |
| 209 | end |
| 210 | fn fputb to void |
| 211 | for i ulong get fbasel 0 bound 64 |
| 212 | fputc get fbase i |
| 213 | end |
| 214 | end |
| 215 | fn flink p view uchar * to void |
| 216 | fputb |
| 217 | fput p |
| 218 | end |
| 219 | fn fputown to void |
| 220 | for i ulong get fownl 0 bound 64 |
| 221 | fescc get fown i |
| 222 | end |
| 223 | end |
| 224 | fn fputrnm to void |
| 225 | for i ulong get frepol 0 bound 128 |
| 226 | fescc get frepo i |
| 227 | end |
| 228 | end |
| 229 | fn fputpin to void |
| 230 | for i ulong 40 |
| 231 | fputc get fpin i |
| 232 | end |
| 233 | end |
| 234 | fn fputrepo to void |
| 235 | fputown |
| 236 | fputc 47 |
| 237 | fputrnm |
| 238 | end |
| 239 | note fputrl: base + owner/repo, for links into the current repo |
| 240 | fn fputrl to void |
| 241 | fputb |
| 242 | fputrepo |
| 243 | end |
| 244 | note ---- allow-list accessors |
| 245 | fn frnlen i ulong to ulong |
| 246 | ret get fanl i |
| 247 | end |
| 248 | fn fridlen i ulong to ulong |
| 249 | ret get fail i |
| 250 | end |
| 251 | fn frnbyte i ulong k ulong to uchar |
| 252 | ret get fanm add get fano i k |
| 253 | end |
| 254 | fn fridbyte i ulong k ulong to uchar |
| 255 | ret get faid add get faio i k |
| 256 | end |
| 257 | fn fputrn i ulong to void |
| 258 | for k ulong get fanl i bound 256 |
| 259 | fescc frnbyte i k |
| 260 | end |
| 261 | end |
| 262 | fn fputrid i ulong to void |
| 263 | for k ulong get fail i bound 64 |
| 264 | fputc fridbyte i k |
| 265 | end |
| 266 | end |
| 267 | fn fputbr i ulong to void |
| 268 | o = get fabo i |
| 269 | for k ulong get fabl i bound 64 |
| 270 | fescc get fabr add o k |
| 271 | end |
| 272 | end |
| 273 | fn fdesc i ulong to void |
| 274 | o = get fado i |
| 275 | for k ulong get fadl i bound 4096 |
| 276 | fescc get fadesc add o k |
| 277 | end |
| 278 | end |
| 279 | note fputlc: last commit of repo i, "YYYY-MM-DD shortsha subject", escaped, at most 200 bytes (falc keeps |
| 280 | note up to 1000 for the JSON subject) |
| 281 | fn fputlc i ulong to void |
| 282 | o = get falco i |
| 283 | for k ulong hmin get falcl i 200 bound 1024 |
| 284 | fescc get falc add o k |
| 285 | end |
| 286 | end |
| 287 | fn fnmeq i ulong to bool |
| 288 | l = get fanl i |
| 289 | if ne l get frepol 0 |
| 290 | ret false |
| 291 | end |
| 292 | o = get fano i |
| 293 | for k ulong l bound 256 |
| 294 | if ne get fanm add o k get frepo k |
| 295 | ret false |
| 296 | end |
| 297 | end |
| 298 | ret true |
| 299 | end |
| 300 | fn frepoix to ulong |
| 301 | for i ulong get fan 0 bound FAMAX |
| 302 | if fnmeq i |
| 303 | ret i |
| 304 | end |
| 305 | end |
| 306 | ret 9999 |
| 307 | end |
| 308 | note ---- query and form fields: s 0 = fqs, 1 = fbody |
| 309 | fn fsrcb s ulong k ulong to uchar |
| 310 | if eq s 0 |
| 311 | ret get fqs k |
| 312 | end |
| 313 | ret get fbody k |
| 314 | end |
| 315 | fn fsrcl s ulong to ulong |
| 316 | if eq s 0 |
| 317 | ret get fqsl 0 |
| 318 | end |
| 319 | ret get fbodyl 0 |
| 320 | end |
| 321 | fn fhexv c uchar to long |
| 322 | if and ge c 48 le c 57 |
| 323 | ret long sub c 48 |
| 324 | end |
| 325 | if and ge c 97 le c 102 |
| 326 | ret long sub c 87 |
| 327 | end |
| 328 | if and ge c 65 le c 70 |
| 329 | ret long sub c 55 |
| 330 | end |
| 331 | ret -1 |
| 332 | end |
| 333 | note fpmat: field name n starts at i and is followed by '=' |
| 334 | fn fpmat s ulong i ulong ln ulong n view uchar * to bool |
| 335 | e = add i len n |
| 336 | if ge e ln |
| 337 | ret false |
| 338 | end |
| 339 | for k ulong len n bound 4096 |
| 340 | if ne fsrcb s add i k get n k |
| 341 | ret false |
| 342 | end |
| 343 | end |
| 344 | ret eq fsrcb s e 61 |
| 345 | end |
| 346 | fn fpnext s ulong i ulong ln ulong to ulong |
| 347 | j = i |
| 348 | while lt j ln bound 70000 |
| 349 | if eq fsrcb s j 38 |
| 350 | ret add j 1 |
| 351 | end |
| 352 | j = add j 1 |
| 353 | end |
| 354 | ret ln |
| 355 | end |
| 356 | note fpdec: percent-decode from a to the next '&' into fval; returns the length (cap 4096) |
| 357 | fn fpdec s ulong a ulong ln ulong to long |
| 358 | j = a |
| 359 | o = ulong 0 |
| 360 | while lt j ln bound 70000 |
| 361 | c = fsrcb s j |
| 362 | if eq c 38 |
| 363 | break |
| 364 | end |
| 365 | if ge o 4096 |
| 366 | break |
| 367 | end |
| 368 | j = add j 1 |
| 369 | if eq c 43 |
| 370 | c = uchar 32 |
| 371 | end |
| 372 | if eq c 37 |
| 373 | c = fpct s j ln |
| 374 | if lt add j 1 ln |
| 375 | j = add j 2 |
| 376 | end |
| 377 | end |
| 378 | put fval o c |
| 379 | o = add o 1 |
| 380 | end |
| 381 | ret long o |
| 382 | end |
| 383 | fn fpct s ulong j ulong ln ulong to uchar |
| 384 | if ge add j 1 ln |
| 385 | ret uchar 37 |
| 386 | end |
| 387 | a = fhexv fsrcb s j |
| 388 | b = fhexv fsrcb s add j 1 |
| 389 | if or lt a 0 lt b 0 |
| 390 | ret uchar 37 |
| 391 | end |
| 392 | ret uchar add mul a 16 b |
| 393 | end |
| 394 | fn fparam s ulong n view uchar * to long |
| 395 | ln = fsrcl s |
| 396 | i = ulong 0 |
| 397 | while lt i ln bound 70000 |
| 398 | if fpmat s i ln n |
| 399 | ret fpdec s add add i len n 1 ln |
| 400 | end |
| 401 | i = fpnext s i ln |
| 402 | end |
| 403 | ret -1 |
| 404 | end |
| 405 | fn fqget n view uchar * to long |
| 406 | ret fparam 0 n |
| 407 | end |
| 408 | fn fbget n view uchar * to long |
| 409 | ret fparam 1 n |
| 410 | end |
| 411 | note ---- response setters |
| 412 | fn flocb to void |
| 413 | put flocl 0 0 |
| 414 | for i ulong get fbasel 0 bound 64 |
| 415 | flocc get fbase i |
| 416 | end |
| 417 | end |
| 418 | fn flocc c uchar to void |
| 419 | n = get flocl 0 |
| 420 | if lt n len floc |
| 421 | put floc n c |
| 422 | put flocl 0 add n 1 |
| 423 | ret |
| 424 | end |
| 425 | note a Location over 4096 bytes is not sent cut either: the same flag, the same 500 |
| 426 | put fobtr 0 1 |
| 427 | end |
| 428 | fn floct t view uchar * to void |
| 429 | for i ulong len t bound 4096 |
| 430 | flocc get t i |
| 431 | end |
| 432 | end |
| 433 | note flocrepo: Location = base + owner/repo |
| 434 | fn flocrepo to void |
| 435 | flocb |
| 436 | for i ulong get fownl 0 bound 64 |
| 437 | flocc get fown i |
| 438 | end |
| 439 | flocc 47 |
| 440 | for i ulong get frepol 0 bound 128 |
| 441 | flocc get frepo i |
| 442 | end |
| 443 | end |
| 444 | fn fctc c uchar to void |
| 445 | n = get fctl 0 |
| 446 | if lt n len fct |
| 447 | put fct n c |
| 448 | put fctl 0 add n 1 |
| 449 | end |
| 450 | end |
| 451 | fn fsetct t view uchar * to void |
| 452 | put fctl 0 0 |
| 453 | for i ulong len t bound 128 |
| 454 | fctc get t i |
| 455 | end |
| 456 | end |
| 457 | fn fcdc c uchar to void |
| 458 | n = get fcdl 0 |
| 459 | if lt n len fcd |
| 460 | put fcd n c |
| 461 | put fcdl 0 add n 1 |
| 462 | end |
| 463 | end |
| 464 | fn fsetcd t view uchar * to void |
| 465 | put fcdl 0 0 |
| 466 | for i ulong len t bound 512 |
| 467 | fcdc get t i |
| 468 | end |
| 469 | end |
| 470 | fn ftic c uchar to void |
| 471 | n = get ftil 0 |
| 472 | if lt n len fti |
| 473 | put fti n c |
| 474 | put ftil 0 add n 1 |
| 475 | end |
| 476 | end |
| 477 | fn fsetti t view uchar * to void |
| 478 | put ftil 0 0 |
| 479 | for i ulong len t bound 256 |
| 480 | ftic get t i |
| 481 | end |
| 482 | end |
| 483 | note ftirepo: append owner/repo to the title |
| 484 | fn ftirepo to void |
| 485 | for i ulong get fownl 0 bound 64 |
| 486 | ftic get fown i |
| 487 | end |
| 488 | ftic 47 |
| 489 | for i ulong get frepol 0 bound 128 |
| 490 | ftic get frepo i |
| 491 | end |
| 492 | end |
| 493 | fn fscc c uchar to void |
| 494 | n = get fsckl 0 |
| 495 | if lt n len fsck |
| 496 | put fsck n c |
| 497 | put fsckl 0 add n 1 |
| 498 | end |
| 499 | end |
| 500 | fn fsetsck t view uchar * to void |
| 501 | put fsckl 0 0 |
| 502 | for i ulong len t bound 512 |
| 503 | fscc get t i |
| 504 | end |
| 505 | end |
| 506 | fn fetc c uchar to void |
| 507 | n = get fetagl 0 |
| 508 | if lt n len fetag |
| 509 | put fetag n c |
| 510 | put fetagl 0 add n 1 |
| 511 | end |
| 512 | end |
| 513 | fn fccc c uchar to void |
| 514 | n = get fccl 0 |
| 515 | if lt n len fcc |
| 516 | put fcc n c |
| 517 | put fccl 0 add n 1 |
| 518 | end |
| 519 | end |
| 520 | fn fsetcc t view uchar * to void |
| 521 | put fccl 0 0 |
| 522 | for i ulong len t bound 128 |
| 523 | fccc get t i |
| 524 | end |
| 525 | end |
| 526 | fn ffilc c uchar to void |
| 527 | n = get ffilel 0 |
| 528 | if lt n 4095 |
| 529 | put ffile n c |
| 530 | put ffilel 0 add n 1 |
| 531 | end |
| 532 | end |
| 533 | note ---- packed argument list fpk (u16 count, then u16 length + bytes per record) |
| 534 | fn fkb to void |
| 535 | put fpk 0 0 |
| 536 | put fpk 1 0 |
| 537 | put fpkl 0 2 |
| 538 | end |
| 539 | fn fkopen to void |
| 540 | n = get fpkl 0 |
| 541 | if ge n 8190 |
| 542 | ret |
| 543 | end |
| 544 | put fpkr 0 n |
| 545 | put fpk n 0 |
| 546 | put fpk add n 1 0 |
| 547 | put fpkl 0 add n 2 |
| 548 | c = add or ulong get fpk 0 shl ulong get fpk 1 8 1 |
| 549 | put fpk 0 uchar and c 255 |
| 550 | put fpk 1 uchar shr c 8 |
| 551 | end |
| 552 | fn fkc c uchar to void |
| 553 | n = get fpkl 0 |
| 554 | if lt n 8191 |
| 555 | put fpk n c |
| 556 | put fpkl 0 add n 1 |
| 557 | r = get fpkr 0 |
| 558 | k = add or ulong get fpk r shl ulong get fpk add r 1 8 1 |
| 559 | put fpk r uchar and k 255 |
| 560 | put fpk add r 1 uchar shr k 8 |
| 561 | end |
| 562 | end |
| 563 | fn fkt t view uchar * to void |
| 564 | for i ulong len t bound 8192 |
| 565 | fkc get t i |
| 566 | end |
| 567 | end |
| 568 | fn fka t view uchar * to void |
| 569 | fkopen |
| 570 | fkt t |
| 571 | end |
| 572 | note fkrid: append the current repo RID to the open argument |
| 573 | fn fkrid to void |
| 574 | for i ulong get fridl 0 bound 64 |
| 575 | fkc get frid i |
| 576 | end |
| 577 | end |
| 578 | note fkgit: argv = /usr/bin/git --git-dir=<storage>/<frid> (store: the current repo's repo.git) |
| 579 | global fpubgit ulong 1 |
| 580 | text tfpubgit "/app/public-git/" |
| 581 | text tfpubext ".git" |
| 582 | note fksource: public browse and download objects. Signed catalog, records and proposal checks use fkgit/fkgiti. |
| 583 | fn fksource to void |
| 584 | if eq get fpubgit 0 0 |
| 585 | fkgit |
| 586 | ret |
| 587 | end |
| 588 | fkb |
| 589 | fka tfgit |
| 590 | fkopen |
| 591 | fkt tfgitdir |
| 592 | fkt tfpubgit |
| 593 | i = get fridx 0 |
| 594 | if lt i get fan 0 |
| 595 | for k ulong get fanl i bound 128 |
| 596 | fkc frnbyte i k |
| 597 | end |
| 598 | end |
| 599 | fkt tfpubext |
| 600 | end |
| 601 | fn fkgit to void |
| 602 | fkb |
| 603 | fka tfgit |
| 604 | fkopen |
| 605 | fkt tfgitdir |
| 606 | if eq get fsrcst 0 1 |
| 607 | fksdir get fridx 0 |
| 608 | ret |
| 609 | end |
| 610 | fkt tfstore |
| 611 | fkrid |
| 612 | end |
| 613 | note fkgiti: the same for allow-list repo i |
| 614 | fn fkgiti i ulong to void |
| 615 | fkb |
| 616 | fka tfgit |
| 617 | fkopen |
| 618 | fkt tfgitdir |
| 619 | if eq get fsrcst 0 1 |
| 620 | fksdir i |
| 621 | ret |
| 622 | end |
| 623 | fkt tfstore |
| 624 | for k ulong get fail i bound 64 |
| 625 | fkc fridbyte i k |
| 626 | end |
| 627 | end |
| 628 | note fksdir: <fsrt>projects/<name of i>/repo.git into the open argument (no name past the list) |
| 629 | fn fksdir i ulong to void |
| 630 | for k ulong get fsrtl 0 bound 4096 |
| 631 | fkc get fsrt k |
| 632 | end |
| 633 | fkt tfsprj |
| 634 | if lt i get fan 0 |
| 635 | for k ulong get fanl i bound 128 |
| 636 | fkc frnbyte i k |
| 637 | end |
| 638 | end |
| 639 | fkt tfsrg |
| 640 | end |
| 641 | note ---- run argv fpk: no shell, no PATH, empty environment, stdout into fxo |
| 642 | fn fpfdv i ulong to int |
| 643 | b = mul i 4 |
| 644 | v = or ulong get fpfd b shl ulong get fpfd add b 1 8 |
| 645 | v = or v shl ulong get fpfd add b 2 16 |
| 646 | v = or v shl ulong get fpfd add b 3 24 |
| 647 | ret int v |
| 648 | end |
| 649 | fn fchild wfd int to void |
| 650 | fchildin wfd -1 |
| 651 | end |
| 652 | fn fchildin wfd int infd int to void |
| 653 | nul = fopen tfdevnul 0 len tfdevnul 2 0 |
| 654 | if lt nul 0 |
| 655 | exit 126 |
| 656 | end |
| 657 | input = infd |
| 658 | if lt input 0 |
| 659 | input = int nul |
| 660 | end |
| 661 | x = dupfd input 0 0 |
| 662 | if lt x 0 |
| 663 | exit 126 |
| 664 | end |
| 665 | x = dupfd wfd 1 0 |
| 666 | x = dupfd int nul 2 0 |
| 667 | x = execprog fpk 0 get fpkl 0 |
| 668 | exit 127 |
| 669 | end |
| 670 | fn fdrain fd int dl long to bool |
| 671 | for it ulong 10000000 |
| 672 | left = sub dl long monons |
| 673 | if le left 0 |
| 674 | ret false |
| 675 | end |
| 676 | w = waitio fd 1 left |
| 677 | if lt w 0 |
| 678 | if ne w -4 |
| 679 | ret false |
| 680 | end |
| 681 | continue |
| 682 | end |
| 683 | if eq w 0 |
| 684 | continue |
| 685 | end |
| 686 | n = get fxol 0 |
| 687 | if ge n len fxo |
| 688 | put fxotr 0 1 |
| 689 | r = read fd fjunk 0 4096 |
| 690 | if eq r 0 |
| 691 | ret true |
| 692 | end |
| 693 | if lt r 0 |
| 694 | if and ne r -4 ne r -11 |
| 695 | ret false |
| 696 | end |
| 697 | end |
| 698 | continue |
| 699 | end |
| 700 | r = read fd fxo n sub len fxo n |
| 701 | if eq r 0 |
| 702 | ret true |
| 703 | end |
| 704 | if lt r 0 |
| 705 | if and ne r -4 ne r -11 |
| 706 | ret false |
| 707 | end |
| 708 | continue |
| 709 | end |
| 710 | put fxol 0 add n ulong r |
| 711 | end |
| 712 | ret false |
| 713 | end |
| 714 | fn freap pid int dl long to long |
| 715 | for it ulong 1000000 |
| 716 | w = waitpid pid fwst 0 4 1 |
| 717 | if eq w long pid |
| 718 | s = or ulong get fwst 0 shl ulong get fwst 1 8 |
| 719 | if eq and s 127 0 |
| 720 | ret long and shr s 8 255 |
| 721 | end |
| 722 | ret -3 |
| 723 | end |
| 724 | if lt w 0 |
| 725 | if ne w -4 |
| 726 | ret -4 |
| 727 | end |
| 728 | continue |
| 729 | end |
| 730 | if gt long monons dl |
| 731 | x = killpid pid 9 |
| 732 | w = waitpid pid fwst 0 4 0 |
| 733 | ret -1 |
| 734 | end |
| 735 | x = sleepns 200000 |
| 736 | end |
| 737 | ret -4 |
| 738 | end |
| 739 | fn fexec tmo long to long |
| 740 | ret fexecin tmo -1 |
| 741 | end |
| 742 | fn fexecin tmo long infd int to long |
| 743 | put fxol 0 0 |
| 744 | put fxotr 0 0 |
| 745 | r = pipefds fpfd 0 8 524288 |
| 746 | if lt r 0 |
| 747 | ret -2 |
| 748 | end |
| 749 | rfd = fpfdv 0 |
| 750 | wfd = fpfdv 1 |
| 751 | dl = add long monons tmo |
| 752 | pid = fork |
| 753 | if lt pid 0 |
| 754 | x = fclose rfd |
| 755 | x = fclose wfd |
| 756 | ret -2 |
| 757 | end |
| 758 | if eq pid 0 |
| 759 | fchildin wfd infd |
| 760 | exit 127 |
| 761 | end |
| 762 | x = fclose wfd |
| 763 | ok = fdrain rfd dl |
| 764 | x = fclose rfd |
| 765 | if not ok |
| 766 | x = killpid int pid 9 |
| 767 | end |
| 768 | st = freap int pid dl |
| 769 | if not ok |
| 770 | ret -1 |
| 771 | end |
| 772 | ret st |
| 773 | end |
| 774 | fn fxput a ulong n ulong to void |
| 775 | for i ulong n bound 16777216 |
| 776 | fputc get fxo add a i |
| 777 | end |
| 778 | end |
| 779 | fn fxesc a ulong n ulong to void |
| 780 | for i ulong n bound 16777216 |
| 781 | fescc get fxo add a i |
| 782 | end |
| 783 | end |
| 784 | note ---- streaming exec: a listing longer than fxo is read through it in windows, one record at a time |
| 785 | note (the cut copy of fexec is never taken for the whole). fsopen starts argv fpk like fexec; fsnext C |
| 786 | note returns the end e of the next record (the bytes up to the next C, or up to the end of the output), |
| 787 | note which is fxo[get fsrs 0, e) until the next fsnext call. fsnext returns FSEND at the end of the output |
| 788 | note and FSFAIL on a timeout, a read error, or one record longer than fxo (fxotr is set then: the |
| 789 | note caller answers the whole 500 of fincut). fsclose ends the child (killed if it is still writing) |
| 790 | note and returns its exit status, or -1 when the output was not read to its end. |
| 791 | const FSEND ulong 16777217 |
| 792 | const FSFAIL ulong 16777218 |
| 793 | fn fsopen tmo long to bool |
| 794 | put fxol 0 0 |
| 795 | put fxotr 0 0 |
| 796 | put fsp 0 0 |
| 797 | put fsq 0 0 |
| 798 | put fsrs 0 0 |
| 799 | put fsst 3 2 |
| 800 | r = pipefds fpfd 0 8 524288 |
| 801 | if lt r 0 |
| 802 | ret false |
| 803 | end |
| 804 | rfd = fpfdv 0 |
| 805 | wfd = fpfdv 1 |
| 806 | pid = fork |
| 807 | if lt pid 0 |
| 808 | x = fclose rfd |
| 809 | x = fclose wfd |
| 810 | ret false |
| 811 | end |
| 812 | if eq pid 0 |
| 813 | fchild wfd |
| 814 | exit 127 |
| 815 | end |
| 816 | x = fclose wfd |
| 817 | put fsst 0 long rfd |
| 818 | put fsst 1 long pid |
| 819 | put fsst 2 add long monons tmo |
| 820 | put fsst 3 0 |
| 821 | ret true |
| 822 | end |
| 823 | note fsfill: move the unread bytes fxo[fsp, fxol) to the front, then read until fxo is full or the |
| 824 | note output ends. false on a timeout or read error. |
| 825 | fn fsfill to bool |
| 826 | p = get fsp 0 |
| 827 | n = get fxol 0 |
| 828 | if gt p 0 |
| 829 | for i ulong sub n p bound 16777216 |
| 830 | put fxo i get fxo add p i |
| 831 | end |
| 832 | put fxol 0 sub n p |
| 833 | put fsq 0 sub get fsq 0 p |
| 834 | put fsrs 0 0 |
| 835 | put fsp 0 0 |
| 836 | end |
| 837 | if ne get fsst 3 0 |
| 838 | ret eq get fsst 3 1 |
| 839 | end |
| 840 | fd = int get fsst 0 |
| 841 | dl = get fsst 2 |
| 842 | for it ulong 100000000 |
| 843 | m = get fxol 0 |
| 844 | if ge m len fxo |
| 845 | ret true |
| 846 | end |
| 847 | left = sub dl long monons |
| 848 | if le left 0 |
| 849 | put fsst 3 2 |
| 850 | ret false |
| 851 | end |
| 852 | w = waitio fd 1 left |
| 853 | if lt w 0 |
| 854 | if ne w -4 |
| 855 | put fsst 3 2 |
| 856 | ret false |
| 857 | end |
| 858 | continue |
| 859 | end |
| 860 | if eq w 0 |
| 861 | continue |
| 862 | end |
| 863 | r = read fd fxo m sub len fxo m |
| 864 | if eq r 0 |
| 865 | put fsst 3 1 |
| 866 | ret true |
| 867 | end |
| 868 | if lt r 0 |
| 869 | if and ne r -4 ne r -11 |
| 870 | put fsst 3 2 |
| 871 | ret false |
| 872 | end |
| 873 | continue |
| 874 | end |
| 875 | put fxol 0 add m ulong r |
| 876 | end |
| 877 | put fsst 3 2 |
| 878 | ret false |
| 879 | end |
| 880 | fn fsnext c uchar to ulong |
| 881 | for it ulong 100000000 |
| 882 | s = get fsp 0 |
| 883 | n = get fxol 0 |
| 884 | q = get fsq 0 |
| 885 | while lt q n bound 16777216 |
| 886 | if eq get fxo q c |
| 887 | put fsrs 0 s |
| 888 | put fsp 0 add q 1 |
| 889 | put fsq 0 add q 1 |
| 890 | ret q |
| 891 | end |
| 892 | q = add q 1 |
| 893 | end |
| 894 | put fsq 0 q |
| 895 | if eq get fsst 3 1 |
| 896 | if lt s n |
| 897 | put fsrs 0 s |
| 898 | put fsp 0 n |
| 899 | ret n |
| 900 | end |
| 901 | ret FSEND |
| 902 | end |
| 903 | if and eq s 0 ge n len fxo |
| 904 | put fxotr 0 1 |
| 905 | ret FSFAIL |
| 906 | end |
| 907 | if not fsfill |
| 908 | ret FSFAIL |
| 909 | end |
| 910 | end |
| 911 | ret FSFAIL |
| 912 | end |
| 913 | fn fsclose to long |
| 914 | fd = int get fsst 0 |
| 915 | x = fclose fd |
| 916 | pid = int get fsst 1 |
| 917 | done = eq get fsst 3 1 |
| 918 | if not done |
| 919 | x = killpid pid 9 |
| 920 | end |
| 921 | st = freap pid get fsst 2 |
| 922 | put fsst 3 2 |
| 923 | if not done |
| 924 | ret -1 |
| 925 | end |
| 926 | ret st |
| 927 | end |
| 928 | note ---- path builder fpth and whole-file read into fxo |
| 929 | fn fpb to void |
| 930 | put fpthl 0 0 |
| 931 | end |
| 932 | fn fpc c uchar to void |
| 933 | n = get fpthl 0 |
| 934 | if lt n 4095 |
| 935 | put fpth n c |
| 936 | put fpthl 0 add n 1 |
| 937 | end |
| 938 | end |
| 939 | fn fpt t view uchar * to void |
| 940 | for i ulong len t bound 4096 |
| 941 | fpc get t i |
| 942 | end |
| 943 | end |
| 944 | note frdall: read the file at fpth (up to the size of fxo) into fxo; returns bytes or -errno |
| 945 | fn frdall to long |
| 946 | put fxol 0 0 |
| 947 | put fxotr 0 0 |
| 948 | fd = fopen fpth 0 get fpthl 0 524288 0 |
| 949 | if lt fd 0 |
| 950 | ret fd |
| 951 | end |
| 952 | for it ulong 100000 |
| 953 | n = get fxol 0 |
| 954 | if ge n len fxo |
| 955 | put fxotr 0 1 |
| 956 | break |
| 957 | end |
| 958 | r = read int fd fxo n sub len fxo n |
| 959 | if le r 0 |
| 960 | break |
| 961 | end |
| 962 | put fxol 0 add n ulong r |
| 963 | end |
| 964 | x = fclose int fd |
| 965 | ret long get fxol 0 |
| 966 | end |
| 967 | note fmtime: modification time (s) of the file at fpth, 0 when missing |
| 968 | fn fmtime to ulong |
| 969 | r = stat fpth 0 get fpthl 0 fstb 0 128 |
| 970 | if lt r 0 |
| 971 | ret 0 |
| 972 | end |
| 973 | v = ulong 0 |
| 974 | for i ulong 8 |
| 975 | v = or v shl ulong get fstb add 88 i mul i 8 |
| 976 | end |
| 977 | ret v |
| 978 | end |
| 979 | fn flog t view uchar * to void |
| 980 | w = write 2 t 0 len t |
| 981 | end |
| 982 | note ---- per-IP write limit |
| 983 | fn frlkey to ulong |
| 984 | h = ulong 14695981039346656037 |
| 985 | for i ulong get fipl 0 bound 64 |
| 986 | c = ulong get fip i |
| 987 | h = mul xor h c 1099511628211 |
| 988 | end |
| 989 | ret or h 1 |
| 990 | end |
| 991 | fn frlslot h ulong to ulong |
| 992 | e = ulong 256 |
| 993 | o = ulong 0 |
| 994 | for i ulong 256 |
| 995 | k = get frlk i |
| 996 | if eq k h |
| 997 | ret i |
| 998 | end |
| 999 | if and eq k 0 eq e 256 |
| 1000 | e = i |
| 1001 | end |
| 1002 | a = get frlt i |
| 1003 | b = get frlt o |
| 1004 | if lt a b |
| 1005 | o = i |
| 1006 | end |
| 1007 | end |
| 1008 | if lt e 256 |
| 1009 | ret e |
| 1010 | end |
| 1011 | ret o |
| 1012 | end |
| 1013 | fn fratelim to bool |
| 1014 | h = frlkey |
| 1015 | b = frlslot h |
| 1016 | now = div ulong monons 1000000000 |
| 1017 | if ne get frlk b h |
| 1018 | put frlk b h |
| 1019 | put frlt b now |
| 1020 | put frlc b 0 |
| 1021 | end |
| 1022 | if gt sub now get frlt b 60 |
| 1023 | put frlt b now |
| 1024 | put frlc b 0 |
| 1025 | end |
| 1026 | c = get frlc b |
| 1027 | if ge c 30 |
| 1028 | ret false |
| 1029 | end |
| 1030 | put frlc b add c 1 |
| 1031 | ret true |
| 1032 | end |
| 1033 | fn fsetsbx to void |
| 1034 | put fcspsb 0 1 |
| 1035 | end |
| 1036 | note ---- small byte helpers |
| 1037 | fn fisdig c uchar to bool |
| 1038 | ret and ge c 48 le c 57 |
| 1039 | end |
| 1040 | fn fisaln c uchar to bool |
| 1041 | if fisdig c |
| 1042 | ret true |
| 1043 | end |
| 1044 | if and ge c 65 le c 90 |
| 1045 | ret true |
| 1046 | end |
| 1047 | ret and ge c 97 le c 122 |
| 1048 | end |
| 1049 | note fpathat: fpath[i] or 0 past the end |
| 1050 | fn fpathat i ulong to uchar |
| 1051 | if lt i get fpathl 0 |
| 1052 | ret get fpath i |
| 1053 | end |
| 1054 | ret uchar 0 |
| 1055 | end |
| 1056 | note fpeq: fpath equals t exactly |
| 1057 | fn fpeq t view uchar * to bool |
| 1058 | if ne len t get fpathl 0 |
| 1059 | ret false |
| 1060 | end |
| 1061 | for i ulong len t bound 4096 |
| 1062 | if ne get t i get fpath i |
| 1063 | ret false |
| 1064 | end |
| 1065 | end |
| 1066 | ret true |
| 1067 | end |
| 1068 | note fpre: fpath starts with t |
| 1069 | fn fpre t view uchar * to bool |
| 1070 | if gt len t get fpathl 0 |
| 1071 | ret false |
| 1072 | end |
| 1073 | for i ulong len t bound 4096 |
| 1074 | if ne get t i get fpath i |
| 1075 | ret false |
| 1076 | end |
| 1077 | end |
| 1078 | ret true |
| 1079 | end |
| 1080 | note frsteq / frstpre: the same tests on frest |
| 1081 | fn frsteq t view uchar * to bool |
| 1082 | if ne len t get frestl 0 |
| 1083 | ret false |
| 1084 | end |
| 1085 | for i ulong len t bound 4096 |
| 1086 | if ne get t i get frest i |
| 1087 | ret false |
| 1088 | end |
| 1089 | end |
| 1090 | ret true |
| 1091 | end |
| 1092 | fn frstpre t view uchar * to bool |
| 1093 | if gt len t get frestl 0 |
| 1094 | ret false |
| 1095 | end |
| 1096 | for i ulong len t bound 4096 |
| 1097 | if ne get t i get frest i |
| 1098 | ret false |
| 1099 | end |
| 1100 | end |
| 1101 | ret true |
| 1102 | end |
| 1103 | note ---- fdclean: the description of repo i as the forge shows it (fadesc[fado i, +fadl i]). |
| 1104 | note Radicle was removed from Interplane on 2026-11-14 and some identity documents still name it. |
| 1105 | note The stored document is not changed; the copy loaded into fadesc is cut once, at load |
| 1106 | note (server/repos.om fdcopy): sentences (ending ". ") that name the retired network are dropped; |
| 1107 | note when none is left, the text before the first " (" or ": " is kept if it names nothing retired; |
| 1108 | note otherwise the description is empty. |
| 1109 | text tfdt1 "radicle" |
| 1110 | text tfdt2 "heartwood" |
| 1111 | text tfdt3 "rad:z" |
| 1112 | text tfdt4 "/rad/" |
| 1113 | text tfdt5 "rad clone" |
| 1114 | text tfdt6 "rad init" |
| 1115 | text tfdt7 "rad sync" |
| 1116 | text tfdt8 "rad id" |
| 1117 | text tfdt9 "rad patch" |
| 1118 | text tfdt10 "rad auth" |
| 1119 | note fdhas: does fadesc[a, b) hold t (lowercase ASCII), compared without case |
| 1120 | fn fdhas a ulong b ulong t view uchar * to bool |
| 1121 | n = len t |
| 1122 | if lt b add a n |
| 1123 | ret false |
| 1124 | end |
| 1125 | m = add sub sub b a n 1 |
| 1126 | for i ulong m bound 65536 |
| 1127 | ok = true |
| 1128 | for k ulong n bound 16 |
| 1129 | c = get fadesc add add a i k |
| 1130 | if and ge c 65 le c 90 |
| 1131 | c = add c 32 |
| 1132 | end |
| 1133 | if ne c get t k |
| 1134 | ok = false |
| 1135 | end |
| 1136 | end |
| 1137 | if ok |
| 1138 | ret true |
| 1139 | end |
| 1140 | end |
| 1141 | ret false |
| 1142 | end |
| 1143 | fn fdterm a ulong b ulong to bool |
| 1144 | if or fdhas a b tfdt1 or fdhas a b tfdt2 or fdhas a b tfdt3 fdhas a b tfdt4 |
| 1145 | ret true |
| 1146 | end |
| 1147 | if or fdhas a b tfdt5 or fdhas a b tfdt6 or fdhas a b tfdt7 fdhas a b tfdt8 |
| 1148 | ret true |
| 1149 | end |
| 1150 | ret or fdhas a b tfdt9 fdhas a b tfdt10 |
| 1151 | end |
| 1152 | note fdfind: the first p in [a, b-1) with fadesc[p] = c1 and fadesc[p+1] = c2, else b |
| 1153 | fn fdfind a ulong b ulong c1 uchar c2 uchar to ulong |
| 1154 | if le b add a 1 |
| 1155 | ret b |
| 1156 | end |
| 1157 | for i ulong sub sub b a 1 bound 65536 |
| 1158 | p = add a i |
| 1159 | x = get fadesc p |
| 1160 | y = get fadesc add p 1 |
| 1161 | if and eq x c1 eq y c2 |
| 1162 | ret p |
| 1163 | end |
| 1164 | end |
| 1165 | ret b |
| 1166 | end |
| 1167 | note fdmove: copy fadesc[s, x) down to w (w <= s); the new end of what was written |
| 1168 | fn fdmove w ulong s ulong x ulong to ulong |
| 1169 | for k ulong sub x s bound 65536 |
| 1170 | c = get fadesc add s k |
| 1171 | put fadesc add w k c |
| 1172 | end |
| 1173 | ret add w sub x s |
| 1174 | end |
| 1175 | fn fdskip s ulong e ulong to ulong |
| 1176 | p = s |
| 1177 | while lt p e bound 65537 |
| 1178 | if ne get fadesc p 32 |
| 1179 | ret p |
| 1180 | end |
| 1181 | p = add p 1 |
| 1182 | end |
| 1183 | ret p |
| 1184 | end |
| 1185 | note fdsent: keep the sentences of fadesc[o, e) that name nothing retired, packed from o, one |
| 1186 | note space apart; the end of what was kept (o when none was) |
| 1187 | fn fdsent o ulong e ulong to ulong |
| 1188 | w = o |
| 1189 | s = o |
| 1190 | while lt s e bound 65537 |
| 1191 | d = fdfind s e 46 32 |
| 1192 | x = e |
| 1193 | if lt d e |
| 1194 | x = add d 1 |
| 1195 | end |
| 1196 | if not fdterm s x |
| 1197 | if gt w o |
| 1198 | put fadesc w 32 |
| 1199 | w = add w 1 |
| 1200 | end |
| 1201 | w = fdmove w s x |
| 1202 | end |
| 1203 | s = fdskip x e |
| 1204 | end |
| 1205 | ret w |
| 1206 | end |
| 1207 | fn fdclean i ulong to void |
| 1208 | o = get fado i |
| 1209 | e = add o get fadl i |
| 1210 | if not fdterm o e |
| 1211 | ret |
| 1212 | end |
| 1213 | w = fdsent o e |
| 1214 | if gt w o |
| 1215 | put fadl i sub w o |
| 1216 | ret |
| 1217 | end |
| 1218 | p = fdfind o e 32 40 |
| 1219 | if and gt p o not fdterm o p |
| 1220 | put fadl i sub p o |
| 1221 | ret |
| 1222 | end |
| 1223 | p = fdfind o e 58 32 |
| 1224 | if and gt p o not fdterm o p |
| 1225 | put fadl i sub p o |
| 1226 | ret |
| 1227 | end |
| 1228 | put fadl i 0 |
| 1229 | end |