Skip to main content

dope_sys/io/file/
metadata.rs

1use std::num::NonZero;
2
3use crate::io::file::Kind;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub struct Identity {
7    pub device: u64,
8    pub inode: u64,
9}
10
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub struct Owner {
13    pub(super) uid: libc::uid_t,
14    pub(super) gid: libc::gid_t,
15}
16
17pub type Value = (Kind, super::Offset, Option<Identity>);
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub enum Error {
21    Missing(NonZero<u32>),
22    Negative(i64),
23    Overflow(u64),
24}
25
26/// Native metadata for one opened or named file.
27#[derive(Clone, Copy, Debug)]
28pub struct Metadata {
29    kind: Kind,
30    size: i64,
31    identity: Identity,
32    owner: Owner,
33}
34
35impl Metadata {
36    pub(super) const fn new(raw: libc::stat) -> Self {
37        Self {
38            kind: Kind::from_mode(raw.st_mode as u32),
39            size: raw.st_size,
40            identity: Identity {
41                device: raw.st_dev as u64,
42                inode: raw.st_ino as u64,
43            },
44            owner: Owner {
45                uid: raw.st_uid,
46                gid: raw.st_gid,
47            },
48        }
49    }
50
51    pub const fn kind(self) -> Kind {
52        self.kind
53    }
54
55    /// Exact signed st_size reported by the native metadata operation.
56    pub const fn size(self) -> i64 {
57        self.size
58    }
59
60    pub const fn identity(self) -> Identity {
61        self.identity
62    }
63
64    pub const fn owner(self) -> Owner {
65        self.owner
66    }
67}