use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::channel::entries;
use crate::channel::material::{ADDRESS, ANCHORS, CHAIN, KEY};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Claimed {
pub workspace: String,
pub name: String,
pub grade: String,
pub addresses: Vec<String>,
pub ca: String,
pub cert: String,
}
pub fn read(body: &[u8]) -> Result<Claimed, String> {
let answer: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
let text = |key: &str| {
answer
.get(key)
.and_then(Value::as_str)
.filter(|said| !said.is_empty())
.map(str::to_owned)
.ok_or_else(|| missing(key))
};
let addresses: Vec<String> = answer
.get(ADDRESS)
.and_then(Value::as_array)
.map(|listed| listed.iter().filter_map(Value::as_str).collect::<Vec<_>>())
.map(|listed| crate::channel::material::list(&listed.join("\n")))
.filter(|listed| !listed.is_empty())
.ok_or_else(|| missing(ADDRESS))?;
let workspace = text("workspace")?;
if !entries::is_leaf(&workspace) {
return Err(format!(
"the server named the workspace {workspace:?}, which cannot be a directory name here; \
nothing was filed"
));
}
Ok(Claimed {
workspace,
name: text("name")?,
grade: text("grade")?,
addresses,
ca: text("ca")?,
cert: text("cert")?,
})
}
fn missing(key: &str) -> String {
format!("the server's answer carried no `{key}`; nothing was filed")
}
pub fn file(data_root: &Path, claimed: &Claimed, key: &str) -> Result<PathBuf, String> {
let entries = entries::dir(data_root);
let dir = entries.join(&claimed.workspace);
std::fs::create_dir_all(&entries).map_err(|e| format!("{}: {e}", entries.display()))?;
std::fs::create_dir(&dir).map_err(|e| {
if e.kind() == std::io::ErrorKind::AlreadyExists {
format!(
"this box already holds an entry for workspace {:?} at {} and it was not \
overwritten. The offer is spent and its certificate was not kept: move or \
remove that directory, have the offer declined, and register again with a \
new one",
claimed.workspace,
dir.display()
)
} else {
format!("{}: {e}", dir.display())
}
})?;
let address = claimed.addresses.join("\n");
for (leaf, body) in [
(ANCHORS, claimed.ca.as_str()),
(CHAIN, claimed.cert.as_str()),
(KEY, key),
(ADDRESS, address.as_str()),
] {
laid(&dir.join(leaf), body)?;
}
Ok(dir)
}
fn laid(path: &Path, body: &str) -> Result<(), String> {
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
options
.open(path)
.and_then(|mut file| {
std::io::Write::write_all(&mut file, format!("{}\n", body.trim_end()).as_bytes())
})
.map_err(|e| format!("{}: {e}", path.display()))
}
#[cfg(test)]
mod tests;