pub struct Dir {
inner: Dir,
}dirfd)Expand description
An object providing access to a directory on the filesystem.
Directories are automatically closed when they go out of scope. Errors detected
on closing are ignored by the implementation of Drop.
§Platform-specific behavior
On supported systems (including Windows and some UNIX-based OSes), this function acquires a
handle/file descriptor for the directory. This allows functions like Dir::open_file to
avoid TOCTOU errors when the directory itself is being moved.
On other systems, it stores an absolute path (see canonicalize()). In the latter case, no
TOCTOU guarantees are made.
§Examples
Opens a directory and then a file inside it.
#![feature(dirfd)]
use std::{fs::Dir, io};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let mut file = dir.open_file("bar.txt")?;
let contents = io::read_to_string(file)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Fields§
§inner: Dirdirfd)Implementations§
Source§impl Dir
impl Dir
Sourcepub fn open<P>(path: P) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open<P>(path: P) -> Result<Dir, Error>
dirfd)Attempts to open a directory at path in read-only mode.
This function opens a directory. To open a file instead, see File::open.
§Errors
This function will return an error if path does not point to an existing directory.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::Dir, io};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let mut f = dir.open_file("bar.txt")?;
let contents = io::read_to_string(f)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Sourcepub fn open_with<P>(path: P, opts: &OpenOptions) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_with<P>(path: P, opts: &OpenOptions) -> Result<Dir, Error>
dirfd)Attempts to open a directory at path according to opts.
This function opens a directory. To open a file instead, see File::open.
§Errors
This function will return an error if path does not point to an existing directory.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::{Dir, OpenOptions}, io};
fn main() -> std::io::Result<()> {
let dir = Dir::open_with("foo", &OpenOptions::new().read(true))?;
let mut f = dir.open_file("bar.txt")?;
let contents = io::read_to_string(f)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Sourcepub fn open_for_traversal<P>(path: P) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_for_traversal<P>(path: P) -> Result<Dir, Error>
dirfd)Attempts to open a directory at path with the minimum permissions for traversal.
The permissions requested by this function are guaranteed to be sufficient to open a child file or folder, but not necessarily to list all children.
§Errors
This function may return an error according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::Dir, io};
fn main() -> std::io::Result<()> {
let foo = Dir::open_for_traversal("foo")?;
let foobar = foo.open_dir("bar")?;
let mut foobarbaz = foobar.open_file("baz")?;
let contents = io::read_to_string(foobarbaz)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Sourcepub fn self_metadata(&self) -> Result<Metadata, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn self_metadata(&self) -> Result<Metadata, Error>
dirfd)Queries metadata about the underlying directory.
This is equivalent to dir.metadata(".") but can be more efficient.
§Examples
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let metadata = dir.self_metadata()?;
Ok(())
}Sourcepub fn open_file<P>(&self, path: P) -> Result<File, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_file<P>(&self, path: P) -> Result<File, Error>
dirfd)Attempts to open a file in read-only mode relative to this directory.
This function interprets path relative to the directory provided by self. To open a file
relative to the current working directory, or at an absolute path, see File::open.
§Errors
This function will return an error if path does not point to an existing file.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::Dir, io};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let mut f = dir.open_file("bar.txt")?;
let contents = io::read_to_string(f)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Sourcepub fn open_file_with<P>(
&self,
path: P,
opts: &OpenOptions,
) -> Result<File, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_file_with<P>( &self, path: P, opts: &OpenOptions, ) -> Result<File, Error>
dirfd)Attempts to open a file according to opts relative to this directory.
This function interprets path relative to the directory provided by self. To open a file
relative to the current working directory, or at an absolute path, see File::open.
§Errors
This function will return an error if path does not point to an existing file.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::{Dir, OpenOptions}, io::{self, Write}};
fn main() -> io::Result<()> {
let dir = Dir::open("foo")?;
let mut opts = OpenOptions::new();
opts.read(true).write(true);
let mut f = dir.open_file_with("bar.txt", &opts)?;
f.write_all(b"Hello, world!")?;
let contents = io::read_to_string(f)?;
assert_eq!(contents, "Hello, world!");
Ok(())
}Sourcepub fn remove_file<P>(&self, path: P) -> Result<(), Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn remove_file<P>(&self, path: P) -> Result<(), Error>
dirfd)Attempts to remove a file relative to this directory.
This function interprets path relative to the directory provided by self. To remove a file
relative to the current working directory, or at an absolute path, see fs::remove_file.
§Errors
This function will return an error if path does not point to an existing file.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
dir.remove_file("bar.txt")?;
Ok(())
}Sourcepub fn rename<P, Q>(&self, from: P, to_dir: &Dir, to: Q) -> Result<(), Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn rename<P, Q>(&self, from: P, to_dir: &Dir, to: Q) -> Result<(), Error>
dirfd)Attempts to rename a file or directory relative to this directory to a new name, replacing the destination file if present.
This function interprets from relative to the directory provided by self and to relative to the directory
provided by to_dir. To rename a file relative to the current working directory, or at an absolute path, see fs::rename.
§Errors
This function will return an error if from does not point to an existing file or directory.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
dir.rename("bar.txt", &dir, "quux.txt")?;
Ok(())
}Sourcepub fn create_dir<P>(&self, path: P) -> Result<(), Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn create_dir<P>(&self, path: P) -> Result<(), Error>
dirfd)Attempts to create a directory relative to this directory.
This function interprets path relative to the directory provided by self. To create a directory
relative to the current working directory, or at an absolute path, see
fs::create_dir.
Sourcepub fn open_dir<P>(&self, path: P) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_dir<P>(&self, path: P) -> Result<Dir, Error>
dirfd)Attempts to open a directory in read-only mode relative to this directory.
This function interprets path relative to the directory provided by self. To open a directory
relative to the current working directory, or at an absolute path, see Dir::open.
§Errors
This function will return an error if path does not point to an existing directory.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::Dir};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let foobar = dir.open_dir("bar")?;
Ok(())
}Sourcepub fn open_dir_with<P>(
&self,
path: P,
opts: &OpenOptions,
) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn open_dir_with<P>( &self, path: P, opts: &OpenOptions, ) -> Result<Dir, Error>
dirfd)Attempts to open a directory relative to this directory according to opts.
This function interprets path relative to the directory provided by self. To open a directory
relative to the current working directory, or at an absolute path, see Dir::open.
§Errors
This function will return errors according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::fs::{Dir, OpenOptions};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let foobar_w = dir.open_dir_with("bar", &OpenOptions::new().write(true))?;
Ok(())
}Sourcepub fn remove_dir<P>(&self, path: P) -> Result<(), Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn remove_dir<P>(&self, path: P) -> Result<(), Error>
dirfd)Attempts to remove a directory relative to this directory.
This function interprets path relative to the directory provided by self. To remove a directory
relative to the current working directory, or at an absolute path, see
fs::remove_dir.
§Errors
This function will return an error if path does not point to an existing directory.
Other errors may also be returned according to OpenOptions::open.
§Examples
#![feature(dirfd)]
use std::{fs::Dir};
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
dir.remove_dir("bar")?;
Ok(())
}Sourcepub fn try_clone(&self) -> Result<Dir, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn try_clone(&self) -> Result<Dir, Error>
dirfd)Creates a new Dir instance that shares the same underlying directory handle
as the existing Dir instance.
§Examples
Creates two handles for a directory named foo:
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let dir_copy = dir.try_clone()?;
Ok(())
}Sourcepub fn metadata<P>(&self, path: P) -> Result<Metadata, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn metadata<P>(&self, path: P) -> Result<Metadata, Error>
dirfd)Queries the file system to get information about a file, directory, etc. relative to this directory.
This function will traverse symbolic links to query information about the destination file.
To query metadata about the path itself without following symbolic links, use
symlink_metadata.
§Examples
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let metadata = dir.metadata("subdir/file.txt")?;
Ok(())
}Sourcepub fn symlink_metadata<P>(&self, path: P) -> Result<Metadata, Error>
🔬This is a nightly-only experimental API. (dirfd)
pub fn symlink_metadata<P>(&self, path: P) -> Result<Metadata, Error>
dirfd)Queries the file system to get information about a file, directory, etc. relative to this directory.
This function will return the Metadata of the exact path without traversing symbolic
links to a resolved destination file. Using this function on a path that is a file or
directory (not a symbolic link) will behave the same as metadata.
§Examples
#![feature(dirfd)]
use std::fs::Dir;
fn main() -> std::io::Result<()> {
let dir = Dir::open("foo")?;
let metadata = dir.symlink_metadata("subdir/file.txt")?;
Ok(())
}Trait Implementations§
Source§impl AsFd for Dir
impl AsFd for Dir
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Auto Trait Implementations§
impl Freeze for Dir
impl RefUnwindSafe for Dir
impl Send for Dir
impl Sync for Dir
impl Unpin for Dir
impl UnsafeUnpin for Dir
impl UnwindSafe for Dir
Blanket Implementations§
Source§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
Source§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> PossiblyOption<T> for T
impl<T> PossiblyOption<T> for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.