Skip to content

Commit

Permalink
Merge pull request ostreedev#217 from lucab/ups/tar-export-skeleton-repo
Browse files Browse the repository at this point in the history
tar/export: add additional mandatory ostree repo directories
  • Loading branch information
cgwalters authored Jan 25, 2022
2 parents 617b6c7 + 4266428 commit 8274bd0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
14 changes: 12 additions & 2 deletions lib/src/tar/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> {
if self.wrote_initdirs {
return Ok(());
}
self.wrote_initdirs = true;

let objdir: Utf8PathBuf = format!("{}/repo/objects", OSTREEDIR).into();
// Add all parent directories
Expand All @@ -128,10 +127,20 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> {
self.append_default_dir(path)?;
}
// Object subdirectories
for d in 0..0xFF {
for d in 0..=0xFF {
let path: Utf8PathBuf = format!("{}/{:02x}", objdir, d).into();
self.append_default_dir(&path)?;
}
// Tmp subdirectories
for d in ["tmp", "tmp/cache"] {
let path: Utf8PathBuf = format!("{}/repo/{}", OSTREEDIR, d).into();
self.append_default_dir(&path)?;
}
// Refs subdirectories
for d in ["refs", "refs/heads", "refs/mirrors", "refs/remotes"] {
let path: Utf8PathBuf = format!("{}/repo/{}", OSTREEDIR, d).into();
self.append_default_dir(&path)?;
}

// The special `repo/xattrs` directory used only in our tar serialization.
let path: Utf8PathBuf = format!("{}/repo/xattrs", OSTREEDIR).into();
Expand All @@ -150,6 +159,7 @@ impl<'a, W: std::io::Write> OstreeTarWriter<'a, W> {
self.out
.append_data(&mut h, path, std::io::Cursor::new(REPO_CONFIG))?;

self.wrote_initdirs = true;
Ok(())
}

Expand Down
80 changes: 59 additions & 21 deletions lib/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ostree_ext::container::{
use ostree_ext::tar::TarImportOptions;
use ostree_ext::{gio, glib};
use sh_inline::bash;
use std::collections::HashMap;
use std::convert::TryInto;
use std::{io::Write, process::Command};

Expand Down Expand Up @@ -237,6 +238,7 @@ async fn test_tar_import_signed() -> Result<()> {
Ok(())
}

#[derive(Debug)]
struct TarExpected {
path: &'static str,
etype: tar::EntryType,
Expand All @@ -257,33 +259,41 @@ fn validate_tar_expected<T: std::io::Read>(
t: tar::Entries<T>,
expected: impl IntoIterator<Item = TarExpected>,
) -> Result<()> {
let expected = expected.into_iter();
let mut entries = t.map(|e| e.unwrap());
let mut expected: HashMap<&'static str, TarExpected> =
expected.into_iter().map(|exp| (exp.path, exp)).collect();
let entries = t.map(|e| e.unwrap());
// Verify we're injecting directories, fixes the absence of `/tmp` in our
// images for example.
for exp in expected {
let mut found = false;
while let Some(entry) = entries.next() {
let header = entry.header();
let entry_path = entry.path().unwrap();
if exp.path == entry_path.as_os_str() {
assert_eq!(header.entry_type(), exp.etype);
assert_eq!(header.mode().unwrap(), exp.mode);
found = true;
break;
}
}
if !found {
anyhow::bail!("Failed to find entry: {}", exp.path);
for entry in entries {
let header = entry.header();
let entry_path = entry.path().unwrap().to_string_lossy().into_owned();
if let Some(exp) = expected.remove(entry_path.as_str()) {
assert_eq!(header.entry_type(), exp.etype, "{}", entry_path);
// FIXME: change the generation code to not inject the format bits into the mode,
// because tar doesn't need/use it.
// https://github.com/ostreedev/ostree-rs-ext/pull/217/files#r791942496
assert_eq!(
header.mode().unwrap() & !libc::S_IFMT,
exp.mode,
"{}",
entry_path
);
}
}

assert!(
expected.is_empty(),
"Expected but not found:\n{:?}",
expected
);
Ok(())
}

/// Validate basic structure of the tar export.
/// Right now just checks the first entry is `sysroot` with mode 0755.
#[test]
fn test_tar_export_structure() -> Result<()> {
use tar::EntryType::{Directory, Regular};

let mut fixture = Fixture::new()?;
let src_tar = initial_export(&fixture)?;
let src_tar = std::io::BufReader::new(std::fs::File::open(&src_tar)?);
Expand All @@ -299,8 +309,22 @@ fn test_tar_export_structure() -> Result<()> {

// Validate format version 0
let expected = [
("sysroot/config", tar::EntryType::Regular, 0o644),
("usr", tar::EntryType::Directory, libc::S_IFDIR | 0o755),
("sysroot/config", Regular, 0o644),
("sysroot/ostree/repo", Directory, 0o755),
("sysroot/ostree/repo/objects/00", Directory, 0o755),
("sysroot/ostree/repo/objects/23", Directory, 0o755),
("sysroot/ostree/repo/objects/77", Directory, 0o755),
("sysroot/ostree/repo/objects/bc", Directory, 0o755),
("sysroot/ostree/repo/objects/ff", Directory, 0o755),
("sysroot/ostree/repo/refs", Directory, 0o755),
("sysroot/ostree/repo/refs", Directory, 0o755),
("sysroot/ostree/repo/refs/heads", Directory, 0o755),
("sysroot/ostree/repo/refs/mirrors", Directory, 0o755),
("sysroot/ostree/repo/refs/remotes", Directory, 0o755),
("sysroot/ostree/repo/tmp", Directory, 0o755),
("sysroot/ostree/repo/tmp/cache", Directory, 0o755),
("sysroot/ostree/repo/xattrs", Directory, 0o755),
("usr", Directory, 0o755),
];
validate_tar_expected(entries, expected.iter().map(Into::into))?;

Expand All @@ -310,8 +334,22 @@ fn test_tar_export_structure() -> Result<()> {
let src_tar = std::io::BufReader::new(std::fs::File::open(&src_tar)?);
let mut src_tar = tar::Archive::new(src_tar);
let expected = [
("sysroot/ostree/repo/config", tar::EntryType::Regular, 0o644),
("usr", tar::EntryType::Directory, libc::S_IFDIR | 0o755),
("sysroot/ostree/repo", Directory, 0o755),
("sysroot/ostree/repo/config", Regular, 0o644),
("sysroot/ostree/repo/objects/00", Directory, 0o755),
("sysroot/ostree/repo/objects/23", Directory, 0o755),
("sysroot/ostree/repo/objects/77", Directory, 0o755),
("sysroot/ostree/repo/objects/bc", Directory, 0o755),
("sysroot/ostree/repo/objects/ff", Directory, 0o755),
("sysroot/ostree/repo/refs", Directory, 0o755),
("sysroot/ostree/repo/refs", Directory, 0o755),
("sysroot/ostree/repo/refs/heads", Directory, 0o755),
("sysroot/ostree/repo/refs/mirrors", Directory, 0o755),
("sysroot/ostree/repo/refs/remotes", Directory, 0o755),
("sysroot/ostree/repo/tmp", Directory, 0o755),
("sysroot/ostree/repo/tmp/cache", Directory, 0o755),
("sysroot/ostree/repo/xattrs", Directory, 0o755),
("usr", Directory, 0o755),
];
validate_tar_expected(src_tar.entries()?, expected.iter().map(Into::into))?;

Expand Down

0 comments on commit 8274bd0

Please sign in to comment.