/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.1.3/src/ugid.rs
Line | Count | Source |
1 | | //! User and Group ID types. |
2 | | |
3 | | use core::fmt; |
4 | | |
5 | | use crate::backend::c; |
6 | | use crate::ffi; |
7 | | |
8 | | /// A group identifier as a raw integer. |
9 | | pub type RawGid = ffi::c_uint; |
10 | | /// A user identifier as a raw integer. |
11 | | pub type RawUid = ffi::c_uint; |
12 | | |
13 | | /// `uid_t`—A Unix user ID. |
14 | | #[repr(transparent)] |
15 | | #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] |
16 | | pub struct Uid(RawUid); |
17 | | |
18 | | /// `gid_t`—A Unix group ID. |
19 | | #[repr(transparent)] |
20 | | #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] |
21 | | pub struct Gid(RawGid); |
22 | | |
23 | | impl Uid { |
24 | | /// A `Uid` corresponding to the root user (uid 0). |
25 | | pub const ROOT: Self = Self(0); |
26 | | |
27 | | /// Converts a `RawUid` into a `Uid`. |
28 | | /// |
29 | | /// `raw` must be the value of a valid Unix user ID, and not `-1`. |
30 | | #[inline] |
31 | 0 | pub fn from_raw(raw: RawUid) -> Self { |
32 | 0 | debug_assert_ne!(raw, !0); |
33 | 0 | Self(raw) |
34 | 0 | } |
35 | | |
36 | | /// Converts a `RawUid` into a `Uid`. |
37 | | /// |
38 | | /// `raw` must be the value of a valid Unix user ID, and not `-1`. |
39 | | #[inline] |
40 | 0 | pub const fn from_raw_unchecked(raw: RawUid) -> Self { |
41 | 0 | Self(raw) |
42 | 0 | } |
43 | | |
44 | | /// Converts a `Uid` into a `RawUid`. |
45 | | #[inline] |
46 | 0 | pub const fn as_raw(self) -> RawUid { |
47 | 0 | self.0 |
48 | 0 | } |
49 | | |
50 | | /// Test whether this uid represents the root user ([`Uid::ROOT`]). |
51 | | #[inline] |
52 | 0 | pub const fn is_root(self) -> bool { |
53 | 0 | self.0 == Self::ROOT.0 |
54 | 0 | } |
55 | | } |
56 | | |
57 | | impl Gid { |
58 | | /// A `Gid` corresponding to the root group (gid 0). |
59 | | pub const ROOT: Self = Self(0); |
60 | | |
61 | | /// Converts a `RawGid` into a `Gid`. |
62 | | /// |
63 | | /// `raw` must be the value of a valid Unix group ID, and not `-1`. |
64 | | #[inline] |
65 | 0 | pub fn from_raw(raw: RawGid) -> Self { |
66 | 0 | debug_assert_ne!(raw, !0); |
67 | 0 | Self(raw) |
68 | 0 | } |
69 | | |
70 | | /// Converts a `RawGid` into a `Gid`. |
71 | | /// |
72 | | /// `raw` must be the value of a valid Unix group ID, and not `-1`. |
73 | | #[inline] |
74 | 0 | pub const fn from_raw_unchecked(raw: RawGid) -> Self { |
75 | 0 | Self(raw) |
76 | 0 | } |
77 | | |
78 | | /// Converts a `Gid` into a `RawGid`. |
79 | | #[inline] |
80 | 0 | pub const fn as_raw(self) -> RawGid { |
81 | 0 | self.0 |
82 | 0 | } |
83 | | |
84 | | /// Test whether this gid represents the root group ([`Gid::ROOT`]). |
85 | | #[inline] |
86 | 0 | pub const fn is_root(self) -> bool { |
87 | 0 | self.0 == Self::ROOT.0 |
88 | 0 | } |
89 | | } |
90 | | |
91 | | impl fmt::Display for Uid { |
92 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
93 | 0 | self.0.fmt(f) |
94 | 0 | } |
95 | | } |
96 | | impl fmt::Binary for Uid { |
97 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
98 | 0 | self.0.fmt(f) |
99 | 0 | } |
100 | | } |
101 | | impl fmt::Octal for Uid { |
102 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
103 | 0 | self.0.fmt(f) |
104 | 0 | } |
105 | | } |
106 | | impl fmt::LowerHex for Uid { |
107 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
108 | 0 | self.0.fmt(f) |
109 | 0 | } |
110 | | } |
111 | | impl fmt::UpperHex for Uid { |
112 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
113 | 0 | self.0.fmt(f) |
114 | 0 | } |
115 | | } |
116 | | impl fmt::LowerExp for Uid { |
117 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
118 | 0 | self.0.fmt(f) |
119 | 0 | } |
120 | | } |
121 | | impl fmt::UpperExp for Uid { |
122 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
123 | 0 | self.0.fmt(f) |
124 | 0 | } |
125 | | } |
126 | | |
127 | | impl fmt::Display for Gid { |
128 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
129 | 0 | self.0.fmt(f) |
130 | 0 | } |
131 | | } |
132 | | impl fmt::Binary for Gid { |
133 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
134 | 0 | self.0.fmt(f) |
135 | 0 | } |
136 | | } |
137 | | impl fmt::Octal for Gid { |
138 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
139 | 0 | self.0.fmt(f) |
140 | 0 | } |
141 | | } |
142 | | impl fmt::LowerHex for Gid { |
143 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
144 | 0 | self.0.fmt(f) |
145 | 0 | } |
146 | | } |
147 | | impl fmt::UpperHex for Gid { |
148 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
149 | 0 | self.0.fmt(f) |
150 | 0 | } |
151 | | } |
152 | | impl fmt::LowerExp for Gid { |
153 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
154 | 0 | self.0.fmt(f) |
155 | 0 | } |
156 | | } |
157 | | impl fmt::UpperExp for Gid { |
158 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
159 | 0 | self.0.fmt(f) |
160 | 0 | } |
161 | | } |
162 | | |
163 | | // Return the raw value of the IDs. In case of `None` it returns `!0` since it |
164 | | // has the same bit pattern as `-1` indicating no change to the owner/group ID. |
165 | 0 | pub(crate) fn translate_fchown_args( |
166 | 0 | owner: Option<Uid>, |
167 | 0 | group: Option<Gid>, |
168 | 0 | ) -> (c::uid_t, c::gid_t) { |
169 | 0 | let ow = match owner { |
170 | 0 | Some(o) => o.as_raw(), |
171 | 0 | None => !0, |
172 | | }; |
173 | | |
174 | 0 | let gr = match group { |
175 | 0 | Some(g) => g.as_raw(), |
176 | 0 | None => !0, |
177 | | }; |
178 | | |
179 | 0 | (ow as c::uid_t, gr as c::gid_t) |
180 | 0 | } |
181 | | |
182 | | #[cfg(test)] |
183 | | mod tests { |
184 | | use super::*; |
185 | | |
186 | | #[test] |
187 | | fn test_sizes() { |
188 | | assert_eq_size!(RawUid, u32); |
189 | | assert_eq_size!(RawGid, u32); |
190 | | assert_eq_size!(RawUid, libc::uid_t); |
191 | | assert_eq_size!(RawGid, libc::gid_t); |
192 | | } |
193 | | } |