Struct File
pub struct File { /* private fields */ }
Expand description
A reference to an open file on the filesystem.
An instance of a File
can be read and/or written depending on what options
it was opened with. The File
type provides positional read and write
operations. The file does not maintain an internal cursor. The caller is
required to specify an offset when issuing an operation.
While files are automatically closed when they go out of scope, the
operation happens asynchronously in the background. It is recommended to
call the close()
function in order to guarantee that the file successfully
closed before exiting the scope. Closing a file does not guarantee writes
have persisted to disk. Use sync_all
to ensure all writes have reached
the filesystem.
§Examples
Creates a new file and write data to it:
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
// Open a file
let file = File::create("hello.txt").await?;
// Write some data
let (res, buf) = file.write_at(&b"hello world"[..], 0).submit().await;
let n = res?;
println!("wrote {} bytes", n);
// Sync data to the file system.
file.sync_all().await?;
// Close the file
file.close().await?;
Ok(())
})
}
Implementations§
§impl File
impl File
pub async fn open(path: impl AsRef<Path>) -> Result<File, Error>
pub async fn open(path: impl AsRef<Path>) -> Result<File, Error>
Attempts to open a file in read-only mode.
See the OpenOptions::open
method for more details.
§Errors
This function will return an error if path
does not already exist.
Other errors may also be returned according to OpenOptions::open
.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::open("foo.txt").await?;
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn create(path: impl AsRef<Path>) -> Result<File, Error>
pub async fn create(path: impl AsRef<Path>) -> Result<File, Error>
Opens a file in write-only mode.
This function will create a file if it does not exist, and will truncate it if it does.
See the OpenOptions::open
function for more details.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::create("foo.txt").await?;
// Close the file
f.close().await?;
Ok(())
})
}
pub fn from_std(file: File) -> File
pub fn from_std(file: File) -> File
Converts a std::fs::File
to a tokio_uring::fs::File
.
pub async fn read_at<T>(&self, buf: T, pos: u64) -> (Result<usize, Error>, T)where
T: BoundedBufMut,
pub async fn read_at<T>(&self, buf: T, pos: u64) -> (Result<usize, Error>, T)where
T: BoundedBufMut,
Read some bytes at the specified offset from the file into the specified buffer, returning how many bytes were read.
§Return
The method returns the operation result and the same buffer value passed as an argument.
If the method returns [Ok(n)
], then the read was successful. A nonzero
n
value indicates that the buffer has been filled with n
bytes of
data from the file. If n
is 0
, then one of the following happened:
- The specified offset is the end of the file.
- The buffer specified was 0 bytes in length.
It is not an error if the returned value n
is smaller than the buffer
size, even when the file contains enough data to fill the buffer.
§Errors
If this function encounters any form of I/O or other error, an error variant will be returned. The buffer is returned on error.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::open("foo.txt").await?;
let buffer = vec![0; 10];
// Read up to 10 bytes
let (res, buffer) = f.read_at(buffer, 0).await;
let n = res?;
println!("The bytes: {:?}", &buffer[..n]);
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn readv_at<T>(
&self,
bufs: Vec<T>,
pos: u64,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBufMut,
pub async fn readv_at<T>(
&self,
bufs: Vec<T>,
pos: u64,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBufMut,
Read some bytes at the specified offset from the file into the specified array of buffers, returning how many bytes were read.
§Return
The method returns the operation result and the same array of buffers passed as an argument.
If the method returns [Ok(n)
], then the read was successful. A nonzero
n
value indicates that the buffers have been filled with n
bytes of
data from the file. If n
is 0
, then one of the following happened:
- The specified offset is the end of the file.
- The buffers specified were 0 bytes in length.
It is not an error if the returned value n
is smaller than the buffer
size, even when the file contains enough data to fill the buffer.
§Errors
If this function encounters any form of I/O or other error, an error variant will be returned. The buffer is returned on error.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::open("foo.txt").await?;
let buffers = vec![Vec::<u8>::with_capacity(10), Vec::<u8>::with_capacity(10)];
// Read up to 20 bytes
let (res, buffer) = f.readv_at(buffers, 0).await;
let n = res?;
println!("Read {} bytes", n);
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn writev_at<T>(
&self,
buf: Vec<T>,
pos: u64,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBuf,
pub async fn writev_at<T>(
&self,
buf: Vec<T>,
pos: u64,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBuf,
Write data from buffers into this file at the specified offset, returning how many bytes were written.
This function will attempt to write the entire contents of bufs
, but
the entire write may not succeed, or the write may also generate an
error. The bytes will be written starting at the specified offset.
§Return
The method returns the operation result and the same array of buffers passed
in as an argument. A return value of 0
typically means that the
underlying file is no longer able to accept bytes and will likely not be
able to in the future as well, or that the buffer provided is empty.
§Errors
Each call to write
may generate an I/O error indicating that the
operation could not be completed. If an error is returned then no bytes
in the buffer were written to this writer.
It is not considered an error if the entire buffer could not be written to this writer.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let file = File::create("foo.txt").await?;
// Writes some prefix of the byte string, not necessarily all of it.
let bufs = vec!["some".to_owned().into_bytes(), " bytes".to_owned().into_bytes()];
let (res, _) = file.writev_at(bufs, 0).await;
let n = res?;
println!("wrote {} bytes", n);
// Close the file
file.close().await?;
Ok(())
})
}
pub async fn writev_at_all<T>(
&self,
buf: Vec<T>,
pos: Option<u64>,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBuf,
pub async fn writev_at_all<T>(
&self,
buf: Vec<T>,
pos: Option<u64>,
) -> (Result<usize, Error>, Vec<T>)where
T: BoundedBuf,
Like writev_at
but will call the io_uring
writev
operation multiple times if
necessary.
Parameter pos
is an Option<u64>
to allow this function to be used for both files that
are seekable and those that are not. The caller is responsible for knowing this.
When None
is supplied, the offset passed to the io_uring
call will always be zero, even
if multiple writev calls are necessary; only the iovec information would be adjusted
between calls. A Unix pipe would fall into this category.
When Some(n)
is suppied, the offset passed to the writev call will be incremented by the
progress of prior writev calls. A file system’s regular file would fall into this category.
If the caller passes Some(n)
for a file that is not seekable, the io_uring
writev
operation will return an error once n is not zero.
If the caller passes None
, when the file is seekable, when multiple writev
calls are
required to complete the writing of all the bytes, the bytes at position 0 of the file will
have been overwritten one or more times with incorrect data. This is true just as if the
caller had invoked seperate write calls to a file, all with position 0, when in fact the
file was seekable.
Performance considerations:
The user may want to check that this function is necessary in their use case or performs better than a series of write_all operations would. There is overhead either way and it is not clear which should be faster or give better throughput.
This function causes the temporary allocation of a Vec one time to hold the array of iovec that is passed to the kernel. The same array is used for any subsequent calls to get all the bytes written. Whereas individual calls to write_all do not require the Vec to be allocated, they do each incur the normal overhead of setting up the submission and completion structures and going through the future poll mechanism.
TODO decide, would a separate writev_all
function for file
that did not take a pos
make things less ambiguous?
TODO more complete documentation here.
TODO define writev_all functions for net/unix/stream, net/tcp/stream, io/socket.
TODO remove usize from result, to be consistent with other write_all_vectored functions.
TODO find a way to test this with some stress to the file so the writev calls don’t all
succeed on their first try.
TODO consider replacing the current write_all
and write_all_at
functions with a similar
mechanism so all the write-all logic is in one place, in the io/write_all.rs file.
pub async fn read_exact_at<T>(&self, buf: T, pos: u64) -> (Result<(), Error>, T)where
T: BoundedBufMut,
pub async fn read_exact_at<T>(&self, buf: T, pos: u64) -> (Result<(), Error>, T)where
T: BoundedBufMut,
Read the exact number of bytes required to fill buf
at the specified
offset from the file.
This function reads as many as bytes as necessary to completely fill the
specified buffer buf
.
§Return
The method returns the operation result and the same buffer value passed as an argument.
If the method returns [Ok(())
], then the read was successful.
§Errors
If this function encounters an “end of file” before completely filling
the buffer, it returns an error of the kind ErrorKind::UnexpectedEof
.
The buffer is returned on error.
If this function encounters any form of I/O or other error, an error variant will be returned. The buffer is returned on error.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::open("foo.txt").await?;
let buffer = Vec::with_capacity(10);
// Read up to 10 bytes
let (res, buffer) = f.read_exact_at(buffer, 0).await;
res?;
println!("The bytes: {:?}", buffer);
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn read_fixed_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<usize, Error>, T)where
T: BoundedBufMut<BufMut = FixedBuf>,
pub async fn read_fixed_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<usize, Error>, T)where
T: BoundedBufMut<BufMut = FixedBuf>,
Like read_at
, but using a pre-mapped buffer
registered with FixedBufRegistry
.
§Errors
In addition to errors that can be reported by read_at
,
this operation fails if the buffer is not registered in the
current tokio-uring
runtime.
§Examples
use tokio_uring::fs::File;
use tokio_uring::buf::fixed::FixedBufRegistry;
use tokio_uring::buf::BoundedBuf;
use std::iter;
tokio_uring::start(async {
let registry = FixedBufRegistry::new(iter::repeat(vec![0; 10]).take(10));
registry.register()?;
let f = File::open("foo.txt").await?;
let buffer = registry.check_out(2).unwrap();
// Read up to 10 bytes
let (res, buffer) = f.read_fixed_at(buffer, 0).await;
let n = res?;
println!("The bytes: {:?}", &buffer[..n]);
// Close the file
f.close().await?;
Ok(())
})
pub fn write_at<T>(
&self,
buf: T,
pos: u64,
) -> UnsubmittedOneshot<WriteData<T>, WriteTransform<T>>where
T: BoundedBuf,
pub fn write_at<T>(
&self,
buf: T,
pos: u64,
) -> UnsubmittedOneshot<WriteData<T>, WriteTransform<T>>where
T: BoundedBuf,
Write a buffer into this file at the specified offset, returning how many bytes were written.
This function will attempt to write the entire contents of buf
, but
the entire write may not succeed, or the write may also generate an
error. The bytes will be written starting at the specified offset.
§Return
The method returns the operation result and the same buffer value passed
in as an argument. A return value of 0
typically means that the
underlying file is no longer able to accept bytes and will likely not be
able to in the future as well, or that the buffer provided is empty.
§Errors
Each call to write
may generate an I/O error indicating that the
operation could not be completed. If an error is returned then no bytes
in the buffer were written to this writer.
It is not considered an error if the entire buffer could not be written to this writer.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let file = File::create("foo.txt").await?;
// Writes some prefix of the byte string, not necessarily all of it.
let (res, _) = file.write_at(&b"some bytes"[..], 0).submit().await;
let n = res?;
println!("wrote {} bytes", n);
// Close the file
file.close().await?;
Ok(())
})
}
pub async fn write_all_at<T>(&self, buf: T, pos: u64) -> (Result<(), Error>, T)where
T: BoundedBuf,
pub async fn write_all_at<T>(&self, buf: T, pos: u64) -> (Result<(), Error>, T)where
T: BoundedBuf,
Attempts to write an entire buffer into this file at the specified offset.
This method will continuously call write_at
until there is no more data
to be written or an error is returned.
This method will not return until the entire buffer has been successfully
written or an error occurs.
If the buffer contains no data, this will never call write_at
.
§Return
The method returns the operation result and the same buffer value passed in as an argument.
§Errors
This function will return the first error that write_at
returns.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let file = File::create("foo.txt").await?;
// Writes some prefix of the byte string, not necessarily all of it.
let (res, _) = file.write_all_at(&b"some bytes"[..], 0).await;
res?;
println!("wrote all bytes");
// Close the file
file.close().await?;
Ok(())
})
}
pub async fn write_fixed_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<usize, Error>, T)where
T: BoundedBuf<Buf = FixedBuf>,
pub async fn write_fixed_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<usize, Error>, T)where
T: BoundedBuf<Buf = FixedBuf>,
Like write_at
, but using a pre-mapped buffer
registered with FixedBufRegistry
.
§Errors
In addition to errors that can be reported by write_at
,
this operation fails if the buffer is not registered in the
current tokio-uring
runtime.
§Examples
use tokio_uring::fs::File;
use tokio_uring::buf::fixed::FixedBufRegistry;
use tokio_uring::buf::BoundedBuf;
tokio_uring::start(async {
let registry = FixedBufRegistry::new([b"some bytes".to_vec()]);
registry.register()?;
let file = File::create("foo.txt").await?;
let buffer = registry.check_out(0).unwrap();
// Writes some prefix of the buffer content,
// not necessarily all of it.
let (res, _) = file.write_fixed_at(buffer, 0).await;
let n = res?;
println!("wrote {} bytes", n);
// Close the file
file.close().await?;
Ok(())
})
pub async fn write_fixed_all_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<(), Error>, T)where
T: BoundedBuf<Buf = FixedBuf>,
pub async fn write_fixed_all_at<T>(
&self,
buf: T,
pos: u64,
) -> (Result<(), Error>, T)where
T: BoundedBuf<Buf = FixedBuf>,
Attempts to write an entire buffer into this file at the specified offset.
This method will continuously call write_fixed_at
until there is no more data
to be written or an error is returned.
This method will not return until the entire buffer has been successfully
written or an error occurs.
If the buffer contains no data, this will never call write_fixed_at
.
§Return
The method returns the operation result and the same buffer value passed in as an argument.
§Errors
This function will return the first error that write_fixed_at
returns.
pub async fn sync_all(&self) -> Result<(), Error>
pub async fn sync_all(&self) -> Result<(), Error>
Attempts to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-memory data reaches the filesystem before completing.
This can be used to handle errors that would otherwise only be caught
when the File
is closed. Dropping a file will ignore errors in
synchronizing this in-memory data.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::create("foo.txt").await?;
let (res, buf) = f.write_at(&b"Hello, world!"[..], 0).submit().await;
let n = res?;
f.sync_all().await?;
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn sync_data(&self) -> Result<(), Error>
pub async fn sync_data(&self) -> Result<(), Error>
Attempts to sync file data to disk.
This method is similar to sync_all
, except that it may not
synchronize file metadata to the filesystem.
This is intended for use cases that must synchronize content, but don’t need the metadata on disk. The goal of this method is to reduce disk operations.
Note that some platforms may simply implement this in terms of
sync_all
.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::create("foo.txt").await?;
let (res, buf) = f.write_at(&b"Hello, world!"[..], 0).submit().await;
let n = res?;
f.sync_data().await?;
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn fallocate(
&self,
offset: u64,
len: u64,
flags: i32,
) -> Result<(), Error>
pub async fn fallocate( &self, offset: u64, len: u64, flags: i32, ) -> Result<(), Error>
Manipulate the allocated disk space of the file.
The manipulated range starts at the offset
and continues for len
bytes.
The specific manipulation to the allocated disk space are specified by
the flags
, to understand what are the possible values here check
the fallocate(2)
man page.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
let f = File::create("foo.txt").await?;
// Allocate a 1024 byte file setting all the bytes to zero
f.fallocate(0, 1024, libc::FALLOC_FL_ZERO_RANGE).await?;
// Close the file
f.close().await?;
Ok(())
})
}
pub async fn close(self) -> Result<(), Error>
pub async fn close(self) -> Result<(), Error>
Closes the file using the uring asynchronous close operation and returns the possible error as described in the close(2) man page.
The programmer has the choice of calling this asynchronous close and waiting for the result or letting the library close the file automatically and simply letting the file go out of scope and having the library close the file descriptor automatically and synchronously.
Calling this asynchronous close is to be preferred because it returns the close result which as the man page points out, should not be ignored. This asynchronous close also avoids the synchronous close system call and may result in better throughput as the thread is not blocked during the close.
§Examples
use tokio_uring::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio_uring::start(async {
// Open the file
let f = File::open("foo.txt").await?;
// Close the file
f.close().await?;
Ok(())
})
}
§impl File
impl File
pub async fn statx(&self) -> Result<statx, Error>
pub async fn statx(&self) -> Result<statx, Error>
Returns statx(2) metadata for an open file via a uring call.
The libc::statx structure returned is described in the statx(2) man page.
This high level version of the statx function uses flags
set to libc::AT_EMPTY_PATH and
mask
set to libc::STATX_ALL which are described in the same man page.
More specific uring statx(2) calls can be made with the StatxBuilder.
§Examples
use tokio_uring::fs::File;
tokio_uring::start(async {
let f = File::create("foo.txt").await.unwrap();
// Fetch file metadata
let statx = f.statx().await.unwrap();
// Close the file
f.close().await.unwrap();
})
pub fn statx_builder(&self) -> StatxBuilder
pub fn statx_builder(&self) -> StatxBuilder
Returns a builder that can return statx(2) metadata for an open file using the uring device.
flags
and mask
can be changed from their defaults and a path
that is absolule or
relative can also be provided.
flags
defaults to libc::AT_EMPTY_PATH.
mask
defaults to libc::STATX_ALL.
Refer to statx(2) for details on the arguments and the returned value.
A little from the man page:
- statx(2) uses path, dirfd, and flags to identify the target file.
- statx(2) uses mask to tell the kernel which fields the caller is interested in.
§Examples
use tokio_uring::fs::File;
tokio_uring::start(async {
let f = File::create("foo.txt").await.unwrap();
// Fetch file metadata
let statx = f.statx_builder()
.flags(libc::AT_NO_AUTOMOUNT)
.statx().await.unwrap();
// Close the file
f.close().await.unwrap();
})