From e96d1fde3433f7c5ce64bdd91a1a2d0a3af35ad2 Mon Sep 17 00:00:00 2001 From: svartalf Date: Wed, 20 Sep 2023 10:37:36 +0200 Subject: [PATCH] Updating the repo to make it usable again * CI is fixed, deprecated actions-rs were removed * More CI targets added * MSRV is set to 1.67.0 * New cargo and clippy lints fixed --- .github/workflows/ci.yml | 312 +++++++++++++++++++++++++++++---------- .rustfmt.toml | 9 -- Cargo.toml | 5 +- README.md | 6 +- src/lib.rs | 4 +- src/nix.rs | 48 +++--- src/windows.rs | 38 ++--- 7 files changed, 279 insertions(+), 143 deletions(-) delete mode 100644 .rustfmt.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7748c06..be62411 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,132 +12,284 @@ jobs: - macOS-latest - windows-latest steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v4 - name: Install Rust toolchain - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal toolchain: nightly - override: true components: rustfmt, clippy - name: Run rustfmt - uses: actions-rs/cargo@v1 - # No need to execute it multiple times if: matrix.os == 'ubuntu-latest' - with: - command: fmt - args: --all -- --check + run: | + cargo fmt --all -- --check - name: Run clippy - uses: actions-rs/cargo@v1 - with: - command: clippy - args: --all-targets --all-features -- -D warnings -A clippy::needless_doctest_main + run: | + cargo clippy --all-targets --all-features -- -D warnings tier1: + # Matches the Rust Tier 1 platform support + # See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-1-with-host-tools name: Tier 1 needs: lints runs-on: ${{ matrix.os }} strategy: matrix: - os: - - ubuntu-latest - - macOS-latest - - windows-latest - toolchain: - - 1.19.0 - - stable + include: + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + use_cross: true + - os: ubuntu-latest + target: i686-unknown-linux-gnu + use_cross: true + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu +# TODO: Enable it back +# - os: windows-latest +# target: i686-pc-windows-gnu + - os: windows-latest + target: i686-pc-windows-msvc + use_cross: true + - os: windows-latest + target: x86_64-pc-windows-gnu + - os: windows-latest + target: x86_64-pc-windows-msvc + - os: macos-latest + target: x86_64-apple-darwin steps: - uses: actions/checkout@v1 - name: Install Rust toolchain - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master + with: + toolchain: 1.67.0 + targets: ${{ matrix.target }} + - uses: taiki-e/install-action@v2 with: - profile: minimal - toolchain: ${{ matrix.toolchain }} - override: true + tool: cross - name: Check without features - uses: actions-rs/cargo@v1 - with: - command: check + run: cargo check --target=${{ matrix.target }} - name: Check with all features enabled - uses: actions-rs/cargo@v1 - with: - command: check - args: --all-features + run: ${{ env.CARGO_BINARY }} check --all-features --target=${{ matrix.target }} + env: + CARGO_BINARY: ${{ matrix.use_cross == true && 'cross' || 'cargo' }} - name: Run tests - if: matrix.toolchain == 'stable' - uses: actions-rs/cargo@v1 + run: ${{ env.CARGO_BINARY }} test --all-features --target=${{ matrix.target }} + env: + CARGO_BINARY: ${{ matrix.use_cross == true && 'cross' || 'cargo' }} + + tier2_with_host_tools: + # Matches Rust "Tier 2 with Host Tools" platform support + # For as much as possible + # + # See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-2-with-host-tools + name: Tier 2 (with Host Tools) + needs: tier1 + runs-on: ${{ matrix.os }} + strategy: + matrix: + include: + - os: macos-latest + target: aarch64-apple-darwin + - os: windows-latest + target: aarch64-pc-windows-msvc + - os: ubuntu-latest + target: aarch64-unknown-linux-musl + - os: ubuntu-latest + target: arm-unknown-linux-gnueabi + - os: ubuntu-latest + target: arm-unknown-linux-gnueabihf + - os: ubuntu-latest + target: armv7-unknown-linux-gnueabihf + # `rust-std` is not available + # - os: ubuntu-latest + # target: loongarch64-unknown-linux-gnu + - os: ubuntu-latest + target: mips-unknown-linux-gnu + - os: ubuntu-latest + target: mips64-unknown-linux-gnuabi64 + - os: ubuntu-latest + target: mips64el-unknown-linux-gnuabi64 + - os: ubuntu-latest + target: mipsel-unknown-linux-gnu + - os: ubuntu-latest + target: powerpc-unknown-linux-gnu + - os: ubuntu-latest + target: powerpc64-unknown-linux-gnu + - os: ubuntu-latest + target: powerpc64le-unknown-linux-gnu + - os: ubuntu-latest + target: riscv64gc-unknown-linux-gnu + - os: ubuntu-latest + target: s390x-unknown-linux-gnu + - os: ubuntu-latest + target: x86_64-unknown-linux-musl + steps: + - uses: actions/checkout@v1 + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@master with: - command: test - args: --all-features + toolchain: 1.67.0 + targets: ${{ matrix.target }} + - uses: taiki-e/install-action@v2 + with: + tool: cross + - name: Run build + run: | + cross build --all-features --target=${{ matrix.target }} - tier2: - name: Tier 2 + tier2_with_host_tools_via_cross: + # Matches Rust "Tier 2 with Host Tools" platform support + # For as much as possible, but where `cross` is needed for cross-compilation + # + # See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-2-with-host-tools + name: Tier 2 (with Host Tools, via Cross) needs: tier1 runs-on: ubuntu-latest strategy: matrix: target: - # Solaris - - x86_64-sun-solaris - - # DragonflyBSD - # TODO: No std available - # - x86_64-unknown-dragonfly - - # NetBSD + - x86_64-unknown-freebsd + - x86_64-unknown-illumos - x86_64-unknown-netbsd - - # Android - - arm-linux-androideabi - - armv7-linux-androideabi - - aarch64-linux-android - - i686-linux-android steps: - uses: actions/checkout@v1 - name: Install Rust toolchain - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal - toolchain: stable - target: ${{ matrix.target }} - override: true - - - name: Run build - uses: actions-rs/cargo@v1 + toolchain: 1.67.0 + targets: ${{ matrix.target }} + - uses: taiki-e/install-action@v2 with: - use-cross: true - command: build - args: --all-features + tool: cross + - name: Run build + run: | + cross build --all-features --target=${{ matrix.target }} - tier2_ios: - name: Tier 2 (iOS) + tier2_without_host_tools: + # Matches Rust "Tier 2 with host tools" platform support + # For as much as possible, but where `cross` is needed for cross-compilation + # + # See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-2-with-host-tools + name: Tier 2 (without Host Tools) needs: tier1 - runs-on: macOS-latest + runs-on: ${{ matrix.os }} strategy: matrix: - target: - - armv7-apple-ios - - armv7s-apple-ios - - x86_64-apple-ios - - aarch64-apple-ios + include: + - os: macos-latest + target: aarch64-apple-ios + # `rust-std` is not available + # - os: ubuntu-latest + # target: aarch64-unknown-fuchsia + - os: ubuntu-latest + target: aarch64-linux-android + - os: ubuntu-latest + target: arm-linux-androideabi + - os: ubuntu-latest + target: arm-unknown-linux-musleabi + - os: ubuntu-latest + target: arm-unknown-linux-musleabihf + - os: ubuntu-latest + target: armv5te-unknown-linux-gnueabi + - os: ubuntu-latest + target: armv5te-unknown-linux-musleabi + - os: ubuntu-latest + target: armv7-linux-androideabi + - os: ubuntu-latest + target: armv7-unknown-linux-gnueabi + - os: ubuntu-latest + target: armv7-unknown-linux-musleabi + - os: ubuntu-latest + target: armv7-unknown-linux-musleabihf + - os: ubuntu-latest + target: i586-unknown-linux-gnu + - os: ubuntu-latest + target: i586-unknown-linux-musl + - os: ubuntu-latest + target: i686-linux-android + - os: ubuntu-latest + target: i686-unknown-freebsd + - os: ubuntu-latest + target: i686-unknown-linux-musl + - os: ubuntu-latest + target: mips-unknown-linux-musl + - os: ubuntu-latest + target: mips64-unknown-linux-muslabi64 + - os: ubuntu-latest + target: mips64el-unknown-linux-muslabi64 + - os: ubuntu-latest + target: mipsel-unknown-linux-musl + - os: ubuntu-latest + target: sparc64-unknown-linux-gnu + - os: ubuntu-latest + target: sparcv9-sun-solaris + - os: ubuntu-latest + target: thumbv7neon-linux-androideabi + - os: ubuntu-latest + target: thumbv7neon-unknown-linux-gnueabihf + - os: macos-latest + target: x86_64-apple-ios + # `gethostname` function is not available + # - os: ubuntu-latest + # target: x86_64-fortanix-unknown-sgx + # + # `rust-std` seems to be not available? + # - os: ubuntu-latest + # target: x86_64-unknown-fuchsia + - os: ubuntu-latest + target: x86_64-linux-android + - os: ubuntu-latest + target: x86_64-pc-solaris + - os: ubuntu-latest + target: x86_64-unknown-linux-gnux32 steps: - uses: actions/checkout@v1 - name: Install Rust toolchain - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@master with: - profile: minimal - toolchain: stable - target: ${{ matrix.target }} - override: true - + toolchain: 1.67.0 + targets: ${{ matrix.target }} + - uses: taiki-e/install-action@v2 + with: + tool: cross - name: Run build - uses: actions-rs/cargo@v1 + run: | + cross build --all-features --target=${{ matrix.target }} + + tier2_without_host_tools_default_features: + # Matches Rust "Tier 2 with host tools" platform support + # For as much as possible, but where `cross` is needed for cross-compilation + # + # The only difference from the `tier2_without_host_tools` group is that + # no optional crate features are enabled (e.g. Redox has `gethostname(2)`, but not the `sethostname(2)`) + # + # See https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-2-with-host-tools + name: Tier 2 (without Host Tools) + needs: tier1 + runs-on: ${{ matrix.os }} + strategy: + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-redox + steps: + - uses: actions/checkout@v1 + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@master with: - use-cross: true - command: build - args: --all-features + toolchain: 1.67.0 + targets: ${{ matrix.target }} + - uses: taiki-e/install-action@v2 + with: + tool: cross + - name: Run build (default features) + run: | + cross build --target=${{ matrix.target }} + +# TODO: Tier 3 +# - x86_64-unknown-dragonfly +# And other targets that have `std` compiled diff --git a/.rustfmt.toml b/.rustfmt.toml deleted file mode 100644 index f944c97..0000000 --- a/.rustfmt.toml +++ /dev/null @@ -1,9 +0,0 @@ -unstable_features = true -edition = "2018" -version = "Two" -wrap_comments = true -comment_width = 80 -max_width = 80 -merge_imports = false -newline_style = "Unix" -struct_lit_single_line = false diff --git a/Cargo.toml b/Cargo.toml index e138a5d..6ed3653 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hostname" -version = "0.3.1" +version = "0.4.0" description = "Cross-platform system's host name functions" categories = ["api-bindings", "os"] keywords = ["hostname", "gethostname", "sethostname"] @@ -12,6 +12,7 @@ repository = "https://github.com/svartalf/hostname" readme = "README.md" license = "MIT" edition = "2021" +rust-version = "1.67" [features] default = [] @@ -25,7 +26,7 @@ cfg-if = "^1.0" libc = "^0.2" [target.'cfg(target_os = "windows")'.dependencies] -windows = { version="^0.43", features=["Win32_Foundation", "Win32_System_SystemInformation"] } +windows = { version = "^0.52", features = ["Win32_Foundation", "Win32_System_SystemInformation"] } [dev-dependencies] version-sync = "0.9" diff --git a/README.md b/README.md index 9305890..41501c8 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Latest Version](https://img.shields.io/crates/v/hostname.svg)](https://crates.io/crates/hostname) [![Latest Version](https://docs.rs/hostname/badge.svg)](https://docs.rs/hostname) [![Build Status](https://github.com/svartalf/hostname/workflows/Continuous%20integration/badge.svg)](https://github.com/svartalf/hostname/actions) -![Minimum rustc version](https://img.shields.io/badge/rustc-1.19+-green.svg) +![Minimum rustc version](https://img.shields.io/badge/rustc-1.67+-green.svg) ![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg) ![Crates.io](https://img.shields.io/crates/d/hostname) @@ -17,7 +17,7 @@ ## Rust version requirements -Since version `0.2.0` this crate requires Rust version `1.19.0` or greater. +Since version `0.4.0` this crate requires Rust version `1.67.0` or greater. This version is explicitly tested in CI and may be bumped in any major or minor release as needed.\ @@ -31,7 +31,7 @@ Add the following dependency to your Cargo manifest: ```toml [dependencies] -hostname = "^0.3" +hostname = "^0.4" ``` Crate API provides two simple functions for retrieving and setting the system's host name: diff --git a/src/lib.rs b/src/lib.rs index d92cbd7..c22faad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ println!("{:?}", name); ``` "# )] -#![doc(html_root_url = "https://docs.rs/hostname/0.3.1")] +#![doc(html_root_url = "https://docs.rs/hostname/0.4.0")] #![deny( unused, unused_imports, @@ -54,8 +54,6 @@ println!("{:?}", name); unused_import_braces, unused_results )] -#![allow(unknown_lints, unused_extern_crates)] - use cfg_if::cfg_if; diff --git a/src/nix.rs b/src/nix.rs index d585590..bfb212a 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -1,13 +1,11 @@ -use std::io; #[cfg(feature = "set")] use std::ffi::OsStr; use std::ffi::OsString; +use std::io; #[cfg(feature = "set")] use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStringExt; -use libc; - const _POSIX_HOST_NAME_MAX: libc::c_long = 255; pub fn get() -> io::Result { @@ -21,9 +19,8 @@ pub fn get() -> io::Result { // Reserve additional space for terminating nul byte. let mut buffer = vec![0u8; size + 1]; - let result = unsafe { - libc::gethostname(buffer.as_mut_ptr() as *mut libc::c_char, size) - }; + #[allow(trivial_casts)] + let result = unsafe { libc::gethostname(buffer.as_mut_ptr() as *mut libc::c_char, size) }; if result != 0 { return Err(io::Error::last_os_error()); @@ -39,7 +36,7 @@ fn wrap_buffer(mut bytes: Vec) -> OsString { let end = bytes .iter() .position(|&byte| byte == 0x00) - .unwrap_or_else(|| bytes.len()); + .unwrap_or(bytes.len()); bytes.resize(end, 0x00); OsString::from_vec(bytes) @@ -47,32 +44,39 @@ fn wrap_buffer(mut bytes: Vec) -> OsString { #[cfg(feature = "set")] pub fn set(hostname: &OsStr) -> io::Result<()> { - #[cfg(not(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos")))] + #[cfg(not(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "solaris", + target_os = "illumos" + )))] #[allow(non_camel_case_types)] type hostname_len_t = libc::size_t; - #[cfg(any(target_os = "dragonfly", - target_os = "freebsd", - target_os = "ios", - target_os = "macos"))] + #[cfg(any( + target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "solaris", + target_os = "illumos" + ))] #[allow(non_camel_case_types)] type hostname_len_t = libc::c_int; - if hostname.len() > hostname_len_t::MAX { + #[allow(clippy::unnecessary_cast)] + // Cast is needed for the `libc::c_int` type + if hostname.len() > hostname_len_t::MAX as usize { return Err(io::Error::new(io::ErrorKind::Other, "hostname too long")); } let size = hostname.len() as hostname_len_t; - let result = unsafe { - libc::sethostname( - hostname.as_bytes().as_ptr() as *const libc::c_char, - size, - ) - }; + #[allow(trivial_casts)] + let result = + unsafe { libc::sethostname(hostname.as_bytes().as_ptr() as *const libc::c_char, size) }; if result != 0 { Err(io::Error::last_os_error()) diff --git a/src/windows.rs b/src/windows.rs index b30641d..ab69829 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,17 +1,13 @@ -use std::io; #[cfg(feature = "set")] use std::ffi::OsStr; use std::ffi::OsString; +use std::io; #[cfg(feature = "set")] use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStringExt; - - use windows::Win32::System::SystemInformation::ComputerNamePhysicalDnsHostname; - - pub fn get() -> io::Result { use windows::core::PWSTR; use windows::Win32::System::SystemInformation::GetComputerNameExW; @@ -21,12 +17,8 @@ pub fn get() -> io::Result { // Don't care much about the result here, // it is guaranteed to return an error, // since we passed the NULL pointer as a buffer - let result = GetComputerNameExW( - ComputerNamePhysicalDnsHostname, - PWSTR::null(), - &mut size, - ); - debug_assert_eq!(result.0, 0); + let result = GetComputerNameExW(ComputerNamePhysicalDnsHostname, PWSTR::null(), &mut size); + debug_assert!(result.is_err()); }; let mut buffer = Vec::with_capacity(size as usize); @@ -39,14 +31,15 @@ pub fn get() -> io::Result { ) }; - if !result.as_bool() { - Err(io::Error::last_os_error()) - } else { - unsafe { - buffer.set_len(size as usize); - } + match result { + Ok(_) => { + unsafe { + buffer.set_len(size as usize); + } - Ok(OsString::from_wide(&buffer)) + Ok(OsString::from_wide(&buffer)) + } + Err(e) => Err(io::Error::from_raw_os_error(e.code().0)), } } @@ -55,7 +48,8 @@ pub fn set(hostname: &OsStr) -> io::Result<()> { use windows::core::PCWSTR; use windows::Win32::System::SystemInformation::SetComputerNameExW; - let buffer = hostname.encode_wide().collect::>(); + let mut buffer = hostname.encode_wide().collect::>(); + buffer.push(0x00); // Appending the null terminator let result = unsafe { SetComputerNameExW( @@ -64,9 +58,5 @@ pub fn set(hostname: &OsStr) -> io::Result<()> { ) }; - if !result.as_bool() { - Err(io::Error::last_os_error()) - } else { - Ok(()) - } + result.map_err(|e| io::Error::from_raw_os_error(e.code().0)) }