]> git.proxmox.com Git - pxar.git/blob - src/format/acl.rs
derive PartialEq trait for Metadata and related structs
[pxar.git] / src / format / acl.rs
1 //! ACL related data
2
3 use std::cmp::Ordering;
4
5 use endian_trait::Endian;
6
7 pub const READ: u64 = 4;
8 pub const WRITE: u64 = 2;
9 pub const EXECUTE: u64 = 1;
10 pub const NO_MASK: u64 = u64::MAX;
11
12 /// ACL permission bits.
13 ///
14 /// While this is normally just a bitfield, the `NO_MASK` special value makes this a value of 2
15 /// possible "types", so we don't use `bitflags!` for this.
16 #[derive(Clone, Copy, Debug, Endian, Eq, Ord, PartialEq, PartialOrd)]
17 pub struct Permissions(pub u64);
18
19 impl Permissions {
20 pub const NO_MASK: Permissions = Permissions(NO_MASK);
21 }
22
23 #[derive(Clone, Debug, Endian, Eq, PartialEq)]
24 #[repr(C)]
25 pub struct User {
26 pub uid: u64,
27 pub permissions: Permissions,
28 //pub name: Vec<u64>, not impl for now
29 }
30
31 impl User {
32 pub fn new(uid: u64, permissions: u64) -> Self {
33 Self {
34 uid,
35 permissions: Permissions(permissions),
36 }
37 }
38 }
39
40 // TODO if also name is impl, sort by uid, then by name and last by permissions
41 impl Ord for User {
42 fn cmp(&self, other: &User) -> Ordering {
43 match self.uid.cmp(&other.uid) {
44 // uids are equal, entries ordered by permissions
45 Ordering::Equal => self.permissions.cmp(&other.permissions),
46 // uids are different, entries ordered by uid
47 uid_order => uid_order,
48 }
49 }
50 }
51
52 impl PartialOrd for User {
53 fn partial_cmp(&self, other: &User) -> Option<Ordering> {
54 Some(self.cmp(other))
55 }
56 }
57
58 #[derive(Clone, Debug, Endian, Eq, PartialEq)]
59 #[repr(C)]
60 pub struct Group {
61 pub gid: u64,
62 pub permissions: Permissions,
63 //pub name: Vec<u64>, not impl for now
64 }
65
66 impl Group {
67 pub fn new(gid: u64, permissions: u64) -> Self {
68 Self {
69 gid,
70 permissions: Permissions(permissions),
71 }
72 }
73 }
74
75 // TODO if also name is impl, sort by gid, then by name and last by permissions
76 impl Ord for Group {
77 fn cmp(&self, other: &Group) -> Ordering {
78 match self.gid.cmp(&other.gid) {
79 // gids are equal, entries are ordered by permissions
80 Ordering::Equal => self.permissions.cmp(&other.permissions),
81 // gids are different, entries ordered by gid
82 gid_ordering => gid_ordering,
83 }
84 }
85 }
86
87 impl PartialOrd for Group {
88 fn partial_cmp(&self, other: &Group) -> Option<Ordering> {
89 Some(self.cmp(other))
90 }
91 }
92
93 #[derive(Clone, Debug, Endian, Eq, PartialEq)]
94 #[repr(C)]
95 pub struct GroupObject {
96 pub permissions: Permissions,
97 }
98
99 #[derive(Clone, Debug, Endian, PartialEq)]
100 #[cfg_attr(feature = "test-harness", derive(Eq))]
101 #[repr(C)]
102 pub struct Default {
103 pub user_obj_permissions: Permissions,
104 pub group_obj_permissions: Permissions,
105 pub other_permissions: Permissions,
106 pub mask_permissions: Permissions,
107 }