pub struct File(/* private fields */);Expand description
A reference to an open file on the filesystem.
Implementations§
Source§impl File
impl File
Sourcepub async fn open(path: impl AsRef<Path>) -> Result<Self>
pub async fn open(path: impl AsRef<Path>) -> Result<Self>
Attempts to open a file in read-only mode.
See std::fs::OpenOptions for more details.
§Errors
This function will return an error if called from outside of the Tokio runtime (if async) or if path does not already exist. Other errors may also be returned according to OpenOptions::open.
See https://docs.rs/rustc-std-workspace-std/latest/std/fs/struct.File.html#method.open
Sourcepub async fn create(path: impl AsRef<Path>) -> Result<Self>
pub async fn create(path: impl AsRef<Path>) -> Result<Self>
Attempts to open a file in read-only mode with buffering.
§Errors
This function will return an error if path does not already exist,
or if memory allocation fails for the new buffer.
Other errors may also be returned according to std::fs::OpenOptions::open.
See https://docs.rs/rustc-std-workspace-std/latest/std/fs/struct.File.html#method.create
Sourcepub async fn create_new(path: impl AsRef<Path>) -> Result<Self>
pub async fn create_new(path: impl AsRef<Path>) -> Result<Self>
Opens a file in read-write mode.
This function will create a file if it does not exist, or return an error if it does. This way, if the call succeeds, the file returned is guaranteed to be new.
This option is useful because it is atomic. Otherwise between checking whether a file exists and creating a new one, the file may have been created by another process (a TOCTOU race condition / attack).
This can also be written using File::options().read(true).write(true).create_new(true).open(...).
See std::fs::OpenOptions for more details.
See https://docs.rs/rustc-std-workspace-std/latest/std/fs/struct.File.html#method.create_new
Sourcepub fn open_options() -> OpenOptions
pub fn open_options() -> OpenOptions
Returns a new OpenOptions object.
This function returns a new OpenOptions object that you can use to open or create a file with specific options if open() or create() are not appropriate.
It is equivalent to OpenOptions::new, but allows you to write more readable code. Instead of OpenOptions::new().append(true).open("example.log"), you can write File::options().append(true).open("example.log").await.
This also avoids the need to import OpenOptions.
See the OpenOptions::new function for more details.
Sourcepub async fn set_len(&self, size: u64) -> Result<()>
pub async fn set_len(&self, size: u64) -> Result<()>
Truncates or extends 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.
§Errors
This function will return an error if the file is not opened for writing.
Sourcepub async fn set_permissions(&self, perm: Permissions) -> Result<()>
pub async fn set_permissions(&self, perm: Permissions) -> Result<()>
Changes the permissions on the underlying file.
Platform-specific behavior This function currently corresponds to the fchmod function on Unix and the SetFileInformationByHandle function on Windows. Note that, this may change in the future.
§Errors
This function will return an error if the user lacks permission change attributes on the underlying file. It may also return an error in other os-specific unspecified cases.
Sourcepub async fn sync_all(&self) -> Result<()>
pub async fn sync_all(&self) -> Result<()>
Attempts to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-core data reaches the filesystem before returning.
Sourcepub async fn sync_data(&self) -> Result<()>
pub async fn sync_data(&self) -> Result<()>
This function is similar to Self::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.
Sourcepub async fn to_std(self) -> File
pub async fn to_std(self) -> File
Converts the File inner instance to a std::fs::File instance if it is currently a tokio::fs::File.
This can be useful when you need for instance to pass an impl std::io::Write to a function.
Trait Implementations§
Source§impl AsFd for File
impl AsFd for File
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl FromRawFd for File
impl FromRawFd for File
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 more