use std::io::Write;
use std::path::Path;
use crate::error::OxenError;
use crate::model::entry::commit_entry::{Entry, SchemaEntry};
use crate::model::{CommitEntry, LocalRepository, Schema};
use crate::util;
pub fn backup_schema(repository: &LocalRepository, schema: &Schema) -> Result<(), OxenError> {
log::debug!("backing up schema {:?}", schema);
let version_path = util::fs::version_path_from_schema(repository.path.clone(), schema);
if let Some(parent) = version_path.parent() {
std::fs::create_dir_all(parent)?;
}
log::debug!("got version path for schema {:?}", version_path);
if should_copy_schema(schema, &version_path) {
let mut file = std::fs::File::create(&version_path)?;
let schema_json = serde_json::to_string(schema)?;
file.write_all(schema_json.as_bytes())?;
}
Ok(())
}
pub fn should_copy_entry(entry: &CommitEntry, path: &Path) -> bool {
!path.exists() || path_hash_is_different(entry, path)
}
pub fn should_unpack_entry(entry: &Entry, path: &Path) -> bool {
match entry {
Entry::CommitEntry(entry) => should_copy_entry(entry, path),
Entry::SchemaEntry(_schema_entry) => false,
}
}
pub fn should_copy_schema_entry(_schema: &SchemaEntry, path: &Path) -> bool {
!path.exists()
}
pub fn should_copy_schema(_schema: &Schema, path: &Path) -> bool {
!path.exists()
}
fn path_hash_is_different(entry: &CommitEntry, path: &Path) -> bool {
if let Ok(hash) = util::hasher::hash_file_contents(path) {
return hash != entry.hash;
}
false
}