mod beneath;
pub mod ios;
mod manifest;
mod store;
mod tar;
use sim_core::hostfs::{FileExt, allocated_bytes};
use std::collections::{BTreeMap, VecDeque};
use std::fs;
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
use std::time::Instant;
pub use manifest::{
AndroidImage, Base, CHUNK_SIZE, COLD_BOOT, COMPRESSION, Captures, Device, Disk, Entry, EntryKind, Extent, FORMAT, File, GuestIdentity,
Identity, IosSimulator, MAX_SIZE, MAX_TARGET, Manifest, Secret, ignorable, is_hash, safe_path, safe_target,
};
pub use store::{ChunkStore, chunk_name, compress, decompress_verified, hash, hash_file, max_compressed, parse_chunk_name, read_regular};
pub use tar::{TarReader, TarWriter};
pub const MANIFEST: &str = "manifest.json";
pub const MAX_MANIFEST: u64 = 32 << 20;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("bundle format {0} isn't one this simee reads (it reads format {FORMAT}); update simee")]
Format(u64),
#[error("bad bundle manifest: {0}")]
Manifest(String),
#[error("unsafe path `{0}` in the bundle (absolute, empty, with `.` or `..`, or with an ignorable or format code point)")]
Path(String),
#[error("chunk {0} doesn't match its hash")]
ChunkHash(String),
#[error("{0} doesn't match its hash in the manifest")]
Hash(String),
#[error("the bundle lacks {count} chunk(s) (first: {first})")]
Missing { first: String, count: usize },
#[error("bad bundle archive: {0}")]
Tar(String),
#[error("{0}: {1}")]
Io(PathBuf, std::io::Error),
#[error("reading the bundle: {0}")]
Read(std::io::Error),
#[error("writing the bundle: {0}")]
Write(std::io::Error),
#[error("{0}")]
Unsupported(String),
#[error("{0} changed while it was exported")]
Changed(String),
#[error("{0} already exists")]
Exists(PathBuf),
#[error("{0}")]
Refused(String),
#[error("`{0}` is a symlink, and nothing is written through one")]
ThroughLink(String),
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Debug, Default)]
pub struct Options {
pub threads: usize,
}
#[derive(Clone, Debug, Default)]
pub struct Contents {
pub disks: Vec<(String, PathBuf)>,
pub files: Vec<(String, Vec<u8>)>,
}
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)]
pub struct Summary {
pub form: String,
pub disk_bytes: u64,
pub allocated_bytes: u64,
pub zero_bytes: u64,
pub data_bytes: u64,
pub unique_bytes: u64,
pub chunks: u64,
pub unique_chunks: u64,
pub compressed_bytes: u64,
pub bundle_bytes: u64,
pub seconds: f64,
}
#[derive(Clone, Copy)]
enum Origin {
Disk { disk: usize, offset: u64, len: u64 },
File { file: usize, offset: u64, len: u64 },
}
fn is_zero(buf: &[u8]) -> bool {
let (words, rest) = buf.as_chunks::<64>();
words.iter().all(|w| w.iter().fold(0, |a, &b| a | b) == 0) && rest.iter().all(|&b| b == 0)
}
fn read_at(f: &fs::File, path: &Path, buf: &mut [u8], offset: u64) -> Result<usize> {
let mut got = 0;
while got < buf.len() {
match f.read_at(&mut buf[got..], offset + got as u64) {
Ok(0) => break,
Ok(n) => got += n,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(e) => return Err(Error::Io(path.to_path_buf(), e)),
}
}
Ok(got)
}
fn plan_disk(path: &str, file: &Path, chunk_size: u64) -> Result<Disk> {
let f = fs::File::open(file).map_err(|e| Error::Io(file.to_path_buf(), e))?;
let size = f.metadata().map_err(|e| Error::Io(file.to_path_buf(), e))?.len();
let mut whole = blake3::Hasher::new();
let mut buf = vec![0u8; chunk_size as usize];
let mut extents: Vec<Extent> = Vec::new();
let mut offset = 0;
while offset < size {
let want = (size - offset).min(chunk_size) as usize;
if read_at(&f, file, &mut buf[..want], offset)? != want {
return Err(Error::Changed(file.display().to_string()));
}
let data = &buf[..want];
whole.update(data);
match extents.last_mut() {
Some(last) if last.zero && is_zero(data) => last.len += want as u64,
_ if is_zero(data) => extents.push(Extent::zero(offset, want as u64)),
_ => extents.push(Extent::chunk(offset, want as u64, hash(data))),
}
offset += want as u64;
}
Ok(Disk { path: path.to_owned(), size, blake3: whole.finalize().to_hex().to_string(), base: None, extents })
}
fn plan_file(path: &str, data: &[u8], chunk_size: u64) -> File {
let chunks = data.chunks(chunk_size as usize).map(hash).collect();
File { path: path.to_owned(), size: data.len() as u64, blake3: hash(data), chunks }
}
fn origins(m: &Manifest) -> Vec<(String, Origin)> {
let mut seen = std::collections::BTreeSet::new();
let mut out = Vec::new();
for (disk, d) in m.disks.iter().enumerate() {
for e in &d.extents {
if let Some(h) = &e.hash
&& seen.insert(h.clone())
{
out.push((h.clone(), Origin::Disk { disk, offset: e.offset, len: e.len }));
}
}
}
for (file, f) in m.files.iter().enumerate() {
for (i, h) in f.chunks.iter().enumerate() {
let offset = i as u64 * m.chunk_size;
if seen.insert(h.clone()) {
out.push((h.clone(), Origin::File { file, offset, len: (f.size - offset).min(m.chunk_size) }));
}
}
}
out
}
enum Sink {
Dir(ChunkStore),
Tar(TarWriter<BufWriter<fs::File>>),
}
impl Sink {
fn put(&mut self, hash: &str, compressed: &[u8]) -> Result<()> {
match self {
Sink::Dir(store) => store.put(hash, compressed).map(|_| ()),
Sink::Tar(w) => w.entry(&chunk_name(hash), compressed),
}
}
}
pub fn is_archive(to: &Path) -> bool {
to.extension().is_some_and(|x| x == "tar")
}
pub fn export(mut manifest: Manifest, contents: &Contents, to: &Path, opts: &Options) -> Result<(Manifest, Summary)> {
let start = Instant::now();
if to.exists() {
return Err(Error::Exists(to.to_path_buf()));
}
manifest.format = FORMAT;
manifest.chunk_size = CHUNK_SIZE;
manifest.compression = COMPRESSION.to_owned();
manifest.disks = contents.disks.iter().map(|(p, f)| plan_disk(p, f, CHUNK_SIZE)).collect::<Result<_>>()?;
manifest.files = contents.files.iter().map(|(p, d)| plan_file(p, d, CHUNK_SIZE)).collect();
manifest.validate()?;
let mut summary = Summary { form: if is_archive(to) { "tar" } else { "dir" }.into(), ..Summary::default() };
for ((_, file), d) in contents.disks.iter().zip(&manifest.disks) {
summary.disk_bytes += d.size;
summary.allocated_bytes += fs::metadata(file).map(|m| allocated_bytes(&m)).unwrap_or(0);
for e in &d.extents {
if e.zero {
summary.zero_bytes += e.len;
} else {
summary.data_bytes += e.len;
summary.chunks += 1;
}
}
}
for f in &manifest.files {
summary.data_bytes += f.size;
summary.chunks += f.chunks.len() as u64;
}
let partial = to.with_file_name(format!(".{}.partial", to.file_name().and_then(|n| n.to_str()).unwrap_or("bundle")));
let made = (|| -> Result<u64> {
let mut sink = if is_archive(to) {
let f = fs::File::create_new(&partial).map_err(|e| Error::Io(partial.clone(), e))?;
let mut w = TarWriter::new(BufWriter::with_capacity(1 << 20, f));
w.entry(MANIFEST, &manifest.to_json())?;
Sink::Tar(w)
} else {
fs::create_dir(&partial).map_err(|e| Error::Io(partial.clone(), e))?;
Sink::Dir(ChunkStore::new(&partial))
};
let mut open: Option<(usize, fs::File)> = None;
let threads = match opts.threads {
0 => std::thread::available_parallelism().map_or(4, |n| n.get()).min(8),
n => n,
};
let all = origins(&manifest);
for batch in all.chunks(threads * 2) {
let mut raw = Vec::with_capacity(batch.len());
for (h, origin) in batch {
let data = match *origin {
Origin::Disk { disk, offset, len } => {
let mut buf = vec![0u8; len as usize];
let path = &contents.disks[disk].1;
let f = match &mut open {
Some((at, f)) if *at == disk => &*f,
slot => {
*slot = None;
&slot.insert((disk, fs::File::open(path).map_err(|e| Error::Io(path.clone(), e))?)).1
}
};
if read_at(f, path, &mut buf, offset)? != buf.len() || hash(&buf) != *h {
return Err(Error::Changed(path.display().to_string()));
}
buf
}
Origin::File { file, offset, len } => contents.files[file].1[offset as usize..(offset + len) as usize].to_vec(),
};
raw.push(data);
}
let packed: Vec<Result<Vec<u8>>> = std::thread::scope(|s| {
let per = raw.len().div_ceil(threads).max(1);
let handles: Vec<_> =
raw.chunks(per).map(|group| s.spawn(move || group.iter().map(|d| Ok(compress(d))).collect::<Vec<_>>())).collect();
handles
.into_iter()
.flat_map(|h| h.join().unwrap_or_else(|_| vec![Err(Error::Unsupported("a compression thread panicked".into()))]))
.collect()
});
for (((h, _), data), z) in batch.iter().zip(&raw).zip(packed) {
let z = z?;
summary.unique_bytes += data.len() as u64;
summary.unique_chunks += 1;
summary.compressed_bytes += z.len() as u64;
sink.put(h, &z)?;
}
}
Ok(match sink {
Sink::Tar(w) => {
let written = w.written() + 1024;
w.finish()?;
written
}
Sink::Dir(_) => {
let json = manifest.to_json();
let path = partial.join(MANIFEST);
fs::write(&path, &json).map_err(|e| Error::Io(path, e))?;
summary.compressed_bytes + json.len() as u64
}
})
})();
match made {
Ok(bytes) => {
fs::rename(&partial, to).map_err(|e| Error::Io(to.to_path_buf(), e))?;
summary.bundle_bytes = bytes;
summary.seconds = (start.elapsed().as_secs_f64() * 100.0).round() / 100.0;
Ok((manifest, summary))
}
Err(e) => {
let _ = if partial.is_dir() { fs::remove_dir_all(&partial) } else { fs::remove_file(&partial) };
Err(e)
}
}
}
pub const OPEN_FILES: usize = 32;
struct Targets<'a> {
root: &'a beneath::Beneath,
paths: Vec<String>,
open: VecDeque<(usize, fs::File)>,
}
impl Targets<'_> {
fn host_path(&self, t: usize) -> PathBuf {
self.root.path().join(&self.paths[t])
}
fn keep(&mut self, t: usize, f: fs::File) {
if self.open.len() >= OPEN_FILES {
self.open.pop_front();
}
self.open.push_back((t, f));
}
fn create(&mut self, path: &str, size: u64) -> Result<usize> {
let f = self.root.create(path)?;
let t = self.paths.len();
self.paths.push(path.to_owned());
f.set_len(size).map_err(|e| Error::Io(self.host_path(t), e))?;
self.keep(t, f);
Ok(t)
}
fn get(&mut self, t: usize) -> Result<(PathBuf, &fs::File)> {
match self.open.iter().position(|(at, _)| *at == t) {
Some(i) if i + 1 == self.open.len() => {}
Some(i) => {
if let Some(e) = self.open.remove(i) {
self.open.push_back(e);
}
}
None => {
let f = self.root.open(&self.paths[t])?;
self.keep(t, f);
}
}
let path = self.host_path(t);
match self.open.back() {
Some((_, f)) => Ok((path, f)),
None => Err(Error::Io(path, std::io::ErrorKind::NotFound.into())),
}
}
fn take(&mut self, t: usize) -> Result<(PathBuf, fs::File)> {
let path = self.host_path(t);
match self.open.iter().position(|(at, _)| *at == t).and_then(|i| self.open.remove(i)) {
Some((_, f)) => Ok((path, f)),
None => Ok((path, self.root.open(&self.paths[t])?)),
}
}
}
enum Source {
Dir(PathBuf),
Tar(TarReader<BufReader<fs::File>>),
}
pub struct Reader {
manifest: Manifest,
source: Source,
}
#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)]
pub struct Extracted {
pub written_bytes: u64,
pub chunks: u64,
pub seconds: f64,
}
impl Reader {
pub fn open(path: &Path) -> Result<Reader> {
let (manifest, source) = if path.is_dir() {
let bytes = read_regular(&path.join(MANIFEST), MAX_MANIFEST)?;
(Manifest::parse(&bytes)?, Source::Dir(path.to_path_buf()))
} else {
let f = fs::File::open(path).map_err(|e| Error::Io(path.to_path_buf(), e))?;
let mut tar = TarReader::new(BufReader::with_capacity(1 << 20, f));
match tar.next_entry(MAX_MANIFEST)? {
Some((name, bytes)) if name == MANIFEST => (Manifest::parse(&bytes)?, Source::Tar(tar)),
Some((name, _)) => return Err(Error::Tar(format!("the first entry is `{name}`, not {MANIFEST}"))),
None => return Err(Error::Tar("the archive is empty".into())),
}
};
if let Some(d) = manifest.disks.iter().find(|d| d.base.is_some()) {
return Err(Error::Unsupported(format!("{} is a delta against a base disk (#171), which this simee can't apply yet", d.path)));
}
Ok(Reader { manifest, source })
}
pub fn manifest(&self) -> &Manifest {
&self.manifest
}
pub fn extract(self, dest: &Path) -> Result<Extracted> {
let start = Instant::now();
let m = self.manifest;
let root = beneath::Beneath::new(dest)?;
let need = m.stored_bytes();
if let Some(free) = root.free_bytes()
&& need > free
{
return Err(Error::Refused(format!(
"the bundle holds {need} bytes of data and {} has {free} free; free some space and import again",
dest.display()
)));
}
let mut targets = Targets { root: &root, paths: Vec::new(), open: VecDeque::new() };
let mut places: BTreeMap<String, Vec<(usize, u64, u64)>> = BTreeMap::new();
let mut create = |path: &str, size: u64| -> Result<usize> {
if !safe_path(path) {
return Err(Error::Path(path.to_owned()));
}
targets.create(path, size)
};
for d in &m.disks {
let t = create(&d.path, d.size)?;
for e in &d.extents {
if let Some(h) = &e.hash {
places.entry(h.clone()).or_default().push((t, e.offset, e.len));
}
}
}
for f in &m.files {
let t = create(&f.path, f.size)?;
for (i, h) in f.chunks.iter().enumerate() {
let offset = i as u64 * m.chunk_size;
places.entry(h.clone()).or_default().push((t, offset, (f.size - offset).min(m.chunk_size)));
}
}
let mut out = Extracted::default();
let mut place = |h: &str, data: &[u8], places: &mut BTreeMap<String, Vec<(usize, u64, u64)>>| -> Result<()> {
let spots = places.remove(h).ok_or_else(|| Error::Tar(format!("chunk {h} isn't in the manifest, or comes twice")))?;
for (t, offset, len) in spots {
if len != data.len() as u64 {
return Err(Error::ChunkHash(h.to_owned()));
}
let (path, f) = targets.get(t)?;
f.write_all_at(data, offset).map_err(|e| Error::Io(path, e))?;
out.written_bytes += len;
}
out.chunks += 1;
Ok(())
};
match self.source {
Source::Dir(root) => {
let store = ChunkStore::new(&root);
for h in m.chunk_hashes() {
let data = store.get(h, m.chunk_size)?;
place(h, &data, &mut places)?;
}
}
Source::Tar(mut tar) => {
let max = max_compressed(m.chunk_size);
while let Some((name, z)) = tar.next_entry(max)? {
let h = parse_chunk_name(&name).ok_or_else(|| Error::Tar(format!("unexpected entry `{name}`")))?;
let data = decompress_verified(h, &z, m.chunk_size)?;
place(h, &data, &mut places)?;
}
}
}
if let Some(first) = places.keys().next() {
return Err(Error::Missing { first: first.clone(), count: places.len() });
}
let wholes = m.disks.iter().map(|d| (&d.path, &d.blake3)).chain(m.files.iter().map(|f| (&f.path, &f.blake3)));
for (t, (name, want)) in wholes.enumerate() {
let (path, f) = targets.take(t)?;
let mut h = blake3::Hasher::new();
let mut r = BufReader::with_capacity(1 << 20, f);
std::io::Seek::rewind(&mut r).map_err(|e| Error::Io(path.clone(), e))?;
h.update_reader(&mut r).map_err(|e| Error::Io(path.clone(), e))?;
if h.finalize().to_hex().as_str() != want.as_str() {
return Err(Error::Hash(name.clone()));
}
}
out.seconds = (start.elapsed().as_secs_f64() * 100.0).round() / 100.0;
Ok(out)
}
}
#[cfg(test)]
mod tests;