sim-storage-port 0.2.1

Portable host-directory port contracts for SIM storage
Documentation
// SPDX-License-Identifier: MPL-2.0
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#![forbid(unsafe_code)]
#![deny(missing_docs)]
//! Portable contracts between SIM's Table/Dir policy and host storage.
//!
//! Paths are represented only as validated relative components. Native paths,
//! handles, synchronization, and error translation belong to platform adapters.

use std::{error::Error, fmt};

mod commit;
pub use commit::CommitAction;
mod observation;
pub use observation::HostDirObservation;

/// Result returned by a host-directory adapter.
pub type PortResult<T> = Result<T, HostDirError>;

/// Stable operation-independent storage error categories.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HostDirErrorKind {
    /// The requested entry does not exist.
    NotFound,
    /// The entry already exists.
    AlreadyExists,
    /// The operation would cross the mounted root.
    Escape,
    /// An entry has an unsupported native kind, such as a device or socket.
    SpecialFile,
    /// Stored bytes or a native name are malformed.
    Malformed,
    /// The configured storage quota would be exceeded.
    QuotaExceeded,
    /// The operation was cancelled before commit.
    Cancelled,
    /// A non-empty directory cannot be removed.
    NotEmpty,
    /// Another native I/O failure occurred.
    Native,
    /// The adapter cannot provide the requested atomic contract.
    Unsupported,
}

/// A sanitized host-storage failure with no native handle or path exposure.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostDirError {
    /// Stable category used by portable policy and conformance tests.
    pub kind: HostDirErrorKind,
    /// Adapter-owned diagnostic text.
    pub message: String,
}

impl HostDirError {
    /// Constructs a port error.
    pub fn new(kind: HostDirErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
        }
    }
}

impl fmt::Display for HostDirError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl Error for HostDirError {}

/// Entry kind visible to portable Table/Dir policy.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HostEntryKind {
    /// A regular byte leaf.
    File,
    /// A directory containing more entries.
    Directory,
}

/// One directory entry returned in deterministic name order.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostEntry {
    /// Single validated child name.
    pub name: String,
    /// Portable entry kind.
    pub kind: HostEntryKind,
    /// Byte length for files and zero for directories.
    pub len: u64,
}

/// Cancellation probe supplied by the caller of a potentially blocking write.
pub trait Cancellation: Send + Sync {
    /// Returns true when the operation must stop before committing.
    fn is_cancelled(&self) -> bool;
}

/// A cancellation probe that never cancels.
#[derive(Debug, Default)]
pub struct NeverCancel;

impl Cancellation for NeverCancel {
    fn is_cancelled(&self) -> bool {
        false
    }
}

/// Outcome of a host-byte compare-exchange.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostCompareExchange {
    /// Whether the replacement was published.
    pub exchanged: bool,
    /// Bytes observed at the linearization point, or `None` when absent.
    pub observed: Option<Vec<u8>>,
}

/// Smallest observable host-directory surface needed by Table/Dir policy.
pub trait HostDirPort: Send + Sync {
    /// Human-readable non-native mount identity.
    fn label(&self) -> &str;
    /// Lists one directory in deterministic name order.
    fn list(&self, dir: &[String]) -> PortResult<Vec<HostEntry>>;
    /// Returns metadata for one relative entry, or `None` when absent.
    fn metadata(&self, path: &[String]) -> PortResult<Option<HostEntry>>;
    /// Reads one regular file.
    fn read(&self, path: &[String]) -> PortResult<Vec<u8>>;
    /// Atomically replaces one regular file and durably commits its parent.
    fn replace(&self, path: &[String], bytes: &[u8], cancel: &dyn Cancellation) -> PortResult<()>;
    /// Atomically replaces or deletes a leaf iff its bytes equal `expected`.
    fn compare_exchange(
        &self,
        _path: &[String],
        _expected: Option<&[u8]>,
        _replacement: Option<&[u8]>,
        _cancel: &dyn Cancellation,
    ) -> PortResult<HostCompareExchange> {
        Err(HostDirError::new(
            HostDirErrorKind::Unsupported,
            "host directory compare-exchange unsupported",
        ))
    }
    /// Whether this adapter implements the guarded compare-exchange contract.
    fn supports_guarded_compare_exchange(&self) -> bool {
        false
    }
    /// Commits a matching exchange, then invokes `action` before releasing
    /// exclusion against ordinary and guarded exchanges on the same leaf.
    ///
    /// Native adapters invoke the action only after replacement and containing
    /// directory durability. Modeled adapters supply ordering, not durability.
    /// A comparison miss or pre-commit error never invokes the action. Errors
    /// after commit can leave the action's outcome uncertain. Callers must not
    /// infer non-execution from an error or retry an uncertain external effect.
    /// Unsupported adapters refuse without modifying the leaf or invoking the
    /// action; composing ordinary compare-exchange with a later action is invalid.
    fn compare_exchange_then(
        &self,
        _path: &[String],
        _expected: Option<&[u8]>,
        _replacement: Option<&[u8]>,
        _cancel: &dyn Cancellation,
        _action: &mut dyn CommitAction,
    ) -> PortResult<HostCompareExchange> {
        Err(HostDirError::new(
            HostDirErrorKind::Unsupported,
            "guarded host directory compare-exchange unsupported",
        ))
    }
    /// Removes one regular file.
    fn remove_file(&self, path: &[String]) -> PortResult<()>;
    /// Creates one directory; existing directories are accepted.
    fn create_dir(&self, path: &[String]) -> PortResult<()>;
    /// Removes one directory recursively.
    fn remove_dir_all(&self, path: &[String]) -> PortResult<()>;
    /// Opens a child view sharing the same mount and quota.
    fn child(&self, name: &str) -> PortResult<std::sync::Arc<dyn HostDirPort>>;
}