wasmer_journal/concrete/
mem_file.rs

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
use std::{
    fs::File,
    io::{Seek, Write},
    path::Path,
    sync::RwLock,
};

use lz4_flex::{block, decompress};

use super::*;

/// The memory file journal processes journal entries by writing any memory mutations
/// directly to a file. Later this can be used as a mounting target for resuming
/// a process without having to reload the journal from scratch.
#[derive(Debug)]
pub struct MemFileJournal {
    file: RwLock<File>,
}

impl MemFileJournal {
    pub fn new(path: &Path) -> anyhow::Result<Self> {
        Ok(Self {
            file: RwLock::new(
                std::fs::OpenOptions::new()
                    .create(true)
                    .truncate(false)
                    .write(true)
                    .open(path)?,
            ),
        })
    }
}

impl Drop for MemFileJournal {
    fn drop(&mut self) {
        let _ = self.flush();
    }
}

impl Clone for MemFileJournal {
    fn clone(&self) -> Self {
        let file = self.file.read().unwrap();
        Self {
            file: RwLock::new(file.try_clone().unwrap()),
        }
    }
}

impl ReadableJournal for MemFileJournal {
    fn read(&self) -> anyhow::Result<Option<LogReadResult<'_>>> {
        Ok(None)
    }

    fn as_restarted(&self) -> anyhow::Result<Box<DynReadableJournal>> {
        Ok(Box::new(self.clone()))
    }
}

impl WritableJournal for MemFileJournal {
    fn write<'a>(&'a self, entry: JournalEntry<'a>) -> anyhow::Result<LogWriteResult> {
        let estimated_size = entry.estimate_size() as u64;
        match entry {
            JournalEntry::UpdateMemoryRegionV1 {
                region,
                compressed_data,
            } => {
                let (uncompressed_size, compressed_data) =
                    block::uncompressed_size(&compressed_data)?;
                let decompressed_data = decompress(compressed_data, uncompressed_size)?;

                let mut file = self.file.write().unwrap();
                file.seek(std::io::SeekFrom::Start(region.start))?;
                file.write_all(&decompressed_data)?;
            }
            JournalEntry::ProcessExitV1 { .. } | JournalEntry::InitModuleV1 { .. } => {
                let file = self.file.read().unwrap();
                file.set_len(0)?;
            }
            _ => {}
        }

        Ok(LogWriteResult {
            record_start: 0,
            record_end: estimated_size,
        })
    }

    fn flush(&self) -> anyhow::Result<()> {
        let mut file = self.file.write().unwrap();
        file.flush()?;
        Ok(())
    }
}

impl Journal for MemFileJournal {
    fn split(self) -> (Box<DynWritableJournal>, Box<DynReadableJournal>) {
        (Box::new(self.clone()), Box::new(self.clone()))
    }
}