interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / interplane-forge / server / util.om

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