From 6e5a3224e5a8ffcd7ec609c604ad2757305cf4e5 Mon Sep 17 00:00:00 2001 From: davotoula Date: Sat, 25 Apr 2026 12:23:20 +0200 Subject: [PATCH] minor sonar fixes --- .../com/vitorpamplona/amethyst/cli/Config.kt | 7 +++++- .../amethyst/cli/SecureFileIO.kt | 25 ++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Config.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Config.kt index 34e1b3a5a1..f62c12f0b8 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Config.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/Config.kt @@ -30,8 +30,11 @@ import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair import com.vitorpamplona.quartz.nip19Bech32.bech32.bechToBytes import com.vitorpamplona.quartz.nip19Bech32.toNpub import com.vitorpamplona.quartz.nip19Bech32.toNsec +import com.vitorpamplona.quartz.utils.Log import java.io.File +private const val TAG = "Config" + /** * Persisted identity. * @@ -184,7 +187,9 @@ class DataDir( val file = Json.mapper.readValue(identityFile.readText()) file.secret?.let { secrets.delete(it) } } - identityFile.delete() + if (!identityFile.delete() && identityFile.exists()) { + Log.w(TAG) { "Failed to delete identity file ${identityFile.absolutePath}" } + } } } diff --git a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/SecureFileIO.kt b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/SecureFileIO.kt index b2d2be10e2..0da221d12c 100644 --- a/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/SecureFileIO.kt +++ b/cli/src/main/kotlin/com/vitorpamplona/amethyst/cli/SecureFileIO.kt @@ -20,6 +20,7 @@ */ package com.vitorpamplona.amethyst.cli +import com.vitorpamplona.quartz.utils.Log import java.io.File import java.io.OutputStream import java.nio.file.AtomicMoveNotSupportedException @@ -153,11 +154,23 @@ object SecureFileIO { return } val f = path.toFile() - f.setReadable(false, false) - f.setWritable(false, false) - f.setExecutable(false, false) - f.setReadable(true, true) - f.setWritable(true, true) - if (isDir) f.setExecutable(true, true) + warnIfFailed(f, "setReadable(false)") { f.setReadable(false, false) } + warnIfFailed(f, "setWritable(false)") { f.setWritable(false, false) } + warnIfFailed(f, "setExecutable(false)") { f.setExecutable(false, false) } + warnIfFailed(f, "setReadable(true, owner)") { f.setReadable(true, true) } + warnIfFailed(f, "setWritable(true, owner)") { f.setWritable(true, true) } + if (isDir) warnIfFailed(f, "setExecutable(true, owner)") { f.setExecutable(true, true) } } + + private inline fun warnIfFailed( + file: File, + op: String, + action: () -> Boolean, + ) { + if (!action()) { + Log.w(TAG) { "$op failed on ${file.absolutePath}" } + } + } + + private const val TAG = "SecureFileIO" }