1use rustix::fs::SealFlags;
2use std::collections::HashSet;
3
4pub type SealsHashSet = HashSet<FileSeal>;
6
7#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11pub enum FileSeal {
12 SealShrink,
16 SealGrow,
20 SealWrite,
24 SealSeal,
28 #[cfg(any(target_os = "android", target_os = "linux"))]
33 SealFutureWrite,
34}
35
36impl FileSeal {
37 pub(crate) const fn bitflags(self) -> SealFlags {
39 match self {
40 Self::SealSeal => SealFlags::SEAL,
41 Self::SealShrink => SealFlags::SHRINK,
42 Self::SealGrow => SealFlags::GROW,
43 Self::SealWrite => SealFlags::WRITE,
44 #[cfg(any(target_os = "android", target_os = "linux"))]
45 Self::SealFutureWrite => SealFlags::FUTURE_WRITE,
46 }
47 }
48}
49
50pub(crate) fn seals_to_bitflags<'a>(seals: impl IntoIterator<Item = &'a FileSeal>) -> SealFlags {
52 let mut bits = SealFlags::empty();
53 for seal in seals {
54 bits |= seal.bitflags();
55 }
56 bits
57}
58
59pub(crate) fn bitflags_to_seals(bitflags: SealFlags) -> SealsHashSet {
61 let mut sset = SealsHashSet::new();
62 if bitflags.contains(SealFlags::SEAL) {
63 sset.insert(FileSeal::SealSeal);
64 }
65 if bitflags.contains(SealFlags::SHRINK) {
66 sset.insert(FileSeal::SealShrink);
67 }
68 if bitflags.contains(SealFlags::GROW) {
69 sset.insert(FileSeal::SealGrow);
70 }
71 if bitflags.contains(SealFlags::WRITE) {
72 sset.insert(FileSeal::SealWrite);
73 }
74 #[cfg(any(target_os = "android", target_os = "linux"))]
75 if bitflags.contains(SealFlags::FUTURE_WRITE) {
76 sset.insert(FileSeal::SealFutureWrite);
77 }
78 sset
79}