From 9c777c35606aa87ea9a6807dfed552564893d9ad Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 10 Oct 2024 15:35:13 -0700 Subject: [PATCH] cleanup temp remote artifact --- .../openrewrite/remote/LocalRemoteArtifactCache.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/remote/LocalRemoteArtifactCache.java b/rewrite-core/src/main/java/org/openrewrite/remote/LocalRemoteArtifactCache.java index 26a4d3ebddcc..d0387d3c57a1 100644 --- a/rewrite-core/src/main/java/org/openrewrite/remote/LocalRemoteArtifactCache.java +++ b/rewrite-core/src/main/java/org/openrewrite/remote/LocalRemoteArtifactCache.java @@ -46,8 +46,9 @@ public LocalRemoteArtifactCache(Path cacheDir) { @Override public @Nullable Path put(URI uri, InputStream artifactInputStream, Consumer onError) { + Path artifact = null; try { - Path artifact = cacheDir.resolve(UUID.randomUUID() + ".tmp"); + artifact = cacheDir.resolve(UUID.randomUUID() + ".tmp"); try (InputStream is = artifactInputStream) { Files.copy(is, artifact, StandardCopyOption.REPLACE_EXISTING); } @@ -56,14 +57,19 @@ public LocalRemoteArtifactCache(Path cacheDir) { if (!Files.exists(cachedArtifact)) { Files.move(artifact, cachedArtifact, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); - } else { - Files.delete(artifact); } } return cachedArtifact; } catch (Exception e) { onError.accept(e); return null; + } finally { + if (artifact != null) { + try { + Files.deleteIfExists(artifact); + } catch (Exception ignored) { + } + } } }