diff --git a/cameleer-server-app/src/main/java/com/cameleer/server/app/storage/FilesystemArtifactStore.java b/cameleer-server-app/src/main/java/com/cameleer/server/app/storage/FilesystemArtifactStore.java index ee20fcca..b3a0ce1a 100644 --- a/cameleer-server-app/src/main/java/com/cameleer/server/app/storage/FilesystemArtifactStore.java +++ b/cameleer-server-app/src/main/java/com/cameleer/server/app/storage/FilesystemArtifactStore.java @@ -5,6 +5,7 @@ import com.cameleer.server.core.storage.ArtifactStore; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -28,8 +29,12 @@ public class FilesystemArtifactStore implements ArtifactStore { Path tmp = target.resolveSibling(target.getFileName() + ".tmp"); try (InputStream in = bytes) { Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING); + Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); + } catch (IOException | RuntimeException e) { + try { Files.deleteIfExists(tmp); } + catch (IOException suppress) { e.addSuppressed(suppress); } + throw e; } - Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); return target.toString(); } @@ -54,11 +59,11 @@ public class FilesystemArtifactStore implements ArtifactStore { Path versionDir = target.getParent(); if (versionDir != null && Files.isDirectory(versionDir) && isEmpty(versionDir)) { try { Files.delete(versionDir); } - catch (java.nio.file.DirectoryNotEmptyException e) { return; } + catch (DirectoryNotEmptyException e) { return; } Path appDir = versionDir.getParent(); if (appDir != null && Files.isDirectory(appDir) && isEmpty(appDir)) { try { Files.delete(appDir); } - catch (java.nio.file.DirectoryNotEmptyException e) { /* leave it */ } + catch (DirectoryNotEmptyException e) { /* leave it */ } } } }