Fix build on NetBSD (#10270)

* Fix build on NetBSD

Notably:

1. A typo in `f_flag` vs `f_flags` - this was probably never tested
2. Some pointless name differences  - `st_mtimensec` vs
`st_mtime_nsec`
3. The big one: This said that LC_GLOBAL_LOCALE() was -1 "everywhere".
   Well, not on NetBSD.

* ifdef for macos
This commit is contained in:
Fabian Boehm 2024-01-28 21:45:14 +01:00 committed by GitHub
parent 45285b3870
commit 6877773fdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 4 deletions

View file

@ -1,3 +1,4 @@
#include <locale.h>
#include <paths.h>
#include <stdbool.h>
#include <stdint.h>
@ -180,3 +181,9 @@ int C_RLIMIT_NTHR() {
return -1;
#endif
}
#ifdef LC_GLOBAL_LOCALE
locale_t C_LC_GLOBAL_LOCALE() {
return LC_GLOBAL_LOCALE;
}
#endif

View file

@ -102,3 +102,8 @@ extern "C" {
fn C_RLIMIT_NPTS() -> i32; // ifndef: -1
fn C_RLIMIT_NTHR() -> i32; // ifndef: -1
}
#[cfg(target_os = "netbsd")]
extern "C" {
pub fn C_LC_GLOBAL_LOCALE() -> libc::locale_t;
}

View file

@ -60,12 +60,17 @@ unsafe fn lconv_to_locale(lconv: &libc::lconv) -> Locale {
/// Read the numeric locale, or None on any failure.
#[cfg(localeconv_l)]
#[allow(non_snake_case)]
unsafe fn read_locale() -> Option<Locale> {
extern "C" {
fn localeconv_l(loc: libc::locale_t) -> *const libc::lconv;
}
/// Rust libc does not provide LC_GLOBAL_LOCALE, but it appears to be -1 everywhere.
/// Rust libc does not provide LC_GLOBAL_LOCALE, but it is usually -1.
#[cfg(not(target_os = "netbsd"))]
const LC_GLOBAL_LOCALE: libc::locale_t = (-1_isize) as libc::locale_t;
// On NetBSD it's not.
#[cfg(target_os = "netbsd")]
let LC_GLOBAL_LOCALE: libc::locale_t = unsafe { crate::libc::C_LC_GLOBAL_LOCALE() };
const empty: [libc::c_char; 1] = [0];
let cur = libc::duplocale(LC_GLOBAL_LOCALE);

View file

@ -684,14 +684,14 @@ fn path_remoteness(path: &wstr) -> DirRemoteness {
}
let mnt_local = MNT_LOCAL();
if mnt_local != 0 {
let mut buf: libc::statfs = unsafe { std::mem::zeroed() };
if unsafe { libc::statfs(narrow.as_ptr(), &mut buf) } < 0 {
let mut buf: libc::statvfs = unsafe { std::mem::zeroed() };
if unsafe { libc::statvfs(narrow.as_ptr(), &mut buf) } < 0 {
return DirRemoteness::unknown;
}
// statfs::f_flag is hard-coded as 64-bits on 32/64-bit FreeBSD but it's a (4-byte)
// long on 32-bit NetBSD.. and always 4-bytes on macOS (even on 64-bit builds).
#[allow(clippy::useless_conversion)]
return if u64::from(buf.f_flags) & mnt_local != 0 {
return if u64::from(buf.f_flag) & mnt_local != 0 {
DirRemoteness::local
} else {
DirRemoteness::remote

View file

@ -601,10 +601,17 @@ impl Screen {
unsafe { libc::fstat(STDOUT_FILENO, &mut post_buff_1) };
unsafe { libc::fstat(STDERR_FILENO, &mut post_buff_2) };
// Yes these differ in one `_`. I hate it.
#[cfg(not(target_os = "netbsd"))]
let changed = self.prev_buff_1.st_mtime != post_buff_1.st_mtime
|| self.prev_buff_1.st_mtime_nsec != post_buff_1.st_mtime_nsec
|| self.prev_buff_2.st_mtime != post_buff_2.st_mtime
|| self.prev_buff_2.st_mtime_nsec != post_buff_2.st_mtime_nsec;
#[cfg(target_os = "netbsd")]
let changed = self.prev_buff_1.st_mtime != post_buff_1.st_mtime
|| self.prev_buff_1.st_mtimensec != post_buff_1.st_mtimensec
|| self.prev_buff_2.st_mtime != post_buff_2.st_mtime
|| self.prev_buff_2.st_mtimensec != post_buff_2.st_mtimensec;
if changed {
// Ok, someone has been messing with our screen. We will want to repaint. However, we do not

View file

@ -537,10 +537,16 @@ impl FileId {
result.change_seconds = buf.st_ctime;
result.mod_seconds = buf.st_mtime;
#[allow(clippy::unnecessary_cast)] // platform-dependent
#[cfg(not(target_os = "netbsd"))]
{
result.change_nanoseconds = buf.st_ctime_nsec as _;
result.mod_nanoseconds = buf.st_mtime_nsec as _;
}
#[cfg(target_os = "netbsd")]
{
result.change_nanoseconds = buf.st_ctimensec as _;
result.mod_nanoseconds = buf.st_mtimensec as _;
}
result
}