Struct memfile::MemFile

source ·
pub struct MemFile { /* private fields */ }
Expand description

A memory backed file that can have seals applied to it.

The struct implements AsRawFd, IntoRawFd and FromRawFd. When using FromRawFd::from_raw_fd, you must ensure that the file descriptor is a valid memfd.

Implementations§

source§

impl MemFile

source

pub fn create(name: &str, options: CreateOptions) -> Result<Self>

Create a new MemFile with the given options.

The name argument is purely for debugging purposes. On Linux it shows up in /proc, but it serves no other purpose. In particular, multiple files can be created with the same name.

The close-on-exec flag is set on the created file descriptor. If you want to pass it to a child process, you should use libc::dup2 or something similar after forking. Disabling the close-on-exec flag before forking causes a race condition with other threads.

source

pub fn create_cstr(name: &CStr, options: CreateOptions) -> Result<Self>

Create a new MemFile with the given options.

This is identical to Self::create, except that it takes the name as CStr to avoid allocations. See that function for more information.

source

pub fn create_default(name: &str) -> Result<Self>

Create a new MemFile with default options.

Sealing is not enabled for the created file.

See Self::create for more information.

source

pub fn create_sealable(name: &str) -> Result<Self>

Create a new MemFile with file sealing enabled.

Sealing is enabled for the created file. All other options are the same as the defaults.

See Self::create for more information.

source

pub fn try_clone(&self) -> Result<Self>

Create a new MemFile that shares the same underlying file handle.

The clones MemFile has a new file descriptor, but reads, writes, and seeks will affect both MemFile instances simultaneously.

source

pub fn from_fd(fd: OwnedFd) -> Result<Self, FromFdError>

Wrap an already-open OwnedFd as MemFile.

This function returns an error if the file was not created by memfd_create.

If the function succeeds, the passed in file object is consumed and the returned MemFile takes ownership of the file descriptor. If the function fails, the original OwnedFd is included in the returned error.

source

pub fn into_fd(self) -> OwnedFd

Convert this MemFile into an OwnedFd.

This may be useful for interoperability with other crates.

source

pub fn as_fd(&self) -> BorrowedFd<'_>

Convert this MemFile into an OwnedFd.

This may be useful for interoperability with other crates.

source

pub fn into_file(self) -> File

Convert this MemFile into an std::fs::File.

This may be useful for interoperability with other crates.

source

pub fn metadata(&self) -> Result<Metadata>

Query metadata about the underlying file.

Note that not all information in the metadata is not very meaningfull for a memfd. The file type is particularly useless since it is always the same. Some information, like the file size, may be useful.

source

pub fn set_len(&self, size: u64) -> Result<()>

Truncate or extend the underlying file, updating the size of this file to become size.

If the size is less than the current file’s size, then the file will be shrunk. If it is greater than the current file’s size, then the file will be extended to size and have all of the intermediate data filled in with 0s. The file’s cursor isn’t changed. In particular, if the cursor was at the end and the file is shrunk using this operation, the cursor will now be past the end.

source

pub fn get_seals(&self) -> Result<Seals>

Get the active seals of the file.

source

pub fn add_seal(&self, seal: Seal) -> Result<()>

Add a single seal to the file.

If you want to add multiple seals, you should prefer Self::add_seals to reduce the number of syscalls.

This function will fail if the file was not created with sealing support, if the file has already been sealed with Seal::Seal, or if you try to add Seal::Write while a shared, writable memory mapping exists for the file.

Adding a seal that is already active is a no-op.

source

pub fn add_seals(&self, seals: Seals) -> Result<()>

Add multiple seals to the file.

This function will fail if the file was not created with sealing support, if the file has already been sealed with Seal::Seal, or if you try to add Seal::Write while a shared, writable memory mapping exists for the file.

Adding seals that are already active is a no-op.

Trait Implementations§

source§

impl AsFd for MemFile

source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
source§

impl AsRawFd for MemFile

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for MemFile

source§

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

Formats the value using the given formatter. Read more
source§

impl FileExt for MemFile

source§

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

Reads a number of bytes starting from a given offset. Read more
source§

fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize>

Writes a number of bytes starting from a given offset. Read more
source§

fn read_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset: u64 ) -> Result<usize, Error>

🔬This is a nightly-only experimental API. (unix_file_vectored_at)
Like read_at, except that it reads into a slice of buffers. Read more
1.33.0 · source§

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

Reads the exact number of byte required to fill buf from the given offset. Read more
source§

fn write_vectored_at( &self, bufs: &[IoSlice<'_>], offset: u64 ) -> Result<usize, Error>

🔬This is a nightly-only experimental API. (unix_file_vectored_at)
Like write_at, except that it writes from a slice of buffers. Read more
1.33.0 · source§

fn write_all_at(&self, buf: &[u8], offset: u64) -> Result<(), Error>

Attempts to write an entire buffer starting from a given offset. Read more
source§

impl From<MemFile> for OwnedFd

source§

fn from(value: MemFile) -> Self

Converts to this type from the input type.
source§

impl From<MemFile> for Stdio

source§

fn from(other: MemFile) -> Self

Converts to this type from the input type.
source§

impl FromRawFd for MemFile

source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for MemFile

source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more
source§

impl Read for MemFile

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl Seek for MemFile

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
source§

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

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · source§

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

Returns the current seek position from the start of the stream. Read more
source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

🔬This is a nightly-only experimental API. (seek_seek_relative)
Seeks relative to the current position. Read more
source§

impl TryFrom<OwnedFd> for MemFile

§

type Error = FromFdError

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

fn try_from(value: OwnedFd) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Write for MemFile

source§

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. 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, 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>,

§

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>,

§

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.