Skip to content

Commit b3c0ae2

Browse files
allisonkarlitskayacgwalters
authored andcommitted
image: introduce error type and drop anyhow
Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent 49cc4d7 commit b3c0ae2

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/image.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{cell::RefCell, collections::BTreeMap, ffi::OsStr, path::Path, rc::Rc};
22

3-
use anyhow::{bail, Context, Result};
3+
use thiserror::Error;
44

55
use crate::fsverity::Sha256HashValue;
66

@@ -47,6 +47,18 @@ pub enum Inode {
4747
Leaf(Rc<Leaf>),
4848
}
4949

50+
#[derive(Error, Debug)]
51+
pub enum ImageError {
52+
#[error("Directory entry {0:?} does not exist")]
53+
NotFound(Box<OsStr>),
54+
#[error("Directory entry {0:?} is not a subdirectory")]
55+
NotADirectory(Box<OsStr>),
56+
#[error("Directory entry {0:?} is a directory")]
57+
IsADirectory(Box<OsStr>),
58+
#[error("Directory entry {0:?} is not a regular file")]
59+
IsNotRegular(Box<OsStr>),
60+
}
61+
5062
impl Inode {
5163
pub fn stat(&self) -> &Stat {
5264
match self {
@@ -64,11 +76,11 @@ impl Directory {
6476
}
6577
}
6678

67-
pub fn recurse(&mut self, name: impl AsRef<OsStr>) -> Result<&mut Directory> {
79+
pub fn recurse(&mut self, name: impl AsRef<OsStr>) -> Result<&mut Directory, ImageError> {
6880
match self.entries.get_mut(name.as_ref()) {
6981
Some(Inode::Directory(subdir)) => Ok(subdir),
70-
Some(_) => bail!("Parent directory is not a directory"),
71-
None => bail!("Unable to find parent directory {:?}", name.as_ref()),
82+
Some(_) => Err(ImageError::NotADirectory(name.as_ref().into())),
83+
None => Err(ImageError::NotFound(name.as_ref().into())),
7284
}
7385
}
7486

@@ -90,11 +102,11 @@ impl Directory {
90102
self.entries.insert(name.into(), inode);
91103
}
92104

93-
pub fn get_for_link(&self, name: &OsStr) -> Result<Rc<Leaf>> {
105+
pub fn get_for_link(&self, name: &OsStr) -> Result<Rc<Leaf>, ImageError> {
94106
match self.entries.get(name) {
95107
Some(Inode::Leaf(leaf)) => Ok(Rc::clone(leaf)),
96-
Some(Inode::Directory(..)) => bail!("Cannot hardlink to directory"),
97-
None => bail!("Attempt to hardlink to non-existent file"),
108+
Some(Inode::Directory(..)) => Err(ImageError::IsADirectory(name.into())),
109+
None => Err(ImageError::NotFound(name.into())),
98110
}
99111
}
100112

@@ -145,7 +157,7 @@ impl FileSystem {
145157
}
146158
}
147159

148-
fn get_parent_dir<'a>(&'a mut self, name: &Path) -> Result<&'a mut Directory> {
160+
fn get_parent_dir<'a>(&'a mut self, name: &Path) -> Result<&'a mut Directory, ImageError> {
149161
let mut dir = &mut self.root;
150162

151163
if let Some(parent) = name.parent() {
@@ -154,24 +166,22 @@ impl FileSystem {
154166
// Path.parent() is really weird...
155167
continue;
156168
}
157-
dir = dir
158-
.recurse(segment)
159-
.with_context(|| format!("Trying to insert item {:?}", name))?;
169+
dir = dir.recurse(segment)?;
160170
}
161171
}
162172

163173
Ok(dir)
164174
}
165175

166-
pub fn mkdir(&mut self, name: &Path, stat: Stat) -> Result<()> {
176+
pub fn mkdir(&mut self, name: &Path, stat: Stat) -> Result<(), ImageError> {
167177
if let Some(filename) = name.file_name() {
168178
let dir = self.get_parent_dir(name)?;
169179
dir.mkdir(filename, stat);
170180
}
171181
Ok(())
172182
}
173183

174-
pub fn insert_rc(&mut self, name: &Path, leaf: Rc<Leaf>) -> Result<()> {
184+
pub fn insert_rc(&mut self, name: &Path, leaf: Rc<Leaf>) -> Result<(), ImageError> {
175185
if let Some(filename) = name.file_name() {
176186
let dir = self.get_parent_dir(name)?;
177187
dir.insert(filename, Inode::Leaf(leaf));
@@ -181,11 +191,11 @@ impl FileSystem {
181191
}
182192
}
183193

184-
pub fn insert(&mut self, name: &Path, leaf: Leaf) -> Result<()> {
194+
pub fn insert(&mut self, name: &Path, leaf: Leaf) -> Result<(), ImageError> {
185195
self.insert_rc(name, Rc::new(leaf))
186196
}
187197

188-
fn get_for_link(&mut self, name: &Path) -> Result<Rc<Leaf>> {
198+
fn get_for_link(&mut self, name: &Path) -> Result<Rc<Leaf>, ImageError> {
189199
if let Some(filename) = name.file_name() {
190200
let dir = self.get_parent_dir(name)?;
191201
dir.get_for_link(filename)
@@ -194,12 +204,12 @@ impl FileSystem {
194204
}
195205
}
196206

197-
pub fn hardlink(&mut self, name: &Path, target: &OsStr) -> Result<()> {
207+
pub fn hardlink(&mut self, name: &Path, target: &OsStr) -> Result<(), ImageError> {
198208
let rc = self.get_for_link(Path::new(target))?;
199209
self.insert_rc(name, rc)
200210
}
201211

202-
pub fn remove(&mut self, name: &Path) -> Result<()> {
212+
pub fn remove(&mut self, name: &Path) -> Result<(), ImageError> {
203213
if let Some(filename) = name.file_name() {
204214
let dir = self.get_parent_dir(name)?;
205215
dir.remove(filename);

0 commit comments

Comments
 (0)