Skip to content

Commit

Permalink
Allow compilation on non-unix family systems
Browse files Browse the repository at this point in the history
Previously, there were several places in the code where `cfg` attributes
where causing compile errors on Windows (when bypassing `libcnb_runtime`
and using `libcnb_runtime_detect` and `libcnb_runtime_build` directly).
This fixes those compile errors (checked using cross-compile on Linux).
  • Loading branch information
nokome committed Feb 13, 2022
1 parent af5cfec commit 638e74d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 16 additions & 5 deletions libcnb/src/layer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,15 @@ impl LayerEnvDelta {
// explicitly written in the spec, we read through the the reference implementation and
// determined that it also treats the file contents as raw bytes.
// See: https://github.com/buildpacks/lifecycle/blob/a7428a55c2a14d8a37e84285b95dc63192e3264e/env/env.go#L73-L106
use std::os::unix::ffi::OsStringExt;
let path = dir_entry?.path();
let file_contents = OsString::from_vec(fs::read(&path)?);

#[cfg(target_family = "unix")]
let file_contents = {
use std::os::unix::ffi::OsStringExt;
OsString::from_vec(fs::read(&path)?)
};
#[cfg(not(target_family = "unix"))]
let file_contents = OsString::from(&fs::read_to_string(&path)?);

// Rely on the Rust standard library for splitting stem and extension. Since paths
// are not necessarily UTF-8 encoded, this is not as trivial as it might look like.
Expand Down Expand Up @@ -549,8 +555,6 @@ impl LayerEnvDelta {
}

fn write_to_env_dir(&self, path: impl AsRef<Path>) -> Result<(), std::io::Error> {
use std::os::unix::ffi::OsStrExt;

if path.as_ref().exists() {
// This is a possible race condition if the path is deleted between the check and
// removal by this code. We accept this for now to keep it simple.
Expand All @@ -573,7 +577,14 @@ impl LayerEnvDelta {

let file_path = path.as_ref().join(file_name);

fs::write(file_path, &value.as_bytes())?;
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
fs::write(file_path, &value.as_bytes())?;
}

#[cfg(not(target_family = "unix"))]
fs::write(file_path, &value.to_string_lossy().as_bytes())?;
}

Ok(())
Expand Down
1 change: 0 additions & 1 deletion libcnb/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub fn libcnb_runtime<B: Buildpack>(buildpack: &B) {
.and_then(Path::file_name)
.and_then(OsStr::to_str);

#[cfg(any(target_family = "unix"))]
let result = match current_exe_file_name {
Some("detect") => libcnb_runtime_detect(buildpack, parse_detect_args_or_exit()),
Some("build") => libcnb_runtime_build(buildpack, parse_build_args_or_exit()),
Expand Down

0 comments on commit 638e74d

Please sign in to comment.