use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::io::{self, Read};
use std::path::PathBuf;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "backend", rename_all = "snake_case", deny_unknown_fields)]
pub enum StorageLocation {
Filesystem {
root: PathBuf,
},
Https {
endpoint: String,
credential_profile: PathBuf,
},
Gdrive {
folder_id: String,
credential_profile: PathBuf,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactRole {
Reproduction,
Raw,
Processed,
Record,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ArtifactRef {
pub key: String,
pub sha256: String,
pub bytes: u64,
pub role: ArtifactRole,
#[serde(default)]
pub executable: bool,
}
impl ArtifactRef {
pub fn validate(&self) -> io::Result<()> {
validate_key(&self.key)?;
if self.sha256.len() != 64
|| !self
.sha256
.bytes()
.all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
{
return Err(io::Error::other("invalid artifact digest"));
}
Ok(())
}
}
pub(crate) fn validate_key(key: &str) -> io::Result<()> {
if key.is_empty()
|| key.starts_with('/')
|| key.contains(['\\', '\0'])
|| key
.split('/')
.any(|s| s.is_empty() || s == "." || s == "..")
{
return Err(io::Error::other("invalid logical artifact key"));
}
Ok(())
}
pub(crate) fn digest_bytes(bytes: &[u8]) -> String {
hex(&Sha256::digest(bytes))
}
pub(crate) fn digest_reader(mut reader: impl Read) -> io::Result<(String, u64)> {
let mut hash = Sha256::new();
let mut size = 0;
let mut buffer = [0; 65536];
loop {
let n = reader.read(&mut buffer)?;
if n == 0 {
break;
}
hash.update(&buffer[..n]);
size += n as u64;
}
Ok((hex(&hash.finalize()), size))
}
pub(crate) fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ArtifactDeclarations {
pub reproduction: Vec<String>,
pub raw: Vec<String>,
pub processed: Vec<String>,
pub shared_inputs: std::collections::BTreeMap<String, String>,
}
impl ArtifactDeclarations {
pub(crate) fn validate(&self) -> io::Result<()> {
let mut paths = Vec::new();
for p in self
.reproduction
.iter()
.chain(&self.raw)
.chain(&self.processed)
{
validate_key(p)?;
if paths.iter().any(|q: &&String| {
p == *q || p.starts_with(&format!("{q}/")) || q.starts_with(&format!("{p}/"))
}) {
return Err(io::Error::other("artifact declarations overlap"));
}
paths.push(p);
}
for (name, path) in &self.shared_inputs {
if name.trim().is_empty() {
return Err(io::Error::other("shared input assertion requires a name"));
}
validate_key(path)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ArtifactLocator {
pub storage: StorageLocation,
pub run_id: String,
pub prefix: String,
}
impl ArtifactLocator {
pub fn open(&self) -> io::Result<super::ArtifactSet> {
super::ArtifactSet::new(
super::RunArchive::open(&self.storage, &self.run_id)?,
self.prefix.clone(),
)
}
}