Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 911d6ba

Browse files
committed
fix: part 2 of NIO bugfixing
1 parent 9de25f7 commit 911d6ba

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

app/src/main/java/app/passwordstore/data/password/PasswordItem.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import java.nio.file.Path
1313
import kotlin.io.path.absolutePathString
1414
import kotlin.io.path.name
1515
import kotlin.io.path.nameWithoutExtension
16-
import kotlin.io.path.pathString
17-
import kotlin.io.path.relativeTo
1816

1917
data class PasswordItem(
2018
val parent: PasswordItem? = null,
@@ -25,7 +23,8 @@ data class PasswordItem(
2523

2624
val name = file.nameWithoutExtension
2725

28-
val fullPathToParent = file.absolutePathString().replace(rootDir.absolutePathString(), "").replace(file.name, "")
26+
val fullPathToParent =
27+
file.absolutePathString().replace(rootDir.absolutePathString(), "").replace(file.name, "")
2928

3029
val longName =
3130
BasePGPActivity.getLongName(fullPathToParent, rootDir.absolutePathString(), toString())

app/src/main/java/app/passwordstore/ui/adapters/PasswordItemRecyclerAdapter.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.MotionEvent
1010
import android.view.View
1111
import androidx.appcompat.widget.AppCompatImageView
1212
import androidx.appcompat.widget.AppCompatTextView
13+
import androidx.core.view.isVisible
1314
import androidx.recyclerview.selection.ItemDetailsLookup
1415
import androidx.recyclerview.selection.Selection
1516
import androidx.recyclerview.widget.RecyclerView
@@ -18,9 +19,11 @@ import app.passwordstore.data.password.PasswordItem
1819
import app.passwordstore.util.coroutines.DispatcherProvider
1920
import app.passwordstore.util.viewmodel.SearchableRepositoryAdapter
2021
import app.passwordstore.util.viewmodel.stableId
22+
import kotlin.io.path.ExperimentalPathApi
23+
import kotlin.io.path.PathWalkOption
2124
import kotlin.io.path.extension
2225
import kotlin.io.path.isDirectory
23-
import kotlin.io.path.listDirectoryEntries
26+
import kotlin.io.path.walk
2427
import kotlinx.coroutines.CoroutineScope
2528
import kotlinx.coroutines.withContext
2629

@@ -52,6 +55,7 @@ open class PasswordItemRecyclerAdapter(
5255
return super.onSelectionChanged(listener) as PasswordItemRecyclerAdapter
5356
}
5457

58+
@OptIn(ExperimentalPathApi::class)
5559
class PasswordItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
5660

5761
private val name: AppCompatTextView = itemView.findViewById(R.id.label)
@@ -75,11 +79,12 @@ open class PasswordItemRecyclerAdapter(
7579
val count =
7680
withContext(dispatcherProvider.io()) {
7781
item.file
78-
.listDirectoryEntries()
82+
.walk(PathWalkOption.INCLUDE_DIRECTORIES)
7983
.filter { it.isDirectory() || it.extension == "gpg" }
84+
.toSet()
8085
.size
8186
}
82-
childCount.visibility = if (count > 0) View.VISIBLE else View.GONE
87+
childCount.isVisible = count > 0
8388
childCount.text = "$count"
8489
} else {
8590
childCount.visibility = View.GONE

app/src/main/java/app/passwordstore/ui/crypto/BasePGPActivity.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ open class BasePGPActivity : AppCompatActivity() {
161161
*/
162162
fun getPGPIdentifiers(subDir: String): List<PGPIdentifier>? {
163163
val repoRoot = PasswordRepository.getRepositoryDirectory()
164-
// This should ideally be `repoRoot.resolve(subDir)` but for some reason doing that returns `/subDir` as the path
164+
// This should ideally be `repoRoot.resolve(subDir)` but for some reason doing that returns
165+
// `/subDir` as the path
165166
// which doesn't work inside `findTillRoot`, so we're doing this manual dance.
166167
val gpgIdentifierFile =
167168
Paths.get(repoRoot.absolutePathString(), subDir).findTillRoot(".gpg-id", repoRoot)

app/src/main/java/app/passwordstore/ui/passwords/PasswordStore.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,20 @@ import java.nio.file.Path
5858
import java.nio.file.Paths
5959
import javax.inject.Inject
6060
import kotlin.io.path.ExperimentalPathApi
61+
import kotlin.io.path.PathWalkOption
6162
import kotlin.io.path.absolute
6263
import kotlin.io.path.absolutePathString
6364
import kotlin.io.path.createDirectories
6465
import kotlin.io.path.deleteRecursively
6566
import kotlin.io.path.exists
6667
import kotlin.io.path.isDirectory
6768
import kotlin.io.path.isRegularFile
68-
import kotlin.io.path.listDirectoryEntries
6969
import kotlin.io.path.moveTo
7070
import kotlin.io.path.name
7171
import kotlin.io.path.nameWithoutExtension
7272
import kotlin.io.path.pathString
7373
import kotlin.io.path.relativeTo
74+
import kotlin.io.path.walk
7475
import kotlinx.coroutines.launch
7576
import kotlinx.coroutines.withContext
7677
import logcat.LogPriority.ERROR
@@ -79,6 +80,7 @@ import logcat.logcat
7980

8081
const val PASSWORD_FRAGMENT_TAG = "PasswordsList"
8182

83+
@OptIn(ExperimentalPathApi::class)
8284
@AndroidEntryPoint
8385
class PasswordStore : BaseGitActivity() {
8486

@@ -446,7 +448,8 @@ class PasswordStore : BaseGitActivity() {
446448
fun deletePasswords(selectedItems: List<PasswordItem>) {
447449
var size = 0
448450
selectedItems.forEach {
449-
if (it.file.isRegularFile()) size++ else size += it.file.listDirectoryEntries().size
451+
if (it.file.isRegularFile()) size++
452+
else size += it.file.walk(PathWalkOption.INCLUDE_DIRECTORIES).toSet().size
450453
}
451454
if (size == 0) {
452455
selectedItems.map { item -> item.file.deleteRecursively() }
@@ -458,7 +461,7 @@ class PasswordStore : BaseGitActivity() {
458461
.setPositiveButton(resources.getString(R.string.dialog_yes)) { _, _ ->
459462
val filesToDelete = arrayListOf<Path>()
460463
selectedItems.forEach { item ->
461-
if (item.file.isDirectory()) filesToDelete.addAll(item.file.listDirectoryEntries())
464+
if (item.file.isDirectory()) filesToDelete.addAll(item.file.walk())
462465
else filesToDelete.add(item.file)
463466
}
464467
selectedItems.map { item -> item.file.deleteRecursively() }
@@ -599,9 +602,7 @@ class PasswordStore : BaseGitActivity() {
599602
// Recursively list all files (not directories) below `source`, then
600603
// obtain the corresponding target file by resolving the relative path
601604
// starting at the destination folder.
602-
source.listDirectoryEntries().associateWith {
603-
destinationFile.resolve(it.relativeTo(source))
604-
}
605+
source.walk().associateWith { destinationFile.resolve(it.relativeTo(source)) }
605606
} else {
606607
mapOf(source to destinationFile)
607608
}

crypto/pgpainless/src/main/kotlin/app/passwordstore/crypto/PGPKeyManager.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import com.github.michaelbull.result.runCatching
2323
import com.github.michaelbull.result.unwrap
2424
import java.nio.file.Paths
2525
import javax.inject.Inject
26+
import kotlin.io.path.ExperimentalPathApi
2627
import kotlin.io.path.createDirectories
2728
import kotlin.io.path.deleteIfExists
2829
import kotlin.io.path.exists
29-
import kotlin.io.path.isRegularFile
30-
import kotlin.io.path.listDirectoryEntries
3130
import kotlin.io.path.readBytes
31+
import kotlin.io.path.walk
3232
import kotlin.io.path.writeBytes
3333
import kotlinx.coroutines.CoroutineDispatcher
3434
import kotlinx.coroutines.withContext
@@ -37,6 +37,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing
3737
import org.pgpainless.PGPainless
3838
import org.pgpainless.util.selection.userid.SelectUserId
3939

40+
@OptIn(ExperimentalPathApi::class)
4041
public class PGPKeyManager
4142
@Inject
4243
constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) :
@@ -98,7 +99,7 @@ constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) :
9899
withContext(dispatcher) {
99100
runSuspendCatching {
100101
if (!keyDirExists()) throw KeyDirectoryUnavailableException
101-
val keyFiles = keyDir.listDirectoryEntries().filter { it.isRegularFile() }
102+
val keyFiles = keyDir.walk().toSet()
102103
if (keyFiles.isEmpty()) throw NoKeysAvailableException
103104
val keys = keyFiles.map { file -> PGPKey(file.readBytes()) }
104105

@@ -134,7 +135,7 @@ constructor(filesDir: String, private val dispatcher: CoroutineDispatcher) :
134135
withContext(dispatcher) {
135136
runSuspendCatching {
136137
if (!keyDirExists()) throw KeyDirectoryUnavailableException
137-
val keyFiles = keyDir.listDirectoryEntries().filter { it.isRegularFile() }
138+
val keyFiles = keyDir.walk().toSet()
138139
if (keyFiles.isEmpty()) return@runSuspendCatching emptyList()
139140
keyFiles.map { keyFile -> PGPKey(keyFile.readBytes()) }.toList()
140141
}

crypto/pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import app.passwordstore.crypto.errors.NoKeysAvailableException
99
import app.passwordstore.crypto.errors.UnusableKeyException
1010
import com.github.michaelbull.result.unwrap
1111
import com.github.michaelbull.result.unwrapError
12+
import kotlin.io.path.ExperimentalPathApi
1213
import kotlin.io.path.absolutePathString
13-
import kotlin.io.path.listDirectoryEntries
1414
import kotlin.io.path.name
15+
import kotlin.io.path.walk
1516
import kotlin.test.Test
1617
import kotlin.test.assertContentEquals
1718
import kotlin.test.assertEquals
@@ -24,6 +25,7 @@ import kotlinx.coroutines.test.runTest
2425
import org.junit.Rule
2526
import org.junit.rules.TemporaryFolder
2627

28+
@OptIn(ExperimentalPathApi::class)
2729
class PGPKeyManagerTest {
2830

2931
@get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder()
@@ -44,9 +46,9 @@ class PGPKeyManagerTest {
4446
val keyId = keyManager.getKeyId(keyManager.addKey(secretKey).unwrap())
4547
assertEquals(KeyId(CryptoConstants.KEY_ID), keyId)
4648
// Check if the keys directory have one file
47-
assertEquals(1, filesDir.listDirectoryEntries().size)
49+
assertEquals(1, filesDir.walk().toSet().size)
4850
// Check if the file name is correct
49-
val keyFile = keysDir.listDirectoryEntries().first()
51+
val keyFile = keysDir.walk().toSet().first()
5052
assertEquals(keyFile.name, "$keyId.${PGPKeyManager.KEY_EXTENSION}")
5153
}
5254

0 commit comments

Comments
 (0)