reifydb-runtime 0.10.1

Runtime infrastructure for ReifyDB
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

#[cfg(any(test, feature = "testing"))]
use std::mem;
use std::{
	collections::{BTreeMap, BTreeSet},
	path::{Path, PathBuf},
	sync::Arc,
};

use crate::{
	io::fs::{
		Create, Filesystem, FsError, Len, Mkdir, Open, OpenMut, Pread, Pwrite, ReadDir, Rename, Result,
		SyncData, SyncDir, Truncate, Unlink,
	},
	sync::mutex::Mutex,
};

pub const SECTOR_BYTES: usize = 512;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SectorState {
	Unwritten,
	Dirty,
	Durable,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SectorMask(Vec<bool>);

impl SectorMask {
	pub fn full(sectors: usize) -> Self {
		Self(vec![true; sectors])
	}

	pub fn empty(sectors: usize) -> Self {
		Self(vec![false; sectors])
	}

	pub fn set(&mut self, sector: usize) {
		if sector < self.0.len() {
			self.0[sector] = true;
		}
	}

	pub fn clear(&mut self, sector: usize) {
		if sector < self.0.len() {
			self.0[sector] = false;
		}
	}

	pub fn is_set(&self, sector: usize) -> bool {
		self.0.get(sector).copied().unwrap_or(false)
	}

	pub fn sectors(&self) -> usize {
		self.0.len()
	}
}

pub(crate) fn sector_span(offset: u64, len: usize, sector_bytes: usize) -> usize {
	if len == 0 {
		return 0;
	}
	let first = offset as usize / sector_bytes;
	let last = (offset as usize + len - 1) / sector_bytes;
	last - first + 1
}

struct FileData {
	bytes: Vec<u8>,
	sectors: Vec<SectorState>,
	shadow: BTreeMap<usize, (SectorState, Vec<u8>)>,
}

impl FileData {
	fn new(len: usize, sector_bytes: usize) -> Self {
		Self {
			bytes: vec![0u8; len],
			sectors: vec![SectorState::Unwritten; len.div_ceil(sector_bytes)],
			shadow: BTreeMap::new(),
		}
	}

	fn resize(&mut self, len: usize, sector_bytes: usize) {
		self.bytes.resize(len, 0);
		self.sectors.resize(len.div_ceil(sector_bytes), SectorState::Unwritten);
		let sectors = self.sectors.len();
		self.shadow.retain(|sector, _| *sector < sectors);
	}

	fn dirty(&mut self, sector: usize, sector_bytes: usize) {
		if !self.shadow.contains_key(&sector) {
			let lo = sector * sector_bytes;
			let hi = (lo + sector_bytes).min(self.bytes.len());
			self.shadow.insert(sector, (self.sectors[sector], self.bytes[lo..hi].to_vec()));
		}
		self.sectors[sector] = SectorState::Dirty;
	}

	fn sync(&mut self) {
		for state in self.sectors.iter_mut() {
			if *state == SectorState::Dirty {
				*state = SectorState::Durable;
			}
		}
		self.shadow.clear();
	}

	#[cfg(any(test, feature = "testing"))]
	fn revert(&mut self, sector_bytes: usize) {
		while let Some((sector, (state, bytes))) = self.shadow.pop_first() {
			let lo = sector * sector_bytes;
			if lo >= self.bytes.len() {
				continue;
			}
			let hi = (lo + bytes.len()).min(self.bytes.len());
			self.bytes[lo..hi].copy_from_slice(&bytes[..hi - lo]);
			if sector < self.sectors.len() {
				self.sectors[sector] = state;
			}
		}
	}
}

pub(crate) struct MemoryFileState {
	sector_bytes: usize,
	data: Mutex<FileData>,
}

impl MemoryFileState {
	fn new(len: usize, sector_bytes: usize) -> Self {
		Self {
			sector_bytes,
			data: Mutex::new(FileData::new(len, sector_bytes)),
		}
	}

	fn pread(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
		let data = self.data.lock();
		let offset = offset as usize;
		if offset >= data.bytes.len() {
			return Ok(0);
		}
		let read = buf.len().min(data.bytes.len() - offset);
		buf[..read].copy_from_slice(&data.bytes[offset..offset + read]);
		Ok(read)
	}

	fn write_masked(&self, offset: u64, buf: &[u8], mask: &SectorMask) -> Result<usize> {
		if buf.is_empty() {
			return Ok(0);
		}
		let sector_bytes = self.sector_bytes;
		let offset = offset as usize;
		let end = offset + buf.len();
		let mut data = self.data.lock();
		if end > data.bytes.len() {
			data.resize(end, sector_bytes);
		}
		let first = offset / sector_bytes;
		let last = (end - 1) / sector_bytes;
		for sector in first..=last {
			if !mask.is_set(sector - first) {
				continue;
			}
			let lo = (sector * sector_bytes).max(offset);
			let hi = (sector * sector_bytes + sector_bytes).min(end);
			data.dirty(sector, sector_bytes);
			data.bytes[lo..hi].copy_from_slice(&buf[lo - offset..hi - offset]);
		}
		Ok(buf.len())
	}

	fn sync_data(&self) -> Result<()> {
		self.data.lock().sync();
		Ok(())
	}

	fn truncate(&self, len: u64) -> Result<()> {
		self.data.lock().resize(len as usize, self.sector_bytes);
		Ok(())
	}

	fn len(&self) -> Result<u64> {
		Ok(self.data.lock().bytes.len() as u64)
	}

	#[cfg(any(test, feature = "testing"))]
	fn revert(&self) {
		self.data.lock().revert(self.sector_bytes);
	}

	#[cfg(any(test, feature = "testing"))]
	fn sector_states(&self) -> Vec<SectorState> {
		self.data.lock().sectors.clone()
	}
}

struct State {
	files: BTreeMap<PathBuf, Arc<MemoryFileState>>,
	dirs: BTreeSet<PathBuf>,
	dirty_dirs: BTreeSet<PathBuf>,
	sector_bytes: usize,
}

impl Default for State {
	fn default() -> Self {
		Self {
			files: BTreeMap::new(),
			dirs: BTreeSet::from([PathBuf::from("/")]),
			dirty_dirs: BTreeSet::new(),
			sector_bytes: SECTOR_BYTES,
		}
	}
}

impl State {
	fn exists(&self, path: &Path) -> bool {
		self.files.contains_key(path) || self.dirs.contains(path)
	}

	fn parent_is_dir(&self, path: &Path) -> bool {
		match path.parent() {
			Some(parent) => self.dirs.contains(parent),
			None => false,
		}
	}
}

#[derive(Default)]
struct Inner {
	state: Mutex<State>,
}

#[derive(Clone, Default)]
pub struct MemoryFs(Arc<Inner>);

impl MemoryFs {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn with_sector_bytes(sector_bytes: usize) -> Self {
		Self(Arc::new(Inner {
			state: Mutex::new(State {
				sector_bytes,
				..State::default()
			}),
		}))
	}

	#[cfg(any(test, feature = "testing"))]
	pub fn crash(&self) {
		{
			let mut state = self.0.state.lock();
			let dirty: Vec<PathBuf> = mem::take(&mut state.dirty_dirs).into_iter().collect();
			for path in dirty {
				state.dirs.retain(|dir| !dir.starts_with(&path));
				state.files.retain(|key, _| !key.starts_with(&path));
			}
		}
		let files: Vec<Arc<MemoryFileState>> = self.0.state.lock().files.values().cloned().collect();
		for file in files {
			file.revert();
		}
	}

	#[cfg(feature = "testing")]
	pub(crate) fn detach(&self, path: &Path) -> Result<Arc<MemoryFileState>> {
		self.0.state.lock().files.remove(path).ok_or_else(|| FsError::NotFound(path.to_path_buf()))
	}

	#[cfg(feature = "testing")]
	pub(crate) fn attach(&self, path: &Path, state: Arc<MemoryFileState>) {
		self.0.state.lock().files.insert(path.to_path_buf(), state);
	}
}

pub struct MemoryFile {
	path: PathBuf,
	state: Arc<MemoryFileState>,
}

pub struct MemoryFileMut {
	path: PathBuf,
	state: Arc<MemoryFileState>,
}

impl MemoryFileMut {
	pub fn write_masked(&self, offset: u64, buf: &[u8], mask: &SectorMask) -> Result<usize> {
		self.state.write_masked(offset, buf, mask)
	}

	pub fn path(&self) -> &Path {
		&self.path
	}

	pub fn sector_bytes(&self) -> usize {
		self.state.sector_bytes
	}

	#[cfg(any(test, feature = "testing"))]
	pub fn sector_states(&self) -> Vec<SectorState> {
		self.state.sector_states()
	}
}

impl MemoryFile {
	pub fn path(&self) -> &Path {
		&self.path
	}

	pub fn sector_bytes(&self) -> usize {
		self.state.sector_bytes
	}

	#[cfg(any(test, feature = "testing"))]
	pub fn sector_states(&self) -> Vec<SectorState> {
		self.state.sector_states()
	}
}

impl Filesystem for MemoryFs {
	type File = MemoryFile;
	type FileMut = MemoryFileMut;
}

impl Mkdir for MemoryFs {
	fn mkdir(&self, path: &Path) -> Result<()> {
		let mut state = self.0.state.lock();
		if !state.parent_is_dir(path) {
			return Err(FsError::NotFound(path.to_path_buf()));
		}
		if state.exists(path) {
			return Err(FsError::AlreadyExists(path.to_path_buf()));
		}
		state.dirs.insert(path.to_path_buf());
		state.dirty_dirs.insert(path.to_path_buf());
		Ok(())
	}
}

impl Create for MemoryFs {
	fn create(&self, path: &Path, len: u64) -> Result<MemoryFileMut> {
		let mut state = self.0.state.lock();
		if !state.parent_is_dir(path) {
			return Err(FsError::NotFound(path.to_path_buf()));
		}
		if state.exists(path) {
			return Err(FsError::AlreadyExists(path.to_path_buf()));
		}
		let file = Arc::new(MemoryFileState::new(len as usize, state.sector_bytes));
		state.files.insert(path.to_path_buf(), Arc::clone(&file));
		Ok(MemoryFileMut {
			path: path.to_path_buf(),
			state: file,
		})
	}
}

impl Open for MemoryFs {
	fn open(&self, path: &Path) -> Result<MemoryFile> {
		let state = self.0.state.lock();
		state.files
			.get(path)
			.map(|file| MemoryFile {
				path: path.to_path_buf(),
				state: Arc::clone(file),
			})
			.ok_or_else(|| FsError::NotFound(path.to_path_buf()))
	}
}

impl OpenMut for MemoryFs {
	fn open_mut(&self, path: &Path) -> Result<MemoryFileMut> {
		let state = self.0.state.lock();
		state.files
			.get(path)
			.map(|file| MemoryFileMut {
				path: path.to_path_buf(),
				state: Arc::clone(file),
			})
			.ok_or_else(|| FsError::NotFound(path.to_path_buf()))
	}
}

impl ReadDir for MemoryFs {
	fn read_dir(&self, path: &Path) -> Result<Vec<PathBuf>> {
		let state = self.0.state.lock();
		if !state.dirs.contains(path) {
			if state.files.contains_key(path) {
				return Err(FsError::NotADirectory(path.to_path_buf()));
			}
			return Err(FsError::NotFound(path.to_path_buf()));
		}
		let mut entries: Vec<PathBuf> = state
			.files
			.keys()
			.chain(state.dirs.iter())
			.filter(|entry| entry.parent() == Some(path))
			.cloned()
			.collect();
		entries.sort();
		Ok(entries)
	}
}

impl Rename for MemoryFs {
	fn rename(&self, from: &Path, to: &Path) -> Result<()> {
		let mut state = self.0.state.lock();
		let Some(file) = state.files.remove(from) else {
			return Err(FsError::NotFound(from.to_path_buf()));
		};
		if !state.parent_is_dir(to) {
			state.files.insert(from.to_path_buf(), file);
			return Err(FsError::NotFound(to.to_path_buf()));
		}
		state.files.insert(to.to_path_buf(), file);
		Ok(())
	}
}

impl Unlink for MemoryFs {
	fn unlink(&self, path: &Path) -> Result<()> {
		let mut state = self.0.state.lock();
		state.files.remove(path).map(|_| ()).ok_or_else(|| FsError::NotFound(path.to_path_buf()))
	}
}

impl SyncDir for MemoryFs {
	fn sync_dir(&self, path: &Path) -> Result<()> {
		let mut state = self.0.state.lock();
		if !state.dirs.contains(path) {
			return Err(FsError::NotFound(path.to_path_buf()));
		}
		state.dirty_dirs.retain(|dirty| dirty.parent() != Some(path));
		Ok(())
	}
}

impl Pread for MemoryFile {
	fn pread(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
		self.state.pread(offset, buf)
	}
}

impl Len for MemoryFile {
	fn len(&self) -> Result<u64> {
		self.state.len()
	}
}

impl Pread for MemoryFileMut {
	fn pread(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
		self.state.pread(offset, buf)
	}
}

impl Pwrite for MemoryFileMut {
	fn pwrite(&self, offset: u64, buf: &[u8]) -> Result<usize> {
		let mask = SectorMask::full(sector_span(offset, buf.len(), self.state.sector_bytes));
		self.state.write_masked(offset, buf, &mask)
	}
}

impl SyncData for MemoryFileMut {
	fn sync_data(&self) -> Result<()> {
		self.state.sync_data()
	}
}

impl Truncate for MemoryFileMut {
	fn truncate(&self, len: u64) -> Result<()> {
		self.state.truncate(len)
	}
}

impl Len for MemoryFileMut {
	fn len(&self) -> Result<u64> {
		self.state.len()
	}
}

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

	fn read_all(file: &MemoryFileMut) -> Vec<u8> {
		let len = file.len().unwrap() as usize;
		let mut buf = vec![0u8; len];
		assert_eq!(file.pread(0, &mut buf).unwrap(), len);
		buf
	}

	#[test]
	fn create_leaves_every_sector_unwritten_and_zeroed() {
		// a fresh file owns no durable and no dirty state, so a crash must leave it exactly as created.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 1024).unwrap();
		assert_eq!(file.sector_states(), vec![SectorState::Unwritten; 2]);
		assert_eq!(read_all(&file), vec![0u8; 1024]);
	}

	#[test]
	fn write_dirties_every_grazed_sector_whole() {
		// a device cannot make half a sector durable, so a one-byte overlap must dirty the entire sector.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 2048).unwrap();
		assert_eq!(file.pwrite(500, &[7u8; 20]).unwrap(), 20);
		assert_eq!(
			file.sector_states(),
			vec![SectorState::Dirty, SectorState::Dirty, SectorState::Unwritten, SectorState::Unwritten]
		);
	}

	#[test]
	fn sync_promotes_every_dirty_sector() {
		// only a sync may turn dirty into durable; untouched sectors must stay unwritten.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 2048).unwrap();
		file.pwrite(0, &[1u8; 600]).unwrap();
		file.sync_data().unwrap();
		assert_eq!(
			file.sector_states(),
			vec![
				SectorState::Durable,
				SectorState::Durable,
				SectorState::Unwritten,
				SectorState::Unwritten
			]
		);
	}

	#[test]
	fn write_masked_skips_unmasked_sectors_but_reports_full_length() {
		// a torn write must tell the caller it succeeded while only some sectors reached the platter.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 1536).unwrap();
		let mut mask = SectorMask::empty(3);
		mask.set(0);
		mask.set(2);
		assert_eq!(file.write_masked(0, &[0xABu8; 1536], &mask).unwrap(), 1536);
		let bytes = read_all(&file);
		assert!(bytes[0..512].iter().all(|byte| *byte == 0xAB));
		assert!(bytes[512..1024].iter().all(|byte| *byte == 0));
		assert!(bytes[1024..1536].iter().all(|byte| *byte == 0xAB));
		assert_eq!(file.sector_states(), vec![SectorState::Dirty, SectorState::Unwritten, SectorState::Dirty]);
	}

	#[test]
	fn mask_ignores_out_of_range_indices() {
		// a mask shorter than the span reads as unset rather than panicking, so no fault plan aborts a run.
		let mut mask = SectorMask::empty(2);
		mask.set(5);
		assert_eq!(mask.sectors(), 2);
		assert!(!mask.is_set(5));
		let mut full = SectorMask::full(2);
		full.clear(1);
		assert!(full.is_set(0));
		assert!(!full.is_set(1));
	}

	#[test]
	fn pread_round_trips_clamps_and_stops_at_the_end() {
		// a short read at the end must report the count copied, and a read past the end is not an error.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 10).unwrap();
		file.pwrite(0, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).unwrap();
		let mut buf = [0u8; 4];
		assert_eq!(file.pread(2, &mut buf).unwrap(), 4);
		assert_eq!(buf, [3, 4, 5, 6]);
		let mut tail = [0u8; 16];
		assert_eq!(file.pread(5, &mut tail).unwrap(), 5);
		assert_eq!(&tail[..5], &[6, 7, 8, 9, 10]);
		assert_eq!(file.pread(10, &mut tail).unwrap(), 0);
		assert_eq!(file.pread(9999, &mut tail).unwrap(), 0);
	}

	#[test]
	fn write_past_the_end_grows_bytes_and_sectors_together() {
		// the sector vector must track the byte vector, or a crash would index a sector that does not exist.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 0).unwrap();
		assert!(file.sector_states().is_empty());
		assert_eq!(file.pwrite(1000, &[9u8; 10]).unwrap(), 10);
		assert_eq!(file.len().unwrap(), 1010);
		assert_eq!(file.sector_states(), vec![SectorState::Unwritten, SectorState::Dirty]);
		assert_eq!(fs.open(Path::new("/a")).unwrap().sector_states().len(), 2);
	}

	#[test]
	fn truncate_shortens_bytes_and_sector_states_together() {
		// a shrink must drop the sectors it removed, otherwise a crash would restore bytes past the new end.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 2048).unwrap();
		file.pwrite(0, &[3u8; 2048]).unwrap();
		file.truncate(600).unwrap();
		assert_eq!(file.len().unwrap(), 600);
		assert_eq!(file.sector_states().len(), 2);
		file.truncate(100).unwrap();
		assert_eq!(file.sector_states().len(), 1);
		assert_eq!(read_all(&file), vec![3u8; 100]);
		file.truncate(1024).unwrap();
		assert_eq!(file.sector_states(), vec![SectorState::Dirty, SectorState::Unwritten]);
	}

	#[test]
	fn crash_reverts_dirty_sectors_to_their_prior_bytes_and_state() {
		// an unsynced write must vanish exactly, leaving the sector in the state it held before it went dirty.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 1024).unwrap();
		file.pwrite(0, &[1u8; 512]).unwrap();
		file.sync_data().unwrap();
		file.pwrite(0, &[2u8; 512]).unwrap();
		file.pwrite(512, &[2u8; 512]).unwrap();
		fs.crash();
		let bytes = read_all(&file);
		assert!(bytes[0..512].iter().all(|byte| *byte == 1));
		assert!(bytes[512..1024].iter().all(|byte| *byte == 0));
		assert_eq!(file.sector_states(), vec![SectorState::Durable, SectorState::Unwritten]);
	}

	#[test]
	fn crash_after_truncate_drops_shadow_beyond_the_new_end() {
		// a shadow entry past the new end must not be replayed, or the crash would resurrect truncated bytes.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 2048).unwrap();
		file.pwrite(1536, &[8u8; 512]).unwrap();
		file.truncate(512).unwrap();
		fs.crash();
		assert_eq!(file.len().unwrap(), 512);
		assert_eq!(file.sector_states(), vec![SectorState::Unwritten]);
	}

	#[test]
	fn sync_clears_the_shadow_so_a_later_crash_keeps_the_data() {
		// once a sync covers a sector the bytes are durable, so a crash after it must change nothing.
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 512).unwrap();
		file.pwrite(0, &[5u8; 512]).unwrap();
		file.sync_data().unwrap();
		fs.crash();
		assert_eq!(read_all(&file), vec![5u8; 512]);
		assert_eq!(file.sector_states(), vec![SectorState::Durable]);
	}

	#[test]
	fn sector_size_is_configurable_and_consistent() {
		// the sector size decides the blast radius of a torn write, so every handle must agree on it.
		let fs = MemoryFs::with_sector_bytes(64);
		let file = fs.create(Path::new("/a"), 100).unwrap();
		assert_eq!(file.sector_bytes(), 64);
		assert_eq!(file.sector_states().len(), 2);
		file.pwrite(0, &[1u8; 100]).unwrap();
		assert_eq!(file.sector_states(), vec![SectorState::Dirty, SectorState::Dirty]);

		let wide = MemoryFs::new();
		let file = wide.create(Path::new("/a"), 100).unwrap();
		assert_eq!(file.sector_bytes(), SECTOR_BYTES);
		file.pwrite(0, &[1u8; 100]).unwrap();
		assert_eq!(file.sector_states(), vec![SectorState::Dirty]);
	}

	#[test]
	fn mkdir_needs_a_parent_directory_and_rejects_an_existing_path() {
		// a tree that accepts an orphan path lets a walk see files under a parent that never existed.
		let fs = MemoryFs::new();
		assert_eq!(fs.mkdir(Path::new("/a/b")), Err(FsError::NotFound(PathBuf::from("/a/b"))));
		fs.mkdir(Path::new("/a")).unwrap();
		assert_eq!(fs.mkdir(Path::new("/a")), Err(FsError::AlreadyExists(PathBuf::from("/a"))));
		fs.create(Path::new("/a/f"), 0).unwrap();
		assert_eq!(fs.mkdir(Path::new("/a/f")), Err(FsError::AlreadyExists(PathBuf::from("/a/f"))));
	}

	#[test]
	fn create_needs_a_parent_directory_and_rejects_an_existing_path() {
		let fs = MemoryFs::new();
		assert!(fs.create(Path::new("/a/f"), 0).is_err());
		fs.mkdir(Path::new("/a")).unwrap();
		fs.create(Path::new("/a/f"), 0).unwrap();
		assert_eq!(fs.create(Path::new("/a/f"), 0).err(), Some(FsError::AlreadyExists(PathBuf::from("/a/f"))));
	}

	#[test]
	fn open_and_open_mut_report_a_missing_file() {
		let fs = MemoryFs::new();
		assert_eq!(fs.open(Path::new("/a")).err(), Some(FsError::NotFound(PathBuf::from("/a"))));
		assert_eq!(fs.open_mut(Path::new("/a")).err(), Some(FsError::NotFound(PathBuf::from("/a"))));
		fs.create(Path::new("/a"), 8).unwrap();
		let file = fs.open(Path::new("/a")).unwrap();
		assert_eq!(file.path(), Path::new("/a"));
		assert_eq!(file.len().unwrap(), 8);
		assert_eq!(file.sector_bytes(), SECTOR_BYTES);
		assert_eq!(file.sector_states(), vec![SectorState::Unwritten]);
	}

	#[test]
	fn open_shares_the_state_with_the_creating_handle() {
		// two handles onto one path are one file, so a write through either must be visible through the other.
		let fs = MemoryFs::new();
		let writer = fs.create(Path::new("/a"), 512).unwrap();
		writer.pwrite(0, &[4u8; 8]).unwrap();
		let reader = fs.open(Path::new("/a")).unwrap();
		let mut buf = [0u8; 8];
		assert_eq!(reader.pread(0, &mut buf).unwrap(), 8);
		assert_eq!(buf, [4u8; 8]);
		assert_eq!(reader.sector_states(), vec![SectorState::Dirty]);
	}

	#[test]
	fn read_dir_lists_direct_children_in_ascending_order() {
		// the walk order feeds seed replay, so it must be sorted rather than whatever a hash gives back.
		let fs = MemoryFs::new();
		fs.mkdir(Path::new("/d")).unwrap();
		fs.mkdir(Path::new("/d/sub")).unwrap();
		fs.create(Path::new("/d/c"), 0).unwrap();
		fs.create(Path::new("/d/a"), 0).unwrap();
		fs.create(Path::new("/d/sub/deep"), 0).unwrap();
		assert_eq!(
			fs.read_dir(Path::new("/d")).unwrap(),
			vec![PathBuf::from("/d/a"), PathBuf::from("/d/c"), PathBuf::from("/d/sub")]
		);
		assert_eq!(fs.read_dir(Path::new("/nope")).err(), Some(FsError::NotFound(PathBuf::from("/nope"))));
		assert_eq!(fs.read_dir(Path::new("/d/a")).err(), Some(FsError::NotADirectory(PathBuf::from("/d/a"))));
	}

	#[test]
	fn rename_moves_the_entry_and_keeps_the_contents() {
		let fs = MemoryFs::new();
		let file = fs.create(Path::new("/a"), 512).unwrap();
		file.pwrite(0, &[6u8; 4]).unwrap();
		fs.rename(Path::new("/a"), Path::new("/b")).unwrap();
		assert_eq!(fs.open(Path::new("/a")).err(), Some(FsError::NotFound(PathBuf::from("/a"))));
		let moved = fs.open(Path::new("/b")).unwrap();
		let mut buf = [0u8; 4];
		assert_eq!(moved.pread(0, &mut buf).unwrap(), 4);
		assert_eq!(buf, [6u8; 4]);
		assert_eq!(
			fs.rename(Path::new("/a"), Path::new("/c")).err(),
			Some(FsError::NotFound(PathBuf::from("/a")))
		);
	}

	#[test]
	fn rename_into_a_missing_directory_leaves_the_source_in_place() {
		// a failed rename must not lose the file, or recovery would find neither name.
		let fs = MemoryFs::new();
		fs.create(Path::new("/a"), 0).unwrap();
		assert_eq!(
			fs.rename(Path::new("/a"), Path::new("/d/b")).err(),
			Some(FsError::NotFound(PathBuf::from("/d/b")))
		);
		assert!(fs.open(Path::new("/a")).is_ok());
	}

	#[test]
	fn unlink_removes_the_entry_or_reports_it_missing() {
		let fs = MemoryFs::new();
		fs.create(Path::new("/a"), 0).unwrap();
		fs.unlink(Path::new("/a")).unwrap();
		assert_eq!(fs.unlink(Path::new("/a")).err(), Some(FsError::NotFound(PathBuf::from("/a"))));
	}

	#[test]
	fn sync_dir_checks_the_directory_exists() {
		let fs = MemoryFs::new();
		fs.sync_dir(Path::new("/")).unwrap();
		assert_eq!(fs.sync_dir(Path::new("/d")).err(), Some(FsError::NotFound(PathBuf::from("/d"))));
		fs.mkdir(Path::new("/d")).unwrap();
		fs.sync_dir(Path::new("/d")).unwrap();
	}

	#[test]
	fn a_cloned_handle_shares_one_filesystem() {
		// the wasm build hands the same disk to several owners, so a clone must not fork the namespace.
		let fs = MemoryFs::new();
		let clone = fs.clone();
		clone.mkdir(Path::new("/d")).unwrap();
		assert_eq!(fs.read_dir(Path::new("/")).unwrap(), vec![PathBuf::from("/d")]);
	}
}