Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

10.1 KB
Raw Blame
use std::fs;
use std::path::{Path, PathBuf};

use ente_assets::{Asset, AssetFile, AssetStore, download};

use crate::config::{
    KNOWLEDGE_ARTIFACT_FILENAMES, KnowledgeDatasetConfig, is_path_safe_component,
    knowledge_artifact_urls,
};

use super::{RetrievalError, index::RetrievalIndex};

pub fn knowledge_asset(expected_pack: &KnowledgeDatasetConfig) -> Result<Asset, download::Error> {
    let urls = knowledge_artifact_urls(expected_pack);
    let files = KNOWLEDGE_ARTIFACT_FILENAMES
        .into_iter()
        .zip(urls)
        .zip(expected_pack.artifact_sizes.iter().copied())
        .zip(expected_pack.artifact_sha256.iter().cloned())
        .map(|(((filename, url), size), sha256)| AssetFile {
            name: filename.to_owned(),
            url,
            size,
            sha256,
        })
        .collect();
    Asset::files(
        vec![
            "knowledge".to_string(),
            expected_pack.stable_id.clone(),
            expected_pack.current_download_identity.clone(),
        ],
        files,
    )
}

pub fn knowledge_pack_root(
    store: &AssetStore,
    expected_pack: &KnowledgeDatasetConfig,
) -> Result<PathBuf, download::Error> {
    store
        .asset_dir(&knowledge_asset(expected_pack)?)
        .parent()
        .map(Path::to_path_buf)
        .ok_or_else(|| {
            download::Error::InvalidTarget("knowledge asset has no pack directory".to_string())
        })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KnowledgeReconciliationStatus {
    Download,
    Ready,
    UpdateAvailable,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KnowledgeReconciliation {
    pub status: KnowledgeReconciliationStatus,
    pub active_identity: Option<String>,
    pub active_directory: Option<String>,
}

pub fn reconcile_knowledge_pack(
    pack_root: impl AsRef<Path>,
    expected_pack: &KnowledgeDatasetConfig,
) -> Result<KnowledgeReconciliation, RetrievalError> {
    let pack_root = pack_root.as_ref();
    validate_pack_root(pack_root, expected_pack, true)?;

    let mut valid = Vec::<(String, PathBuf)>::new();
    let mut invalid = Vec::<PathBuf>::new();
    for entry in fs::read_dir(pack_root)? {
        let Ok(entry) = entry else { continue };
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        if !file_type.is_dir() {
            continue;
        }
        let path = entry.path();
        match RetrievalIndex::open(&path, expected_pack) {
            Ok(index) => valid.push((index.dataset_identity().to_owned(), path)),
            Err(_) => invalid.push(path),
        }
    }

    let current_position = valid
        .iter()
        .position(|(identity, _)| identity == &expected_pack.current_download_identity);
    let active = if let Some(position) = current_position {
        Some(valid.swap_remove(position))
    } else {
        valid.sort_by(|left, right| right.0.cmp(&left.0));
        (!valid.is_empty()).then(|| valid.remove(0))
    };

    for path in &invalid {
        let _ = fs::remove_dir_all(path);
    }

    match active {
        Some((identity, directory)) => {
            cleanup_revision_artifacts(&directory);
            let status = if identity == expected_pack.current_download_identity {
                KnowledgeReconciliationStatus::Ready
            } else {
                KnowledgeReconciliationStatus::UpdateAvailable
            };
            Ok(KnowledgeReconciliation {
                status,
                active_identity: Some(identity),
                active_directory: Some(directory.display().to_string()),
            })
        }
        None => Ok(KnowledgeReconciliation {
            status: KnowledgeReconciliationStatus::Download,
            active_identity: None,
            active_directory: None,
        }),
    }
}

pub fn cleanup_obsolete_knowledge_pack_revisions(
    pack_root: impl AsRef<Path>,
    expected_pack: &KnowledgeDatasetConfig,
    active_identity: &str,
) -> Result<(), RetrievalError> {
    if !is_path_safe_component(active_identity) {
        return Err(RetrievalError::InvalidInput(
            "active knowledge revision identity is not path-safe".to_string(),
        ));
    }

    let pack_root = pack_root.as_ref();
    validate_pack_root(pack_root, expected_pack, false)?;

    let active_directory = pack_root.join(active_identity);
    RetrievalIndex::open(&active_directory, expected_pack)?;

    for entry in fs::read_dir(pack_root)?.flatten() {
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        if file_type.is_dir() && entry.file_name().to_str() != Some(active_identity) {
            let _ = fs::remove_dir_all(entry.path());
        }
    }
    cleanup_revision_artifacts(&active_directory);
    Ok(())
}

fn validate_pack_root(
    pack_root: &Path,
    expected_pack: &KnowledgeDatasetConfig,
    create: bool,
) -> Result<(), RetrievalError> {
    let root_identity = pack_root
        .file_name()
        .and_then(|name| name.to_str())
        .ok_or_else(|| {
            RetrievalError::InvalidInput("pack root must have a UTF-8 stable ID".to_string())
        })?;
    if root_identity != expected_pack.stable_id || !is_path_safe_component(root_identity) {
        return Err(RetrievalError::InvalidInput(
            "pack root must end with the selected stable dataset ID".to_string(),
        ));
    }
    if create {
        fs::create_dir_all(pack_root)?;
    }
    if !fs::symlink_metadata(pack_root)?.file_type().is_dir() {
        return Err(RetrievalError::InvalidInput(
            "pack root must be a directory, not a symlink".to_string(),
        ));
    }
    Ok(())
}

fn cleanup_revision_artifacts(directory: &Path) {
    let Ok(entries) = fs::read_dir(directory) else {
        return;
    };
    for entry in entries.flatten() {
        if entry
            .file_name()
            .to_str()
            .is_some_and(|name| KNOWLEDGE_ARTIFACT_FILENAMES.contains(&name))
        {
            continue;
        }
        let path = entry.path();
        match entry.file_type() {
            Ok(file_type) if file_type.is_dir() => {
                let _ = fs::remove_dir_all(path);
            }
            Ok(_) => {
                let _ = fs::remove_file(path);
            }
            Err(_) => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;
    use crate::config::defaults;
    use crate::retrieval::index::tests::synthetic_pack;

    #[test]
    fn constructs_only_the_four_fixed_download_files() {
        let expected = defaults().knowledge_datasets.remove(0);
        let store = AssetStore::new("assets");
        let asset = knowledge_asset(&expected).unwrap();
        assert_eq!(
            store.asset_dir(&asset),
            Path::new("assets")
                .join("knowledge")
                .join(&expected.stable_id)
                .join(&expected.current_download_identity)
        );
        for name in KNOWLEDGE_ARTIFACT_FILENAMES {
            assert!(store.file_path(&asset, name).is_some());
        }
        assert!(!store.is_downloaded(&asset));
    }

    #[test]
    fn retains_a_non_current_revision_and_reports_update() {
        let pack = synthetic_pack("simplewiki-old");
        let invalid_current = pack
            .pack_root
            .join(&pack.expected.current_download_identity);
        fs::create_dir_all(&invalid_current).unwrap();

        let result = reconcile_knowledge_pack(&pack.pack_root, &pack.expected).unwrap();
        assert_eq!(
            result.status,
            KnowledgeReconciliationStatus::UpdateAvailable
        );
        assert_eq!(result.active_identity.as_deref(), Some("simplewiki-old"));
        assert!(!invalid_current.exists());
        assert!(pack.revision.exists());
    }

    #[test]
    fn current_revision_wins_and_artifacts_are_removed() {
        let old = synthetic_pack("simplewiki-old");
        let current_identity = old.expected.current_download_identity.clone();
        let current = old.pack_root.join(&current_identity);
        copy_dir(&old.revision, &current);
        let mut manifest: serde_json::Value =
            serde_json::from_slice(&fs::read(current.join("manifest.json")).unwrap()).unwrap();
        manifest["dataset"] = serde_json::Value::String(current_identity.clone());
        fs::write(
            current.join("manifest.json"),
            serde_json::to_vec(&manifest).unwrap(),
        )
        .unwrap();
        fs::write(current.join("manifest.json.metadata.json"), "sidecar").unwrap();
        fs::create_dir(current.join("orphan")).unwrap();

        let result = reconcile_knowledge_pack(&old.pack_root, &old.expected).unwrap();
        assert_eq!(result.status, KnowledgeReconciliationStatus::Ready);
        assert_eq!(
            result.active_identity.as_deref(),
            Some(current_identity.as_str())
        );
        cleanup_obsolete_knowledge_pack_revisions(&old.pack_root, &old.expected, &current_identity)
            .unwrap();
        assert!(!old.revision.exists());
        assert!(!current.join("manifest.json.metadata.json").exists());
        assert!(!current.join("orphan").exists());
        let retained = fs::read_dir(current)
            .unwrap()
            .map(|entry| entry.unwrap().file_name().into_string().unwrap())
            .collect::<std::collections::HashSet<_>>();
        assert_eq!(
            retained,
            KNOWLEDGE_ARTIFACT_FILENAMES
                .map(str::to_owned)
                .into_iter()
                .collect()
        );
    }

    #[test]
    fn no_valid_revision_reports_download_and_cleans_invalid_directories() {
        let pack = synthetic_pack("simplewiki-old");
        fs::remove_file(pack.revision.join("vectors.i8")).unwrap();
        let result = reconcile_knowledge_pack(&pack.pack_root, &pack.expected).unwrap();
        assert_eq!(result.status, KnowledgeReconciliationStatus::Download);
        assert!(!pack.revision.exists());
    }

    fn copy_dir(source: &Path, destination: &Path) {
        fs::create_dir_all(destination).unwrap();
        for entry in fs::read_dir(source).unwrap() {
            let entry = entry.unwrap();
            fs::copy(entry.path(), destination.join(entry.file_name())).unwrap();
        }
    }
}