Skip to content

Commit f4d6ffc

Browse files
committed
requested changes
1 parent 4890e60 commit f4d6ffc

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
@@ -132,7 +132,6 @@ pub enum TryLockError {
132132
WouldBlock,
133133
}
134134

135-
#[unstable(feature = "dirfd", issue = "120426")]
136135
/// An object providing access to a directory on the filesystem.
137136
///
138137
/// Files are automatically closed when they go out of scope. Errors detected
@@ -144,14 +143,18 @@ pub enum TryLockError {
144143
///
145144
/// ```no_run
146145
/// #![feature(dirfd)]
147-
/// use std::fs::Dir;
146+
/// use std::{fs::Dir, io::Read};
148147
///
149148
/// fn main() -> std::io::Result<()> {
150149
/// let dir = Dir::new("foo")?;
151-
/// let file = dir.open("bar.txt")?;
150+
/// let mut file = dir.open("bar.txt")?;
151+
/// let mut s = String::new();
152+
/// file.read_to_string(&mut s)?;
153+
/// println!("{}", s);
152154
/// Ok(())
153155
/// }
154156
/// ```
157+
#[unstable(feature = "dirfd", issue = "120426")]
155158
pub struct Dir {
156159
inner: fs_imp::Dir,
157160
}
@@ -1484,10 +1487,8 @@ impl Dir {
14841487
///
14851488
/// # Errors
14861489
///
1487-
/// This function will return an error in these (and other) situations:
1488-
/// * The path doesn't exist
1489-
/// * The path doesn't specify a directory
1490-
/// * The process doesn't have permission to read the directory
1490+
/// This function will return an error if `path` does not point to an existing directory.
1491+
/// Other errors may also be returned according to [`OpenOptions::open`].
14911492
///
14921493
/// # Examples
14931494
///
@@ -1514,10 +1515,7 @@ impl Dir {
15141515
///
15151516
/// # Errors
15161517
///
1517-
/// This function will return an error in these (and other) situations:
1518-
/// * The path doesn't exist
1519-
/// * The path doesn't specify a directory
1520-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1518+
/// This function may return an error according to [`OpenOptions::open`].
15211519
///
15221520
/// # Examples
15231521
///
@@ -1540,10 +1538,8 @@ impl Dir {
15401538
///
15411539
/// # Errors
15421540
///
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 read/write (according to `opts`) the directory
1541+
/// This function will return an error if `path` does not point to an existing file.
1542+
/// Other errors may also be returned according to [`OpenOptions::open`].
15471543
///
15481544
/// # Examples
15491545
///
@@ -1568,11 +1564,7 @@ impl Dir {
15681564
///
15691565
/// # Errors
15701566
///
1571-
/// This function may return an error in these (and other) situations, depending on the
1572-
/// specified `opts`:
1573-
/// * The path doesn't exist
1574-
/// * The path doesn't specify a regular file
1575-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1567+
/// This function may return an error according to [`OpenOptions::open`].
15761568
///
15771569
/// # Examples
15781570
///
@@ -1597,9 +1589,8 @@ impl Dir {
15971589
///
15981590
/// # Errors
15991591
///
1600-
/// This function will return an error in these (and other) situations:
1601-
/// * The path exists
1602-
/// * The process doesn't have permission to create the directory
1592+
/// This function will return an error if `path` points to an existing file or directory.
1593+
/// Other errors may also be returned according to [`OpenOptions::open`].
16031594
///
16041595
/// # Examples
16051596
///
@@ -1624,10 +1615,8 @@ impl Dir {
16241615
///
16251616
/// # Errors
16261617
///
1627-
/// This function will return an error in these (and other) situations:
1628-
/// * The path doesn't exist
1629-
/// * The path doesn't specify a regular file
1630-
/// * The process doesn't have permission to delete the file.
1618+
/// This function will return an error if `path` does not point to an existing file.
1619+
/// Other errors may also be returned according to [`OpenOptions::open`].
16311620
///
16321621
/// # Examples
16331622
///
@@ -1650,11 +1639,8 @@ impl Dir {
16501639
///
16511640
/// # Errors
16521641
///
1653-
/// This function will return an error in these (and other) situations:
1654-
/// * The path doesn't exist
1655-
/// * The path doesn't specify a directory
1656-
/// * The directory isn't empty
1657-
/// * The process doesn't have permission to delete the directory.
1642+
/// This function will return an error if `path` does not point to an existing, non-empty directory.
1643+
/// Other errors may also be returned according to [`OpenOptions::open`].
16581644
///
16591645
/// # Examples
16601646
///
@@ -1678,10 +1664,8 @@ impl Dir {
16781664
///
16791665
/// # Errors
16801666
///
1681-
/// This function will return an error in these (and other) situations:
1682-
/// * The `from` path doesn't exist
1683-
/// * The `from` path doesn't specify a directory
1684-
/// * `self` and `to_dir` are on different mount points
1667+
/// This function will return an error if `from` does not point to an existing file or directory.
1668+
/// Other errors may also be returned according to [`OpenOptions::open`].
16851669
///
16861670
/// # Examples
16871671
///

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

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

1079-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10801081
let file_rename_info;
1082+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10811083
unsafe {
10821084
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
10831085
if file_rename_info.is_null() {
@@ -1090,7 +1092,7 @@ impl Dir {
10901092

10911093
(&raw mut (*file_rename_info).RootDirectory).write(to_dir.handle.as_raw_handle());
10921094
// Don't include the NULL in the size
1093-
(&raw mut (*file_rename_info).FileNameLength).write(new_len_without_nul_in_bytes);
1095+
(&raw mut (*file_rename_info).FileNameLength).write(to_byte_len_without_nul);
10941096

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

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

0 commit comments

Comments
 (0)