|
18 | 18 |
|
19 | 19 | import java.io.BufferedReader;
|
20 | 20 | import java.io.ByteArrayInputStream;
|
| 21 | +import java.io.FileInputStream; |
21 | 22 | import java.io.IOException;
|
22 | 23 | import java.io.InputStream;
|
23 | 24 | import java.io.InputStreamReader;
|
|
30 | 31 | import java.util.Arrays;
|
31 | 32 | import java.util.Collection;
|
32 | 33 | import java.util.Collections;
|
33 |
| -import java.util.HashMap; |
34 | 34 | import java.util.List;
|
35 |
| -import java.util.Map; |
36 | 35 | import java.util.Objects;
|
37 | 36 | import java.util.stream.Collectors;
|
38 | 37 |
|
@@ -293,29 +292,20 @@ public void exportLayerFiles(ImageReference reference, IOBiConsumer<String, Path
|
293 | 292 | Assert.notNull(exports, "Exports must not be null");
|
294 | 293 | URI saveUri = buildUrl("/images/" + reference + "/get");
|
295 | 294 | Response response = http().get(saveUri);
|
296 |
| - ImageArchiveManifest manifest = null; |
297 |
| - Map<String, Path> layerFiles = new HashMap<>(); |
298 |
| - try (TarArchiveInputStream tar = new TarArchiveInputStream(response.getContent())) { |
| 295 | + Path exportFile = copyToTemp(response.getContent()); |
| 296 | + ImageArchiveManifest manifest = getManifest(reference, exportFile); |
| 297 | + try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) { |
299 | 298 | TarArchiveEntry entry = tar.getNextEntry();
|
300 | 299 | while (entry != null) {
|
301 |
| - if (entry.getName().equals("manifest.json")) { |
302 |
| - manifest = readManifest(tar); |
303 |
| - } |
304 |
| - if (entry.getName().endsWith(".tar")) { |
305 |
| - layerFiles.put(entry.getName(), copyToTemp(tar)); |
| 300 | + if (manifestContainsLayerEntry(manifest, entry.getName())) { |
| 301 | + Path layerFile = copyToTemp(tar); |
| 302 | + exports.accept(entry.getName(), layerFile); |
| 303 | + Files.delete(layerFile); |
306 | 304 | }
|
307 | 305 | entry = tar.getNextEntry();
|
308 | 306 | }
|
309 | 307 | }
|
310 |
| - Assert.notNull(manifest, "Manifest not found in image " + reference); |
311 |
| - for (Map.Entry<String, Path> entry : layerFiles.entrySet()) { |
312 |
| - String name = entry.getKey(); |
313 |
| - Path path = entry.getValue(); |
314 |
| - if (manifestContainsLayerEntry(manifest, name)) { |
315 |
| - exports.accept(name, path); |
316 |
| - } |
317 |
| - Files.delete(path); |
318 |
| - } |
| 308 | + Files.delete(exportFile); |
319 | 309 | }
|
320 | 310 |
|
321 | 311 | /**
|
@@ -355,13 +345,26 @@ public void tag(ImageReference sourceReference, ImageReference targetReference)
|
355 | 345 | http().post(uri).close();
|
356 | 346 | }
|
357 | 347 |
|
| 348 | + private ImageArchiveManifest getManifest(ImageReference reference, Path exportFile) throws IOException { |
| 349 | + try (TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(exportFile.toFile()))) { |
| 350 | + TarArchiveEntry entry = tar.getNextEntry(); |
| 351 | + while (entry != null) { |
| 352 | + if (entry.getName().equals("manifest.json")) { |
| 353 | + return readManifest(tar); |
| 354 | + } |
| 355 | + entry = tar.getNextEntry(); |
| 356 | + } |
| 357 | + } |
| 358 | + throw new IllegalArgumentException("Manifest not found in image " + reference); |
| 359 | + } |
| 360 | + |
358 | 361 | private ImageArchiveManifest readManifest(TarArchiveInputStream tar) throws IOException {
|
359 | 362 | String manifestContent = new BufferedReader(new InputStreamReader(tar, StandardCharsets.UTF_8)).lines()
|
360 | 363 | .collect(Collectors.joining());
|
361 | 364 | return ImageArchiveManifest.of(new ByteArrayInputStream(manifestContent.getBytes(StandardCharsets.UTF_8)));
|
362 | 365 | }
|
363 | 366 |
|
364 |
| - private Path copyToTemp(TarArchiveInputStream in) throws IOException { |
| 367 | + private Path copyToTemp(InputStream in) throws IOException { |
365 | 368 | Path path = Files.createTempFile("create-builder-scratch-", null);
|
366 | 369 | try (OutputStream out = Files.newOutputStream(path)) {
|
367 | 370 | StreamUtils.copy(in, out);
|
|
0 commit comments