Fixes CacheWriter#writeLayer could throw FileAlreadyExistsException.
@@ -30,6 +30,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.zip.GZIPInputStream;
@@ -79,9 +80,7 @@ public class CacheWriter {
// skip renaming and use the existing file. This happens if a new layer happens to have the
// same content as a previously-cached layer.
Path layerFile = getLayerFile(compressedBlobDescriptor.getDigest());
- if (!Files.exists(layerFile)) {
- Files.move(tempLayerFile, layerFile);
- }
+ Files.move(tempLayerFile, layerFile, StandardCopyOption.REPLACE_EXISTING);
CachedLayer cachedLayer = new CachedLayer(layerFile, compressedBlobDescriptor, diffId);
LayerMetadata layerMetadata =
