rustic_core
rustic_core copied to clipboard
Let callers handle errors
If we encounter an error, we should return an error and let the caller handle it. In the following code example, we encounter errors, but instead of returning them as such, we are returning Ok(()) and logging an error. IMHO, logging it is fine, but we should also return an Error in case of encountering one. We should fix that all over the code base.
// check header
let header = be.decrypt(&data.split_off(data.len() - header_len as usize))?;
let pack_blobs = PackHeader::from_binary(&header)?.into_blobs();
let mut blobs = index_pack.blobs;
blobs.sort_unstable_by_key(|b| b.offset);
if pack_blobs != blobs {
error!("pack {id}: Header from pack file does not match the index");
debug!("pack file header: {pack_blobs:?}");
debug!("index: {:?}", blobs);
return Ok(());
}
p.inc(u64::from(header_len) + 4);
// check blobs
for blob in blobs {
let blob_id = blob.id;
let mut blob_data = be.decrypt(&data.split_to(blob.length as usize))?;
// TODO: this is identical to backend/decrypt.rs; unify these two parts!
if let Some(length) = blob.uncompressed_length {
blob_data = decode_all(&*blob_data)?;
if blob_data.len() != length.get() as usize {
error!("pack {id}, blob {blob_id}: Actual uncompressed length does not fit saved uncompressed length");
return Ok(());
}
}
let comp_id = hash(&blob_data);
if blob.id != comp_id {
error!("pack {id}, blob {blob_id}: Hash mismatch. Computed hash: {comp_id}");
return Ok(());
}
p.inc(blob.length.into());
other example:
fn set_metadata(self, dest: &LocalDestination, path: &PathBuf, node: &Node) {
debug!("setting metadata for {:?}", path);
dest.create_special(path, node)
.unwrap_or_else(|_| warn!("restore {:?}: creating special file failed.", path));
match (self.no_ownership, self.numeric_id) {
(true, _) => {}
(false, true) => dest
.set_uid_gid(path, &node.meta)
.unwrap_or_else(|_| warn!("restore {:?}: setting UID/GID failed.", path)),
(false, false) => dest
.set_user_group(path, &node.meta)
.unwrap_or_else(|_| warn!("restore {:?}: setting User/Group failed.", path)),
}
dest.set_permission(path, node)
.unwrap_or_else(|_| warn!("restore {:?}: chmod failed.", path));
dest.set_extended_attributes(path, &node.meta.extended_attributes)
.unwrap_or_else(|_| warn!("restore {:?}: setting extended attributes failed.", path));
dest.set_times(path, &node.meta)
.unwrap_or_else(|_| warn!("restore {:?}: setting file times failed.", path));
}
fn set_metadata(self, dest: &LocalDestination, path: &PathBuf, node: &Node) {