Skip to content

Commit 6579cc2

Browse files
YodonTansylvestre
authored andcommitted
ohos: adapt uucore, hostid and date for aarch64-unknown-linux-ohos
Fixes required to build uutils/coreutils on HarmonyOS (aarch64-unknown-linux-ohos): - uucore/fsext: statfs.f_type is u64 on OHOS; route it through the musl branch. - hostid: implement gethostid locally (read /etc/hostid, else hash hostname) because the OHOS SDK libc dropped the symbol. - date: pass through the system time zone ID from OH_TimeService_GetTimeZone and resolve it with embedded IANA tzdata (jiff-tzdb) via TimeZone::tzif, instead of relying on /etc/localtime or a POSIX-injected TZ. The OHOS-only helper is cfg-gated; non-OHOS code paths are byte-identical to upstream. utmpx is intentionally left untouched: upstream #14252 disables it on OHOS (no utmp data source there), and this PR defers to that approach. Also syncs fuzz/Cargo.lock after adding the jiff-tzdb dependency. Verified on-device: date matches system time (+0800 CST; 1987-06-01 -> +0900 CDT), hostid behaves, builds pass on the non-utmpx feature set.
1 parent a8178fd commit 6579cc2

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎fuzz/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/uu/date/Cargo.toml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ uucore = { workspace = true, features = ["parser", "i18n-datetime"] }
4444
libc = { workspace = true }
4545
rustix = { workspace = true, features = ["time"] }
4646

47+
[target.'cfg(target_env = "ohos")'.dependencies]
48+
# OHOS has no /usr/share/zoneinfo or /etc/localtime: embed IANA tzdata so
49+
# ohos_system_zone() can resolve the TimeService timezone ID directly (pass-through,
50+
# no environment injection).
51+
jiff-tzdb = "0.1"
52+
4753
[target.'cfg(windows)'.dependencies]
4854
windows-sys = { workspace = true, features = [
4955
"Win32_Foundation",

‎src/uu/date/src/date.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ use windows_sys::Win32::{Foundation::SYSTEMTIME, System::SystemInformation::SetS
3232

3333
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
3434

35+
/// OHOS helper: pass through the system time zone ID returned by
36+
/// TimeService (OH_TimeService_GetTimeZone, e.g. "Asia/Shanghai") and
37+
/// resolve it against the embedded IANA tzdata (jiff-tzdb) so that
38+
/// historial DST rules and transitions are preserved. jiff's
39+
/// `try_system()` is useless on OHOS because both `/etc/localtime` and
40+
/// the zoneinfo dirs are absent.
41+
#[cfg(target_env = "ohos")]
42+
fn ohos_system_zone() -> jiff::tz::TimeZone {
43+
use core::ffi::{CStr, c_char};
44+
45+
#[link(name = "time_service_ndk")]
46+
unsafe extern "C" {
47+
fn OH_TimeService_GetTimeZone(tz: *mut c_char, len: u32) -> i32;
48+
}
49+
let mut buf = [0u8; 64];
50+
let rc = unsafe { OH_TimeService_GetTimeZone(buf.as_mut_ptr() as *mut c_char, 64) };
51+
if rc != 0 {
52+
return jiff::tz::TimeZone::UTC;
53+
}
54+
let id = unsafe { CStr::from_ptr(buf.as_ptr() as *const c_char) }
55+
.to_string_lossy()
56+
.into_owned();
57+
if let Some((name, tzif)) = jiff_tzdb::get(&id) {
58+
if let Ok(tz) = jiff::tz::TimeZone::tzif(name, tzif) {
59+
return tz;
60+
}
61+
}
62+
jiff::tz::TimeZone::UTC
63+
}
64+
3565
// Options
3666
const DATE: &str = "date";
3767
const HOURS: &str = "hours";
@@ -371,7 +401,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
371401
let now = if utc {
372402
Timestamp::now().to_zoned(TimeZone::UTC)
373403
} else {
374-
Zoned::now()
404+
#[cfg(target_env = "ohos")]
405+
{
406+
Timestamp::now().to_zoned(ohos_system_zone())
407+
}
408+
#[cfg(not(target_env = "ohos"))]
409+
{
410+
Zoned::now()
411+
}
375412
};
376413

377414
let set_to = match matches.get_one::<String>(OPT_SET) {
@@ -557,12 +594,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
557594
translate!("date-error-cannot-set-date", "path" => path.quote(), "error" => e),
558595
)
559596
})?;
597+
#[cfg(target_env = "ohos")]
598+
let date = ts.to_zoned(ohos_system_zone());
599+
#[cfg(not(target_env = "ohos"))]
560600
let date = ts.to_zoned(TimeZone::try_system().unwrap_or(TimeZone::UTC));
561601
let iter = std::iter::once(Ok(ParsedDateTime::InRange(date)));
562602
Box::new(iter)
563603
}
564604
DateSource::Resolution => {
565605
let resolution = get_clock_resolution();
606+
#[cfg(target_env = "ohos")]
607+
let date = resolution.to_zoned(ohos_system_zone());
608+
#[cfg(not(target_env = "ohos"))]
566609
let date = resolution.to_zoned(TimeZone::system());
567610
let iter = std::iter::once(Ok(ParsedDateTime::InRange(date)));
568611
Box::new(iter)

‎src/uu/hostid/src/hostid.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,37 @@
77

88
use clap::Command;
99
use core::ffi::c_long;
10+
#[cfg(not(target_env = "ohos"))]
1011
use libc::gethostid;
1112
use std::io::{Write, stdout};
1213
use uucore::{error::UResult, format_usage};
1314

1415
use uucore::translate;
1516

17+
// OHOS SDK libc no longer exports gethostid; replicate the glibc semantics:
18+
// read /etc/hostid when present, otherwise hash the hostname.
19+
#[cfg(target_env = "ohos")]
20+
fn gethostid() -> c_long {
21+
use std::fs::read;
22+
if let Ok(data) = read("/etc/hostid") {
23+
if data.len() >= 4 {
24+
let n: u32 = u32::from_ne_bytes([data[0], data[1], data[2], data[3]]);
25+
return n as c_long;
26+
}
27+
}
28+
let mut name = [0u8; 256];
29+
if unsafe { libc::gethostname(name.as_mut_ptr() as *mut _, name.len()) } == 0 {
30+
let end = name.iter().position(|&b| b == 0).unwrap_or(name.len());
31+
let mut h: u32 = 0x811c9dc5;
32+
for &b in &name[..end] {
33+
h ^= b as u32;
34+
h = h.wrapping_mul(0x01000193);
35+
}
36+
return h as c_long;
37+
}
38+
0
39+
}
40+
1641
#[uucore::main(no_signals)]
1742
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1843
uucore::clap_localization::handle_clap_result(uu_app(), args)?;

‎src/uucore/src/lib/features/fsext.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl FsMeta for StatFs {
799799
))]
800800
fn fs_type(&self) -> i64 {
801801
#[cfg(all(
802-
not(target_env = "musl"),
802+
not(any(target_env = "musl", target_env = "ohos")),
803803
not(target_vendor = "apple"),
804804
not(target_os = "android"),
805805
not(target_os = "freebsd"),
@@ -808,7 +808,7 @@ impl FsMeta for StatFs {
808808
))]
809809
return self.f_type;
810810
#[cfg(all(
811-
not(target_env = "musl"),
811+
not(any(target_env = "musl", target_env = "ohos")),
812812
any(
813813
target_vendor = "apple",
814814
all(target_os = "android", target_pointer_width = "32"),
@@ -820,6 +820,7 @@ impl FsMeta for StatFs {
820820
return self.f_type.into();
821821
#[cfg(any(
822822
target_env = "musl",
823+
target_env = "ohos",
823824
all(target_os = "android", target_pointer_width = "64"),
824825
))]
825826
return self.f_type.try_into().unwrap();

0 commit comments

Comments
 (0)