interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / open / intake.om

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