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 / seed / trace / segment.om

424 lines · 8727 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note seed/trace/segment.om: local trace segment, format version 1 (omega-mv #608). Library unit with no
3note main: concatenate it before a program that defines main (units concatenate; there is no import).
4note Header, 16 bytes: "AITR", version u16 = 1, record size u16 = 64, header size u32 = 16, reserved
5note u32 = 0. Record, 64 bytes: type u16, payload length u16 (at most 52), sequence u32 (= record index,
6note assigned by the writer), 52 payload bytes (zero past the length), CRC-32 u32 of bytes 0..59.
7note CRC-32 is IEEE 802.3, reflected polynomial 3988292384 (0xEDB88320), initial value and final xor
8note 4294967295. Every integer is little-endian. Single writer. No network, consent, export, index,
9note rotation or retention in this unit. The caller puts the segment path in tpath and its length in
10note tlen; sibling paths are the same path plus ".bad" (quarantine) or ".new" (recovery staging).
11note Opens carry O_NOFOLLOW (32768), so a symbolic link in the last path component is refused.
12global thdr uchar 16
13global trec uchar 64
14global tpath uchar 1024
15global tlen ulong 1
16global opath uchar 1040
17global olen ulong 1
18global tpkt uchar 2100
19global tn ulong 1
20global pay uchar 52
21global scount ulong 1
22global sreason ulong 1
23global stotal ulong 1
24global slost ulong 1
25global sseq ulong 1
26global serrno long 1
27text sbadsfx ".bad"
28text snewsfx ".new"
29note Error codes (function results): 0 ok; 10 open refused (missing, exists on create, symlink:
30note serrno holds -errno); 11 short header or bad magic; 12 close failed; 13 header write/sync failed;
31note 15 unsupported version; 16 bad geometry or nonzero reserved bytes; 17 read error; 20 payload over
32note 52 bytes; 21 append open failed; 22 short record write (a torn tail may now exist); 23 fsync;
33note 24 close; 25 segment length is not header + whole records (torn tail: recover first); 26 segment
34note full (4096 records); 27 seek failed; 30 staging file; 31 copy failed; 32 quarantine link refused
35note (segment.bad already exists or link failed); 33 rename of the staged segment failed.
36note Scan reasons (sreason): 0 clean; 1 torn tail (partial record); 2 payload length over 52;
37note 3 CRC mismatch; 4 sequence is not the record index; 5 more than 4096 records.
38fn shut fd long to void
39c = fclose int fd
40if lt c 0
41put serrno 0 c
42end
43end
44fn sidepath k ulong to void
45n = get tlen 0
46for i ulong n
47put opath i get tpath i
48end
49if eq k 1
50for i ulong len sbadsfx
51put opath add n i get sbadsfx i
52end
53put olen 0 add n len sbadsfx
54else
55for i ulong len snewsfx
56put opath add n i get snewsfx i
57end
58put olen 0 add n len snewsfx
59end
60end
61fn tput16 v ulong to void
62put tpkt get tn 0 uchar and v 255
63put tpkt add get tn 0 1 uchar and shr v 8 255
64put tn 0 add get tn 0 2
65end
66fn pkttop to void
67tput16 get tlen 0
68for i ulong get tlen 0
69put tpkt add get tn 0 i get tpath i
70end
71put tn 0 add get tn 0 get tlen 0
72end
73fn pktoth to void
74tput16 get olen 0
75for i ulong get olen 0
76put tpkt add get tn 0 i get opath i
77end
78put tn 0 add get tn 0 get olen 0
79end
80fn segcrc n ulong to ulong
81c = ulong 4294967295
82for i ulong n
83c = xor c ulong get trec i
84for k ulong 8
85if eq and c 1 1
86c = xor shr c 1 3988292384
87else
88c = shr c 1
89end
90end
91end
92ret xor c 4294967295
93end
94fn ld16 o ulong to ulong
95ret or ulong get trec o shl ulong get trec add o 1 8
96end
97fn ld32 o ulong to ulong
98lo = ld16 o
99hi = ld16 add o 2
100ret or lo shl hi 16
101end
102fn st32 o ulong v ulong to void
103put trec o uchar and v 255
104put trec add o 1 uchar and shr v 8 255
105put trec add o 2 uchar and shr v 16 255
106put trec add o 3 uchar and shr v 24 255
107end
108fn segseal to void
109st32 60 segcrc 60
110end
111fn segpack t ulong n ulong seq ulong to void
112for i ulong 64
113put trec i 0
114end
115put trec 0 uchar and t 255
116put trec 1 uchar and shr t 8 255
117put trec 2 uchar and n 255
118put trec 3 uchar and shr n 8 255
119st32 4 seq
120for i ulong n
121put trec add 8 i get pay i
122end
123segseal
124end
125fn mkheader to void
126for i ulong 16
127put thdr i 0
128end
129put thdr 0 65
130put thdr 1 73
131put thdr 2 84
132put thdr 3 82
133put thdr 4 1
134put thdr 6 64
135put thdr 8 16
136end
137fn hdrcheck to ulong
138if or ne get thdr 0 65 ne get thdr 1 73
139ret 11
140end
141if or ne get thdr 2 84 ne get thdr 3 82
142ret 11
143end
144if or ne get thdr 4 1 ne get thdr 5 0
145ret 15
146end
147if or ne get thdr 6 64 ne get thdr 7 0
148ret 16
149end
150if ne get thdr 8 16
151ret 16
152end
153for i ulong 7
154if ne get thdr add 9 i 0
155ret 16
156end
157end
158ret 0
159end
160note segcreate refuses an existing path (O_EXCL) instead of truncating it: data is never overwritten.
161fn segcreate to ulong
162mkheader
163fd = fopen tpath 0 get tlen 0 32961 384
164if lt fd 0
165put serrno 0 fd
166ret 10
167end
168n = write int fd thdr 0 16
169if ne n 16
170shut fd
171ret 13
172end
173if ne fsync int fd 0
174shut fd
175ret 13
176end
177if ne fclose int fd 0
178ret 12
179end
180ret 0
181end
182note segready creates a missing segment and otherwise requires a valid header; it never recreates one.
183fn segready to ulong
184fd = fopen tpath 0 get tlen 0 32768 0
185if eq fd -2
186ret segcreate
187end
188if lt fd 0
189put serrno 0 fd
190ret 10
191end
192n = read int fd thdr 0 16
193if ne fclose int fd 0
194ret 12
195end
196if lt n 0
197ret 17
198end
199if ne n 16
200ret 11
201end
202ret hdrcheck
203end
204fn segput fd long t ulong n ulong to ulong
205pos = fseek int fd 0 2
206if lt pos 16
207ret 27
208end
209r = sub ulong pos 16
210if ne rem r 64 0
211ret 25
212end
213q = div r 64
214if ge q 4096
215ret 26
216end
217segpack t n q
218put sseq 0 q
219m = write int fd trec 0 64
220if ne m 64
221ret 22
222end
223if ne fsync int fd 0
224ret 23
225end
226ret 0
227end
228note segappend appends one record of type t with payload pay[0..n); the sequence is the record index.
229fn segappend t ulong n ulong to ulong
230if gt n 52
231ret 20
232end
233e = segready
234if ne e 0
235ret e
236end
237fd = fopen tpath 0 get tlen 0 33793 0
238if lt fd 0
239put serrno 0 fd
240ret 21
241end
242e = segput fd t n
243if ne e 0
244shut fd
245ret e
246end
247if ne fclose int fd 0
248ret 24
249end
250ret 0
251end
252fn recok idx ulong to ulong
253if gt ld16 2 52
254ret 2
255end
256want = ld32 60
257if ne want segcrc 60
258ret 3
259end
260if ne ld32 4 idx
261ret 4
262end
263ret 0
264end
265note Each pass reads one record or ends; at most 4097 full records plus the final read: bound 4098.
266fn scanrecs fd long to ulong
267go = true
268while go bound 4098
269m = read int fd trec 0 64
270if lt m 0
271ret 17
272end
273if eq m 0
274go = false
275else
276if lt m 64
277put sreason 0 1
278go = false
279else
280if ge get scount 0 4096
281put sreason 0 5
282go = false
283else
284r = recok get scount 0
285if ne r 0
286put sreason 0 r
287go = false
288else
289put scount 0 add get scount 0 1
290end
291end
292end
293end
294end
295ret 0
296end
297note segscan is read-only. scount = complete valid records before the first damage, sreason = why the
298note scan stopped, stotal = file bytes, slost = bytes after the valid prefix (what recovery quarantines).
299fn segscan to ulong
300put scount 0 0
301put sreason 0 0
302put stotal 0 0
303put slost 0 0
304fd = fopen tpath 0 get tlen 0 32768 0
305if lt fd 0
306put serrno 0 fd
307ret 10
308end
309n = read int fd thdr 0 16
310if ne n 16
311shut fd
312ret 11
313end
314e = hdrcheck
315if ne e 0
316shut fd
317ret e
318end
319pos = fseek int fd 0 2
320p2 = fseek int fd 16 0
321if or lt pos 16 ne p2 16
322shut fd
323ret 27
324end
325put stotal 0 ulong pos
326e = scanrecs fd
327if ne fclose int fd 0
328ret 12
329end
330used = add 16 mul get scount 0 64
331put slost 0 sub get stotal 0 used
332ret e
333end
334fn copyrecs src long dst long to ulong
335mkheader
336w = write int dst thdr 0 16
337if ne w 16
338ret 31
339end
340p = fseek int src 16 0
341if ne p 16
342ret 31
343end
344for i ulong get scount 0
345m = read int src trec 0 64
346if ne m 64
347ret 31
348end
349w2 = write int dst trec 0 64
350if ne w2 64
351ret 31
352end
353end
354if ne fsync int dst 0
355ret 31
356end
357ret 0
358end
359note stage writes header + the valid prefix to segment.new (a stale .new from an interrupted recovery is
360note removed first; the segment itself is untouched until the final rename).
361fn stage to ulong
362sidepath 2
363u = unlink opath 0 get olen 0
364if and lt u 0 ne u -2
365put serrno 0 u
366ret 30
367end
368src = fopen tpath 0 get tlen 0 32768 0
369if lt src 0
370put serrno 0 src
371ret 30
372end
373dst = fopen opath 0 get olen 0 32961 384
374if lt dst 0
375put serrno 0 dst
376shut src
377ret 30
378end
379e = copyrecs src dst
380shut src
381if ne fclose int dst 0
382ret 31
383end
384ret e
385end
386note segrecover: a damaged record area is never repaired in place. The valid prefix is staged, the damaged
387note file is hard-linked to segment.bad (refused if that exists: a quarantine is never overwritten), then
388note the staged file is renamed over the segment. At every step the segment path names either the old
389note damaged file or the complete new one. Returns 0 clean, 1 recovered (slost bytes quarantined), or an
390note error code. A bad header is not recovered automatically (segscan's error is returned).
391fn segrecover to ulong
392e = segscan
393if ne e 0
394ret e
395end
396if eq get sreason 0 0
397ret 0
398end
399e = stage
400if ne e 0
401ret e
402end
403sidepath 1
404put tn 0 0
405tput16 2
406pkttop
407pktoth
408lk = link tpkt 0 get tn 0
409if lt lk 0
410put serrno 0 lk
411ret 32
412end
413sidepath 2
414put tn 0 0
415tput16 2
416pktoth
417pkttop
418rr = renames tpkt 0 get tn 0 0
419if lt rr 0
420put serrno 0 rr
421ret 33
422end
423ret 1
424end