Skip to content

Commit

Permalink
Auto merge of #77674 - cuviper:direntry-diet, r=dtolnay
Browse files Browse the repository at this point in the history
unix/vxworks: make DirEntry slightly smaller

`DirEntry` contains a `ReadDir` handle, which used to just be a wrapper
on `Arc<InnerReadDir>`. Commit af75314 added `end_of_stream: bool`
which is not needed by `DirEntry`, but adds 8 bytes after padding. We
can let `DirEntry` have an `Arc<InnerReadDir>` directly to avoid that.
  • Loading branch information
bors committed Oct 9, 2020
2 parents 7b06cb1 + 1d06b07 commit 5ddef54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
28 changes: 21 additions & 7 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ struct InnerReadDir {
root: PathBuf,
}

#[derive(Clone)]
pub struct ReadDir {
inner: Arc<InnerReadDir>,
#[cfg(not(any(
target_os = "solaris",
target_os = "illumos",
target_os = "fuchsia",
target_os = "redox",
)))]
end_of_stream: bool,
}

Expand All @@ -196,7 +201,7 @@ unsafe impl Sync for Dir {}

pub struct DirEntry {
entry: dirent64,
dir: ReadDir,
dir: Arc<InnerReadDir>,
// We need to store an owned copy of the entry name
// on Solaris and Fuchsia because a) it uses a zero-length
// array to store the name, b) its lifetime between readdir
Expand Down Expand Up @@ -443,7 +448,7 @@ impl Iterator for ReadDir {
name: slice::from_raw_parts(name as *const u8, namelen as usize)
.to_owned()
.into_boxed_slice(),
dir: self.clone(),
dir: Arc::clone(&self.inner),
};
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
return Some(Ok(ret));
Expand All @@ -464,7 +469,7 @@ impl Iterator for ReadDir {
}

unsafe {
let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
let mut entry_ptr = ptr::null_mut();
loop {
if readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
Expand Down Expand Up @@ -497,7 +502,7 @@ impl Drop for Dir {

impl DirEntry {
pub fn path(&self) -> PathBuf {
self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
}

pub fn file_name(&self) -> OsString {
Expand All @@ -506,7 +511,7 @@ impl DirEntry {

#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "android"))]
pub fn metadata(&self) -> io::Result<FileAttr> {
let fd = cvt(unsafe { dirfd(self.dir.inner.dirp.0) })?;
let fd = cvt(unsafe { dirfd(self.dir.dirp.0) })?;
let name = self.entry.d_name.as_ptr();

cfg_has_statx! {
Expand Down Expand Up @@ -944,7 +949,16 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
Err(Error::last_os_error())
} else {
let inner = InnerReadDir { dirp: Dir(ptr), root };
Ok(ReadDir { inner: Arc::new(inner), end_of_stream: false })
Ok(ReadDir {
inner: Arc::new(inner),
#[cfg(not(any(
target_os = "solaris",
target_os = "illumos",
target_os = "fuchsia",
target_os = "redox",
)))]
end_of_stream: false,
})
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions library/std/src/sys/vxworks/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct InnerReadDir {
root: PathBuf,
}

#[derive(Clone)]
pub struct ReadDir {
inner: Arc<InnerReadDir>,
end_of_stream: bool,
Expand All @@ -40,7 +39,7 @@ unsafe impl Sync for Dir {}

pub struct DirEntry {
entry: dirent,
dir: ReadDir,
dir: Arc<InnerReadDir>,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -170,7 +169,7 @@ impl Iterator for ReadDir {
}

unsafe {
let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
let mut entry_ptr = ptr::null_mut();
loop {
if readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
Expand Down Expand Up @@ -204,7 +203,7 @@ impl Drop for Dir {
impl DirEntry {
pub fn path(&self) -> PathBuf {
use crate::sys::vxworks::ext::ffi::OsStrExt;
self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
}

pub fn file_name(&self) -> OsString {
Expand Down

0 comments on commit 5ddef54

Please sign in to comment.