diff options
| author | Tavian Barnes <tavianator@tavianator.com> | 2022-01-20 16:54:45 -0500 |
|---|---|---|
| committer | Tavian Barnes <tavianator@tavianator.com> | 2022-01-21 07:59:14 -0500 |
| commit | c3e92fec948f3875d1fa66d37c8e2599e6140215 (patch) | |
| tree | ea99a971c82a180aebecbdf932d4f81df1cfa213 | |
| parent | 777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b (diff) | |
| download | rust-c3e92fec948f3875d1fa66d37c8e2599e6140215.tar.gz rust-c3e92fec948f3875d1fa66d37c8e2599e6140215.zip | |
fs: Implement more ReadDir methods in terms of name_cstr()
| -rw-r--r-- | library/std/src/sys/unix/fs.rs | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index f8deda93fe2..14e8f06b303 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -531,17 +531,17 @@ impl Drop for Dir { impl DirEntry { pub fn path(&self) -> PathBuf { - self.dir.root.join(OsStr::from_bytes(self.name_bytes())) + self.dir.root.join(self.file_name_os_str()) } pub fn file_name(&self) -> OsString { - OsStr::from_bytes(self.name_bytes()).to_os_string() + self.file_name_os_str().to_os_string() } #[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.dirp.0) })?; - let name = self.entry.d_name.as_ptr(); + let name = self.name_cstr().as_ptr(); cfg_has_statx! { if let Some(ret) = unsafe { try_statx( @@ -639,26 +639,16 @@ impl DirEntry { ) } } - #[cfg(any( - target_os = "android", - target_os = "linux", - target_os = "emscripten", - target_os = "l4re", - target_os = "haiku", - target_os = "vxworks", - target_os = "espidf" - ))] - fn name_bytes(&self) -> &[u8] { - unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes() } - } - #[cfg(any( - target_os = "solaris", - target_os = "illumos", - target_os = "fuchsia", - target_os = "redox" - ))] + #[cfg(not(any( + target_os = "macos", + target_os = "ios", + target_os = "netbsd", + target_os = "openbsd", + target_os = "freebsd", + target_os = "dragonfly" + )))] fn name_bytes(&self) -> &[u8] { - self.name.as_bytes() + self.name_cstr().to_bytes() } #[cfg(not(any( @@ -670,7 +660,12 @@ impl DirEntry { fn name_cstr(&self) -> &CStr { unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) } } - #[cfg(any(target_os = "solaris", target_os = "illumos", target_os = "fuchsia"))] + #[cfg(any( + target_os = "solaris", + target_os = "illumos", + target_os = "fuchsia", + target_os = "redox" + ))] fn name_cstr(&self) -> &CStr { &self.name } |
