Skip to content

Commit

Permalink
Future-proof for FreeBSD 12 (#469)
Browse files Browse the repository at this point in the history
FreeBSD 12 changes the dirent structure, among other things.  libc
currently binds a FreeBSD 11 ABI, but that will change some day.  Tweak
rustix's dirent initialization code to work with either FreeBSD 11 or
12.
  • Loading branch information
asomers committed Dec 3, 2022
1 parent d66ba5c commit e9cce9b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/backend/libc/fs/dir.rs
Expand Up @@ -242,7 +242,7 @@ unsafe fn read_dirent(input: &libc_dirent) -> libc_dirent {
// with a field that we missed here. And we can avoid blindly copying the
// whole `d_name` field, which may not be entirely allocated.
#[cfg_attr(target_os = "wasi", allow(unused_mut))]
#[cfg(not(target_os = "dragonfly"))]
#[cfg(not(any(target_os = "freebsd", target_os = "dragonfly")))]
let mut dirent = libc_dirent {
#[cfg(not(any(
target_os = "aix",
Expand All @@ -253,7 +253,7 @@ unsafe fn read_dirent(input: &libc_dirent) -> libc_dirent {
d_type,
#[cfg(not(any(
target_os = "aix",
target_os = "freebsd",
target_os = "freebsd", // Until FreeBSD 12
target_os = "haiku",
target_os = "ios",
target_os = "macos",
Expand Down Expand Up @@ -306,14 +306,18 @@ unsafe fn read_dirent(input: &libc_dirent) -> libc_dirent {
pub d_name: [::c_char; 1024], // Max length is _POSIX_PATH_MAX
// */

// On dragonfly, `dirent` has some non-public padding fields so we can't
// directly initialize it.
#[cfg(target_os = "dragonfly")]
let mut dirent = unsafe {
// On dragonfly and FreeBSD 12, `dirent` has some non-public padding fields
// so we can't directly initialize it.
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
let mut dirent = {
let mut dirent: libc_dirent = zeroed();
dirent.d_fileno = d_fileno;
dirent.d_namlen = d_namlen;
dirent.d_type = d_type;
#[cfg(target_os = "freebsd")]
{
dirent.d_reclen = d_reclen;
}
dirent
};

Expand Down

0 comments on commit e9cce9b

Please sign in to comment.