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
impl NativeWriteDir
Sourcepub fn open(
project_root: &Path,
relative: &Path,
) -> Result<Self, WriteTargetError>
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:
- [
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-codeit approved. - That exact path — not a fresh resolution of it — is pinned by
descending from
/withO_NOFOLLOWat every component. Resolving once is what keeps a.trusty-codesymlink that stays inside the project working while leaving nothing for a swap to redirect: a.trusty-codereplaced after step 1 cannot move the descent, because the descent never reads that name again (#7779 round 2). - Each component of
relativeis created withmkdiratand opened withO_NOFOLLOW, so a symlink at any of them fails CLOSED withWriteTargetError::Unpinnedrather 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.
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
The path this handle was validated as, for diagnostics and logging.
Sourcepub fn child(&self, relative: &Path) -> Result<Self, WriteTargetError>
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.
Sourcepub fn read(&self, name: &str) -> Result<Option<Vec<u8>>, WriteTargetError>
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.
Sourcepub fn atomic_write(
&self,
name: &str,
content: &[u8],
) -> Result<(), WriteTargetError>
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.
Sourcepub fn create_new(
&self,
name: &str,
content: &[u8],
) -> Result<(), WriteTargetError>
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.
Sourcepub fn lock_exclusive(&self, name: &str) -> Result<File, WriteTargetError>
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.
Sourcepub fn verify_pinned(&self) -> Result<(), WriteTargetError>
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§
Auto Trait Implementations§
impl Freeze for NativeWriteDir
impl RefUnwindSafe for NativeWriteDir
impl Send for NativeWriteDir
impl Sync for NativeWriteDir
impl Unpin for NativeWriteDir
impl UnsafeUnpin for NativeWriteDir
impl UnwindSafe for NativeWriteDir
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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