Skip to content

Commit 7ac742d

Browse files
committed
add new Dir struct, rename/remove functions, constructors, and documentation
1 parent 014790d commit 7ac742d

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
@@ -167,7 +167,7 @@ pub enum TryLockError {
167167
/// use std::fs::Dir;
168168
///
169169
/// fn main() -> std::io::Result<()> {
170-
/// let dir = Dir::new("/home/foo")?;
170+
/// let dir = Dir::new("foo")?;
171171
/// let file = dir.open("bar.txt")?;
172172
/// Ok(())
173173
/// }
@@ -1493,23 +1493,197 @@ impl Seek for Arc<File> {
14931493
}
14941494

14951495
impl Dir {
1496-
/// Opens a file relative to this directory.
1496+
/// Attempts to open a directory at `path` in read-only mode.
1497+
///
1498+
/// See [`new_with`] for more options.
1499+
///
1500+
/// # Errors
1501+
///
1502+
/// This function will return an error in these (and other) situations:
1503+
/// * The path doesn't exist
1504+
/// * The path doesn't specify a directory
1505+
/// * The process doesn't have permission to read the directory
1506+
///
1507+
/// # Examples
1508+
///
1509+
/// ```no_run
1510+
/// use std::fs::Dir;
1511+
///
1512+
/// fn main() -> std::io::Result<()> {
1513+
/// let dir = Dir::new("foo")?;
1514+
/// let mut f = dir.open("bar.txt")?;
1515+
/// let mut data = vec![];
1516+
/// f.read_to_end(&mut data)?;
1517+
/// Ok(())
1518+
/// }
1519+
/// ```
1520+
///
1521+
/// [`new_with`]: Dir::new_with
1522+
#[unstable(feature = "dirfd", issue = "120426")]
1523+
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
1524+
Ok(Self { inner: fs_imp::Dir::new(path)? })
1525+
}
1526+
1527+
/// Attempts to open a directory at `path` with the options specified by `opts`.
1528+
///
1529+
/// # Errors
1530+
///
1531+
/// This function will return an error in these (and other) situations:
1532+
/// * The path doesn't exist
1533+
/// * The path doesn't specify a directory
1534+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
14971535
///
14981536
/// # Examples
1537+
///
14991538
/// ```no_run
15001539
/// use std::fs::Dir;
15011540
///
1502-
/// let dir = Dir::new("foo")?;
1541+
/// fn main() -> std::io::Result<()> {
1542+
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
1543+
/// let mut f = dir.remove_file("bar.txt")?;
1544+
/// Ok(())
1545+
/// }
1546+
/// ```
1547+
#[unstable(feature = "dirfd", issue = "120426")]
1548+
pub fn new_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<Self> {
1549+
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
1550+
}
1551+
1552+
/// Attempts to open a file relative to this directory.
1553+
///
1554+
/// # Errors
1555+
///
1556+
/// This function will return an error in these (and other) situations:
1557+
/// * The path doesn't exist
1558+
/// * The path doesn't specify a regular file
1559+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1560+
///
1561+
/// # Examples
1562+
///
1563+
/// ```no_run
1564+
/// use std::fs::Dir;
1565+
///
1566+
/// fn main() -> std::io::Result<()> {
1567+
/// let dir = Dir::new("foo")?;
1568+
/// let mut f = dir.open("bar.txt")?;
1569+
/// let mut data = vec![];
1570+
/// f.read_to_end(&mut data)?;
1571+
/// Ok(())
1572+
/// }
15031573
/// ```
15041574
#[unstable(feature = "dirfd", issue = "120426")]
15051575
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
15061576
self.inner.open(path).map(|f| File { inner: f })
15071577
}
1508-
/// Opens a file relative to this directory with the specified options.
1578+
1579+
/// Attempts to open a file relative to this directory with the options specified by `opts`.
1580+
///
1581+
/// # Errors
1582+
///
1583+
/// This function will return an error in these (and other) situations:
1584+
/// * The path doesn't exist
1585+
/// * The path doesn't specify a regular file
1586+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1587+
///
1588+
/// # Examples
1589+
///
1590+
/// ```no_run
1591+
/// use std::fs::Dir;
1592+
///
1593+
/// fn main() -> std::io::Result<()> {
1594+
/// let dir = Dir::new("foo")?;
1595+
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1596+
/// let mut data = vec![];
1597+
/// f.read_to_end(&mut data)?;
1598+
/// Ok(())
1599+
/// }
1600+
/// ```
15091601
#[unstable(feature = "dirfd", issue = "120426")]
15101602
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
15111603
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
15121604
}
1605+
1606+
/// Attempts to remove a file relative to this directory.
1607+
///
1608+
/// # Errors
1609+
///
1610+
/// This function will return an error in these (and other) situations:
1611+
/// * The path doesn't exist
1612+
/// * The path doesn't specify a regular file
1613+
/// * The process doesn't have permission to delete the file.
1614+
///
1615+
/// # Examples
1616+
///
1617+
/// ```no_run
1618+
/// use std::fs::Dir;
1619+
///
1620+
/// fn main() -> std::io::Result<()> {
1621+
/// let dir = Dir::new("foo")?;
1622+
/// dir.remove_file("bar.txt")?;
1623+
/// Ok(())
1624+
/// }
1625+
/// ```
1626+
#[unstable(feature = "dirfd", issue = "120426")]
1627+
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1628+
self.inner.remove_file(path)
1629+
}
1630+
1631+
/// Attempts to remove a directory relative to this directory.
1632+
///
1633+
/// # Errors
1634+
///
1635+
/// This function will return an error in these (and other) situations:
1636+
/// * The path doesn't exist
1637+
/// * The path doesn't specify a directory
1638+
/// * The directory isn't empty
1639+
/// * The process doesn't have permission to delete the directory.
1640+
///
1641+
/// # Examples
1642+
///
1643+
/// ```no_run
1644+
/// use std::fs::Dir;
1645+
///
1646+
/// fn main() -> std::io::Result<()> {
1647+
/// let dir = Dir::new("foo")?;
1648+
/// dir.remove_dir("baz")?;
1649+
/// Ok(())
1650+
/// }
1651+
/// ```
1652+
#[unstable(feature = "dirfd", issue = "120426")]
1653+
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1654+
self.inner.remove_dir(path)
1655+
}
1656+
1657+
/// Attempts to rename a file or directory relative to this directory to a new name, replacing
1658+
/// the destination file if present.
1659+
///
1660+
/// # Errors
1661+
///
1662+
/// This function will return an error in these (and other) situations:
1663+
/// * The `from` path doesn't exist
1664+
/// * The `from` path doesn't specify a directory
1665+
/// * `self` and `to_dir` are on different mount points
1666+
///
1667+
/// # Examples
1668+
///
1669+
/// ```no_run
1670+
/// use std::fs::Dir;
1671+
///
1672+
/// fn main() -> std::io::Result<()> {
1673+
/// let dir = Dir::new("foo")?;
1674+
/// dir.rename("bar.txt", &dir, "quux.txt")?;
1675+
/// Ok(())
1676+
/// }
1677+
/// ```
1678+
#[unstable(feature = "dirfd", issue = "120426")]
1679+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
1680+
&self,
1681+
from: P,
1682+
to_dir: &Self,
1683+
to: Q,
1684+
) -> io::Result<()> {
1685+
self.inner.rename(from, &to_dir.inner, to)
1686+
}
15131687
}
15141688

15151689
#[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)