From 71bfa94d1019c6bdae9979d0ae25d7c9536fe665 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 4 Sep 2026 11:12:11 +0100 Subject: [PATCH] Polish "Delete layer temp file when layer creation fails" See gh-51519 Signed-off-by: Andy Wilkinson --- .../platform/build/ImageBuildpack.java | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/ImageBuildpack.java b/buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/ImageBuildpack.java index eb8d74e46fb..c596ade2acd 100644 --- a/buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/ImageBuildpack.java +++ b/buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/ImageBuildpack.java @@ -139,25 +139,7 @@ final class ImageBuildpack implements Buildpack { } Path layerFile = Files.createTempFile("create-builder-scratch-", null); try { - try (TarArchiveOutputStream out = new TarArchiveOutputStream(Files.newOutputStream(layerFile))) { - try (TarArchiveInputStream in = new TarArchiveInputStream( - Files.newInputStream(sourceTarFile))) { - out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); - TarArchiveEntry entry = in.getNextEntry(); - while (entry != null) { - String entryName = entry.getName(); - Path entryPath = Path.of(entryName); - Assert.state(entryPath.toAbsolutePath().equals(entryPath.toAbsolutePath().normalize()), - () -> "Malformed zip entry name '%s'".formatted(entryName)); - out.putArchiveEntry(entry); - StreamUtils.copy(in, out); - out.closeArchiveEntry(); - entry = in.getNextEntry(); - } - out.finish(); - } - } - return layerFile; + writeLayerFile(layerFile, sourceTarFile); } catch (IOException | RuntimeException ex) { try { @@ -168,12 +150,33 @@ final class ImageBuildpack implements Buildpack { } throw ex; } + return layerFile; } finally { Files.deleteIfExists(sourceTarFile); } } + private void writeLayerFile(Path layerFile, Path sourceTarFile) throws IOException { + try (TarArchiveOutputStream out = new TarArchiveOutputStream(Files.newOutputStream(layerFile))) { + try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(sourceTarFile))) { + out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); + TarArchiveEntry entry = in.getNextEntry(); + while (entry != null) { + String entryName = entry.getName(); + Path entryPath = Path.of(entryName); + Assert.state(entryPath.toAbsolutePath().equals(entryPath.toAbsolutePath().normalize()), + () -> "Malformed zip entry name '%s'".formatted(entryName)); + out.putArchiveEntry(entry); + StreamUtils.copy(in, out); + out.closeArchiveEntry(); + entry = in.getNextEntry(); + } + out.finish(); + } + } + } + void apply(IOConsumer layers) throws IOException { for (Path path : this.layerFiles) { layers.accept(Layer.fromTarArchive((out) -> {