interplane.Get Omega
Read in your language

spark-connector

aien-dev/spark-connector

Brought by drakestapleton

branch main Branches Tags History

spark-connector / runner / 00base.om

479 lines · 10501 bytes Raw Blame History
1note runner/00base.om -- interplane-runner: state cells, texts and helpers (seeded Omega, OM-2).
2note Composed after the om/tls units, seed/net/http.om and seed/net/https.om (see build.sh).
3note Every name here starts with r: http.om reserves h*, H*; om/tls reserves its module prefixes.
4note A global is never passed as an argument (omc error 19), so helpers that fill a global copy
5note from a local array in a loop.
6
7note rgv: scalar cells, indexed by the constants below
8global rgv ulong 64
9note rph: the last path built by r_path / r_jpath; rhm: the state directory
10global rph uchar 4096
11global rhm uchar 4096
12note rhub: the hub URL as given; rhost: its host name
13global rhub uchar 512
14global rhost uchar 256
15note rmsg: the message being built; rrsp: the last response body. rrsp holds the largest salvage
16note record the hub can serve (the record format caps each field: 16 KiB source, 4 KiB stdin,
17note 4 KiB expected, and the text fields; under 32 KiB in all) with room to spare; a longer body is
18note refused as -413, never cut.
19global rmsg uchar 8192
20global rrsp uchar 65536
21note rsk: the private scalar; rpub: the public point 04 || X || Y; rpubh / rkid: their hex forms
22global rsk uchar 32
23global rpub uchar 65
24global rpubh uchar 130
25global rkid uchar 64
26note rjd: the current job directory; rjf: the job's text fields at the offsets rf* below
27global rjd uchar 4096
28global rjf uchar 512
29
30const rhml ulong 0
31const rphl ulong 1
32const rhstl ulong 2
33const rport ulong 3
34const rtls ulong 4
35const rmsgl ulong 5
36const rrspl ulong 6
37const rrate ulong 7
38const rwhy ulong 8
39const rcode ulong 9
40const rhubl ulong 10
41const rpost ulong 11
42const rjdl ulong 12
43const rexl ulong 13
44const rsrco ulong 14
45const rsrcn ulong 15
46const rstio ulong 16
47const rstin ulong 17
48
49note rjf offsets: job id 32, part 64, compiler 64, binary 64, stdout 64, exit text (rgv rexl bytes)
50const rfjob ulong 0
51const rfpart ulong 32
52const rfcomp ulong 96
53const rfbin ulong 160
54const rfout ulong 224
55const rfexit ulong 288
56
57note open flags (arm64): rdonly|nofollow|cloexec; wronly|creat|excl|nofollow|cloexec;
58note wronly|creat|trunc|nofollow|cloexec; wronly|creat|append|nofollow|cloexec;
59note rdonly|directory|nofollow|cloexec
60const rford int 557056
61const rfonew int 557249
62const rfotr int 557633
63const rfoapp int 558145
64const rfodir int 573440
65
66note the compiler every job must name and every check uses: omc of omega-mv f4df8239, the pin
67note of the interplane-forge salvage collector (run.sh PIN)
68text rtpin "3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194"
69text rhex "0123456789abcdef"
70text rtnl "\n"
71text rtsp " "
72text renvh "HOME="
73text renvr "INTERPLANE_RUNNER_HOME="
74text rdotd "/.interplane-runner"
75
76fn r_say t view uchar * to void
77w = write 1 t 0 len t
78end
79
80note r_dec: v in decimal into d at o; returns the offset after it
81fn r_dec d edit uchar * o ulong v ulong to ulong
82t = new uchar 20 0
83i = ulong 20
84x = v
85for it ulong 20
86i = sub i 1
87put t i uchar add rem x 10 48
88x = div x 10
89if eq x 0
90break
91end
92end
93k = o
94for j ulong sub 20 i bound 20
95put d k get t add i j
96k = add k 1
97end
98ret k
99end
100
101fn r_num v ulong to void
102d = new uchar 20 0
103n = r_dec d 0 v
104w = write 1 d 0 n
105end
106
107note r_undec: a decimal of 1..19 digits without a leading zero (or "0"); 2^64-1 when malformed
108fn r_undec s view uchar * to ulong
109bad = ulong 18446744073709551615
110if or eq len s 0 gt len s 19
111ret bad
112end
113if and gt len s 1 eq get s 0 48
114ret bad
115end
116v = ulong 0
117for i ulong len s bound 19
118c = get s i
119if or lt c 48 gt c 57
120ret bad
121end
122v = add mul v 10 sub ulong c 48
123end
124ret v
125end
126
127note r_hex: s as lowercase hex into d at o; returns the offset after it
128fn r_hex s view uchar * d edit uchar * o ulong to ulong
129for i ulong len s bound 8388608
130put d add o mul i 2 get rhex ulong shr get s i 4
131put d add add o mul i 2 1 get rhex ulong and get s i 15
132end
133ret add o mul len s 2
134end
135
136fn r_hv c uchar to ulong
137if and ge c 48 le c 57
138ret sub ulong c 48
139end
140if and ge c 97 le c 102
141ret sub ulong c 87
142end
143ret 16
144end
145
146note r_unhex: exactly 2 * len d lowercase hex characters into d
147fn r_unhex s view uchar * d edit uchar * to bool
148if ne len s mul len d 2
149ret false
150end
151for i ulong len d bound 4194304
152a = r_hv get s mul i 2
153b = r_hv get s add mul i 2 1
154if or gt a 15 gt b 15
155ret false
156end
157put d i uchar or shl a 4 b
158end
159ret true
160end
161
162note r_ishex: s is lowercase hex only
163fn r_ishex s view uchar * to bool
164for i ulong len s bound 16777216
165if gt r_hv get s i 15
166ret false
167end
168end
169ret true
170end
171
172fn r_eq a view uchar * b view uchar * to bool
173if ne len a len b
174ret false
175end
176for i ulong len a bound 16777216
177if ne get a i get b i
178ret false
179end
180end
181ret true
182end
183
184fn r_wipe b edit uchar * to void
185for i ulong len b bound 16777216
186put b i 0
187end
188end
189
190fn r_sha m view uchar * h edit uchar * to void
191c = new ulong 40 0
192sha256_init c
193sha256_update c m
194sha256_final c h
195end
196
197note r_envget: the value of the first environment entry that starts with pre, into d; -1 when absent
198fn r_envget pre view uchar * d edit uchar * to long
199b = new uchar 4096 0
200for i ulong ulong envcnt bound 1000000
201n = envread i b 0 4096
202if gt n long len pre
203if r_envcp part b 0 ulong n pre d
204ret sub n long len pre
205end
206end
207end
208ret -1
209end
210
211note r_envcp: b starts with pre and the rest fits d: copy the rest into d
212fn r_envcp b view uchar * pre view uchar * d edit uchar * to bool
213if not r_eq part b 0 len pre pre
214ret false
215end
216m = sub len b len pre
217if gt m len d
218ret false
219end
220for j ulong m bound 4096
221put d j get b add len pre j
222end
223ret true
224end
225
226note r_home: the state directory, $INTERPLANE_RUNNER_HOME when set, else $HOME/.interplane-runner;
227note it must be absolute and free of control bytes
228fn r_home to bool
229b = new uchar 4096 0
230n = r_envget renvr b
231if lt n 0
232n = r_envget renvh b
233if lt n 0
234ret false
235end
236if gt add ulong n len rdotd 4000
237ret false
238end
239for i ulong len rdotd
240put b add ulong n i get rdotd i
241end
242n = add n long len rdotd
243end
244if or lt n 2 gt n 4000
245ret false
246end
247if ne get b 0 47
248ret false
249end
250note the folder's own name must be .interplane-runner (also for INTERPLANE_RUNNER_HOME, which tests
251note use): everything the runner removes is inside a folder of that name
252if lt ulong n len rdotd
253ret false
254end
255if not r_eq part b sub ulong n len rdotd len rdotd rdotd
256ret false
257end
258for i ulong ulong n bound 4096
259c = get b i
260if or lt c 32 eq c 127
261ret false
262end
263put rhm i c
264end
265put rgv rhml ulong n
266ret true
267end
268
269note r_path: rph = state directory + "/" + nm (nm empty: the directory itself); returns the length
270fn r_path nm view uchar * to ulong
271n = get rgv rhml
272for i ulong n bound 4096
273put rph i get rhm i
274end
275if gt len nm 0
276put rph n 47
277for i ulong len nm bound 4000
278put rph add add n 1 i get nm i
279end
280n = add add n 1 len nm
281end
282put rgv rphl n
283ret n
284end
285
286note r_jpath: rph = job directory + "/" + nm
287fn r_jpath nm view uchar * to ulong
288n = get rgv rjdl
289for i ulong n bound 4096
290put rph i get rjd i
291end
292put rph n 47
293for i ulong len nm bound 4000
294put rph add add n 1 i get nm i
295end
296n = add add n 1 len nm
297put rgv rphl n
298ret n
299end
300
301note r_phcp: a copy of rph into d; returns its length
302fn r_phcp d edit uchar * to ulong
303n = get rgv rphl
304for i ulong n bound 4096
305put d i get rph i
306end
307ret n
308end
309
310note r_rd: the whole file at rph into b; its length, -27 when it is longer than b, or -errno
311fn r_rd b edit uchar * to long
312one = new uchar 1 0
313fd = fopen rph 0 get rgv rphl rford 0
314if lt fd 0
315ret fd
316end
317n = ulong 0
318rc = long 0
319while true bound 9223372036854775807
320if eq n len b
321r = read int fd one 0 1
322if ne r 0
323rc = -27
324end
325break
326end
327r = read int fd b n sub len b n
328if lt r 0
329rc = r
330break
331end
332if eq r 0
333break
334end
335n = add n ulong r
336end
337x = fclose int fd
338if lt rc 0
339ret rc
340end
341ret long n
342end
343
344note r_wr: write d to the file at rph opened with flags fl and mode md, then fsync
345fn r_wr d view uchar * fl int md int to bool
346fd = fopen rph 0 get rgv rphl fl md
347if lt fd 0
348ret false
349end
350n = ulong 0
351ok = true
352while lt n len d bound 9223372036854775807
353r = write int fd d n sub len d n
354if le r 0
355ok = false
356break
357end
358n = add n ulong r
359end
360s = fsync int fd
361x = fclose int fd
362ret and ok eq s 0
363end
364
365note r_pk: one packed-list record (u16 length, bytes) at o; returns the offset after it
366fn r_pk p edit uchar * o ulong s view uchar * to ulong
367put p o uchar and len s 255
368put p add o 1 uchar shr len s 8
369for i ulong len s bound 65536
370put p add add o 2 i get s i
371end
372ret add add o 2 len s
373end
374
375text rtnew ".new"
376note r_replace: write d to "<rph>.new" (0600), then rename it over rph
377fn r_replace d view uchar * to bool
378a = new uchar 4100 0
379b = new uchar 4100 0
380p = new uchar 8300 0
381n = r_phcp a
382for i ulong 4
383put b add n i get rtnew i
384end
385for i ulong n bound 4096
386put b i get a i
387end
388for i ulong add n 4 bound 4100
389put rph i get b i
390end
391put rgv rphl add n 4
392if not r_wr d rfotr 384
393ret false
394end
395put p 0 2
396put p 1 0
397k = r_pk p 2 part b 0 add n 4
398k = r_pk p k part a 0 n
399r = renames p 0 k 0
400ret eq r 0
401end
402
403note r_join: q = p + "/" + nm; returns its length (0 when it would not fit)
404fn r_join p view uchar * nm view uchar * q edit uchar * to ulong
405if gt add add len p 1 len nm len q
406ret 0
407end
408for i ulong len p bound 4096
409put q i get p i
410end
411put q len p 47
412for i ulong len nm bound 4096
413put q add add len p 1 i get nm i
414end
415ret add add len p 1 len nm
416end
417
418note r_dotdot: nm is "." or ".."
419fn r_dotdot nm view uchar * to bool
420if eq len nm 1
421ret eq get nm 0 46
422end
423if eq len nm 2
424ret and eq get nm 0 46 eq get nm 1 46
425end
426ret false
427end
428
429note r_rmtree: remove p and everything below it, never following a symbolic link (a link is removed
430note as an entry). True when p is gone afterwards.
431fn r_rmtree p view uchar * to bool
432b = new uchar 8192 0
433q = new uchar 4096 0
434fd = fopen p 0 len p rfodir 0
435if lt fd 0
436r = unlink p 0 len p
437ret or eq r 0 eq r -2
438end
439while true bound 9223372036854775807
440n = getdnts int fd b 0 8192
441if le n 0
442break
443end
444o = ulong 0
445while lt o ulong n bound 8192
446o = add o r_rment p part b o sub ulong n o q
447end
448end
449x = fclose int fd
450r = rmdir p 0 len p
451ret or eq r 0 eq r -2
452end
453
454note r_rment: one getdents64 record at the start of d below the directory p; returns the record
455note length (a removal that fails leaves the directory non-empty, so the caller's rmdir fails)
456fn r_rment p view uchar * d view uchar * q edit uchar * to ulong
457rl = or ulong get d 16 shl ulong get d 17 8
458ty = get d 18
459nl = ulong 0
460while lt add 19 nl rl bound 65536
461if eq get d add 19 nl 0
462break
463end
464nl = add nl 1
465end
466if r_dotdot part d 19 nl
467ret rl
468end
469k = r_join p part d 19 nl q
470if eq k 0
471ret rl
472end
473if or eq ty 4 eq ty 0
474x = r_rmtree part q 0 k
475ret rl
476end
477r = unlink q 0 k
478ret rl
479end