/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sysinfo-0.37.2/src/unix/groups.rs
Line | Count | Source |
1 | | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | | |
3 | | use crate::{Gid, Group, GroupInner}; |
4 | | |
5 | | impl GroupInner { |
6 | 0 | pub(crate) fn new(id: crate::Gid, name: String) -> Self { |
7 | 0 | Self { id, name } |
8 | 0 | } |
9 | | |
10 | 0 | pub(crate) fn id(&self) -> &crate::Gid { |
11 | 0 | &self.id |
12 | 0 | } |
13 | | |
14 | 0 | pub(crate) fn name(&self) -> &str { |
15 | 0 | &self.name |
16 | 0 | } |
17 | | } |
18 | | |
19 | 0 | pub(crate) fn get_groups(groups: &mut Vec<Group>) { |
20 | 0 | groups.clear(); |
21 | | |
22 | 0 | let mut groups_map = std::collections::HashMap::with_capacity(10); |
23 | | |
24 | | unsafe { |
25 | 0 | libc::setgrent(); |
26 | | loop { |
27 | 0 | let gr = libc::getgrent(); |
28 | 0 | if gr.is_null() { |
29 | | // The call was interrupted by a signal, retrying. |
30 | 0 | if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { |
31 | 0 | continue; |
32 | 0 | } |
33 | 0 | break; |
34 | 0 | } |
35 | | |
36 | 0 | if let Some(name) = crate::unix::utils::cstr_to_rust((*gr).gr_name) { |
37 | 0 | if groups_map.contains_key(&name) { |
38 | 0 | continue; |
39 | 0 | } |
40 | | |
41 | 0 | let gid = (*gr).gr_gid; |
42 | 0 | groups_map.insert(name, Gid(gid)); |
43 | 0 | } |
44 | | } |
45 | 0 | libc::endgrent(); |
46 | | } |
47 | 0 | for (name, gid) in groups_map { |
48 | 0 | groups.push(Group { |
49 | 0 | inner: GroupInner::new(gid, name), |
50 | 0 | }); |
51 | 0 | } |
52 | 0 | } |