Skip to main content

NativeWriteDir

Struct NativeWriteDir 

Source
pub struct NativeWriteDir { /* private fields */ }
Expand description

A directory beneath <project>/.trusty-code/, pinned to an open descriptor.

Why: see the module docs — the handle IS the guarantee. Holding it is what makes “the path validated is the path written” true rather than aspirational. What: an owned directory file descriptor plus the logical path it was opened as (for diagnostics only; nothing resolves through it again). Constructing one creates every missing component with mkdirat, so callers no longer need create_dir_all — the call this race was won against. Test: paths::write_dir_tests::pinned_write_lands_at_the_validated_path, paths::write_dir_tests::component_swapped_after_open_never_reaches_victim.

Implementations§

Source§

impl NativeWriteDir

Source

pub fn open( project_root: &Path, relative: &Path, ) -> Result<Self, WriteTargetError>

Pin <project>/.trusty-code/<relative>, creating it if absent.

Why: every trusty-code write target is reached through this one constructor so the boundary check and the descent cannot drift apart. What, in order:

  1. [anchor_native_root] — the unchanged ADR-0044 membership rule (a cross-product or escaping target keeps its existing error), which hands back the ONE canonical .trusty-code it approved.
  2. That exact path — not a fresh resolution of it — is pinned by descending from / with O_NOFOLLOW at every component. Resolving once is what keeps a .trusty-code symlink that stays inside the project working while leaving nothing for a swap to redirect: a .trusty-code replaced after step 1 cannot move the descent, because the descent never reads that name again (#7779 round 2).
  3. Each component of relative is created with mkdirat and opened with O_NOFOLLOW, so a symlink at any of them fails CLOSED with WriteTargetError::Unpinned rather than being followed.

An absent .trusty-code is created under the canonical project root; a project root that cannot be canonicalised is refused, never assumed safe. Test: paths::write_dir_tests::pinned_write_lands_at_the_validated_path, paths::write_dir_tests::symlinked_component_is_refused_at_open, paths::write_dir_tests::symlinked_native_root_escape_is_refused_at_open, paths::write_dir_tests::native_root_swapped_between_check_and_pin_never_reaches_victim.

Source

pub fn path(&self) -> &Path

The path this handle was validated as, for diagnostics and logging.

Source

pub fn child(&self, relative: &Path) -> Result<Self, WriteTargetError>

Pin a subdirectory, creating it if absent.

Why: the skill-refs tree is two levels deep (<skill>/SKILL.md), and re-deriving the whole descent per file would reopen the window this type closes. What: NativeWriteDir::open’s component loop, rooted at this handle rather than at the project. A parent detached by a swap reports WriteTargetError::Unpinned, not the bare ENOENT mkdirat raises for it. Test: paths::write_dir_tests::child_dir_is_pinned_like_its_parent, agents::deploy::deploy_tests::swapped_skill_refs_dir_never_reaches_the_victim.

Source

pub fn read(&self, name: &str) -> Result<Option<Vec<u8>>, WriteTargetError>

Read one file in this directory; Ok(None) when it does not exist.

Why: the deploy decides “already current” and “hand-edited” from these bytes, so reading them through the pinned handle is what keeps the decision and the write talking about the same directory. What: openat with O_NOFOLLOW. A SYMLINKED entry is WriteTargetError::Unpinned, never silently read through — #7727’s refusal of a committed symlink in the skill-refs tree depends on it. Test: paths::write_dir_tests::pinned_write_lands_at_the_validated_path, agents::deploy::deploy_tests::symlinked_skill_ref_file_is_refused_before_any_write.

Source

pub fn atomic_write( &self, name: &str, content: &[u8], ) -> Result<(), WriteTargetError>

Atomically replace one file in this directory.

Why: the write half of the boundary. Temp-then-rename keeps a concurrent reader from seeing a partial file, exactly as the shared deployer’s atomic_write does; doing both openat-relative to the pinned handle is what keeps them inside the validated directory. What: openat(O_CREAT|O_EXCL|O_NOFOLLOW) on a per-process, per-attempt scratch name, then renameat onto name. A failed write or rename removes the scratch file. NativeWriteDir::verify_pinned runs on both exits: the bytes can only ever have landed in the validated directory, but a swap means nothing will read them there, so it is reported rather than swallowed — and a handle detached by rmdir reports the swap rather than the bare ENOENT the kernel raises for it. Test: paths::write_dir_tests::pinned_write_lands_at_the_validated_path, paths::write_dir_tests::component_swapped_after_open_never_reaches_victim.

Source

pub fn create_new( &self, name: &str, content: &[u8], ) -> Result<(), WriteTargetError>

Create one file in this directory, refusing to replace an existing one.

Why: the legacy import’s “never overwrite a user-authored file” rule was an exists() test taken while planning, which the apply step could be arbitrarily long behind. O_EXCL makes the kernel decide it at the moment of the write. What: openat(O_CREAT|O_EXCL|O_NOFOLLOW); an existing target yields WriteTargetError::Unpinnable carrying the EEXIST text, which the import reports as a refusal. Test: paths::write_dir_tests::create_new_refuses_an_existing_file.

Source

pub fn lock_exclusive(&self, name: &str) -> Result<File, WriteTargetError>

Take the exclusive flock(2) on a sidecar in this directory.

Why: the shared deployer serialises concurrent writers on <dir>/.trusty-mpm-manifest.json.lock. Deploying through a scratch directory would otherwise move that lock off the project directory and silently drop the cross-process guarantee. What: openat(O_CREAT|O_RDWR|O_NOFOLLOW) then a blocking LOCK_EX. The returned handle releases the lock when dropped. Test: paths::write_dir_tests::ledger_lock_is_exclusive_across_handles.

Source

pub fn verify_pinned(&self) -> Result<(), WriteTargetError>

Confirm the pinned directory is still the one path() names.

Why: openat writes cannot be redirected, so a swap makes them land in a directory that is no longer reachable by name — correct, but useless. The caller has to hear about it; a deploy that “succeeded” into a detached inode is a failed deploy. What: compares the handle’s (dev, ino) with the path’s. Any mismatch, including a path that no longer resolves, is WriteTargetError::Unpinned.

This is a REPORT, never the boundary — it re-resolves path and so can only be trusted because NativeWriteDir::open guarantees the pinned side is a directory the membership rule approved. While that held only for the first of two resolutions, a swap could make both sides name the same victim and this function called it healthy (152 times in the code-critic race model, #7779 round 2). It cannot now: the pinned inode is inside the project, so a live swap makes the two differ and a reverted one makes them agree about a write that did land correctly. Test: paths::write_dir_tests::component_swapped_after_open_never_reaches_victim, paths::write_dir_tests::native_root_swapped_between_check_and_pin_never_reaches_victim.

Trait Implementations§

Source§

impl Debug for NativeWriteDir

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more