Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/open/intake.om # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / open / intake.om` 4780 lines · 88505 bytes Raw Blame History 1 note intake.om: the Open Humanity intake, its own seeded Omega program. It runs outside the forged 2 note sandbox, because bringing a project means asking the Radicle node to seed and fetch it, and 3 note forged has no network and cannot run rad. 4 note open-intake [once] 5 note open dir: open/ (queue written by forged; list.tsv published here, whole or not at all) 6 note state: one decimal number, the queue offset already handled 7 note work dir: private, outside the folder forged can write: the list this program acts on 8 note (list.tsv; the copy in the open dir is only what forged shows, never read back), 9 note the allow file and command output 10 note Queue lines (from a less trusted process, so every field is checked again): 11 note B rid did note bring the project 12 note W rid did withdraw it (only the key that brought it) 13 note A queue read to its end past 262,144 bytes is renamed to queue.old, finished and removed, so 14 note the queue file does not grow for ever (forged has no cap on it; the data reserve bounds it). 15 note Bring: refused when the key is not in allow, when the project is already on the 16 note shelf, when its storage folder already exists on this node (unless the project is held, below) 17 note or the node already has a seeding policy for it (a repository this node holds or seeds for 18 note another reason is never touched), 19 note or when the storage filesystem is at its reserve. There is no count of projects a key or on 20 note the shelf and no project size limit: alarms measure them (see "alarms" below). A folder left 21 note by this program's own 22 note failed fetch of the same project is removed first. While the row says that fetch failed, a 23 note refusal that is not about a fetch (key not invited, the reserve, node not answering) leaves the row 24 note as it is, so it cannot turn that leftover into a refusal for good. Then rad seed --scope all --fetch (90 s); 25 note not found is refused and unseeded; a fetch that took the storage filesystem below its reserve 26 note is refused, unseeded and removed. The 27 note project's name and description come from its Radicle identity document. 28 note Held: a public project this node already holds and seeds by its own policy (rad inspect 29 note --visibility says public, rad seed lists it) is not fetched, seeded or changed; it gets a row 30 note in state "held", so the owner of the node and the keys it invited can put the node's own 31 note unfinished projects on the shelf. A stored folder the node does not seed (such as the leftover 32 note of this program's own failed fetch) is refused. Withdrawing a held row only marks it; the 33 note repository and its seeding policy stay as they were. 34 note Only a delegate of a held project (a key that rad inspect --delegates lists) may list 35 note it; any other key is refused, so nobody can put the node's own projects on the shelf under 36 note their key. A held row costs no fetch and no disk, so the key_shelf_rows alarm does not count 37 note it (that alarm counts rows in state open or stored, the projects this program fetched or 38 note stored for the key); shelf_rows counts it. 39 note Withdraw: rad unseed, remove the storage folder, mark the row withdrawn. Only a row that is 40 note "open" (or "held", which is only marked) in the private list, brought by the same key, can be 41 note withdrawn, so this program never unseeds or removes a repository it did not bring, whatever 42 note the open dir is made to say. 43 note The page snapshot of a listed project is p/.txt (see "the snapshot" below); 44 note it is written when the project is listed, again when its lister brings it again, and removed 45 note when it is withdrawn. 46 note list.tsv rows: rid, state, did, date, kilobytes, name, description, note. The list has no 47 note size limit: it is read a row at a time and written whole through a temp file (see "the 48 note private list.tsv" below). 49 global od uchar 512 50 global odl ulong 1 51 global sf uchar 512 52 global sfl ulong 1 53 global wd uchar 512 54 global wdl ulong 1 55 global sd uchar 512 56 global sdl ulong 1 57 global hd uchar 256 58 global hdl ulong 1 59 global pth uchar 1100 60 global pthl ulong 1 61 global pt2 uchar 1100 62 global rpk uchar 2300 63 global qb uchar 65536 64 global lb uchar 65536 65 global lbl ulong 1 66 global lcb uchar 131072 67 global lst ulong 16 68 global lerr ulong 1 69 global lfail ulong 1 70 global lrmax ulong 1 71 global lcd uchar 96 72 global lcdl ulong 1 73 global lcdn ulong 1 74 global ob uchar 524288 75 global obl ulong 1 76 global osf ulong 1 77 global oerr ulong 1 78 global ahd uchar 64 79 global ahdl ulong 1 80 global cb uchar 65536 81 global cbl ulong 1 82 global pk uchar 4096 83 global pkn ulong 1 84 global sb uchar 1100 85 global sbl ulong 1 86 global rid uchar 48 87 global ridl ulong 1 88 global did uchar 96 89 global didl ulong 1 90 global nt uchar 208 91 global ntl ulong 1 92 global nm uchar 128 93 global nml ulong 1 94 global ds uchar 408 95 global dsl ulong 1 96 global dt uchar 16 97 global dtl ulong 1 98 global kb ulong 1 99 global ub uchar 408 100 global ubl ulong 1 101 global qe ulong 1 102 text kq "queue" 103 text kqold "queue.old" 104 text klist "list.tsv" 105 text kxt ".tmp" 106 text kout "out" 107 text kb58 "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" 108 text kenv "/usr/bin/env" 109 text kdashi "-i" 110 text khome "HOME=" 111 text kpath "PATH=" 112 text kpath2 "/.local/bin:/usr/bin:/bin" 113 text krad "/.local/bin/rad" 114 text kseed "seed" 115 text kself "self" 116 text kdidf "--did" 117 text kunseed "unseed" 118 text kinspect "inspect" 119 text kpayload "--payload" 120 text kscope "--scope" 121 text kall "all" 122 text kfetch "--fetch" 123 text kradp "rad:" 124 text kdu "/usr/bin/du" 125 text ksk "-sk" 126 text kdate "/usr/bin/date" 127 text ku "-u" 128 text kfmt "+%F" 129 text krm "/usr/bin/rm" 130 text krf "-rf" 131 text kdd "--" 132 text knull "/dev/null" 133 text kopen "open" 134 text kwith "withdrawn" 135 text kheld "held" 136 text kpdir "p" 137 text ktxt ".txt" 138 text kgit "/usr/bin/git" 139 text kgd "--git-dir" 140 text kgn "GIT_CONFIG_NOSYSTEM=1" 141 text kgg "GIT_CONFIG_GLOBAL=/dev/null" 142 text kpathg "PATH=/usr/bin:/bin" 143 text kghead "HEAD" 144 text kgrev "rev-parse" 145 text kgverify "--verify" 146 text kgheadc "HEAD^{commit}" 147 text kuhead ".head" 148 text klnot "open-intake: list.tsv not replaced (a read or a write failed); the old list stays\n" 149 text klread "open-intake: list.tsv read failed; no list decision made; queue request will be retried\n" 150 text kglog "log" 151 text kgm1 "-1" 152 text kgn10 "-10" 153 text kgfh "--format=%H%x09%cs" 154 text kgfc "--format=%h%x09%cs%x09%an%x09%s" 155 text kgls "ls-tree" 156 text kgshow "show" 157 text kgrd1 "HEAD:README.md" 158 text kgrd2 "HEAD:README" 159 text kvis "--visibility" 160 text kpub "public" 161 text kallow "allow" 162 text kremove "remove" 163 text kr0 "refused: intake is open to invited keys for now" 164 text kr1 "refused: this project's storage is already on this node" 165 text kr4 "refused: not found on the Radicle network (publish it, then rad sync --announce)" 166 text kfull "refused: this computer is nearly full; try again later" 167 text kr6 "refused: the Radicle node did not answer; try again" 168 text kr7 "refused: this node already seeds this project" 169 text kr8 "refused: removed by the operator" 170 text kr9 "refused: only a delegate of this project can list it from this node" 171 text kdel "--delegates" 172 text kname "\"name\"" 173 text kdesc "\"description\"" 174 text kunnamed "unnamed project" 175 note ---- small helpers 176 text kustored "stored" 177 text kgallow "GIT_ALLOW_PROTOCOL=file" 178 text kgnlazy "GIT_NO_LAZY_FETCH=1" 179 text kgprompt "GIT_TERMINAL_PROMPT=0" 180 text kuinput "input" 181 text kusign "signers" 182 text kuarc "upload.tar" 183 text kutree "upload-tree" 184 text kugit "upload.git" 185 text kuup "up" 186 text kua "a" 187 text kroot "/" 188 text kdot "." 189 text kutmp ".tar.tmp" 190 text kubsigw "bring.sig" 191 text kub64 ".b64" 192 text kusig ".sig" 193 text kujson ".json" 194 text kutar ".tar" 195 text kuhash ".sha256" 196 text kuhex "`0123456789abcdef`" 197 text ku64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 198 text kustmt "interplane-bring 1\nsha256 " 199 text kufiles "\nfiles " 200 text kubytes "\nbytes " 201 text kuname "\nname " 202 text kussh "/usr/bin/ssh-keygen" 203 text kuY "-Y" 204 text kuverify "verify" 205 text kuf "-f" 206 text kuI "-I" 207 text kuuser "forge-user" 208 text kun "-n" 209 text kuns "interplane-bring" 210 text kus "-s" 211 text kued "ssh-`ed25519`" 212 text kugd "--git-dir=" 213 text kuwt "--work-tree=" 214 text kuc "-c" 215 text kuhooks "core.hooksPath=/dev/null" 216 text kusign0 "commit.gpgsign=false" 217 text kuinit "init" 218 text kubare "--bare" 219 text kutmpl "--template=" 220 text kubr "--initial-branch=main" 221 text kuadd "add" 222 text kuall "--all" 223 text kuforce "--force" 224 text kucommit "commit" 225 text kunover "--no-verify" 226 text kum "-m" 227 text kumsg "Project brought through Interplane" 228 text kuan "GIT_AUTHOR_NAME=" 229 text kuae "GIT_AUTHOR_EMAIL=" 230 text kucn "GIT_COMMITTER_NAME=Interplane hub" 231 text kuce "GIT_COMMITTER_EMAIL=" 232 text kupub "--public" 233 text kunocon "--no-confirm" 234 text kunm "--name=" 235 text kudesceq "--description=" 236 text kudesc "--description" 237 text kubranch "--default-branch" 238 text kumain "main" 239 text kuarch "archive" 240 text kuformat "--format=tar" 241 text kuinter "/.interplane" 242 text kubtxt "/.interplane/brought.txt" 243 text kubsig "/.interplane/brought.sig" 244 text kurbad "refused: malformed upload" 245 text kursha "refused: wrong archive sha256" 246 text kurbyte "refused: wrong archive byte count" 247 text kursig "refused: bad bring signature" 248 text kurtar "refused: archive is not canonical ustar" 249 text kurkey "refused: the archive holds a private key" 250 text kurgit "refused: the archive holds a .git folder" 251 text kurfile "refused: wrong file count" 252 text kurdisk "refused: storage failed" 253 text kurinit "refused: repository creation failed" 254 text kusj1 "{\"schema\":\"interplane/open-upload/1\",\"state\":\"" 255 text kusj2 "\",\"rid\":null,\"page\":null}\n" 256 text kusj3 "\",\"rid\":\"rad:" 257 text kusj4 "\",\"page\":\"/open/" 258 text kusj5 "\"}\n" 259 global ustdin ulong 1 260 global usha uchar 64 261 global udh uchar 16 262 global uvals ulong 4 263 global ubuf uchar 65536 264 global uhdr uchar 512 265 global uprev uchar 256 266 global uprevl ulong 1 267 global uname uchar 256 268 global unamel ulong 1 269 global ufd long 1 270 global uscan ulong 2 271 global uerr ulong 1 272 global udigest uchar 64 273 global usigbuf uchar 65536 274 global usiglen ulong 1 275 global upiped ulong 1 276 global upipew ulong 1 277 text ubegin "-----BEGIN" 278 text upriv "PRIVATE KEY-----" 279 global avals ulong 13 280 global avol ulong 12 281 global akeys uchar 1152 282 global akl ulong 12 283 global aref ulong 1 284 global alimit ulong 1 285 global dfv ulong 2 286 global adelmax ulong 1 287 global apass ulong 1 288 global aqmax ulong 1 289 text kadf "/usr/bin/df" 290 text kadfp "-P" 291 text kaprefix "ALARM name value threshold source:\n" 292 text kalarms "alarms.txt" 293 text kajson "alarms.json" 294 text kan0 "key_shelf_rows" 295 text kan1 "shelf_rows" 296 text kan2 "largest_project_kib" 297 text kan3 "queue_bytes" 298 text kan4 "spool_bytes" 299 text kan5 "rad_seed_output_bytes" 300 text kan6 "storage_free_bytes" 301 text kan8 "data_free_bytes" 302 text kan9 "list_bytes" 303 text kan10 "reserve_refused" 304 text kan11 "list_row_bytes" 305 text kanull "null" 306 text kadfb "-B1" 307 text krsvf "reserve" 308 text krsvs "OPEN_RESERVE_STORAGE_BYTES=" 309 text krsvd "OPEN_RESERVE_DATA_BYTES=" 310 text kspace "space" 311 text kspfree "free " 312 text kspres " reserve " 313 text kspat " at " 314 text kan7 "rad_delegates_output_bytes" 315 text kas0 "work list.tsv; no cap: the former 8 a key, 200 on the shelf, 51200 KiB a project" 316 text kas3 "open queue; no cap in forged (the former `1048576`): bounded by the data reserve" 317 text kas4 "open/up spool; no cap: bounded by the data reserve" 318 text kas5 "rad seed output; intake cb buffer 65535" 319 text kas6 "df storage filesystem; warning at twice OPEN_RESERVE_STORAGE_BYTES" 320 text kas8 "df open dir filesystem; warning at twice OPEN_RESERVE_DATA_BYTES" 321 text kas9 "work list.tsv bytes; no size limit (read a row at a time); 80 percent of the former 262143-byte read buffer" 322 text kas11 "longest work list.tsv row (this program writes at most about 900 bytes); kept whole at any length; the intake and forged read the first 4096 bytes of each field" 323 text kas10 "a bring refused at a free-space reserve; value free bytes, volume asked" 324 text kas7 "largest rad inspect --delegates output; intake cb buffer 65535" 325 text kaj1 "{\"schema\":\"interplane/open-alarms/1\",\"alarms\":[" 326 text kaj2 "{\"name\":\"" 327 text kaj3 "\",\"value\":" 328 text kaj4 ",\"threshold\":" 329 text kaj5 ",\"source\":\"" 330 text kaj6 "\",\"firing\":" 331 text kaj7 "]}\n" 332 text kaj8 "\",\"key\":" 333 text kaj9 ",\"volume_bytes\":" 334 text kaj10 ",\"firing\":" 335 text katrue "true" 336 text kafalse "false" 337 fn b58 c uchar to bool 338 for i ulong len kb58 bound 64 339 if eq get kb58 i c 340 ret true 341 end 342 end 343 ret false 344 end 345 note sbz / sbt / sbg: the scratch string: empty it, append a text, append the rid 346 fn sbz to void 347 put sbl 0 0 348 end 349 fn sbc c uchar to void 350 o = get sbl 0 351 if lt o 1099 352 put sb o c 353 put sbl 0 add o 1 354 end 355 end 356 fn sbt t view uchar * to void 357 for i ulong len t bound 256 358 sbc get t i 359 end 360 end 361 fn sbrid to void 362 for i ulong get ridl 0 bound 44 363 sbc get rid i 364 end 365 end 366 fn sbhome to void 367 for i ulong get hdl 0 bound 256 368 sbc get hd i 369 end 370 end 371 fn sbstore to void 372 for i ulong get sdl 0 bound 512 373 sbc get sd i 374 end 375 sbrid 376 end 377 note ---- the packed argument list (u16 count, then per record a u16 length and the bytes) 378 fn pknew to void 379 put pkn 0 2 380 put pk 0 0 381 put pk 1 0 382 end 383 fn pkrec n ulong to ulong 384 o = get pkn 0 385 put pk o uchar and n 255 386 put pk add o 1 uchar shr n 8 387 put pk 0 uchar add get pk 0 1 388 ret add o 2 389 end 390 fn pkt t view uchar * to void 391 n = len t 392 o = pkrec n 393 for i ulong n bound 256 394 put pk add o i get t i 395 end 396 put pkn 0 add o n 397 end 398 note pks: the scratch string as one argument 399 fn pks to void 400 n = get sbl 0 401 o = pkrec n 402 for i ulong n bound 1100 403 put pk add o i get sb i 404 end 405 put pkn 0 add o n 406 end 407 note pkrad: env -i HOME= PATH=/.local/bin:/usr/bin:/bin /.local/bin/rad 408 fn pkrad to void 409 pknew 410 pkt kenv 411 pkt kdashi 412 sbz 413 sbt khome 414 sbhome 415 pks 416 sbz 417 sbt kpath 418 sbhome 419 sbt kpath2 420 pks 421 sbz 422 sbhome 423 sbt krad 424 pks 425 end 426 fn pkridarg to void 427 sbz 428 sbt kradp 429 sbrid 430 pks 431 end 432 note wpath: pth = work dir + name 433 fn wpath name view uchar * to void 434 n = get wdl 0 435 for i ulong n bound 512 436 put pth i get wd i 437 end 438 for i ulong len name bound 16 439 put pth add n i get name i 440 end 441 put pthl 0 add n len name 442 end 443 note ---- the open dir belongs to forged (its sandbox can write it), so every write, rename and 444 note unlink this program makes there, and in its up/, a/ and p/, goes through a folder this program 445 note opened itself with O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC (573440) and made its working directory 446 note (fchdir), and then names only a file inside it. A link planted for the open dir or one of those 447 note folders is refused: nothing is written. A link planted for a file is replaced by the rename of 448 note wr (temp file O_EXCL|O_NOFOLLOW, then rename), never followed. unpin returns to "/"; every 449 note program this one starts runs in "/". 450 fn pinopen to bool 451 r = chdir kroot 0 1 452 n = get odl 0 453 if lt n 2 454 ret false 455 end 456 fd = fopen od 0 sub n 1 573440 0 457 if lt fd 0 458 ret false 459 end 460 r = fchdir int fd 461 x = fclose int fd 462 if ne r 0 463 unpin 464 ret false 465 end 466 ret true 467 end 468 note pinsub: pin the open dir, then its folder dn (made first when mk) 469 fn pinsub dn view uchar * mk bool to bool 470 if not pinopen 471 ret false 472 end 473 if mk 474 r = makedir dn 0 len dn 448 475 end 476 fd = fopen dn 0 len dn 573440 0 477 if lt fd 0 478 unpin 479 ret false 480 end 481 r = fchdir int fd 482 x = fclose int fd 483 if ne r 0 484 unpin 485 ret false 486 end 487 ret true 488 end 489 fn unpin to void 490 r = chdir kroot 0 1 491 end 492 note ropen: open pth, a file forged can plant, for reading: O_NONBLOCK (a FIFO does not wait for a 493 note writer), O_NOFOLLOW, O_CLOEXEC (559104); -1 unless fstat says it is a regular file, so a FIFO, 494 note device or folder planted as the queue or a spool file is skipped and the pass goes on 495 fn ropen to long 496 fd = fopen pth 0 get pthl 0 559104 0 497 if lt fd 0 498 ret -1 499 end 500 if not uisreg int fd 501 x = fclose int fd 502 ret -1 503 end 504 ret fd 505 end 506 fn uisreg fd int to bool 507 st = new uchar 128 0 508 r = fstat fd st 0 128 509 if ne r 0 510 ret false 511 end 512 m = or ulong get st 16 shl ulong get st 17 8 513 ret eq and m 61440 32768 514 end 515 note rname: pth = name, a file in the pinned folder 516 fn rname name view uchar * to void 517 for i ulong len name bound 32 518 put pth i get name i 519 end 520 put pthl 0 len name 521 end 522 note child: stdin from /dev/null, stdout to out, stderr to /dev/null, then the program 523 fn child to void 524 a = new uchar 4096 0 525 r = setpgid 0 0 526 r = chdir kroot 0 1 527 fd = long -1 528 if eq get ustdin 0 0 529 fd = fopen knull 0 len knull 524288 0 530 else 531 wpath kuinput 532 fd = fopen pth 0 get pthl 0 557056 0 533 end 534 if lt fd 0 535 exit 121 536 end 537 r = dupfd int fd 0 0 538 x = fclose int fd 539 if ne get upiped 0 0 540 r = dupfd int get upipew 0 1 0 541 else 542 wpath kout 543 r = unlink pth 0 get pthl 0 544 fd = fopen pth 0 get pthl 0 557249 384 545 if lt fd 0 546 exit 123 547 end 548 r = dupfd int fd 1 0 549 x = fclose int fd 550 end 551 fd = fopen knull 0 len knull 524289 0 552 if lt fd 0 553 exit 125 554 end 555 r = dupfd int fd 2 0 556 x = fclose int fd 557 n = get pkn 0 558 for i ulong n bound 4096 559 put a i get pk i 560 end 561 r = execprog a 0 n 562 exit 127 563 end 564 note run: the packed command with a deadline in seconds. True when it exited 0. Its stdout is in cb. 565 fn run secs ulong to bool 566 st = new uchar 4 0 567 put cbl 0 0 568 pid = fork 569 if lt pid 0 570 ret false 571 end 572 if eq pid 0 573 child 574 exit 127 575 end 576 r = setpgid int pid int pid 577 ok = false 578 for i ulong mul secs 100 bound 12000 579 r = waitpid int pid st 0 4 1 580 if eq r pid 581 ok = true 582 break 583 end 584 if lt r 0 585 break 586 end 587 r = sleepns `10000000` 588 end 589 if not ok 590 r = killpid int neg pid 9 591 r = killpid int pid 9 592 for i ulong 200 593 r = waitpid int pid st 0 4 1 594 if ne r 0 595 break 596 end 597 r = sleepns `10000000` 598 end 599 ret false 600 end 601 for i ulong 4 602 if ne get st i 0 603 ret false 604 end 605 end 606 wpath kout 607 fd = fopen pth 0 get pthl 0 557056 0 608 if lt fd 0 609 ret true 610 end 611 n = ulong 0 612 k = long 1 613 while gt k 0 bound 65536 614 k = read int fd cb n sub 65535 n 615 if gt k 0 616 n = add n ulong k 617 end 618 end 619 x = fclose int fd 620 put cbl 0 n 621 ret true 622 end 623 note ---- the commands 624 fn doseed to bool 625 pkrad 626 pkt kseed 627 pkridarg 628 pkt kscope 629 pkt kall 630 pkt kfetch 631 ret run 90 632 end 633 fn dounseed to void 634 pkrad 635 pkt kunseed 636 pkridarg 637 ok = run 30 638 end 639 fn dopayload to bool 640 pkrad 641 pkt kinspect 642 pkridarg 643 pkt kpayload 644 ret run 30 645 end 646 note dovis: rad inspect --visibility says public 647 fn dovis to bool 648 pkrad 649 pkt kinspect 650 pkridarg 651 pkt kvis 652 if not run 30 653 ret false 654 end 655 if lt get cbl 0 len kpub 656 ret false 657 end 658 for i ulong len kpub bound 16 659 if ne get cb i get kpub i 660 ret false 661 end 662 end 663 ret true 664 end 665 note delat: the current did starts a line of the command output at i and ends there (the output 666 note of rad inspect --delegates is one "did:key:... (alias)" a line), so a key that is only the 667 note start of a longer one never matches 668 fn delat i ulong to bool 669 m = get didl 0 670 n = get cbl 0 671 if gt add i m n 672 ret false 673 end 674 if gt i 0 675 if ne get cb sub i 1 10 676 ret false 677 end 678 end 679 for k ulong m bound 96 680 if ne get cb add i k get did k 681 ret false 682 end 683 end 684 e = add i m 685 if eq e n 686 ret true 687 end 688 c = get cb e 689 if eq c 32 690 ret true 691 end 692 if eq c 10 693 ret true 694 end 695 ret eq c 13 696 end 697 note dodel: 1 when rad inspect --delegates lists the current did, 0 when it does not, 2 when 698 note the node did not answer 699 fn dodel to ulong 700 if eq get didl 0 0 701 ret 0 702 end 703 pkrad 704 pkt kinspect 705 pkridarg 706 pkt kdel 707 if not run 30 708 ret 2 709 end 710 n = get cbl 0 711 put adelmax 0 amax get adelmax 0 n 712 if ge n 65535 713 ret 2 714 end 715 i = ulong 0 716 while lt i n bound 65536 717 if delat i 718 ret 1 719 end 720 i = add i 1 721 end 722 ret 0 723 end 724 fn dodu to bool 725 pknew 726 pkt kdu 727 pkt ksk 728 sbz 729 sbstore 730 pks 731 if not run 30 732 ret false 733 end 734 v = ulong 0 735 n = get cbl 0 736 i = ulong 0 737 while lt i n bound 24 738 c = get cb i 739 if or lt c 48 gt c 57 740 break 741 end 742 v = add mul v 10 ulong sub c 48 743 i = add i 1 744 end 745 put kb 0 v 746 ret gt i 0 747 end 748 fn dodate to void 749 put dtl 0 0 750 pknew 751 pkt kdate 752 pkt ku 753 pkt kfmt 754 if not run 10 755 ret 756 end 757 n = get cbl 0 758 if lt n 10 759 ret 760 end 761 for i ulong 10 762 c = get cb i 763 ok = false 764 if and ge c 48 le c 57 765 ok = true 766 end 767 if eq c 45 768 ok = true 769 end 770 if not ok 771 ret 772 end 773 put dt i c 774 end 775 put dtl 0 10 776 end 777 note dorm: remove the storage folder of the current rid. Callers have checked that this program 778 note brought it. 779 fn dorm to void 780 pknew 781 pkt krm 782 pkt krf 783 pkt kdd 784 sbz 785 sbstore 786 pks 787 ok = run 60 788 end 789 fn stored to bool 790 sbz 791 sbstore 792 ret eq faccess sb 0 get sbl 0 0 0 793 end 794 note jget: the string value of key t in the command output, sanitized, at most cap bytes, into 795 note nm (which 0) or ds (which 1). An escape becomes a space, a control byte a space. 796 note uin: lo <= c <= hi 797 fn uin c uchar lo uchar hi uchar to bool 798 ret and ge c lo le c hi 799 end 800 note uvalid: the length of the well-formed UTF-8 character at ub[i], or 0 (RFC 3629: no overlong 801 note form, no surrogate, nothing past U+10FFFF). The same rule as research/agent.om. 802 fn uvalid i ulong to ulong 803 n = get ubl 0 804 c = get ub i 805 if lt c 128 806 ret 1 807 end 808 if uin c 194 223 809 if lt add i 1 n 810 if uin get ub add i 1 128 191 811 ret 2 812 end 813 end 814 ret 0 815 end 816 lo = uchar 128 817 hi = uchar 191 818 if uin c 224 239 819 if eq c 224 820 lo = uchar 160 821 end 822 if eq c 237 823 hi = uchar 159 824 end 825 if lt add i 2 n 826 if and uin get ub add i 1 lo hi uin get ub add i 2 128 191 827 ret 3 828 end 829 end 830 ret 0 831 end 832 if uin c 240 244 833 if eq c 240 834 lo = uchar 144 835 end 836 if eq c 244 837 hi = uchar 143 838 end 839 if lt add i 3 n 840 if and and uin get ub add i 1 lo hi uin get ub add i 2 128 191 uin get ub add i 3 128 191 841 ret 4 842 end 843 end 844 end 845 ret 0 846 end 847 note usan: every byte of ub that does not start a well-formed UTF-8 character becomes "?". A name 848 note or description cut at its cap can end inside a character; the list and its JSON stay UTF-8. 849 fn usan to void 850 n = get ubl 0 851 i = ulong 0 852 while lt i n bound 408 853 k = uvalid i 854 if eq k 0 855 put ub i 63 856 i = add i 1 857 else 858 i = add i k 859 end 860 end 861 end 862 fn usnm to void 863 n = get nml 0 864 for i ulong n bound 120 865 put ub i get nm i 866 end 867 put ubl 0 n 868 usan 869 for i ulong n bound 120 870 put nm i get ub i 871 end 872 end 873 fn usds to void 874 n = get dsl 0 875 for i ulong n bound 400 876 put ub i get ds i 877 end 878 put ubl 0 n 879 usan 880 for i ulong n bound 400 881 put ds i get ub i 882 end 883 end 884 fn usnt to void 885 n = get ntl 0 886 for i ulong n bound 200 887 put ub i get nt i 888 end 889 put ubl 0 n 890 usan 891 for i ulong n bound 200 892 put nt i get ub i 893 end 894 end 895 note policy: does the node's seeding policy list (rad seed without a rid) name the current rid? 896 note 0 no, 1 yes, 2 the node did not answer or the list did not fit. A bring goes on only at 0, so 897 note it never rewrites the scope of a policy the owner set, and a failed fetch never unseeds one. 898 note rmatch: the rid is the first column of a row: "rad:" then the rid at the start of a line, or 899 note after the table's left border and one space ("│ rad:", the border is E2 94 82), and followed 900 note by a space, tab, CR, LF or the end of the output; so a longer rid, or a project name that 901 note holds the rid's text, never matches 902 fn rmatch i ulong to bool 903 n = get ridl 0 904 lim = get cbl 0 905 if lt i 4 906 ret false 907 end 908 s = sub i 4 909 if ne get cb s 114 910 ret false 911 end 912 if ne get cb add s 1 97 913 ret false 914 end 915 if ne get cb add s 2 100 916 ret false 917 end 918 if ne get cb add s 3 58 919 ret false 920 end 921 st = false 922 if eq s 0 923 st = true 924 else 925 if eq get cb sub s 1 10 926 st = true 927 end 928 end 929 if not st 930 if ge s 4 931 if eq get cb sub s 1 32 932 if eq get cb sub s 2 130 933 if eq get cb sub s 3 148 934 if eq get cb sub s 4 226 935 if eq s 4 936 st = true 937 else 938 st = eq get cb sub s 5 10 939 end 940 end 941 end 942 end 943 end 944 end 945 end 946 if not st 947 ret false 948 end 949 for k ulong n bound 44 950 if ne get cb add i k get rid k 951 ret false 952 end 953 end 954 e = add i n 955 if ge e lim 956 ret true 957 end 958 c = get cb e 959 if eq c 32 960 ret true 961 end 962 if eq c 9 963 ret true 964 end 965 if eq c 13 966 ret true 967 end 968 ret eq c 10 969 end 970 fn policy to ulong 971 pkrad 972 pkt kseed 973 if not run 30 974 ret 2 975 end 976 n = get cbl 0 977 if ge n 65535 978 ret 2 979 end 980 m = get ridl 0 981 i = ulong 0 982 while le add i m n bound 65536 983 if rmatch i 984 ret 1 985 end 986 i = add i 1 987 end 988 ret 0 989 end 990 fn jput which ulong o ulong c uchar to void 991 if eq which 0 992 put nm o c 993 else 994 put ds o c 995 end 996 end 997 fn jmatch t view uchar * i ulong to bool 998 for k ulong len t bound 16 999 if ne get cb add i k get t k 1000 ret false 1001 end 1002 end 1003 ret true 1004 end 1005 fn jfind t view uchar * to ulong 1006 n = get cbl 0 1007 m = len t 1008 i = ulong 0 1009 while lt add i m n bound 65536 1010 if jmatch t i 1011 ret add i m 1012 end 1013 i = add i 1 1014 end 1015 ret n 1016 end 1017 fn jget t view uchar * which ulong cap ulong to ulong 1018 n = get cbl 0 1019 p = jfind t 1020 while lt p n bound 65536 1021 if eq get cb p 34 1022 break 1023 end 1024 p = add p 1 1025 end 1026 if ge p n 1027 ret 0 1028 end 1029 p = add p 1 1030 o = ulong 0 1031 while lt p n bound 65536 1032 c = get cb p 1033 if eq c 34 1034 break 1035 end 1036 if eq c 92 1037 p = add p 1 1038 c = uchar 32 1039 end 1040 if or lt c 32 eq c 127 1041 c = uchar 32 1042 end 1043 if lt o cap 1044 jput which o c 1045 o = add o 1 1046 end 1047 p = add p 1 1048 end 1049 ret o 1050 end 1051 note ---- the private list.tsv, a row at a time. It has no size limit: the file is read in chunks 1052 note of 65,536 bytes (pread at the slot's own offset) through one of two slots, so one scan can run 1053 note inside another (arows counts a key's rows while it walks the list). The current row is in lb, 1054 note lbl bytes from offset 0, so the row helpers below take s = 0 and e = lbl. lb holds the first 1055 note 4,096 bytes of each field (every tab is kept, so the fields keep their places): this program 1056 note writes rows of at most about 900 bytes and reads no field past 400. A row that is not all in 1057 note lb (its real length, in the slot, differs from lbl) is copied whole from the file by keep 1058 note (kcopy), and the list_row_bytes alarm measures the longest row. A failed read sets lerr: save 1059 note then replaces nothing, so the list is never cut. 1060 note Slot sl is lst[8 sl ..]: 0 fd + 1 (0 closed), 1 next byte of its chunk (lcb[65536 sl ..]), 1061 note 2 bytes in the chunk, 3 file offset of the chunk, 4 the current row's length, 5 its offset. 1062 fn lopen sl ulong to void 1063 lclose sl 1064 wpath klist 1065 fd = fopen pth 0 get pthl 0 557056 0 1066 if lt fd 0 1067 if ne fd -2 1068 put lerr 0 1 1069 end 1070 ret 1071 end 1072 o = mul sl 8 1073 put lst o add ulong fd 1 1074 for i ulong 5 1075 put lst add add o 1 i 0 1076 end 1077 end 1078 fn lclose sl ulong to void 1079 o = mul sl 8 1080 f = get lst o 1081 if gt f 0 1082 x = fclose int sub f 1 1083 end 1084 put lst o 0 1085 end 1086 note lfill: the next chunk of slot sl; false at the end of the file or on a failed read (lerr) 1087 fn lfill sl ulong to bool 1088 o = mul sl 8 1089 pos = add get lst add o 3 get lst add o 2 1090 k = long -4 1091 while eq k -4 bound `1000000` 1092 k = pread int sub get lst o 1 lcb mul sl 65536 65536 long pos 1093 end 1094 if le k 0 1095 if lt k 0 1096 put lerr 0 1 1097 end 1098 ret false 1099 end 1100 put lst add o 3 pos 1101 put lst add o 2 ulong k 1102 put lst add o 1 0 1103 ret true 1104 end 1105 note lnext: the next row of slot sl into lb; false at the end of the list (the slot is closed) 1106 fn lnext sl ulong to bool 1107 o = mul sl 8 1108 if eq get lst o 0 1109 ret false 1110 end 1111 n = ulong 0 1112 m = ulong 0 1113 fl = ulong 0 1114 st = add get lst add o 3 get lst add o 1 1115 got = false 1116 while true bound `9223372036854775807` 1117 p = get lst add o 1 1118 if ge p get lst add o 2 1119 if lfill sl 1120 continue 1121 end 1122 if ne get lerr 0 0 1123 lclose sl 1124 put lbl 0 0 1125 ret false 1126 end 1127 if not got 1128 lclose sl 1129 ret false 1130 end 1131 break 1132 end 1133 put lst add o 1 add p 1 1134 got = true 1135 c = get lcb add mul sl 65536 p 1136 if eq c 10 1137 break 1138 end 1139 if eq c 9 1140 fl = ulong 0 1141 end 1142 if and lt fl 4096 lt m 65536 1143 put lb m c 1144 m = add m 1 1145 end 1146 if ne c 9 1147 fl = add fl 1 1148 end 1149 n = add n 1 1150 end 1151 put lbl 0 m 1152 put lst add o 4 n 1153 put lst add o 5 st 1154 if gt n get lrmax 0 1155 put lrmax 0 n 1156 end 1157 ret true 1158 end 1159 note lfe: the end of the field that starts at a in the row ending at e 1160 fn lfe a ulong e ulong to ulong 1161 p = a 1162 while lt p e bound 262144 1163 if eq get lb p 9 1164 ret p 1165 end 1166 p = add p 1 1167 end 1168 ret e 1169 end 1170 note lfld: start of field k of the row s..e 1171 fn lfld s ulong e ulong k ulong to ulong 1172 p = s 1173 j = ulong 0 1174 while lt j k bound 8 1175 p = lfe p e 1176 if lt p e 1177 p = add p 1 1178 end 1179 j = add j 1 1180 end 1181 ret p 1182 end 1183 note lisrid: the row's first field is the current rid 1184 fn lisrid s ulong e ulong to bool 1185 b = lfe s e 1186 n = get ridl 0 1187 if ne sub b s n 1188 ret false 1189 end 1190 for i ulong n bound 44 1191 if ne get lb add s i get rid i 1192 ret false 1193 end 1194 end 1195 ret true 1196 end 1197 fn lisopen s ulong e ulong to bool 1198 a = lfld s e 1 1199 b = lfe a e 1200 if ne sub b a len kopen 1201 ret false 1202 end 1203 for i ulong len kopen 1204 if ne get lb add a i get kopen i 1205 ret false 1206 end 1207 end 1208 ret true 1209 end 1210 note lisdid: the row's did (field 2) is the current did 1211 fn lisdid s ulong e ulong to bool 1212 a = lfld s e 2 1213 b = lfe a e 1214 n = get didl 0 1215 if ne sub b a n 1216 ret false 1217 end 1218 for i ulong n bound 96 1219 if ne get lb add a i get did i 1220 ret false 1221 end 1222 end 1223 ret true 1224 end 1225 note lfind: the row of the current rid into lb (true), or false when the list has none 1226 note lvalid: validate a complete read before making a list decision. In particular allarchives 1227 note and sweep must not act on a prefix before a later read fails. The decision scan reads the 1228 note same private file; nobody else writes it. Missing file is an empty list, other errors fail. 1229 fn lvalid to bool 1230 put lerr 0 0 1231 lopen 0 1232 while lnext 0 bound `9223372036854775807` 1233 end 1234 ret lok 1235 end 1236 fn lok to bool 1237 if ne get lerr 0 0 1238 put lfail 0 1 1239 w = write 2 klread 0 len klread 1240 ret false 1241 end 1242 ret true 1243 end 1244 fn lfind to bool 1245 if not lvalid 1246 ret false 1247 end 1248 lopen 0 1249 while lnext 0 bound `9223372036854775807` 1250 if lisrid 0 get lbl 0 1251 lclose 0 1252 ret true 1253 end 1254 end 1255 ok = lok 1256 ret false 1257 end 1258 note lstate: the state (field 1) of the row s..e is t 1259 fn lstate s ulong e ulong t view uchar * to bool 1260 a = lfld s e 1 1261 b = lfe a e 1262 if ne sub b a len t 1263 ret false 1264 end 1265 for i ulong len t bound 128 1266 if ne get lb add a i get t i 1267 ret false 1268 end 1269 end 1270 ret true 1271 end 1272 note lfailed: the private list says this program's own fetch of the current rid failed (not found 1273 note or over the size limit). A storage folder found for it then is that fetch's leftover (the 1274 note node can finish a fetch after rad was stopped at its deadline), not a repository of the owner. 1275 fn lfailed to bool 1276 if not lfind 1277 ret false 1278 end 1279 e = get lbl 0 1280 if lstate 0 e kr4 1281 ret true 1282 end 1283 ret lstate 0 e kfull 1284 end 1285 note lpub: the row is on the shelf: open (this program fetched the project) or held (the node 1286 note already had it; the row only lists it) 1287 fn lpub s ulong e ulong to bool 1288 if lisopen s e 1289 ret true 1290 end 1291 if lstate s e kustored 1292 ret true 1293 end 1294 ret lstate s e kheld 1295 end 1296 note lcount: rows on the shelf (open or held); mine true counts only the current did's rows in 1297 note state open, the projects this program fetched for that key (a held row costs no fetch or disk). 1298 note Slot 1: arows calls it inside its own scan (slot 0). 1299 fn lcount mine bool to ulong 1300 n = ulong 0 1301 lopen 1 1302 while lnext 1 bound `9223372036854775807` 1303 e = get lbl 0 1304 if lpub 0 e 1305 if mine 1306 if lowned 0 e 1307 if lisdid 0 e 1308 n = add n 1 1309 end 1310 end 1311 else 1312 n = add n 1 1313 end 1314 end 1315 end 1316 if not lok 1317 ret 0 1318 end 1319 ret n 1320 end 1321 note ---- writing: emb appends to ob. While a stream is open (wbeg .. wend) a full ob is written 1322 note out first (oflush), so the list is written whole at any size. Outside a stream ob holds one 1323 note small file (a snapshot, a status, the alarms) and a byte past its 524,288 is dropped. 1324 fn emb c uchar to void 1325 o = get obl 0 1326 if ge o 524288 1327 if gt get osf 0 0 1328 if oflush 1329 o = ulong 0 1330 end 1331 end 1332 end 1333 if lt o 524288 1334 put ob o c 1335 put obl 0 add o 1 1336 end 1337 end 1338 note oflush: ob to the open stream, then ob is empty; false (and oerr) when a write fails 1339 fn oflush to bool 1340 f = get osf 0 1341 if or eq f 0 ne get oerr 0 0 1342 ret false 1343 end 1344 z = get obl 0 1345 o = ulong 0 1346 while lt o z bound 524288 1347 k = write int sub f 1 ob o sub z o 1348 if le k 0 1349 put oerr 0 1 1350 ret false 1351 end 1352 o = add o ulong k 1353 end 1354 put obl 0 0 1355 ret true 1356 end 1357 note wall: b[0, n) to the open stream, after what ob holds 1358 fn wall b view uchar * n ulong to bool 1359 if not oflush 1360 ret false 1361 end 1362 f = get osf 0 1363 o = ulong 0 1364 while lt o n bound `9223372036854775807` 1365 k = write int sub f 1 b o sub n o 1366 if le k 0 1367 put oerr 0 1 1368 ret false 1369 end 1370 o = add o ulong k 1371 end 1372 ret true 1373 end 1374 fn emt t view uchar * to void 1375 for i ulong len t bound 256 1376 emb get t i 1377 end 1378 end 1379 fn emn v ulong to void 1380 d = new uchar 20 0 1381 n = ulong 0 1382 x = v 1383 if eq x 0 1384 emb 48 1385 end 1386 while gt x 0 bound 20 1387 put d n uchar add 48 rem x 10 1388 x = div x 10 1389 n = add n 1 1390 end 1391 while gt n 0 bound 20 1392 n = sub n 1 1393 emb get d n 1394 end 1395 end 1396 note keep: every row but the current rid's, in order, into the open stream. No row is dropped and 1397 note none is cut: a row not all in lb is copied whole from the file (kcopy). There is no budget and 1398 note no count: the list holds every refusal and withdrawal; it is bounded by the free space of the 1399 note work dir's filesystem (a write that fails replaces nothing) and measured by the list_bytes 1400 note alarm. False when the list could not be read to its end. 1401 fn keep to bool 1402 put lerr 0 0 1403 lopen 0 1404 while lnext 0 bound `9223372036854775807` 1405 e = get lbl 0 1406 n = get lst 4 1407 if and gt n 0 not lisrid 0 e 1408 if ne n e 1409 if not kcopy 0 1410 lclose 0 1411 ret false 1412 end 1413 else 1414 for i ulong e bound 65536 1415 emb get lb i 1416 end 1417 end 1418 emb 10 1419 end 1420 end 1421 ret eq get lerr 0 0 1422 end 1423 note kcopy: the current row of slot sl, read again from the file whole, into the stream 1424 fn kcopy sl ulong to bool 1425 o = mul sl 8 1426 fd = int sub get lst o 1 1427 pos = get lst add o 5 1428 left = get lst add o 4 1429 b = new uchar 65536 0 1430 while gt left 0 bound `9223372036854775807` 1431 w = left 1432 if gt w 65536 1433 w = ulong 65536 1434 end 1435 k = pread fd b 0 w long pos 1436 if eq k -4 1437 continue 1438 end 1439 if le k 0 1440 put lerr 0 1 1441 ret false 1442 end 1443 if not wall b ulong k 1444 ret false 1445 end 1446 pos = add pos ulong k 1447 left = sub left ulong k 1448 end 1449 ret true 1450 end 1451 note lcopy: the whole file of slot sl from offset 0 into the stream (the published copy) 1452 fn lcopy sl ulong to bool 1453 f = get lst mul sl 8 1454 if eq f 0 1455 ret false 1456 end 1457 b = new uchar 65536 0 1458 pos = long 0 1459 while true bound `9223372036854775807` 1460 k = pread int sub f 1 b 0 65536 pos 1461 if eq k -4 1462 continue 1463 end 1464 if lt k 0 1465 put oerr 0 1 1466 ret false 1467 end 1468 if eq k 0 1469 ret true 1470 end 1471 if not wall b ulong k 1472 ret false 1473 end 1474 pos = add pos k 1475 end 1476 ret false 1477 end 1478 note rowhead: rid, state, did, date for the current request 1479 fn rowhead state view uchar * to void 1480 for i ulong get ridl 0 bound 44 1481 emb get rid i 1482 end 1483 emb 9 1484 emt state 1485 emb 9 1486 for i ulong get didl 0 bound 96 1487 emb get did i 1488 end 1489 emb 9 1490 for i ulong get dtl 0 bound 10 1491 emb get dt i 1492 end 1493 emb 9 1494 end 1495 note rowtail: kilobytes, name, description, note 1496 fn rowtail to void 1497 emn get kb 0 1498 emb 9 1499 for i ulong get nml 0 bound 120 1500 emb get nm i 1501 end 1502 emb 9 1503 for i ulong get dsl 0 bound 400 1504 emb get ds i 1505 end 1506 emb 9 1507 for i ulong get ntl 0 bound 200 1508 emb get nt i 1509 end 1510 emb 10 1511 end 1512 note wbeg: open pth + ".tmp" (O_EXCL|O_NOFOLLOW, after removing an old one) as the stream; ob 1513 note keeps what it holds. wend writes the rest, syncs and renames the temp file onto pth; when any 1514 note write or the sync failed it removes the temp file and the old file stays as it was. Between 1515 note the two pth and pt2 must not change (nothing there may call wpath for another name or run). 1516 fn wbeg to bool 1517 n = get pthl 0 1518 for i ulong n bound 1100 1519 put pt2 i get pth i 1520 end 1521 for i ulong len kxt 1522 put pt2 add n i get kxt i 1523 end 1524 m = add n len kxt 1525 r = unlink pt2 0 m 1526 fd = fopen pt2 0 m 557249 420 1527 if lt fd 0 1528 ret false 1529 end 1530 put osf 0 add ulong fd 1 1531 put oerr 0 0 1532 ret true 1533 end 1534 note wr: write ob to pth + ".tmp", sync, rename onto pth 1535 fn wr to bool 1536 if not wbeg 1537 ret false 1538 end 1539 ret wend 1540 end 1541 fn wend to bool 1542 f = get osf 0 1543 if eq f 0 1544 ret false 1545 end 1546 ok = oflush 1547 put osf 0 0 1548 r = fsync int sub f 1 1549 x = fclose int sub f 1 1550 n = get pthl 0 1551 m = add n len kxt 1552 if or not ok ne r 0 1553 r = unlink pt2 0 m 1554 ret false 1555 end 1556 put rpk 0 2 1557 put rpk 1 0 1558 put rpk 2 uchar and m 255 1559 put rpk 3 uchar shr m 8 1560 for i ulong m bound 1100 1561 put rpk add 4 i get pt2 i 1562 end 1563 o = add 4 m 1564 put rpk o uchar and n 255 1565 put rpk add o 1 uchar shr n 8 1566 for i ulong n bound 1100 1567 put rpk add add o 2 i get pth i 1568 end 1569 ret eq renames rpk 0 add add o 2 n 0 0 1570 end 1571 note save: the list with the current row in the given state. The private list first: keep streams 1572 note the old list into list.tsv.tmp, the row goes last, then sync and rename (wend). Then the 1573 note published copy: the private file copied whole the same way into the pinned open dir. A list 1574 note that could not be read or written whole is not replaced (stderr says so); the old one stays. 1575 fn save state view uchar * to void 1576 dodate 1577 put obl 0 0 1578 wpath klist 1579 if not wbeg 1580 lnot 1581 ret 1582 end 1583 ok = keep 1584 rowhead state 1585 rowtail 1586 if not ok 1587 put oerr 0 1 1588 end 1589 if not wend 1590 lnot 1591 ret 1592 end 1593 lopen 1 1594 if pinopen 1595 rname klist 1596 put obl 0 0 1597 if wbeg 1598 ok = lcopy 1 1599 ok = wend 1600 end 1601 unpin 1602 end 1603 lclose 1 1604 end 1605 fn lnot to void 1606 w = write 2 klnot 0 len klnot 1607 end 1608 fn blank to void 1609 put kb 0 0 1610 put nml 0 0 1611 put dsl 0 0 1612 end 1613 note allowed: allow lists the keys that may bring a project, one DID a line; a line 1614 note that is only * admits every signed-in key. No file admits nobody. The file is outside the 1615 note folder forged can write. 1616 fn aload to bool 1617 put cbl 0 0 1618 wpath kallow 1619 fd = fopen pth 0 get pthl 0 557056 0 1620 if lt fd 0 1621 ret false 1622 end 1623 n = ulong 0 1624 k = long 1 1625 while gt k 0 bound 65536 1626 k = read int fd cb n sub 65535 n 1627 if gt k 0 1628 n = add n ulong k 1629 end 1630 end 1631 x = fclose int fd 1632 put cbl 0 n 1633 ret true 1634 end 1635 note aline: the allow line cb[a, e) is * or the current did 1636 fn aline a ulong e ulong to bool 1637 m = sub e a 1638 if eq m 1 1639 if eq get cb a 42 1640 ret true 1641 end 1642 end 1643 d = get didl 0 1644 if ne m d 1645 ret false 1646 end 1647 for i ulong d bound 96 1648 if ne get cb add a i get did i 1649 ret false 1650 end 1651 end 1652 ret true 1653 end 1654 fn aeol a ulong to ulong 1655 n = get cbl 0 1656 e = a 1657 while lt e n bound 65536 1658 if eq get cb e 10 1659 ret e 1660 end 1661 e = add e 1 1662 end 1663 ret n 1664 end 1665 fn allowed to bool 1666 if not aload 1667 ret false 1668 end 1669 n = get cbl 0 1670 a = ulong 0 1671 while lt a n bound 4096 1672 e = aeol a 1673 if aline a e 1674 ret true 1675 end 1676 a = add e 1 1677 end 1678 ret false 1679 end 1680 note ---- the operator's remove file: remove 1681 note One line each: a project (rad:z... or z...) or a key (did:key:...), exactly, nothing else on 1682 note the line. A project named there, and every project brought by a key named there, comes off 1683 note the shelf within a pass (sweep) and cannot be brought again while the line stands; a named key 1684 note cannot bring anything. A * line names nothing. This is how the operator takes a listing down 1685 note without the bringer. The file is outside the folder forged can write. No file: nothing removed. 1686 fn dload to bool 1687 put cbl 0 0 1688 wpath kremove 1689 fd = fopen pth 0 get pthl 0 557056 0 1690 if lt fd 0 1691 ret false 1692 end 1693 n = ulong 0 1694 k = long 1 1695 while gt k 0 bound 65536 1696 k = read int fd cb n sub 65535 n 1697 if gt k 0 1698 n = add n ulong k 1699 end 1700 end 1701 x = fclose int fd 1702 put cbl 0 n 1703 ret true 1704 end 1705 note dline: the remove line cb[a, e) is the current did, or the current rid with or without rad: 1706 fn dline a ulong e ulong to bool 1707 m = sub e a 1708 d = get didl 0 1709 if and gt d 0 eq m d 1710 same = true 1711 for i ulong d bound 96 1712 if ne get cb add a i get did i 1713 same = false 1714 end 1715 end 1716 if same 1717 ret true 1718 end 1719 end 1720 b = a 1721 if gt m 4 1722 pre = true 1723 for i ulong 4 1724 if ne get cb add a i get kradp i 1725 pre = false 1726 end 1727 end 1728 if pre 1729 b = add a 4 1730 end 1731 end 1732 r = get ridl 0 1733 if eq r 0 1734 ret false 1735 end 1736 if ne sub e b r 1737 ret false 1738 end 1739 for i ulong r bound 44 1740 if ne get cb add b i get rid i 1741 ret false 1742 end 1743 end 1744 ret true 1745 end 1746 note dscan: the loaded remove file names the current rid or did 1747 fn dscan to bool 1748 n = get cbl 0 1749 a = ulong 0 1750 while lt a n bound 65536 1751 e = aeol a 1752 if dline a e 1753 ret true 1754 end 1755 a = add e 1 1756 end 1757 ret false 1758 end 1759 fn denied to bool 1760 if not dload 1761 ret false 1762 end 1763 ret dscan 1764 end 1765 note ---- the snapshot a project page is drawn from: p/.txt 1766 note forged cannot read a brought repository (its sandbox mounts only the curated ones), so this 1767 note program asks git for a small, fixed view of the project's HEAD and writes it as lines forged 1768 note escapes and shows: H head and date, C the last 10 commits, T the top-level files (at most 1769 note 200), R the README (README.md, else README; at most 32 KiB). Every line is cut at 400 bytes, 1770 note control bytes become spaces and bytes that are not UTF-8 become "?". git runs with a cleared 1771 note environment, no system or user configuration, read-only commands and a 20 s deadline each; 1772 note nothing from the project is executed. A command that fails leaves its part out. 1773 note pkgit: env -i HOME= GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null 1774 note PATH=/usr/bin:/bin /usr/bin/git --git-dir 1775 fn pkgit to void 1776 pknew 1777 pkt kenv 1778 pkt kdashi 1779 sbz 1780 sbt khome 1781 for i ulong get wdl 0 bound 512 1782 sbc get wd i 1783 end 1784 pks 1785 pkt kgn 1786 pkt kgg 1787 pkt kgallow 1788 pkt kgnlazy 1789 pkt kgprompt 1790 pkt kpathg 1791 pkt kgit 1792 pkt kgd 1793 sbz 1794 sbstore 1795 pks 1796 end 1797 note eml: one snapshot line: the kind byte, a tab, then cb[a, b) cut at 400 bytes. A tab stays 1798 note when tabs is true. 1799 fn eml kind uchar a ulong b ulong tabs bool to void 1800 n = sub b a 1801 if gt n 400 1802 n = ulong 400 1803 end 1804 for i ulong n bound 400 1805 c = get cb add a i 1806 if or lt c 32 eq c 127 1807 if not and tabs eq c 9 1808 c = uchar 32 1809 end 1810 end 1811 put ub i c 1812 end 1813 put ubl 0 n 1814 usan 1815 emb kind 1816 emb 9 1817 for i ulong n bound 400 1818 emb get ub i 1819 end 1820 emb 10 1821 end 1822 note snnode: N the node's DID (rad self --did), the hub's key the project page names 1823 fn snnode to void 1824 pkrad 1825 pkt kself 1826 pkt kdidf 1827 if not run 10 1828 ret 1829 end 1830 n = get cbl 0 1831 e = ulong 0 1832 while lt e n bound 65536 1833 if eq get cb e 10 1834 break 1835 end 1836 e = add e 1 1837 end 1838 if gt e 0 1839 eml 78 0 e false 1840 end 1841 end 1842 fn snhead to void 1843 pkgit 1844 pkt kglog 1845 pkt kgm1 1846 pkt kgfh 1847 pkt kghead 1848 if not run 20 1849 ret 1850 end 1851 if eq get cbl 0 0 1852 ret 1853 end 1854 eml 72 0 aeol 0 true 1855 end 1856 fn sncommits to void 1857 pkgit 1858 pkt kglog 1859 pkt kgn10 1860 pkt kgfc 1861 pkt kghead 1862 if not run 20 1863 ret 1864 end 1865 n = get cbl 0 1866 a = ulong 0 1867 k = ulong 0 1868 while lt a n bound 16 1869 if ge k 10 1870 break 1871 end 1872 e = aeol a 1873 if gt e a 1874 eml 67 a e true 1875 end 1876 a = add e 1 1877 k = add k 1 1878 end 1879 end 1880 note sntline: one ls-tree line "mode type sha name" becomes T first letter of the type 1881 note name 1882 fn sntline a ulong e ulong to void 1883 p = a 1884 while lt p e bound 65536 1885 if eq get cb p 32 1886 break 1887 end 1888 p = add p 1 1889 end 1890 q = add p 1 1891 while lt q e bound 65536 1892 if eq get cb q 9 1893 break 1894 end 1895 q = add q 1 1896 end 1897 if ge q e 1898 ret 1899 end 1900 if lt q add p 3 1901 ret 1902 end 1903 put cb sub q 1 get cb add p 1 1904 eml 84 sub q 1 e true 1905 end 1906 fn sntree to void 1907 pkgit 1908 pkt kgls 1909 pkt kghead 1910 if not run 20 1911 ret 1912 end 1913 n = get cbl 0 1914 a = ulong 0 1915 k = ulong 0 1916 while lt a n bound 256 1917 if ge k 200 1918 break 1919 end 1920 e = aeol a 1921 if gt e a 1922 sntline a e 1923 end 1924 a = add e 1 1925 k = add k 1 1926 end 1927 end 1928 note snrun: git show of the README into cb 1929 fn snrun to bool 1930 pkgit 1931 pkt kgshow 1932 pkt kgrd1 1933 if run 20 1934 ret true 1935 end 1936 pkgit 1937 pkt kgshow 1938 pkt kgrd2 1939 ret run 20 1940 end 1941 fn snreadme to void 1942 if not snrun 1943 ret 1944 end 1945 n = get cbl 0 1946 if gt n 32768 1947 n = ulong 32768 1948 end 1949 a = ulong 0 1950 while lt a n bound 32769 1951 e = aeol a 1952 if gt e n 1953 e = n 1954 end 1955 if eq e a 1956 eml 82 a a false 1957 end 1958 s = a 1959 while lt s e bound 256 1960 z = add s 400 1961 if gt z e 1962 z = e 1963 end 1964 eml 82 s z false 1965 s = z 1966 end 1967 a = add e 1 1968 end 1969 end 1970 note snpath: pin p/ (mk makes it first); pth = .txt in it. Callers unpin. 1971 fn snpath mk bool to bool 1972 if not pinsub kpdir mk 1973 ret false 1974 end 1975 for i ulong get ridl 0 bound 44 1976 put pth i get rid i 1977 end 1978 n = get ridl 0 1979 for i ulong len ktxt bound 8 1980 put pth add n i get ktxt i 1981 end 1982 put pthl 0 add n len ktxt 1983 ret true 1984 end 1985 fn snap to void 1986 archive 1987 put obl 0 0 1988 snnode 1989 snhead 1990 sncommits 1991 sntree 1992 snreadme 1993 if snpath true 1994 ok = wr 1995 unpin 1996 end 1997 end 1998 note ---- one request 1999 note rsave: save a refusal that is not about a fetch, unless the row says this program's own fetch 2000 note of the rid failed (fl): that row is what lets the next bring clear a late folder, so a key 2001 note that is not invited, a full quota or a node that does not answer must not replace it. 2002 fn rsave t view uchar * fl bool to void 2003 if not fl 2004 save t 2005 end 2006 end 2007 note describe: name and description from the project's identity document 2008 fn describe to void 2009 if dopayload 2010 put nml 0 jget kname 0 120 2011 put dsl 0 jget kdesc 1 400 2012 usnm 2013 usds 2014 end 2015 if eq get nml 0 0 2016 for i ulong len kunnamed bound 32 2017 put nm i get kunnamed i 2018 end 2019 put nml 0 len kunnamed 2020 end 2021 end 2022 fn bring to void 2023 blank 2024 if lfind 2025 e = get lbl 0 2026 if lpub 0 e 2027 note a second bring by the key that listed the project refreshes its page snapshot (a row the 2028 note remove file names is left as it is; sweep takes it down) 2029 if lisdid 0 e 2030 if not denied 2031 snap 2032 end 2033 end 2034 ret 2035 end 2036 end 2037 fl = lfailed 2038 if ne get lfail 0 0 2039 ret 2040 end 2041 if denied 2042 rsave kr8 fl 2043 ret 2044 end 2045 if not allowed 2046 rsave kr0 fl 2047 ret 2048 end 2049 left = false 2050 held = false 2051 if stored 2052 left = fl 2053 if not left 2054 held = true 2055 end 2056 end 2057 if held 2058 if not dovis 2059 save kr1 2060 ret 2061 end 2062 note held needs the node's own seeding policy for the rid: a folder this program's failed fetch 2063 note left (unseeded again) is not the node's project, even after its refusal row was replaced 2064 hp = policy 2065 if eq hp 2 2066 save kr6 2067 ret 2068 end 2069 if eq hp 0 2070 save kr1 2071 ret 2072 end 2073 dg = dodel 2074 if eq dg 2 2075 save kr6 2076 ret 2077 end 2078 if eq dg 0 2079 save kr9 2080 ret 2081 end 2082 ok = dodu 2083 describe 2084 save kheld 2085 snap 2086 ret 2087 end 2088 p = policy 2089 if eq p 1 2090 save kr7 2091 ret 2092 end 2093 if eq p 2 2094 rsave kr6 fl 2095 ret 2096 end 2097 if not rsvok 0 0 2098 rsave kfull fl 2099 ret 2100 end 2101 if left 2102 dorm 2103 end 2104 ok = doseed 2105 if ok 2106 ok = stored 2107 end 2108 if not ok 2109 dounseed 2110 if stored 2111 dorm 2112 end 2113 save kr4 2114 ret 2115 end 2116 ok = dodu 2117 if not ok 2118 dounseed 2119 dorm 2120 save kr4 2121 ret 2122 end 2123 note no size refusal: largest_project_kib is an alarm. The fetch is refused only when it took the 2124 note storage filesystem below its reserve; then it is unseeded and removed (a failed fetch). 2125 note What one fetch can write is bounded by radicle-node itself (limits.fetchPackReceive, 500 MiB 2126 note on the Spark, fetchConcurrency 1): it limits ONE fetch, not the total repository size. A 2127 note repository seeded with scope all keeps fetching from peers after it is listed; the intake 2128 note does not check its reserve for those later fetches. Only storage_free_bytes alarms on that 2129 note growth. The initial-fetch check is not a lifetime storage guarantee (docs/OPEN-BUILDS.md). 2130 if not rsvok 0 0 2131 put avol 10 mul get kb 0 1024 2132 dounseed 2133 dorm 2134 save kfull 2135 ret 2136 end 2137 describe 2138 save kopen 2139 snap 2140 end 2141 note withdraw: only an open or held row brought by this key. Its name is kept; size, description 2142 note and note are cleared. 2143 fn withdraw to void 2144 if not lfind 2145 ret 2146 end 2147 s = ulong 0 2148 e = get lbl 0 2149 held = lstate s e kheld 2150 if not held 2151 if not lowned s e 2152 ret 2153 end 2154 end 2155 if not lisdid s e 2156 ret 2157 end 2158 blank 2159 a = lfld s e 5 2160 b = lfe a e 2161 n = sub b a 2162 if gt n 120 2163 n = ulong 120 2164 end 2165 for i ulong n bound 120 2166 put nm i get lb add a i 2167 end 2168 put nml 0 n 2169 put ntl 0 0 2170 if not held 2171 dounseed 2172 dorm 2173 end 2174 if snpath false 2175 r = unlink pth 0 get pthl 0 2176 unpin 2177 end 2178 adel 2179 save kwith 2180 end 2181 note setrow: the rid and the did of the row s..e become the current ones 2182 fn setrow s ulong e ulong to void 2183 b = lfe s e 2184 n = sub b s 2185 if gt n 44 2186 n = ulong 44 2187 end 2188 for i ulong n bound 44 2189 put rid i get lb add s i 2190 end 2191 put ridl 0 n 2192 a = lfld s e 2 2193 c = lfe a e 2194 m = sub c a 2195 note 96 is the did cap of the queue (qfield) and of forged (ohdid): a shorter cap would cut a 2196 note 96-byte did, so a remove line naming it would miss and a line naming its prefix would match 2197 if gt m 96 2198 m = ulong 96 2199 end 2200 for i ulong m bound 96 2201 put did i get lb add a i 2202 end 2203 put didl 0 m 2204 end 2205 note takedown: the operator's removal of the row s..e (on the shelf; its rid and did are current). 2206 note As a withdraw, without the bringer: a fetched copy is unseeded and deleted, a held project is 2207 note left as the node had it; the snapshot goes; the row stays, off the shelf, under the bringer's 2208 note key, so that key sees why. 2209 fn takedown s ulong e ulong to void 2210 fetched = lowned s e 2211 blank 2212 a = lfld s e 5 2213 b = lfe a e 2214 n = sub b a 2215 if gt n 120 2216 n = ulong 120 2217 end 2218 for i ulong n bound 120 2219 put nm i get lb add a i 2220 end 2221 put nml 0 n 2222 put ntl 0 0 2223 if fetched 2224 dounseed 2225 dorm 2226 end 2227 if snpath false 2228 u = unlink pth 0 get pthl 0 2229 unpin 2230 end 2231 adel 2232 save kr8 2233 end 2234 note sweep: take down one row on the shelf that the remove file names (by project or by the key 2235 note that brought it); true when it did. One row a pass keeps the list it scans in step with the 2236 note list it writes. 2237 fn sweep to bool 2238 if not dload 2239 ret false 2240 end 2241 if not lvalid 2242 ret false 2243 end 2244 lopen 0 2245 while lnext 0 bound `9223372036854775807` 2246 e = get lbl 0 2247 if lpub 0 e 2248 setrow 0 e 2249 if dscan 2250 lclose 0 2251 takedown 0 e 2252 ret true 2253 end 2254 end 2255 end 2256 ok = lok 2257 ret false 2258 end 2259 note ---- the queue 2260 fn getoff to ulong 2261 b = new uchar 24 0 2262 fd = fopen sf 0 get sfl 0 524288 0 2263 if lt fd 0 2264 ret 0 2265 end 2266 k = read int fd b 0 20 2267 x = fclose int fd 2268 v = ulong 0 2269 if gt k 0 2270 for i ulong ulong k bound 20 2271 c = get b i 2272 if and ge c 48 le c 57 2273 v = add mul v 10 ulong sub c 48 2274 end 2275 end 2276 end 2277 ret v 2278 end 2279 fn putoff v ulong to void 2280 put obl 0 0 2281 emn v 2282 emb 10 2283 n = get sfl 0 2284 for i ulong n bound 512 2285 put pth i get sf i 2286 end 2287 put pthl 0 n 2288 ok = wr 2289 end 2290 note field: copy qb[a, e) up to the next tab into rid (which 0), did (1) or the note (2); the 2291 note index after the tab, or e. A field that breaks its rule sets its length to 0. 2292 fn fput which ulong o ulong c uchar to void 2293 if eq which 0 2294 put rid o c 2295 end 2296 if eq which 1 2297 put did o c 2298 end 2299 if eq which 2 2300 put nt o c 2301 end 2302 end 2303 fn qfield a ulong e ulong which ulong cap ulong to ulong 2304 o = ulong 0 2305 bad = false 2306 p = a 2307 while lt p e bound 65536 2308 c = get qb p 2309 if eq c 9 2310 break 2311 end 2312 if eq which 0 2313 if not b58 c 2314 bad = true 2315 end 2316 end 2317 if eq which 1 2318 if or le c 32 ge c 127 2319 bad = true 2320 end 2321 end 2322 if eq which 2 2323 if or lt c 32 eq c 127 2324 c = uchar 32 2325 end 2326 end 2327 if ge o cap 2328 bad = true 2329 else 2330 fput which o c 2331 o = add o 1 2332 end 2333 p = add p 1 2334 end 2335 if bad 2336 o = ulong 0 2337 end 2338 if eq which 0 2339 put ridl 0 o 2340 end 2341 if eq which 1 2342 put didl 0 o 2343 end 2344 if eq which 2 2345 put ntl 0 o 2346 end 2347 if lt p e 2348 ret add p 1 2349 end 2350 ret e 2351 end 2352 note qline: qb[a, e) is one queue line. True when it was well formed and was acted on. 2353 fn qline a ulong e ulong to bool 2354 if uline a e 2355 ret true 2356 end 2357 if lt sub e a 4 2358 ret false 2359 end 2360 kind = get qb a 2361 if ne get qb add a 1 9 2362 ret false 2363 end 2364 p = qfield add a 2 e 0 44 2365 p = qfield p e 1 96 2366 put ntl 0 0 2367 if eq kind 66 2368 p = qfield p e 2 200 2369 usnt 2370 end 2371 if or lt get ridl 0 20 eq get didl 0 0 2372 ret false 2373 end 2374 if ne get rid 0 122 2375 ret false 2376 end 2377 if eq kind 66 2378 bring 2379 ret true 2380 end 2381 if eq kind 87 2382 withdraw 2383 ret true 2384 end 2385 ret false 2386 end 2387 note qeol: the index of the line feed at or after a in qb[0, m), or m 2388 fn qeol a ulong m ulong to ulong 2389 e = a 2390 while lt e m bound 65536 2391 if eq get qb e 10 2392 ret e 2393 end 2394 e = add e 1 2395 end 2396 ret m 2397 end 2398 note qchunk: the complete lines of one 64 KiB chunk of the queue file at pth, from the stored 2399 note offset. The offset is saved after each line that was acted on and once at the end of the 2400 note chunk, not after every ignored line (each save is a write, a sync and a rename). qe is 1 when the file opened and nothing was left to read. 2401 fn qchunk name view uchar * to bool 2402 put qe 0 0 2403 if not pinopen 2404 ret false 2405 end 2406 rname name 2407 fd = ropen 2408 unpin 2409 if lt fd 0 2410 ret false 2411 end 2412 off = getoff 2413 k = pread int fd qb 0 65536 long off 2414 x = fclose int fd 2415 if le k 0 2416 put qe 0 1 2417 ret false 2418 end 2419 m = ulong k 2420 a = ulong 0 2421 did1 = false 2422 dirty = false 2423 while lt a m bound 65536 2424 e = qeol a m 2425 if ge e m 2426 if and eq a 0 eq m 65536 2427 a = m 2428 dirty = true 2429 did1 = true 2430 end 2431 break 2432 end 2433 put lfail 0 0 2434 acted = qline a e 2435 if ne get lfail 0 0 2436 if dirty 2437 putoff add off a 2438 end 2439 ret false 2440 end 2441 did1 = true 2442 a = add e 1 2443 if acted 2444 putoff add off a 2445 dirty = false 2446 else 2447 dirty = true 2448 end 2449 end 2450 if dirty 2451 putoff add off a 2452 end 2453 ret did1 2454 end 2455 note qrotate: rename queue to queue.old. forged creates a new queue at its next request, 2456 note so the queue never stays at forged's 1 MiB cap once this program has read it. 2457 fn qrotate to bool 2458 if not pinopen 2459 ret false 2460 end 2461 rname kqold 2462 m = get pthl 0 2463 for i ulong m bound 1100 2464 put pt2 i get pth i 2465 end 2466 rname kq 2467 n = get pthl 0 2468 put rpk 0 2 2469 put rpk 1 0 2470 put rpk 2 uchar and n 255 2471 put rpk 3 uchar shr n 8 2472 for i ulong n bound 1100 2473 put rpk add 4 i get pth i 2474 end 2475 o = add 4 n 2476 put rpk o uchar and m 255 2477 put rpk add o 1 uchar shr m 8 2478 for i ulong m bound 1100 2479 put rpk add add o 2 i get pt2 i 2480 end 2481 r = renames rpk 0 add add o 2 m 0 2482 unpin 2483 ret eq r 0 2484 end 2485 note qold: finish a rotated queue. The stored offset is one into queue.old while it exists and is 2486 note above 0 (a rotation happens only past 262,144 bytes); 0 means it was finished before a 2487 note restart, so it is only removed. Any line forged appended during the rename is read here. 2488 fn qold to bool 2489 if not pinopen 2490 ret false 2491 end 2492 rname kqold 2493 there = eq faccess pth 0 get pthl 0 0 0 2494 unpin 2495 if not there 2496 ret false 2497 end 2498 if gt getoff 0 2499 for i ulong 4096 2500 if not qchunk kqold 2501 break 2502 end 2503 end 2504 end 2505 putoff 0 2506 if not pinopen 2507 ret false 2508 end 2509 rname kqold 2510 r = unlink pth 0 get pthl 0 2511 unpin 2512 ret eq r 0 2513 end 2514 note qpass: one pass. A queue read to its end past 262,144 bytes is rotated; the second of sleep 2515 note lets a request forged opened before the rename finish its write before queue.old is read. 2516 fn qpass to bool 2517 if qold 2518 ret true 2519 end 2520 if qchunk kq 2521 ret true 2522 end 2523 if eq get qe 0 0 2524 ret false 2525 end 2526 if lt getoff 262144 2527 ret false 2528 end 2529 if not qrotate 2530 ret false 2531 end 2532 z = sleepns `1000000000` 2533 ret true 2534 end 2535 note argp: argument i into its buffer; its length 2536 fn aput i ulong k ulong c uchar to void 2537 if eq i 1 2538 put od k c 2539 end 2540 if eq i 2 2541 put sf k c 2542 end 2543 if eq i 3 2544 put wd k c 2545 end 2546 if eq i 4 2547 put sd k c 2548 end 2549 if eq i 5 2550 put hd k c 2551 end 2552 end 2553 fn argp i ulong cap ulong to ulong 2554 b = new uchar 512 0 2555 n = argread i b 0 cap 2556 if le n 0 2557 ret 0 2558 end 2559 m = ulong n 2560 for k ulong m bound 512 2561 aput i k get b k 2562 end 2563 ret m 2564 end 2565 fn slashed i ulong to bool 2566 if eq i 1 2567 ret eq get od sub get odl 0 1 47 2568 end 2569 if eq i 3 2570 ret eq get wd sub get wdl 0 1 47 2571 end 2572 ret eq get sd sub get sdl 0 1 47 2573 end 2574 fn main to int 2575 if lt argcnt 6 2576 ret 2 2577 end 2578 put odl 0 argp 1 500 2579 put sfl 0 argp 2 500 2580 put wdl 0 argp 3 500 2581 put sdl 0 argp 4 500 2582 put hdl 0 argp 5 250 2583 if eq get odl 0 0 2584 ret 2 2585 end 2586 if eq get sfl 0 0 2587 ret 2 2588 end 2589 if eq get wdl 0 0 2590 ret 2 2591 end 2592 if eq get sdl 0 0 2593 ret 2 2594 end 2595 if eq get hdl 0 0 2596 ret 2 2597 end 2598 if not slashed 1 2599 ret 2 2600 end 2601 if not slashed 3 2602 ret 2 2603 end 2604 if not slashed 4 2605 ret 2 2606 end 2607 once = gt argcnt 6 2608 note the open dir may not exist yet (forged makes it at its first write, and takes files only once 2609 note this program has written its space file there): make it, never through a link (mkdir does not 2610 note follow one; pinopen refuses one) 2611 r = makedir od 0 sub get odl 0 1 448 2612 allarchives 2613 alarms 2614 swept = ulong 0 2615 while true bound `9223372036854775807` 2616 put aqmax 0 amax get aqmax 0 aqsize 2617 did1 = qpass 2618 did2 = sweep 2619 put apass 0 add get apass 0 1 2620 if or or once or did1 did2 ge get apass 0 60 2621 alarms 2622 put apass 0 0 2623 end 2624 if once 2625 note a removal that cannot be written would be tried for ever: give up after 512 2626 if did2 2627 swept = add swept 1 2628 end 2629 if gt swept 512 2630 ret 1 2631 end 2632 if not or did1 did2 2633 ret 0 2634 end 2635 else 2636 if not did1 2637 z = sleepns `1000000000` 2638 end 2639 end 2640 end 2641 ret 0 2642 end 2643 note Upload implementation. Buffers are sized by the unchanged public request/project/path limits. 2644 fn lowned s ulong e ulong to bool 2645 if lisopen s e 2646 ret true 2647 end 2648 ret lstate s e kustored 2649 end 2650 fn uhex c uchar to bool 2651 ret or and ge c 48 le c 57 and ge c 97 le c 102 2652 end 2653 fn uhash data view uchar * dest edit uchar * to void 2654 ctx = new ulong 40 0 2655 h = new uchar 32 0 2656 sha256_init ctx 2657 sha256_update ctx data 2658 sha256_final ctx h 2659 for i ulong 32 2660 c = get h i 2661 put dest mul i 2 get kuhex ulong shr c 4 2662 put dest add mul i 2 1 get kuhex ulong and c 15 2663 end 2664 end 2665 fn udidh to void 2666 b = new uchar 96 0 2667 h = new uchar 64 0 2668 n = get didl 0 2669 for i ulong n bound 96 2670 put b i get did i 2671 end 2672 uhash part b 0 n h 2673 for i ulong 16 2674 put udh i get h i 2675 end 2676 end 2677 note upath: pin up/; pth = . then ext, in it. Callers unpin. 2678 fn upath ext view uchar * to bool 2679 if not pinsub kuup false 2680 ret false 2681 end 2682 for i ulong 64 2683 put pth i get usha i 2684 end 2685 put pth 64 46 2686 for i ulong 16 2687 put pth add 65 i get udh i 2688 end 2689 for i ulong len ext bound 16 2690 put pth add 81 i get ext i 2691 end 2692 put pthl 0 add 81 len ext 2693 ret true 2694 end 2695 fn ustatus state view uchar * good bool to void 2696 put obl 0 0 2697 emt kusj1 2698 emt state 2699 if good 2700 emt kusj3 2701 for i ulong get ridl 0 bound 44 2702 emb get rid i 2703 end 2704 emt kusj4 2705 for i ulong get ridl 0 bound 44 2706 emb get rid i 2707 end 2708 emt kusj5 2709 else 2710 emt kusj2 2711 end 2712 if upath kujson 2713 ok = wr 2714 unpin 2715 end 2716 end 2717 note uscratch: remove the scratch files and folders of an upload in the work dir 2718 fn uscratch to void 2719 wpath kuarc 2720 r = unlink pth 0 get pthl 0 2721 wpath kuinput 2722 r = unlink pth 0 get pthl 0 2723 wpath kusign 2724 r = unlink pth 0 get pthl 0 2725 wpath kubsigw 2726 r = unlink pth 0 get pthl 0 2727 pknew 2728 pkt krm 2729 pkt krf 2730 pkt kdd 2731 sbz 2732 for i ulong get wdl 0 bound 512 2733 sbc get wd i 2734 end 2735 sbt kutree 2736 pks 2737 sbz 2738 for i ulong get wdl 0 bound 512 2739 sbc get wd i 2740 end 2741 sbt kugit 2742 pks 2743 ok = run 60 2744 end 2745 fn uclean to void 2746 if upath kub64 2747 r = unlink pth 0 get pthl 0 2748 unpin 2749 end 2750 if upath kusig 2751 r = unlink pth 0 get pthl 0 2752 unpin 2753 end 2754 uscratch 2755 end 2756 note ugetsig: the spool's signature (read from the pinned up/, never through a link) into usigbuf and 2757 note a private copy bring.sig, which ssh-keygen reads 2758 fn ugetsig to bool 2759 if not upath kusig 2760 ret false 2761 end 2762 fd = ropen 2763 unpin 2764 if lt fd 0 2765 ret false 2766 end 2767 k = read int fd usigbuf 0 65536 2768 x = fclose int fd 2769 if or le k 0 ge k 65536 2770 ret false 2771 end 2772 put usiglen 0 ulong k 2773 put obl 0 0 2774 for i ulong ulong k bound 65536 2775 emb get usigbuf i 2776 end 2777 wpath kubsigw 2778 ret wr 2779 end 2780 fn uref t view uchar * to void 2781 ustatus t false 2782 uclean 2783 end 2784 note uqfield copies one of the seven U queue fields, with no truncation or unchecked arithmetic. 2785 fn unamec c uchar to bool 2786 if eq c 45 2787 ret true 2788 end 2789 if and ge c 97 le c 122 2790 ret true 2791 end 2792 ret and ge c 48 le c 57 2793 end 2794 fn uqfield a ulong e ulong which ulong to ulong 2795 p = a 2796 n = ulong 0 2797 v = ulong 0 2798 cap = ulong 0 2799 if eq which 0 2800 cap = ulong 64 2801 end 2802 if eq which 1 2803 cap = ulong 96 2804 end 2805 if or eq which 2 eq which 3 2806 cap = ulong 18 2807 end 2808 if eq which 4 2809 cap = ulong 64 2810 end 2811 if eq which 5 2812 cap = ulong 200 2813 end 2814 while lt p e bound 65536 2815 c = get qb p 2816 if eq c 9 2817 break 2818 end 2819 if ge n cap 2820 put uerr 0 1 2821 else 2822 if eq which 0 2823 if not uhex c 2824 put uerr 0 1 2825 end 2826 put usha n c 2827 end 2828 if eq which 1 2829 if or le c 32 ge c 127 2830 put uerr 0 1 2831 end 2832 put did n c 2833 end 2834 if or eq which 2 eq which 3 2835 if or lt c 48 gt c 57 2836 put uerr 0 1 2837 else 2838 v = add mul v 10 ulong sub c 48 2839 end 2840 end 2841 if eq which 4 2842 if not unamec c 2843 put uerr 0 1 2844 end 2845 put nm n c 2846 end 2847 if eq which 5 2848 if or lt c 32 eq c 127 2849 put uerr 0 1 2850 end 2851 put nt n c 2852 end 2853 end 2854 n = add n 1 2855 p = add p 1 2856 end 2857 if eq which 0 2858 if ne n 64 2859 put uerr 0 1 2860 end 2861 end 2862 if eq which 1 2863 put didl 0 n 2864 end 2865 if or eq which 2 eq which 3 2866 if eq n 0 2867 put uerr 0 1 2868 end 2869 put uvals sub which 2 v 2870 end 2871 if eq which 4 2872 if eq n 0 2873 put uerr 0 1 2874 end 2875 put nml 0 n 2876 end 2877 if eq which 5 2878 put ntl 0 n 2879 end 2880 if lt p e 2881 ret add p 1 2882 end 2883 ret p 2884 end 2885 note unamerule: the name keeps forged's rule (ohname): no '-' first, last or twice in a row; the 2886 note characters and the length were checked by uqfield 2887 fn unamerule to bool 2888 n = get nml 0 2889 if or eq n 0 gt n 64 2890 ret false 2891 end 2892 if or eq get nm 0 45 eq get nm sub n 1 45 2893 ret false 2894 end 2895 for i ulong sub n 1 bound 64 2896 if and eq get nm i 45 eq get nm add i 1 45 2897 ret false 2898 end 2899 end 2900 ret true 2901 end 2902 fn uline a ulong e ulong to bool 2903 if lt sub e a 4 2904 ret false 2905 end 2906 if ne get qb a 85 2907 ret false 2908 end 2909 if ne get qb add a 1 9 2910 ret false 2911 end 2912 put uerr 0 0 2913 p = add a 2 2914 for i ulong 6 2915 p = uqfield p e i 2916 end 2917 if ne p e 2918 put uerr 0 1 2919 end 2920 if eq get didl 0 0 2921 put uerr 0 1 2922 end 2923 if ne get uerr 0 0 2924 ret false 2925 end 2926 if not unamerule 2927 ret false 2928 end 2929 udidh 2930 upload 2931 ret true 2932 end 2933 fn ub64v c uchar to long 2934 if and ge c 65 le c 90 2935 ret long sub c 65 2936 end 2937 if and ge c 97 le c 122 2938 ret add long sub c 97 26 2939 end 2940 if and ge c 48 le c 57 2941 ret add long sub c 48 52 2942 end 2943 if eq c 45 2944 ret 62 2945 end 2946 if eq c 95 2947 ret 63 2948 end 2949 ret -1 2950 end 2951 note Decode a single spool record in local b, including its seq/total trailer. No body is executed. 2952 fn udecode b view uchar * n ulong dest int seq ulong total ulong to long 2953 if lt n 45 2954 ret -1 2955 end 2956 m = sub n 43 2957 if or ne get b m 9 ne get b add m 21 9 2958 ret -1 2959 end 2960 if ne get b sub n 1 10 2961 ret -1 2962 end 2963 r4 = rem m 4 2964 if eq r4 1 2965 ret -1 2966 end 2967 if ne urecnum b add m 1 long seq 2968 ret -1 2969 end 2970 k = mul div m 4 3 2971 if gt r4 0 2972 k = add k sub r4 1 2973 end 2974 if ne urecnum b add m 22 long add total k 2975 ret -1 2976 end 2977 if gt add total k get uvals 1 2978 ret -1 2979 end 2980 ret udecbody b m dest 2981 end 2982 note udecbody: canonical unpadded base64url b[0, m) into dest; unused low bits must be zero 2983 fn udecbody b view uchar * m ulong dest int to long 2984 acc = ulong 0 2985 bits = ulong 0 2986 k = ulong 0 2987 for i ulong m bound 60000 2988 v = ub64v get b i 2989 if lt v 0 2990 ret -1 2991 end 2992 acc = and or shl acc 6 ulong v 4095 2993 bits = add bits 6 2994 if ge bits 8 2995 bits = sub bits 8 2996 put ubuf k uchar and shr acc bits 255 2997 k = add k 1 2998 end 2999 end 3000 if gt bits 0 3001 if ne and acc sub shl ulong 1 bits 1 0 3002 ret -1 3003 end 3004 end 3005 w = write dest ubuf 0 k 3006 if ne w long k 3007 ret -1 3008 end 3009 ret long k 3010 end 3011 note urecnum: the 20-digit number at b[a] (a spool record's seq or total); -1 when it is not one 3012 note or is past `922337203685477579` (so the long never overflows) 3013 fn urecnum b view uchar * a ulong to long 3014 v = long 0 3015 for i ulong 20 3016 c = get b add a i 3017 if or lt c 48 gt c 57 3018 ret -1 3019 end 3020 if gt v `922337203685477579` 3021 ret -1 3022 end 3023 v = add mul v 10 long sub c 48 3024 end 3025 ret v 3026 end 3027 fn udecodeall to bool 3028 if not upath kub64 3029 ret false 3030 end 3031 fd = ropen 3032 unpin 3033 if lt fd 0 3034 ret false 3035 end 3036 wpath kuarc 3037 dest = fopen pth 0 get pthl 0 557249 384 3038 if lt dest 0 3039 x = fclose int fd 3040 ret false 3041 end 3042 ok = udecodeloop int fd int dest 3043 x = fclose int fd 3044 x = fclose int dest 3045 ret ok 3046 end 3047 fn udecodeloop fd int dest int to bool 3048 b = new uchar 60048 0 3049 pos = long 0 3050 seq = ulong 0 3051 total = ulong 0 3052 while true bound `9223372036854775807` 3053 k = pread fd b 0 60044 pos 3054 if lt k 0 3055 ret false 3056 end 3057 if eq k 0 3058 put uvals 2 total 3059 ret true 3060 end 3061 n = ulong 0 3062 for i ulong ulong k bound 60044 3063 if eq get b i 10 3064 n = add i 1 3065 break 3066 end 3067 end 3068 if eq n 0 3069 ret false 3070 end 3071 v = udecode b n dest seq total 3072 if lt v 0 3073 ret false 3074 end 3075 total = add total ulong v 3076 seq = add seq 1 3077 pos = add pos long n 3078 end 3079 ret false 3080 end 3081 fn ufilehash to bool 3082 b = new uchar 4096 0 3083 ctx = new ulong 40 0 3084 h = new uchar 32 0 3085 fd = fopen pth 0 get pthl 0 557056 0 3086 if lt fd 0 3087 ret false 3088 end 3089 sha256_init ctx 3090 ok = true 3091 while true bound `9223372036854775807` 3092 k = read int fd b 0 4096 3093 if eq k 0 3094 break 3095 end 3096 if lt k 0 3097 ok = false 3098 break 3099 end 3100 sha256_update ctx part b 0 ulong k 3101 end 3102 x = fclose int fd 3103 sha256_final ctx h 3104 for i ulong 32 3105 c = get h i 3106 put udigest mul i 2 get kuhex ulong shr c 4 3107 put udigest add mul i 2 1 get kuhex ulong and c 15 3108 end 3109 ret ok 3110 end 3111 fn ustatement to void 3112 put obl 0 0 3113 emt kustmt 3114 for i ulong 64 3115 emb get usha i 3116 end 3117 emt kufiles 3118 emn get uvals 0 3119 emt kubytes 3120 emn get uvals 1 3121 emt kuname 3122 for i ulong get nml 0 bound 64 3123 emb get nm i 3124 end 3125 emb 10 3126 end 3127 fn upubkey to bool 3128 raw = new uchar 34 0 3129 blob = new uchar 51 0 3130 if lt get didl 0 10 3131 ret false 3132 end 3133 textp = new uchar 9 0 3134 put textp 0 100 3135 put textp 1 105 3136 put textp 2 100 3137 put textp 3 58 3138 put textp 4 107 3139 put textp 5 101 3140 put textp 6 121 3141 put textp 7 58 3142 put textp 8 122 3143 for i ulong 9 3144 if ne get did i get textp i 3145 ret false 3146 end 3147 end 3148 note canonical base58 only, as forged's sob58: a leading '1' (a zero byte) would give the same key 3149 note another DID string, so another quota and another remove line 3150 if eq get did 9 49 3151 ret false 3152 end 3153 for i ulong sub get didl 0 9 bound 87 3154 v = long -1 3155 for j ulong 58 3156 if eq get did add i 9 get kb58 j 3157 v = long j 3158 break 3159 end 3160 end 3161 if lt v 0 3162 ret false 3163 end 3164 carry = ulong v 3165 for j ulong 34 3166 idx = sub 33 j 3167 carry = add carry mul ulong get raw idx 58 3168 put raw idx uchar and carry 255 3169 carry = shr carry 8 3170 end 3171 if ne carry 0 3172 ret false 3173 end 3174 end 3175 if or ne get raw 0 237 ne get raw 1 1 3176 ret false 3177 end 3178 put blob 3 11 3179 for i ulong 11 3180 put blob add i 4 get kued i 3181 end 3182 put blob 18 32 3183 for i ulong 32 3184 put blob add i 19 get raw add i 2 3185 end 3186 put obl 0 0 3187 emt kuuser 3188 emb 32 3189 emt kued 3190 emb 32 3191 for i ulong 17 3192 j = mul i 3 3193 v = or or shl ulong get blob j 16 shl ulong get blob add j 1 8 ulong get blob add j 2 3194 emb get ku64 and shr v 18 63 3195 emb get ku64 and shr v 12 63 3196 emb get ku64 and shr v 6 63 3197 emb get ku64 and v 63 3198 end 3199 emb 10 3200 wpath kusign 3201 ret wr 3202 end 3203 fn uverify to bool 3204 ustatement 3205 wpath kuinput 3206 if not wr 3207 ret false 3208 end 3209 if not upubkey 3210 ret false 3211 end 3212 pknew 3213 pkt kenv 3214 pkt kdashi 3215 pkt kpathg 3216 pkt kussh 3217 pkt kuY 3218 pkt kuverify 3219 pkt kuf 3220 sbz 3221 for i ulong get wdl 0 bound 512 3222 sbc get wd i 3223 end 3224 sbt kusign 3225 pks 3226 pkt kuI 3227 pkt kuuser 3228 pkt kun 3229 pkt kuns 3230 pkt kus 3231 wpath kubsigw 3232 sbz 3233 for i ulong get pthl 0 bound 1100 3234 sbc get pth i 3235 end 3236 pks 3237 put ustdin 0 1 3238 ok = run 20 3239 put ustdin 0 0 3240 ret ok 3241 end 3242 note Canonical ustar: header bytes, numeric fields, checksum, sorted distinct safe paths; two zero 3243 note blocks, regular files only, no links/devices/extensions. Validate the entire archive before 3244 note making any file in the scratch work tree. Pass two writes only paths validated by pass one. 3245 fn uoct a ulong n ulong to long 3246 v = long 0 3247 for i ulong sub n 1 bound 12 3248 c = get uhdr add a i 3249 if or lt c 48 gt c 55 3250 ret -1 3251 end 3252 v = add mul v 8 long sub c 48 3253 end 3254 if ne get uhdr sub add a n 1 0 3255 ret -1 3256 end 3257 ret v 3258 end 3259 fn uzero a ulong n ulong to bool 3260 for i ulong n bound 512 3261 if ne get uhdr add a i 0 3262 ret false 3263 end 3264 end 3265 ret true 3266 end 3267 fn ustr a ulong n ulong to ulong 3268 for i ulong n bound 512 3269 if eq get uhdr add a i 0 3270 if not uzero add add a i 1 sub sub n i 1 3271 put uerr 0 1 3272 end 3273 ret i 3274 end 3275 end 3276 ret n 3277 end 3278 fn upathto a ulong n ulong k ulong to ulong 3279 for i ulong n bound 255 3280 put uname add k i get uhdr add a i 3281 end 3282 ret add k n 3283 end 3284 fn ucheckpath to bool 3285 n = get unamel 0 3286 if or eq n 0 gt n 255 3287 ret false 3288 end 3289 a = ulong 0 3290 for i ulong add n 1 bound 256 3291 c = uchar 47 3292 if lt i n 3293 c = get uname i 3294 if or lt c 32 eq c 127 3295 ret false 3296 end 3297 end 3298 if eq c 47 3299 k = sub i a 3300 if eq k 0 3301 ret false 3302 end 3303 if eq k 1 3304 if eq get uname a 46 3305 ret false 3306 end 3307 end 3308 if eq k 2 3309 if and eq get uname a 46 eq get uname add a 1 46 3310 ret false 3311 end 3312 end 3313 if eq k 4 3314 if eq get uname a 46 3315 if and and eq ulower get uname add a 1 103 eq ulower get uname add a 2 105 eq ulower get uname add a 3 116 3316 put uerr 0 3 3317 ret false 3318 end 3319 end 3320 end 3321 a = add i 1 3322 end 3323 end 3324 if gt get uprevl 0 0 3325 cmp = long 0 3326 m = get uprevl 0 3327 for i ulong n bound 255 3328 if ge i m 3329 cmp = long 1 3330 break 3331 end 3332 if ne get uname i get uprev i 3333 cmp = sub long get uname i long get uprev i 3334 break 3335 end 3336 end 3337 if le cmp 0 3338 ret false 3339 end 3340 end 3341 for i ulong n bound 255 3342 put uprev i get uname i 3343 end 3344 put uprevl 0 n 3345 ret true 3346 end 3347 fn ulower c uchar to uchar 3348 if and ge c 65 le c 90 3349 ret add c 32 3350 end 3351 ret c 3352 end 3353 fn uheader to long 3354 if not uzero 500 12 3355 ret -1 3356 end 3357 if or ne get uhdr 156 48 not uzero 157 100 3358 ret -1 3359 end 3360 if not uzero 265 64 3361 ret -1 3362 end 3363 if not uzero 329 16 3364 ret -1 3365 end 3366 if or ne uoct 108 8 0 ne uoct 116 8 0 3367 ret -1 3368 end 3369 if ne uoct 136 12 0 3370 ret -1 3371 end 3372 mode = uoct 100 8 3373 if not or eq mode 420 eq mode 493 3374 ret -1 3375 end 3376 if or or ne get uhdr 257 117 ne get uhdr 258 115 or ne get uhdr 259 116 ne get uhdr 260 97 3377 ret -1 3378 end 3379 if or or ne get uhdr 261 114 ne get uhdr 262 0 or ne get uhdr 263 48 ne get uhdr 264 48 3380 ret -1 3381 end 3382 sum = ulong 0 3383 for i ulong 512 3384 c = get uhdr i 3385 if and ge i 148 lt i 156 3386 c = uchar 32 3387 end 3388 sum = add sum ulong c 3389 end 3390 if ne get uhdr 154 0 3391 ret -1 3392 end 3393 if ne get uhdr 155 32 3394 ret -1 3395 end 3396 cs = ulong 0 3397 for i ulong 6 3398 c = get uhdr add 148 i 3399 if or lt c 48 gt c 55 3400 ret -1 3401 end 3402 cs = add mul cs 8 ulong sub c 48 3403 end 3404 if ne cs sum 3405 ret -1 3406 end 3407 put uerr 0 0 3408 pre = ustr 345 155 3409 nam = ustr 0 100 3410 if eq nam 0 3411 ret -1 3412 end 3413 k = ulong 0 3414 if gt pre 0 3415 k = upathto 345 pre 0 3416 put uname k 47 3417 k = add k 1 3418 end 3419 if gt add k nam 255 3420 ret -1 3421 end 3422 k = upathto 0 nam k 3423 put unamel 0 k 3424 if ne get uerr 0 0 3425 ret -1 3426 end 3427 if not ucheckpath 3428 ret -1 3429 end 3430 ret uoct 124 12 3431 end 3432 fn utreepath parents bool to void 3433 wpath kutree 3434 k = get pthl 0 3435 put pth k 47 3436 k = add k 1 3437 for i ulong get unamel 0 bound 255 3438 c = get uname i 3439 if and parents eq c 47 3440 r = makedir pth 0 add k i 448 3441 end 3442 put pth add k i c 3443 end 3444 put pthl 0 add k get unamel 0 3445 end 3446 note uscanc: a streaming match of "-----BEGIN" then "PRIVATE KEY-----" on the same line, so a 3447 note key block split across tar blocks is still found 3448 fn umatch t view uchar * st ulong c uchar to ulong 3449 if eq c get t st 3450 ret add st 1 3451 end 3452 if eq c get t 0 3453 ret 1 3454 end 3455 ret 0 3456 end 3457 fn uscanc c uchar to void 3458 if eq get uscan 1 0 3459 s = umatch ubegin get uscan 0 c 3460 if eq s len ubegin 3461 put uscan 1 1 3462 s = ulong 0 3463 end 3464 put uscan 0 s 3465 ret 3466 end 3467 if eq c 10 3468 put uscan 1 0 3469 put uscan 0 0 3470 ret 3471 end 3472 s = umatch upriv get uscan 0 c 3473 if eq s len upriv 3474 put uerr 0 2 3475 s = ulong 0 3476 end 3477 put uscan 0 s 3478 end 3479 note ublock: one archive data block: key scan, zero padding, digest and the optional write 3480 fn ublock b view uchar * n ulong dest int ctx edit ulong * to bool 3481 for i ulong n bound 512 3482 uscanc get b i 3483 end 3484 if ne get uerr 0 0 3485 ret false 3486 end 3487 for i ulong sub 512 n bound 512 3488 if ne get b add n i 0 3489 ret false 3490 end 3491 end 3492 sha256_update ctx part b 0 n 3493 if lt dest 0 3494 ret true 3495 end 3496 w = write dest b 0 n 3497 ret eq w long n 3498 end 3499 fn udata fd int dest int size ulong inv bool to bool 3500 b = new uchar 512 0 3501 ctx = new ulong 40 0 3502 h = new uchar 32 0 3503 sha256_init ctx 3504 left = size 3505 put uscan 0 0 3506 put uscan 1 0 3507 while gt left 0 bound `9223372036854775807` 3508 k = read fd b 0 512 3509 if ne k 512 3510 ret false 3511 end 3512 n = left 3513 if gt n 512 3514 n = ulong 512 3515 end 3516 if not ublock b n dest ctx 3517 ret false 3518 end 3519 left = sub left n 3520 end 3521 sha256_final ctx h 3522 if inv 3523 uinv h size 3524 end 3525 ret true 3526 end 3527 fn uinv h view uchar * size ulong to void 3528 for i ulong 32 3529 c = get h i 3530 emb get kuhex ulong shr c 4 3531 emb get kuhex ulong and c 15 3532 end 3533 emb 9 3534 emn size 3535 emb 9 3536 for i ulong get unamel 0 bound 255 3537 emb get uname i 3538 end 3539 emb 10 3540 end 3541 fn utar unpack bool to bool 3542 wpath kuarc 3543 fd = fopen pth 0 get pthl 0 557056 0 3544 if lt fd 0 3545 ret false 3546 end 3547 put uprevl 0 0 3548 ok = utarloop int fd unpack 3549 x = fclose int fd 3550 ret ok 3551 end 3552 fn utarloop fd int unpack bool to bool 3553 b = new uchar 512 0 3554 count = ulong 0 3555 while true bound `9223372036854775807` 3556 k = read fd uhdr 0 512 3557 if ne k 512 3558 ret false 3559 end 3560 if uzero 0 512 3561 k = read fd uhdr 0 512 3562 if ne k 512 3563 ret false 3564 end 3565 if not uzero 0 512 3566 ret false 3567 end 3568 k = read fd b 0 1 3569 if ne k 0 3570 ret false 3571 end 3572 ret eq count get uvals 0 3573 end 3574 size = uheader 3575 if lt size 0 3576 ret false 3577 end 3578 dest = long -1 3579 if unpack 3580 utreepath true 3581 dest = fopen pth 0 get pthl 0 557249 int uoct 100 8 3582 if lt dest 0 3583 ret false 3584 end 3585 end 3586 ok = udata fd int dest ulong size unpack 3587 if ge dest 0 3588 x = fclose int dest 3589 end 3590 if not ok 3591 ret false 3592 end 3593 count = add count 1 3594 end 3595 ret false 3596 end 3597 fn ugitbase wt bool to void 3598 pknew 3599 pkt kenv 3600 pkt kdashi 3601 pkt kgn 3602 pkt kgg 3603 pkt kgallow 3604 pkt kgnlazy 3605 pkt kgprompt 3606 pkt kpathg 3607 sbz 3608 sbt khome 3609 for i ulong get wdl 0 bound 512 3610 sbc get wd i 3611 end 3612 pks 3613 sbz 3614 sbt kuan 3615 for i ulong get didl 0 bound 96 3616 sbc get did i 3617 end 3618 pks 3619 pkt kuae 3620 pkt kucn 3621 pkt kuce 3622 pkt kgit 3623 pkt kuc 3624 pkt kuhooks 3625 pkt kuc 3626 pkt kusign0 3627 sbz 3628 sbt kugd 3629 for i ulong get wdl 0 bound 512 3630 sbc get wd i 3631 end 3632 sbt kugit 3633 pks 3634 if wt 3635 sbz 3636 sbt kuwt 3637 for i ulong get wdl 0 bound 512 3638 sbc get wd i 3639 end 3640 sbt kutree 3641 pks 3642 end 3643 end 3644 fn ucommit to bool 3645 ugitbase false 3646 pkt kuinit 3647 pkt kubare 3648 pkt kutmpl 3649 pkt kubr 3650 if not run 20 3651 ret false 3652 end 3653 ugitbase true 3654 pkt kuadd 3655 pkt kuall 3656 pkt kuforce 3657 pkt kdd 3658 sbz 3659 for i ulong get wdl 0 bound 512 3660 sbc get wd i 3661 end 3662 sbt kutree 3663 pks 3664 if not run 20 3665 ret false 3666 end 3667 ugitbase true 3668 pkt kucommit 3669 pkt kunover 3670 pkt kum 3671 pkt kumsg 3672 ret run 30 3673 end 3674 fn uproof ext view uchar * to bool 3675 wpath kutree 3676 n = get pthl 0 3677 for i ulong len ext bound 32 3678 put pth add n i get ext i 3679 end 3680 put pthl 0 add n len ext 3681 ret wr 3682 end 3683 note uinitrad: rad init of the scratch git dir as the node's user; the RID is read from the 3684 note "rad:z..." in its output. Description: the bring note, else a fixed sentence. 3685 fn uinitrad to bool 3686 pkrad 3687 pkt kuinit 3688 pkt kupub 3689 pkt kunocon 3690 note --name= and --description= are one argument each, so a note that starts with '-' 3691 note is only ever a description, whatever rad's argument parser makes of a separate word 3692 sbz 3693 sbt kunm 3694 for i ulong get nml 0 bound 64 3695 sbc get nm i 3696 end 3697 pks 3698 sbz 3699 sbt kudesceq 3700 if gt get ntl 0 0 3701 for i ulong get ntl 0 bound 200 3702 sbc get nt i 3703 end 3704 else 3705 sbt kumsg 3706 end 3707 pks 3708 pkt kubranch 3709 pkt kumain 3710 sbz 3711 for i ulong get wdl 0 bound 512 3712 sbc get wd i 3713 end 3714 sbt kugit 3715 pks 3716 if not run 60 3717 ret false 3718 end 3719 ret uridout 3720 end 3721 text kuradz "rad:z" 3722 fn uradat i ulong to bool 3723 for j ulong 5 3724 if ne get cb add i j get kuradz j 3725 ret false 3726 end 3727 end 3728 ret true 3729 end 3730 fn uridout to bool 3731 n = get cbl 0 3732 i = ulong 0 3733 while lt add i 5 n bound 65536 3734 if uradat i 3735 k = ulong 0 3736 while lt add add i 4 k n bound 45 3737 c = get cb add add i 4 k 3738 if not b58 c 3739 break 3740 end 3741 put rid k c 3742 k = add k 1 3743 end 3744 if and ge k 20 le k 44 3745 put ridl 0 k 3746 ret true 3747 end 3748 end 3749 i = add i 1 3750 end 3751 ret false 3752 end 3753 fn upload to void 3754 uscratch 3755 put ridl 0 0 3756 if denied 3757 uref kr8 3758 ret 3759 end 3760 if not allowed 3761 uref kr0 3762 ret 3763 end 3764 note storage while it is stored: the decoded archive, the unpacked tree, the git objects and the 3765 note Radicle copy, at most the archive's size each 3766 if not rsvok 0 mul get uvals 1 4 3767 uref kfull 3768 ret 3769 end 3770 if not udecodeall 3771 uref kurbad 3772 ret 3773 end 3774 if ne get uvals 2 get uvals 1 3775 uref kurbyte 3776 ret 3777 end 3778 wpath kuarc 3779 if not ufilehash 3780 uref kurdisk 3781 ret 3782 end 3783 for i ulong 64 3784 if ne get udigest i get usha i 3785 uref kursha 3786 ret 3787 end 3788 end 3789 if not ugetsig 3790 uref kurbad 3791 ret 3792 end 3793 if not uverify 3794 uref kursig 3795 ret 3796 end 3797 put uerr 0 0 3798 if not utar false 3799 utarfail 3800 ret 3801 end 3802 wpath kutree 3803 r = makedir pth 0 get pthl 0 448 3804 ustatement 3805 if not utar true 3806 uref kurdisk 3807 ret 3808 end 3809 note ob holds the exact statement followed by every file's digest, byte count and path. 3810 wpath kutree 3811 n = get pthl 0 3812 for i ulong len kuinter 3813 put pth add n i get kuinter i 3814 end 3815 r = makedir pth 0 add n len kuinter 448 3816 if not uproof kubtxt 3817 uref kurdisk 3818 ret 3819 end 3820 put obl 0 0 3821 for i ulong get usiglen 0 bound 65536 3822 emb get usigbuf i 3823 end 3824 if not uproof kubsig 3825 uref kurdisk 3826 ret 3827 end 3828 if not ucommit 3829 uref kurinit 3830 ret 3831 end 3832 if not uinitrad 3833 uref kurinit 3834 ret 3835 end 3836 put dsl 0 0 3837 ok = dodu 3838 save kustored 3839 snap 3840 ustatus kustored true 3841 uclean 3842 end 3843 fn utarfail to void 3844 if eq get uerr 0 2 3845 uref kurkey 3846 ret 3847 end 3848 if eq get uerr 0 3 3849 uref kurgit 3850 ret 3851 end 3852 uref kurtar 3853 end 3854 note Downloads are made for every shelf row on each intake pass, not only newly brought rows. 3855 note apath: pin a/ (mk makes it first); pth = then ext, in it. Callers unpin. 3856 fn apath ext view uchar * mk bool to bool 3857 if not pinsub kua mk 3858 ret false 3859 end 3860 arel ext 3861 ret true 3862 end 3863 note arel: pth = then ext (in the pinned a/) 3864 fn arel ext view uchar * to void 3865 for i ulong get ridl 0 bound 44 3866 put pth i get rid i 3867 end 3868 n = get ridl 0 3869 for i ulong len ext bound 16 3870 put pth add n i get ext i 3871 end 3872 put pthl 0 add n len ext 3873 end 3874 note adelp: remove the download of the current row from the pinned a/ (tar, hash, temp file) 3875 fn adelp to void 3876 arel kutar 3877 r = unlink pth 0 get pthl 0 3878 arel kuhash 3879 r = unlink pth 0 get pthl 0 3880 arel kuhead 3881 r = unlink pth 0 get pthl 0 3882 arel kutmp 3883 r = unlink pth 0 get pthl 0 3884 end 3885 fn adel to void 3886 if apath kutar false 3887 adelp 3888 unpin 3889 end 3890 end 3891 note archive: the download of the current row. git rev-parse (the snapshot's environment, no 3892 note network) names the commit HEAD is at (ahead). When .tar and .sha256 are regular 3893 note files and .head names that commit, the download is current and nothing more is done, so a 3894 note second bring line of the lister and every allarchives pass cost one git process, not an 3895 note archive. Else git archive of that commit writes to a pipe; this program counts, hashes and 3896 note copies the bytes into a/.tar.tmp, created exclusively and never through a link in the a/ 3897 note it pinned. When HEAD has moved (or there is no current download) the old download is stale: 3898 note it is removed first (adel), so the old and the new file never take room on the data filesystem 3899 note together. Then alimit is the free bytes of the open dir's filesystem less its reserve; past it 3900 note or 30 s, git is stopped and the row has no download. There is no size cap. Else the temp file 3901 note is renamed onto .tar, and the hash and the commit are written to .sha256 and 3902 note .head with wr. Nothing is written outside the pinned a/. 3903 fn archive to void 3904 if not ahead 3905 adel 3906 ret 3907 end 3908 if apath kuhead true 3909 cur = acurrent 3910 unpin 3911 if cur 3912 ret 3913 end 3914 end 3915 adel 3916 fa = dfree 1 3917 ra = rsvget 1 3918 put alimit 0 0 3919 if gt fa ra 3920 put alimit 0 sub fa ra 3921 end 3922 if not apath kutmp true 3923 ret 3924 end 3925 r = unlink pth 0 get pthl 0 3926 fd = fopen pth 0 get pthl 0 557249 420 3927 if lt fd 0 3928 adelp 3929 unpin 3930 ret 3931 end 3932 ok = apipe int fd 3933 r = fsync int fd 3934 x = fclose int fd 3935 if ok 3936 ok = amove 3937 end 3938 if ok 3939 put obl 0 0 3940 for i ulong 64 3941 emb get udigest i 3942 end 3943 arel kuhash 3944 ok = wr 3945 end 3946 if ok 3947 put obl 0 0 3948 for i ulong get ahdl 0 bound 64 3949 emb get ahd i 3950 end 3951 arel kuhead 3952 ok = wr 3953 end 3954 if not ok 3955 adelp 3956 end 3957 unpin 3958 end 3959 note ahead: the commit HEAD is at (git rev-parse --verify HEAD^{commit}): 40 or 64 hex into ahd 3960 fn ahead to bool 3961 pkgit 3962 pkt kgrev 3963 pkt kgverify 3964 pkt kgheadc 3965 if not run 20 3966 ret false 3967 end 3968 m = get cbl 0 3969 n = ulong 0 3970 while lt n m bound 65 3971 c = get cb n 3972 if eq c 10 3973 break 3974 end 3975 if or ge n 64 not uhex c 3976 ret false 3977 end 3978 put ahd n c 3979 n = add n 1 3980 end 3981 if and ne n 40 ne n 64 3982 ret false 3983 end 3984 put ahdl 0 n 3985 ret true 3986 end 3987 note acurrent: (a/ pinned) .tar and .sha256 are regular files and .head holds ahd 3988 fn acurrent to bool 3989 arel kutar 3990 if not areg 3991 ret false 3992 end 3993 arel kuhash 3994 if not areg 3995 ret false 3996 end 3997 arel kuhead 3998 fd = ropen 3999 if lt fd 0 4000 ret false 4001 end 4002 b = new uchar 72 0 4003 k = read int fd b 0 72 4004 x = fclose int fd 4005 n = get ahdl 0 4006 if ne k long n 4007 ret false 4008 end 4009 for i ulong n bound 64 4010 if ne get b i get ahd i 4011 ret false 4012 end 4013 end 4014 ret true 4015 end 4016 note areg: pth (in the pinned folder) is a regular file itself (lstat: a link is not followed) 4017 fn areg to bool 4018 b = new uchar 128 0 4019 r = lstat pth 0 get pthl 0 b 0 128 4020 if ne r 0 4021 ret false 4022 end 4023 m = or ulong get b 16 shl ulong get b 17 8 4024 ret eq and m 61440 32768 4025 end 4026 note amove: rename .tar.tmp onto .tar in the pinned a/, after removing the old hash and 4027 note commit 4028 fn amove to bool 4029 arel kuhead 4030 r = unlink pth 0 get pthl 0 4031 arel kuhash 4032 r = unlink pth 0 get pthl 0 4033 arel kutar 4034 m = get pthl 0 4035 for i ulong m bound 1100 4036 put pt2 i get pth i 4037 end 4038 arel kutmp 4039 n = get pthl 0 4040 put rpk 0 2 4041 put rpk 1 0 4042 put rpk 2 uchar and n 255 4043 put rpk 3 uchar shr n 8 4044 for i ulong n bound 1100 4045 put rpk add 4 i get pth i 4046 end 4047 o = add 4 n 4048 put rpk o uchar and m 255 4049 put rpk add o 1 uchar shr m 8 4050 for i ulong m bound 1100 4051 put rpk add add o 2 i get pt2 i 4052 end 4053 ret eq renames rpk 0 add add o 2 m 0 0 4054 end 4055 fn upfd b view uchar * i ulong to int 4056 o = mul i 4 4057 v = or ulong get b o shl ulong get b add o 1 8 4058 v = or v shl ulong get b add o 2 16 4059 v = or v shl ulong get b add o 3 24 4060 ret int v 4061 end 4062 note apipe: run git archive with its stdout on a pipe and copy it to dest (acopy); true when git 4063 note exited 0 and every byte was copied within the cap and the deadline 4064 fn apipe dest int to bool 4065 pf = new uchar 8 0 4066 pkgit 4067 pkt kuarch 4068 pkt kuformat 4069 sbz 4070 for i ulong get ahdl 0 bound 64 4071 sbc get ahd i 4072 end 4073 pks 4074 r = pipefds pf 0 8 524288 4075 if lt r 0 4076 ret false 4077 end 4078 rfd = upfd pf 0 4079 wfd = upfd pf 1 4080 put upipew 0 ulong wfd 4081 put upiped 0 1 4082 pid = fork 4083 if eq pid 0 4084 child 4085 exit 127 4086 end 4087 put upiped 0 0 4088 x = fclose wfd 4089 if lt pid 0 4090 x = fclose rfd 4091 ret false 4092 end 4093 r = setpgid int pid int pid 4094 ok = acopy rfd dest 4095 x = fclose rfd 4096 if not ok 4097 r = killpid int neg pid 9 4098 r = killpid int pid 9 4099 end 4100 ret areap pid ok 4101 end 4102 note acopy: the pipe src into dest, counted and hashed (udigest); false past the cap or 30 s 4103 fn acopy src int dest int to bool 4104 b = new uchar 65536 0 4105 ctx = new ulong 40 0 4106 sha256_init ctx 4107 tot = ulong 0 4108 dl = add long monons `30000000000` 4109 while true bound `9223372036854775807` 4110 n = aread src b dl 4111 if eq n 0 4112 break 4113 end 4114 if lt n 0 4115 ret false 4116 end 4117 tot = add tot ulong n 4118 if gt tot get alimit 0 4119 ret false 4120 end 4121 sha256_update ctx part b 0 ulong n 4122 k = write dest b 0 ulong n 4123 if ne k n 4124 ret false 4125 end 4126 end 4127 ahex ctx 4128 ret true 4129 end 4130 note aread: one read from the pipe before the deadline: the count, 0 at its end, -1 on failure 4131 fn aread src int b edit uchar 65536 dl long to long 4132 while true bound `9223372036854775807` 4133 left = sub dl long monons 4134 if le left 0 4135 ret -1 4136 end 4137 w = waitio src 1 left 4138 if gt w 0 4139 n = read src b 0 65536 4140 if ge n 0 4141 ret n 4142 end 4143 if and ne n -4 ne n -11 4144 ret -1 4145 end 4146 else 4147 if and lt w 0 ne w -4 4148 ret -1 4149 end 4150 end 4151 end 4152 ret -1 4153 end 4154 fn ahex ctx edit ulong 40 to void 4155 h = new uchar 32 0 4156 sha256_final ctx h 4157 for i ulong 32 4158 c = get h i 4159 put udigest mul i 2 get kuhex ulong shr c 4 4160 put udigest add mul i 2 1 get kuhex ulong and c 15 4161 end 4162 end 4163 note areap: wait for git (10 s more at most); true when ok and it exited 0 4164 fn areap pid long ok bool to bool 4165 st = new uchar 4 0 4166 for i ulong 1000 4167 r = waitpid int pid st 0 4 1 4168 if eq r pid 4169 if not ok 4170 ret false 4171 end 4172 for j ulong 4 4173 if ne get st j 0 4174 ret false 4175 end 4176 end 4177 ret true 4178 end 4179 if lt r 0 4180 ret false 4181 end 4182 r = sleepns `10000000` 4183 end 4184 r = killpid int neg pid 9 4185 r = killpid int pid 9 4186 r = waitpid int pid st 0 4 0 4187 ret false 4188 end 4189 fn allarchives to void 4190 if not lvalid 4191 ret 4192 end 4193 lopen 0 4194 while lnext 0 bound `9223372036854775807` 4195 e = get lbl 0 4196 if lpub 0 e 4197 setrow 0 e 4198 archive 4199 end 4200 end 4201 ok = lok 4202 end 4203 note ---- alarms (no-hard-caps point 5): measurements beside the unchanged caps. Nothing here refuses. 4204 note A check fires at value >= threshold (free disk: percent free <= 15). alarms.txt holds one 4205 note line per firing alarm: name, value, threshold, source (tab separated); the same lines go to 4206 note stderr with "ALARM "; alarms.json lists every check with "firing". 4207 fn amax a ulong b ulong to ulong 4208 if gt a b 4209 ret a 4210 end 4211 ret b 4212 end 4213 note aqsize: the size of queue 4214 fn aqsize to ulong 4215 if not pinopen 4216 ret 0 4217 end 4218 rname kq 4219 v = alsize 4220 unpin 4221 ret v 4222 end 4223 note alsize: the size of pth, the file itself (lstat: a link is not followed) 4224 fn alsize to ulong 4225 b = new uchar 128 0 4226 r = lstat pth 0 get pthl 0 b 0 128 4227 if ne r 0 4228 ret 0 4229 end 4230 v = ulong 0 4231 for i ulong 8 4232 v = or v shl ulong get b add 48 i mul i 8 4233 end 4234 ret v 4235 end 4236 fn aspool to ulong 4237 a = new uchar 65536 0 4238 if not pinsub kuup false 4239 ret 0 4240 end 4241 fd = fopen kdot 0 1 573440 0 4242 if lt fd 0 4243 unpin 4244 ret 0 4245 end 4246 base = ulong 0 4247 tot = ulong 0 4248 while true bound `1000000` 4249 n = getdnts int fd a 0 65536 4250 if le n 0 4251 break 4252 end 4253 o = ulong 0 4254 while lt o ulong n bound 65536 4255 rl = or ulong get a add o 16 shl ulong get a add o 17 8 4256 if eq get a add o 19 46 4257 o = add o rl 4258 continue 4259 end 4260 k = ulong 0 4261 while lt k 256 bound 256 4262 c = get a add add o 19 k 4263 if eq c 0 4264 break 4265 end 4266 put pth add base k c 4267 k = add k 1 4268 end 4269 put pthl 0 add base k 4270 tot = add tot alsize 4271 if eq rl 0 4272 break 4273 end 4274 o = add o rl 4275 end 4276 end 4277 x = fclose int fd 4278 unpin 4279 ret tot 4280 end 4281 note arows: the shelf from the private list: rows on it (avals 1), the key with the most rows 4282 note this program fetched or stored for it (avals 0, its key and volume), the largest row in KiB 4283 note (avals 2, its key), the total volume, the list's own size in bytes (avals 9) and its longest 4284 note row (avals 12). One scan in slot 0; lcount and lkib scan in slot 1 inside it. 4285 fn arows to void 4286 put lerr 0 0 4287 for i ulong 3 4288 put avals i 0 4289 akclr i 4290 end 4291 put avol 1 0 4292 wpath klist 4293 put avals 9 alsize 4294 put lrmax 0 0 4295 put lcdl 0 1000 4296 lopen 0 4297 while lnext 0 bound `9223372036854775807` 4298 e = get lbl 0 4299 s = ulong 0 4300 if lpub s e 4301 put avals 1 add get avals 1 1 4302 v = lkibat s e 4303 put avol 1 add get avol 1 mul v 1024 4304 setrow s e 4305 if gt v get avals 2 4306 put avals 2 v 4307 put avol 2 mul v 1024 4308 akset 2 4309 end 4310 if lowned s e 4311 n = lcountc 4312 if gt n get avals 0 4313 put avals 0 n 4314 put avol 0 mul lkib 1024 4315 akset 0 4316 akcopy 0 1 4317 end 4318 end 4319 end 4320 end 4321 put avals 12 get lrmax 0 4322 put avol 11 get lrmax 0 4323 ok = lok 4324 end 4325 note lcountc: lcount true for the current did, remembered for the last did it counted: arows asks 4326 note once for every row of a key, and a whole scan each time would cost the list's size per row 4327 note (arows sets lcdl to 1000, a length no did has, at the start of each pass) 4328 fn lcountc to ulong 4329 n = get didl 0 4330 if eq get lcdl 0 n 4331 same = true 4332 for i ulong n bound 96 4333 if ne get lcd i get did i 4334 same = false 4335 end 4336 end 4337 if same 4338 ret get lcdn 0 4339 end 4340 end 4341 v = lcount true 4342 for i ulong n bound 96 4343 put lcd i get did i 4344 end 4345 put lcdl 0 n 4346 put lcdn 0 v 4347 ret v 4348 end 4349 note lkibat: field 4 (kilobytes) of the row s..e 4350 fn lkibat s ulong e ulong to ulong 4351 a = lfld s e 4 4352 b = lfe a e 4353 v = ulong 0 4354 for i ulong sub b a bound 20 4355 c = get lb add a i 4356 if and ge c 48 le c 57 4357 v = add mul v 10 ulong sub c 48 4358 end 4359 end 4360 ret v 4361 end 4362 note lkib: the kilobytes of the current did's rows this program fetched or stored (slot 1) 4363 fn lkib to ulong 4364 t = ulong 0 4365 lopen 1 4366 while lnext 1 bound `9223372036854775807` 4367 e = get lbl 0 4368 if lpub 0 e 4369 if lowned 0 e 4370 if lisdid 0 e 4371 t = add t lkibat 0 e 4372 end 4373 end 4374 end 4375 end 4376 ret t 4377 end 4378 note akeys: the key an alarm names (96 bytes a slot), its length; akset copies the current did 4379 fn akclr i ulong to void 4380 put akl i 0 4381 end 4382 fn akset i ulong to void 4383 n = get didl 0 4384 for j ulong n bound 96 4385 put akeys add mul i 96 j get did j 4386 end 4387 put akl i n 4388 end 4389 fn akcopy a ulong b ulong to void 4390 n = get akl a 4391 for j ulong n bound 96 4392 put akeys add mul b 96 j get akeys add mul a 96 j 4393 end 4394 put akl b n 4395 end 4396 note dfree: the bytes free on the filesystem of the storage dir (which 0) or the open dir (1), from 4397 note df -B1 -P (column Available); 0 when df does not answer, so a check refuses rather than guesses 4398 fn dfree which ulong to ulong 4399 pknew 4400 pkt kadf 4401 pkt kadfb 4402 pkt kadfp 4403 sbz 4404 if eq which 0 4405 for i ulong get sdl 0 bound 512 4406 sbc get sd i 4407 end 4408 else 4409 for i ulong get odl 0 bound 512 4410 sbc get od i 4411 end 4412 end 4413 pks 4414 if not run 10 4415 ret 0 4416 end 4417 ret dfcol 3 4418 end 4419 note dfcol: column k of the second line of the command output, as a number; 0 when it is not one 4420 fn dfcol k ulong to ulong 4421 p = dfnl 4422 for col ulong add k 1 bound 16 4423 p = dftok p 4424 end 4425 if eq get dfv 1 0 4426 ret 0 4427 end 4428 ret get dfv 0 4429 end 4430 fn dfnl to ulong 4431 n = get cbl 0 4432 l = ulong 0 4433 while lt l n bound 65536 4434 if eq get cb l 10 4435 ret add l 1 4436 end 4437 l = add l 1 4438 end 4439 ret n 4440 end 4441 note dftok: the token at or after p (spaces skipped): its value in dfv 0, 1 in dfv 1 when it is all digits 4442 fn dftok p ulong to ulong 4443 n = get cbl 0 4444 q = p 4445 while and lt q n eq get cb q 32 bound 65536 4446 q = add q 1 4447 end 4448 v = ulong 0 4449 ok = lt q n 4450 while and lt q n ne get cb q 32 bound 65536 4451 c = get cb q 4452 if or lt c 48 gt c 57 4453 ok = false 4454 else 4455 v = add mul v 10 ulong sub c 48 4456 end 4457 q = add q 1 4458 end 4459 put dfv 0 v 4460 put dfv 1 0 4461 if ok 4462 put dfv 1 1 4463 end 4464 ret q 4465 end 4466 fn aseed to ulong 4467 pkrad 4468 pkt kseed 4469 if not run 30 4470 ret 0 4471 end 4472 ret get cbl 0 4473 end 4474 fn atext i ulong src bool to void 4475 if src 4476 atsrc i 4477 ret 4478 end 4479 if eq i 0 4480 emt kan0 4481 end 4482 if eq i 1 4483 emt kan1 4484 end 4485 if eq i 2 4486 emt kan2 4487 end 4488 if eq i 3 4489 emt kan3 4490 end 4491 if eq i 4 4492 emt kan4 4493 end 4494 if eq i 5 4495 emt kan5 4496 end 4497 if eq i 6 4498 emt kan6 4499 end 4500 if eq i 7 4501 emt kan7 4502 end 4503 if eq i 8 4504 emt kan8 4505 end 4506 if eq i 9 4507 emt kan9 4508 end 4509 if eq i 10 4510 emt kan10 4511 end 4512 if eq i 11 4513 emt kan11 4514 end 4515 end 4516 fn atsrc i ulong to void 4517 if lt i 3 4518 emt kas0 4519 end 4520 if eq i 3 4521 emt kas3 4522 end 4523 if eq i 4 4524 emt kas4 4525 end 4526 if eq i 5 4527 emt kas5 4528 end 4529 if eq i 6 4530 emt kas6 4531 end 4532 if eq i 7 4533 emt kas7 4534 end 4535 if eq i 8 4536 emt kas8 4537 end 4538 if eq i 9 4539 emt kas9 4540 end 4541 if eq i 10 4542 emt kas10 4543 end 4544 if eq i 11 4545 emt kas11 4546 end 4547 end 4548 note akey: the key of alarm i, as text ("-" when it names none) or as a JSON value (null) 4549 fn akey i ulong json bool to void 4550 n = get akl i 4551 if eq n 0 4552 if json 4553 emt kanull 4554 else 4555 emb 45 4556 end 4557 ret 4558 end 4559 if json 4560 emb 34 4561 end 4562 for j ulong n bound 96 4563 emb get akeys add mul i 96 j 4564 end 4565 if json 4566 emb 34 4567 end 4568 end 4569 note aemit: one alarm. A line in alarms.txt when it fires (name, value, threshold, source, key, 4570 note volume in bytes); every alarm in alarms.json. Free-space alarms (6, 8) fire at or below 4571 note their threshold, the others at or above it; reserve_refused (10) fires while the last reserve check 4572 note refused a bring (until a later check finds room). 4573 fn aemit i ulong v ulong th ulong json bool to void 4574 fire = ge v th 4575 if or eq i 6 eq i 8 4576 fire = le v th 4577 end 4578 if eq i 10 4579 fire = ne get aref 0 0 4580 end 4581 if json 4582 if gt i 0 4583 emb 44 4584 end 4585 emt kaj2 4586 atext i false 4587 emt kaj3 4588 emn v 4589 emt kaj4 4590 emn th 4591 emt kaj5 4592 atext i true 4593 emt kaj8 4594 akey i true 4595 emt kaj9 4596 emn get avol i 4597 emt kaj10 4598 if fire 4599 emt katrue 4600 else 4601 emt kafalse 4602 end 4603 emb 125 4604 ret 4605 end 4606 if fire 4607 atext i false 4608 emb 9 4609 emn v 4610 emb 9 4611 emn th 4612 emb 9 4613 atext i true 4614 emb 9 4615 akey i false 4616 emb 9 4617 emn get avol i 4618 emb 10 4619 end 4620 end 4621 fn aall json bool to void 4622 aemit 0 get avals 0 8 json 4623 aemit 1 get avals 1 200 json 4624 aemit 2 get avals 2 51200 json 4625 aemit 3 get avals 3 `1048576` json 4626 aemit 4 get avals 4 `1073741824` json 4627 aemit 5 get avals 5 52428 json 4628 aemit 6 get avals 6 mul rsvget 0 2 json 4629 aemit 7 get adelmax 0 52428 json 4630 aemit 8 get avals 8 mul rsvget 1 2 json 4631 aemit 9 get avals 9 209715 json 4632 aemit 10 get avals 10 get avals 11 json 4633 aemit 11 get avals 12 4096 json 4634 end 4635 note arefuse: a bring refused at the reserve of filesystem which: free bytes then, the volume it 4636 note asked for, its key; reported by the next alarm pass 4637 fn arefuse which ulong fb ulong vol ulong to void 4638 put aref 0 1 4639 put avals 10 fb 4640 put avals 11 rsvget which 4641 put avol 10 vol 4642 akset 10 4643 end 4644 fn alarms to void 4645 arows 4646 if ne get lerr 0 0 4647 aspace dfree 1 4648 ret 4649 end 4650 put avals 3 amax aqsize get aqmax 0 4651 put avol 3 get avals 3 4652 put aqmax 0 0 4653 put avals 4 aspool 4654 put avol 4 get avals 4 4655 put avals 5 aseed 4656 put avals 6 dfree 0 4657 put avol 6 rsvget 0 4658 put avals 8 dfree 1 4659 put avol 8 rsvget 1 4660 put avol 9 get avals 9 4661 put obl 0 0 4662 aall false 4663 if gt get obl 0 0 4664 w = write 2 kaprefix 0 len kaprefix 4665 w = write 2 ob 0 get obl 0 4666 end 4667 wpath kalarms 4668 ok = wr 4669 put obl 0 0 4670 emt kaj1 4671 aall true 4672 emt kaj7 4673 if pinopen 4674 rname kajson 4675 ok = wr 4676 unpin 4677 end 4678 aspace get avals 8 4679 end 4680 note aspace: space, what forged reads before it writes under open/: "free reserve 4681 note at " for the open dir's filesystem. Advisory for forged; the intake checks again. 4682 fn aspace fb ulong to void 4683 put obl 0 0 4684 emt kspfree 4685 emn fb 4686 emt kspres 4687 emn rsvget 1 4688 emt kspat 4689 emn ulong realns 4690 emb 10 4691 if pinopen 4692 rname kspace 4693 ok = wr 4694 unpin 4695 end 4696 end 4697 note ---- the reserves: the free bytes each filesystem keeps for the machine. The only refusal 4698 note for room on intake. reserve (~/forge-open-work/reserve on the Spark) may set 4699 note OPEN_RESERVE_STORAGE_BYTES (the filesystem of the Radicle storage and the work dir) and 4700 note OPEN_RESERVE_DATA_BYTES (the filesystem of the open dir: spool, downloads), one NAME=digits 4701 note line each; else the defaults below (measured, see docs/OPEN-BUILDS.md). A malformed line 4702 note keeps the default. 4703 note OPEN_RESERVE_STORAGE_BYTES, default 100 GB, protects what shares that filesystem on the Spark: 4704 note the live sites (forged and the site trees it serves), the Radicle storage and the hive builds. 4705 note It is sized to the measured swing of that filesystem on 2026-11-11: free space moved between 4706 note 178 and 184 GB within minutes (16:24-16:29Z) and 92 GB went between 14:31 and 16:22Z, so a 4707 note smaller reserve could be gone before a person looks. 4708 fn rsvget which ulong to ulong 4709 v = ulong `100000000000` 4710 if eq which 1 4711 v = ulong `1073741824` 4712 end 4713 wpath krsvf 4714 fd = fopen pth 0 get pthl 0 557056 0 4715 if lt fd 0 4716 ret v 4717 end 4718 b = new uchar 512 0 4719 k = read int fd b 0 512 4720 x = fclose int fd 4721 if le k 0 4722 ret v 4723 end 4724 a = ulong 0 4725 while lt a ulong k bound 512 4726 e = a 4727 while and lt e ulong k ne get b e 10 bound 512 4728 e = add e 1 4729 end 4730 r = rsvline b a e which 4731 if ge r 0 4732 v = ulong r 4733 end 4734 a = add e 1 4735 end 4736 ret v 4737 end 4738 note rsvline: b[a, e) is the line NAME=digits for this which: its value, else -1 4739 fn rsvline b view uchar 512 a ulong e ulong which ulong to long 4740 n = len krsvs 4741 if eq which 1 4742 n = len krsvd 4743 end 4744 if le sub e a n 4745 ret -1 4746 end 4747 for i ulong n bound 64 4748 c = get krsvs i 4749 if eq which 1 4750 c = get krsvd i 4751 end 4752 if ne get b add a i c 4753 ret -1 4754 end 4755 end 4756 if gt sub sub e a n 19 4757 ret -1 4758 end 4759 v = long 0 4760 for i ulong sub sub e a n bound 19 4761 c = get b add add a n i 4762 if or lt c 48 gt c 57 4763 ret -1 4764 end 4765 v = add mul v 10 long sub c 48 4766 end 4767 ret v 4768 end 4769 note rsvok: filesystem which keeps its reserve after need more bytes; else the refusal is 4770 note recorded for the alarm (free bytes, need, the current did) 4771 fn rsvok which ulong need ulong to bool 4772 f = dfree which 4773 r = rsvget which 4774 if and ge f r ge sub f r need 4775 put aref 0 0 4776 ret true 4777 end 4778 arefuse which f need 4779 ret false 4780 end