Skip to content

Commit

Permalink
Merge branch 'GuillaumeGomez:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sigsegved authored Jan 29, 2025
2 parents 4f8e4e9 + 64f2157 commit 78b9e3d
Show file tree
Hide file tree
Showing 33 changed files with 932 additions and 714 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 0.33.1

* Linux: Fix components retrieval.
* Linux: Filter out more virtual file systems for `Disk` API.
* Linux/Android: Improve `System::long_os_version()`.
* Apple: Add missing CPU brands for iPhone and iPads.
* macOS: Improve system name retrieval in `System::long_os_version()`.
* Linux/macOS: Avoid trailing whitespace and consecutive whitespace in `System::long_os_version()`.
* Windows: Fix `User::groups`.
* Improve documentation for `System::name`, `System::kernel_version`, `System::os_version`, `System::long_os_version` and `System::distribution_id`.

# 0.33.0

* Linux: Add more ARM vendor IDs.
Expand Down
25 changes: 22 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sysinfo"
version = "0.33.0"
version = "0.33.1"
authors = ["Guillaume Gomez <[email protected]>"]
description = "Library to get system information such as processes, CPUs, disks, components and networks"
repository = "https://github.com/GuillaumeGomez/sysinfo"
Expand All @@ -23,6 +23,11 @@ component = [
"windows/Win32_System_Rpc",
"windows/Win32_System_Variant",
"windows/Win32_System_Wmi",
"objc2-core-foundation/CFArray",
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFDictionary",
"objc2-core-foundation/CFNumber",
"objc2-core-foundation/CFString",
]
disk = [
"windows/Win32_Foundation",
Expand All @@ -32,9 +37,17 @@ disk = [
"windows/Win32_System_Ioctl",
"windows/Win32_System_SystemServices",
"windows/Win32_System_WindowsProgramming",
"objc2-core-foundation/CFArray",
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFDictionary",
"objc2-core-foundation/CFError",
"objc2-core-foundation/CFNumber",
"objc2-core-foundation/CFString",
"objc2-core-foundation/CFURL",
]
system = [
"windows/Win32_Foundation",
"windows/Win32_System_Diagnostics_ToolHelp",
"windows/Wdk_System_SystemInformation",
"windows/Wdk_System_SystemServices",
"windows/Wdk_System_Threading",
Expand All @@ -53,6 +66,10 @@ system = [
"windows/Win32_UI_Shell",
"dep:ntapi",
"dep:memchr",
"objc2-core-foundation/CFBase",
"objc2-core-foundation/CFData",
"objc2-core-foundation/CFDictionary",
"objc2-core-foundation/CFString",
]
network = [
"windows/Win32_Foundation",
Expand Down Expand Up @@ -94,7 +111,7 @@ rustdoc-args = ["--generate-link-to-definition"]
[dependencies]
memchr = { version = "2.5", optional = true }
rayon = { version = "^1.8", optional = true }
serde = { version = "^1.0.190", optional = true }
serde = { version = "^1.0.190", optional = true, features = ["derive"] }

[target.'cfg(windows)'.dependencies]
ntapi = { version = "0.4", optional = true }
Expand All @@ -106,7 +123,9 @@ windows = { version = ">=0.54, <=0.57", optional = true }
libc = "^0.2.164"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8.7"
objc2-core-foundation = { version = "0.3.0", optional = true, default-features = false, features = [
"std",
] }

[target.'cfg(all(target_os = "linux", not(target_os = "android")))'.dev-dependencies]
tempfile = "3.9"
Expand Down
3 changes: 2 additions & 1 deletion src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ impl std::ops::DerefMut for Disks {
/// println!("{:?}: {:?}", disk.name(), disk.kind());
/// }
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub enum DiskKind {
/// HDD type.
HDD,
Expand Down
146 changes: 141 additions & 5 deletions src/common/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use std::collections::HashMap;
use std::fmt;
use std::net::IpAddr;
use std::net::{AddrParseError, IpAddr};
use std::num::ParseIntError;
use std::str::FromStr;

use crate::{NetworkDataInner, NetworksInner};

Expand Down Expand Up @@ -409,7 +411,8 @@ impl NetworkData {
/// MAC address for network interface.
///
/// It is returned by [`NetworkData::mac_address`][crate::NetworkData::mac_address].
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct MacAddr(pub [u8; 6]);

impl MacAddr {
Expand All @@ -433,14 +436,59 @@ impl fmt::Display for MacAddr {
}
}

/// Ip networks address for network interface.
/// Error type returned from `MacAddr::from_str` implementation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MacAddrFromStrError {
/// A number is not in hexadecimal format.
IntError(ParseIntError),
/// Input is not of format `{02X}:{02X}:{02X}:{02X}:{02X}:{02X}`.
InvalidAddrFormat,
}

impl FromStr for MacAddr {
type Err = MacAddrFromStrError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s
.split(':')
.map(|s| u8::from_str_radix(s, 16).map_err(MacAddrFromStrError::IntError));

let Some(data0) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};
let Some(data1) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};
let Some(data2) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};
let Some(data3) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};
let Some(data4) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};
let Some(data5) = parts.next() else {
return Err(MacAddrFromStrError::InvalidAddrFormat);
};

if parts.next().is_some() {
return Err(MacAddrFromStrError::InvalidAddrFormat);
}

Ok(MacAddr([data0?, data1?, data2?, data3?, data4?, data5?]))
}
}

/// IP networks address for network interface.
///
/// It is returned by [`NetworkData::ip_networks`][crate::NetworkData::ip_networks].
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct IpNetwork {
/// The ip of the network interface
/// The IP of the network interface.
pub addr: IpAddr,
/// The netmask, prefix of the ipaddress
/// The netmask, prefix of the IP address.
pub prefix: u8,
}

Expand All @@ -450,10 +498,46 @@ impl fmt::Display for IpNetwork {
}
}

/// Error type returned from `MacAddr::from_str` implementation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IpNetworkFromStrError {
/// Prefix is not an integer.
PrefixError(ParseIntError),
/// Failed to parse IP address.
AddrParseError(AddrParseError),
/// Input is not of format `[IP address]/[number]`.
InvalidAddrFormat,
}

impl FromStr for IpNetwork {
type Err = IpNetworkFromStrError;

#[allow(clippy::from_str_radix_10)]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.split('/');

let Some(addr) = parts.next() else {
return Err(IpNetworkFromStrError::InvalidAddrFormat);
};
let Some(prefix) = parts.next() else {
return Err(IpNetworkFromStrError::InvalidAddrFormat);
};
if parts.next().is_some() {
return Err(IpNetworkFromStrError::InvalidAddrFormat);
}

Ok(IpNetwork {
addr: IpAddr::from_str(addr).map_err(IpNetworkFromStrError::AddrParseError)?,
prefix: u8::from_str_radix(prefix, 10).map_err(IpNetworkFromStrError::PrefixError)?,
})
}
}

#[cfg(test)]
mod tests {
use crate::*;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;

// Ensure that the `Display` and `Debug` traits are implemented on the `MacAddr` struct
#[test]
Expand All @@ -476,6 +560,24 @@ mod tests {
assert!(!MacAddr([1, 2, 3, 4, 5, 6]).is_unspecified());
}

#[test]
fn check_mac_address_conversions() {
let mac = MacAddr([0xa, 0xb, 0xc, 0xd, 0xe, 0xf]);

let mac_s = mac.to_string();
assert_eq!("0a:0b:0c:0d:0e:0f", mac_s);
assert_eq!(Ok(mac), MacAddr::from_str(&mac_s));

assert_eq!(
MacAddr::from_str("0a:0b:0c:0d:0e:0f:01"),
Err(MacAddrFromStrError::InvalidAddrFormat)
);
assert_eq!(
MacAddr::from_str("0a:0b:0c:0d:0e"),
Err(MacAddrFromStrError::InvalidAddrFormat)
);
}

// Ensure that the `Display` and `Debug` traits are implemented on the `IpNetwork` struct
#[test]
fn check_display_impl_ip_network_ipv4() {
Expand Down Expand Up @@ -518,4 +620,38 @@ mod tests {
}
panic!("Networks should have at least one IP network ");
}

#[test]
fn check_ip_network_conversions() {
let addr = IpNetwork {
addr: IpAddr::from(Ipv6Addr::new(0xff, 0xa, 0x8, 0x12, 0x7, 0xc, 0xa, 0xb)),
prefix: 12,
};

let addr_s = addr.to_string();
assert_eq!("ff:a:8:12:7:c:a:b/12", addr_s);
assert_eq!(Ok(addr), IpNetwork::from_str(&addr_s));

let addr = IpNetwork {
addr: IpAddr::from(Ipv4Addr::new(255, 255, 255, 0)),
prefix: 21,
};

let addr_s = addr.to_string();
assert_eq!("255.255.255.0/21", addr_s);
assert_eq!(Ok(addr), IpNetwork::from_str(&addr_s));

assert_eq!(
IpNetwork::from_str("ff:a:8:12:7:c:a:b"),
Err(IpNetworkFromStrError::InvalidAddrFormat)
);
assert_eq!(
IpNetwork::from_str("ff:a:8:12:7:c:a:b/12/12"),
Err(IpNetworkFromStrError::InvalidAddrFormat)
);
match IpNetwork::from_str("0a:0b:0c:0d:0e/12") {
Err(IpNetworkFromStrError::AddrParseError(_)) => {}
x => panic!("expected `IpNetworkFromStrError::AddrParseError`, found {x:?}"),
}
}
}
Loading

0 comments on commit 78b9e3d

Please sign in to comment.