1pub type Stage = u32;
3
4mod mode;
5pub use mode::Mode;
6
7mod flags;
8pub(crate) use flags::at_rest;
9pub use flags::Flags;
10
11mod write;
12
13#[derive(Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
15#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
16pub struct Time {
17 pub secs: u32,
19 pub nsecs: u32,
21}
22
23#[derive(Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
25#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
26pub struct Stat {
27 pub mtime: Time,
29 pub ctime: Time,
31 pub dev: u32,
33 pub ino: u32,
35 pub uid: u32,
37 pub gid: u32,
39 pub size: u32,
41}
42
43mod access {
44 use bstr::{BStr, ByteSlice};
45
46 use crate::{entry, Entry, State};
47
48 impl Entry {
49 pub fn path<'a>(&self, state: &'a State) -> &'a BStr {
51 state.path_backing[self.path.clone()].as_bstr()
52 }
53
54 pub fn path_in<'backing>(&self, backing: &'backing crate::PathStorageRef) -> &'backing BStr {
56 backing[self.path.clone()].as_bstr()
57 }
58
59 pub fn stage(&self) -> entry::Stage {
61 self.flags.stage()
62 }
63 }
64}
65
66mod _impls {
67 use std::{cmp::Ordering, ops::Add, time::SystemTime};
68
69 use bstr::BStr;
70
71 use crate::{entry::Time, Entry, State};
72
73 impl From<SystemTime> for Time {
74 fn from(s: SystemTime) -> Self {
75 let d = s
76 .duration_since(std::time::UNIX_EPOCH)
77 .expect("system time is not before unix epoch!");
78 Time {
79 secs: d.as_secs() as u32,
80 nsecs: d.subsec_nanos(),
81 }
82 }
83 }
84
85 impl From<Time> for SystemTime {
86 fn from(s: Time) -> Self {
87 std::time::UNIX_EPOCH.add(std::time::Duration::new(s.secs.into(), s.nsecs))
88 }
89 }
90
91 impl Entry {
92 pub fn cmp(&self, other: &Self, state: &State) -> Ordering {
95 let lhs = self.path(state);
96 let rhs = other.path(state);
97 Entry::cmp_filepaths(lhs, rhs).then_with(|| self.stage().cmp(&other.stage()))
98 }
99
100 pub fn cmp_filepaths(a: &BStr, b: &BStr) -> Ordering {
103 let common_len = a.len().min(b.len());
104 a[..common_len]
105 .cmp(&b[..common_len])
106 .then_with(|| a.len().cmp(&b.len()))
107 }
108 }
109}