Skip to content

Commit

Permalink
Expose overwrite_layer_exec_d_programs in BuildContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Feb 15, 2024
1 parent 789cc29 commit a7a3927
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
16 changes: 16 additions & 0 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
crate::layer::delete_layer(&self.layers_dir, layer_name.as_ref())
.map_err(crate::Error::DeleteLayerError)
}

pub fn overwrite_layer_exec_d_programs<I>(
&self,
layer_name: impl AsRef<LayerName>,
exec_d_programs: I,
) -> crate::Result<(), B::Error>
where
I: IntoIterator<Item = (String, PathBuf)>,
{
crate::layer::overwrite_layer_exec_d_programs(
&self.layers_dir,
layer_name.as_ref(),
&exec_d_programs.into_iter().collect(),
)
.map_err(crate::Error::OverwriteLayerExecdError)
}
}

/// Describes the result of the build phase.
Expand Down
5 changes: 4 additions & 1 deletion libcnb/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::data::launch::ProcessTypeError;
use crate::layer::{DeleteLayerError, HandleLayerError, ReadLayerError};
use crate::layer::{DeleteLayerError, HandleLayerError, OverwriteLayerExecdError, ReadLayerError};
use libcnb_common::toml_file::TomlFileError;
use std::fmt::Debug;

Expand All @@ -20,6 +20,9 @@ pub enum Error<E> {
#[error("Couldn't delete layer: {0}")]
DeleteLayerError(#[from] DeleteLayerError),

#[error("Couldn't write exec.d programs to layer: {0}")]
OverwriteLayerExecdError(#[from] OverwriteLayerExecdError),

#[error("Process type error: {0}")]
ProcessTypeError(#[from] ProcessTypeError),

Expand Down
18 changes: 10 additions & 8 deletions libcnb/src/layer/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ pub enum WriteLayerError {
TomlFileError(#[from] TomlFileError),

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

#[derive(thiserror::Error, Debug)]
pub enum WriteLayerExecdError {
pub enum OverwriteLayerExecdError {
#[error("Unexpected I/O error while writing layer execd programs: {0}")]
IoError(#[from] std::io::Error),

Expand Down Expand Up @@ -303,11 +303,11 @@ fn write_layer_sboms<P: AsRef<Path>>(
Ok(())
}

fn write_layer_exec_d<P: AsRef<Path>>(
pub(crate) fn overwrite_layer_exec_d_programs<P: AsRef<Path>>(
layers_dir: P,
layer_name: &LayerName,
exec_d_programs: &HashMap<String, PathBuf>,
) -> Result<(), WriteLayerExecdError> {
) -> Result<(), OverwriteLayerExecdError> {
let layer_dir = layers_dir.as_ref().join(layer_name.as_str());
let exec_d_dir = layer_dir.join("exec.d");

Expand All @@ -326,9 +326,9 @@ fn write_layer_exec_d<P: AsRef<Path>>(
// buildpack author might not be aware of.
Some(&path)
.filter(|path| path.exists())
.ok_or_else(|| WriteLayerExecdError::MissingExecDFile(path.clone()))
.ok_or_else(|| OverwriteLayerExecdError::MissingExecDFile(path.clone()))
.and_then(|path| {
fs::copy(path, exec_d_dir.join(name)).map_err(WriteLayerExecdError::IoError)
fs::copy(path, exec_d_dir.join(name)).map_err(OverwriteLayerExecdError::IoError)
})?;
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ fn write_layer<M: Serialize, P: AsRef<Path>>(

match layer_exec_d_programs {
ExecDPrograms::Overwrite(exec_d_programs) => {
write_layer_exec_d(layers_dir.as_ref(), layer_name, &exec_d_programs)?;
overwrite_layer_exec_d_programs(layers_dir.as_ref(), layer_name, &exec_d_programs)?;
}
ExecDPrograms::Keep => {}
}
Expand Down Expand Up @@ -568,7 +568,9 @@ mod tests {
.unwrap_err();

match write_layer_error {
WriteLayerError::WriteLayerExecdError(WriteLayerExecdError::MissingExecDFile(path)) => {
WriteLayerError::OverwriteLayerExecdError(
OverwriteLayerExecdError::MissingExecDFile(path),
) => {
assert_eq!(path, execd_file);
}
other => {
Expand Down

0 comments on commit a7a3927

Please sign in to comment.