use crate::model::{atomic_write, lock};
use anyhow::{Context, Result, ensure};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
fs,
path::{Path, PathBuf},
process::Command,
};
pub(crate) fn supports(helper: &Path, command: &str, option: &str) -> bool {
Command::new(helper)
.args([command, "--help"])
.output()
.ok()
.is_some_and(|output| {
output.status.success()
&& String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.any(|word| word == option)
})
}
fn scope(home: &Path, input: &impl Serialize) -> Result<PathBuf> {
let config = hashtree_config::Config::load()?;
let data = std::env::var_os("HTREE_DATA_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| hashtree_config::get_hashtree_dir().join("data"));
let key = serde_json::to_vec(&(
input,
config.blossom.all_write_servers(),
canonical_path(&data)?,
canonical_path(Path::new(&config.storage.data_dir))?,
))?;
canonical_path(
&home
.join("publication")
.join(hex::encode(Sha256::digest(key))),
)
}
fn canonical_path(path: &Path) -> Result<PathBuf> {
let path = std::path::absolute(path)?;
let mut ancestor = path.clone();
let mut missing = Vec::new();
while !ancestor.exists() {
missing.push(
ancestor
.file_name()
.context("invalid publication path")?
.to_owned(),
);
ancestor = ancestor
.parent()
.context("invalid publication path")?
.to_path_buf();
}
let mut resolved = ancestor.canonicalize()?;
for component in missing.into_iter().rev() {
resolved.push(component);
}
Ok(resolved)
}
fn helper_identity(helper: &Path) -> Result<Option<(PathBuf, String)>> {
let path = helper.canonicalize()?;
let mut file = match fs::File::open(&path) {
Ok(file) => file,
Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => return Ok(None),
Err(error) => return Err(error.into()),
};
let mut digest = Sha256::new();
std::io::copy(&mut file, &mut digest)?;
Ok(Some((path, hex::encode(digest.finalize()))))
}
pub(crate) fn helper_scope(
home: &Path,
input: &impl Serialize,
helper: &Path,
) -> Result<Option<PathBuf>> {
helper_identity(helper)?
.map(|identity| scope(home, &(input, identity)))
.transpose()
}
#[derive(Serialize, Deserialize)]
struct Uploaded {
root: String,
checksum: String,
}
pub(crate) fn uploaded(scope: &Path) -> Option<String> {
fs::read(scope.join("uploaded.json"))
.ok()
.and_then(|bytes| serde_json::from_slice::<Uploaded>(&bytes).ok())
.filter(|state| {
crate::event_catalog::decode_root(&state.root).is_ok()
&& hex::encode(Sha256::digest(state.root.as_bytes())) == state.checksum
})
.map(|state| state.root)
}
pub(crate) fn remember(scope: &Path, root: &str) -> Result<()> {
crate::event_catalog::decode_root(root)?;
atomic_write(
&scope.join("uploaded.json"),
&serde_json::to_vec(&Uploaded {
root: root.into(),
checksum: hex::encode(Sha256::digest(root.as_bytes())),
})?,
)
}
pub(crate) fn upload_directory(home: &Path, helper: &Path, path: &Path) -> Result<String> {
let catalog = path.canonicalize()?;
let scope = helper_scope(home, &catalog, helper)?;
let mut command = Command::new(helper);
command.arg("add").arg(path).arg("--local");
let _guard = if let Some(scope) = &scope {
ensure!(
!scope.starts_with(&catalog),
"publication cache must be outside the catalog"
);
let guard = lock(&scope.join("publish.lock"))?;
if cfg!(unix) && supports(helper, "add", "--cache") {
command.arg("--cache").arg(scope.join("files.json"));
} else {
eprintln!(
"This htree helper does not support source-file caching; addressing the complete catalog. Update htree to enable incremental addressing."
);
}
Some(guard)
} else {
None
};
let output = command.output()?;
eprint!("{}", String::from_utf8_lossy(&output.stderr));
ensure!(
output.status.success(),
"Hashtree catalog publication failed"
);
let root = crate::event_catalog::uploaded_root(&output.stdout)?;
let previous = scope.as_deref().and_then(uploaded);
if previous.as_deref() == Some(&root) {
return Ok(root);
}
let mut command = Command::new(helper);
command.args(["push", root.strip_prefix("htree://").unwrap_or(&root)]);
if let Some(previous) = previous {
if supports(helper, "push", "--previous-root") {
command.args([
"--previous-root",
previous.strip_prefix("htree://").unwrap_or(&previous),
]);
} else {
eprintln!(
"This htree helper does not support retained-root delta uploads; checking the complete catalog. Update htree to enable incremental upload."
);
}
}
let output = command.output().context("cannot upload Hashtree catalog")?;
eprint!("{}", String::from_utf8_lossy(&output.stderr));
ensure!(
output.status.success(),
"Hashtree content upload failed; catalog was not announced"
);
if let Some(scope) = &scope {
remember(scope, &root)?;
}
Ok(root)
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{Duration, UNIX_EPOCH};
#[test]
fn checkpoint_identity_follows_helper_bytes() -> Result<()> {
let directory = tempfile::tempdir()?;
let path = directory.path().join("htree");
let identity = || helper_identity(&path)?.context("readable test helper");
let set_time = |seconds| {
fs::OpenOptions::new()
.write(true)
.open(&path)?
.set_modified(UNIX_EPOCH + Duration::from_secs(seconds))
};
fs::write(&path, b"helper A")?;
set_time(1_600_000_000)?;
let original = identity()?;
set_time(1_700_000_000)?;
assert_eq!(
identity()?,
original,
"touching the same bytes keeps the scope"
);
fs::write(&path, b"helper B")?;
set_time(1_600_000_000)?;
assert_ne!(
identity()?,
original,
"different bytes with the original length and timestamp need a fresh scope"
);
Ok(())
}
#[cfg(unix)]
#[test]
fn execute_only_helpers_do_not_share_checkpoints() -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let directory = tempfile::tempdir()?;
let path = directory.path().join("htree");
fs::write(&path, b"helper")?;
fs::set_permissions(&path, fs::Permissions::from_mode(0o111))?;
if fs::File::open(&path).is_ok() {
return Ok(());
}
assert!(helper_scope(directory.path(), &"index", &path)?.is_none());
assert!(!directory.path().join("publication").exists());
Ok(())
}
}