Misc yanix fixes (#715)

* Correctly handle possibly misaligned pointers in readdir

This reapplies #615, which was inadvertently reverted.

* Tidy up unneeded `self::` qualifiers.

* Make Dir's contents private.

Also remove the `unsafe` from `impl_iter`. With `Dir`'s field being
private, we can rely on the pointer being only what we've assigned to
it.

* Make `poll`'s timeout argument a `libc::c_int`.

This clarifies that there are no subsequent conversions before calling the
underlying libc API.

* Use clock_gettime instead of clock_getres to get the time.

* Mark FileType::from_raw as safe.

It handles unknown values, so it can be marked safe.
This commit is contained in:
Dan Gohman
2019-12-16 13:34:22 -08:00
committed by GitHub
parent e21016f773
commit c2ba419409
11 changed files with 40 additions and 26 deletions

View File

@@ -8,7 +8,7 @@ use std::{ffi::CStr, ops::Deref, ptr};
pub use crate::sys::EntryExt;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Dir(pub(crate) ptr::NonNull<libc::DIR>);
pub struct Dir(ptr::NonNull<libc::DIR>);
impl Dir {
/// Takes the ownership of the passed-in descriptor-based object,
@@ -51,6 +51,12 @@ impl Dir {
let loc = unsafe { libc::telldir(self.0.as_ptr()) };
SeekLoc(loc)
}
/// For use by platform-specific implementation code. Returns the raw
/// underlying state.
pub(crate) fn as_raw(&self) -> ptr::NonNull<libc::DIR> {
self.0
}
}
unsafe impl Send for Dir {}
@@ -78,7 +84,7 @@ impl Entry {
/// Returns the type of this directory entry.
pub fn file_type(&self) -> FileType {
unsafe { FileType::from_raw(self.0.d_type) }
FileType::from_raw(self.0.d_type)
}
}
@@ -107,7 +113,7 @@ pub enum FileType {
}
impl FileType {
pub unsafe fn from_raw(file_type: u8) -> Self {
pub fn from_raw(file_type: u8) -> Self {
match file_type {
libc::DT_CHR => Self::CharacterDevice,
libc::DT_DIR => Self::Directory,
@@ -153,6 +159,6 @@ where
type Item = Result<Entry>;
fn next(&mut self) -> Option<Self::Item> {
unsafe { iter_impl(&self.0).map(|x| x.map(Entry)) }
iter_impl(&self.0).map(|x| x.map(Entry))
}
}