Skip to content

Commit

Permalink
Remove unnecessary or broken code
Browse files Browse the repository at this point in the history
  • Loading branch information
sisungo committed Feb 23, 2024
1 parent 296c70e commit 8d48849
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 184 deletions.
31 changes: 0 additions & 31 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2981,37 +2981,6 @@ impl Groups {
pub fn refresh_list(&mut self) {
crate::sys::get_groups(&mut self.groups);
}

/// Returns the [`Group`] matching the given `group_id`.
///
/// **Important**: The group list must be filled before using this method, otherwise it will
/// always return `None` (through the `refresh_*` methods).
///
/// It is a shorthand for:
///
/// ```ignore
/// # use sysinfo::Groups;
/// let groups = Groups::new_with_refreshed_list();
/// groups.list().find(|user| user.id() == user_id);
/// ```
///
/// Full example:
///
/// ```no_run
/// use sysinfo::{Pid, System, Groups};
///
/// let mut s = System::new_all();
/// let groups = Groups::new_with_refreshed_list();
///
/// if let Some(process) = s.process(Pid::from(1337)) {
/// if let Some(group_id) = process.group_id() {
/// println!("User for process 1337: {:?}", groups.get_group_by_id(&group_id));
/// }
/// }
/// ```
pub fn get_group_by_id(&self, group_id: &Gid) -> Option<&Group> {
self.groups.iter().find(|group| group.id() == group_id)
}
}

/// An enum representing signals on UNIX-like systems.
Expand Down
124 changes: 0 additions & 124 deletions src/unix/apple/groups.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/unix/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub mod component;
pub mod cpu;
pub mod disk;
mod ffi;
pub mod groups;
pub mod network;
pub mod process;
pub mod system;
Expand Down
57 changes: 29 additions & 28 deletions src/unix/groups.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::common::Gid;

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
use crate::Group;
use crate::{Gid, Group};
use libc::{endgrent, getgrent, setgrent};

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check stable / aarch64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check nightly / aarch64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check stable / armv7-linux-androideabi

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check nightly / armv7-linux-androideabi

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check 1.69.0 / aarch64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check stable / i686-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check nightly / i686-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check 1.69.0 / armv7-linux-androideabi

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check 1.69.0 / i686-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`

Check failure on line 4 in src/unix/groups.rs

View workflow job for this annotation

GitHub Actions / Check 1.69.0 / x86_64-linux-android

unresolved imports `libc::endgrent`, `libc::getgrent`, `libc::setgrent`
use std::collections::HashMap;

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub(crate) struct GroupInner {
Expand All @@ -25,35 +24,37 @@ impl GroupInner {
}
}

// Not used by mac.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub(crate) fn get_groups(groups: &mut Vec<Group>) {
use std::fs::File;
use std::io::Read;
groups.clear();

#[inline]
fn parse_id(id: &str) -> Option<u32> {
id.parse::<u32>().ok()
}
let mut groups_map = HashMap::with_capacity(10);

unsafe {
setgrent();
loop {
let gr = getgrent();
if gr.is_null() {
// The call was interrupted by a signal, retrying.
if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
continue;
}
break;
}

groups.clear();
if let Some(name) = crate::unix::utils::cstr_to_rust((*gr).gr_name) {
if groups_map.contains_key(&name) {
continue;
}

let mut s = String::new();

let _ = File::open("/etc/group").and_then(|mut f| f.read_to_string(&mut s));
for line in s.lines() {
let mut parts = line.split(':');
if let Some(name) = parts.next() {
let mut parts = parts.skip(1);
// Skip the user if the uid cannot be parsed correctly
if let Some(gid) = parts.next().and_then(parse_id) {
groups.push(Group {
inner: GroupInner::new(Gid(gid), name.to_owned()),
});
let gid = (*gr).gr_gid;
groups_map.insert(name, Gid(gid));
}
}
endgrent();
}
for (name, gid) in groups_map {
groups.push(Group {
inner: GroupInner::new(gid, name),
});
}
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) use crate::unix::apple::groups::get_groups;

0 comments on commit 8d48849

Please sign in to comment.