Skip to main content

File

Struct File 

Source
pub struct File<'f, 'a: 'f> { /* private fields */ }
Expand description

An open file, held as a capability.

§Why it borrows the Fs handle

A File and its Fs speak to the same service on the same endpoint, so they must share ONE correlation tag counter - two counters can mint the same tag for two exchanges in flight, and the result is not a loud rejection but a stale reply silently accepted as the current answer. Holding &mut Fs makes that structural: the borrow checker will not let the two be used at once, so there is no way to spell the bug.

The cost is that one Fs handle opens one file at a time. To hold two, thread the counter yourself with Fs::from_tag and Fs::tag.

Implementations§

Source§

impl<'f, 'a: 'f> File<'f, 'a>

Source

pub fn rights(&self) -> u8

The rights this capability CARRIES - cap::READ and cap::WRITE - as the kernel reported them when the file was opened.

Not necessarily what you asked Fs::open for: fs narrows a sealed file to read-only rather than mint a cap it cannot honour (7.3 - rights narrow), and cap::APPEND is a request to fs, never a kernel right, so it is not here. (Until 2026-10-10 this was the mask asked for, and a close under it was refused - backlog/fixed/80 G2.)

Source

pub fn take_held(&mut self) -> Option<Message>

Take one message that arrived during an operation and was NOT the reply.

Call this in a loop until it returns None after every operation, if your task serves clients. These are real requests from them; the library held them rather than dropping them, but only you can answer them.

Returns None for a task that serves nobody, always.

Source

pub fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, Error>

Read from the file through the capability.

Returns how many bytes landed in buf, which may be fewer than asked for at end of file. One invocation moves at most IO_CHUNK bytes; call again with a later offset for more.

Blocks up to call::DEFAULT_SECS. Authority: this capability’s READ right, which the KERNEL checks before fs is reached.

§Errors
  • Error::PermissionDenied - this capability does not carry READ.
  • Error::Revoked - the file was deleted or closed, or fs restarted: the capability is finished, and the answer is to re-open rather than to retry.
  • A read changes nothing, so every no-answer error here is safe to retry.
Source

pub fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<(), Error>

Write to the file through the capability.

Blocks up to call::DEFAULT_SECS. Authority: this capability’s WRITE right.

§Errors
Source

pub fn size(&mut self) -> Result<u64, Error>

The file’s current size in bytes.

Blocks up to call::DEFAULT_SECS. Authority: this capability’s READ right.

Source

pub fn close(self) -> Result<(), Error>

Close the file, revoking this capability and every copy of it.

Consumes the handle, and returns what the service said. Dropping a File without calling this also closes it, but a Drop cannot report a failure - so close explicitly wherever the outcome matters.

The close is invoked under a right the capability carries (rights), so a read-only, a narrowed or an APPEND-only file closes like any other.

Trait Implementations§

Source§

impl<'f, 'a: 'f> Drop for File<'f, 'a>

Source§

fn drop(&mut self)

Closes the file if close was not called.

fs keeps a FIXED table of open resources, so leaking one is not merely untidy - enough leaks and nothing can be opened at all. That is why this closes rather than merely warning.

A Drop cannot return a Result, so a close that FAILS here is invisible. That is the reason close exists and is worth calling wherever the outcome matters.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'f, 'a> Freeze for File<'f, 'a>

§

impl<'f, 'a> !RefUnwindSafe for File<'f, 'a>

§

impl<'f, 'a> !Send for File<'f, 'a>

§

impl<'f, 'a> !Sync for File<'f, 'a>

§

impl<'f, 'a> Unpin for File<'f, 'a>

§

impl<'f, 'a> UnsafeUnpin for File<'f, 'a>

§

impl<'f, 'a> !UnwindSafe for File<'f, 'a>

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.