Add useTempFile extension methods

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 4 years ago
parent 8370ad104e
commit c3715d7320
  1. 6
      asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt
  2. 21
      crypto/src/test/java/dev/openrs2/crypto/RsaTest.kt
  3. 29
      util/src/main/java/dev/openrs2/util/io/PathExtensions.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<ClassNode>) {
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)
}
}

@ -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)
}
}
}

@ -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 <T> 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 <T> 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 <T> Path.atomicWrite(f: (Path) -> T): T {
val tempFile = Files.createTempFile(parent, ".$fileName", ".tmp")

Loading…
Cancel
Save