Struct cap_std::fs::File

source ·
pub struct File { /* private fields */ }
Expand description

A reference to an open file on a filesystem.

This corresponds to std::fs::File.

This File has no open or create methods. To open or create a file, first obtain a Dir containing the path, and then call Dir::open or Dir::create.

Implementations§

Constructs a new instance of Self from the given std::fs::File.

This grants access the resources the std::fs::File instance already has access to.

Examples found in repository?
src/fs_utf8/file.rs (line 46)
45
46
47
    pub fn from_std(std: fs::File) -> Self {
        Self::from_cap_std(crate::fs::File::from_std(std))
    }
More examples
Hide additional examples
src/fs/dir_entry.rs (line 30)
28
29
30
31
32
33
34
35
36
37
38
    pub fn open(&self) -> io::Result<File> {
        let file = self.inner.open()?;
        Ok(File::from_std(file))
    }

    /// Open the file with the given options.
    #[inline]
    pub fn open_with(&self, options: &OpenOptions) -> io::Result<File> {
        let file = self.inner.open_with(options)?;
        Ok(File::from_std(file))
    }
src/fs/file.rs (line 94)
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    pub fn try_clone(&self) -> io::Result<Self> {
        let file = self.std.try_clone()?;
        Ok(Self::from_std(file))
    }

    /// Changes the permissions on the underlying file.
    ///
    /// This corresponds to [`std::fs::File::set_permissions`].
    #[inline]
    pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
        self.std
            .set_permissions(permissions_into_std(&self.std, perm)?)
    }

    /// Constructs a new instance of `Self` in read-only mode by opening the
    /// given path as a file using the host process' ambient authority.
    ///
    /// # Ambient Authority
    ///
    /// This function is not sandboxed and may access any path that the host
    /// process has access to.
    #[inline]
    pub fn open_ambient<P: AsRef<Path>>(
        path: P,
        ambient_authority: AmbientAuthority,
    ) -> io::Result<Self> {
        let std = open_ambient(
            path.as_ref(),
            OpenOptions::new().read(true),
            ambient_authority,
        )?;
        Ok(Self::from_std(std))
    }

    /// Constructs a new instance of `Self` with the options specified by
    /// `options` by opening the given path as a file using the host process'
    /// ambient authority.
    ///
    /// # Ambient Authority
    ///
    /// This function is not sandboxed and may access any path that the host
    /// process has access to.
    #[inline]
    pub fn open_ambient_with<P: AsRef<Path>>(
        path: P,
        options: &OpenOptions,
        ambient_authority: AmbientAuthority,
    ) -> io::Result<Self> {
        let std = open_ambient(path.as_ref(), options, ambient_authority)?;
        Ok(Self::from_std(std))
    }

    /// Returns a new `OpenOptions` object.
    ///
    /// This corresponds to [`std::fs::File::options`].
    #[must_use]
    #[inline]
    pub fn options() -> OpenOptions {
        OpenOptions::new()
    }
}

#[inline]
fn metadata_from(file: &fs::File) -> io::Result<Metadata> {
    Metadata::from_file(file)
}

#[inline]
fn permissions_into_std(file: &fs::File, permissions: Permissions) -> io::Result<fs::Permissions> {
    permissions.into_std(file)
}

// Safety: `FilelikeViewType` is implemented for `std::fs::File`.
unsafe impl io_lifetimes::views::FilelikeViewType for File {}

#[cfg(not(windows))]
impl FromRawFd for File {
    #[inline]
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self::from_std(fs::File::from_raw_fd(fd))
    }
}

#[cfg(not(windows))]
impl From<OwnedFd> for File {
    #[inline]
    fn from(fd: OwnedFd) -> Self {
        Self::from_std(fs::File::from(fd))
    }
src/fs/dir.rs (line 94)
92
93
94
95
    fn _open_with(&self, path: &Path, options: &OpenOptions) -> io::Result<File> {
        let dir = open(&self.std_file, path, options)?;
        Ok(File::from_std(dir))
    }

Consumes self and returns a std::fs::File.

Examples found in repository?
src/fs_utf8/file.rs (line 58)
57
58
59
    pub fn into_std(self) -> fs::File {
        self.cap_std.into_std()
    }

Attempts to sync all OS-internal metadata to disk.

This corresponds to std::fs::File::sync_all.

Examples found in repository?
src/fs_utf8/file.rs (line 66)
65
66
67
    pub fn sync_all(&self) -> io::Result<()> {
        self.cap_std.sync_all()
    }

This function is similar to sync_all, except that it may not synchronize file metadata to a filesystem.

This corresponds to std::fs::File::sync_data.

Examples found in repository?
src/fs_utf8/file.rs (line 75)
74
75
76
    pub fn sync_data(&self) -> io::Result<()> {
        self.cap_std.sync_data()
    }

Truncates or extends the underlying file, updating the size of this file to become size.

This corresponds to std::fs::File::set_len.

Examples found in repository?
src/fs_utf8/file.rs (line 84)
83
84
85
    pub fn set_len(&self, size: u64) -> io::Result<()> {
        self.cap_std.set_len(size)
    }

Queries metadata about the underlying file.

This corresponds to std::fs::File::metadata.

Examples found in repository?
src/fs_utf8/file.rs (line 92)
91
92
93
    pub fn metadata(&self) -> io::Result<Metadata> {
        self.cap_std.metadata()
    }
More examples
Hide additional examples
src/fs/dir.rs (line 830)
826
827
828
829
830
831
fn initial_buffer_size(file: &File) -> usize {
    // Allocate one extra byte so the buffer doesn't need to grow before the
    // final `read` call at the end of the file. Don't worry about `usize`
    // overflow because reading will fail regardless in that case.
    file.metadata().map(|m| m.len() as usize + 1).unwrap_or(0)
}

Creates a new File instance that shares the same underlying file handle as the existing File instance.

This corresponds to std::fs::File::try_clone.

Examples found in repository?
src/fs_utf8/file.rs (line 101)
100
101
102
    pub fn try_clone(&self) -> io::Result<Self> {
        Ok(Self::from_cap_std(self.cap_std.try_clone()?))
    }

Changes the permissions on the underlying file.

This corresponds to std::fs::File::set_permissions.

Examples found in repository?
src/fs_utf8/file.rs (line 109)
108
109
110
    pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
        self.cap_std.set_permissions(perm)
    }

Constructs a new instance of Self in read-only mode by opening the given path as a file using the host process’ ambient authority.

Ambient Authority

This function is not sandboxed and may access any path that the host process has access to.

Examples found in repository?
src/fs_utf8/file.rs (lines 125-128)
120
121
122
123
124
125
126
127
128
129
    pub fn open_ambient<P: AsRef<Utf8Path>>(
        path: P,
        ambient_authority: AmbientAuthority,
    ) -> io::Result<Self> {
        let path = from_utf8(path.as_ref())?;
        Ok(Self::from_cap_std(crate::fs::File::open_ambient(
            path,
            ambient_authority,
        )?))
    }

Constructs a new instance of Self with the options specified by options by opening the given path as a file using the host process’ ambient authority.

Ambient Authority

This function is not sandboxed and may access any path that the host process has access to.

Examples found in repository?
src/fs_utf8/file.rs (lines 146-150)
140
141
142
143
144
145
146
147
148
149
150
151
    pub fn open_ambient_with<P: AsRef<Utf8Path>>(
        path: P,
        options: &OpenOptions,
        ambient_authority: AmbientAuthority,
    ) -> io::Result<Self> {
        let path = from_utf8(path.as_ref())?;
        Ok(Self::from_cap_std(crate::fs::File::open_ambient_with(
            path,
            options,
            ambient_authority,
        )?))
    }

Returns a new OpenOptions object.

This corresponds to std::fs::File::options.

Trait Implementations§

Borrows the file descriptor. Read more
Extracts the raw file descriptor. Read more
Formats the value using the given formatter. Read more
Reads a number of bytes starting from a given offset. Read more
Writes a number of bytes starting from a given offset. Read more
Reads the exact number of byte required to fill buf from the given offset. Read more
Attempts to write an entire buffer starting from a given offset. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Constructs a new instance of Self from the given raw file descriptor. Read more
Consumes this object, returning the raw underlying file descriptor. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
Read the exact number of bytes required to fill buf. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
Read the exact number of bytes required to fill buf. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Seek to an offset, in bytes, in a stream. Read more
Returns the current seek position from the start of the stream. Read more
Rewind to the beginning of a stream. Read more
🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
Seek to an offset, in bytes, in a stream. Read more
Returns the current seek position from the start of the stream. Read more
Rewind to the beginning of a stream. Read more
🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Borrows the reference. Read more
Return a borrowing view of a resource which dereferences to a &Target. Read more
Extracts the grip.
Returns the raw value.
Extracts the raw grip.
Returns the raw value.
Borrows the reference.
Return a borrowing view of a resource which dereferences to a &Target. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

👎Deprecated since 1.0.0: FromFd::from_fd is replaced by From<OwnedFd>::from
Constructs a new instance of Self from the given file descriptor. Read more
Constructs a new instance of Self from the given file descriptor converted from into_owned. Read more
Constructs a new instance of Self from the given filelike object. Read more
Constructs a new instance of Self from the given filelike object converted from into_owned. Read more
Consume an OwnedGrip and convert into a Self.
Constructs Self from the raw value. Read more
Consume an RawGrip and convert into a Self. Read more
Constructs Self from the raw value. Read more
Constructs a new instance of Self from the given socketlike object.
Constructs a new instance of Self from the given socketlike object converted from into_owned.

Calls U::from(self).

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

👎Deprecated since 1.0.0: IntoFd is replaced by From<...> for OwnedFd or Into<OwnedFd>
Consumes this object, returning the underlying file descriptor. Read more
Consumes this object, returning the underlying filelike object. Read more
Consume self and convert into an OwnedGrip.
Returns the raw value.
Consume self and convert into an RawGrip.
Returns the raw value.
Consumes this object, returning the underlying socketlike object.
Set the last access and last modification timestamps of an open file handle. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.