interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / server / util.om

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