refactor(storage): clean up tmp on put failure; promote DirectoryNotEmptyException import
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import com.cameleer.server.core.storage.ArtifactStore;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.DirectoryNotEmptyException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
@@ -28,8 +29,12 @@ public class FilesystemArtifactStore implements ArtifactStore {
|
|||||||
Path tmp = target.resolveSibling(target.getFileName() + ".tmp");
|
Path tmp = target.resolveSibling(target.getFileName() + ".tmp");
|
||||||
try (InputStream in = bytes) {
|
try (InputStream in = bytes) {
|
||||||
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
|
||||||
}
|
|
||||||
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
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;
|
||||||
|
}
|
||||||
return target.toString();
|
return target.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,11 +59,11 @@ public class FilesystemArtifactStore implements ArtifactStore {
|
|||||||
Path versionDir = target.getParent();
|
Path versionDir = target.getParent();
|
||||||
if (versionDir != null && Files.isDirectory(versionDir) && isEmpty(versionDir)) {
|
if (versionDir != null && Files.isDirectory(versionDir) && isEmpty(versionDir)) {
|
||||||
try { Files.delete(versionDir); }
|
try { Files.delete(versionDir); }
|
||||||
catch (java.nio.file.DirectoryNotEmptyException e) { return; }
|
catch (DirectoryNotEmptyException e) { return; }
|
||||||
Path appDir = versionDir.getParent();
|
Path appDir = versionDir.getParent();
|
||||||
if (appDir != null && Files.isDirectory(appDir) && isEmpty(appDir)) {
|
if (appDir != null && Files.isDirectory(appDir) && isEmpty(appDir)) {
|
||||||
try { Files.delete(appDir); }
|
try { Files.delete(appDir); }
|
||||||
catch (java.nio.file.DirectoryNotEmptyException e) { /* leave it */ }
|
catch (DirectoryNotEmptyException e) { /* leave it */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user