From c3715d7320feae07276aeab6b38f88161f4d1d2f Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 20 Jun 2020 14:10:43 +0100 Subject: [PATCH] Add useTempFile extension methods Signed-off-by: Graham --- .../openrs2/asm/io/SignedJarLibraryWriter.kt | 6 ++-- .../test/java/dev/openrs2/crypto/RsaTest.kt | 21 ++++---------- .../dev/openrs2/util/io/PathExtensions.kt | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt index b188f5cb..5f946cf7 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt @@ -4,6 +4,7 @@ import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.crypto.Pkcs12KeyStore import dev.openrs2.util.io.DeterministicJarOutputStream import dev.openrs2.util.io.entries +import dev.openrs2.util.io.useTempFile import org.objectweb.asm.tree.ClassNode import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -18,8 +19,7 @@ class SignedJarLibraryWriter( private val keyStore: Pkcs12KeyStore ) : LibraryWriter { override fun write(output: OutputStream, classPath: ClassPath, classes: Iterable) { - val unsignedJar = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) - try { + useTempFile(TEMP_PREFIX, JAR_SUFFIX) { unsignedJar -> Files.newOutputStream(unsignedJar).use { unsignedOutput -> ManifestJarLibraryWriter(manifest).write(unsignedOutput, classPath, classes) } @@ -31,8 +31,6 @@ class SignedJarLibraryWriter( repack(signedInput, output) } } - } finally { - Files.deleteIfExists(unsignedJar) } } diff --git a/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt b/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt index d013c6bd..08019fbc 100644 --- a/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt +++ b/crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt @@ -2,6 +2,7 @@ package dev.openrs2.crypto import com.google.common.jimfs.Configuration import com.google.common.jimfs.Jimfs +import dev.openrs2.util.io.useTempFile import io.netty.buffer.Unpooled import org.bouncycastle.crypto.params.RSAKeyParameters import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters @@ -116,16 +117,13 @@ object RsaTest { @Test fun testReadPublicKey() { Jimfs.newFileSystem(Configuration.unix()).use { fs -> - val file = Files.createTempFile(fs.getPath("/"), "public", ".key") - try { + fs.getPath("/").useTempFile("public", ".key") { file -> Files.write(file, PUBLIC_KEY_PEM) val key = allowUnsafeMod { Rsa.readPublicKey(file) } assertFalse(key.isPrivate) assertEquals(PUBLIC_KEY.modulus, key.modulus) assertEquals(PUBLIC_KEY.exponent, key.exponent) - } finally { - Files.deleteIfExists(file) } } } @@ -133,13 +131,10 @@ object RsaTest { @Test fun testWritePublicKey() { Jimfs.newFileSystem(Configuration.unix()).use { fs -> - val file = Files.createTempFile(fs.getPath("/"), "public", ".key") - try { + fs.getPath("/").useTempFile("public", ".key") { file -> Rsa.writePublicKey(file, PUBLIC_KEY) assertEquals(PUBLIC_KEY_PEM, Files.readAllLines(file)) - } finally { - Files.deleteIfExists(file) } } } @@ -147,8 +142,7 @@ object RsaTest { @Test fun testReadPrivateKey() { Jimfs.newFileSystem(Configuration.unix()).use { fs -> - val file = Files.createTempFile(fs.getPath("/"), "private", ".key") - try { + fs.getPath("/").useTempFile("private", ".key") { file -> Files.write(file, PRIVATE_KEY_PEM) val key = allowUnsafeMod { Rsa.readPrivateKey(file) } @@ -161,8 +155,6 @@ object RsaTest { assertEquals(PRIVATE_KEY_CRT.dp, key.dp) assertEquals(PRIVATE_KEY_CRT.dq, key.dq) assertEquals(PRIVATE_KEY_CRT.qInv, key.qInv) - } finally { - Files.deleteIfExists(file) } } } @@ -170,13 +162,10 @@ object RsaTest { @Test fun testWritePrivateKey() { Jimfs.newFileSystem(Configuration.unix()).use { fs -> - val file = Files.createTempFile(fs.getPath("/"), "private", ".key") - try { + fs.getPath("/").useTempFile("private", ".key") { file -> Rsa.writePrivateKey(file, PRIVATE_KEY_CRT) assertEquals(PRIVATE_KEY_PEM, Files.readAllLines(file)) - } finally { - Files.deleteIfExists(file) } } } diff --git a/util/src/main/java/dev/openrs2/util/io/PathExtensions.kt b/util/src/main/java/dev/openrs2/util/io/PathExtensions.kt index dfa72a90..580e54fb 100644 --- a/util/src/main/java/dev/openrs2/util/io/PathExtensions.kt +++ b/util/src/main/java/dev/openrs2/util/io/PathExtensions.kt @@ -7,6 +7,35 @@ import java.nio.file.Files import java.nio.file.OpenOption import java.nio.file.Path import java.nio.file.StandardCopyOption +import java.nio.file.attribute.FileAttribute + +inline fun useTempFile( + prefix: String? = null, + suffix: String? = null, + vararg attributes: FileAttribute<*>, + f: (Path) -> T +): T { + val tempFile = Files.createTempFile(prefix, suffix, *attributes) + try { + return f(tempFile) + } finally { + Files.deleteIfExists(tempFile) + } +} + +inline fun Path.useTempFile( + prefix: String? = null, + suffix: String? = null, + vararg attributes: FileAttribute<*>, + f: (Path) -> T +): T { + val tempFile = Files.createTempFile(this, prefix, suffix, *attributes) + try { + return f(tempFile) + } finally { + Files.deleteIfExists(tempFile) + } +} inline fun Path.atomicWrite(f: (Path) -> T): T { val tempFile = Files.createTempFile(parent, ".$fileName", ".tmp")