Dispose для EncryptedStorage, скрытые системные файлы для LocalStorage
This commit is contained in:
@@ -3,6 +3,7 @@ package com.github.nullptroma.wallenc.data.vaults.local
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
@@ -20,7 +21,8 @@ class LocalStorage(
|
||||
get() = accessor.numberOfFiles
|
||||
override val isAvailable: StateFlow<Boolean>
|
||||
get() = accessor.isAvailable
|
||||
override val accessor = LocalStorageAccessor(absolutePath, ioDispatcher)
|
||||
private val _accessor = LocalStorageAccessor(absolutePath, ioDispatcher)
|
||||
override val accessor: IStorageAccessor = _accessor
|
||||
|
||||
private val _encInfo = MutableStateFlow<StorageEncryptionInfo?>(null)
|
||||
override val encInfo: StateFlow<StorageEncryptionInfo?>
|
||||
@@ -31,14 +33,12 @@ class LocalStorage(
|
||||
private val encInfoFileName: String = "$uuid$ENC_INFO_FILE_POSTFIX"
|
||||
|
||||
suspend fun init() {
|
||||
accessor.init()
|
||||
_accessor.init()
|
||||
readEncInfo()
|
||||
}
|
||||
|
||||
private suspend fun readEncInfo() {
|
||||
accessor.touchFile(encInfoFileName)
|
||||
accessor.setHidden(encInfoFileName, true)
|
||||
val reader = accessor.openRead(encInfoFileName)
|
||||
val reader = _accessor.openReadSystemFile(encInfoFileName)
|
||||
var enc: StorageEncryptionInfo? = null
|
||||
try {
|
||||
enc = _jackson.readValue(reader, StorageEncryptionInfo::class.java)
|
||||
@@ -51,23 +51,13 @@ class LocalStorage(
|
||||
isEncrypted = false,
|
||||
encryptedTestData = null
|
||||
)
|
||||
val writer = accessor.openWrite(encInfoFileName)
|
||||
try {
|
||||
_jackson.writeValue(writer, enc)
|
||||
}
|
||||
catch (e: Exception) {
|
||||
TODO("Это никогда не должно произойти")
|
||||
}
|
||||
writer.close()
|
||||
setEncInfo(enc)
|
||||
}
|
||||
_encInfo.value = enc
|
||||
}
|
||||
|
||||
suspend fun setEncInfo(enc: StorageEncryptionInfo) {
|
||||
accessor.touchFile(encInfoFileName)
|
||||
accessor.setHidden(encInfoFileName, true)
|
||||
|
||||
val writer = accessor.openWrite(encInfoFileName)
|
||||
val writer = _accessor.openWriteSystemFile(encInfoFileName)
|
||||
try {
|
||||
_jackson.writeValue(writer, enc)
|
||||
}
|
||||
|
||||
@@ -85,9 +85,11 @@ class LocalStorageAccessor(
|
||||
|
||||
val children = dir.listFiles()
|
||||
if (children != null) {
|
||||
|
||||
// вызвать коллбек для каждого элемента директории
|
||||
for (child in children) {
|
||||
callback(child)
|
||||
if(child.name != SYSTEM_HIDDEN_DIRNAME)
|
||||
callback(child)
|
||||
}
|
||||
|
||||
if (useCallbackForSelf)
|
||||
@@ -96,7 +98,7 @@ class LocalStorageAccessor(
|
||||
if (maxDepth != 0) {
|
||||
val nextMaxDepth = if (maxDepth > 0) maxDepth - 1 else maxDepth
|
||||
for (child in children) {
|
||||
if (child.isDirectory) {
|
||||
if (child.isDirectory && child.name != SYSTEM_HIDDEN_DIRNAME) {
|
||||
scanFileSystem(child, nextMaxDepth, callback, false)
|
||||
}
|
||||
}
|
||||
@@ -488,7 +490,33 @@ class LocalStorageAccessor(
|
||||
writeMeta(pair.metaFile, newMeta)
|
||||
}
|
||||
|
||||
suspend fun openReadSystemFile(name: String): InputStream = withContext(ioDispatcher) {
|
||||
val dirPath = _filesystemBasePath.resolve(SYSTEM_HIDDEN_DIRNAME)
|
||||
val path = dirPath.resolve(name)
|
||||
val file = path.toFile()
|
||||
if(!file.exists()) {
|
||||
Files.createDirectories(dirPath)
|
||||
file.createNewFile()
|
||||
}
|
||||
|
||||
return@withContext file.inputStream()
|
||||
}
|
||||
|
||||
suspend fun openWriteSystemFile(name: String): OutputStream = withContext(ioDispatcher) {
|
||||
val dirPath = _filesystemBasePath.resolve(SYSTEM_HIDDEN_DIRNAME)
|
||||
val path = dirPath.resolve(name)
|
||||
val file = path.toFile()
|
||||
if(!file.exists()) {
|
||||
Files.createDirectories(dirPath)
|
||||
file.createNewFile()
|
||||
}
|
||||
|
||||
return@withContext file.outputStream()
|
||||
}
|
||||
|
||||
companion object {
|
||||
// Файлы, которые можно использовать для чтения и записи, но не отображаются в хранилище
|
||||
private const val SYSTEM_HIDDEN_DIRNAME = "wallenc-local-storage-meta-dir"
|
||||
private const val META_INFO_POSTFIX = ".wallenc-meta"
|
||||
private const val DATA_PAGE_LENGTH = 10
|
||||
private val _jackson = jacksonObjectMapper().apply { findAndRegisterModules() }
|
||||
|
||||
Reference in New Issue
Block a user