Coverage Report

Created: 2025-07-23 06:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/cap-primitives-3.4.4/src/fs/permissions.rs
Line
Count
Source (jump to first uncovered line)
1
#[cfg(not(windows))]
2
use crate::fs::ImplPermissionsExt;
3
#[cfg(unix)]
4
use rustix::fs::RawMode;
5
use std::{fs, io};
6
7
/// Representation of the various permissions on a file.
8
///
9
/// This corresponds to [`std::fs::Permissions`].
10
///
11
/// <details>
12
/// We need to define our own version because the libstd `Permissions` doesn't
13
/// have a public constructor that we can use.
14
/// </details>
15
#[derive(Debug, Clone, Eq, PartialEq)]
16
pub struct Permissions {
17
    pub(crate) readonly: bool,
18
19
    #[cfg(any(unix, target_os = "vxworks"))]
20
    pub(crate) ext: ImplPermissionsExt,
21
}
22
23
impl Permissions {
24
    /// Constructs a new instance of `Self` from the given
25
    /// `std::fs::Permissions`.
26
    #[inline]
27
205k
    pub fn from_std(std: fs::Permissions) -> Self {
28
205k
        Self {
29
205k
            readonly: std.readonly(),
30
205k
31
205k
            #[cfg(any(unix, target_os = "vxworks"))]
32
205k
            ext: ImplPermissionsExt::from_std(std),
33
205k
        }
34
205k
    }
Unexecuted instantiation: <cap_primitives::fs::permissions::Permissions>::from_std
<cap_primitives::fs::permissions::Permissions>::from_std
Line
Count
Source
27
205k
    pub fn from_std(std: fs::Permissions) -> Self {
28
205k
        Self {
29
205k
            readonly: std.readonly(),
30
205k
31
205k
            #[cfg(any(unix, target_os = "vxworks"))]
32
205k
            ext: ImplPermissionsExt::from_std(std),
33
205k
        }
34
205k
    }
35
36
    /// Consumes `self` and produces a new instance of `std::fs::Permissions`.
37
    ///
38
    /// <details>
39
    /// The `file` parameter works around the fact that we can't construct a
40
    /// `Permissions` object ourselves on Windows.
41
    /// </details>
42
    #[inline]
43
0
    pub fn into_std(self, file: &fs::File) -> io::Result<fs::Permissions> {
44
0
        self._into_std(file)
45
0
    }
46
47
    #[cfg(unix)]
48
    #[inline]
49
    #[allow(clippy::unnecessary_wraps)]
50
0
    fn _into_std(self, _file: &fs::File) -> io::Result<fs::Permissions> {
51
        use std::os::unix::fs::PermissionsExt;
52
0
        Ok(fs::Permissions::from_mode(crate::fs::PermissionsExt::mode(
53
0
            &self.ext,
54
0
        )))
55
0
    }
56
57
    #[cfg(target_os = "wasi")]
58
    #[inline]
59
    #[allow(clippy::unnecessary_wraps)]
60
    fn _into_std(self, file: &fs::File) -> io::Result<fs::Permissions> {
61
        let mut permissions = file.metadata()?.permissions();
62
        permissions.set_readonly(self.readonly());
63
        Ok(permissions)
64
    }
65
66
    #[cfg(windows)]
67
    #[inline]
68
    fn _into_std(self, file: &fs::File) -> io::Result<fs::Permissions> {
69
        let mut permissions = file.metadata()?.permissions();
70
        permissions.set_readonly(self.readonly());
71
        Ok(permissions)
72
    }
73
74
    /// Returns `true` if these permissions describe a readonly (unwritable)
75
    /// file.
76
    ///
77
    /// This corresponds to [`std::fs::Permissions::readonly`].
78
    #[inline]
79
0
    pub const fn readonly(&self) -> bool {
80
0
        self.readonly
81
0
    }
82
83
    /// Modifies the readonly flag for this set of permissions.
84
    ///
85
    /// This corresponds to [`std::fs::Permissions::set_readonly`].
86
    #[inline]
87
0
    pub fn set_readonly(&mut self, readonly: bool) {
88
0
        self.readonly = readonly;
89
0
90
0
        #[cfg(any(unix, target_os = "vxworks"))]
91
0
        self.ext.set_readonly(readonly);
92
0
    }
93
}
94
95
/// Unix-specific extensions to [`Permissions`].
96
#[cfg(unix)]
97
pub trait PermissionsExt {
98
    /// Returns the underlying raw `st_mode` bits that contain the standard
99
    /// Unix permissions for this file.
100
    fn mode(&self) -> u32;
101
102
    /// Sets the underlying raw bits for this set of permissions.
103
    fn set_mode(&mut self, mode: u32);
104
105
    /// Creates a new instance of `Permissions` from the given set of Unix
106
    /// permission bits.
107
    fn from_mode(mode: u32) -> Self;
108
}
109
110
#[cfg(unix)]
111
impl PermissionsExt for Permissions {
112
    #[inline]
113
0
    fn mode(&self) -> u32 {
114
0
        crate::fs::PermissionsExt::mode(&self.ext)
115
0
    }
116
117
    #[inline]
118
0
    fn set_mode(&mut self, mode: u32) {
119
0
        crate::fs::PermissionsExt::set_mode(&mut self.ext, mode)
120
0
    }
121
122
    #[inline]
123
0
    fn from_mode(mode: u32) -> Self {
124
0
        Self {
125
0
            readonly: ImplPermissionsExt::readonly(mode as RawMode),
126
0
            ext: crate::fs::PermissionsExt::from_mode(mode),
127
0
        }
128
0
    }
129
}
130
131
#[cfg(target_os = "vxworks")]
132
impl PermissionsExt for Permissions {
133
    #[inline]
134
    fn mode(&self) -> u32 {
135
        self.ext.mode()
136
    }
137
138
    #[inline]
139
    fn set_mode(&mut self, mode: u32) {
140
        self.ext.set_mode(mode)
141
    }
142
143
    #[inline]
144
    fn from_mode(mode: u32) -> Self {
145
        Self {
146
            readonly: ImplPermissionsExt::readonly(mode),
147
            ext: ImplPermissionsExt::from(mode),
148
        }
149
    }
150
}