Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/tests/lone_index.rs

23.8 KiB, 1 run

created by r1870400018:72468, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! An index standing without its data file is judged at the open (2026-10-11). A collection
2//! deletes a whole file only when every record in it is old, by two unlinks. Builds before this
3//! day removed the data file first, so a crash between the two left the index alone, and the open
4//! refused the store as if the data had been lost. A store opened by this build completes such a
5//! delete, rebuilds an index removed first, and refuses only when the index names a record nothing
6//! else holds, saying which and what to do.
7
8mod gc_pair;
9
10use gc_pair::*;
11
12use oxedyne_fe2o3_core::{
13 prelude::*,
14 alt::Override,
15};
16use oxedyne_fe2o3_crypto::enc::EncryptionScheme;
17use oxedyne_fe2o3_hash::{
18 csum::ChecksumScheme,
19 hash::HashScheme,
20};
21use oxedyne_fe2o3_iop_db::api::{
22 Database,
23 RestSchemesOverride,
24};
25use oxedyne_fe2o3_jdat::prelude::*;
26use oxedyne_fe2o3_o3db_sync::{
27 O3db,
28 api::OzoneApi,
29 base::cfg::OzoneConfig,
30 data::core::RestSchemesInput,
31 file::core::FileType,
32 test::{
33 hooks,
34 setup::{
35 self,
36 Uid,
37 UID_LEN,
38 },
39 },
40};
41
42use std::{
43 fs,
44 path::{
45 Path,
46 PathBuf,
47 },
48 sync::{
49 Mutex,
50 MutexGuard,
51 },
52};
53
54// The test hooks are process-wide, and a head or retire dropped for one test would drop another's
55// running beside it, so every test here runs alone.
56static HOOKS: Mutex<()> = Mutex::new(());
57
58fn serial() -> MutexGuard<'static, ()> {
59 match HOOKS.lock() { Ok(g) => g, Err(p) => p.into_inner() }
60}
61
62fn fresh(dir: &str) -> Outcome<PathBuf> {
63 let _ = fs::remove_dir_all(dir);
64 res!(fs::create_dir_all(dir));
65 Ok(res!(Path::new(dir).canonicalize()))
66}
67
68// Collection is off throughout, so the files stay as the test leaves them.
69fn open(root: &Path, cfg: &OzoneConfig, wipe: bool) -> Outcome<TestDb> {
70 log_set_level!("error");
71 setup::start_db(root.to_path_buf(), Some(cfg.clone()), schemes(), None, false, wipe)
72}
73
74// Five records to a file, written at version 1 and then all overwritten at version 2, so files 1
75// and 2 hold only old records: the files a collection deletes whole.
76fn superseded(name: &str) -> Outcome<(PathBuf, OzoneConfig)> {
77 let root = res!(fresh(&fmt!("./test_db_lone_index_{}", name)));
78 let probe = res!(probe(&fmt!("./test_db_lone_index_{}_probe", name)));
79 let cfg = res!(config(5 * probe.len + probe.len / 2));
80 let db = res!(open(&root, &cfg, true));
81 res!(fill(&db));
82 for i in 0..NKEYS {
83 res!(db.insert(key(i), value(i, 2), Uid::default(), None));
84 }
85 assert_eq!(judge(&db, &[2u8; NKEYS], "before the cut"), 0);
86 res!(db.close());
87 for n in 1..3 {
88 assert!(file(&root, &cfg, FileType::Data, n).is_file()
89 && size(&file(&root, &cfg, FileType::Index, n)) > 0,
90 "positive control: file {} stands whole with its index", n);
91 }
92 Ok((root, cfg))
93}
94
95/// The order of builds before 2026-10-11: the data file of an all-old pair was removed and the
96/// crash came before its index went. The open completes the delete.
97#[test]
98fn the_old_order_completes_the_delete() -> Outcome<()> {
99 let _lock = serial();
100 let (root, cfg) = res!(superseded("old_order"));
101 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
102 res!(fs::remove_file(&d1));
103 let db = match open(&root, &cfg, false) {
104 Ok(db) => db,
105 Err(e) => return Err(err!(e, "An interrupted delete of an all-old file was refused."; Test)),
106 };
107 assert!(!i1.exists(), "the index of the deleted data file {:?} still stands", i1);
108 assert!(!d1.exists());
109 assert_eq!(judge(&db, &[2u8; NKEYS], "after the completed delete"), 0);
110 // The store goes on as usual, and opens again.
111 res!(db.insert(key(0), value(0, 3), Uid::default(), None));
112 res!(db.close());
113 let db = res!(open(&root, &cfg, false));
114 let mut want = [2u8; NKEYS];
115 want[0] = 3;
116 assert_eq!(judge(&db, &want, "after a second open"), 0);
117 res!(db.close());
118 let _ = fs::remove_dir_all(&root);
119 Ok(())
120}
121
122/// The present order: the index went first and the crash came before the data file. The index
123/// is rebuilt from the data and the old records stay old.
124#[test]
125fn the_current_order_rebuilds_the_index() -> Outcome<()> {
126 let _lock = serial();
127 let (root, cfg) = res!(superseded("current_order"));
128 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
129 res!(fs::remove_file(&i1));
130 let db = res!(open(&root, &cfg, false));
131 assert!(d1.is_file(), "the data file {:?} is gone", d1);
132 assert!(size(&i1) > 0, "the index {:?} was not rebuilt", i1);
133 assert_eq!(judge(&db, &[2u8; NKEYS], "after the rebuild"), 0);
134 res!(db.close());
135 let _ = fs::remove_dir_all(&root);
136 Ok(())
137}
138
139/// A data file really lost: its index names records nothing else holds. The open refuses and
140/// says which index, which record and which data file, and what to do. Taking the way out it
141/// names opens the store without those records.
142#[test]
143fn a_lone_index_naming_a_current_record_refuses() -> Outcome<()> {
144 let _lock = serial();
145 let root = res!(fresh("./test_db_lone_index_loss"));
146 let probe = res!(probe("./test_db_lone_index_loss_probe"));
147 let cfg = res!(config(5 * probe.len + probe.len / 2));
148 let db = res!(open(&root, &cfg, true));
149 res!(fill(&db));
150 res!(db.close());
151 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
152 res!(fs::remove_file(&d1));
153 let e = match open(&root, &cfg, false) {
154 Ok(db) => {
155 let bad = judge(&db, &[1u8; NKEYS], "opened without a data file");
156 let _ = db.close();
157 return Err(err!("The store opened with {:?} lost; {} keys read wrong.", d1, bad; Test));
158 },
159 Err(e) => fmt!("{}", e),
160 };
161 println!("Refused: {}", e);
162 assert!(e.contains(&fmt!("{:?}", i1)), "the refusal does not name the index {:?}: {}", i1, e);
163 assert!(e.contains(&fmt!("{:?}", d1)), "the refusal does not name the data file {:?}: {}", d1, e);
164 assert!(e.contains("is missing"), "{}", e);
165 assert!(e.contains("gc pair key 00"), "the refusal does not name the key: {}", e);
166 assert!(e.contains("(record "), "the refusal does not name the record: {}", e);
167 assert!(e.contains("backup or a replica"), "the refusal does not say to restore: {}", e);
168 assert!(e.contains("remove the index file"), "the refusal does not give the last resort: {}", e);
169 assert!(size(&i1) > 0, "a refused open removed the index {:?}", i1);
170
171 // The way out: losing those records is accepted, so the index goes.
172 res!(fs::remove_file(&i1));
173 let db = res!(open(&root, &cfg, false));
174 for i in 0..NKEYS {
175 let got = res!(db.get(&key(i), None));
176 match i < 5 {
177 true => assert!(got.is_none(), "key {} of the lost file reads back", i),
178 false => assert!(matches!(got, Some((ref v, _)) if *v == value(i, 1)), "key {} is wrong", i),
179 }
180 }
181 res!(db.close());
182 let _ = fs::remove_dir_all(&root);
183 Ok(())
184}
185
186/// The chunks of a value since overwritten, in a file deleted whole whose index outlived it.
187/// Nothing names them, so the delete is completed.
188#[test]
189fn chunks_of_a_retired_set_complete_the_delete() -> Outcome<()> {
190 let _lock = serial();
191 let root = res!(fresh("./test_db_lone_index_chunks"));
192 let mut cfg = res!(config(2_000));
193 cfg.rest_chunk_threshold = 1_000;
194 cfg.rest_chunk_bytes = 400;
195 let db = res!(open(&root, &cfg, true));
196 let big = dat!("lone index big");
197 res!(db.insert(big.clone(), Dat::BU32(vec![0xa7; 3_000]), Uid::default(), None));
198 res!(db.insert(big.clone(), Dat::BU32(vec![0x5c; 200]), Uid::default(), None));
199 for i in 0..NKEYS {
200 res!(db.insert(key(i), value(i, 1), Uid::default(), None));
201 }
202 res!(db.close());
203 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
204 let chunky = files_holding(&root, &cfg, &[0xa7u8; 64]);
205 assert!(chunky.contains(&d1), "positive control: file 1 holds chunks of the first value: {:?}", chunky);
206 assert!(!files_holding(&root, &cfg, &[0x5cu8; 64]).contains(&d1)
207 && !files_holding(&root, &cfg, b"gc pair key").contains(&d1),
208 "positive control: file 1 holds nothing current");
209 res!(fs::remove_file(&d1));
210 let db = match open(&root, &cfg, false) {
211 Ok(db) => db,
212 Err(e) => return Err(err!(e, "An interrupted delete of retired chunks was refused."; Test)),
213 };
214 assert!(!i1.exists(), "the index {:?} of the retired chunks still stands", i1);
215 let got = res!(db.get(&big, None));
216 assert!(matches!(got, Some((Dat::BU32(ref v), _)) if *v == vec![0x5c; 200]), "the value of the big key is wrong");
217 assert_eq!(judge(&db, &[1u8; NKEYS], "after the completed delete"), 0);
218 res!(db.close());
219 let _ = fs::remove_dir_all(&root);
220 Ok(())
221}
222
223/// The number of an index standing alone at the survey is not handed out again as a live file,
224/// even once the judgement has removed it: until then the old index would have named the new
225/// file's records. Here the index is put above every data file, so a counter seeded from the data
226/// files alone gives its number to the first live file the store rolls over to.
227#[test]
228fn a_lone_index_number_is_not_reused() -> Outcome<()> {
229 let _lock = serial();
230 let (root, cfg) = res!(superseded("reuse"));
231 let mut top = 0;
232 for n in 1..40 {
233 if file(&root, &cfg, FileType::Data, n).is_file() {
234 top = n;
235 }
236 }
237 let lone = top + 1;
238 res!(fs::remove_file(file(&root, &cfg, FileType::Data, 1)));
239 res!(fs::rename(file(&root, &cfg, FileType::Index, 1), file(&root, &cfg, FileType::Index, lone)));
240 let db = res!(open(&root, &cfg, false));
241 assert!(!file(&root, &cfg, FileType::Index, lone).exists(), "the lone index {} still stands", lone);
242 for i in 0..NKEYS {
243 res!(db.insert(key(i), value(i, 3), Uid::default(), None));
244 }
245 assert_eq!(judge(&db, &[3u8; NKEYS], "after the rollovers"), 0);
246 res!(db.close());
247 assert!(file(&root, &cfg, FileType::Data, lone + 1).is_file(), "positive control: the store rolled over past {}", lone);
248 assert!(!file(&root, &cfg, FileType::Data, lone).exists(),
249 "a live file took the number {} of the index that stood alone", lone);
250 let _ = fs::remove_dir_all(&root);
251 Ok(())
252}
253
254/// Completing the delete can itself fail, here at the unlink of the index in a directory the
255/// process may not write. The refusal names the index, the data file and the phase, and says
256/// that nothing was changed; once the cause is fixed the open completes.
257#[test]
258fn a_failed_unlink_names_the_phase() -> Outcome<()> {
259 let _lock = serial();
260 use std::os::unix::fs::PermissionsExt;
261 let (root, cfg) = res!(superseded("unlink"));
262 let (d1, i1) = (file(&root, &cfg, FileType::Data, 1), file(&root, &cfg, FileType::Index, 1));
263 res!(fs::remove_file(&d1));
264 let zdir = res!(i1.parent().map(Path::to_path_buf).ok_or_else(|| err!("No zone directory."; Test)));
265 res!(fs::set_permissions(&zdir, fs::Permissions::from_mode(0o555)));
266 // Root writes regardless, so the test has nothing to show there.
267 let canary = zdir.join("lone_index_canary.tmp");
268 if fs::write(&canary, b"x").is_ok() {
269 let _ = fs::remove_file(&canary);
270 res!(fs::set_permissions(&zdir, fs::Permissions::from_mode(0o755)));
271 let _ = fs::remove_dir_all(&root);
272 msg!("Skipped: this process writes a read-only directory.");
273 return Ok(());
274 }
275 let opened = open(&root, &cfg, false);
276 res!(fs::set_permissions(&zdir, fs::Permissions::from_mode(0o755)));
277 let e = match opened {
278 Ok(db) => {
279 let _ = db.close();
280 return Err(err!("The store opened though the index {:?} could not be unlinked.", i1; Test));
281 },
282 Err(e) => fmt!("{}", e),
283 };
284 println!("Refused: {}", e);
285 assert!(e.contains(&fmt!("{:?}", i1)), "the refusal does not name the index {:?}: {}", i1, e);
286 assert!(e.contains(&fmt!("{:?}", d1)), "the refusal does not name the data file {:?}: {}", d1, e);
287 assert!(e.contains("unlink"), "the refusal does not name the phase: {}", e);
288 assert!(e.contains("fix the cause and reopen"), "the refusal does not give the way out: {}", e);
289 assert!(size(&i1) > 0 && !d1.exists(), "the refused open changed the files");
290 let db = match open(&root, &cfg, false) {
291 Ok(db) => db,
292 Err(e) => return Err(err!(e, "The open did not complete once the directory was writable."; Test)),
293 };
294 assert!(!i1.exists(), "the index {:?} still stands", i1);
295 assert_eq!(judge(&db, &[2u8; NKEYS], "after the completed delete"), 0);
296 res!(db.close());
297 let _ = fs::remove_dir_all(&root);
298 Ok(())
299}
300
301type EncDb = O3db<{ UID_LEN }, Uid, EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>;
302
303// A store encrypted by default with key A, the override that writes with key B, and its config:
304// files of 2000 bytes, values over 1000 bytes in chunks of 400.
305fn encrypted() -> Outcome<(OzoneConfig, RestSchemesInput<EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>,
306 RestSchemesOverride<EncryptionScheme, HashScheme>)>
307{
308 let mut cfg = res!(config(2_000));
309 cfg.rest_chunk_threshold = 1_000;
310 cfg.rest_chunk_bytes = 400;
311 let a = res!(EncryptionScheme::new_aes_256_gcm_with_key(&[0x0a; 32]));
312 let b = res!(EncryptionScheme::new_aes_256_gcm_with_key(&[0x0b; 32]));
313 let input = RestSchemesInput::new(Some(a), None::<HashScheme>, None::<HashScheme>,
314 Some(ChecksumScheme::new_crc32()));
315 let ovr = RestSchemesOverride::default().set_encrypter(Override::Default(b));
316 Ok((cfg, input, ovr))
317}
318
319fn open_enc(
320 root: &Path,
321 cfg: &OzoneConfig,
322 input: &RestSchemesInput<EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>,
323 wipe: bool,
324)
325 -> Outcome<EncDb>
326{
327 log_set_level!("error");
328 setup::start_db(root.to_path_buf(), Some(cfg.clone()), input.clone(), None, false, wipe)
329}
330
331fn part_key(db: &EncDb, k: &Dat, ovr: Option<&RestSchemesOverride<EncryptionScheme, HashScheme>>)
332 -> Outcome<[u64; 5]>
333{
334 match res!(db.api().get_head_wait(k, ovr)) {
335 Some((Dat::Tup5u64(t), _)) => Ok(t),
336 got => Err(err!("Key {:?} reads {:?}, not a part key.", k, got; Test)),
337 }
338}
339
340fn chunk_key_bytes(pk: &[u64; 5], n: u64) -> Outcome<Vec<u8>> {
341 Dat::Tup5u64([pk[0], n, pk[2], pk[3], pk[4]]).as_bytes()
342}
343
344fn data_files_holding(root: &Path, cfg: &OzoneConfig, needle: &[u8]) -> Vec<PathBuf> {
345 files_holding(root, cfg, needle).into_iter()
346 .filter(|p| p.extension().map_or(false, |x| x == "dat"))
347 .collect()
348}
349
350fn secret_value() -> Dat { Dat::BU32((0..3_000u32).map(|i| (i % 241) as u8).collect()) }
351
352const PADS: usize = 20;
353
354/// A lone index holding a retired chunk, in a store where another key's chunked value is written
355/// with a key the store does not hold by default, so its head cannot be read at the open. Only
356/// the heads stamped with the lone chunk's metadata are read, so that value is never looked at and
357/// the delete is completed (review F3: the open used to read every value and refuse).
358#[test]
359fn an_unreadable_value_elsewhere_does_not_block_the_delete() -> Outcome<()> {
360 let _lock = serial();
361 let root = res!(fresh("./test_db_lone_index_unreadable"));
362 let (cfg, input, ovr) = res!(encrypted());
363 let db = res!(open_enc(&root, &cfg, &input, true));
364 let (big, secret) = (dat!("lone index big"), dat!("lone index secret"));
365 res!(db.insert(big.clone(), Dat::BU32(vec![0xa7; 3_000]), Uid::default(), None));
366 let pk1 = res!(part_key(&db, &big, None));
367 for i in 0..PADS {
368 res!(db.insert(key(i), value(i, 1), Uid::default(), None));
369 }
370 // The retire of the displaced chunks is dropped, as a crash after the head would drop it, so
371 // nothing newer stands at their keys and only the heads can tell they are not named.
372 // The cache bot sends the retire after the insert returns, so the drop stays on to the close;
373 // the other tests of this binary retire nothing they rely on.
374 hooks::set_retire_drop(true);
375 res!(db.insert(big.clone(), Dat::BU32(vec![0x5c; 200]), Uid::default(), None));
376 res!(db.insert(secret.clone(), secret_value(), Uid::default(), Some(&ovr)));
377 let sk = res!(part_key(&db, &secret, Some(&ovr)));
378 for i in 0..PADS {
379 res!(db.insert(key(i), value(i, 2), Uid::default(), None));
380 }
381 let closed = db.close();
382 hooks::set_retire_drop(false);
383 res!(closed);
384
385 let target = data_files_holding(&root, &cfg, &res!(chunk_key_bytes(&pk1, pk1[3])));
386 assert_eq!(target.len(), 1, "positive control: one data file holds the last chunk of the first value, and no \
387 tombstone retired it: {:?}", target);
388 let d = target[0].clone();
389 let i = d.with_extension("ind");
390 let mut current = data_files_holding(&root, &cfg, &res!(secret.as_bytes()));
391 for n in 1..(sk[3] + 1) {
392 current.extend(data_files_holding(&root, &cfg, &res!(chunk_key_bytes(&sk, n))));
393 }
394 // The current head of the big key is the last of its records written.
395 current.extend(data_files_holding(&root, &cfg, &res!(big.as_bytes())).into_iter().max());
396 assert!(!current.contains(&d), "positive control: {:?} holds a current record", d);
397 assert!(size(&i) > 0, "positive control: the index {:?} stands", i);
398 res!(fs::remove_file(&d));
399
400 let db = match open_enc(&root, &cfg, &input, false) {
401 Ok(db) => db,
402 Err(e) => return Err(err!(e, "An interrupted delete was refused over a value it does not touch."; Test)),
403 };
404 assert!(!i.exists(), "the index {:?} still stands", i);
405 let got = res!(db.get(&big, None));
406 assert!(matches!(got, Some((Dat::BU32(ref v), _)) if *v == vec![0x5c; 200]), "the value of the big key is wrong");
407 let got = res!(db.api().fetch_chunks(&Dat::Tup5u64(res!(part_key(&db, &secret, Some(&ovr)))), Some(&ovr)));
408 assert!(got == secret_value(), "the secret value reads wrong");
409 for i in 0..PADS {
410 let got = res!(db.get(&key(i), None));
411 assert!(matches!(got, Some((ref v, _)) if *v == value(i, 2)), "pad {} reads wrong", i);
412 }
413 res!(db.close());
414 let _ = fs::remove_dir_all(&root);
415 Ok(())
416}
417
418/// The guard on that narrowing: a lone index holding a chunk of a current value whose head cannot
419/// be read. The head carries the chunk's own stamp, so it is read, fails, and the open refuses.
420#[test]
421fn an_unreadable_head_of_the_chunks_own_write_refuses() -> Outcome<()> {
422 let _lock = serial();
423 let root = res!(fresh("./test_db_lone_index_guard"));
424 let (cfg, input, ovr) = res!(encrypted());
425 let db = res!(open_enc(&root, &cfg, &input, true));
426 let secret = dat!("lone index secret");
427 res!(db.insert(secret.clone(), secret_value(), Uid::default(), Some(&ovr)));
428 let sk = res!(part_key(&db, &secret, Some(&ovr)));
429 for ver in 1..3 {
430 for i in 0..PADS {
431 res!(db.insert(key(i), value(i, ver), Uid::default(), None));
432 }
433 }
434 res!(db.close());
435
436 let heads = data_files_holding(&root, &cfg, &res!(secret.as_bytes()));
437 let mut target = None;
438 for n in (1..(sk[3] + 1)).rev() {
439 if let Some(d) = data_files_holding(&root, &cfg, &res!(chunk_key_bytes(&sk, n)))
440 .into_iter().find(|d| !heads.contains(d))
441 {
442 target = Some(d);
443 break;
444 }
445 }
446 let d = res!(target.ok_or_else(|| err!("Positive control: no data file holds a chunk of the \
447 secret apart from its head."; Test)));
448 let i = d.with_extension("ind");
449 res!(fs::remove_file(&d));
450
451 let e = match open_enc(&root, &cfg, &input, false) {
452 Ok(db) => {
453 let _ = db.close();
454 return Err(err!("The store opened with a chunk of a current value lost in {:?}.", d; Test));
455 },
456 Err(e) => fmt!("{}", e),
457 };
458 println!("Refused: {}", e);
459 assert!(e.contains("cannot be read"), "the refusal does not say the head cannot be read: {}", e);
460 assert!(e.contains(&fmt!("{:?}", i)), "the refusal does not name the index {:?}: {}", i, e);
461 assert!(size(&i) > 0, "a refused open removed the index {:?}", i);
462 let _ = fs::remove_dir_all(&root);
463 Ok(())
464}
465
466// A chunked value written as the builds of 2026-09 wrote it, its set taken from the key alone, and
467// rewritten at the same length, so that both writes use one set of chunk keys. With `torn` the
468// rewrite stops after its chunks, so the head of the first write names the second write's chunks.
469// The data file holding the rewrite's first chunk is then removed, as the old delete order leaves
470// it. The open must refuse either way: the chunk is named by the current head.
471fn legacy_rewrite(name: &str, torn: bool) -> Outcome<()> {
472 let root = res!(fresh(&fmt!("./test_db_lone_index_{}", name)));
473 let mut cfg = res!(config(2_000));
474 cfg.rest_chunk_threshold = 1_000;
475 cfg.rest_chunk_bytes = 400;
476 let db = res!(open(&root, &cfg, true));
477 let k = dat!("lone index legacy");
478 let (kbuf, _, _) = res!(db.api().ozone_key_dat(&k, None));
479 let legacy = OzoneApi::<{ UID_LEN }, Uid, (), HashScheme, HashScheme, ChecksumScheme>::legacy_chunk_set_id(&kbuf);
480 let resp = db.api().responder();
481 res!(db.api().store_dat_using_responder_forcing_set_id(
482 k.clone(), Dat::BU32(vec![0x11; 3_000]), Uid::default(), None, resp.clone(), legacy));
483 res!(resp.recv_store_ack());
484 let pk = match res!(db.api().get_head_wait(&k, None)) {
485 Some((Dat::Tup5u64(t), _)) => t,
486 got => return Err(err!("The legacy write reads {:?}, not a part key.", got; Test)),
487 };
488 assert_eq!(pk[0], legacy, "positive control: the first write is not under the legacy set");
489 for i in 0..PADS {
490 res!(db.insert(key(i), value(i, 1), Uid::default(), None));
491 }
492 hooks::set_head_drop(torn);
493 let resp = db.api().responder();
494 let second = db.api().store_dat_using_responder_forcing_set_id(
495 k.clone(), Dat::BU32(vec![0x22; 3_000]), Uid::default(), None, resp.clone(), legacy)
496 .and_then(|_| resp.recv_store_ack().map(|_| ()));
497 hooks::set_head_drop(false);
498 assert_eq!(second.is_err(), torn, "positive control: the rewrite failed {} where torn is {}", second.is_err(), torn);
499 for i in 0..PADS {
500 res!(db.insert(key(i), value(i, 2), Uid::default(), None));
501 }
502 res!(db.close());
503
504 let holding = data_files_holding(&root, &cfg, &res!(chunk_key_bytes(&pk, 1)));
505 assert_eq!(holding.len(), 2, "positive control: each write put its first chunk in a file of its own: {:?}", holding);
506 let d = res!(holding.iter().max().cloned().ok_or_else(|| err!("No file."; Test)));
507 let i = d.with_extension("ind");
508 assert!(!data_files_holding(&root, &cfg, &res!(k.as_bytes())).contains(&d) || !torn,
509 "positive control: {:?} holds a head of the key", d);
510 res!(fs::remove_file(&d));
511
512 let e = match open(&root, &cfg, false) {
513 Ok(db) => {
514 let got = db.get(&k, None);
515 let _ = db.close();
516 return Err(err!("The store opened with a chunk of the current value lost in {:?} (torn {}); \
517 the key reads {:?}.", d, torn, got.map(|o| o.is_some()); Test));
518 },
519 Err(e) => fmt!("{}", e),
520 };
521 println!("Refused: {}", e);
522 assert!(e.contains("a chunk of a current value"), "the refusal does not say the chunk is current: {}", e);
523 assert!(e.contains(&fmt!("{:?}", i)), "the refusal does not name the index {:?}: {}", i, e);
524 let _ = fs::remove_dir_all(&root);
525 Ok(())
526}
527
528/// A torn rewrite under a key-derived set leaves the current head stamped by the first write and
529/// its chunks by the second. The head is found by its key, and the open refuses (2026-10-11).
530#[test]
531fn a_torn_legacy_rewrite_refuses() -> Outcome<()> {
532 let _lock = serial();
533 legacy_rewrite("legacy_torn", true)
534}
535
536/// The control: the rewrite completes, so its head carries its chunks' stamp.
537#[test]
538fn a_whole_legacy_rewrite_refuses() -> Outcome<()> {
539 let _lock = serial();
540 legacy_rewrite("legacy_whole", false)
541}