oxedyne/fe2o3/fe2o3_o3db_sync/src/textscan.rs
4.5 KiB, 10 runs
created by r1870400018:67327, 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 | //! A read-only check of the JDAT text that an Ozone store holds, against the decoder as it now refuses. |
| 2 | //! |
| 3 | //! Steel and Daimond keep binary `Dat` values, and a value may carry text inside it. Every live |
| 4 | //! key is read, the value tree is walked (lists, maps and their keys, boxes, options, notes, user |
| 5 | //! kinds, tuples) and each `Str` or UTF-8 byte string that starts like JDAT text (`{`, `[` or `(`) |
| 6 | //! is decoded. The counts, the classes and the report are `oxedyne_fe2o3_jdat::string::scan`'s, |
| 7 | //! shared with the scan of framed text files (Oxegen's tables), and nothing a store holds is ever |
| 8 | //! printed: a refusal carries the key's tag, the text's number within the value, the line, the |
| 9 | //! column and the class. |
| 10 | //! |
| 11 | //! The scan writes nothing, and since 2026-10-09 neither does an open that is only read from (no |
| 12 | //! live file is created and an unchanged `config.jdat` is left alone). Scan a `cp -a` COPY all |
| 13 | //! the same, never a live store, since a second process on one store is never safe. |
| 14 | |
| 15 | use crate::{ |
| 16 | prelude::*, |
| 17 | comm::response::Wait, |
| 18 | }; |
| 19 | |
| 20 | use oxedyne_fe2o3_iop_db::api::ScanOpts; |
| 21 | use oxedyne_fe2o3_jdat::{ |
| 22 | prelude::*, |
| 23 | id::NumIdDat, |
| 24 | string::scan::{ |
| 25 | self, |
| 26 | Refused, |
| 27 | TextScan, |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | // Every string and UTF-8 byte string in a value, however deep. |
| 33 | fn gather<'a>(d: &'a Dat, out: &mut Vec<&'a str>) { |
| 34 | fn each<'a>(ds: &'a [Dat], out: &mut Vec<&'a str>) { |
| 35 | for d in ds { |
| 36 | gather(d, out); |
| 37 | } |
| 38 | } |
| 39 | match d { |
| 40 | Dat::Str(s) => out.push(s.as_str()), |
| 41 | Dat::BU8(b) | Dat::BU16(b) | Dat::BU32(b) | Dat::BU64(b) => { |
| 42 | if let Ok(s) = std::str::from_utf8(b) { |
| 43 | out.push(s); |
| 44 | } |
| 45 | }, |
| 46 | Dat::List(v) => each(v, out), |
| 47 | Dat::Map(m) => { |
| 48 | for (k, v) in m { |
| 49 | gather(k, out); |
| 50 | gather(v, out); |
| 51 | } |
| 52 | }, |
| 53 | Dat::OrdMap(m) => { |
| 54 | for (k, v) in m { |
| 55 | gather(k.dat(), out); |
| 56 | gather(v, out); |
| 57 | } |
| 58 | }, |
| 59 | Dat::Box(b) => gather(b, out), |
| 60 | Dat::Opt(o) => { |
| 61 | if let Some(x) = &**o { |
| 62 | gather(x, out); |
| 63 | } |
| 64 | }, |
| 65 | Dat::ABox(_, b, _) => gather(b, out), |
| 66 | Dat::Usr(_, Some(b)) => gather(b, out), |
| 67 | Dat::Tup2(t) => each(&**t, out), |
| 68 | Dat::Tup3(t) => each(&**t, out), |
| 69 | Dat::Tup4(t) => each(&**t, out), |
| 70 | Dat::Tup5(t) => each(&**t, out), |
| 71 | Dat::Tup6(t) => each(&**t, out), |
| 72 | Dat::Tup7(t) => each(&**t, out), |
| 73 | Dat::Tup8(t) => each(&**t, out), |
| 74 | Dat::Tup9(t) => each(&**t, out), |
| 75 | Dat::Tup10(t) => each(&**t, out), |
| 76 | _ => {}, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Reads every live key of an Ozone store and decodes the JDAT text its values hold. |
| 81 | /// |
| 82 | /// The store must be a COPY, opened with garbage collection off and used by nobody else; this |
| 83 | /// function issues no write. The scan leaves deleted keys out, and one deleted after it reads |
| 84 | /// as absent and is passed over. |
| 85 | /// |
| 86 | /// # Arguments |
| 87 | /// * `scan_wait` - how long each zone's scan may take; a large store needs a generous wait. |
| 88 | pub fn scan_o3db< |
| 89 | const UIDL: usize, |
| 90 | UID: NumIdDat<UIDL> + 'static, |
| 91 | ENC: Encrypter + 'static, |
| 92 | KH: Hasher + 'static, |
| 93 | PR: Hasher + 'static, |
| 94 | CS: Checksummer + 'static, |
| 95 | >( |
| 96 | api: &OzoneApi<UIDL, UID, ENC, KH, PR, CS>, |
| 97 | scan_wait: Wait, |
| 98 | out: &mut TextScan, |
| 99 | ) |
| 100 | -> Outcome<()> |
| 101 | { |
| 102 | let entries = res!(api.scan_with_wait(&ScanOpts::all(), None, scan_wait)); |
| 103 | for (kdat, _, _) in &entries { |
| 104 | let val = match res!(api.get_wait(kdat, None)) { |
| 105 | None => continue, |
| 106 | Some((val, _)) => val, |
| 107 | }; |
| 108 | out.records += 1; |
| 109 | let mut found = Vec::new(); |
| 110 | gather(&val, &mut found); |
| 111 | let mut rec = 0usize; |
| 112 | for text in found { |
| 113 | let t = text.trim_start(); |
| 114 | if !(t.starts_with('{') || t.starts_with('[') || t.starts_with('(')) { |
| 115 | out.skipped += 1; |
| 116 | continue; |
| 117 | } |
| 118 | rec += 1; |
| 119 | out.texts += 1; |
| 120 | if let Some((line, col, class)) = scan::refusal(text) { |
| 121 | out.refuse(Refused { |
| 122 | store: "o3db".to_string(), |
| 123 | rec, |
| 124 | at: 0, |
| 125 | hash: scan::tag(fmt!("{}", kdat).as_bytes()), |
| 126 | line, |
| 127 | col, |
| 128 | class, |
| 129 | }); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | Ok(()) |
| 134 | } |