Skip to main content

async_fuser/
open_flags.rs

1use std::fmt;
2use std::fmt::Formatter;
3use std::fmt::LowerHex;
4use std::fmt::UpperHex;
5
6/// How the file should be opened: read-only, write-only, or read-write.
7#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
8#[repr(i32)]
9#[allow(non_camel_case_types)]
10pub enum OpenAccMode {
11    /// Open file for reading only.
12    O_RDONLY = libc::O_RDONLY,
13    /// Open file for writing only.
14    O_WRONLY = libc::O_WRONLY,
15    /// Open file for reading and writing.
16    O_RDWR = libc::O_RDWR,
17}
18
19/// Open flags as passed to open operation.
20#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
21pub struct OpenFlags(pub i32);
22
23impl LowerHex for OpenFlags {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        LowerHex::fmt(&self.0, f)
26    }
27}
28
29impl UpperHex for OpenFlags {
30    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
31        UpperHex::fmt(&self.0, f)
32    }
33}
34
35impl OpenFlags {
36    /// File access mode.
37    pub fn acc_mode(self) -> OpenAccMode {
38        match self.0 & libc::O_ACCMODE {
39            libc::O_RDONLY => OpenAccMode::O_RDONLY,
40            libc::O_WRONLY => OpenAccMode::O_WRONLY,
41            libc::O_RDWR => OpenAccMode::O_RDWR,
42            _ => {
43                // Impossible combination of flags.
44                // Do not panic because the field is public.
45                OpenAccMode::O_RDONLY
46            }
47        }
48    }
49}