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 / authority / store.om

184 lines · 3348 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Durable 32-word authority record store. Caller supplies the record and generation.
3note Writes a temporary 256-byte file, fsyncs it, then atomically renames it into place.
4note Reads require exactly 256 bytes; a short or torn file is refused. Store calls do not lock; the
5note dispatcher holds lock.om around its read-check-persist window.
6text recp "build/authority-store/rec"
7text tmpp "build/authority-store/tmp"
8text genp "build/authority-store/gen"
9global sb uchar 256
10global stail uchar 1
11global spkt uchar 256
12global sn ulong 1
13global spath uchar 128
14global splen ulong 1
15global sgerr ulong 1
16fn put16 v ulong to void
17put spkt get sn 0 uchar and v 255
18put spkt add get sn 0 1 uchar shr v 8
19put sn 0 add get sn 0 2
20end
21fn setrec to void
22put splen 0 len recp
23for i ulong len recp
24put spath i get recp i
25end
26end
27fn settmp to void
28put splen 0 len tmpp
29for i ulong len tmpp
30put spath i get tmpp i
31end
32end
33fn setgen to void
34put splen 0 len genp
35for i ulong len genp
36put spath i get genp i
37end
38end
39fn pack2 to ulong
40put sn 0 0
41put16 2
42put16 len tmpp
43for i ulong len tmpp
44put spkt add get sn 0 i get tmpp i
45end
46put sn 0 add get sn 0 len tmpp
47put16 len recp
48for i ulong len recp
49put spkt add get sn 0 i get recp i
50end
51put sn 0 add get sn 0 len recp
52ret get sn 0
53end
54fn enc r view ulong 32 to void
55for i ulong 32
56v = get r i
57for j ulong 8
58put sb add mul i 8 j uchar and shr v mul j 8 255
59end
60end
61end
62fn dec r edit ulong 32 to void
63for i ulong 32
64v = ulong 0
65for j ulong 8
66v = or v shl ulong get sb add mul i 8 j mul j 8
67end
68put r i v
69end
70end
71note p10 effect
72fn writerec r view ulong 32 to ulong
73enc r
74settmp
75fd = fopen spath 0 get splen 0 577 384
76if lt fd 0
77ret 10
78end
79n = write int fd sb 0 256
80if ne n 256
81note p10 allow E1 primary write error wins over cleanup close error
82cleanup = fclose int fd
83ret 11
84end
85if ne fsync int fd 0
86note p10 allow E1 primary sync error wins over cleanup close error
87cleanup = fclose int fd
88ret 12
89end
90if ne fclose int fd 0
91ret 13
92end
93pn = pack2
94rr = renames spkt 0 pn 0
95if lt rr 0
96ret 14
97end
98ret 0
99end
100note p10 effect
101fn readrec r edit ulong 32 to ulong
102setrec
103fd = fopen spath 0 get splen 0 0 0
104if lt fd 0
105ret 20
106end
107n = read int fd sb 0 256
108extra = long -1
109if eq n 256
110extra = read int fd stail 0 1
111end
112if ne fclose int fd 0
113ret 21
114end
115if or ne n 256 ne extra 0
116ret 22
117end
118dec r
119ret 0
120end
121note p10 effect
122fn readgen to ulong
123put sgerr 0 0
124setgen
125fd = fopen spath 0 get splen 0 0 0
126if lt fd 0
127put sgerr 0 30
128ret 0
129end
130n = read int fd sb 0 8
131extra = long -1
132if eq n 8
133extra = read int fd stail 0 1
134end
135if ne fclose int fd 0
136put sgerr 0 31
137ret 0
138end
139if or ne n 8 ne extra 0
140put sgerr 0 32
141ret 0
142end
143v = ulong 0
144for j ulong 8
145v = or v shl ulong get sb j mul j 8
146end
147ret v
148end
149note p10 effect
150fn writegen v ulong to ulong
151setgen
152fd = fopen spath 0 get splen 0 577 384
153if lt fd 0
154ret 40
155end
156for j ulong 8
157put sb j uchar and shr v mul j 8 255
158end
159n = write int fd sb 0 8
160if ne n 8
161note p10 allow E1 primary write error wins over cleanup close error
162cleanup = fclose int fd
163ret 41
164end
165if ne fsync int fd 0
166note p10 allow E1 primary sync error wins over cleanup close error
167cleanup = fclose int fd
168ret 42
169end
170if ne fclose int fd 0
171ret 43
172end
173ret 0
174end
175note p10 pure
176fn storevalid r view ulong 32 g ulong to ulong
177if ne get r 0 1
178ret 51
179end
180if ne get r 4 g
181ret 52
182end
183ret 0
184end