pub struct File<Device = ()> { /* private fields */ }
Expand description
An encapsulated Linux file descriptor.
The methods of File
are largely just thin wrappers around Linux system
calls that work with file descriptors. Aside from type conversions to make
the API safer and more ergonomic there are no additional userspace
abstractions such as buffering.
When the std
crate feature is enabled, a File
also implements the
std:io
traits Read
, Write
, and Seek
.
A File
can have an optional Device
type parameter, which if set to
an implementation of IODevice
enables the ioctl
method to accept
request constants that are declared as being compatible with that device.
Otherwise, the ioctl
method is unavailable.
Implementations§
Source§impl File<()>
impl File<()>
Sourcepub fn open(path: &CStr, options: OpenOptions<OpenWithoutMode>) -> Result<Self>
pub fn open(path: &CStr, options: OpenOptions<OpenWithoutMode>) -> Result<Self>
Open an existing file.
Use this function for OpenOptions
that don’t require a mode. If you
set the “create” option then you will need to use
Self::open_with_mode
instead, to specify the mode of the new file.
Sourcepub fn open_with_mode(
path: &CStr,
options: OpenOptions<OpenWithMode>,
mode: mode_t,
) -> Result<Self>
pub fn open_with_mode( path: &CStr, options: OpenOptions<OpenWithMode>, mode: mode_t, ) -> Result<Self>
Open a file, creating it if necessary using the given file mode.
Use this function only for OpenOptions
that require a mode. For
most options you can use Self::open
instead.
Sourcepub fn open_raw(path: &CStr, flags: int, mode: mode_t) -> Result<Self>
pub fn open_raw(path: &CStr, flags: int, mode: mode_t) -> Result<Self>
Open a file using the openat
system call.
This function exposes the raw flags
and mode
arguments from the
underlying system call, which the caller must populate appropriately.
Sourcepub fn create_raw(path: &CStr, mode: mode_t) -> Result<Self>
pub fn create_raw(path: &CStr, mode: mode_t) -> Result<Self>
Create a new file using the openat
system call.
This function exposes the raw mode
argument from the underlying
system call, which the caller must populate appropriately.
Sourcepub fn socket<Protocol: SocketProtocol>(
domain: sa_family_t,
typ: sock_type,
protocol: Protocol,
) -> Result<File<Protocol::Device>>
pub fn socket<Protocol: SocketProtocol>( domain: sa_family_t, typ: sock_type, protocol: Protocol, ) -> Result<File<Protocol::Device>>
Create a new socket using the socket
system call.
The protocol is specifed as a special typed constant which carries
both the protocol number expected by the kernel and the device type
to use for the returned file, so the result can accept ioctl
requests that are defined for that specific protocol.
Sourcepub fn socket_raw<Protocol: SocketProtocol>(
domain: sa_family_t,
typ: sock_type,
protocol: int,
) -> Result<File<SocketDevice>>
pub fn socket_raw<Protocol: SocketProtocol>( domain: sa_family_t, typ: sock_type, protocol: int, ) -> Result<File<SocketDevice>>
Create a new socket using the socket
system call without automatically
assigning a device type based on the protocol.
This is similar to Self::socket
but allows specifying any arbitrary
protocol number without needing a special implementation of
super::socket::SocketProtocol
. However, that means that the result
will be typed only as a generic socket and so will not accept any
protocol-specific ioctl
requests.
Source§impl<Device> File<Device>
impl<Device> File<Device>
Sourcepub unsafe fn from_raw_fd(fd: int) -> Self
pub unsafe fn from_raw_fd(fd: int) -> Self
Wrap an existing raw file descriptor into a File
.
Safety:
- The given file descriptor must not belong to an active standard library file or any similar wrapping abstraction.
- The file descriptor must remain open and valid for the full lifetime
of the
File
object. - The same file descriptor must not be wrapped in instances of
File
, because the first one to be dropped will close the file descriptor.
Sourcepub fn duplicate(&self) -> Result<Self>
pub fn duplicate(&self) -> Result<Self>
Creates a new file descriptor referring to the same underlying file
description as self
.
Note that the read/write position of a file is a property of its file description rather than its descriptor, and so modifying the position (and some other aspects) of the new file will also affect the original.
Sourcepub fn open_relative(
&self,
path: &CStr,
options: OpenOptions<OpenWithoutMode>,
) -> Result<File<()>>
pub fn open_relative( &self, path: &CStr, options: OpenOptions<OpenWithoutMode>, ) -> Result<File<()>>
Open a file relative to the current file, which must represent a directory.
Sourcepub fn open_relative_with_mode(
&self,
path: &CStr,
options: OpenOptions<OpenWithMode>,
mode: mode_t,
) -> Result<File<()>>
pub fn open_relative_with_mode( &self, path: &CStr, options: OpenOptions<OpenWithMode>, mode: mode_t, ) -> Result<File<()>>
Open a file relative to the current file, which must represent a directory.
Sourcepub fn open_relative_raw(
&self,
path: &CStr,
flags: int,
mode: mode_t,
) -> Result<File<()>>
pub fn open_relative_raw( &self, path: &CStr, flags: int, mode: mode_t, ) -> Result<File<()>>
Open a file using the openat
system call.
This function exposes the raw flags
and mode
arguments from the
underlying system call, which the caller must populate appropriately.
pub fn fd(&self) -> int
Sourcepub fn into_raw_fd(self) -> int
pub fn into_raw_fd(self) -> int
Consumes the file object and returns the underlying file descriptor without closing it.
Sourcepub fn close(self) -> Result<()>
pub fn close(self) -> Result<()>
Consumes the file object and closes the underlying file descriptor.
If close
fails then the file descriptor is always leaked, because
there is no way to recover it once consumed.
Sourcepub unsafe fn close_mut(&mut self) -> Result<()>
pub unsafe fn close_mut(&mut self) -> Result<()>
Closes the underlying file descriptor without consuming it.
Safety:
- Callers must pass the file to
core::mem::forget
immediately after calling this function to prevent the implicitclose
in theDrop
implementation. - Callers must not use the file object again after calling this method; file descriptor will either be dangling or will be referring to some other unrelated file.
Sourcepub fn read(&self, buf: &mut [u8]) -> Result<usize>
pub fn read(&self, buf: &mut [u8]) -> Result<usize>
Read some bytes from the file into the given buffer, returning the number of bytes that were read.
Sourcepub unsafe fn read_raw(&self, buf: *mut void, count: size_t) -> Result<size_t>
pub unsafe fn read_raw(&self, buf: *mut void, count: size_t) -> Result<size_t>
A thin wrapper around the raw read
system call against this file’s
file descriptor.
Use File::read
as a safe alternative.
Sourcepub fn getdents<'a>(&self, buf: &'a mut [u8]) -> Result<DirEntries<'a>>
pub fn getdents<'a>(&self, buf: &'a mut [u8]) -> Result<DirEntries<'a>>
Read some directory entries from the directory into the given buffer, and obtain an iterator over those directory entries.
The caller must fully-consume the returned iterator; any items not retrieved will be lost.
Once the iterator is dropped the original buffer contains the raw directory entries returned from the kernel, and can be used again for a subsequent call to this function.
Sourcepub fn getdents_all<'file, 'buf, TF, R>(
&'file self,
buf: &'buf mut [u8],
transform: TF,
) -> AllDirEntries<'_, '_, TF, R, Device> ⓘ
pub fn getdents_all<'file, 'buf, TF, R>( &'file self, buf: &'buf mut [u8], transform: TF, ) -> AllDirEntries<'_, '_, TF, R, Device> ⓘ
Read a transformation of every directory entry from the directory.
This wrapper around Self::getdents
returns an iterator that can
call Self::getdents
multiple times to visit all of the entries in
the directory.
However, since all of the calls to Self::getdents
write into the
same buffer buf
it isn’t possible for the iterator to directly
return the borrowed directory entries. Instead, each entry is passed
to the given function transform
, which must then return a
representation of that entry that can outlive the buffer contents.
This admittedly-awkward compromise means that the caller can decide how and whether to allocate memory to retain ownership of the directory entry names, so that this crate can avoid imposing any particular opinion about that.
Sourcepub unsafe fn getdents_raw(
&self,
buf: *mut void,
buf_len: int,
) -> Result<size_t>
pub unsafe fn getdents_raw( &self, buf: *mut void, buf_len: int, ) -> Result<size_t>
A thin wrapper around the raw getdents64
system call against this
file’s file descriptor.
Use File::getdents
as a more convenient alternative.
Sourcepub fn readlink<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]>
pub fn readlink<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]>
Read the content of the symbolic link that the file refers to.
This makes sense only for a file that was opened with the “path only” and “no follow symlinks” options.
Sourcepub fn readlink_relative<'a>(
&self,
path: &CStr,
buf: &'a mut [u8],
) -> Result<&'a [u8]>
pub fn readlink_relative<'a>( &self, path: &CStr, buf: &'a mut [u8], ) -> Result<&'a [u8]>
Read the content of a symbolic link relative to the file, which must represent a directory.
Sourcepub fn exists_relative(&self, path: &CStr) -> Result<bool>
pub fn exists_relative(&self, path: &CStr) -> Result<bool>
Use a statx
system call to determine whether a particular path exists
relative to the file, which must represent a directory.
Sourcepub fn seek(&self, pos: impl Into<SeekFrom>) -> Result<u64>
pub fn seek(&self, pos: impl Into<SeekFrom>) -> Result<u64>
Change the current read/write position of the file.
Sourcepub fn sync(&self) -> Result<()>
pub fn sync(&self) -> Result<()>
Tell the kernel to flush any in-memory buffers and caches for the file.
Sourcepub fn write(&self, buf: &[u8]) -> Result<usize>
pub fn write(&self, buf: &[u8]) -> Result<usize>
Write bytes from the given buffer to the file, returning how many bytes were written.
Sourcepub unsafe fn write_raw(
&self,
buf: *const void,
count: size_t,
) -> Result<size_t>
pub unsafe fn write_raw( &self, buf: *const void, count: size_t, ) -> Result<size_t>
A thin wrapper around the raw write
system call against this file’s
file descriptor.
Use File::write
as a safe alternative.
Sourcepub fn fcntl<'a, Cmd: FcntlCmd<'a>>(
&'a self,
cmd: Cmd,
arg: Cmd::ExtArg,
) -> Result<Cmd::Result>
pub fn fcntl<'a, Cmd: FcntlCmd<'a>>( &'a self, cmd: Cmd, arg: Cmd::ExtArg, ) -> Result<Cmd::Result>
Safe wrapper for the fcntl
system call.
The safety of this wrapper relies on being passed only correct
implementations of fcntl::FcntlCmd
, some of which are predefined
as constants in the fcntl
module.
The type of the argument depends on which cmd
you choose.
Sourcepub unsafe fn fcntl_raw(&self, cmd: int, arg: impl AsRawV) -> Result<int>
pub unsafe fn fcntl_raw(&self, cmd: int, arg: impl AsRawV) -> Result<int>
Direct wrapper around the raw fcntl
system call.
This system call is particularly unsafe because it interprets its
last argument differently depending on the value of cmd
.
Self::fcntl
provides a slightly safer abstraction around this
operation.
Sourcepub unsafe fn to_device<T: IoDevice>(self, devty: T) -> File<T> ⓘ
pub unsafe fn to_device<T: IoDevice>(self, devty: T) -> File<T> ⓘ
Adds a device type parameter to the type of a file, allowing the
Self::ioctl
method to accept request constants that are compatible
with that device type.
Safety:
- Caller must guarantee that the underlying file descriptor really is representing a device of the given type, because the kernel has some overloaded ioctl request numbers that have different meaning depending on driver and using the wrong one can corrupt memory.
Sourcepub unsafe fn ioctl_raw(&self, request: ulong, arg: impl AsRawV) -> Result<int>
pub unsafe fn ioctl_raw(&self, request: ulong, arg: impl AsRawV) -> Result<int>
Direct wrapper around the raw ioctl
system call.
This system call is particularly unsafe because it interprets its
last argument differently depending on the value of request
.
Self::ioctl
provides a slightly safer abstraction around this
operation.
Sourcepub unsafe fn bind_raw(
&self,
addr: *const sockaddr,
addrlen: socklen_t,
) -> Result<()>
pub unsafe fn bind_raw( &self, addr: *const sockaddr, addrlen: socklen_t, ) -> Result<()>
Bind an address to a socket using a raw pointer.
Sourcepub unsafe fn connect_raw(
&self,
addr: *const sockaddr,
addrlen: socklen_t,
) -> Result<()>
pub unsafe fn connect_raw( &self, addr: *const sockaddr, addrlen: socklen_t, ) -> Result<()>
Initiate a connection on a socket using a raw pointer.
Sourcepub fn listen(&self, backlog: int) -> Result<()>
pub fn listen(&self, backlog: int) -> Result<()>
Listen for incoming connections on this socket.
Sourcepub fn getsockopt<'a, O: GetSockOpt<'a>>(&self, opt: O) -> Result<O::Result>
pub fn getsockopt<'a, O: GetSockOpt<'a>>(&self, opt: O) -> Result<O::Result>
Get a socket option for a file descriptor representing a socket.
The value for opt
is typically a constant defined elsewhere in this
crate, or possibly in another crate, which describes both the level
and optname for the underlying call and the type of the result.
Sourcepub unsafe fn getsockopt_raw(
&self,
level: int,
optname: int,
optval: *mut void,
optlen: *mut socklen_t,
) -> Result<int>
pub unsafe fn getsockopt_raw( &self, level: int, optname: int, optval: *mut void, optlen: *mut socklen_t, ) -> Result<int>
Get a socket option for a file descriptor representing a socket using
the raw arguments to the getsockopt
system call.
Sourcepub fn setsockopt<'a, O: SetSockOpt<'a>>(
&self,
opt: O,
arg: O::ExtArg,
) -> Result<O::Result>
pub fn setsockopt<'a, O: SetSockOpt<'a>>( &self, opt: O, arg: O::ExtArg, ) -> Result<O::Result>
Set a socket option for a file descriptor representing a socket.
The value for opt
is typically a constant defined elsewhere in this
crate, or possibly in another crate, which describes both the level
and optname for the underlying call and the type of the argument.
Sourcepub unsafe fn setsockopt_raw(
&self,
level: int,
optname: int,
optval: *const void,
optlen: socklen_t,
) -> Result<int>
pub unsafe fn setsockopt_raw( &self, level: int, optname: int, optval: *const void, optlen: socklen_t, ) -> Result<int>
Set a socket option for a file descriptor representing a socket using
the raw arguments to the setsockopt
system call.
Sourcepub unsafe fn mmap_raw(
&self,
offset: off_t,
length: size_t,
addr: *mut void,
prot: int,
flags: int,
) -> Result<*mut void>
pub unsafe fn mmap_raw( &self, offset: off_t, length: size_t, addr: *mut void, prot: int, flags: int, ) -> Result<*mut void>
Map the file into memory using the mmap
system call.
There is no safe wrapper for this because mapping a file into memory
is inherently unsafe. Callers must take care to ensure they use the
returned pointer in a safe way and to release the mapping with
linux_unsafe::munmap
when it’s no longer needed.
Source§impl<Device: IoDevice> File<Device>
Files that have been marked as representing a particular device type using
File::to_device
can support ioctl
requests that are designated for
that device.
impl<Device: IoDevice> File<Device>
Files that have been marked as representing a particular device type using
File::to_device
can support ioctl
requests that are designated for
that device.
Sourcepub fn ioctl<'a, ReqDevice: IoDevice, Req: IoctlReq<'a, ReqDevice>>(
&'a self,
request: Req,
arg: Req::ExtArg,
) -> Result<Req::Result>where
Device: SubDevice<ReqDevice>,
pub fn ioctl<'a, ReqDevice: IoDevice, Req: IoctlReq<'a, ReqDevice>>(
&'a self,
request: Req,
arg: Req::ExtArg,
) -> Result<Req::Result>where
Device: SubDevice<ReqDevice>,
Safe wrapper for the ioctl
system call.
The safety of this wrapper relies on being passed only correct
implementations of ioctl::IoctlReq
, some of which are predefined
as constants elsewhere in this crate, while others will appear in
device-specific support crates.
The type of the argument depends on which request
you choose.
Some requests expect no argument, in which case you should pass
()
.
Trait Implementations§
Source§impl<Device> AsFd for File<Device>
Available on crate feature std
only.
impl<Device> AsFd for File<Device>
std
only.Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl<Device> Drop for File<Device>
impl<Device> Drop for File<Device>
Source§fn drop(&mut self)
fn drop(&mut self)
Attempts to close the file when it’s no longer in scope.
This implicit close ignores errors, which might cause data loss if
the final commit of data to disk fails. Use File::close
explicitly
if you need to detect errors.
Source§impl FromFcntlResult for File
impl FromFcntlResult for File
unsafe fn prepare_result(raw: int) -> Self
Source§impl<Device: IoDevice> FromIoctlResult<i32> for File<Device>
impl<Device: IoDevice> FromIoctlResult<i32> for File<Device>
fn from_ioctl_result(raw: &int) -> Self
Source§impl<Device> Read for File<Device>
Available on crate feature std
only.
impl<Device> Read for File<Device>
std
only.Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<Device> Seek for File<Device>
Available on crate feature std
only.
impl<Device> Seek for File<Device>
std
only.Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Source§impl<Device> Write for File<Device>
Available on crate feature std
only.
impl<Device> Write for File<Device>
std
only.Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Source§impl<T> Write for File<T>
File
implements core::fmt::Write
by passing UTF-8 encoded bytes
directly to the write
method.
impl<T> Write for File<T>
File
implements core::fmt::Write
by passing UTF-8 encoded bytes
directly to the write
method.