Skip to content

Commit bdeb57e

Browse files
committed
add new Dir struct, rename/remove functions, constructors, and documentation
1 parent 05f87c5 commit bdeb57e

File tree

3 files changed

+250
-62
lines changed

3 files changed

+250
-62
lines changed

library/std/src/fs.rs

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub enum TryLockError {
146146
/// use std::fs::Dir;
147147
///
148148
/// fn main() -> std::io::Result<()> {
149-
/// let dir = Dir::new("/home/foo")?;
149+
/// let dir = Dir::new("foo")?;
150150
/// let file = dir.open("bar.txt")?;
151151
/// Ok(())
152152
/// }
@@ -1477,23 +1477,197 @@ impl Seek for Arc<File> {
14771477
}
14781478

14791479
impl Dir {
1480-
/// Opens a file relative to this directory.
1480+
/// Attempts to open a directory at `path` in read-only mode.
1481+
///
1482+
/// See [`new_with`] for more options.
1483+
///
1484+
/// # Errors
1485+
///
1486+
/// This function will return an error in these (and other) situations:
1487+
/// * The path doesn't exist
1488+
/// * The path doesn't specify a directory
1489+
/// * The process doesn't have permission to read the directory
1490+
///
1491+
/// # Examples
1492+
///
1493+
/// ```no_run
1494+
/// use std::fs::Dir;
1495+
///
1496+
/// fn main() -> std::io::Result<()> {
1497+
/// let dir = Dir::new("foo")?;
1498+
/// let mut f = dir.open("bar.txt")?;
1499+
/// let mut data = vec![];
1500+
/// f.read_to_end(&mut data)?;
1501+
/// Ok(())
1502+
/// }
1503+
/// ```
1504+
///
1505+
/// [`new_with`]: Dir::new_with
1506+
#[unstable(feature = "dirfd", issue = "120426")]
1507+
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
1508+
Ok(Self { inner: fs_imp::Dir::new(path)? })
1509+
}
1510+
1511+
/// Attempts to open a directory at `path` with the options specified by `opts`.
1512+
///
1513+
/// # Errors
1514+
///
1515+
/// This function will return an error in these (and other) situations:
1516+
/// * The path doesn't exist
1517+
/// * The path doesn't specify a directory
1518+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
14811519
///
14821520
/// # Examples
1521+
///
14831522
/// ```no_run
14841523
/// use std::fs::Dir;
14851524
///
1486-
/// let dir = Dir::new("foo")?;
1525+
/// fn main() -> std::io::Result<()> {
1526+
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
1527+
/// let mut f = dir.remove_file("bar.txt")?;
1528+
/// Ok(())
1529+
/// }
1530+
/// ```
1531+
#[unstable(feature = "dirfd", issue = "120426")]
1532+
pub fn new_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<Self> {
1533+
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
1534+
}
1535+
1536+
/// Attempts to open a file relative to this directory.
1537+
///
1538+
/// # Errors
1539+
///
1540+
/// This function will return an error in these (and other) situations:
1541+
/// * The path doesn't exist
1542+
/// * The path doesn't specify a regular file
1543+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1544+
///
1545+
/// # Examples
1546+
///
1547+
/// ```no_run
1548+
/// use std::fs::Dir;
1549+
///
1550+
/// fn main() -> std::io::Result<()> {
1551+
/// let dir = Dir::new("foo")?;
1552+
/// let mut f = dir.open("bar.txt")?;
1553+
/// let mut data = vec![];
1554+
/// f.read_to_end(&mut data)?;
1555+
/// Ok(())
1556+
/// }
14871557
/// ```
14881558
#[unstable(feature = "dirfd", issue = "120426")]
14891559
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14901560
self.inner.open(path).map(|f| File { inner: f })
14911561
}
1492-
/// Opens a file relative to this directory with the specified options.
1562+
1563+
/// Attempts to open a file relative to this directory with the options specified by `opts`.
1564+
///
1565+
/// # Errors
1566+
///
1567+
/// This function will return an error in these (and other) situations:
1568+
/// * The path doesn't exist
1569+
/// * The path doesn't specify a regular file
1570+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1571+
///
1572+
/// # Examples
1573+
///
1574+
/// ```no_run
1575+
/// use std::fs::Dir;
1576+
///
1577+
/// fn main() -> std::io::Result<()> {
1578+
/// let dir = Dir::new("foo")?;
1579+
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1580+
/// let mut data = vec![];
1581+
/// f.read_to_end(&mut data)?;
1582+
/// Ok(())
1583+
/// }
1584+
/// ```
14931585
#[unstable(feature = "dirfd", issue = "120426")]
14941586
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14951587
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14961588
}
1589+
1590+
/// Attempts to remove a file relative to this directory.
1591+
///
1592+
/// # Errors
1593+
///
1594+
/// This function will return an error in these (and other) situations:
1595+
/// * The path doesn't exist
1596+
/// * The path doesn't specify a regular file
1597+
/// * The process doesn't have permission to delete the file.
1598+
///
1599+
/// # Examples
1600+
///
1601+
/// ```no_run
1602+
/// use std::fs::Dir;
1603+
///
1604+
/// fn main() -> std::io::Result<()> {
1605+
/// let dir = Dir::new("foo")?;
1606+
/// dir.remove_file("bar.txt")?;
1607+
/// Ok(())
1608+
/// }
1609+
/// ```
1610+
#[unstable(feature = "dirfd", issue = "120426")]
1611+
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1612+
self.inner.remove_file(path)
1613+
}
1614+
1615+
/// Attempts to remove a directory relative to this directory.
1616+
///
1617+
/// # Errors
1618+
///
1619+
/// This function will return an error in these (and other) situations:
1620+
/// * The path doesn't exist
1621+
/// * The path doesn't specify a directory
1622+
/// * The directory isn't empty
1623+
/// * The process doesn't have permission to delete the directory.
1624+
///
1625+
/// # Examples
1626+
///
1627+
/// ```no_run
1628+
/// use std::fs::Dir;
1629+
///
1630+
/// fn main() -> std::io::Result<()> {
1631+
/// let dir = Dir::new("foo")?;
1632+
/// dir.remove_dir("baz")?;
1633+
/// Ok(())
1634+
/// }
1635+
/// ```
1636+
#[unstable(feature = "dirfd", issue = "120426")]
1637+
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1638+
self.inner.remove_dir(path)
1639+
}
1640+
1641+
/// Attempts to rename a file or directory relative to this directory to a new name, replacing
1642+
/// the destination file if present.
1643+
///
1644+
/// # Errors
1645+
///
1646+
/// This function will return an error in these (and other) situations:
1647+
/// * The `from` path doesn't exist
1648+
/// * The `from` path doesn't specify a directory
1649+
/// * `self` and `to_dir` are on different mount points
1650+
///
1651+
/// # Examples
1652+
///
1653+
/// ```no_run
1654+
/// use std::fs::Dir;
1655+
///
1656+
/// fn main() -> std::io::Result<()> {
1657+
/// let dir = Dir::new("foo")?;
1658+
/// dir.rename("bar.txt", &dir, "quux.txt")?;
1659+
/// Ok(())
1660+
/// }
1661+
/// ```
1662+
#[unstable(feature = "dirfd", issue = "120426")]
1663+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
1664+
&self,
1665+
from: P,
1666+
to_dir: &Self,
1667+
to: Q,
1668+
) -> io::Result<()> {
1669+
self.inner.rename(from, &to_dir.inner, to)
1670+
}
14971671
}
14981672

14991673
#[unstable(feature = "dirfd", issue = "120426")]

library/std/src/sys/fs/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> i
4646
f(path)
4747
}
4848

49-
#[cfg(target_family = "unix")]
50-
pub use imp::Dir;
5149
pub use imp::{
52-
DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
50+
Dir, DirBuilder, DirEntry, File, FileAttr, FilePermissions, FileTimes, FileType, OpenOptions,
5351
ReadDir,
5452
};
5553

0 commit comments

Comments
 (0)