Skip to content

Commit

Permalink
Return error when layer is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Feb 26, 2024
1 parent c72e304 commit 2a6bc55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libcnb/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::data::launch::ProcessTypeError;
use crate::layer::{
DeleteLayerError, HandleLayerError, ReadLayerError, ReplaceLayerExecdProgramsError,
WriteLayerMetadataError,
ReplaceLayerSbomsError, WriteLayerMetadataError,
};
use libcnb_common::toml_file::TomlFileError;
use std::fmt::Debug;
Expand Down Expand Up @@ -30,7 +30,7 @@ pub enum Error<E> {
ReplaceLayerExecdProgramsError(#[from] ReplaceLayerExecdProgramsError),

#[error("Couldn't replace SBOMs of layer: {0}")]
ReplaceLayerSbomsError(#[from] std::io::Error),
ReplaceLayerSbomsError(#[from] ReplaceLayerSbomsError),

#[error("Process type error: {0}")]
ProcessTypeError(#[from] ProcessTypeError),
Expand Down
36 changes: 31 additions & 5 deletions libcnb/src/layer/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ pub enum WriteLayerError {
#[error("{0}")]
WriteLayerEnvError(#[from] std::io::Error),

#[error("{0}")]
ReplaceLayerSbomsError(#[from] ReplaceLayerSbomsError),

#[error("{0}")]
WriteLayerMetadataError(#[from] WriteLayerMetadataError),

Expand All @@ -261,6 +264,18 @@ pub enum ReplaceLayerExecdProgramsError {

#[error("Couldn't find exec.d file for copying: {0}")]
MissingExecDFile(PathBuf),

#[error("Layer doesn't exist: {0}")]
MissingLayer(LayerName),
}

#[derive(thiserror::Error, Debug)]
pub enum ReplaceLayerSbomsError {
#[error("Layer doesn't exist: {0}")]
MissingLayer(LayerName),

#[error("Unexpected I/O error while replacing layer SBOMs: {0}")]
IoError(#[from] std::io::Error),
}

#[derive(Debug)]
Expand Down Expand Up @@ -293,18 +308,22 @@ pub(crate) fn replace_layer_sboms<P: AsRef<Path>>(
layers_dir: P,
layer_name: &LayerName,
sboms: &[Sbom],
) -> Result<(), std::io::Error> {
) -> Result<(), ReplaceLayerSbomsError> {
let layers_dir = layers_dir.as_ref();

if !layers_dir.join(layer_name.as_str()).is_dir() {
return Err(ReplaceLayerSbomsError::MissingLayer(layer_name.clone()));
}

for format in SBOM_FORMATS {
default_on_not_found(fs::remove_file(cnb_sbom_path(
format,
&layers_dir,
layer_name,
format, layers_dir, layer_name,
)))?;
}

for sbom in sboms {
fs::write(
cnb_sbom_path(&sbom.format, &layers_dir, layer_name),
cnb_sbom_path(&sbom.format, layers_dir, layer_name),
&sbom.data,
)?;
}
Expand All @@ -318,6 +337,13 @@ pub(crate) fn replace_layer_exec_d_programs<P: AsRef<Path>>(
exec_d_programs: &HashMap<String, PathBuf>,
) -> Result<(), ReplaceLayerExecdProgramsError> {
let layer_dir = layers_dir.as_ref().join(layer_name.as_str());

if !layer_dir.is_dir() {
return Err(ReplaceLayerExecdProgramsError::MissingLayer(
layer_name.clone(),
));
}

let exec_d_dir = layer_dir.join("exec.d");

if exec_d_dir.is_dir() {
Expand Down

0 comments on commit 2a6bc55

Please sign in to comment.