@@ -1578,6 +1578,63 @@ impl Dir {
15781578 . map ( |inner| Self { inner } )
15791579 }
15801580
1581+ /// Attempts to open a directory at `path` according to `opts`.
1582+ ///
1583+ /// This function opens a directory. To open a file instead, see [`File::open`].
1584+ ///
1585+ /// # Errors
1586+ ///
1587+ /// This function will return an error if `path` does not point to an existing directory.
1588+ /// Other errors may also be returned according to [`OpenOptions::open`].
1589+ ///
1590+ /// # Examples
1591+ ///
1592+ /// ```no_run
1593+ /// #![feature(dirfd)]
1594+ /// use std::{fs::{Dir, OpenOptions}, io};
1595+ ///
1596+ /// fn main() -> std::io::Result<()> {
1597+ /// let dir = Dir::open_with("foo", &OpenOptions::new().read(true))?;
1598+ /// let mut f = dir.open_file("bar.txt")?;
1599+ /// let contents = io::read_to_string(f)?;
1600+ /// assert_eq!(contents, "Hello, world!");
1601+ /// Ok(())
1602+ /// }
1603+ /// ```
1604+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1605+ pub fn open_with < P : AsRef < Path > > ( path : P , opts : & OpenOptions ) -> io:: Result < Self > {
1606+ fs_imp:: Dir :: open ( path. as_ref ( ) , & opts. 0 ) . map ( |inner| Self { inner } )
1607+ }
1608+
1609+ /// Attempts to open a directory at `path` with the minimum permissions for traversal.
1610+ ///
1611+ /// The permissions requested by this function are guaranteed to be sufficient to open a child
1612+ /// file or folder, but not necessarily to list all children.
1613+ ///
1614+ /// # Errors
1615+ ///
1616+ /// This function may return an error according to [`OpenOptions::open`].
1617+ ///
1618+ /// # Examples
1619+ ///
1620+ /// ```no_run
1621+ /// #![feature(dirfd)]
1622+ /// use std::{fs::Dir, io};
1623+ ///
1624+ /// fn main() -> std::io::Result<()> {
1625+ /// let foo = Dir::open_for_traversal("foo")?;
1626+ /// let foobar = foo.open_dir("bar")?;
1627+ /// let mut foobarbaz = foobar.open_file("baz")?;
1628+ /// let contents = io::read_to_string(foobarbaz)?;
1629+ /// assert_eq!(contents, "Hello, world!");
1630+ /// Ok(())
1631+ /// }
1632+ /// ```
1633+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1634+ pub fn open_for_traversal < P : AsRef < Path > > ( path : P ) -> io:: Result < Self > {
1635+ fs_imp:: Dir :: open_for_traversal ( path. as_ref ( ) ) . map ( |inner| Self { inner } )
1636+ }
1637+
15811638 /// Queries metadata about the underlying directory.
15821639 ///
15831640 /// # Examples
@@ -1719,6 +1776,99 @@ impl Dir {
17191776 ) -> io:: Result < ( ) > {
17201777 self . inner . rename ( from. as_ref ( ) , & to_dir. inner , to. as_ref ( ) )
17211778 }
1779+
1780+ /// Attempts to create a directory relative to this directory.
1781+ ///
1782+ /// This function interprets `path` relative to the directory provided by `self`. To create a directory
1783+ /// relative to the current working directory, or at an absolute path, see
1784+ /// [`fs::create_dir`][crate::fs::create_dir].
1785+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1786+ pub fn create_dir < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1787+ self . inner . create_dir ( path. as_ref ( ) )
1788+ }
1789+
1790+ /// Attempts to open a directory in read-only mode relative to this directory.
1791+ ///
1792+ /// This function interprets `path` relative to the directory provided by `self`. To open a directory
1793+ /// relative to the current working directory, or at an absolute path, see [`Dir::open`].
1794+ ///
1795+ /// # Errors
1796+ ///
1797+ /// This function will return an error if `path` does not point to an existing directory.
1798+ /// Other errors may also be returned according to [`OpenOptions::open`].
1799+ ///
1800+ /// # Examples
1801+ ///
1802+ /// ```no_run
1803+ /// #![feature(dirfd)]
1804+ /// use std::{fs::Dir};
1805+ ///
1806+ /// fn main() -> std::io::Result<()> {
1807+ /// let dir = Dir::open("foo")?;
1808+ /// let foobar = dir.open_dir("bar")?;
1809+ /// Ok(())
1810+ /// }
1811+ /// ```
1812+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1813+ pub fn open_dir < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < Self > {
1814+ self . inner
1815+ . open_dir ( path. as_ref ( ) , & OpenOptions :: new ( ) . read ( true ) . 0 )
1816+ . map ( |inner| Self { inner } )
1817+ }
1818+
1819+ /// Attempts to open a directory relative to this directory according to `opts`.
1820+ ///
1821+ /// This function interprets `path` relative to the directory provided by `self`. To open a directory
1822+ /// relative to the current working directory, or at an absolute path, see [`Dir::open`].
1823+ ///
1824+ /// # Errors
1825+ ///
1826+ /// This function will return errors according to [`OpenOptions::open`].
1827+ ///
1828+ /// # Examples
1829+ ///
1830+ /// ```no_run
1831+ /// #![feature(dirfd)]
1832+ /// use std::fs::{Dir, OpenOptions};
1833+ ///
1834+ /// fn main() -> std::io::Result<()> {
1835+ /// let dir = Dir::open("foo")?;
1836+ /// let foobar_w = dir.open_dir_with("bar", &OpenOptions::new().write(true))?;
1837+ /// Ok(())
1838+ /// }
1839+ /// ```
1840+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1841+ pub fn open_dir_with < P : AsRef < Path > > ( & self , path : P , opts : & OpenOptions ) -> io:: Result < Self > {
1842+ self . inner . open_dir ( path. as_ref ( ) , & opts. 0 ) . map ( |inner| Self { inner } )
1843+ }
1844+
1845+ /// Attempts to remove a directory relative to this directory.
1846+ ///
1847+ /// This function interprets `path` relative to the directory provided by `self`. To remove a directory
1848+ /// relative to the current working directory, or at an absolute path, see
1849+ /// [`fs::remove_dir`][crate::fs::remove_dir].
1850+ ///
1851+ /// # Errors
1852+ ///
1853+ /// This function will return an error if `path` does not point to an existing directory.
1854+ /// Other errors may also be returned according to [`OpenOptions::open`].
1855+ ///
1856+ /// # Examples
1857+ ///
1858+ /// ```no_run
1859+ /// #![feature(dirfd)]
1860+ /// use std::{fs::Dir};
1861+ ///
1862+ /// fn main() -> std::io::Result<()> {
1863+ /// let dir = Dir::open("foo")?;
1864+ /// dir.remove_dir("bar")?;
1865+ /// Ok(())
1866+ /// }
1867+ /// ```
1868+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
1869+ pub fn remove_dir < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
1870+ self . inner . remove_dir ( path. as_ref ( ) )
1871+ }
17221872}
17231873
17241874impl AsInner < fs_imp:: Dir > for Dir {
0 commit comments