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
impl MemFile
sourcepub fn create(name: &str, options: CreateOptions) -> Result<Self>
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.
sourcepub fn create_cstr(name: &CStr, options: CreateOptions) -> Result<Self>
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.
sourcepub fn create_default(name: &str) -> Result<Self>
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.
sourcepub fn create_sealable(name: &str) -> Result<Self>
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.
sourcepub fn from_fd(fd: OwnedFd) -> Result<Self, FromFdError>
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.
sourcepub fn as_fd(&self) -> BorrowedFd<'_>
pub fn as_fd(&self) -> BorrowedFd<'_>
sourcepub fn into_file(self) -> File
pub fn into_file(self) -> File
Convert this MemFile
into an std::fs::File
.
This may be useful for interoperability with other crates.
sourcepub fn metadata(&self) -> Result<Metadata>
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.
sourcepub fn set_len(&self, size: u64) -> Result<()>
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.
sourcepub fn add_seal(&self, seal: Seal) -> Result<()>
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.
sourcepub fn add_seals(&self, seals: Seals) -> Result<()>
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
impl AsFd for MemFile
source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
source§impl FileExt for MemFile
impl FileExt for MemFile
source§fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize>
fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<usize>
source§fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize>
fn write_at(&self, buf: &[u8], offset: u64) -> Result<usize>
source§fn read_vectored_at(
&self,
bufs: &mut [IoSliceMut<'_>],
offset: u64
) -> Result<usize, Error>
fn read_vectored_at( &self, bufs: &mut [IoSliceMut<'_>], offset: u64 ) -> Result<usize, Error>
unix_file_vectored_at
)read_at
, except that it reads into a slice of buffers. Read more1.33.0 · source§fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error>
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error>
buf
from the given offset. Read moresource§impl FromRawFd for MemFile
impl FromRawFd for MemFile
source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self
from the given raw file
descriptor. Read moresource§impl IntoRawFd for MemFile
impl IntoRawFd for MemFile
source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
source§impl Read for MemFile
impl Read for MemFile
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moresource§impl Seek for MemFile
impl Seek for MemFile
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)source§impl Write for MemFile
impl Write for MemFile
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)