OXYGENHUB

Coverage / crates/flow/src/tier.rs 88.5%

23 of 26 lines run, 6 of 13 functions · run #195 on main · run missed

1
//! What the sandbox tiers share, on every system a runner runs on: where a
2
//! job's disks are kept, how a helper says a job never started, reading a
3
//! job's output by lines, and this binary's hash for receipts.
4
5
use std::io::{BufRead, BufReader, Read};
6
use std::path::{Path, PathBuf};
7
use std::sync::{mpsc, OnceLock};
8
9
/// Where the `n`th `ephemeral://disk` mount of a job keeps its files.
104×
pub fn disk_dir(scratch: &Path, n: usize) -> PathBuf {
114×
    scratch.join(format!("mount-{n}"))
124×
}
13
14
/// How the helper says the job never started, on its stderr.
15
pub(crate) const FAILED: &str = "\u{1e}F ";
16
1712×
pub(crate) fn lines(r: impl Read, stdout: bool, tx: mpsc::Sender<(bool, Vec<u8>)>) {
1812×
    let mut r = BufReader::new(r);
1912×
    let mut buf = Vec::new();
20
    loop {
2121×
        buf.clear();
22
        // A long line is cut at 16 KiB, as flow-init cuts it.
2321×
        match (&mut r).take(16 * 1024).read_until(b'\n', &mut buf) {
2412×
            Ok(0) | Err(_) => break,
25
            Ok(_) => {
269×
                if buf.last() == Some(&b'\n') {
279×
                    buf.pop();
289×
                }
299×
                if tx.send((stdout, buf.clone())).is_err() {
300×
                    break;
319×
                }
32
            }
33
        }
34
    }
3512×
}
36
37
/// This binary, to start helpers from and hash: `/proc/self/exe` on Linux
38
/// and Android (the running file, even after an update replaced its name),
39
/// the path it was started from elsewhere (oxygenflow#83).
408×
pub fn self_exe() -> PathBuf {
418×
    if cfg!(target_os = "linux") || cfg!(target_os = "android") {
428×
        PathBuf::from("/proc/self/exe")
43
    } else {
440×
        std::env::current_exe().unwrap_or_default()
45
    }
468×
}
47
48
/// This binary's hash, which receipts name as the sandbox.
4912×
pub(crate) fn self_hash() -> &'static str {
50
    static HASH: OnceLock<String> = OnceLock::new();
5112×
    HASH.get_or_init(|| crate::home::hash_file(&self_exe()).unwrap_or_else(|_| "unknown".into()))
5212×
}