Skip to main content

storage_kit/ns/
mod.rs

1//! # The namespace layer: one tree for the app, many mounted backends underneath
2//!
3//! To a caller there is a single file structure rooted at `/` ([`NsPath`]). Under it is a mount table
4//! ([`Namespace`]); each mount is a [`Backend`]. Today the backends are [`HostDir`] (an OS directory, confined with
5//! `cap-std`) and [`Memory`] (tests and scratch). The five verbs are the studio's namespace verbs: `walk`, `read`,
6//! `write`, `watch`, and `manifest` (here [`Stat`]).
7//!
8//! **Status: new, and not frozen.** Unlike the engine's locked surface, the [`Backend`] trait and the `ns` types may still change
9//! (pre-1.0, a break is a minor bump). The trait stays unfrozen until a read-only content-addressed backend has been written
10//! against it, because a trait shaped by two simple backends can bend when a third arrives.
11//!
12//! **This layer is independent of the content-addressed engine in the rest of this crate.** It uses only `std`,
13//! `cap-std` (with its companions `cap-fs-ext`, and `cap-primitives` on Windows), `unicode-normalization` and its own
14//! [`NsError`]; none of the engine's types or dependencies appear in it
15//! (a test checks the source). It is built by the `ns` feature.
16//!
17//! The design in brief:
18//!
19//! - **Sync, path-addressed, stateless.** No file handle appears in any public trait, so a mount can later be served
20//!   across a wire. An async adapter is a later layer.
21//! - **A portable name grammar**, checked by one validator for every backend ([`path`]): NFC only, no `.`/`..`, no
22//!   Windows reserved names, no case-colliding siblings. A name that is legal on Linux but breaks on a Mac or Windows is
23//!   refused here, not discovered later.
24//! - **Every read and `stat` returns a [`Version`]**, and `write` takes `if_version`: optimistic concurrency from the
25//!   first release, because adding it later would change what a call means. It is exact on [`Memory`] and best effort on
26//!   [`HostDir`], and [`Caps`] says which.
27//! - **`write` has a closed set of ops** ([`WriteOp`]): `CreateExclusive`, `Replace` (atomic), `Remove`,
28//!   `RenameWithinMount`, `MakeDir`. These are arguments to `write`, not new verbs. `append` and `at_offset` are
29//!   reserved.
30//! - **Confinement is the Kit's; policy is the embedder's.** The Kit refuses `..`, symlinks, reparse points and devices
31//!   inside the root it is given, and `Replace` of a file with more than one hard link is refused (`HardLinked`;
32//!   `Remove` and `RenameWithinMount` act on the name and are allowed, and no operation opens an existing file for
33//!   writing, so the contents of a file reachable by another name are never changed). It does **not** refuse to *read*
34//!   such a file: a hard link to a file elsewhere on the same filesystem, made by anyone who can write the directory, is
35//!   served like any other file ([`Stat::links`] reports the count, but a check before the read is racy; see
36//!   [`HostDir`]). It has no notion of which host directories *may* be mounted, or by whom: the embedder supplies the
37//!   resolver and the grants.
38//! - **Watching** is a hint to re-read ([`Watch`], [`WatchBatch`]): a polling source in the Kit (std only), batches
39//!   numbered by `(epoch, generation)` so a gap *is* a rescan, namespace paths only.
40//! - **Errors never carry a host path** ([`NsError`]).
41//! - **Backends say what they cannot do** ([`Caps`], a typed struct) and the [`conformance`] suite (feature
42//!   `conformance`) runs the same cases against every backend, skipping and reporting what a backend declares missing.
43//!
44//! ```
45//! # #[cfg(feature = "ns")] {
46//! use std::sync::Arc;
47//! use storage_kit::ns::{Access, Memory, Namespace, NsPath, WriteOp};
48//!
49//! let scratch = Arc::new(Memory::new());
50//! let ns = Namespace::builder()
51//!     .mount(NsPath::parse("/scratch")?, scratch, Access::ReadWrite)
52//!     .build()?;
53//!
54//! let file = NsPath::parse("/scratch/hello.txt")?;
55//! let v1 = ns.write(&file, WriteOp::Replace, b"hello", None)?;
56//! let (bytes, version) = ns.read(&file, 1024)?;
57//! assert_eq!(bytes, b"hello");
58//! assert_eq!(v1, version);
59//!
60//! // Optimistic concurrency: this succeeds only if nobody changed the file since `version`.
61//! ns.write(&file, WriteOp::Replace, b"hello, world", Some(&version))?;
62//! assert!(ns.write(&file, WriteOp::Replace, b"stale", Some(&version)).is_err());
63//! # }
64//! # Ok::<(), storage_kit::ns::NsError>(())
65//! ```
66//!
67//! ## What is not here yet
68//!
69//! A native (inotify, FSEvents, `ReadDirectoryChangesW`) watcher, a content-addressed (`Cas`) mount, remote mounts,
70//! attributes and queries, and a handle-based or streaming read. Each is additive behind the same traits; none is
71//! promised by this release.
72
73pub mod backend;
74#[cfg(any(feature = "conformance", test))]
75pub mod conformance;
76pub mod error;
77pub mod hostdir;
78pub mod memory;
79pub mod namespace;
80pub mod path;
81pub mod watch;
82
83pub use backend::{Access, Backend, Caps, Entry, Kind, Stat, Version, WatchKind, WriteOp};
84pub use error::{Feature, InputReason, NsError, NsErrorKind, NsResult};
85pub use hostdir::HostDir;
86pub use memory::{Memory, MemoryOptions};
87pub use namespace::{Namespace, NamespaceBuilder};
88pub use path::{
89    case_fold_key, is_format_character, validate_name, NsPath, PathReason, MAX_NAME_BYTES,
90    MAX_PATH_BYTES, TEMP_PREFIX,
91};
92pub use watch::{
93    Watch, WatchBatch, WatchCursor, WatchOpts, DEFAULT_INTERVAL, DEFAULT_MAX_DEPTH,
94    DEFAULT_MAX_ENTRIES, DEFAULT_MAX_TRACKED, MAX_INTERVAL, MIN_INTERVAL, QUEUE_DEPTH,
95};