Skip to main content

File

Struct File 

Source
pub struct File { /* private fields */ }
Expand description

An asynchronous file handle backed by either a remote VFS client or the local process.

File handles implement Tokio’s asynchronous read, write, and seek traits.

Clones share one open file, as duplicated descriptors do: one cursor position, one set of locks, and one closed state. Closing or handing off the file through any clone closes it for all of them, and their later operations fail with ErrorKind::Closed. Operations in flight are not shared: a clone starts with none, and its reads and writes do not wait for another clone’s.

Implementations§

Source§

impl File

Source

pub fn poll_read_uninit( &mut self, cx: &mut Context<'_>, buf: &mut [MaybeUninit<u8>], ) -> Poll<Result<usize>>

Reads at the cursor into uninitialized storage, returning how many bytes at its front were filled. Zero means end of file, unless buf is empty.

This is [AsyncRead::poll_read] without the requirement that buf be initialized, so a caller reading into fresh memory need not zero it first. The remote backend writes the reply straight into buf.

Source§

impl File

Source

pub async fn close(self) -> Result<()>

Closes the file, for every clone. Closing a closed file does nothing.

Source

pub fn read_at<'b>( &self, buf: &'b mut BytesMut, offset: u64, ) -> impl Future<Output = Result<usize>> + Send + use<'b>

Reads into buf’s spare capacity starting at offset, returning the transfer count.

Cancelling the read may leave buf empty.

Fewer bytes than the spare capacity available may be read. 0 indicates either end-of-file or that buf had no spare capacity.

Source

pub fn write_at( &self, data: Bytes, offset: u64, ) -> impl Future<Output = Result<usize>> + Send + use<>

Writes data at offset, returning the byte count.

May write less than all of data on success, but at least 1 byte unless data is empty. Use on an append-mode handle is an error; use append there.

Source

pub fn append( &self, data: Bytes, ) -> impl Future<Output = Result<(usize, u64)>> + Send + use<>

Appends data, returning the byte count and the position just past what was written. Less data may be written than requested on success, but at least 1 byte will be written unless data was empty.

Source

pub fn read_at_into<'b>( &self, buf: &'b mut [MaybeUninit<u8>], offset: u64, ) -> impl Future<Output = Result<usize>> + Send + use<'b>

Reads into the possibly uninitialized `buf``, returning how many bytes at its front were filled.

Source

pub fn write_at_from<'b>( &self, data: &'b [u8], offset: u64, ) -> impl Future<Output = Result<usize>> + Send + use<'b>

Writes data at offset from borrowed storage, returning the byte count.

Less data may be written than requested on success, but always at least 1 byte unless data is empty.

Source

pub async fn copy_data( &self, dst: &File, src_offset: u64, target: CopyDest, len: Option<u64>, mode: CopyMode, ) -> Result<CopyDataResult>

Copies at most 2 MiB from this file into dst.

len of None copies toward end of source. A positive result may be short for any reason; callers that want a complete operation must loop. Zero indicates end of source (or an empty request).

Nothing is promised about physical allocation: an accelerated route may preserve holes, and the fallback does not.

Source

pub async fn set_size(&self, size: u64) -> Result<()>

Changes the file length to size bytes.

Source

pub async fn sync(&self, data: bool) -> Result<()>

Flushes the file’s written data to durable storage, returning once the device reports it committed.

data selects a data-only flush (fdatasync), which may skip metadata the caller does not need — notably the modification time — and so can avoid a second write to the inode. Size changes are still flushed, since a reader could not find the data without them.

This durably places the contents. It says nothing about the directory entry naming the file, which is a separate inode and needs its own flush to survive a crash.

Source

pub async fn metadata(&self) -> Result<Metadata>

Returns metadata for the open file.

Source

pub async fn fs_metadata(&self) -> Result<FsMetadata>

Returns metadata for the filesystem containing the open file.

Source

pub async fn acl(&self, kind: AclKind, default: bool) -> Result<Option<Acl>>

Returns the ACL of the requested kind. For a POSIX ACL, default selects the directory’s default ACL rather than its access ACL; it must be false for AclKind::Nfs4.

Source

pub async fn set_acl( &self, kind: AclKind, acl: Option<&Acl>, default: bool, ) -> Result<()>

Sets or removes the ACL of kind. acl, if present, must match kind. default selects the POSIX default ACL, as in acl; it must be false for AclKind::Nfs4.

Source

pub async fn sec_desc(&self, mask: SecInfo) -> Result<SecDesc>

Returns the Windows security descriptor selected by mask.

Source

pub async fn update_sec_desc(&self, sec_desc: &SecDesc) -> Result<()>

Replaces the Windows security descriptor.

Source

pub async fn xattrs( &self, namespace: XattrNamespace<'_>, ) -> Result<Vec<XattrEntry>>

Lists extended attributes in namespace.

Source

pub async fn xattr( &self, name: &str, namespace: Option<&str>, ) -> Result<Vec<u8>>

Reads one extended attribute.

Source

pub async fn streams(&self) -> Result<Vec<StreamEntry>>

Lists alternate data streams.

Source

pub async fn set_xattr( &self, name: &str, namespace: Option<&str>, value: &[u8], ) -> Result<()>

Creates or replaces an extended attribute.

Source

pub async fn remove_xattr( &self, name: &str, namespace: Option<&str>, ) -> Result<()>

Removes an extended attribute.

Source

pub async fn lock( &self, range: FileLockRange, mode: FileLockMode, behavior: FileLockBehavior, ) -> Result<Option<FileLock>>

Acquires a byte-range lock with the requested mode and behavior.

Source

pub async fn try_into_std(self) -> Result<File, Self>

Converts this handle into a local standard-library file when possible, taking it from every clone.

Surrendering the descriptor means surrendering the cursor with it, so the file comes back positioned where this handle is.

Trait Implementations§

Source§

impl AsyncRead for File

Source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize>>

Attempt to read from the AsyncRead into buf. Read more
§

fn poll_read_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>], ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
Source§

impl AsyncSeek for File

Source§

fn poll_seek( self: Pin<&mut Self>, cx: &mut Context<'_>, position: SeekFrom, ) -> Poll<Result<u64>>

An end-relative seek has to ask for the file’s length, so it may be pending; polling again with the same position drives that query.

Source§

impl AsyncWrite for File

Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Each write reaches the file when it completes, so there is nothing buffered to push; flushing only has to settle work in flight.

Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Only flushes: the file is closed by File::close, or when dropped.

Source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Source§

impl Clone for File

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for File

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<File> for ChildIn

Source§

fn from(file: File) -> Self

Converts to this type from the input type.
Source§

impl From<File> for ChildOut

Source§

fn from(file: File) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !RefUnwindSafe for File

§

impl !UnwindSafe for File

§

impl Freeze for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnsafeUnpin for File

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

§

fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized, R: AsyncRead,

Creates an adaptor which will chain this stream with another. Read more
§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> ⓘ
where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectored<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToString<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
where Self: Sized + AsyncWrite,

Helper method for splitting this read/write object into two halves. Read more
§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
§

impl<S> AsyncSeekExt for S
where S: AsyncSeek + ?Sized,

§

fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self> ⓘ
where Self: Unpin,

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more
§

fn stream_position(&mut self) -> Seek<'_, Self> ⓘ
where Self: Unpin,

Creates a future which will return the current seek position from the start of the stream. Read more
§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

§

fn flush(&mut self) -> Flush<'_, Self> ⓘ
where Self: Unpin,

Creates a future which will entirely flush this AsyncWrite. Read more
§

fn close(&mut self) -> Close<'_, Self> ⓘ
where Self: Unpin,

Creates a future which will entirely close this AsyncWrite.
§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will write bytes from buf into the object. Read more
§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> WriteVectored<'a, Self> ⓘ
where Self: Unpin,

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> ⓘ
where Self: Unpin,

Write data into this object. Read more
§

fn write_all_vectored<'a>( &'a mut self, bufs: &'a mut [IoSlice<'a>], ) -> WriteAllVectored<'a, Self> ⓘ
where Self: Unpin,

Available on crate feature write-all-vectored only.
Attempts to write multiple buffers into this writer. Read more
§

fn into_sink<Item>(self) -> IntoSink<Self, Item>
where Item: AsRef<[u8]>, Self: Sized,

Available on crate feature sink only.
Allow using an [AsyncWrite] as a Sink<Item: AsRef<[u8]>>. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FuturesAsyncReadCompatExt for T
where T: AsyncRead,

§

fn compat(self) -> Compat<Self>
where Self: Sized,

Wraps self with a compatibility layer that implements tokio_io::AsyncRead.
§

impl<T> FuturesAsyncWriteCompatExt for T
where T: AsyncWrite,

§

fn compat_write(self) -> Compat<Self>
where Self: Sized,

Wraps self with a compatibility layer that implements tokio::io::AsyncWrite.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.