Dispose для EncryptedStorage
This commit is contained in:
@@ -22,7 +22,13 @@ class UnlockManager(dao: StorageKeyDao, ioDispatcher: CoroutineDispatcher): IUnl
|
|||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close(storage: IStorage) {
|
override fun close(uuid: UUID) {
|
||||||
TODO("Not yet implemented")
|
val enc = _openedStorages.value[uuid]
|
||||||
|
if(enc == null)
|
||||||
|
return
|
||||||
|
_openedStorages.value = _openedStorages.value.toMutableMap().apply {
|
||||||
|
remove(uuid)
|
||||||
|
}
|
||||||
|
enc.dispose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,8 +4,8 @@ import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
|||||||
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.ILogger
|
import com.github.nullptroma.wallenc.domain.interfaces.ILogger
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
||||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.DisposableHandle
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ class EncryptedStorage(
|
|||||||
key: EncryptKey,
|
key: EncryptKey,
|
||||||
logger: ILogger,
|
logger: ILogger,
|
||||||
ioDispatcher: CoroutineDispatcher,
|
ioDispatcher: CoroutineDispatcher,
|
||||||
) : IStorage {
|
) : IStorage, DisposableHandle {
|
||||||
override val size: StateFlow<Long?>
|
override val size: StateFlow<Long?>
|
||||||
get() = TODO("Not yet implemented")
|
get() = TODO("Not yet implemented")
|
||||||
override val numberOfFiles: StateFlow<Int?>
|
override val numberOfFiles: StateFlow<Int?>
|
||||||
@@ -27,10 +27,14 @@ class EncryptedStorage(
|
|||||||
get() = TODO("Not yet implemented")
|
get() = TODO("Not yet implemented")
|
||||||
override val encInfo: StateFlow<StorageEncryptionInfo>
|
override val encInfo: StateFlow<StorageEncryptionInfo>
|
||||||
get() = TODO("Not yet implemented")
|
get() = TODO("Not yet implemented")
|
||||||
override val accessor: IStorageAccessor =
|
override val accessor: EncryptedStorageAccessor =
|
||||||
EncryptedStorageAccessor(source.accessor, key, logger, ioDispatcher)
|
EncryptedStorageAccessor(source.accessor, key, logger, ioDispatcher)
|
||||||
|
|
||||||
override suspend fun rename(newName: String) {
|
override suspend fun rename(newName: String) {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun dispose() {
|
||||||
|
accessor.dispose()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,8 @@ import com.github.nullptroma.wallenc.domain.interfaces.IMetaInfo
|
|||||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.DisposableHandle
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
@@ -36,7 +38,10 @@ class EncryptedStorageAccessor(
|
|||||||
key: EncryptKey,
|
key: EncryptKey,
|
||||||
private val logger: ILogger,
|
private val logger: ILogger,
|
||||||
ioDispatcher: CoroutineDispatcher
|
ioDispatcher: CoroutineDispatcher
|
||||||
) : IStorageAccessor {
|
) : IStorageAccessor, DisposableHandle {
|
||||||
|
private val _job = Job()
|
||||||
|
private val _scope = CoroutineScope(ioDispatcher + _job)
|
||||||
|
|
||||||
override val size: StateFlow<Long?> = source.size
|
override val size: StateFlow<Long?> = source.size
|
||||||
override val numberOfFiles: StateFlow<Int?> = source.numberOfFiles
|
override val numberOfFiles: StateFlow<Int?> = source.numberOfFiles
|
||||||
override val isAvailable: StateFlow<Boolean> = source.isAvailable
|
override val isAvailable: StateFlow<Boolean> = source.isAvailable
|
||||||
@@ -50,11 +55,11 @@ class EncryptedStorageAccessor(
|
|||||||
private val _secretKey = SecretKeySpec(key.to32Bytes(), "AES")
|
private val _secretKey = SecretKeySpec(key.to32Bytes(), "AES")
|
||||||
|
|
||||||
init {
|
init {
|
||||||
collectSourceState(CoroutineScope(ioDispatcher))
|
collectSourceState()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun collectSourceState(coroutineScope: CoroutineScope) {
|
private fun collectSourceState() {
|
||||||
coroutineScope.launch {
|
_scope.launch {
|
||||||
launch {
|
launch {
|
||||||
source.filesUpdates.collect {
|
source.filesUpdates.collect {
|
||||||
val files = it.data.map(::decryptEntity)
|
val files = it.data.map(::decryptEntity)
|
||||||
@@ -246,6 +251,11 @@ class EncryptedStorageAccessor(
|
|||||||
source.moveToTrash(encryptPath(path))
|
source.moveToTrash(encryptPath(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun dispose() {
|
||||||
|
_job.cancel()
|
||||||
|
// TODO сделать удаление ключа, чтобы нельзя было вызвать ни один из методов
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val IV_LEN = 16
|
private const val IV_LEN = 16
|
||||||
|
|||||||
@@ -11,5 +11,5 @@ interface IUnlockManager {
|
|||||||
val openedStorages: StateFlow<Map<UUID, IStorage>>
|
val openedStorages: StateFlow<Map<UUID, IStorage>>
|
||||||
|
|
||||||
fun open(storage: IStorage, key: EncryptKey)
|
fun open(storage: IStorage, key: EncryptKey)
|
||||||
fun close(storage: IStorage)
|
fun close(storage: UUID)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user