Skip to content

Commit

Permalink
fix: Compare file content from stream (#10640)
Browse files Browse the repository at this point in the history
Compare jar file content and existing target file content before writing to disk.
  • Loading branch information
caalador authored and vaadin-bot committed Apr 16, 2021
1 parent c772991 commit 995027d
Showing 1 changed file with 16 additions and 13 deletions.
Expand Up @@ -17,6 +17,7 @@
package com.vaadin.flow.server.frontend;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -306,24 +307,26 @@ private void copyJarEntryTrimmingBasePath(JarFile jarFile,
+ basePath.length());
File target = new File(outputDirectory, relativePath);
try {
if (target.exists()) {
File tempFile = File.createTempFile(fullPath, null);
FileUtils.copyInputStreamToFile(
jarFile.getInputStream(jarEntry), tempFile);
if (!FileUtils.contentEquals(tempFile, target)) {
FileUtils.forceDelete(target);
FileUtils.moveFile(tempFile, target);
} else {
tempFile.delete();
}
} else {
if (!target.exists()
|| !hasSameContent(jarFile.getInputStream(jarEntry),
target)) {
FileUtils.copyInputStreamToFile(
jarFile.getInputStream(jarEntry), target);
}
} catch (IOException e) {
throw new UncheckedIOException(String.format(
"Failed to extract jar entry '%s' from jarFile '%s'",
jarEntry, outputDirectory), e);
"Failed to extract jar entry '%s' from jarFile", jarEntry),
e);
}
}

private boolean hasSameContent(InputStream jarContent, File existingContent)
throws IOException {
try (InputStream existingContentStream = new FileInputStream(
existingContent)) {
return IOUtils.contentEquals(jarContent, existingContentStream);
} finally {
IOUtils.closeQuietly(jarContent);
}
}

Expand Down

0 comments on commit 995027d

Please sign in to comment.