Skip to content

Commit

Permalink
Merge #1908
Browse files Browse the repository at this point in the history
1908: Move some pure formatting changes out of #1863 r=asomers a=SUPERCILEX



Co-authored-by: Alex Saveau <[email protected]>
  • Loading branch information
bors[bot] and SUPERCILEX authored Dec 4, 2022
2 parents 1fd5fe7 + 59f66d1 commit 40c8535
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl Entry {

/// Returns the bare file name of this directory entry without any other leading path component.
pub fn file_name(&self) -> &ffi::CStr {
unsafe { ::std::ffi::CStr::from_ptr(self.0.d_name.as_ptr()) }
unsafe { ffi::CStr::from_ptr(self.0.d_name.as_ptr()) }
}

/// Returns the type of this directory entry, if known.
Expand Down
111 changes: 67 additions & 44 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
}
}

#[cfg(all(target_os = "linux", target_env = "gnu",))]
#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[cfg(feature = "fs")]
libc_bitflags! {
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
Expand All @@ -250,10 +250,7 @@ libc_bitflags! {

feature! {
#![feature = "fs"]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
))]
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub fn renameat2<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
old_dirfd: Option<RawFd>,
old_path: &P1,
Expand Down Expand Up @@ -308,54 +305,80 @@ fn readlink_maybe_at<P: ?Sized + NixPath>(

fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P) -> Result<OsString> {
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
// simple case: result is strictly less than `PATH_MAX`
let res = readlink_maybe_at(dirfd, path, &mut v)?;
let len = Errno::result(res)?;
debug_assert!(len >= 0);
if (len as usize) < v.capacity() {
return wrap_readlink_result(v, res);

{
// simple case: result is strictly less than `PATH_MAX`
let res = readlink_maybe_at(dirfd, path, &mut v)?;
let len = Errno::result(res)?;
debug_assert!(len >= 0);
if (len as usize) < v.capacity() {
return wrap_readlink_result(v, res);
}
}

// Uh oh, the result is too long...
// Let's try to ask lstat how many bytes to allocate.
let reported_size = match dirfd {
#[cfg(target_os = "redox")]
Some(_) => unreachable!(),
#[cfg(any(target_os = "android", target_os = "linux"))]
Some(dirfd) => {
let flags = if path.is_empty() { AtFlags::AT_EMPTY_PATH } else { AtFlags::empty() };
super::sys::stat::fstatat(dirfd, path, flags | AtFlags::AT_SYMLINK_NOFOLLOW)
},
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "redox")))]
Some(dirfd) => super::sys::stat::fstatat(dirfd, path, AtFlags::AT_SYMLINK_NOFOLLOW),
None => super::sys::stat::lstat(path)
}
let mut try_size = {
let reported_size = match dirfd {
#[cfg(target_os = "redox")]
Some(_) => unreachable!(),
#[cfg(any(target_os = "android", target_os = "linux"))]
Some(dirfd) => {
let flags = if path.is_empty() {
AtFlags::AT_EMPTY_PATH
} else {
AtFlags::empty()
};
super::sys::stat::fstatat(
dirfd,
path,
flags | AtFlags::AT_SYMLINK_NOFOLLOW,
)
}
#[cfg(not(any(
target_os = "android",
target_os = "linux",
target_os = "redox"
)))]
Some(dirfd) => super::sys::stat::fstatat(
dirfd,
path,
AtFlags::AT_SYMLINK_NOFOLLOW,
),
None => super::sys::stat::lstat(path),
}
.map(|x| x.st_size)
.unwrap_or(0);
let mut try_size = if reported_size > 0 {
// Note: even if `lstat`'s apparently valid answer turns out to be
// wrong, we will still read the full symlink no matter what.
reported_size as usize + 1
} else {
// If lstat doesn't cooperate, or reports an error, be a little less
// precise.
(libc::PATH_MAX as usize).max(128) << 1

if reported_size > 0 {
// Note: even if `lstat`'s apparently valid answer turns out to be
// wrong, we will still read the full symlink no matter what.
reported_size as usize + 1
} else {
// If lstat doesn't cooperate, or reports an error, be a little less
// precise.
(libc::PATH_MAX as usize).max(128) << 1
}
};

loop {
v.reserve_exact(try_size);
let res = readlink_maybe_at(dirfd, path, &mut v)?;
let len = Errno::result(res)?;
debug_assert!(len >= 0);
if (len as usize) < v.capacity() {
break wrap_readlink_result(v, res);
} else {
// Ugh! Still not big enough!
match try_size.checked_shl(1) {
Some(next_size) => try_size = next_size,
// It's absurd that this would happen, but handle it sanely
// anyway.
None => break Err(Errno::ENAMETOOLONG),
{
v.reserve_exact(try_size);
let res = readlink_maybe_at(dirfd, path, &mut v)?;
let len = Errno::result(res)?;
debug_assert!(len >= 0);
if (len as usize) < v.capacity() {
return wrap_readlink_result(v, res);
}
}

// Ugh! Still not big enough!
match try_size.checked_shl(1) {
Some(next_size) => try_size = next_size,
// It's absurd that this would happen, but handle it sanely
// anyway.
None => break Err(Errno::ENAMETOOLONG),
}
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,14 +2221,14 @@ pub fn recvfrom<T: SockaddrLike>(
buf.as_ptr() as *mut c_void,
buf.len() as size_t,
0,
addr.as_mut_ptr() as *mut libc::sockaddr,
addr.as_mut_ptr() as *mut sockaddr,
&mut len as *mut socklen_t,
))? as usize;

Ok((
ret,
T::from_raw(
addr.assume_init().as_ptr() as *const libc::sockaddr,
addr.assume_init().as_ptr() as *const sockaddr,
Some(len),
),
))
Expand Down Expand Up @@ -2336,11 +2336,8 @@ pub fn getpeername<T: SockaddrLike>(fd: RawFd) -> Result<T> {
let mut addr = mem::MaybeUninit::<T>::uninit();
let mut len = T::size();

let ret = libc::getpeername(
fd,
addr.as_mut_ptr() as *mut libc::sockaddr,
&mut len,
);
let ret =
libc::getpeername(fd, addr.as_mut_ptr() as *mut sockaddr, &mut len);

Errno::result(ret)?;

Expand All @@ -2356,11 +2353,8 @@ pub fn getsockname<T: SockaddrLike>(fd: RawFd) -> Result<T> {
let mut addr = mem::MaybeUninit::<T>::uninit();
let mut len = T::size();

let ret = libc::getsockname(
fd,
addr.as_mut_ptr() as *mut libc::sockaddr,
&mut len,
);
let ret =
libc::getsockname(fd, addr.as_mut_ptr() as *mut sockaddr, &mut len);

Errno::result(ret)?;

Expand Down
Loading

0 comments on commit 40c8535

Please sign in to comment.