Skip to content

Commit 9badb42

Browse files
committed
requested changes
1 parent 58b0877 commit 9badb42

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-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
}
@@ -1432,10 +1435,8 @@ impl Dir {
14321435
///
14331436
/// # Errors
14341437
///
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
1438+
/// This function will return an error if `path` does not point to an existing directory.
1439+
/// Other errors may also be returned according to [`OpenOptions::open`].
14391440
///
14401441
/// # Examples
14411442
///
@@ -1462,10 +1463,7 @@ impl Dir {
14621463
///
14631464
/// # Errors
14641465
///
1465-
/// This function will return an error in these (and other) situations:
1466-
/// * The path doesn't exist
1467-
/// * The path doesn't specify a directory
1468-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1466+
/// This function may return an error according to [`OpenOptions::open`].
14691467
///
14701468
/// # Examples
14711469
///
@@ -1488,10 +1486,8 @@ impl Dir {
14881486
///
14891487
/// # Errors
14901488
///
1491-
/// This function will return an error in these (and other) situations:
1492-
/// * The path doesn't exist
1493-
/// * The path doesn't specify a regular file
1494-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1489+
/// This function will return an error if `path` does not point to an existing file.
1490+
/// Other errors may also be returned according to [`OpenOptions::open`].
14951491
///
14961492
/// # Examples
14971493
///
@@ -1516,11 +1512,7 @@ impl Dir {
15161512
///
15171513
/// # Errors
15181514
///
1519-
/// This function may return an error in these (and other) situations, depending on the
1520-
/// specified `opts`:
1521-
/// * The path doesn't exist
1522-
/// * The path doesn't specify a regular file
1523-
/// * The process doesn't have permission to read/write (according to `opts`) the directory
1515+
/// This function may return an error according to [`OpenOptions::open`].
15241516
///
15251517
/// # Examples
15261518
///
@@ -1545,9 +1537,8 @@ impl Dir {
15451537
///
15461538
/// # Errors
15471539
///
1548-
/// This function will return an error in these (and other) situations:
1549-
/// * The path exists
1550-
/// * The process doesn't have permission to create the directory
1540+
/// This function will return an error if `path` points to an existing file or directory.
1541+
/// Other errors may also be returned according to [`OpenOptions::open`].
15511542
///
15521543
/// # Examples
15531544
///
@@ -1572,10 +1563,8 @@ impl Dir {
15721563
///
15731564
/// # Errors
15741565
///
1575-
/// This function will return an error in these (and other) situations:
1576-
/// * The path doesn't exist
1577-
/// * The path doesn't specify a regular file
1578-
/// * The process doesn't have permission to delete the file.
1566+
/// This function will return an error if `path` does not point to an existing file.
1567+
/// Other errors may also be returned according to [`OpenOptions::open`].
15791568
///
15801569
/// # Examples
15811570
///
@@ -1598,11 +1587,8 @@ impl Dir {
15981587
///
15991588
/// # Errors
16001589
///
1601-
/// This function will return an error in these (and other) situations:
1602-
/// * The path doesn't exist
1603-
/// * The path doesn't specify a directory
1604-
/// * The directory isn't empty
1605-
/// * The process doesn't have permission to delete the directory.
1590+
/// This function will return an error if `path` does not point to an existing, non-empty directory.
1591+
/// Other errors may also be returned according to [`OpenOptions::open`].
16061592
///
16071593
/// # Examples
16081594
///
@@ -1626,10 +1612,8 @@ impl Dir {
16261612
///
16271613
/// # Errors
16281614
///
1629-
/// This function will return an error in these (and other) situations:
1630-
/// * The `from` path doesn't exist
1631-
/// * The `from` path doesn't specify a directory
1632-
/// * `self` and `to_dir` are on different mount points
1615+
/// This function will return an error if `from` does not point to an existing file or directory.
1616+
/// Other errors may also be returned according to [`OpenOptions::open`].
16331617
///
16341618
/// # Examples
16351619
///

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

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

1077-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10781077
let file_rename_info;
1078+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
10791079
unsafe {
10801080
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
10811081
if file_rename_info.is_null() {
@@ -1088,7 +1088,7 @@ impl Dir {
10881088

10891089
(&raw mut (*file_rename_info).RootDirectory).write(to_dir.handle.as_raw_handle());
10901090
// Don't include the NULL in the size
1091-
(&raw mut (*file_rename_info).FileNameLength).write(new_len_without_nul_in_bytes);
1091+
(&raw mut (*file_rename_info).FileNameLength).write(to_byte_len_without_nul);
10921092

10931093
to.as_ptr().copy_to_nonoverlapping(
10941094
(&raw mut (*file_rename_info).FileName).cast::<u16>(),
@@ -1537,8 +1537,8 @@ pub fn rename(old: &WCStr, new: &WCStr) -> io::Result<()> {
15371537
Layout::from_size_align(struct_size as usize, align_of::<c::FILE_RENAME_INFO>())
15381538
.unwrap();
15391539

1540-
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15411540
let file_rename_info;
1541+
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
15421542
unsafe {
15431543
file_rename_info = alloc(layout).cast::<c::FILE_RENAME_INFO>();
15441544
if file_rename_info.is_null() {

0 commit comments

Comments
 (0)