Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Apr 24, 2024
1 parent 109069b commit ce3d180
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ExportableLayersTar implements Closeable {
Files.copy(inputStream, this.path, StandardCopyOption.REPLACE_EXISTING);
ImageArchiveManifest manifest = null;
ImageArchiveIndex index = null;
try (TarArchiveInputStream tar = openTar()) {
try (TarArchiveInputStream tar = openTar(this.path)) {
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
if ("manifest.json".equals(entry.getName())) {
Expand All @@ -80,18 +80,18 @@ class ExportableLayersTar implements Closeable {
}

private Map<String, String> getLayerDigestMediaTypes(ImageArchiveIndex index) throws IOException {
Map<String, String> digestMediaTypes = new HashMap<>();
Set<String> distributionManifestListDigests = getDistributionManifestListDigests(index);
List<DistributionManifestList> distributionManifestLists = getDistributionManifestLists(this.path,
distributionManifestListDigests);
List<DistributionManifest> distributionManifests = getDistributionManifests(this.path,
distributionManifestLists);
Map<String, String> digestMediaTypes = new HashMap<>();
for (DistributionManifest distributionManifest : distributionManifests) {
for (DistributionManifest.Layer layer : distributionManifest.getLayers()) {
digestMediaTypes.put(layer.getDigest(), layer.getMediaType());
}
}
return digestMediaTypes;
return Collections.unmodifiableMap(digestMediaTypes);
}

private static Set<String> getDistributionManifestListDigests(ImageArchiveIndex index) {
Expand All @@ -102,7 +102,7 @@ private static Set<String> getDistributionManifestListDigests(ImageArchiveIndex
digests.add(manifest.getDigest());
}
}
return digests;
return Collections.unmodifiableSet(digests);
}

private static List<DistributionManifestList> getDistributionManifestLists(Path path, Set<String> digests)
Expand All @@ -111,7 +111,7 @@ private static List<DistributionManifestList> getDistributionManifestLists(Path
return Collections.emptyList();
}
List<DistributionManifestList> distributionManifestLists = new ArrayList<>();
try (TarArchiveInputStream tar = new TarArchiveInputStream(Files.newInputStream(path))) {
try (TarArchiveInputStream tar = openTar(path)) {
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
if (isDigestMatch(entry, digests)) {
Expand All @@ -138,7 +138,7 @@ private List<DistributionManifest> getDistributionManifests(Path path,
}
}
List<DistributionManifest> distributionManifests = new ArrayList<>();
try (TarArchiveInputStream tar = new TarArchiveInputStream(Files.newInputStream(path))) {
try (TarArchiveInputStream tar = openTar(path)) {
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
if (isDigestMatch(entry, digests)) {
Expand Down Expand Up @@ -172,7 +172,7 @@ private static <T> T readJson(TarArchiveInputStream in, ThrowingFunction<InputSt
}

void export(IOBiConsumer<String, TarArchive> exports) throws IOException {
try (TarArchiveInputStream tar = openTar()) {
try (TarArchiveInputStream tar = openTar(this.path)) {
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
if (isLayer(entry)) {
Expand All @@ -197,12 +197,11 @@ private Compression getLayerCompression(TarArchiveEntry entry) {
}

private boolean isLayer(TarArchiveEntry entry) {
// FIXME also check layerDigestMediaTypes?
return this.manifest.getEntries().stream().anyMatch((content) -> content.getLayers().contains(entry.getName()));
}

private TarArchiveInputStream openTar() throws IOException {
return new TarArchiveInputStream(Files.newInputStream(this.path));
private static TarArchiveInputStream openTar(Path path) throws IOException {
return new TarArchiveInputStream(Files.newInputStream(path));
}

@Override
Expand Down

0 comments on commit ce3d180

Please sign in to comment.