Include the original file instead of a patch if the patch is larger than the original file.

This saves processing and disk space, as there is no point in applying a patch when the original file is smaller and can be included in its entirety.
This commit is contained in:
CalDescent
2021-10-24 12:02:09 +01:00
parent 8dd4d71d75
commit 04aabe0921
4 changed files with 46 additions and 11 deletions

View File

@@ -91,7 +91,8 @@ public class UnifiedDiffPatch {
}
/**
* Validate the patch to ensure it works correctly
* Validate the patch at the "destination" path to ensure
* it works correctly and is smaller than the original file
*
* @return true if valid, false if invalid
* @throws IOException
@@ -110,8 +111,13 @@ public class UnifiedDiffPatch {
byte[] inputDigest = Crypto.digest(after.toFile());
byte[] outputDigest = Crypto.digest(tempPath.toFile());
if (Arrays.equals(inputDigest, outputDigest)) {
// Patch is valid
return true;
// Patch is valid, but we might want to reject if it's larger than the original file
long originalSize = Files.size(after);
long patchSize = Files.size(destination);
if (patchSize < originalSize) {
// Patch file is smaller than the original file size, so treat it as valid
return true;
}
}
else {
LOGGER.info("Checksum mismatch when verifying patch for file {}", destination.toString());