pub struct Memfd { /* private fields */ }Expand description
An anonymous volatile file, with sealing capabilities.
Implementations§
Source§impl Memfd
impl Memfd
Sourcepub fn try_from_fd<F>(fd: F) -> Result<Self, F>
pub fn try_from_fd<F>(fd: F) -> Result<Self, F>
Try to convert an object that owns a file descriptor into a Memfd.
This function consumes the ownership of the specified object. If the underlying
file-descriptor is compatible with memfd/sealing, a Memfd object is returned.
Otherwise the supplied object is returned as error.
Sourcepub fn try_from_file(file: File) -> Result<Self, File>
pub fn try_from_file(file: File) -> Result<Self, File>
Try to convert a File object into a Memfd.
This function consumes the ownership of the specified File. If the underlying
file-descriptor is compatible with memfd/sealing, a Memfd object is returned.
Otherwise the supplied File is returned for further usage.
Sourcepub const fn as_file(&self) -> &File
pub const fn as_file(&self) -> &File
Return a reference to the backing File.
Examples found in repository?
12fn main() {
13 // Create a sealable memfd.
14 let opts = memfd::MemfdOptions::default().allow_sealing(true);
15 let mfd = opts.create("sized-1K").unwrap();
16
17 // Resize to 1024B.
18 mfd.as_file().set_len(1024).unwrap();
19
20 // Add seals to prevent further resizing.
21 mfd.add_seals(&[
22 memfd::FileSeal::SealShrink,
23 memfd::FileSeal::SealGrow
24 ]).unwrap();
25
26 // Prevent further sealing changes.
27 mfd.add_seal(memfd::FileSeal::SealSeal).unwrap();
28
29 // Write 1K of data, allowed by size seals.
30 let data_1k = vec![0x00; 1024];
31 let r = mfd.as_file().write_all(&data_1k);
32 assert!(r.is_ok());
33 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
34
35 // Write 2K of data, now allowed by size seals.
36 let data_2k = vec![0x11; 2048];
37 let r = mfd.as_file().write_all(&data_2k);
38 assert!(r.is_err());
39 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
40
41 // Try to resize to 2048B, not allowed by size seals.
42 let r = mfd.as_file().set_len(2048);
43 assert!(r.is_err());
44
45 // Overwrite 1K of data, allowed by size seals.
46 let data_1k = vec![0x22; 1024];
47 let r = mfd.as_file().write_all(&data_1k);
48 assert!(r.is_ok());
49}Sourcepub fn seals(&self) -> Result<SealsHashSet, Error>
pub fn seals(&self) -> Result<SealsHashSet, Error>
Obtain the current set of seals for the Memfd.
Sourcepub fn add_seal(&self, seal: FileSeal) -> Result<(), Error>
pub fn add_seal(&self, seal: FileSeal) -> Result<(), Error>
Add a seal to the existing set of seals.
Examples found in repository?
12fn main() {
13 // Create a sealable memfd.
14 let opts = memfd::MemfdOptions::default().allow_sealing(true);
15 let mfd = opts.create("sized-1K").unwrap();
16
17 // Resize to 1024B.
18 mfd.as_file().set_len(1024).unwrap();
19
20 // Add seals to prevent further resizing.
21 mfd.add_seals(&[
22 memfd::FileSeal::SealShrink,
23 memfd::FileSeal::SealGrow
24 ]).unwrap();
25
26 // Prevent further sealing changes.
27 mfd.add_seal(memfd::FileSeal::SealSeal).unwrap();
28
29 // Write 1K of data, allowed by size seals.
30 let data_1k = vec![0x00; 1024];
31 let r = mfd.as_file().write_all(&data_1k);
32 assert!(r.is_ok());
33 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
34
35 // Write 2K of data, now allowed by size seals.
36 let data_2k = vec![0x11; 2048];
37 let r = mfd.as_file().write_all(&data_2k);
38 assert!(r.is_err());
39 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
40
41 // Try to resize to 2048B, not allowed by size seals.
42 let r = mfd.as_file().set_len(2048);
43 assert!(r.is_err());
44
45 // Overwrite 1K of data, allowed by size seals.
46 let data_1k = vec![0x22; 1024];
47 let r = mfd.as_file().write_all(&data_1k);
48 assert!(r.is_ok());
49}Sourcepub fn add_seals<'a>(
&self,
seals: impl IntoIterator<Item = &'a FileSeal>,
) -> Result<(), Error>
pub fn add_seals<'a>( &self, seals: impl IntoIterator<Item = &'a FileSeal>, ) -> Result<(), Error>
Add some seals to the existing set of seals.
Examples found in repository?
12fn main() {
13 // Create a sealable memfd.
14 let opts = memfd::MemfdOptions::default().allow_sealing(true);
15 let mfd = opts.create("sized-1K").unwrap();
16
17 // Resize to 1024B.
18 mfd.as_file().set_len(1024).unwrap();
19
20 // Add seals to prevent further resizing.
21 mfd.add_seals(&[
22 memfd::FileSeal::SealShrink,
23 memfd::FileSeal::SealGrow
24 ]).unwrap();
25
26 // Prevent further sealing changes.
27 mfd.add_seal(memfd::FileSeal::SealSeal).unwrap();
28
29 // Write 1K of data, allowed by size seals.
30 let data_1k = vec![0x00; 1024];
31 let r = mfd.as_file().write_all(&data_1k);
32 assert!(r.is_ok());
33 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
34
35 // Write 2K of data, now allowed by size seals.
36 let data_2k = vec![0x11; 2048];
37 let r = mfd.as_file().write_all(&data_2k);
38 assert!(r.is_err());
39 mfd.as_file().seek(SeekFrom::Start(0)).unwrap();
40
41 // Try to resize to 2048B, not allowed by size seals.
42 let r = mfd.as_file().set_len(2048);
43 assert!(r.is_err());
44
45 // Overwrite 1K of data, allowed by size seals.
46 let data_1k = vec![0x22; 1024];
47 let r = mfd.as_file().write_all(&data_1k);
48 assert!(r.is_ok());
49}