scientific-workflow 0.17.0

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
//! Release authoritative copies from the disposable execution cache.
use crate::{
    artifact::{ArtifactRole, RunManifest},
    storage::{Store, atomic_json},
};
use std::{io, path::Path};
pub(crate) fn cleanup_completed(
    root: &Path,
    store: &Store,
    manifest: &RunManifest,
) -> io::Result<()> {
    for (key, reference) in &manifest.artifacts {
        let path = root.join(key);
        if !path.is_file() {
            continue;
        }
        // Small commit evidence remains directly available for offline inspection.
        if reference.role == ArtifactRole::Record && reference.bytes <= 4 * 1024 * 1024 {
            continue;
        }
        if let Some(parent) = path.parent() {
            make_writable(parent)?;
        }
        atomic_json(
            &path.with_extension("wf-object"),
            &serde_json::json!({"reference":reference,"locator":crate::artifact::ArtifactLocator{storage:store.location.clone(),run_id:manifest.run_id.clone(),prefix:reference.key.clone()}}),
        )?;
        std::fs::remove_file(&path)?;
    }
    // The execution view is a disposable copy of the committed effective-inputs
    // inventory. Keep its structure and verified pointers for diagnostic tooling.
    for (key, reference) in &manifest.artifacts {
        if let Some(relative) = key.strip_prefix("reproduction/effective-inputs/") {
            let path = root.join("execution-input").join(relative);
            if path.is_file() {
                if let Some(parent) = path.parent() {
                    make_writable(parent)?;
                }
                atomic_json(
                    &path.with_extension("wf-object"),
                    &serde_json::json!({"reference":reference,"locator":crate::artifact::ArtifactLocator{storage:store.location.clone(),run_id:manifest.run_id.clone(),prefix:reference.key.clone()}}),
                )?;
                std::fs::remove_file(path)?;
            }
        }
    }
    Ok(())
}
pub(crate) fn remove_sealed_cache(
    root: &Path,
    plan: &crate::seal::SealPlan,
    manifest: &RunManifest,
) -> io::Result<()> {
    for key in &plan.remove {
        let reference = &manifest.artifacts[key];
        let path = root.join(key);
        if path.is_file() {
            if crate::artifact::digest_reader(std::fs::File::open(&path)?)?
                != (reference.sha256.clone(), reference.bytes)
            {
                return Err(io::Error::other(
                    "local raw cache changed after commit; refusing an unverified removal",
                ));
            }
            if let Some(parent) = path.parent() {
                make_writable(parent)?;
            }
            std::fs::remove_file(&path)?;
        }
        let pointer = path.with_extension("wf-object");
        if pointer.is_file() {
            let value: serde_json::Value = serde_json::from_reader(std::fs::File::open(&pointer)?)
                .map_err(io::Error::other)?;
            if value["reference"]["sha256"] == reference.sha256 {
                if let Some(parent) = pointer.parent() {
                    make_writable(parent)?;
                }
                std::fs::remove_file(pointer)?;
            }
        }
    }
    Ok(())
}
fn make_writable(path: &Path) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut permissions = path.metadata()?.permissions();
        permissions.set_mode(permissions.mode() | 0o700);
        std::fs::set_permissions(path, permissions)?;
    }
    Ok(())
}