Skip to content

Commit 32af4ce

Browse files
authored
Rollup merge of rust-lang#40024 - raphlinus:readdir, r=alexcrichton
Switch Fuchsia to readdir (instead of readdir_r) The readdir_r function is deprecated on newer Posix systems because of various problems, and not implemented at all for Fuchsia. There are already implementations using both, and this patch switches Fuchsia over to the readdir-based one. Fixes rust-lang#40021 for Fuchsia, but that issue also contains discussion of what should happen for other Posix systems.
2 parents ebde617 + b3ee249 commit 32af4ce

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/libstd/sys/unix/fs.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off_t as off64_t,
3535
ftruncate as ftruncate64, lseek as lseek64, dirent as dirent64, open as open64};
3636
#[cfg(not(any(target_os = "linux",
3737
target_os = "emscripten",
38-
target_os = "solaris")))]
38+
target_os = "solaris",
39+
target_os = "fuchsia")))]
3940
use libc::{readdir_r as readdir64_r};
4041

4142
pub struct File(FileDesc);
@@ -59,10 +60,10 @@ pub struct DirEntry {
5960
entry: dirent64,
6061
root: Arc<PathBuf>,
6162
// We need to store an owned copy of the directory name
62-
// on Solaris because a) it uses a zero-length array to
63-
// store the name, b) its lifetime between readdir calls
64-
// is not guaranteed.
65-
#[cfg(target_os = "solaris")]
63+
// on Solaris and Fuchsia because a) it uses a zero-length
64+
// array to store the name, b) its lifetime between readdir
65+
// calls is not guaranteed.
66+
#[cfg(any(target_os = "solaris", target_os = "fuchsia"))]
6667
name: Box<[u8]>
6768
}
6869

@@ -205,14 +206,14 @@ impl fmt::Debug for ReadDir {
205206
impl Iterator for ReadDir {
206207
type Item = io::Result<DirEntry>;
207208

208-
#[cfg(target_os = "solaris")]
209+
#[cfg(any(target_os = "solaris", target_os = "fuchsia"))]
209210
fn next(&mut self) -> Option<io::Result<DirEntry>> {
210211
unsafe {
211212
loop {
212213
// Although readdir_r(3) would be a correct function to use here because
213-
// of the thread safety, on Illumos the readdir(3C) function is safe to use
214-
// in threaded applications and it is generally preferred over the
215-
// readdir_r(3C) function.
214+
// of the thread safety, on Illumos and Fuchsia the readdir(3C) function
215+
// is safe to use in threaded applications and it is generally preferred
216+
// over the readdir_r(3C) function.
216217
super::os::set_errno(0);
217218
let entry_ptr = libc::readdir(self.dirp.0);
218219
if entry_ptr.is_null() {
@@ -240,7 +241,7 @@ impl Iterator for ReadDir {
240241
}
241242
}
242243

243-
#[cfg(not(target_os = "solaris"))]
244+
#[cfg(not(any(target_os = "solaris", target_os = "fuchsia")))]
244245
fn next(&mut self) -> Option<io::Result<DirEntry>> {
245246
unsafe {
246247
let mut ret = DirEntry {
@@ -344,14 +345,14 @@ impl DirEntry {
344345
#[cfg(any(target_os = "android",
345346
target_os = "linux",
346347
target_os = "emscripten",
347-
target_os = "haiku",
348-
target_os = "fuchsia"))]
348+
target_os = "haiku"))]
349349
fn name_bytes(&self) -> &[u8] {
350350
unsafe {
351351
CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes()
352352
}
353353
}
354-
#[cfg(target_os = "solaris")]
354+
#[cfg(any(target_os = "solaris",
355+
target_os = "fuchsia"))]
355356
fn name_bytes(&self) -> &[u8] {
356357
&*self.name
357358
}

src/libstd/sys/unix/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn errno() -> i32 {
6464
}
6565

6666
/// Sets the platform-specific value of errno
67-
#[cfg(target_os = "solaris")] // only needed for readdir so far
67+
#[cfg(any(target_os = "solaris", target_os = "fuchsia"))] // only needed for readdir so far
6868
pub fn set_errno(e: i32) {
6969
unsafe {
7070
*errno_location() = e as c_int

0 commit comments

Comments
 (0)