Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/initramfs/create valid image #18

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion initramfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ env_logger = { version = "0.10.0" }
flate2 = { version = "1.0.25" }
tar = { version = "0.4.38" }
bytes = "1.4.0"
libflate = "0.1"
libflate = "2.0.0"
cpio = "0.2.2"
openssl = { version = "0.10", features = ["vendored"] }
50 changes: 39 additions & 11 deletions initramfs/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::{anyhow, Result};
use bytes::Bytes;
use cpio::{newc::Builder, write_cpio};
use libflate::gzip::{Decoder, Encoder};
use log::debug;
use log::{debug, info};
use serde::Deserialize;
use tar::Archive;

Expand Down Expand Up @@ -95,9 +95,9 @@ impl Image {
)
.map_err(|e| anyhow!(e).context("Failed to create gzip encoder"))?;

let mut entries = HashMap::new();
let mut entries: HashMap<String, (Builder, Cursor<Vec<u8>>)> = HashMap::new();

for layer in self.layers.clone() {
for layer in self.layers.clone().into_iter() {
let mut archive = Archive::new(Decoder::new(layer.content)?);

for entry in archive
Expand All @@ -113,7 +113,21 @@ impl Image {
.ok_or_else(|| anyhow!("Failed to convert path to string"))?
.to_string();

if entries.contains_key(&path) {
// This means we need to delete everything that is in the parent directory
if path.contains(".wh..wh..opq") {
debug!("Found opaque whiteout file : {}", &path);

let parent = path.trim_end_matches(".wh..wh..opq");
let keys = entries
.keys()
.filter(|key| key.starts_with(parent))
.cloned()
.collect::<Vec<_>>();

keys.iter().for_each(|key| {
entries.remove(key);
});

continue;
}

Expand All @@ -139,9 +153,21 @@ impl Image {
debug!("Adding {} to archive", &path);

let mut contents = Vec::new();
entry
.read_to_end(&mut contents)
.map_err(|e| anyhow!(e).context("Failed to read entry"))?;
if headers.entry_type().is_symlink() {
let link_path = headers
.link_name()
.map_err(|e| anyhow!(e).context("Failed to get link name of entry"))?
.ok_or(anyhow!("Failed to get link name of entry"))?
.to_str()
.ok_or_else(|| anyhow!("Failed to convert link name to string"))?
.to_string();

contents.extend_from_slice(link_path.as_bytes());
} else {
entry
.read_to_end(&mut contents)
.map_err(|e| anyhow!(e).context("Failed to read entry"))?;
}

entries.insert(path, (builder, Cursor::new(contents)));
}
Expand Down Expand Up @@ -181,11 +207,13 @@ impl Image {
),
);

let test = entries
.drain()
.map(|(_, (builder, contents))| (builder, contents));
info!("Writing cpio to disk");

let inputs = entries.drain().map(|(_, data)| data);

let archive =
write_cpio(test, archive).map_err(|e| anyhow!(e).context("Failed to write cpio"))?;
write_cpio(inputs, archive).map_err(|e| anyhow!(e).context("Failed to write cpio"))?;

let handler = archive
.finish()
.into_result()
Expand Down
Loading