diff --git a/src/content/read.rs b/src/content/read.rs index 854c6b3..a5d0adb 100644 --- a/src/content/read.rs +++ b/src/content/read.rs @@ -1,23 +1,30 @@ -use crate::content::path; -use ssri::Integrity; +use std::fs; use std::path::Path; -use std::fs::{self, File}; -use std::io; -pub fn open(cache: &Path, sri: &Integrity) -> io::Result { - File::open(path::content_path(&cache, &sri)) +use ssri::Integrity; + +use crate::content::path; +use crate::errors::Error; + +pub fn read(cache: &Path, sri: &Integrity) -> Result, Error> { + let cpath = path::content_path(&cache, &sri); + let ret = fs::read(&cpath)?; + if sri.clone().check(&ret).is_some() { + Ok(ret) + } else { + Err(Error::IntegrityError) + } } -pub fn read(cache: &Path, sri: &Integrity) -> io::Result> { - fs::read(path::content_path(&cache, &sri)) -} - -pub fn read_to_string(cache: &Path, sri: &Integrity) -> io::Result { - fs::read_to_string(path::content_path(&cache, &sri)) -} - -pub fn copy(cache: &Path, sri: &Integrity, to: &Path) -> io::Result { - fs::copy(path::content_path(&cache, &sri), to) +pub fn copy(cache: &Path, sri: &Integrity, to: &Path) -> Result { + let cpath = path::content_path(&cache, &sri); + let ret = fs::copy(&cpath, to)?; + let data = fs::read(cpath)?; + if sri.clone().check(data).is_some() { + Ok(ret) + } else { + Err(Error::IntegrityError) + } } pub fn has_content(cache: &Path, sri: &Integrity) -> Option { diff --git a/src/errors.rs b/src/errors.rs index 5138f49..ecadcfa 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -6,6 +6,10 @@ use serde_json; #[derive(Fail, Debug)] pub enum Error { + #[fail(display = "not found")] + NotFound, + #[fail(display = "integrity check failed")] + IntegrityError, #[fail(display = "{}", _0)] Io(#[fail(cause)] io::Error), #[fail(display = "{}", _0)] diff --git a/src/get.rs b/src/get.rs new file mode 100644 index 0000000..352498c --- /dev/null +++ b/src/get.rs @@ -0,0 +1,39 @@ +use std::path::Path; + +use ssri::Integrity; + +use crate::content::read; +use crate::errors::Error; +use crate::index::{self, Entry}; + +pub fn read(cache: &Path, key: String) -> Result, Error> { + if let Some(entry) = index::find(&cache, &key)? { + read_hash(cache, &entry.integrity) + } else { + Err(Error::NotFound) + } +} + +pub fn read_hash(cache: &Path, sri: &Integrity) -> Result, Error> { + Ok(read::read(cache, sri)?) +} + +pub fn copy(cache: &Path, key: String, to: &Path) -> Result { + if let Some(entry) = index::find(&cache, &key)? { + copy_hash(cache, &entry.integrity, to) + } else { + Err(Error::NotFound) + } +} + +pub fn copy_hash(cache: &Path, sri: &Integrity, to: &Path) -> Result { + Ok(read::copy(cache, sri, to)?) +} + +pub fn info(cache: &Path, key: String) -> Result, Error> { + index::find(cache, &key) +} + +pub fn hash_exists(cache: &Path, sri: &Integrity) -> bool { + read::has_content(&cache, &sri).is_some() +} diff --git a/src/lib.rs b/src/lib.rs index 6eeee60..2b13582 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,7 @@ -pub mod content; -pub mod index; +mod content; +pub mod get; +mod index; +mod errors; + +pub use errors::Error; +pub use index::Entry;