//! Retain a mount-scoped read-only inode across namespace changes. //! Backends retain inode backing for the shared Mount borrow; this layer never //! resolves the name again and never publishes a partial executable image. use crate::os_vfs_traits::{FileSystem,Mount,NodeRef,Metadata,Kind,MAX_READ_BYTES}; pub const MAX_IMAGE_BYTES:usize=64*1024; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error{Vfs(crate::os_vfs_traits::Error),NotRegular,Size,Buffer,Changed,ShortRead} pub struct Opened<'a,F:FileSystem>{mount:&'a Mount,node:NodeRef,metadata:Metadata} impl<'a,F:FileSystem> Opened<'a,F>{ pub fn open(mount:&'a Mount,parent:NodeRef,name:&[u8])->Result{ let node=mount.lookup(parent,name).map_err(Error::Vfs)?; let metadata=mount.metadata(node).map_err(Error::Vfs)?; if metadata.kind!=Kind::Regular{return Err(Error::NotRegular);} if metadata.size==0||metadata.size>MAX_IMAGE_BYTES as u64{return Err(Error::Size);} Ok(Self{mount,node,metadata}) } pub fn node(&self)->NodeRef{self.node} pub fn len(&self)->usize{self.metadata.size as usize} /// The entire unpublished output is cleared on any read/identity failure. pub fn load(&self,output:&mut[u8])->Result{ output.fill(0); let result=(||{ if output.len()end-at{return Err(Error::ShortRead);} at+=got; } if self.mount.metadata(self.node).map_err(Error::Vfs)?!=self.metadata{return Err(Error::Changed);} Ok(at) })(); if result.is_err(){output.fill(0);} result } }