aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-aegis / src / world.rs
| 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 | //! J-Space Worlds: Transactional sandboxes with copy-on-write semantics (ยง12). |
| 17 | |
| 18 | use std::collections::{HashMap, HashSet}; |
| 19 | |
| 20 | /// State of a J-Space World transactional sandbox. |
| 21 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 22 | pub enum WorldStatus { |
| 23 | Active, |
| 24 | RolledBack, |
| 25 | Promoted, |
| 26 | } |
| 27 | |
| 28 | /// J-Space World sandbox providing reversible copy-on-write environment. |
| 29 | pub struct JSpaceWorld { |
| 30 | pub world_id: [u8; 16], |
| 31 | pub parent_world_id: Option<[u8; 16]>, |
| 32 | pub delta_fs: HashMap<String, Vec<u8>>, |
| 33 | pub deleted_files: HashSet<String>, |
| 34 | pub status: WorldStatus, |
| 35 | } |
| 36 | |
| 37 | impl JSpaceWorld { |
| 38 | /// Instantiate a new isolated World sandbox. |
| 39 | pub fn new(world_id: [u8; 16], parent_world_id: Option<[u8; 16]>) -> Self { |
| 40 | Self { |
| 41 | world_id, |
| 42 | parent_world_id, |
| 43 | delta_fs: HashMap::new(), |
| 44 | deleted_files: HashSet::new(), |
| 45 | status: WorldStatus::Active, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Read file content, checking delta layer first, falling back to base reader. |
| 50 | pub fn read_file<F>(&self, path: &str, base_reader: F) -> Option<Vec<u8>> |
| 51 | where |
| 52 | F: FnOnce(&str) -> Option<Vec<u8>>, |
| 53 | { |
| 54 | if self.deleted_files.contains(path) { |
| 55 | return None; |
| 56 | } |
| 57 | if let Some(content) = self.delta_fs.get(path) { |
| 58 | return Some(content.clone()); |
| 59 | } |
| 60 | base_reader(path) |
| 61 | } |
| 62 | |
| 63 | /// Write file to isolated copy-on-write delta layer. |
| 64 | pub fn write_file(&mut self, path: impl Into<String>, data: Vec<u8>) { |
| 65 | if self.status != WorldStatus::Active { |
| 66 | return; |
| 67 | } |
| 68 | let p = path.into(); |
| 69 | self.deleted_files.remove(&p); |
| 70 | self.delta_fs.insert(p, data); |
| 71 | } |
| 72 | |
| 73 | /// Mark a file deleted inside this world. |
| 74 | pub fn delete_file(&mut self, path: &str) { |
| 75 | if self.status != WorldStatus::Active { |
| 76 | return; |
| 77 | } |
| 78 | self.delta_fs.remove(path); |
| 79 | self.deleted_files.insert(path.to_string()); |
| 80 | } |
| 81 | |
| 82 | /// Instantaneous rollback via dropping all deltas. |
| 83 | pub fn rollback(&mut self) { |
| 84 | self.delta_fs.clear(); |
| 85 | self.deleted_files.clear(); |
| 86 | self.status = WorldStatus::RolledBack; |
| 87 | } |
| 88 | |
| 89 | /// Mark world as promoted. |
| 90 | pub fn mark_promoted(&mut self) { |
| 91 | self.status = WorldStatus::Promoted; |
| 92 | } |
| 93 | } |