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
use crate::fs::Permissions;
use std::fs;
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct PermissionsExt {
mode: libc::mode_t,
}
impl PermissionsExt {
/// Constructs a new instance of `Self` from the given
/// [`std::fs::Permissions`].
#[inline]
pub(crate) fn from_std(std: fs::Permissions) -> Self {
use std::os::unix::fs::PermissionsExt;
Self {
mode: std.mode() as libc::mode_t & 0o7777,
}
}
/// Constructs a new instance of `Permissions` from the given
/// `libc::mode_t`.
#[inline]
pub(crate) const fn from_libc(mode: libc::mode_t) -> Permissions {
Permissions {
readonly: Self::readonly(mode),
ext: Self {
mode: mode & 0o7777,
},
}
}
/// Test whether the given `libc::mode_t` lacks write permissions.
#[inline]
pub(crate) const fn readonly(mode: libc::mode_t) -> bool {
mode & 0o222 == 0
}
/// Test whether the given `libc::mode_t` lacks write permissions.
#[inline]
pub(crate) fn set_readonly(&mut self, readonly: bool) {
if readonly {
// remove write permission for all classes; equivalent to `chmod a-w <file>`
self.mode &= !0o222;
} else {
// add write permission for all classes; equivalent to `chmod a+w <file>`
self.mode |= 0o222;
}
}
}
impl std::os::unix::fs::PermissionsExt for PermissionsExt {
fn mode(&self) -> u32 {
self.mode as u32
}
fn set_mode(&mut self, mode: u32) {
self.mode = mode as libc::mode_t & 0o7777;
}
fn from_mode(mode: u32) -> Self {
Self {
mode: mode as libc::mode_t & 0o7777,
}
}
}