Skip to content

dirfd: preliminary unix and windows implementations #139514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
321 changes: 321 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ pub enum TryLockError {
WouldBlock,
}

/// An object providing access to a directory on the filesystem.
///
/// Files are automatically closed when they go out of scope. Errors detected
/// on closing are ignored by the implementation of `Drop`.
///
/// # Examples
///
/// Opens a directory and then a file inside it.
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::Dir, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// let mut file = dir.open("bar.txt")?;
/// let mut s = String::new();
/// file.read_to_string(&mut s)?;
/// println!("{}", s);
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub struct Dir {
inner: fs_imp::Dir,
}

/// Metadata information about a file.
///
/// This structure is returned from the [`metadata`] or
Expand Down Expand Up @@ -1453,6 +1480,300 @@ impl Seek for Arc<File> {
}
}

impl Dir {
/// Attempts to open a directory at `path` in read-only mode.
///
/// See [`new_with`] for more options.
///
/// # 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
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::Dir, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// let mut f = dir.open("bar.txt")?;
/// let mut data = vec![];
/// f.read_to_end(&mut data)?;
/// Ok(())
/// }
/// ```
///
/// [`new_with`]: Dir::new_with
#[unstable(feature = "dirfd", issue = "120426")]
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
fs_imp::Dir::new(path).map(|inner| Self { inner })
}

/// Attempts to open a directory at `path` with the options specified by `opts`.
///
/// # Errors
///
/// This function may return an error according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::fs::{Dir, OpenOptions};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
/// let mut f = dir.remove_file("bar.txt")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn new_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<Self> {
fs_imp::Dir::new_with(path, &opts.0).map(|inner| Self { inner })
}

/// Attempts to open a directory at `path` with the minimum permissions for traversal.
///
/// # Errors
///
/// This function may return an error according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::Dir, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let foo = Dir::new_for_traversal("foo")?;
/// let foobar = foo.open_dir("bar")?;
/// let mut s = String::new();
/// foobar.open("baz")?.read_to_string(&mut s)?;
/// println!("{s}");
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn new_for_traversal<P: AsRef<Path>>(path: P) -> io::Result<Self> {
fs_imp::Dir::new_for_traversal(path).map(|inner| Self { inner })
}

/// Attempts to open a file in read-only mode relative to this directory.
///
/// # 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
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::Dir, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// let mut f = dir.open("bar.txt")?;
/// let mut data = vec![];
/// f.read_to_end(&mut data)?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
self.inner.open(path).map(|f| File { inner: f })
}

/// Attempts to open a file relative to this directory with the options specified by `opts`.
///
/// # Errors
///
/// This function may return an error according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::{Dir, OpenOptions}, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
/// let mut data = vec![];
/// f.read_to_end(&mut data)?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
}

/// Attempts to create a directory relative to this directory.
///
/// # Errors
///
/// This function will return an error if `path` points to an existing file or directory.
/// Other errors may also be returned according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::{Dir, OpenOptions}, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
/// let mut data = vec![];
/// f.read_to_end(&mut data)?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.inner.create_dir(path)
}

/// Attempts to open a directory relative to this directory in read-only mode.
///
/// # 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
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::Dir, io::Read};
///
/// fn main() -> std::io::Result<()> {
/// let foo = Dir::new("foo")?;
/// let foobar = foo.open_dir("bar")?;
/// let mut s = String::new();
/// foobar.open("baz")?.read_to_string(&mut s)?;
/// println!("{s}");
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<Self> {
self.inner.open_dir(path).map(|inner| Self { inner })
}

/// Attempts to open a directory relative to this directory in read-only mode.
///
/// # Errors
///
/// This function will return an error according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::{fs::{Dir, OpenOptions}, io::Write};
///
/// fn main() -> std::io::Result<()> {
/// let foo = Dir::new("foo")?;
/// let foobar = foo.open_dir_with("bar", OpenOptions::new().write(true))?;
/// foobar.open("baz")?.write(b"baz")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn open_dir_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<Self> {
self.inner.open_dir_with(path, &opts.0).map(|inner| Self { inner })
}

/// Attempts to remove a file relative to this directory.
///
/// # 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
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::fs::Dir;
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// dir.remove_file("bar.txt")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.inner.remove_file(path)
}

/// Attempts to remove a directory relative to this directory.
///
/// # Errors
///
/// This function will return an error if `path` does not point to an existing, non-empty directory.
/// Other errors may also be returned according to [`OpenOptions::open`].
///
/// # Examples
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::fs::Dir;
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// dir.remove_dir("baz")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
self.inner.remove_dir(path)
}

/// Attempts to rename a file or directory relative to this directory to a new name, replacing
/// the destination file if present.
///
/// # 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
///
/// ```no_run
/// #![feature(dirfd)]
/// use std::fs::Dir;
///
/// fn main() -> std::io::Result<()> {
/// let dir = Dir::new("foo")?;
/// dir.rename("bar.txt", &dir, "quux.txt")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
&self,
from: P,
to_dir: &Self,
to: Q,
) -> io::Result<()> {
self.inner.rename(from, &to_dir.inner, to)
}
}

#[unstable(feature = "dirfd", issue = "120426")]
impl fmt::Debug for Dir {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}

impl OpenOptions {
/// Creates a blank new set of options ready for configuration.
///
Expand Down
Loading
Loading