| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use std::io::{BufRead, BufReader, Read}; |
| 6 |
use std::path::{Path, PathBuf}; |
| 7 |
use std::sync::{mpsc, OnceLock}; |
| 8 |
|
| 9 |
|
| 10 |
pub fn disk_dir(scratch: &Path, n: usize) -> PathBuf { |
| 11 |
scratch.join(format!("mount-{n}")) |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
pub(crate) const FAILED: &str = "\u{1e}F "; |
| 16 |
|
| 17 |
pub(crate) fn lines(r: impl Read, stdout: bool, tx: mpsc::Sender<(bool, Vec<u8>)>) { |
| 18 |
let mut r = BufReader::new(r); |
| 19 |
let mut buf = Vec::new(); |
| 20 |
loop { |
| 21 |
buf.clear(); |
| 22 |
|
| 23 |
match (&mut r).take(16 * 1024).read_until(b'\n', &mut buf) { |
| 24 |
Ok(0) | Err(_) => break, |
| 25 |
Ok(_) => { |
| 26 |
if buf.last() == Some(&b'\n') { |
| 27 |
buf.pop(); |
| 28 |
} |
| 29 |
if tx.send((stdout, buf.clone())).is_err() { |
| 30 |
break; |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub fn self_exe() -> PathBuf { |
| 41 |
if cfg!(target_os = "linux") || cfg!(target_os = "android") { |
| 42 |
PathBuf::from("/proc/self/exe") |
| 43 |
} else { |
| 44 |
std::env::current_exe().unwrap_or_default() |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
pub(crate) fn self_hash() -> &'static str { |
| 50 |
static HASH: OnceLock<String> = OnceLock::new(); |
| 51 |
HASH.get_or_init(|| crate::home::hash_file(&self_exe()).unwrap_or_else(|_| "unknown".into())) |
| 52 |
} |