Skip to content

Commit 148fd24

Browse files
committed
add new Dir struct, rename/remove functions, constructors, and documentation
1 parent 4691b25 commit 148fd24

File tree

3 files changed

+250
-62
lines changed

3 files changed

+250
-62
lines changed

library/std/src/fs.rs

+178-4
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
/// }
@@ -1426,23 +1426,197 @@ impl Seek for Arc<File> {
14261426
}
14271427

14281428
impl Dir {
1429-
/// Opens a file relative to this directory.
1429+
/// Attempts to open a directory at `path` in read-only mode.
1430+
///
1431+
/// See [`new_with`] for more options.
1432+
///
1433+
/// # Errors
1434+
///
1435+
/// This function will return an error in these (and other) situations:
1436+
/// * The path doesn't exist
1437+
/// * The path doesn't specify a directory
1438+
/// * The process doesn't have permission to read the directory
1439+
///
1440+
/// # Examples
1441+
///
1442+
/// ```no_run
1443+
/// use std::fs::Dir;
1444+
///
1445+
/// fn main() -> std::io::Result<()> {
1446+
/// let dir = Dir::new("foo")?;
1447+
/// let mut f = dir.open("bar.txt")?;
1448+
/// let mut data = vec![];
1449+
/// f.read_to_end(&mut data)?;
1450+
/// Ok(())
1451+
/// }
1452+
/// ```
1453+
///
1454+
/// [`new_with`]: Dir::new_with
1455+
#[unstable(feature = "dirfd", issue = "120426")]
1456+
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
1457+
Ok(Self { inner: fs_imp::Dir::new(path)? })
1458+
}
1459+
1460+
/// Attempts to open a directory at `path` with the options specified by `opts`.
1461+
///
1462+
/// # Errors
1463+
///
1464+
/// This function will return an error in these (and other) situations:
1465+
/// * The path doesn't exist
1466+
/// * The path doesn't specify a directory
1467+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
14301468
///
14311469
/// # Examples
1470+
///
14321471
/// ```no_run
14331472
/// use std::fs::Dir;
14341473
///
1435-
/// let dir = Dir::new("foo")?;
1474+
/// fn main() -> std::io::Result<()> {
1475+
/// let dir = Dir::new_with("foo", OpenOptions::new().write(true))?;
1476+
/// let mut f = dir.remove_file("bar.txt")?;
1477+
/// Ok(())
1478+
/// }
1479+
/// ```
1480+
#[unstable(feature = "dirfd", issue = "120426")]
1481+
pub fn new_with<P: AsRef<Path>>(path: P, opts: &OpenOptions) -> io::Result<Self> {
1482+
Ok(Self { inner: fs_imp::Dir::new_with(path, &opts.0)? })
1483+
}
1484+
1485+
/// Attempts to open a file relative to this directory.
1486+
///
1487+
/// # Errors
1488+
///
1489+
/// This function will return an error in these (and other) situations:
1490+
/// * The path doesn't exist
1491+
/// * The path doesn't specify a regular file
1492+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1493+
///
1494+
/// # Examples
1495+
///
1496+
/// ```no_run
1497+
/// use std::fs::Dir;
1498+
///
1499+
/// fn main() -> std::io::Result<()> {
1500+
/// let dir = Dir::new("foo")?;
1501+
/// let mut f = dir.open("bar.txt")?;
1502+
/// let mut data = vec![];
1503+
/// f.read_to_end(&mut data)?;
1504+
/// Ok(())
1505+
/// }
14361506
/// ```
14371507
#[unstable(feature = "dirfd", issue = "120426")]
14381508
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
14391509
self.inner.open(path).map(|f| File { inner: f })
14401510
}
1441-
/// Opens a file relative to this directory with the specified options.
1511+
1512+
/// Attempts to open a file relative to this directory with the options specified by `opts`.
1513+
///
1514+
/// # Errors
1515+
///
1516+
/// This function will return an error in these (and other) situations:
1517+
/// * The path doesn't exist
1518+
/// * The path doesn't specify a regular file
1519+
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1520+
///
1521+
/// # Examples
1522+
///
1523+
/// ```no_run
1524+
/// use std::fs::Dir;
1525+
///
1526+
/// fn main() -> std::io::Result<()> {
1527+
/// let dir = Dir::new("foo")?;
1528+
/// let mut f = dir.open_with("bar.txt", OpenOptions::new().read(true))?;
1529+
/// let mut data = vec![];
1530+
/// f.read_to_end(&mut data)?;
1531+
/// Ok(())
1532+
/// }
1533+
/// ```
14421534
#[unstable(feature = "dirfd", issue = "120426")]
14431535
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
14441536
self.inner.open_with(path, &opts.0).map(|f| File { inner: f })
14451537
}
1538+
1539+
/// Attempts to remove a file relative to this directory.
1540+
///
1541+
/// # Errors
1542+
///
1543+
/// This function will return an error in these (and other) situations:
1544+
/// * The path doesn't exist
1545+
/// * The path doesn't specify a regular file
1546+
/// * The process doesn't have permission to delete the file.
1547+
///
1548+
/// # Examples
1549+
///
1550+
/// ```no_run
1551+
/// use std::fs::Dir;
1552+
///
1553+
/// fn main() -> std::io::Result<()> {
1554+
/// let dir = Dir::new("foo")?;
1555+
/// dir.remove_file("bar.txt")?;
1556+
/// Ok(())
1557+
/// }
1558+
/// ```
1559+
#[unstable(feature = "dirfd", issue = "120426")]
1560+
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1561+
self.inner.remove_file(path)
1562+
}
1563+
1564+
/// Attempts to remove a directory relative to this directory.
1565+
///
1566+
/// # Errors
1567+
///
1568+
/// This function will return an error in these (and other) situations:
1569+
/// * The path doesn't exist
1570+
/// * The path doesn't specify a directory
1571+
/// * The directory isn't empty
1572+
/// * The process doesn't have permission to delete the directory.
1573+
///
1574+
/// # Examples
1575+
///
1576+
/// ```no_run
1577+
/// use std::fs::Dir;
1578+
///
1579+
/// fn main() -> std::io::Result<()> {
1580+
/// let dir = Dir::new("foo")?;
1581+
/// dir.remove_dir("baz")?;
1582+
/// Ok(())
1583+
/// }
1584+
/// ```
1585+
#[unstable(feature = "dirfd", issue = "120426")]
1586+
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1587+
self.inner.remove_dir(path)
1588+
}
1589+
1590+
/// Attempts to rename a file or directory relative to this directory to a new name, replacing
1591+
/// the destination file if present.
1592+
///
1593+
/// # Errors
1594+
///
1595+
/// This function will return an error in these (and other) situations:
1596+
/// * The `from` path doesn't exist
1597+
/// * The `from` path doesn't specify a directory
1598+
/// * `self` and `to_dir` are on different mount points
1599+
///
1600+
/// # Examples
1601+
///
1602+
/// ```no_run
1603+
/// use std::fs::Dir;
1604+
///
1605+
/// fn main() -> std::io::Result<()> {
1606+
/// let dir = Dir::new("foo")?;
1607+
/// dir.rename("bar.txt", &dir, "quux.txt")?;
1608+
/// Ok(())
1609+
/// }
1610+
/// ```
1611+
#[unstable(feature = "dirfd", issue = "120426")]
1612+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
1613+
&self,
1614+
from: P,
1615+
to_dir: &Self,
1616+
to: Q,
1617+
) -> io::Result<()> {
1618+
self.inner.rename(from, &to_dir.inner, to)
1619+
}
14461620
}
14471621

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

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

+1-3
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)