interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / collab / native / write.om

841 lines · 13148 bytes Raw Blame History
1note Native issue writes. The author comes from the session established by SSH key proof. The
2note repository lock serializes replacements of its index and threads, even across server processes.
3note fopen flags below use Linux AArch64 values (O_CLOEXEC 524288, O_NOFOLLOW 32768).
4text tniwnp "/issues/new"
5text tniwlog "login"
6text tniwlink "<p><a href=\""
7text tniwnewa "\">New issue</a></p>\n"
8text tniwttl "New issue"
9text tniwform "<section class=\"box\" id=\"comment-form\">"
10text tniwsign "<p><a href=\""
11text tniwsgc "\">Sign in to comment</a></p>"
12text tniwsgn "\">Sign in to open an issue</a></p>"
13text tniwfa "<form method=\"post\" action=\""
14text tniwcs "\"><input type=\"hidden\" name=\"csrf\" value=\""
15text tniwrh "\"><input type=\"hidden\" name=\"request\" value=\""
16text tniwqt "\">"
17text tniwtf "<p><label>Title<br><input name=\"title\" required maxlength=\"256\"></label></p>"
18text tniwdf "<p><label>Description (Markdown)<br><textarea name=\"body\" rows=\"8\" maxlength=\"16000\"></textarea></label></p>"
19text tniwcf "<p><label>Comment (Markdown)<br><textarea name=\"body\" rows=\"6\" required maxlength=\"16000\"></textarea></label></p>"
20text tniwbn "<p><button type=\"submit\">Open issue</button></p>"
21text tniwbc "<p><button type=\"submit\">Post comment</button></p>"
22text tniwfe "</form></section>\n"
23text tniwrq "request"
24text tniwrql "request "
25text tniwmrq "Reload the form before sending it again."
26text tniwfs "issue store: directory sync failed after publication\n"
27text tniwfsch "forge/issue-form/1"
28text tniwwsch "forge/issue-write/1"
29text tniwact "action"
30text tniwcsrf "csrf"
31text tniwappl "applied"
32text tniwft "title"
33text tniwfb "body"
34text tniwbadt "Enter a single-line title of 1 to 256 UTF-8 bytes."
35text tniwbadb "Enter valid Markdown text of at most 16000 UTF-8 bytes. A comment cannot be empty."
36text tniwopa "open\n"
37text tniwlock ".write.lock"
38text tniwist ".index.tmp"
39text tniwtht ".thread.tmp"
40text tniwstg ".new-"
41text tniwdir "issues"
42text tniwemp ""
43global gniwid uchar 16
44global gniwtt uchar 256
45global gniwtl ulong 1
46global gniwrq uchar 32
47
48fn niwlink to void
49fput tniwlink
50fputrl
51fput tniwnp
52fput tniwnewa
53end
54fn niwnewjson to void
55fputc 34
56fputrl
57fput tniwnp
58fputc 34
59end
60fn niwform k ulong opening bool to void
61fput tniwform
62if eq get gscur 0 0
63fput tniwsign
64fputb
65fput tniwlog
66if opening
67fput tniwsgn
68else
69fput tniwsgc
70end
71fput tnsece
72ret
73end
74hex = new uchar 32 0
75if not niwtoken hex
76fput tniwmrq
77fput tnsece
78ret
79end
80fput tniwfa
81if opening
82fputrl
83fput tniwnp
84else
85nilink k
86fput tncoms
87end
88fput tniwcs
89for j ulong get fcsrfl 0 bound 64
90fputc get fcsrf j
91end
92fput tniwrh
93for j ulong 32
94fputc get hex j
95end
96fput tniwqt
97if opening
98fput tniwtf
99fput tniwdf
100fput tniwbn
101else
102fput tniwcf
103fput tniwbc
104end
105fput tniwfe
106end
107fn niwtoken hex edit uchar 32 to bool
108raw = new uchar 16 0
109if not sorand raw
110ret false
111end
112sohex raw hex
113ret true
114end
115fn niwlogin to long
116put fkind 0 2
117flocb
118floct tniwlog
119ret 303
120end
121fn niwjsform k ulong opening bool to long
122if eq get gscur 0 0
123ret niwlogin
124end
125hex = new uchar 32 0
126if not niwtoken hex
127ret 500
128end
129fyrhead tniwfsch
130fyk tniwact
131cojq
132if opening
133fputrl
134fput tniwnp
135else
136nilink k
137fput tncoms
138end
139cojq
140fyk tniwcsrf
141fjtb
142for i ulong get fcsrfl 0 bound 64
143fjtc get fcsrf i
144end
145fjstr
146fyk tniwrq
147fysv hex
148fyend
149ret 200
150end
151fn niwcform to long
152if not fjwant
153ret 405
154end
155if eq get gscur 0 0
156ret niwlogin
157end
158if not niidx
159ret 500
160end
161k = niref
162if ge k get gnn 0
163ret 404
164end
165ret niwjsform k false
166end
167fn niwresult applied bool to long
168if fjwant
169fyrhead tniwwsch
170fyk tcjpath
171cojq
172fputrl
173fput tcissp
174for i ulong 16
175fputc get gniwid i
176end
177cojq
178fyk tniwappl
179fybool applied
180fyend
181if applied
182ret 201
183end
184ret 200
185end
186put fkind 0 2
187flocrepo
188floct tcissp
189for i ulong 16
190flocc get gniwid i
191end
192ret 303
193end
194fn niwnew to long
195if fjwant
196ret niwjsform 0 true
197end
198fsetti tniwttl
199frepohd 6
200niwform 0 true
201ret 200
202end
203fn niwerr t view uchar * to long
204if fjwant
205fyerr 400
206note The common error envelope is used by all machine clients.
207else
208frepohd 6
209fput tnbox
210fput t
211fput tnmetae
212fput tnsece
213end
214ret 400
215end
216fn niwfields opening bool to long
217if ne cobfld tniwrq 32
218ret niwerr tniwmrq
219end
220for i ulong 32
221c = get gcfv i
222if lt sohexv c 0
223ret niwerr tniwmrq
224end
225put gniwrq i c
226end
227if opening
228n = cobfld tniwft
229if or lt n 1 gt n 256
230ret niwerr tniwbadt
231end
232n = cotxtok ulong n false
233if lt n 1
234ret niwerr tniwbadt
235end
236put gniwtl 0 ulong n
237for i ulong ulong n bound 256
238put gniwtt i get gcfv i
239end
240end
241b = cobfld tniwfb
242if eq b -1
243b = 0
244end
245if lt b 0
246ret niwerr tniwbadb
247end
248b = cotxtok ulong b true
249if or or lt b 0 gt b 16000 and not opening eq b 0
250ret niwerr tniwbadb
251end
252put gcfvl 0 ulong b
253ret 0
254end
255fn niwpost opening bool to long
256r = sowpre
257if ne r 0
258if and fjwant ne r 303
259put fobl 0 0
260put fkind 0 0
261end
262ret r
263end
264soratev get gscur 0 get gsnow 0
265r = niwfields opening
266if ne r 0
267ret r
268end
269fd = niwacq opening
270if lt fd 0
271if eq fd -2
272ret 404
273end
274if eq fd -11
275put fretry 0 1
276ret 503
277end
278ret 500
279end
280if opening
281r = niwcreate
282else
283r = niwcomment
284end
285x = fclose int fd
286if gt r 0
287ret r
288end
289ret niwresult eq r 0
290end
291
292note ---- Storage
293fn niwdirs to bool
294fpb
295fpt tfdata
296fpt tniwdir
297if not niwmkdir
298ret false
299end
300fpc 47
301fpt tfowner
302if not niwmkdir
303ret false
304end
305fpc 47
306i = get fridx 0
307for k ulong get fanl i bound 128
308fpc frnbyte i k
309end
310ret niwmkdir
311end
312fn niwmkdir to bool
313r = makedir fpth 0 get fpthl 0 448
314ret or eq r 0 eq r -17
315end
316fn niwacq opening bool to long
317if opening
318if not niwdirs
319ret -5
320end
321end
322nipath
323fpt tniwlock
324fd = fopen fpth 0 get fpthl 0 557121 384
325if lt fd 0
326ret fd
327end
328r = flock int fd 6
329if ne r 0
330x = fclose int fd
331ret r
332end
333ret fd
334end
335fn niwpath stage bool file view uchar * to void
336nipath
337if stage
338fpt tniwstg
339end
340for i ulong 16
341fpc get gniwid i
342end
343if ne len file 0
344fpc 47
345fpt file
346end
347end
348fn niwarg to void
349soavopen
350for i ulong get fpthl 0 bound 4096
351soavc get fpth i
352end
353end
354note Source selectors: response buffer (0), file read buffer (1), title (2), form body (3), open status (4).
355fn niwwrite fd int src ulong p ulong n ulong to long
356if eq src 0
357ret write fd fob p n
358end
359if eq src 1
360ret write fd fxo p n
361end
362if eq src 2
363ret write fd gniwtt p n
364end
365if eq src 3
366ret write fd gcfv p n
367end
368ret write fd tniwopa p n
369end
370fn niwout fd int src ulong size ulong to bool
371p = ulong 0
372for i ulong 100000
373if eq p size
374ret true
375end
376n = niwwrite fd src p sub size p
377if gt n 0
378p = add p ulong n
379else
380if ne n -4
381ret false
382end
383end
384end
385ret false
386end
387fn niwput src ulong size ulong tail ulong to bool
388fd = fopen fpth 0 get fpthl 0 557249 384
389if lt fd 0
390ret false
391end
392ok = niwout int fd src size
393if and ok gt tail 0
394ok = niwout int fd 0 tail
395end
396if ok
397ok = eq fsync int fd 0
398end
399r = fclose int fd
400if or not ok ne r 0
401x = unlink fpth 0 get fpthl 0
402ret false
403end
404ret true
405end
406note Read a whole file into fxo under the write lock. Distinguish missing from other I/O errors.
407fn niwread cap ulong to long
408fd = fopen fpth 0 get fpthl 0 559104 0
409if lt fd 0
410ret fd
411end
412n = niwreadfd int fd cap
413r = fclose int fd
414ret n
415end
416fn niwreadfd fd int cap ulong to long
417p = ulong 0
418for i ulong 100000
419if gt p cap
420ret -27
421end
422n = read fd fxo p sub add cap 1 p
423if eq n 0
424ret long p
425end
426if gt n 0
427p = add p ulong n
428else
429if ne n -4
430ret n
431end
432end
433end
434ret -5
435end
436fn niwremove file view uchar * stage bool to void
437niwpath stage file
438x = unlink fpth 0 get fpthl 0
439end
440fn niwdiscard stage bool to void
441niwremove tnfttl stage
442niwremove tnfdsc stage
443niwremove tnfsts stage
444niwremove tniwnat stage
445niwpath stage tniwemp
446x = rmdir fpth 0 get fpthl 0
447end
448note Use the authenticated DID and the form token as a stable issue ID across retries.
449fn niwnewid to void
450seed = new uchar 160 0
451hash = new uchar 32 0
452hex = new uchar 16 0
453for i ulong 32
454put seed i get gniwrq i
455end
456d = get gscur 0
457o = get gsso d
458n = get gssl d
459for i ulong n bound 128
460put seed add i 32 get gsstr add o i
461end
462x = sha2_digest 256 part seed 0 add 32 n hash
463sohex part hash 0 8 hex
464for i ulong 16
465put gniwid i get hex i
466end
467end
468fn niwreq to void
469for i ulong 32
470fputc get gniwrq i
471end
472end
473fn niwmeta to void
474put fobl 0 0
475fput tniwdid
476niwactor
477fputc 10
478fput tnghop
479fjisob get gsnow 0
480fputc 10
481fput tniwrql
482niwreq
483fputc 10
484end
485fn niwactor to void
486d = get gscur 0
487o = get gsso d
488for i ulong get gssl d bound 128
489fputc get gsstr add o i
490end
491end
492fn niwnewfiles to bool
493niwpath true tnfttl
494if not niwput 2 get gniwtl 0 0
495ret false
496end
497niwpath true tnfdsc
498if not niwput 3 get gcfvl 0 0
499ret false
500end
501niwpath true tnfsts
502if not niwput 4 len tniwopa 0
503ret false
504end
505niwmeta
506niwpath true tniwnat
507ok = niwput 0 get fobl 0 0
508put fobl 0 0
509if not ok
510ret false
511end
512ret niwsync true
513end
514fn niwsync stage bool to bool
515niwpath stage tniwemp
516ret niwsyncat
517end
518fn niwsyncat to bool
519fd = fopen fpth 0 get fpthl 0 573440 0
520if lt fd 0
521ret false
522end
523r = fsync int fd
524x = fclose int fd
525ret eq r 0
526end
527note Publish the complete issue before replacing its index. After a crash between the renames the
528note unindexed directory is unreachable. Retrying the same request repairs its index entry.
529fn niwcreate to long
530niwnewid
531nipath
532fpt tnindex
533n = niwread len gnix
534if eq n -2
535n = 0
536end
537if eq n -27
538ret 409
539end
540if or lt n 0 ne rem ulong n 17 0
541ret 500
542end
543for j ulong div ulong n 17 bound NIROWS
544p = mul j 17
545if not niidok p
546ret 500
547end
548if niwrow p
549if niwexisting
550ret -1
551end
552ret 409
553end
554end
555if eq n long len gnix
556ret 409
557end
558for i ulong 16
559put fob i get gniwid i
560end
561put fob 16 10
562nipath
563fpt tniwist
564x = unlink fpth 0 get fpthl 0
565if not niwput 1 ulong n 17
566ret 500
567end
568r = niwinst
569if gt r 0
570nipath
571fpt tniwist
572x = unlink fpth 0 get fpthl 0
573end
574ret r
575end
576fn niwrow p ulong to bool
577for i ulong 16
578if ne get fxo add p i get gniwid i
579ret false
580end
581end
582ret true
583end
584fn niwexisting to bool
585niwpath false tniwnat
586r = niwread 256
587if le r 0
588ret false
589end
590for i ulong ulong r bound 256
591put gngh i get fxo i
592end
593put gnghl 0 ulong r
594if not niwmetasame
595ret false
596end
597ret niwsame
598end
599fn niwmetasame to bool
600d = get gscur 0
601o = get gsso d
602a = nighv tniwdid
603if ne get gnr 15 get gssl d
604ret false
605end
606for i ulong get gssl d bound 128
607if ne get gngh add a i get gsstr add o i
608ret false
609end
610end
611b = nighv tniwrql
612if ne get gnr 15 32
613ret false
614end
615for i ulong 32
616if ne get gngh add b i get gniwrq i
617ret false
618end
619end
620ret true
621end
622fn niwsame to bool
623niwpath false tnfttl
624if ne niwread 256 long get gniwtl 0
625ret false
626end
627for i ulong get gniwtl 0 bound 256
628if ne get fxo i get gniwtt i
629ret false
630end
631end
632niwpath false tnfdsc
633if ne niwread 16000 long get gcfvl 0
634ret false
635end
636for i ulong get gcfvl 0 bound 16000
637if ne get fxo i get gcfv i
638ret false
639end
640end
641ret true
642end
643fn niwinst to long
644niwpath false tniwemp
645r = stat fpth 0 get fpthl 0 fstb 0 128
646if eq r -2
647if not niwstage
648ret 500
649end
650else
651if lt r 0
652ret 500
653end
654if not niwexisting
655ret 409
656end
657end
658nipath
659if not niwsyncat
660ret 500
661end
662soavb
663nipath
664fpt tniwist
665niwarg
666nipath
667fpt tnindex
668niwarg
669if ne renames gspk 0 get gspkl 0 0 0
670ret 500
671end
672nipath
673if not niwsyncat
674flog tniwfs
675end
676ret 0
677end
678fn niwstage to bool
679note Discard remnants of an interrupted attempt of this same unindexed action.
680niwdiscard true
681niwpath true tniwemp
682if ne makedir fpth 0 get fpthl 0 448 0
683ret false
684end
685if not niwnewfiles
686niwdiscard true
687ret false
688end
689soavb
690niwpath true tniwemp
691niwarg
692niwpath false tniwemp
693niwarg
694if ne renames gspk 0 get gspkl 0 1 0
695niwdiscard true
696ret false
697end
698ret true
699end
700fn niwrecord to void
701put fobl 0 0
702fput tniwncom
703cosp
704fjisob get gsnow 0
705cosp
706niwactor
707cosp
708niwreq
709cosp
710n = get gcfvl 0
711fnum n
712fputc 10
713for i ulong n bound 16000
714fputc get gcfv i
715end
716fputc 10
717end
718fn niwcomment to long
719for i ulong 16
720put gniwid i get gcrq i
721end
722r = niwindexed
723if ne r 0
724ret r
725end
726niwpath false tnfthr
727n = niwread 1048576
728if eq n -2
729n = 0
730end
731if eq n -27
732ret 409
733end
734if lt n 0
735ret 500
736end
737r = niwthread ulong n
738if ne r 0
739ret r
740end
741niwrecord
742if gt add ulong n get fobl 0 1048576
743put fobl 0 0
744ret 409
745end
746niwpath false tniwtht
747x = unlink fpth 0 get fpthl 0
748ok = niwput 1 ulong n get fobl 0
749put fobl 0 0
750if not ok
751ret 500
752end
753soavb
754niwarg
755niwpath false tnfthr
756niwarg
757if ne renames gspk 0 get gspkl 0 0 0
758niwremove tniwtht false
759ret 500
760end
761if not niwsync false
762flog tniwfs
763end
764ret 0
765end
766fn niwindexed to long
767nipath
768fpt tnindex
769n = niwread len gnix
770if eq n -2
771ret 404
772end
773if or lt n 0 ne rem ulong n 17 0
774ret 500
775end
776for j ulong div ulong n 17 bound NIROWS
777p = mul j 17
778if and niidok p niwrow p
779ret 0
780end
781end
782ret 404
783end
784note Check the whole prefix before replacing it. An already-published form returns its prior result.
785fn niwthread n ulong to long
786for i ulong n bound 1048576
787put gnth i get fxo i
788end
789put gnthl 0 n
790p = ulong 0
791while lt p n bound 1048576
792p = nirec p
793k = get gnr 0
794if eq k 0
795ret 409
796end
797if eq k 4
798if niwcomreq
799if niwcombody
800ret -1
801end
802ret 409
803end
804end
805end
806ret 0
807end
808fn niwcomreq to bool
809d = get gscur 0
810n = get gssl d
811if or ne get gnr 4 n ne get gnr 6 32
812ret false
813end
814a = get gnr 3
815o = get gsso d
816for i ulong n bound 128
817if ne get gnth add a i get gsstr add o i
818ret false
819end
820end
821b = get gnr 5
822for i ulong 32
823if ne get gnth add b i get gniwrq i
824ret false
825end
826end
827ret true
828end
829fn niwcombody to bool
830n = get gcfvl 0
831if ne n get gnr 11
832ret false
833end
834a = get gnr 10
835for i ulong n bound 16000
836if ne get gnth add a i get gcfv i
837ret false
838end
839end
840ret true
841end