aboutsummaryrefslogtreecommitdiff
path: root/crypto/pgpainless
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2024-07-22 22:28:44 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2024-07-28 23:00:16 +0530
commit911d6ba563aece61f65a4b119fb5e6299ba14e5f (patch)
tree49f622dead4d33e107eacf9ae1b7b7d6a13676f4 /crypto/pgpainless
parent9de25f751c4d992c749cb875f7bdbab2a66bfbee (diff)
fix: part 2 of NIO bugfixinghs/nio-paths
Diffstat (limited to 'crypto/pgpainless')
-rw-r--r--crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt9
-rw-r--r--crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt8
2 files changed, 10 insertions, 7 deletions
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}")
}