forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrp.rs
More file actions
86 lines (78 loc) · 2.52 KB
/
Copy pathgrp.rs
File metadata and controls
86 lines (78 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// spell-checker:disable
pub(crate) use grp::module_def;
#[pymodule]
mod grp {
use crate::vm::{
PyObjectRef, PyResult, VirtualMachine,
builtins::{PyIntRef, PyListRef, PyUtf8StrRef},
convert::{IntoPyException, ToPyObject},
exceptions,
types::PyStructSequence,
};
use rustpython_host_env::grp as host_grp;
#[pystruct_sequence_data]
struct GroupData {
gr_name: String,
gr_passwd: String,
gr_gid: u32,
gr_mem: PyListRef,
}
#[pyattr]
#[pystruct_sequence(name = "struct_group", module = "grp", data = "GroupData")]
struct PyGroup;
#[pyclass(with(PyStructSequence))]
impl PyGroup {}
impl GroupData {
fn from_group(group: host_grp::Group, vm: &VirtualMachine) -> Self {
Self {
gr_name: group.name,
gr_passwd: group.passwd,
gr_gid: group.gid,
gr_mem: vm
.ctx
.new_list(group.mem.iter().map(|s| s.to_pyobject(vm)).collect()),
}
}
}
#[pyfunction]
fn getgrgid(gid: PyIntRef, vm: &VirtualMachine) -> PyResult<GroupData> {
let gr_gid = gid.as_bigint();
let gid = libc::gid_t::try_from(gr_gid).ok();
let group = gid
.map(host_grp::getgrgid)
.transpose()
.map_err(|err| err.into_pyexception(vm))?
.flatten();
let group = group.ok_or_else(|| {
vm.new_key_error(
vm.ctx
.new_str(format!("getgrgid: group id {gr_gid} not found"))
.into(),
)
})?;
Ok(GroupData::from_group(group, vm))
}
#[pyfunction]
fn getgrnam(name: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<GroupData> {
let gr_name = name.as_str();
if gr_name.contains('\0') {
return Err(exceptions::cstring_error(vm));
}
let group = host_grp::getgrnam(gr_name).map_err(|err| err.into_pyexception(vm))?;
let group = group.ok_or_else(|| {
vm.new_key_error(
vm.ctx
.new_str(format!("getgrnam: group name {gr_name} not found"))
.into(),
)
})?;
Ok(GroupData::from_group(group, vm))
}
#[pyfunction]
fn getgrall(vm: &VirtualMachine) -> Vec<PyObjectRef> {
host_grp::getgrall()
.into_iter()
.map(|group| GroupData::from_group(group, vm).to_pyobject(vm))
.collect()
}
}