1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const SEAL_MASK: u32 = (libc::F_SEAL_SEAL | libc::F_SEAL_SHRINK | libc::F_SEAL_GROW | libc::F_SEAL_WRITE | libc::F_SEAL_FUTURE_WRITE) as u32;
const ALL_SEALS: [Seal; 5] = [Seal::Seal, Seal::Shrink, Seal::Grow, Seal::Write, Seal::FutureWrite];

/// A seal that prevents certain actions from being performed on a file.
///
/// Note that seals apply to a file, not a file descriptor.
/// If two file descriptors refer to the same file, they share the same set of seals.
///
/// Seals can not be removed from a file once applied.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[repr(u32)]
#[non_exhaustive]
pub enum Seal {
	/// Prevent adding more seals to the file.
	Seal = libc::F_SEAL_SEAL as u32,

	/// Prevent the file from being shrunk with `truncate` or similar.
	///
	/// Combine with [`Seal::Grow`] to prevent the file from being resized in any way.
	Shrink = libc::F_SEAL_SHRINK as u32,

	/// Prevent the file from being extended with `truncate`, `fallocate` or simillar.
	///
	/// Combine with [`Seal::Shrink`] to prevent the file from being resized in any way.
	Grow = libc::F_SEAL_GROW as u32,

	/// Prevent write to the file.
	///
	/// This will block *all* writes to the file and prevents any shared, writable memory mappings from being created.
	///
	/// If a shared, writable memory mapping already exists, adding this seal will fail.
	Write = libc::F_SEAL_WRITE as u32,

	/// Similar to [`Seal::Write`], but allows existing shared, writable memory mappings to modify the file contents.
	///
	/// This can be used to share a read-only view of the file with other processes,
	/// while still being able to modify the contents through an existing mapping.
	FutureWrite = libc::F_SEAL_FUTURE_WRITE as u32,
}

/// A set of [seals][Seal].
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct Seals {
	bits: u32,
}

impl Seals {
	/// Construct a set of seals from a bitmask.
	///
	/// Unknown bits are trunctated.
	#[inline]
	pub const fn from_bits_truncate(bits: u32) -> Self {
		Self::from_bits(bits & SEAL_MASK)
	}

	/// Construct a set of seals from a bitmask.
	///
	/// Unknown bits are trunctated.
	#[inline]
	const fn from_bits(bits: u32) -> Self {
		Self { bits }
	}

	#[inline]
	pub const fn bits(self) -> u32 {
		self.bits
	}

	/// Get an empty set of seals.
	#[inline]
	pub const fn empty() -> Self {
		Self::from_bits_truncate(0)
	}

	/// Get a set of seals containing all possible seals.
	#[inline]
	pub const fn all() -> Self {
		Self::from_bits(SEAL_MASK)
	}

	/// Get the number of seals in the set.
	#[inline]
	pub const fn len(self) -> usize {
		self.bits.count_ones() as usize
	}

	/// Check if the set of seals is empty.
	#[inline]
	pub const fn is_empty(self) -> bool {
		self.bits == 0
	}

	/// Check if the set of seals contains all possible seals.
	#[inline]
	pub const fn is_all(self) -> bool {
		self.bits == Self::all().bits
	}

	/// Check if the set of seals contains all the given seals.
	#[inline]
	pub fn contains(self, other: impl Into<Self>) -> bool {
		let other = other.into();
		self & other == other
	}

	/// Check if the set of seals contains at least one of the given seals.
	#[inline]
	pub fn intersects(self, other: impl Into<Self>) -> bool {
		!(self & other).is_empty()
	}

	/// Iterate over the seals in the set.
	#[inline]
	pub fn iter(&self) -> SealsIterator {
		SealsIterator::new(*self)
	}
}

impl IntoIterator for Seals {
	type Item = Seal;
	type IntoIter = SealsIterator;

	#[inline]
	fn into_iter(self) -> SealsIterator {
		self.iter()
	}
}

impl IntoIterator for &Seals {
	type Item = Seal;
	type IntoIter = SealsIterator;

	#[inline]
	fn into_iter(self) -> SealsIterator {
		self.iter()
	}
}

impl From<Seal> for Seals {
	#[inline]
	fn from(other: Seal) -> Self {
		Self::from_bits_truncate(other as u32)
	}
}

impl<T: Into<Seals>> std::ops::BitOr<T> for Seals {
	type Output = Seals;

	#[inline]
	fn bitor(self, right: T) -> Self {
		Self::from_bits(self.bits | right.into().bits)
	}
}

impl<T: Into<Seals>> std::ops::BitOrAssign<T> for Seals {
	#[inline]
	fn bitor_assign(&mut self, right: T) {
		self.bits |= right.into().bits;
	}
}

impl<T: Into<Seals>> std::ops::BitAnd<T> for Seals {
	type Output = Seals;

	#[inline]
	fn bitand(self, right: T) -> Self {
		Self::from_bits(self.bits & right.into().bits)
	}
}

impl<T: Into<Seals>> std::ops::BitAndAssign<T> for Seals {
	#[inline]
	fn bitand_assign(&mut self, right: T) {
		self.bits &= right.into().bits;
	}
}

impl<T: Into<Seals>> std::ops::Sub<T> for Seals {
	type Output = Seals;

	#[inline]
	fn sub(self, right: T) -> Self {
		Self::from_bits(self.bits & !right.into().bits)
	}
}

impl<T: Into<Seals>> std::ops::SubAssign<T> for Seals {
	#[inline]
	fn sub_assign(&mut self, right: T) {
		self.bits &= !right.into().bits;
	}
}

impl<T: Into<Seals>> std::ops::BitXor<T> for Seals {
	type Output = Seals;

	#[inline]
	fn bitxor(self, right: T) -> Self {
		Self::from_bits(self.bits ^ right.into().bits)
	}
}

impl<T: Into<Seals>> std::ops::BitXorAssign<T> for Seals {
	#[inline]
	fn bitxor_assign(&mut self, right: T) {
		self.bits ^= right.into().bits;
	}
}

impl std::ops::Not for Seals {
	type Output = Seals;

	#[inline]
	fn not(self) -> Seals {
		Self::from_bits(!self.bits)
	}
}

impl std::ops::BitOr<Seals> for Seal {
	type Output = Seals;

	#[inline]
	fn bitor(self, right: Seals) -> Seals {
		Seals::from(self) | right
	}
}

impl std::ops::BitAnd<Seals> for Seal {
	type Output = Seals;

	#[inline]
	fn bitand(self, right: Seals) -> Seals {
		Seals::from(self) & right
	}
}

impl std::ops::Sub<Seals> for Seal {
	type Output = Seals;

	#[inline]
	fn sub(self, right: Seals) -> Seals {
		Seals::from(self) - right
	}
}

impl std::ops::BitXor<Seals> for Seal {
	type Output = Seals;

	#[inline]
	fn bitxor(self, right: Seals) -> Seals {
		Seals::from(self) ^ right
	}
}

impl std::ops::BitOr<Seal> for Seal {
	type Output = Seals;

	#[inline]
	fn bitor(self, right: Seal) -> Seals {
		Seals::from(self) | right
	}
}

impl std::ops::BitAnd<Seal> for Seal {
	type Output = Seals;

	#[inline]
	fn bitand(self, right: Seal) -> Seals {
		Seals::from(self) & right
	}
}

impl std::ops::Sub<Seal> for Seal {
	type Output = Seals;

	#[inline]
	fn sub(self, right: Seal) -> Seals {
		Seals::from(self) - right
	}
}

impl std::ops::BitXor<Seal> for Seal {
	type Output = Seals;

	#[inline]
	fn bitxor(self, right: Seal) -> Seals {
		Seals::from(self) ^ right
	}
}

pub struct SealsIterator {
	seals: Seals,
}

impl SealsIterator {
	fn new(seals: Seals) -> Self {
		Self { seals }
	}

}

impl Iterator for SealsIterator {
	type Item = Seal;

	#[inline]
	fn next(&mut self) -> Option<Seal> {
		for &seal in &ALL_SEALS {
			if self.seals.contains(seal) {
				self.seals -= seal;
				return Some(seal)
			}
		}
		None
	}
}

impl std::fmt::Debug for Seals {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		write!(f, "Seals {{ ")?;
		for (i, seal) in self.iter().enumerate() {
			if i == 0 {
				write!(f, "{:?} ", seal)?
			} else {
				write!(f, "| {:?} ", seal)?
			}
		}
		write!(f, "}}")?;
		Ok(())
	}
}

#[cfg(test)]
mod test {
	use super::*;
	use assert2::assert;

	#[test]
	fn test_empty() {
		assert!(Seals::empty().len() == 0);
		assert!(Seals::empty().is_empty());
		assert!(!Seals::empty().is_all());
		assert!(Seals::empty().contains(Seals::empty()));
		assert!(!Seals::empty().contains(Seals::all()));
		assert!(!Seals::empty().contains(Seal::Seal));
		assert!(!Seals::empty().contains(Seal::Shrink));
		assert!(!Seals::empty().contains(Seal::Grow));
		assert!(!Seals::empty().contains(Seal::Write));
		assert!(!Seals::empty().contains(Seal::FutureWrite));
	}

	#[test]
	fn test_all() {
		assert!(Seals::all().len() == 5);
		assert!(!Seals::all().is_empty());
		assert!(Seals::all().is_all());
		assert!(Seals::all().contains(Seals::empty()));
		assert!(Seals::all().contains(Seals::all()));
		assert!(Seals::all().contains(Seal::Seal));
		assert!(Seals::all().contains(Seal::Shrink));
		assert!(Seals::all().contains(Seal::Grow));
		assert!(Seals::all().contains(Seal::Write));
		assert!(Seals::all().contains(Seal::FutureWrite));
	}

	#[test]
	fn test_iter() {
		let mut iter = Seals::all().into_iter();
		assert!(iter.next() == Some(Seal::Seal));
		assert!(iter.next() == Some(Seal::Shrink));
		assert!(iter.next() == Some(Seal::Grow));
		assert!(iter.next() == Some(Seal::Write));
		assert!(iter.next() == Some(Seal::FutureWrite));
		assert!(iter.next() == None);

		let mut iter = (Seal::Shrink | Seal::Grow).into_iter();
		assert!(iter.next() == Some(Seal::Shrink));
		assert!(iter.next() == Some(Seal::Grow));
		assert!(iter.next() == None);
	}

	#[test]
	fn test_bitor() {
		assert!((Seal::Seal | Seal::FutureWrite | Seal::Write).len() == 3);
		assert!((Seal::Seal | Seal::FutureWrite | Seal::Write).contains(Seal::Seal));
		assert!(!(Seal::Seal | Seal::FutureWrite | Seal::Write).contains(Seal::Shrink));
		assert!(!(Seal::Seal | Seal::FutureWrite | Seal::Write).contains(Seal::Grow));
		assert!((Seal::Seal | Seal::FutureWrite | Seal::Write).contains(Seal::Write));
		assert!((Seal::Seal | Seal::FutureWrite | Seal::Write).contains(Seal::FutureWrite));
	}

	#[test]
	fn test_bitand() {
		let subset = Seal::Seal | Seal::Write;
		assert!(Seals::all() & subset == subset);
		assert!((Seals::all() & subset).len() == 2);
	}

	#[test]
	fn test_bitxor() {
		assert!(Seals::all() ^ (Seal::Seal | Seal::Write) == (Seal::Shrink | Seal::Grow | Seal::FutureWrite));
	}

	#[test]
	fn test_debug() {
		assert!(format!("{:?}", Seals::empty()) == "Seals { }");
		assert!(format!("{:?}", Seals::from(Seal::Seal)) == "Seals { Seal }");
		assert!(format!("{:?}", Seal::Seal | Seal::Shrink) == "Seals { Seal | Shrink }");
		assert!(format!("{:?}", Seals::all()) == "Seals { Seal | Shrink | Grow | Write | FutureWrite }");
	}
}