commit 7723cf2b55bd8348577a1d228961985dcb02e9ff
parent 33e1a6ae220a5b359d26f6648b90a2c20545fdfb
Author: Antoine A <>
Date: Wed, 24 Jan 2024 22:01:32 +0100
Improve fs logic
Diffstat:
3 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/bank/src/test/kotlin/helpers.kt b/bank/src/test/kotlin/helpers.kt
@@ -24,6 +24,7 @@ import io.ktor.http.*
import io.ktor.server.testing.*
import java.io.ByteArrayOutputStream
import java.util.zip.DeflaterOutputStream
+import java.nio.file.*
import kotlin.test.*
import kotlin.io.path.*
import kotlin.random.Random
@@ -301,14 +302,14 @@ suspend fun ApplicationTestBuilder.convert(amount: String): TalerAmount {
}
suspend fun tanCode(info: String): String? {
- // TODO rewrite with only two files access
- val file = Path("/tmp/tan-$info.txt")
- if (file.exists()) {
+ try {
+ val file = Path("/tmp/tan-$info.txt")
val code = file.readText()
file.deleteExisting()
return code;
- } else {
- return null
+ } catch (e: Exception) {
+ if (e is NoSuchFileException) return null
+ throw e
}
}
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsSetup.kt
@@ -203,17 +203,15 @@ fun extractEbicsConfig(configFile: String?): EbicsSetupConfig {
*/
private fun makePdf(privs: ClientPrivateKeysFile, cfg: EbicsSetupConfig) {
val pdf = generateKeysPdf(privs, cfg)
- // TODO rewrite with a single file access
- val pdfFile = Path("/tmp/libeufin-nexus-keys-${Instant.now().epochSecond}.pdf")
- if (pdfFile.exists()) {
- throw Exception("PDF file exists already at: ${pdfFile}, not overriding it")
- }
+ val path = Path("/tmp/libeufin-nexus-keys-${Instant.now().epochSecond}.pdf")
try {
- pdfFile.writeBytes(pdf)
+ path.writeBytes(pdf, StandardOpenOption.CREATE_NEW)
} catch (e: Exception) {
- throw Exception("Could not write PDF to ${pdfFile}, detail: ${e.message}")
+ if (e is FileAlreadyExistsException) throw Exception("PDF file exists already at '$path', not overriding it")
+ throw Exception("Could not write PDF to '$path'", e)
}
- println("PDF file with keys hex encoding created at: $pdfFile")
+
+ println("PDF file with keys hex encoding created at: $path")
}
/**
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/KeyFiles.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/KeyFiles.kt
@@ -129,8 +129,8 @@ private inline fun <reified T> persistJsonFile(obj: T, path: Path, name: String)
tmp.moveTo(path, StandardCopyOption.REPLACE_EXISTING);
} catch (e: Exception) {
when {
- !parent.toFile().canWrite() -> throw Exception("Could not write $name at '$path': permission denied on '$parent'")
- !path.toFile().canWrite() -> throw Exception("Could not write $name at '$path': permission denied")
+ !parent.isWritable() -> throw Exception("Could not write $name at '$path': permission denied on '$parent'")
+ !path.isWritable() -> throw Exception("Could not write $name at '$path': permission denied")
else -> throw Exception("Could not write $name at '$path'", e)
}
}