From 911d6ba563aece61f65a4b119fb5e6299ba14e5f Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 22 Jul 2024 22:28:44 +0530 Subject: fix: part 2 of NIO bugfixing --- .../src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt | 9 +++++---- .../test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) (limited to 'crypto/pgpainless') diff --git a/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt b/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt index 8efa2a97..f405affc 100644 --- a/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt +++ b/crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt @@ -23,12 +23,12 @@ import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.unwrap import java.nio.file.Paths import javax.inject.Inject +import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.createDirectories import kotlin.io.path.deleteIfExists import kotlin.io.path.exists -import kotlin.io.path.isRegularFile -import kotlin.io.path.listDirectoryEntries import kotlin.io.path.readBytes +import kotlin.io.path.walk import kotlin.io.path.writeBytes import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.withContext @@ -37,6 +37,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing import org.pgpainless.PGPainless import org.pgpainless.util.selection.userid.SelectUserId +@OptIn(ExperimentalPathApi::class) public class PGPKeyManager @Inject constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) : @@ -98,7 +99,7 @@ constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) : withContext(dispatcher) { runSuspendCatching { if (!keyDirExists()) throw KeyDirectoryUnavailableException - val keyFiles = keyDir.listDirectoryEntries().filter { it.isRegularFile() } + val keyFiles = keyDir.walk().toSet() if (keyFiles.isEmpty()) throw NoKeysAvailableException val keys = keyFiles.map { file -> PGPKey(file.readBytes()) } @@ -134,7 +135,7 @@ constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) : withContext(dispatcher) { runSuspendCatching { if (!keyDirExists()) throw KeyDirectoryUnavailableException - val keyFiles = keyDir.listDirectoryEntries().filter { it.isRegularFile() } + val keyFiles = keyDir.walk().toSet() if (keyFiles.isEmpty()) return@runSuspendCatching emptyList() keyFiles.map { keyFile -> PGPKey(keyFile.readBytes()) }.toList() } diff --git a/crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt b/crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt index 8f4452c8..7f53c6f9 100644 --- a/crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt +++ b/crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt @@ -9,9 +9,10 @@ import app.passwordstore.crypto.errors.NoKeysAvailableException import app.passwordstore.crypto.errors.UnusableKeyException import com.github.michaelbull.result.unwrap import com.github.michaelbull.result.unwrapError +import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.absolutePathString -import kotlin.io.path.listDirectoryEntries import kotlin.io.path.name +import kotlin.io.path.walk import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals @@ -24,6 +25,7 @@ import kotlinx.coroutines.test.runTest import org.junit.Rule import org.junit.rules.TemporaryFolder +@OptIn(ExperimentalPathApi::class) class PGPKeyManagerTest { @get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder() @@ -44,9 +46,9 @@ class PGPKeyManagerTest { val keyId = keyManager.getKeyId(keyManager.addKey(secretKey).unwrap()) assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) // Check if the keys directory have one file - assertEquals(1, filesDir.listDirectoryEntries().size) + assertEquals(1, filesDir.walk().toSet().size) // Check if the file name is correct - val keyFile = keysDir.listDirectoryEntries().first() + val keyFile = keysDir.walk().toSet().first() assertEquals(keyFile.name, "$keyId.${PGPKeyManager.KEY_EXTENSION}") } -- cgit v1.2.3