Skip to content

Commit

Permalink
review: only one convert_whiteout function
Browse files Browse the repository at this point in the history
  • Loading branch information
squarti committed Aug 22, 2024
1 parent 819f183 commit 6e7cae3
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions image-rs/src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use filetime::FileTime;
use futures::StreamExt;
use log::warn;
Expand All @@ -25,52 +25,51 @@ use tokio_tar::ArchiveBuilder;
const WHITEOUT_PREFIX: &str = ".wh.";
const WHITEOUT_OPAQUE_DIR: &str = ".wh..wh..opq";

#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "macos")))]
fn convert_whiteout(path: &Path, uid: u32, gid: u32, mode: Option<u32>, destination: &Path) -> Result<bool> {
Ok(true)
}
async fn convert_whiteout(
path: &Path,
uid: u32,
gid: u32,
mode: Option<u32>,
destination: &Path,
) -> Result<bool> {
if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") || cfg!(target_os = "macos") {
// Handle whiteout conversion
let name = path
.file_name()
.unwrap_or_default()
.to_str()
.ok_or(anyhow!("Invalid unicode in whiteout path: {:?}", path))?;

if name.starts_with(WHITEOUT_PREFIX) {
let parent = path
.parent()
.ok_or(anyhow!("Invalid whiteout parent for path: {:?}", path))?;

if name == WHITEOUT_OPAQUE_DIR {
let destination_parent = destination.join(parent);
xattr::set(destination_parent, "trusted.overlay.opaque", b"y")?;
return Ok(false);
}

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
use anyhow::anyhow;

#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
async fn convert_whiteout(path: &Path, uid: u32, gid: u32, mode: Option<u32>, destination: &Path) -> Result<bool> {
// Handle whiteout conversion
let name = path
.file_name()
.unwrap_or_default()
.to_str()
.ok_or(anyhow!("Invalid unicode in whiteout path: {:?}", path))?;

if name.starts_with(WHITEOUT_PREFIX) {
let parent = path
.parent()
.ok_or(anyhow!("Invalid whiteout parent for path: {:?}", path))?;

if name == WHITEOUT_OPAQUE_DIR {
let destination_parent = destination.join(parent);
xattr::set(destination_parent, "trusted.overlay.opaque", b"y")?;
return Ok(false);
}
let original_name = name
.strip_prefix(WHITEOUT_PREFIX)
.ok_or(anyhow!("Failed to strip whiteout prefix for: {}", name))?;
let original_path = parent.join(original_name);
let path = CString::new(format!(
"{}/{}",
destination.display(),
original_path.display()
))?;

let ret = unsafe { nix::libc::mknod(path.as_ptr(), nix::libc::S_IFCHR, 0) };
if ret != 0 {
bail!("mknod: {:?} error: {:?}", path, io::Error::last_os_error());
}

let original_name = name
.strip_prefix(WHITEOUT_PREFIX)
.ok_or(anyhow!("Failed to strip whiteout prefix for: {}", name))?;
let original_path = parent.join(original_name);
let path = CString::new(format!(
"{}/{}",
destination.display(),
original_path.display()
))?;
set_perms_ownerships(&path, ChownType::LChown, uid, gid, mode).await?;

let ret = unsafe { nix::libc::mknod(path.as_ptr(), nix::libc::S_IFCHR, 0) };
if ret != 0 {
bail!("mknod: {:?} error: {:?}", path, io::Error::last_os_error());
return Ok(false);
}

set_perms_ownerships(&path, ChownType::LChown, uid, gid, mode).await?;

return Ok(false);
}

Ok(true)
Expand Down

0 comments on commit 6e7cae3

Please sign in to comment.