Skip to content
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
70 changes: 5 additions & 65 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ impl Archive {
fn unpack_directory(
&self,
loose: &mut BTreeSet<Hash>,
embedded: &mut BTreeMap<RelativePath, Hash>,
embedded: &mut BTreeMap<Hash, Vec<u8>>,
hash: Hash,
prefix: Option<&RelativePath>,
expected_totals: Totals,
) -> Result<DirectoryTree, ArchiveError> {
let directory = self.decode_directory(Some(loose), hash)?;
Expand All @@ -127,23 +126,17 @@ impl Archive {
for (name, entry) in &directory.entries {
let crate_entry = match entry {
Entry::File { hash, size } => {
if self.files.contains_key(hash) {
if let Some(content) = self.files.get(hash) {
loose.remove(hash);
embedded.insert(RelativePath::join_opt(prefix, name), *hash);
embedded.insert(*hash, content.clone());
}
DirectoryTreeEntry::File(File {
hash: *hash,
size: *size,
})
}
Entry::Directory { hash, totals, .. } => {
DirectoryTreeEntry::Directory(self.unpack_directory(
loose,
embedded,
*hash,
Some(&RelativePath::join_opt(prefix, name)),
*totals,
)?)
DirectoryTreeEntry::Directory(self.unpack_directory(loose, embedded, *hash, *totals)?)
}
};
entries.insert(name.clone(), crate_entry);
Expand Down Expand Up @@ -195,7 +188,7 @@ impl Archive {

let mut embedded = BTreeMap::new();

let package = self.unpack_directory(&mut loose, &mut embedded, *hash, None, *totals)?;
let package = self.unpack_directory(&mut loose, &mut embedded, *hash, *totals)?;

let signatures = {
let entry = root
Expand Down Expand Up @@ -232,24 +225,6 @@ impl Archive {
archive_error::LooseFiles { hashes: loose },
}

{
let unexpected = embedded
.keys()
.filter(|path| **path != Metadata::DECO_FILENAME)
.cloned()
.collect::<BTreeSet<RelativePath>>();

ensure! {
unexpected.is_empty(),
archive_error::UnexpectedEmbeddedFiles { paths: unexpected },
}
}

let embedded = embedded
.into_values()
.map(|hash| (hash, self.files[&hash].clone()))
.collect();

Ok((
Manifest {
embedded,
Expand Down Expand Up @@ -798,41 +773,6 @@ mod tests {
);
}

#[test]
fn unexpected_embedded_files() {
let content = b"foo";

let mut package = DirectoryTree::new();
for path in &["bar/bob", "baz"] {
package
.create_file(&path.parse().unwrap(), File::new(content))
.unwrap();
}

let mut builder = ArchiveBuilder::new();

let package = builder.pack_directory(&package).unwrap();

builder.files.insert(Hash::bytes(content), content.to_vec());

let signatures = builder.directory(&Directory::default()).unwrap();

let mut root = Directory::new();
root
.insert_entry("package", package)
.insert_entry("signatures", signatures);

let root = builder.directory(&root).unwrap();

let archive = builder.build(root.hash());

assert_matches!(
archive.unpack(),
Err(ArchiveError::UnexpectedEmbeddedFiles { paths })
if paths.to_string() == "`bar/bob`, `baz`",
);
}

#[test]
fn unexpected_entries() {
let mut builder = ArchiveBuilder::new();
Expand Down
2 changes: 0 additions & 2 deletions src/archive_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub enum ArchiveError {
SignaturesMissing,
#[snafu(display("expected archive `signatures` entry to be directory but found {ty}"))]
SignaturesType { ty: EntryType },
#[snafu(display("archive contains unexpected embedded files: {paths}"))]
UnexpectedEmbeddedFiles { paths: Ticked<RelativePath> },
#[snafu(display("archive contains unexpected entries: {names}"))]
UnexpectedEntries { names: Ticked<ComponentBuf> },
}
6 changes: 0 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,6 @@ pub enum Error {
path: DisplayPath,
source: ArchiveError,
},
#[snafu(display("manifest `{path}` contains unexpected embedded files: {unexpected}"))]
UnexpectedEmbeddedFiles {
backtrace: Option<Backtrace>,
path: DisplayPath,
unexpected: Ticked<RelativePath>,
},
#[snafu(display("invalid video `{path}`"))]
Video {
backtrace: Option<Backtrace>,
Expand Down
19 changes: 0 additions & 19 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ impl Manifest {
Archive::pack(self).unwrap().fingerprint().unwrap()
}

pub(crate) fn from_json(json: &str, path: &Utf8Path) -> Result<Self> {
let manifest =
serde_json::from_str::<Self>(json).context(error::DeserializeManifest { path: &path })?;

let mut unexpected = BTreeSet::new();
for (file_path, file) in manifest.files() {
if manifest.embedded.contains_key(&file.hash) && file_path != Metadata::DECO_FILENAME {
unexpected.insert(file_path);
}
}

ensure! {
unexpected.is_empty(),
error::UnexpectedEmbeddedFiles { path, unexpected },
}

Ok(manifest)
}

pub fn load(path: Option<&Utf8Path>) -> Result<Self> {
Ok(Self::load_with_opt_path(path)?.1)
}
Expand Down
15 changes: 0 additions & 15 deletions src/relative_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ impl RelativePath {
Component::new(self.0.rsplit('/').next().unwrap()).unwrap()
}

pub(crate) fn join(&self, component: &Component) -> Self {
let mut path = self.0.clone();
path.push('/');
path.push_str(component.as_str());
Self(path)
}

pub(crate) fn join_opt(prefix: Option<&Self>, component: &Component) -> Self {
if let Some(prefix) = prefix {
prefix.join(component)
} else {
component.into()
}
}

pub(crate) fn lint(&self, lints: &BTreeSet<Lint>) -> Option<LintError> {
for component in Utf8Path::new(&self.0).components() {
let Utf8Component::Normal(component) = component else {
Expand Down
3 changes: 2 additions & 1 deletion src/subcommand/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ impl Archive {
pub(crate) fn run(self) -> Result {
let json = filesystem::read_to_string(&self.input)?;

let manifest = Manifest::from_json(&json, &self.input)?;
let manifest = serde_json::from_str::<Manifest>(&json)
.context(error::DeserializeManifest { path: &self.input })?;

manifest.save(&self.output)?;

Expand Down
29 changes: 0 additions & 29 deletions tests/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,6 @@ fn rejects_totals_overflow() {
.failure();
}

#[test]
fn rejects_unexpected_embedded_files() {
let content = b"foo";
let hash = Hash::bytes(content).to_string();
let hex_content = hex::encode(content);

Test::new()
.write(
"manifest.json",
json! {
embedded: {
*hash: hex_content
},
package: {
bar: {
hash: hash,
size: 3
}
},
signatures: [],
},
)
.args(["archive", "manifest.json", "manifest.filepack"])
.stderr_regex_path(
"error: manifest `.*manifest.json` contains unexpected embedded files: `bar`\n",
)
.failure();
}

#[test]
fn round_trip() {
Test::new()
Expand Down