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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#![allow(unsafe_code)]
use crate::backend::c;
#[cfg(not(target_os = "wasi"))]
pub type RawGid = c::gid_t;
#[cfg(not(target_os = "wasi"))]
pub type RawUid = c::uid_t;
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Uid(RawUid);
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Gid(RawGid);
impl Uid {
pub const ROOT: Self = Self(0);
#[inline]
pub const unsafe fn from_raw(raw: RawUid) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawUid {
self.0
}
#[inline]
pub const fn is_root(self) -> bool {
self.0 == Self::ROOT.0
}
}
impl Gid {
pub const ROOT: Self = Self(0);
#[inline]
pub const unsafe fn from_raw(raw: RawGid) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawGid {
self.0
}
#[inline]
pub const fn is_root(self) -> bool {
self.0 == Self::ROOT.0
}
}
pub(crate) fn translate_fchown_args(owner: Option<Uid>, group: Option<Gid>) -> (RawUid, RawGid) {
let ow = match owner {
Some(o) => o.as_raw(),
None => !0,
};
let gr = match group {
Some(g) => g.as_raw(),
None => !0,
};
(ow, gr)
}
#[test]
fn test_sizes() {
use core::mem::size_of;
assert_eq!(size_of::<RawUid>(), size_of::<u32>());
assert_eq!(size_of::<RawGid>(), size_of::<u32>());
}