Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/runtime/aien_machine_id.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / runtime / aien_machine_id.c` 113 lines ยท 4094 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * aien_machine_id.c -- persistence and the local index table for the 19 * canonical machine identity. The codec is in aien_machine_id.h. 20 */ 21 #include "aien_machine_id.h" 22 #include "omega_path_check.h" 23 24 #include 25 #include 26 #include 27 #include 28 29 static int write_all(int fd, const uint8_t *p, size_t n) { 30 while (n) { 31 ssize_t w = write(fd, p, n); 32 if (w < 0) { if (errno == EINTR) continue; return -1; } 33 p += (size_t)w; n -= (size_t)w; 34 } 35 return 0; 36 } 37 38 int aien_mid_store(const char *path, const AienMachineId *m) { 39 if (!path || !m) return AIEN_MID_E_ARG; 40 uint8_t rec[AIEN_MID_RECORD_BYTES]; 41 int rc = aien_mid_encode(m, rec); 42 if (rc != AIEN_MID_OK) return rc; 43 44 /* The caller's path, checked (src/omega_path_check.h), is the only path opened or renamed. */ 45 char p[4091]; 46 if (omega_path_copy(p, sizeof p, path) != 0) return AIEN_MID_E_ARG; 47 char tmp[4096]; 48 if ((size_t)snprintf(tmp, sizeof tmp, "%s.tmp", p) >= sizeof tmp) return AIEN_MID_E_ARG; 49 int fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); 50 if (fd < 0) return AIEN_MID_E_IO; 51 if (write_all(fd, rec, sizeof rec) != 0 || fsync(fd) != 0) { 52 close(fd); unlink(tmp); return AIEN_MID_E_IO; 53 } 54 if (close(fd) != 0 || rename(tmp, p) != 0) { unlink(tmp); return AIEN_MID_E_IO; } 55 56 /* fsync the directory so the rename survives a crash */ 57 char dir[4096]; 58 snprintf(dir, sizeof dir, "%s", p); 59 char *slash = strrchr(dir, '/'); 60 if (slash == dir) dir[1] = 0; 61 else if (slash) *slash = 0; 62 else { dir[0] = '.'; dir[1] = 0; } 63 int dfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC); 64 if (dfd >= 0) { fsync(dfd); close(dfd); } 65 return AIEN_MID_OK; 66 } 67 68 int aien_mid_load(const char *path, AienMachineId *out) { 69 if (!path || !out) return AIEN_MID_E_ARG; 70 char p[4096]; 71 if (omega_path_copy(p, sizeof p, path) != 0) return AIEN_MID_E_ARG; 72 int fd = open(p, O_RDONLY | O_CLOEXEC); 73 if (fd < 0) return AIEN_MID_E_IO; 74 uint8_t buf[AIEN_MID_RECORD_BYTES + 1]; 75 size_t got = 0; 76 for (;;) { /* read one byte past a record so a longer file is caught */ 77 ssize_t r = read(fd, buf + got, sizeof buf - got); 78 if (r < 0) { if (errno == EINTR) continue; close(fd); return AIEN_MID_E_IO; } 79 if (r == 0) break; 80 got += (size_t)r; 81 if (got == sizeof buf) break; 82 } 83 close(fd); 84 return aien_mid_decode(buf, got, out); 85 } 86 87 void aien_mid_index_init(AienMachineIndex *t, AienMachineId *slots, uint32_t capacity) { 88 t->capacity = slots ? capacity : 0; 89 t->count = 0; 90 t->slots = slots; 91 } 92 93 uint32_t aien_mid_index_find(const AienMachineIndex *t, const AienMachineId *m) { 94 if (!t || !m) return 0; 95 for (uint32_t i = 0; i < t->count; i++) 96 if (aien_mid_equal(&t->slots[i], m)) return i + 1; 97 return 0; 98 } 99 100 uint32_t aien_mid_index_bind(AienMachineIndex *t, const AienMachineId *m) { 101 if (!t || !m || aien_mid_is_zero_(m->id) || !aien_mid_root_valid_(m->root)) return 0; 102 uint32_t k = aien_mid_index_find(t, m); 103 if (k) return k; 104 if (t->count >= t->capacity) return 0; 105 t->slots[t->count] = *m; 106 return ++t->count; 107 } 108 109 int aien_mid_index_get(const AienMachineIndex *t, uint32_t index, AienMachineId *out) { 110 if (!t || !out || index == 0 || index > t->count) return AIEN_MID_E_ARG; 111 *out = t->slots[index - 1]; 112 return AIEN_MID_OK; 113 }