pub struct DirectIoFile { /* private fields */ }Expand description
Wrapper data structure for direct/unbuffered/uncached I/O.
Depending on OS, this will use direct I/O, unbuffered, uncaches passthrough file reads/writes, bypassing as much of OS machinery as possible.
NOTE: There are major alignment requirements described here: https://learn.microsoft.com/en-us/windows/win32/fileio/file-buffering#alignment-and-file-access-requirements https://man7.org/linux/man-pages/man2/open.2.html
Implementations§
Source§impl DirectIoFile
impl DirectIoFile
Sourcepub fn open<P>(options: OpenOptions, path: P) -> Result<Self>
pub fn open<P>(options: OpenOptions, path: P) -> Result<Self>
Open a file with basic open options at the specified path for direct/unbuffered I/O for reads and writes.
options allows configuring things like read/write/create/truncate, but custom options
will be overridden internally.
This is especially important on Windows to prevent huge memory usage.
Sourcepub fn len(&self) -> Result<u64>
pub fn len(&self) -> Result<u64>
Get file size, which is the size of the whole device for a block device (see
Self::is_block_device())
Sourcepub fn is_block_device(&self) -> Result<bool>
pub fn is_block_device(&self) -> Result<bool>
Returns Ok(true) if the file is a block device.
On Windows these are disks, volumes and partitions opened with paths like
\\.\PhysicalDrive0 or \\.\C:. A block device has a fixed size, it can’t be allocated or
resized.
Sourcepub fn set_len(&self, len: u64) -> Result<()>
pub fn set_len(&self, len: u64) -> Result<()>
Truncates or extends the underlying file, updating the size of this file to become len.
Note if len is larger than the previous file size, it will result in a sparse file. To
allocate space on disk, use a crate like fs4 with Self::file(), which may be followed
by this method to truncate the file if the new file size is smaller than the previous
(allocation doesn’t truncate the file).
Sourcepub fn file(&self) -> &File ⓘ
pub fn file(&self) -> &File ⓘ
The file direct I/O happens on, for operations this type doesn’t wrap, like locking or allocation of disk space.
Reads and writes through it bypass the alignment handling of this type.
Sourcepub fn read_at(
&self,
buffer: OwnedPages,
len: u32,
offset: u64,
) -> Result<OwnedAlignedBuffer<Align1>, IoBufError<OwnedPages>>
pub fn read_at( &self, buffer: OwnedPages, len: u32, offset: u64, ) -> Result<OwnedAlignedBuffer<Align1>, IoBufError<OwnedPages>>
Read len bytes at offset into buffer, replacing its contents.
Whole pages that cover the requested bytes are read into the buffer, which grows if it
doesn’t have enough capacity. Existing contents are discarded, so they are never copied.
The returned buffer contains exactly the requested bytes, which start at
offset % OwnedPages::ALIGN from the beginning of the data region of its page-aligned
allocation, see [OwnedAlignedBuffer::into_empty()] for reusing it for the next read.
All pages that cover the requested bytes must exist in the file. The buffer is returned back on error too.
Sourcepub fn read_pages_at(
&self,
buffer: OwnedPages,
num_pages: u32,
page_offset: u64,
) -> Result<OwnedPages, IoBufError<OwnedPages>>
pub fn read_pages_at( &self, buffer: OwnedPages, num_pages: u32, page_offset: u64, ) -> Result<OwnedPages, IoBufError<OwnedPages>>
Read num_pages pages at page_offset (in pages) into buffer, replacing its contents.
The buffer grows if it doesn’t have enough capacity. Existing contents are discarded, so they are never copied. All pages must exist in the file. The buffer is returned back on error too.
Sourcepub fn write_at<B>(&self, buffer: B, offset: u64) -> Result<B, IoBufError<B>>where
B: AlignedBuffer,
pub fn write_at<B>(&self, buffer: B, offset: u64) -> Result<B, IoBufError<B>>where
B: AlignedBuffer,
Write the contents of buffer at offset.
The contents must be as unaligned in memory as offset is in the file, both must have the
same offset within a page, which is the case for a buffer returned by Self::read_at()
at the same offset and for page-aligned buffers written at page-aligned offsets, otherwise
io::ErrorKind::InvalidInput is returned. Whole pages are written directly from the
buffer. Partially written pages at the beginning and the end are read from the file first,
so they must exist in the file, and are written from a page on the stack, serialized with
other such writes. The buffer is returned back on error too.