pub(crate) mod cache;
mod error;
pub(super) use error::Error;
mod http;
pub(crate) mod msf;
use crate::{Builder, Image};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum Source {
Local,
Cache,
Downloaded,
}
#[derive(Debug)]
pub(super) struct Acquired {
pub(super) path: PathBuf,
pub(super) source: Source,
}
enum Range {
Bytes(Vec<u8>),
Full(ureq::Body),
}
enum Blocks {
Data(BTreeMap<u32, Vec<u8>>),
Full(ureq::Body),
}
pub(super) fn acquire(
builder: &Builder,
image: &Image,
out_dir: &Path,
) -> Result<Acquired, error::Error> {
let expected = image.identity();
if let Some(path) = image.local_pdb_path() {
cache::validate(path, expected)?;
return Ok(Acquired {
path: path.into(),
source: Source::Local,
});
}
let root = builder.cache_policy().directory().map_or_else(
|| {
out_dir.join(
builder
.cache_policy()
.out_dir_relative_path()
.expect("out-dir cache policy"),
)
},
Path::to_path_buf,
);
let destination = cache::path(&root, expected);
match cache::validate(&destination, expected) {
Ok(()) => {
return Ok(Acquired {
path: destination,
source: Source::Cache,
});
}
Err(error::Error::Io { source, .. }) if source.kind() == std::io::ErrorKind::NotFound => {}
Err(error) if builder.is_offline() || builder.symbol_server_policy().is_disabled() => {
return Err(error);
}
Err(_) => {}
}
if builder.is_offline() || builder.symbol_server_policy().is_disabled() {
return Err(error::Error::Unavailable {
identity: expected.clone(),
});
}
let url = format!(
"{}/{}/{}/{}",
builder
.symbol_server_policy()
.url()
.expect("enabled server")
.trim_end_matches('/'),
percent(expected.filename()),
cache::key(expected),
percent(expected.filename())
);
let client = http::Client::new();
let body = match staged(&client, &url, expected)? {
Some(body) => body,
None => normal(&client, &url)?,
};
let path = cache::publish(&root, expected, body.into_reader())?;
Ok(Acquired {
path,
source: Source::Downloaded,
})
}
fn normal(client: &http::Client, url: &str) -> Result<ureq::Body, error::Error> {
match client.get(url)? {
http::Response::Full(body) => Ok(body),
http::Response::NotFound => Err(error::Error::Http(error::HttpError::NotFound)),
http::Response::Status(status) => Err(error::Error::Http(error::HttpError::Status(status))),
http::Response::Partial { .. } => Err(error::Error::Http(error::HttpError::Status(206))),
http::Response::RangeNotSatisfiable => {
Err(error::Error::Http(error::HttpError::Status(416)))
}
}
}
fn staged(
client: &http::Client,
url: &str,
expected: &crate::PdbIdentity,
) -> Result<Option<ureq::Body>, error::Error> {
let (superblock_bytes, total) = match client.range(url, 0, 55)? {
http::Response::Full(body) => return Ok(Some(body)),
http::Response::RangeNotSatisfiable => return Ok(None),
http::Response::NotFound => return Err(error::Error::Http(error::HttpError::NotFound)),
http::Response::Status(status) => {
return Err(error::Error::Http(error::HttpError::Status(status)));
}
http::Response::Partial { bytes, total } => (bytes, total),
};
let superblock = msf::parse_superblock(&superblock_bytes)?;
if superblock.total()? != total {
return Err(error::Error::Http(error::HttpError::ContentRange(
"total differs from MSF superblock",
)));
}
let map_len = msf::directory_map_len(superblock)?;
let (map_start, _) = superblock.block_range(superblock.block_map)?;
let map_end = map_start
.checked_add(map_len as u64)
.and_then(|n| n.checked_sub(1))
.ok_or(error::Error::Msf {
reason: "directory map range overflow",
})?;
let map = match range(client, url, map_start, map_end, total)? {
Range::Bytes(bytes) => bytes,
Range::Full(body) => return Ok(Some(body)),
};
let directory_blocks = msf::directory_blocks(superblock, &map)?;
let directory_data = match fetch_blocks(client, url, superblock, &directory_blocks, total)? {
Blocks::Data(data) => data,
Blocks::Full(body) => return Ok(Some(body)),
};
let directory = assemble(
superblock,
&directory_blocks,
&directory_data,
superblock.directory_bytes as usize,
)?;
let pdb = msf::pdb_stream(superblock, &directory)?;
let count = 28usize.div_ceil(superblock.block_size as usize);
let pdb_data = match fetch_blocks(client, url, superblock, &pdb.blocks[..count], total)? {
Blocks::Data(data) => data,
Blocks::Full(body) => return Ok(Some(body)),
};
let header = assemble(superblock, &pdb.blocks[..count], &pdb_data, 28)?;
let actual = msf::header_identity(&header)?;
if actual.guid != expected.guid() || actual.age != expected.age() {
return Err(error::Error::Identity {
expected: expected.clone(),
actual,
});
}
Ok(None)
}
fn range(
client: &http::Client,
url: &str,
start: u64,
end: u64,
total: u64,
) -> Result<Range, error::Error> {
match client.range(url, start, end)? {
http::Response::Partial {
bytes,
total: received,
} if received == total => Ok(Range::Bytes(bytes)),
http::Response::Partial { .. } => Err(error::Error::Http(error::HttpError::ContentRange(
"total changed during probing",
))),
http::Response::Full(body) => Ok(Range::Full(body)),
http::Response::NotFound => Err(error::Error::Http(error::HttpError::NotFound)),
http::Response::RangeNotSatisfiable => {
Err(error::Error::Http(error::HttpError::Status(416)))
}
http::Response::Status(status) => Err(error::Error::Http(error::HttpError::Status(status))),
}
}
fn fetch_blocks(
client: &http::Client,
url: &str,
superblock: msf::Superblock,
blocks: &[u32],
total: u64,
) -> Result<Blocks, error::Error> {
let mut result = BTreeMap::new();
for (start, end) in msf::coalesce(superblock, blocks)? {
let bytes = match range(client, url, start, end, total)? {
Range::Bytes(bytes) => bytes,
Range::Full(body) => return Ok(Blocks::Full(body)),
};
let block_size = superblock.block_size as usize;
let first = u32::try_from(start / u64::from(superblock.block_size)).map_err(|_| {
error::Error::Msf {
reason: "block index overflow",
}
})?;
for (index, piece) in bytes.chunks_exact(block_size).enumerate() {
let block = first
.checked_add(u32::try_from(index).map_err(|_| error::Error::Msf {
reason: "block index overflow",
})?)
.ok_or(error::Error::Msf {
reason: "block index overflow",
})?;
result.insert(block, piece.to_vec());
}
}
Ok(Blocks::Data(result))
}
fn assemble(
superblock: msf::Superblock,
blocks: &[u32],
data: &BTreeMap<u32, Vec<u8>>,
length: usize,
) -> Result<Vec<u8>, error::Error> {
let mut output = Vec::with_capacity(length);
for block in blocks {
let bytes = data.get(block).ok_or(error::Error::Msf {
reason: "missing fetched block",
})?;
if bytes.len() != superblock.block_size as usize {
return Err(error::Error::Msf {
reason: "short fetched block",
});
}
output.extend_from_slice(bytes);
}
if output.len() < length {
return Err(error::Error::Msf {
reason: "truncated stream",
});
}
output.truncate(length);
Ok(output)
}
fn percent(value: &str) -> String {
value
.bytes()
.flat_map(|byte| {
if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') {
vec![byte as char]
} else {
format!("%{byte:02X}").chars().collect()
}
})
.collect()
}