From 396957b9189065640ec18176493dc6efd3a6b167 Mon Sep 17 00:00:00 2001 From: Jeryn Aldaron Lau Date: Tue, 12 Nov 2024 20:28:15 -0600 Subject: [PATCH] Keep V2 up to date with V1 patches (#123) Co-authored-by: David Knaack Co-authored-by: Pino Toscano --- .github/workflows/ci.yml | 48 ++++++++++++++++++++++++++++++---------- CHANGELOG.md | 14 ++++++++++++ Cargo.toml | 5 ++++- README.md | 10 +++++++++ TESTING.md | 24 ++++++++++++++------ recipe.toml | 2 +- src/api.rs | 30 ++++++++++++------------- src/os.rs | 8 +++---- src/os/target.rs | 4 ++-- src/os/unix.rs | 27 +++++++++++++++++++++- src/os/windows.rs | 32 ++++++++++++++------------- src/platform.rs | 2 ++ 12 files changed, 147 insertions(+), 59 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10f3ef2..490e047 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,10 +31,8 @@ jobs: - i686-pc-windows-gnu - i686-unknown-freebsd - i686-unknown-linux-gnu - - wasm32-wasi + - wasm32-wasip1 - x86_64-apple-darwin - - x86_64-unknown-redox - - x86_64-unknown-illumos steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -82,7 +80,7 @@ jobs: - run: cargo clippy --all-features --target=${{ matrix.cc }} -- -D warnings - run: cargo update - run: cargo clippy --no-default-features --target=${{ matrix.cc }} -- -D warnings - - run: RUSTFLAGS="--cfg target_os=\"daku\"" cargo clippy --target=${{ matrix.cc }} -- -D warnings + - run: RUSTFLAGS="--cfg daku" cargo clippy --target=${{ matrix.cc }} -- -D warnings test: runs-on: ${{ matrix.os }} strategy: @@ -111,13 +109,15 @@ jobs: toolchain: ${{ matrix.tc }} override: true - run: cargo update - - run: cargo test --all --all-features --target=x86_64-apple-darwin - cross-compile: + - run: cargo test --all --all-features + # TODO: can be merged back into "cross-compile" after MSRV bumped to 1.78 + # only difference is wasi -> wasip1 + cross-compile-msrv: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest] - tc: [1.65.0, stable, beta, nightly] + tc: [1.65.0] cc: - aarch64-linux-android - i686-pc-windows-gnu @@ -134,6 +134,28 @@ jobs: target: ${{ matrix.cc }} override: true - run: cargo build --all-features --target=${{ matrix.cc }} + cross-compile: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest] + tc: [stable, beta, nightly] + cc: + - aarch64-linux-android + - i686-pc-windows-gnu + - i686-unknown-freebsd + - i686-unknown-linux-gnu + - wasm32-wasip1 + - x86_64-apple-darwin + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.tc }} + target: ${{ matrix.cc }} + override: true + - run: cargo build --all-features --target=${{ matrix.cc }} cross-compile-ios: runs-on: ${{ matrix.os }} strategy: @@ -185,8 +207,8 @@ jobs: - run: cargo build --all-features --target=${{ matrix.cc }} - run: cargo update - run: cargo build --no-default-features --target=${{ matrix.cc }} - - run: RUSTFLAGS="--cfg target_os=\"daku\"" cargo build --target=${{ matrix.cc }} - cross-compile-illumos: + - run: RUSTFLAGS="--cfg daku" cargo build --target=${{ matrix.cc }} + cross-compile-illumos-and-checks: runs-on: ${{ matrix.os }} strategy: matrix: @@ -198,12 +220,13 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal + components: clippy toolchain: ${{ matrix.tc }} target: ${{ matrix.cc }} override: true - - run: cargo update - run: cargo build --all-features --target=${{ matrix.cc }} - cross-compile-redox: + - run: cargo clippy --all-features --target=${{ matrix.cc }} -- -D warnings + cross-compile-redox-and-checks: runs-on: ${{ matrix.os }} strategy: matrix: @@ -215,8 +238,9 @@ jobs: - uses: actions-rs/toolchain@v1 with: profile: minimal + components: clippy toolchain: ${{ matrix.tc }} target: ${{ matrix.cc }} override: true - - run: cargo update - run: cargo build --all-features --target=${{ matrix.cc }} + - run: cargo clippy --all-features --target=${{ matrix.cc }} -- -D warnings diff --git a/CHANGELOG.md b/CHANGELOG.md index 0325aa1..205c803 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. +## [1.5.2] - 2024-09-02 + +### Changed + + - Updated redox\_syscall to 0.5 + - Replace `target_os = daku` with `daku` cfg + - Improved repository documentation + +## [1.5.1] - 2024-03-09 + +### Fixed + + - Broken link in docs + ## [1.5.0] - 2024-03-03 ### Added diff --git a/Cargo.toml b/Cargo.toml index 6432e65..4d57f9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ version = "0.5" version = "0.1" # Target-specific dependency for web browser -[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi"), not(target_os = "daku")))'.dependencies.web-sys] +[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi"), not(daku)))'.dependencies.web-sys] version = "0.3" features = ["Navigator", "Document", "Window", "Location"] optional = true @@ -44,3 +44,6 @@ default = ["web"] # Enabling this feature indicates that the wasm32-unknown-unknown target should # be assumed to be in a web environment where it can call DOM APIs. web = ["web-sys"] + +[lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(daku)'] } diff --git a/README.md b/README.md index 76ee91f..dd4643d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Retrieve the current user and environment through simple functions. Check out the [documentation][0] for examples. ### Features + - Get the user's full name - Get the user's username - Get the user's preferred language(s) @@ -27,7 +28,9 @@ Check out the [documentation][0] for examples. - Get the devices's CPU architecture and its width ### Supported Platforms + WhoAmI targets all platforms that can run Rust, including: + - Linux - Windows - Mac OS @@ -42,9 +45,11 @@ WhoAmI targets all platforms that can run Rust, including: - Android **planned later** - iOS / watchOS / tvOS **planned later** - Fuchsia **planned later** + - GNU/Hurd **untested** - Others? (make a PR or open an issue) ## MSRV + WhoAmI 1.x.y targets Rust 1.40.0 stable and later, and the 1.x.y track will be maintained at least until the release of the Rust 2024 edition. @@ -55,15 +60,18 @@ releases to be supported and receive bugfixes at least until sometime in 2027, bumping MSRV only as needed. ## Binary + [whome](https://crates.io/crates/whome): `whoami` command RiR (Re-written in Rust) that depends on this crate. ## Testing + The testing procedure is documented at . The full manual test suite is run for each change that affects multiple platforms. ## License + Copyright © 2017-2024 The WhoAmI Contributors. Licensed under any of @@ -76,11 +84,13 @@ Licensed under any of at your option. ### Contribution + Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as described above, without any additional terms or conditions. ## Help + If you want help using or contributing to this library, feel free to send me an email at [aldaronlau@gmail.com][13]. diff --git a/TESTING.md b/TESTING.md index c1bb5ee..974a9a5 100644 --- a/TESTING.md +++ b/TESTING.md @@ -282,7 +282,7 @@ Device's CPU Arch whoami::arch(): Unknown: i86pc -Tested on Fedora Silverblue 39 +Tested on Fedora Silverblue 40 ### Update Rust Nightly and Stable @@ -294,7 +294,7 @@ rustup target add --toolchain stable x86_64-unknown-redox ### Install pre-requisites ```shell -sudo dnf install git file autoconf vim bison flex genisoimage gperf glibc-devel.i686 expat expat-devel fuse-devel fuse3-devel gmp-devel perl-HTML-Parser libpng-devel libtool libjpeg-turbo-devel libvorbis-devel SDL2_ttf-devel mesa-libOSMesa-devel m4 nasm po4a syslinux texinfo sdl12-compat-devel ninja-build meson python3-mako make gcc gcc-c++ openssl patch automake perl-Pod-Html perl-FindBin gperf curl gettext-devel perl-Pod-Xhtml pkgconf-pkg-config cmake cbindgen just qemu doxygen 'perl(ExtUtils::MakeMaker)' +sudo dnf install git file autoconf vim bison flex genisoimage gperf glibc-devel.i686 expat expat-devel fuse-devel fuse3-devel gmp-devel perl-HTML-Parser libpng-devel libtool libjpeg-turbo-devel libvorbis-devel SDL2_ttf-devel mesa-libOSMesa-devel m4 nasm po4a syslinux texinfo sdl12-compat-devel ninja-build meson python3-mako make gcc gcc-c++ openssl patch automake perl-Pod-Html perl-FindBin gperf curl gettext-devel perl-Pod-Xhtml pkgconf-pkg-config cmake cbindgen just mpfr-devel qemu doxygen 'perl(ExtUtils::MakeMaker)' cargo install --locked --force --version 0.1.1 cargo-config ``` @@ -305,27 +305,37 @@ cargo install --locked --force --version 0.1.1 cargo-config mkdir -p build/ cd build/ git clone https://gitlab.redox-os.org/redox-os/redox.git --origin upstream --recursive +cd redox +git submodule update --recursive --init ``` ### Create our demo recipe -Make sure whome is updated to the whoami testing branch. +Back in the root whoami directory, make sure whome is updated to the whoami +testing branch. ```shell mkdir -p build/redox/cookbook/recipes/demos/whome/ cp recipe.toml build/redox/cookbook/recipes/demos/whome/ -cp build/redox/config/desktop.toml config/x86_64/ardaku.toml +cp build/redox/config/desktop.toml build/redox/config/x86_64/ardaku.toml ``` -In `config/x86_64/ardaku.toml`, under `[packages]`: +In `build/redox/config/x86_64/ardaku.toml`, under `[packages]`, add: ```toml whome = {} ``` +### Select the config +IN `build/redox/mk/config.mk`, set: + +```make +CONFIG_NAME?=ardaku +``` + ### Build Redox -This takes a while +Back in `cd build/redox`, this takes a while ```shell make all @@ -358,7 +368,7 @@ realname: user username: user devicename: redox hostname: redox -distro: Redox OS 0.8.0 +distro: Redox OS 0.9.0 desktop_env: Orbital platform: Redox arch: Unknown: x86_64 diff --git a/recipe.toml b/recipe.toml index 4304376..63dcc9d 100644 --- a/recipe.toml +++ b/recipe.toml @@ -2,7 +2,7 @@ [source] git = "https://github.com/AldaronLau/whome.git" -rev = "0545b887c8419edddc79e5044e5d67f8ec9e0c30" +rev = "ddcadeae154bf2f907b43aed415a3feb209c7486" [build] template = "cargo" diff --git a/src/api.rs b/src/api.rs index d5995ce..eeed4d8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -72,9 +72,9 @@ pub fn realname_os() -> Result { /// Get the host device's hostname. /// -/// Limited to a-z, A-Z, 0-9, and dashes. This limit also applies to -/// [`devicename()`] when targeting Windows. Usually hostnames are -/// case-insensitive, but it's not a hard requirement. +/// Usually hostnames are case-insensitive, but it's not a hard requirement. +/// +/// FIXME: Document platform-specific character limitations #[inline(always)] pub fn hostname() -> Result { Target::hostname(Os) @@ -130,19 +130,19 @@ pub fn langs() -> Result> { let langs = langs .split(';') .map(ToString::to_string) - .collect::>(); + .filter_map(|lang| { + let lang = lang + .split_terminator('.') + .next() + .unwrap_or_default() + .replace(|x| ['_', '-'].contains(&x), "/"); - Ok(langs.into_iter().filter_map(|lang| { - let lang = lang - .split_terminator('.') - .next() - .unwrap_or_default() - .replace(|x| ['_', '-'].contains(&x), "/"); + if lang == "C" { + return None; + } - if lang == "C" { - return None; - } + Some(Language::__(Box::new(lang))) + }); - Some(Language::__(Box::new(lang))) - })) + Ok(langs.collect::>().into_iter()) } diff --git a/src/os.rs b/src/os.rs index ac43a54..af3695d 100644 --- a/src/os.rs +++ b/src/os.rs @@ -1,10 +1,7 @@ #![allow(unsafe_code)] // Daku -#[cfg_attr( - all(target_arch = "wasm32", target_os = "daku"), - path = "os/daku.rs" -)] +#[cfg_attr(all(target_arch = "wasm32", daku), path = "os/daku.rs")] // Redox #[cfg_attr( all(target_os = "redox", not(target_arch = "wasm32")), @@ -21,6 +18,7 @@ target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ), not(target_arch = "wasm32") ), @@ -36,7 +34,7 @@ all( target_arch = "wasm32", not(target_os = "wasi"), - not(target_os = "daku"), + not(daku), feature = "web", ), path = "os/web.rs" diff --git a/src/os/target.rs b/src/os/target.rs index 69b6ed4..7822257 100644 --- a/src/os/target.rs +++ b/src/os/target.rs @@ -50,7 +50,7 @@ impl Target for Os { #[inline(always)] fn platform(self) -> Platform { - if cfg!(target_os = "daku") { + if cfg!(daku) { Platform::Unknown("Daku".to_string()) } else if cfg!(target_os = "wasi") { Platform::Unknown("WASI".to_string()) @@ -92,7 +92,7 @@ impl Target for Os { } else if cfg!(target_os = "vita") { Platform::PlayStation } else if cfg!(target_os = "hurd") { - Platform::Unknown("GNU Hurd".to_string()) + Platform::Hurd } else if cfg!(target_os = "aix") { Platform::Unknown("AIX OS".to_string()) } else if cfg!(target_os = "espidf") { diff --git a/src/os/unix.rs b/src/os/unix.rs index c6b66b5..5755bdf 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -5,6 +5,7 @@ target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ))] use std::env; use std::{ @@ -32,7 +33,7 @@ use crate::{ Arch, DesktopEnv, Platform, Result, }; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "hurd",))] #[repr(C)] struct PassWd { pw_name: *const c_void, @@ -97,6 +98,7 @@ extern "system" { target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", + target_os = "hurd", ))] extern "system" { fn getpwuid_r( @@ -114,6 +116,8 @@ extern "system" { } #[cfg(target_os = "macos")] +// FIXME: seemingly false positive for link lint +#[allow(clippy::duplicated_attributes)] #[link(name = "CoreFoundation", kind = "framework")] #[link(name = "SystemConfiguration", kind = "framework")] extern "system" { @@ -246,6 +250,7 @@ fn getpwuid(name: Name) -> Result { target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", + target_os = "hurd", ))] { let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit(); @@ -395,6 +400,16 @@ struct UtsName { domainname: [c_char; 65], } +#[cfg(target_os = "hurd")] +#[repr(C)] +struct UtsName { + sysname: [c_char; 1024], + nodename: [c_char; 1024], + release: [c_char; 1024], + version: [c_char; 1024], + machine: [c_char; 1024], +} + // Buffer initialization impl Default for UtsName { fn default() -> Self { @@ -412,6 +427,7 @@ unsafe fn uname(buf: *mut UtsName) -> c_int { target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ))] fn uname(buf: *mut UtsName) -> c_int; @@ -478,6 +494,7 @@ impl Target for Os { target_os = "freebsd", target_os = "netbsd", target_os = "openbsd", + target_os = "hurd", ))] { let machine_info = fs::read("/etc/machine-info")?; @@ -537,6 +554,7 @@ impl Target for Os { target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ))] { let program = fs::read("/etc/os-release")?; @@ -583,6 +601,7 @@ impl Target for Os { target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ))] let env = env::var_os("DESKTOP_SESSION"); #[cfg(any( @@ -592,6 +611,7 @@ impl Target for Os { target_os = "netbsd", target_os = "openbsd", target_os = "illumos", + target_os = "hurd", ))] let env = if let Some(ref env) = env { env.to_string_lossy() @@ -645,6 +665,11 @@ impl Target for Os { { Platform::Illumos } + + #[cfg(target_os = "hurd")] + { + Platform::Hurd + } } #[inline(always)] diff --git a/src/os/windows.rs b/src/os/windows.rs index 131b49c..1412a4b 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -1,7 +1,7 @@ use std::{ ffi::OsString, io::{Error, ErrorKind}, - mem::MaybeUninit, + mem::{self, MaybeUninit}, os::{ raw::{c_char, c_int, c_uchar, c_ulong, c_ushort, c_void}, windows::ffi::OsStringExt, @@ -121,8 +121,8 @@ fn username() -> Result { // Step 2. Allocate memory to put the Windows (UTF-16) string. let mut name: Vec = - Vec::with_capacity(size.try_into().unwrap_or(std::usize::MAX)); - size = name.capacity().try_into().unwrap_or(std::u32::MAX); + Vec::with_capacity(size.try_into().unwrap_or(usize::MAX)); + size = name.capacity().try_into().unwrap_or(u32::MAX); let orig_size = size; let fail = unsafe { GetUserNameW(name.as_mut_ptr().cast(), &mut size) == 0 }; @@ -131,7 +131,7 @@ fn username() -> Result { } debug_assert_eq!(orig_size, size); unsafe { - name.set_len(size.try_into().unwrap_or(std::usize::MAX)); + name.set_len(size.try_into().unwrap_or(usize::MAX)); } let terminator = name.pop(); // Remove Trailing Null debug_assert_eq!(terminator, Some(0u16)); @@ -160,8 +160,8 @@ fn extended_name(format: ExtendedNameFormat) -> Result { // Step 2. Allocate memory to put the Windows (UTF-16) string. let mut name: Vec = - Vec::with_capacity(buf_size.try_into().unwrap_or(std::usize::MAX)); - let mut name_len = name.capacity().try_into().unwrap_or(std::u32::MAX); + Vec::with_capacity(buf_size.try_into().unwrap_or(usize::MAX)); + let mut name_len = name.capacity().try_into().unwrap_or(u32::MAX); let fail = unsafe { GetUserNameExW(format, name.as_mut_ptr().cast(), &mut name_len) == 0 }; @@ -171,7 +171,7 @@ fn extended_name(format: ExtendedNameFormat) -> Result { assert_eq!(buf_size, name_len + 1); - unsafe { name.set_len(name_len.try_into().unwrap_or(std::usize::MAX)) }; + unsafe { name.set_len(name_len.try_into().unwrap_or(usize::MAX)) }; // Step 3. Convert to Rust String Ok(OsString::from_wide(&name)) @@ -249,8 +249,8 @@ impl Target for Os { // Step 2. Allocate memory to put the Windows (UTF-16) string. let mut name: Vec = - Vec::with_capacity(size.try_into().unwrap_or(std::usize::MAX)); - let mut size = name.capacity().try_into().unwrap_or(std::u32::MAX); + Vec::with_capacity(size.try_into().unwrap_or(usize::MAX)); + let mut size = name.capacity().try_into().unwrap_or(u32::MAX); if unsafe { GetComputerNameExW( @@ -263,7 +263,7 @@ impl Target for Os { } unsafe { - name.set_len(size.try_into().unwrap_or(std::usize::MAX)); + name.set_len(size.try_into().unwrap_or(usize::MAX)); } // Step 3. Convert to Rust String @@ -290,8 +290,8 @@ impl Target for Os { // Step 2. Allocate memory to put the Windows (UTF-16) string. let mut name: Vec = - Vec::with_capacity(size.try_into().unwrap_or(std::usize::MAX)); - let mut size = name.capacity().try_into().unwrap_or(std::u32::MAX); + Vec::with_capacity(size.try_into().unwrap_or(usize::MAX)); + let mut size = name.capacity().try_into().unwrap_or(u32::MAX); if unsafe { GetComputerNameExW( @@ -304,7 +304,7 @@ impl Target for Os { } unsafe { - name.set_len(size.try_into().unwrap_or(std::usize::MAX)); + name.set_len(size.try_into().unwrap_or(usize::MAX)); } // Step 3. Convert to Rust String @@ -350,13 +350,15 @@ impl Target for Os { let get_version: unsafe extern "system" fn( a: *mut OsVersionInfoEx, - ) -> u32 = unsafe { std::mem::transmute(func) }; + ) -> u32 = unsafe { mem::transmute(func) }; let mut version = MaybeUninit::::zeroed(); + // FIXME `mem::size_of` seemingly false positive for lint + #[allow(unused_qualifications)] let version = unsafe { (*version.as_mut_ptr()).os_version_info_size = - std::mem::size_of::() as u32; + mem::size_of::() as u32; get_version(version.as_mut_ptr()); if FreeLibrary(inst) == 0 { diff --git a/src/platform.rs b/src/platform.rs index f38aeb8..22eabe3 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -17,6 +17,7 @@ pub enum Platform { PlayStation, Fuchsia, Redox, + Hurd, } impl Display for Platform { @@ -38,6 +39,7 @@ impl Display for Platform { Self::PlayStation => "PlayStation", Self::Fuchsia => "Fuchsia", Self::Redox => "Redox", + Self::Hurd => "GNU Hurd", }) } }