hayson-engine 0.22.0

Storage engine for hayson document databases — the embeddable core behind hayson-server, hayson-cli and hayson-browser
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
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fs;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};

use serde_json::json;
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipArchive, ZipWriter};

use super::backup::{
    check_relative, hold, owned_files, parse_manifest, read_catalog_of, refuse, restore_staged, sibling, ManifestEntry,
    RestoreResult, BACKUP_MANIFEST,
};
use super::database::{Database, DatabaseError, LockMode};
use super::layout::LayoutOptions;

const MIB: u64 = 1024 * 1024;
const MAX_NAME_BYTES: usize = 1024;
const MAX_COMPONENT_BYTES: usize = 255;
const DEVICE_NAMES: [&str; 22] = [
    "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2",
    "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9",
];

/// What a pack may hold and what an unpack accepts. The same limits guard both directions, so a
/// pack this engine writes is one it reads back.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackLimits {
    /// Size of the archive file itself.
    pub max_archive_bytes: u64,
    /// Sum of the declared sizes of every entry.
    pub max_total_bytes: u64,
    /// Declared size of a single entry.
    pub max_entry_bytes: u64,
    pub max_entries: usize,
}

impl Default for PackLimits {
    fn default() -> Self {
        PackLimits {
            max_archive_bytes: 256 * MIB,
            max_total_bytes: 256 * MIB,
            max_entry_bytes: 128 * MIB,
            max_entries: 10_000,
        }
    }
}

#[derive(Debug, Clone)]
pub struct PackOptions {
    /// How the writer lock is taken for the length of the read. As in [`super::BackupOptions`].
    pub lock: LockMode,
    /// Also pack the `<table>.idx` snapshots (caches; off by default).
    pub indexes: bool,
    /// Files of the caller's own to store next to the database files (a manifest of the application's
    /// format, say), as `(name inside the archive, bytes)`. Read back with [`read_pack_entry`].
    pub extra: Vec<(String, Vec<u8>)>,
    pub limits: PackLimits,
}

impl Default for PackOptions {
    fn default() -> Self {
        PackOptions {
            lock: LockMode::default(),
            indexes: false,
            extra: Vec::new(),
            limits: PackLimits::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackResult {
    pub dest: PathBuf,
    /// Database files in the archive (not counting the manifest and the caller's extra entries).
    pub files: usize,
    /// Their size.
    pub bytes: u64,
    /// Size of the archive file.
    pub archive_bytes: u64,
}

#[derive(Debug, Clone, Default)]
pub struct UnpackOptions {
    pub lock: LockMode,
    pub limits: PackLimits,
}

fn zip_error(error: zip::result::ZipError) -> DatabaseError {
    match error {
        zip::result::ZipError::Io(error) => DatabaseError::Io(error),
        other => refuse(format!("not a readable archive: {other}")),
    }
}

/// An entry name this engine writes or extracts: relative, `/`-separated, no empty, `.` or `..`
/// component, no control character, no Windows device name, no trailing dot or space.
fn check_entry_name(name: &str) -> Result<(), DatabaseError> {
    let bad = |why: &str| refuse(format!("archive entry name {name:?} is refused: {why}"));
    if name.is_empty() || name.len() > MAX_NAME_BYTES {
        return Err(bad("empty or too long"));
    }
    if name.starts_with('/') || name.contains('\\') {
        return Err(bad("absolute path or backslash"));
    }
    if name.chars().any(|c| c.is_control()) {
        return Err(bad("control character"));
    }
    for component in name.split('/') {
        if component.is_empty() || component == "." || component == ".." {
            return Err(bad("empty, \".\" or \"..\" component"));
        }
        if component.len() > MAX_COMPONENT_BYTES {
            return Err(bad("component too long"));
        }
        if component.ends_with('.') || component.ends_with(' ') {
            return Err(bad("component ends with a dot or a space"));
        }
        if component.contains(':') {
            return Err(bad("drive letter or stream marker"));
        }
        let stem = component.split('.').next().unwrap_or("").trim_end().to_lowercase();
        if DEVICE_NAMES.contains(&stem.as_str()) {
            return Err(bad("device name"));
        }
    }
    check_relative(Path::new(name))
}

fn fold(name: &str) -> String {
    name.to_lowercase()
}

/// The result of reading an archive's central directory and judging every entry before any byte is
/// extracted.
struct Inspected {
    archive: ZipArchive<fs::File>,
    /// Non-directory entries: name → index.
    files: HashMap<String, usize>,
    sizes: HashMap<String, u64>,
}

fn inspect(path: &Path, limits: &PackLimits) -> Result<Inspected, DatabaseError> {
    let meta = fs::symlink_metadata(path)
        .map_err(|error| refuse(format!("\"{}\" cannot be read: {error}", path.display())))?;
    if !meta.is_file() {
        return Err(refuse(format!("\"{}\" is not a regular file", path.display())));
    }
    if meta.len() > limits.max_archive_bytes {
        return Err(refuse(format!(
            "\"{}\" is {} bytes, over the {} byte limit",
            path.display(),
            meta.len(),
            limits.max_archive_bytes
        )));
    }
    let mut archive = ZipArchive::new(fs::File::open(path)?).map_err(zip_error)?;
    if archive.len() > limits.max_entries {
        return Err(refuse(format!(
            "the archive has {} entries, over the limit of {}",
            archive.len(),
            limits.max_entries
        )));
    }
    let mut files = HashMap::new();
    let mut sizes = HashMap::new();
    let mut seen: HashSet<String> = HashSet::new();
    let mut total = 0u64;
    for index in 0..archive.len() {
        let entry = archive.by_index_raw(index).map_err(zip_error)?;
        let raw_name = entry.name().to_string();
        let is_dir = raw_name.ends_with('/');
        check_entry_name(raw_name.trim_end_matches('/'))?;
        if !seen.insert(fold(raw_name.trim_end_matches('/'))) {
            return Err(refuse(format!(
                "archive entry {raw_name:?} appears twice (names are compared ignoring case)"
            )));
        }
        if entry.encrypted() {
            return Err(refuse(format!("archive entry {raw_name:?} is encrypted")));
        }
        if let Some(mode) = entry.unix_mode() {
            let kind = mode & 0o170000;
            if kind == 0o120000 {
                return Err(refuse(format!("archive entry {raw_name:?} is a symbolic link")));
            }
            if kind != 0 && kind != 0o100000 && kind != 0o040000 {
                return Err(refuse(format!("archive entry {raw_name:?} is not a regular file")));
            }
        }
        if is_dir {
            continue;
        }
        if entry.compression() != CompressionMethod::Stored {
            return Err(refuse(format!(
                "archive entry {raw_name:?} is compressed — packs are stored, never compressed"
            )));
        }
        if entry.size() != entry.compressed_size() {
            return Err(refuse(format!(
                "archive entry {raw_name:?} declares inconsistent sizes"
            )));
        }
        if entry.size() > limits.max_entry_bytes {
            return Err(refuse(format!(
                "archive entry {raw_name:?} declares {} bytes, over the {} byte limit",
                entry.size(),
                limits.max_entry_bytes
            )));
        }
        total = total.saturating_add(entry.size());
        if total > limits.max_total_bytes {
            return Err(refuse(format!(
                "the archive declares more than {} bytes in total",
                limits.max_total_bytes
            )));
        }
        sizes.insert(raw_name.clone(), entry.size());
        files.insert(raw_name, index);
    }
    Ok(Inspected { archive, files, sizes })
}

fn read_entry(inspected: &mut Inspected, name: &str) -> Result<Option<Vec<u8>>, DatabaseError> {
    let Some(&index) = inspected.files.get(name) else {
        return Ok(None);
    };
    let declared = inspected.sizes[name];
    let mut entry = inspected.archive.by_index(index).map_err(zip_error)?;
    let mut bytes = Vec::with_capacity(declared as usize);
    (&mut entry).take(declared + 1).read_to_end(&mut bytes)?;
    if bytes.len() as u64 != declared {
        return Err(refuse(format!("archive entry {name:?} is not the size it declares")));
    }
    Ok(Some(bytes))
}

/// Reads one entry of a pack — the caller's own manifest, say — after the same checks an unpack makes.
/// `None` when the archive has no entry of that name.
pub fn read_pack_entry(archive: &Path, name: &str, limits: &PackLimits) -> Result<Option<Vec<u8>>, DatabaseError> {
    let mut inspected = inspect(archive, limits)?;
    read_entry(&mut inspected, name)
}

/// Packs a database directory into one zip archive at `dest`: the files the database accounts for,
/// stored without compression, plus the `hayson-backup.json` manifest and the caller's `extra` entries.
///
/// `dest` must not exist; the archive is built next to it and renamed into place. The directory is
/// held under the writer lock while it is read, and a database with an unfinished unit of work is
/// refused, as in [`super::backup_database`]. Limits are enforced here too, so what is packed can be
/// unpacked. History kept in a database of its own is not part of this database's files.
pub fn pack_database(dir: &Path, dest: &Path, options: &PackOptions) -> Result<PackResult, DatabaseError> {
    let layout = Database::resolve_layout(dir, &LayoutOptions::default())?;
    let catalog = read_catalog_of(&layout)?;
    if dest.exists() {
        return Err(refuse(format!("\"{}\" already exists", dest.display())));
    }
    let mut taken: BTreeSet<String> = BTreeSet::new();
    taken.insert(fold(BACKUP_MANIFEST));
    for (name, bytes) in &options.extra {
        check_entry_name(name)?;
        if !taken.insert(fold(name)) {
            return Err(refuse(format!("extra entry {name:?} appears twice or is reserved")));
        }
        if bytes.len() as u64 > options.limits.max_entry_bytes {
            return Err(refuse(format!("extra entry {name:?} is over the entry size limit")));
        }
    }
    let partial = sibling(dest, ".partial");
    if partial.exists() {
        fs::remove_file(&partial)?;
    }

    let held = hold(dir, &layout, &options.lock)?;
    let outcome = (|| -> Result<PackResult, DatabaseError> {
        if layout.journal_path().exists() {
            return Err(refuse(format!(
                "\"{}\" holds an unfinished unit of work — open the database for writing once so it is finished, then pack it",
                layout.journal_path().display()
            )));
        }
        let files = owned_files(dir, &layout, &catalog, options.indexes)?;
        if files.len() + options.extra.len() + 1 > options.limits.max_entries {
            return Err(refuse(
                "the database has more files than the entry limit allows".to_string(),
            ));
        }
        let stored = SimpleFileOptions::default()
            .compression_method(CompressionMethod::Stored)
            .unix_permissions(0o644)
            .large_file(false);
        let mut writer = ZipWriter::new(fs::File::create(&partial)?);
        let mut listed = Vec::new();
        let mut total = 0u64;
        for relative in &files {
            let name = relative.to_string_lossy().replace('\\', "/");
            check_entry_name(&name)?;
            if !taken.insert(fold(&name)) {
                return Err(refuse(format!("database file {name:?} collides with an extra entry")));
            }
            let mut source = fs::File::open(dir.join(relative))?;
            writer.start_file(name.as_str(), stored).map_err(zip_error)?;
            let bytes = io::copy(&mut (&mut source).take(options.limits.max_entry_bytes + 1), &mut writer)?;
            if bytes > options.limits.max_entry_bytes {
                return Err(refuse(format!(
                    "\"{name}\" is over the {} byte entry limit",
                    options.limits.max_entry_bytes
                )));
            }
            total += bytes;
            if total > options.limits.max_total_bytes {
                return Err(refuse(format!(
                    "the database is over the {} byte limit",
                    options.limits.max_total_bytes
                )));
            }
            listed.push(json!({ "path": name, "bytes": bytes }));
        }
        let manifest = json!({
            "format": 1,
            "createdAt": chrono::Utc::now().timestamp_millis(),
            "engine": env!("CARGO_PKG_VERSION"),
            "metadataFile": layout.metadata_file(),
            "files": listed,
        });
        let mut text = serde_json::to_string_pretty(&manifest).map_err(|e| DatabaseError::Catalog(e.to_string()))?;
        text.push('\n');
        writer.start_file(BACKUP_MANIFEST, stored).map_err(zip_error)?;
        writer.write_all(text.as_bytes())?;
        for (name, bytes) in &options.extra {
            writer.start_file(name.as_str(), stored).map_err(zip_error)?;
            writer.write_all(bytes)?;
        }
        let file = writer.finish().map_err(zip_error)?;
        file.sync_all()?;
        let archive_bytes = file.metadata()?.len();
        if archive_bytes > options.limits.max_archive_bytes {
            return Err(refuse(format!(
                "the archive would be {archive_bytes} bytes, over the {} byte limit",
                options.limits.max_archive_bytes
            )));
        }
        Ok(PackResult {
            dest: dest.to_path_buf(),
            files: files.len(),
            bytes: total,
            archive_bytes,
        })
    })();
    drop(held);
    match outcome {
        Ok(result) => {
            fs::rename(&partial, dest)?;
            Ok(result)
        }
        Err(error) => {
            let _ = fs::remove_file(&partial);
            Err(error)
        }
    }
}

/// Unpacks an archive made by [`pack_database`] into `dir`, replacing the database that is there,
/// with the recoverable procedure of [`super::restore_database`] (stage, verify, swap, roll back).
///
/// The archive is judged before anything is written: size and entry-count limits, names (no `..`,
/// absolute or drive paths, backslashes, control characters, device names, duplicates ignoring case),
/// no symbolic links, no compressed or encrypted entries, declared sizes against the limits and against
/// what each entry really holds. Entries the manifest does not list — the caller's extras — are not
/// extracted.
pub fn unpack_database(archive: &Path, dir: &Path, options: &UnpackOptions) -> Result<RestoreResult, DatabaseError> {
    let mut inspected = inspect(archive, &options.limits)?;
    let manifest = read_entry(&mut inspected, BACKUP_MANIFEST)?.ok_or_else(|| {
        refuse(format!(
            "the archive has no \"{BACKUP_MANIFEST}\" — it is not a hayson pack"
        ))
    })?;
    let text = String::from_utf8(manifest).map_err(|_| refuse(format!("\"{BACKUP_MANIFEST}\" is not UTF-8")))?;
    let (metadata_file, entries) = parse_manifest(&text, BACKUP_MANIFEST)?;
    let mut listed: HashSet<String> = HashSet::new();
    for entry in &entries {
        let name = entry.relative.to_string_lossy().replace('\\', "/");
        check_entry_name(&name)?;
        if !listed.insert(fold(&name)) {
            return Err(refuse(format!("the manifest lists {name:?} twice")));
        }
        match inspected.sizes.get(&name) {
            Some(size) if *size == entry.bytes => {}
            Some(_) => {
                return Err(refuse(format!(
                    "archive entry {name:?} does not match its manifest entry ({} bytes expected)",
                    entry.bytes
                )))
            }
            None => return Err(refuse(format!("archive entry {name:?} is missing"))),
        }
    }
    restore_staged(dir, &metadata_file, &entries, &options.lock, |staged| {
        extract(&mut inspected, &entries, staged)
    })
}

fn extract(inspected: &mut Inspected, entries: &[ManifestEntry], staged: &Path) -> Result<u64, DatabaseError> {
    let mut total = 0u64;
    for entry in entries {
        let name = entry.relative.to_string_lossy().replace('\\', "/");
        let index = inspected.files[&name];
        let target = staged.join(&entry.relative);
        if let Some(parent) = target.parent() {
            fs::create_dir_all(parent)?;
        }
        let mut source = inspected.archive.by_index(index).map_err(zip_error)?;
        let mut out = fs::OpenOptions::new().write(true).create_new(true).open(&target)?;
        let written = io::copy(&mut (&mut source).take(entry.bytes + 1), &mut out)?;
        if written != entry.bytes {
            return Err(refuse(format!("archive entry {name:?} is not the size it declares")));
        }
        out.sync_all()?;
        total += written;
    }
    Ok(total)
}

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

    #[test]
    fn refuses_names_that_escape_or_confuse() {
        for bad in [
            "",
            "/etc/passwd",
            "../x",
            "a/../x",
            "a//b",
            "./a",
            "a\\b",
            "C:/x",
            "a\u{0}b",
            "a\nb",
            "con",
            "tables/NUL.txt",
            "a. ",
            "a.",
        ] {
            assert!(check_entry_name(bad).is_err(), "{bad:?} should be refused");
        }
        for good in [
            "hayson.json",
            "tables/users.jsonl",
            "tables/users.images/ab/cd.png",
            "gly.json",
        ] {
            assert!(check_entry_name(good).is_ok(), "{good:?} should be accepted");
        }
    }
}