Skip to content

Commit e0e30ae

Browse files
committed
requested changes
1 parent d7c07cc commit e0e30ae

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

library/std/src/fs.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ pub enum TryLockError {
153153
WouldBlock,
154154
}
155155

156-
#[unstable(feature = "dirfd", issue = "120426")]
157156
/// An object providing access to a directory on the filesystem.
158157
///
159158
/// Files are automatically closed when they go out of scope. Errors detected
@@ -165,14 +164,18 @@ pub enum TryLockError {
165164
///
166165
/// ```no_run
167166
/// #![feature(dirfd)]
168-
/// use std::fs::Dir;
167+
/// use std::{fs::Dir, io::Read};
169168
///
170169
/// fn main() -> std::io::Result<()> {
171170
/// let dir = Dir::new("foo")?;
172-
/// let file = dir.open("bar.txt")?;
171+
/// let mut file = dir.open("bar.txt")?;
172+
/// let mut s = String::new();
173+
/// file.read_to_string(&mut s)?;
174+
/// println!("{}", s);
173175
/// Ok(())
174176
/// }
175177
/// ```
178+
#[unstable(feature = "dirfd", issue = "120426")]
176179
pub struct Dir {
177180
inner: fs_imp::Dir,
178181
}
@@ -1505,10 +1508,8 @@ impl Dir {
15051508
///
15061509
/// # Errors
15071510
///
1508-
/// This function will return an error in these (and other) situations:
1509-
/// * The path doesn't exist
1510-
/// * The path doesn't specify a directory
1511-
/// * The process doesn't have permission to read the directory
1511+
/// This function will return an error if `path` does not point to an existing directory.
1512+
/// Other errors may also be returned according to [`OpenOptions::open`].
15121513
///
15131514
/// # Examples
15141515
///
@@ -1535,10 +1536,7 @@ impl Dir {
15351536
///
15361537
/// # Errors
15371538
///
1538-
/// This function will return an error in these (and other) situations:
1539-
/// * The path doesn't exist
1540-
/// * The path doesn't specify a directory
1541-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1539+
/// This function may return an error according to [`OpenOptions::open`].
15421540
///
15431541
/// # Examples
15441542
///
@@ -1561,10 +1559,8 @@ impl Dir {
15611559
///
15621560
/// # Errors
15631561
///
1564-
/// This function will return an error in these (and other) situations:
1565-
/// * The path doesn't exist
1566-
/// * The path doesn't specify a regular file
1567-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1562+
/// This function will return an error if `path` does not point to an existing file.
1563+
/// Other errors may also be returned according to [`OpenOptions::open`].
15681564
///
15691565
/// # Examples
15701566
///
@@ -1589,11 +1585,7 @@ impl Dir {
15891585
///
15901586
/// # Errors
15911587
///
1592-
/// This function may return an error in these (and other) situations, depending on the
1593-
/// specified `opts`:
1594-
/// * The path doesn't exist
1595-
/// * The path doesn't specify a regular file
1596-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1588+
/// This function may return an error according to [`OpenOptions::open`].
15971589
///
15981590
/// # Examples
15991591
///
@@ -1618,9 +1610,8 @@ impl Dir {
16181610
///
16191611
/// # Errors
16201612
///
1621-
/// This function will return an error in these (and other) situations:
1622-
/// * The path exists
1623-
/// * The process doesn't have permission to create the directory
1613+
/// This function will return an error if `path` points to an existing file or directory.
1614+
/// Other errors may also be returned according to [`OpenOptions::open`].
16241615
///
16251616
/// # Examples
16261617
///
@@ -1645,10 +1636,8 @@ impl Dir {
16451636
///
16461637
/// # Errors
16471638
///
1648-
/// This function will return an error in these (and other) situations:
1649-
/// * The path doesn't exist
1650-
/// * The path doesn't specify a regular file
1651-
/// * The process doesn't have permission to delete the file.
1639+
/// This function will return an error if `path` does not point to an existing file.
1640+
/// Other errors may also be returned according to [`OpenOptions::open`].
16521641
///
16531642
/// # Examples
16541643
///
@@ -1671,11 +1660,8 @@ impl Dir {
16711660
///
16721661
/// # Errors
16731662
///
1674-
/// This function will return an error in these (and other) situations:
1675-
/// * The path doesn't exist
1676-
/// * The path doesn't specify a directory
1677-
/// * The directory isn't empty
1678-
/// * The process doesn't have permission to delete the directory.
1663+
/// This function will return an error if `path` does not point to an existing, non-empty directory.
1664+
/// Other errors may also be returned according to [`OpenOptions::open`].
16791665
///
16801666
/// # Examples
16811667
///
@@ -1699,10 +1685,8 @@ impl Dir {
16991685
///
17001686
/// # Errors
17011687
///
1702-
/// This function will return an error in these (and other) situations:
1703-
/// * The `from` path doesn't exist
1704-
/// * The `from` path doesn't specify a directory
1705-
/// * `self` and `to_dir` are on different mount points
1688+
/// This function will return an error if `from` does not point to an existing file or directory.
1689+
/// Other errors may also be returned according to [`OpenOptions::open`].
17061690
///
17071691
/// # Examples
17081692
///

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,19 +1071,21 @@ impl Dir {
10711071
let handle = run_path_with_utf16(from, &|u| self.open_native(u, &opts))?;
10721072
// Calculate the layout of the `FILE_RENAME_INFO` we pass to `SetFileInformation`
10731073
// This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
1074-
let Ok(new_len_without_nul_in_bytes): Result<u32, _> =
1075-
((to.count_bytes() - 1) * 2).try_into()
1076-
else {
1077-
return Err(io::Error::new(io::ErrorKind::InvalidFilename, "Filename too long"));
1078-
};
1079-
let offset: u32 = offset_of!(c::FILE_RENAME_INFO, FileName).try_into().unwrap();
1080-
let struct_size = offset + new_len_without_nul_in_bytes + 2;
1081-
let layout =
1082-
Layout::from_size_align(struct_size as usize, align_of::<c::FILE_RENAME_INFO>())
1083-
.unwrap();
1074+
const too_long_err: io::Error =
1075+
io::const_error!(io::ErrorKind::InvalidFilename, "Filename too long");
1076+
let struct_size = to
1077+
.count_bytes()
1078+
.checked_mul(2)
1079+
.and_then(|x| x.checked_add(offset_of!(c::FILE_RENAME_INFO, FileName)))
1080+
.ok_or(too_long_err)?;
1081+
let layout = Layout::from_size_align(struct_size, align_of::<c::FILE_RENAME_INFO>())
1082+
.map_err(|_| too_long_err)?;
1083+
let to_byte_len_without_nul =
1084+
u32::try_from((to.count_bytes() - 1) * 2).map_err(|_| too_long_err)?;
1085+
let struct_size = u32::try_from(struct_size).map_err(|_| too_long_err)?;
10841086

1085-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10861087
let file_rename_info;
1088+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10871089
unsafe {
10881090
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
10891091
if file_rename_info.is_null() {
@@ -1096,7 +1098,7 @@ impl Dir {
10961098

10971099
(&raw mut (*file_rename_info).RootDirectory).write(to_dir.handle.as_raw_handle());
10981100
// Don't include the NULL in the size
1099-
(&raw mut (*file_rename_info).FileNameLength).write(new_len_without_nul_in_bytes);
1101+
(&raw mut (*file_rename_info).FileNameLength).write(to_byte_len_without_nul);
11001102

11011103
to.as_ptr().copy_to_nonoverlapping(
11021104
(&raw mut (*file_rename_info).FileName).cast::<u16>(),
@@ -1545,8 +1547,8 @@ pub fn rename(old: &WCStr, new: &WCStr) -> io::Result<()> {
15451547
Layout::from_size_align(struct_size as usize, align_of::<c::FILE_RENAME_INFO>())
15461548
.unwrap();
15471549

1548-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15491550
let file_rename_info;
1551+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15501552
unsafe {
15511553
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
15521554
if file_rename_info.is_null() {

0 commit comments

Comments
 (0)