Skip to content

Commit 3f5c57c

Browse files
committed
requested changes
1 parent 616c4ce commit 3f5c57c

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
}
@@ -1500,10 +1503,8 @@ impl Dir {
15001503
///
15011504
/// # Errors
15021505
///
1503-
/// This function will return an error in these (and other) situations:
1504-
/// * The path doesn't exist
1505-
/// * The path doesn't specify a directory
1506-
/// * The process doesn't have permission to read the directory
1506+
/// This function will return an error if `path` does not point to an existing directory.
1507+
/// Other errors may also be returned according to [`OpenOptions::open`].
15071508
///
15081509
/// # Examples
15091510
///
@@ -1530,10 +1531,7 @@ impl Dir {
15301531
///
15311532
/// # Errors
15321533
///
1533-
/// This function will return an error in these (and other) situations:
1534-
/// * The path doesn't exist
1535-
/// * The path doesn't specify a directory
1536-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1534+
/// This function may return an error according to [`OpenOptions::open`].
15371535
///
15381536
/// # Examples
15391537
///
@@ -1556,10 +1554,8 @@ impl Dir {
15561554
///
15571555
/// # Errors
15581556
///
1559-
/// This function will return an error in these (and other) situations:
1560-
/// * The path doesn't exist
1561-
/// * The path doesn't specify a regular file
1562-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1557+
/// This function will return an error if `path` does not point to an existing file.
1558+
/// Other errors may also be returned according to [`OpenOptions::open`].
15631559
///
15641560
/// # Examples
15651561
///
@@ -1584,11 +1580,7 @@ impl Dir {
15841580
///
15851581
/// # Errors
15861582
///
1587-
/// This function may return an error in these (and other) situations, depending on the
1588-
/// specified `opts`:
1589-
/// * The path doesn't exist
1590-
/// * The path doesn't specify a regular file
1591-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1583+
/// This function may return an error according to [`OpenOptions::open`].
15921584
///
15931585
/// # Examples
15941586
///
@@ -1613,9 +1605,8 @@ impl Dir {
16131605
///
16141606
/// # Errors
16151607
///
1616-
/// This function will return an error in these (and other) situations:
1617-
/// * The path exists
1618-
/// * The process doesn't have permission to create the directory
1608+
/// This function will return an error if `path` points to an existing file or directory.
1609+
/// Other errors may also be returned according to [`OpenOptions::open`].
16191610
///
16201611
/// # Examples
16211612
///
@@ -1640,10 +1631,8 @@ impl Dir {
16401631
///
16411632
/// # Errors
16421633
///
1643-
/// This function will return an error in these (and other) situations:
1644-
/// * The path doesn't exist
1645-
/// * The path doesn't specify a regular file
1646-
/// * The process doesn't have permission to delete the file.
1634+
/// This function will return an error if `path` does not point to an existing file.
1635+
/// Other errors may also be returned according to [`OpenOptions::open`].
16471636
///
16481637
/// # Examples
16491638
///
@@ -1666,11 +1655,8 @@ impl Dir {
16661655
///
16671656
/// # Errors
16681657
///
1669-
/// This function will return an error in these (and other) situations:
1670-
/// * The path doesn't exist
1671-
/// * The path doesn't specify a directory
1672-
/// * The directory isn't empty
1673-
/// * The process doesn't have permission to delete the directory.
1658+
/// This function will return an error if `path` does not point to an existing, non-empty directory.
1659+
/// Other errors may also be returned according to [`OpenOptions::open`].
16741660
///
16751661
/// # Examples
16761662
///
@@ -1694,10 +1680,8 @@ impl Dir {
16941680
///
16951681
/// # Errors
16961682
///
1697-
/// This function will return an error in these (and other) situations:
1698-
/// * The `from` path doesn't exist
1699-
/// * The `from` path doesn't specify a directory
1700-
/// * `self` and `to_dir` are on different mount points
1683+
/// This function will return an error if `from` does not point to an existing file or directory.
1684+
/// Other errors may also be returned according to [`OpenOptions::open`].
17011685
///
17021686
/// # Examples
17031687
///

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

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

1080-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10811082
let file_rename_info;
1083+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10821084
unsafe {
10831085
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
10841086
if file_rename_info.is_null() {
@@ -1091,7 +1093,7 @@ impl Dir {
10911093

10921094
(&raw mut (*file_rename_info).RootDirectory).write(to_dir.handle.as_raw_handle());
10931095
// Don't include the NULL in the size
1094-
(&raw mut (*file_rename_info).FileNameLength).write(new_len_without_nul_in_bytes);
1096+
(&raw mut (*file_rename_info).FileNameLength).write(to_byte_len_without_nul);
10951097

10961098
to.as_ptr().copy_to_nonoverlapping(
10971099
(&raw mut (*file_rename_info).FileName).cast::<u16>(),
@@ -1540,8 +1542,8 @@ pub fn rename(old: &WCStr, new: &WCStr) -> io::Result<()> {
15401542
Layout::from_size_align(struct_size as usize, align_of::<c::FILE_RENAME_INFO>())
15411543
.unwrap();
15421544

1543-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15441545
let file_rename_info;
1546+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15451547
unsafe {
15461548
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
15471549
if file_rename_info.is_null() {

0 commit comments

Comments
 (0)