diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-12-07 13:31:40 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-12-07 13:31:40 +0100 |
commit | f8b987c9e48a668ed6fdd5201292f9f372f4acbf (patch) | |
tree | 751409f8c8e776972be335f7b57bbbda967a9b76 /src/filestore/path.rs | |
parent | 6aa6154d9f8dfb26da5dd662f021d9dde9b5814c (diff) |
Add checks so that FullArtifactPath can only be constructed for existing files
In the process, FullArtifactPath::is_file() and FullArtifactPath::is_dir()
as well as ArtifactPath::file_stem() were removed.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/filestore/path.rs')
-rw-r--r-- | src/filestore/path.rs | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/src/filestore/path.rs b/src/filestore/path.rs index b2f677b..c691cdb 100644 --- a/src/filestore/path.rs +++ b/src/filestore/path.rs @@ -25,8 +25,18 @@ impl StoreRoot { } } - pub fn join<'a>(&'a self, ap: &'a ArtifactPath) -> FullArtifactPath<'a> { - FullArtifactPath(&self, ap) + pub fn join<'a>(&'a self, ap: &'a ArtifactPath) -> Result<FullArtifactPath<'a>> { + let join = self.0.join(&ap.0); + + if join.is_file() { + Ok(FullArtifactPath(&self, ap)) + } else { + if join.is_dir() { + Err(anyhow!("Cannot load non-file path: {}", join.display())) + } else { + Err(anyhow!("Path does not exist: {}", join.display())) + } + } } pub fn is_file(&self, subpath: &Path) -> bool { @@ -83,10 +93,6 @@ impl ArtifactPath { pub fn to_str(&self) -> Option<&str> { self.0.to_str() } - - pub (in crate::filestore) fn file_stem(&self) -> Option<&OsStr> { - self.0.file_stem() - } } #[derive(Clone, Debug, PartialEq, Eq)] @@ -97,18 +103,14 @@ impl<'a> FullArtifactPath<'a> { self.0.0.join(&self.1.0) } - pub (in crate::filestore) fn is_file(&self) -> bool { - self.joined().is_file() - } - - pub (in crate::filestore) fn is_dir(&self) -> bool { - self.joined().is_dir() - } - pub fn display(&self) -> FullArtifactPathDisplay<'a> { FullArtifactPathDisplay(self.0, self.1) } + pub (in crate::filestore) fn file_stem(&self) -> Option<&OsStr> { + self.1.0.file_stem() + } + pub async fn read(self) -> Result<Vec<u8>> { tokio::fs::read(self.joined()) .await |