libfuse_fs/
context.rs

1/// Operation context for filesystem operations.
2/// This provides a way to pass additional parameters beyond the FUSE Request,
3/// allowing internal operations to override UID/GID or other parameters.
4use rfuse3::raw::Request;
5
6#[derive(Debug, Clone, Copy)]
7pub struct OperationContext {
8    /// The original FUSE request
9    pub req: Request,
10    /// Override UID for internal operations, otherwise use req.uid
11    pub uid: Option<u32>,
12    /// Override GID for internal operations, otherwise use req.gid
13    pub gid: Option<u32>,
14}
15
16impl From<Request> for OperationContext {
17    fn from(req: Request) -> Self {
18        OperationContext {
19            req,
20            uid: None,
21            gid: None,
22        }
23    }
24}
25
26impl OperationContext {
27    /// Create a new context from a request
28    pub fn new(req: Request) -> Self {
29        Self::from(req)
30    }
31
32    /// Create a context with explicit UID/GID override
33    pub fn with_credentials(req: Request, uid: u32, gid: u32) -> Self {
34        OperationContext {
35            req,
36            uid: Some(uid),
37            gid: Some(gid),
38        }
39    }
40
41    /// Get the effective UID (override or from request)
42    pub fn effective_uid(&self) -> Option<u32> {
43        self.uid.or(Some(self.req.uid))
44    }
45
46    /// Get the effective GID (override or from request)
47    pub fn effective_gid(&self) -> Option<u32> {
48        self.gid.or(Some(self.req.gid))
49    }
50}