Coverage Report

Created: 2025-02-25 06:39

/rust/registry/src/index.crates.io-6f17d22bba15001f/rustix-0.38.44/src/ugid.rs
Line
Count
Source (jump to first uncovered line)
1
//! User and Group ID types.
2
3
#![allow(unsafe_code)]
4
5
use crate::backend::c;
6
7
/// A group identifier as a raw integer.
8
#[cfg(not(target_os = "wasi"))]
9
pub type RawGid = c::gid_t;
10
/// A user identifier as a raw integer.
11
#[cfg(not(target_os = "wasi"))]
12
pub type RawUid = c::uid_t;
13
14
/// `uid_t`—A Unix user ID.
15
#[repr(transparent)]
16
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
17
pub struct Uid(RawUid);
18
19
/// `gid_t`—A Unix group ID.
20
#[repr(transparent)]
21
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
22
pub struct Gid(RawGid);
23
24
impl Uid {
25
    /// A `Uid` corresponding to the root user (uid 0).
26
    pub const ROOT: Self = Self(0);
27
28
    /// Converts a `RawUid` into a `Uid`.
29
    ///
30
    /// # Safety
31
    ///
32
    /// `raw` must be the value of a valid Unix user ID.
33
    #[inline]
34
0
    pub const unsafe fn from_raw(raw: RawUid) -> Self {
35
0
        Self(raw)
36
0
    }
37
38
    /// Converts a `Uid` into a `RawUid`.
39
    #[inline]
40
0
    pub const fn as_raw(self) -> RawUid {
41
0
        self.0
42
0
    }
43
44
    /// Test whether this uid represents the root user (uid 0).
45
    #[inline]
46
0
    pub const fn is_root(self) -> bool {
47
0
        self.0 == Self::ROOT.0
48
0
    }
49
}
50
51
impl Gid {
52
    /// A `Gid` corresponding to the root group (gid 0).
53
    pub const ROOT: Self = Self(0);
54
55
    /// Converts a `RawGid` into a `Gid`.
56
    ///
57
    /// # Safety
58
    ///
59
    /// `raw` must be the value of a valid Unix group ID.
60
    #[inline]
61
0
    pub const unsafe fn from_raw(raw: RawGid) -> Self {
62
0
        Self(raw)
63
0
    }
64
65
    /// Converts a `Gid` into a `RawGid`.
66
    #[inline]
67
0
    pub const fn as_raw(self) -> RawGid {
68
0
        self.0
69
0
    }
70
71
    /// Test whether this gid represents the root group (gid 0).
72
    #[inline]
73
0
    pub const fn is_root(self) -> bool {
74
0
        self.0 == Self::ROOT.0
75
0
    }
76
}
77
78
// Return the raw value of the IDs. In case of `None` it returns `!0` since it
79
// has the same bit pattern as `-1` indicating no change to the owner/group ID.
80
0
pub(crate) fn translate_fchown_args(owner: Option<Uid>, group: Option<Gid>) -> (RawUid, RawGid) {
81
0
    let ow = match owner {
82
0
        Some(o) => o.as_raw(),
83
0
        None => !0,
84
    };
85
86
0
    let gr = match group {
87
0
        Some(g) => g.as_raw(),
88
0
        None => !0,
89
    };
90
91
0
    (ow, gr)
92
0
}
93
94
#[test]
95
fn test_sizes() {
96
    assert_eq_size!(RawUid, u32);
97
    assert_eq_size!(RawGid, u32);
98
}