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