Use short-circuiting content comparison instead of hash comparison when possible (#222)

This commit is contained in:
Jason Penilla 2023-11-16 23:44:02 -07:00 committed by GitHub
parent c7d0820fa1
commit 5225a4d132
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 2 deletions

View file

@ -45,6 +45,17 @@ dependencies {
compileOnly(kotlin("stdlib-jdk8"))
}
testing {
suites {
val test by getting(JvmTestSuite::class) {
useKotlinTest(embeddedKotlinVersion)
dependencies {
implementation("org.junit.jupiter:junit-jupiter-engine:5.10.1")
}
}
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"

View file

@ -30,6 +30,7 @@ import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.PathMatcher
import java.nio.file.attribute.DosFileAttributeView
import java.util.Arrays
import java.util.stream.Collectors
import java.util.stream.Stream
import java.util.stream.StreamSupport
@ -160,6 +161,38 @@ fun Path.hashFile(algorithm: HashingAlgorithm): ByteArray = inputStream().use {
fun Path.sha256asHex(): String = hashFile(HashingAlgorithm.SHA256).asHexString()
fun Path.contentEquals(file: Path, bufferSizeBytes: Int = 8192): Boolean {
inputStream().use { one ->
file.inputStream().use { two ->
val bufOne = ByteArray(bufferSizeBytes)
val bufTwo = ByteArray(bufferSizeBytes)
while (true) {
val readOne = one.read(bufOne)
val readTwo = two.read(bufTwo)
if (readOne != readTwo) {
// length differs
return false
}
if (readOne == -1) {
// end of content
break
}
if (!Arrays.equals(bufOne, 0, readOne, bufTwo, 0, readOne)) {
// content differs
return false
}
}
}
}
return true
}
fun Path.withDifferentExtension(ext: String): Path = resolveSibling("$nameWithoutExtension.$ext")
// Returns true if our process already owns the lock

View file

@ -88,12 +88,12 @@ private fun upToDate(
binaryIn: Path,
xmlIn: String
): Boolean {
val bin = binDest.isRegularFile() && binDest.sha256asHex() == binaryIn.sha256asHex()
val bin = binDest.isRegularFile() && binDest.contentEquals(binaryIn)
val xml = ivyXml.isRegularFile() && ivyXml.readText(Charsets.UTF_8) == xmlIn
val sources = if (sourcesIn == null) {
sourcesDest.notExists()
} else {
sourcesDest.isRegularFile() && sourcesDest.sha256asHex() == sourcesIn.sha256asHex()
sourcesDest.isRegularFile() && sourcesDest.contentEquals(sourcesIn)
}
return bin && xml && sources
}

View file

@ -0,0 +1,62 @@
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight.util
import java.nio.file.Path
import kotlin.io.path.*
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.jupiter.api.io.TempDir
class FileUtilsTest {
@Test
fun testContentEquals(@TempDir tempDir: Path) {
val someBytes = classBytes<FileUtilsTest>()
// write same data to two files
val one = tempDir.resolve("${FileUtilsTest::class.simpleName}.class")
one.writeBytes(someBytes)
val two = tempDir.resolve("${FileUtilsTest::class.simpleName}.class_1")
two.writeBytes(someBytes)
// assert the files have matching content
assertTrue(one.contentEquals(two), "These files have the same content")
assertTrue(two.contentEquals(one), "These files have the same content")
val someDifferentBytes = classBytes<Map<*, *>>()
// write some different data to a third file
val three = tempDir.resolve("Map.class")
three.writeBytes(someDifferentBytes)
// assert it's content is different from the previously written files
assertFalse(one.contentEquals(three), "These files are different")
}
private inline fun <reified C> classBytes(): ByteArray {
val resourceName = C::class.java.name.replace(".", "/") + ".class"
return this::class.java.classLoader.getResource(resourceName)
?.openStream()?.readAllBytes() ?: error("Couldn't get resource $resourceName")
}
}