diff --git a/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/common/SystemFileRead.kt b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/common/SystemFileRead.kt new file mode 100644 index 0000000..78f0294 --- /dev/null +++ b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/common/SystemFileRead.kt @@ -0,0 +1,12 @@ +package com.github.nullptroma.wallenc.domain.vault.storages.common + +import com.github.nullptroma.wallenc.domain.errors.WallencException +import java.io.InputStream + +/** Читает системный файл; отсутствие файла — пустой массив байт (не исключение). */ +internal suspend fun readSystemFileBytesOrEmpty(open: suspend () -> InputStream): ByteArray = + try { + open().use { it.readBytes() } + } catch (_: WallencException.Storage.FileNotFound) { + ByteArray(0) + } diff --git a/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/local/LocalStorageAccessor.kt b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/local/LocalStorageAccessor.kt index 37cc14a..b10e1c0 100644 --- a/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/local/LocalStorageAccessor.kt +++ b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/local/LocalStorageAccessor.kt @@ -1,6 +1,7 @@ package com.github.nullptroma.wallenc.domain.vault.storages.local import com.github.nullptroma.wallenc.domain.errors.WallencException +import com.github.nullptroma.wallenc.domain.vault.storages.common.readSystemFileBytesOrEmpty import com.fasterxml.jackson.core.JacksonException import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper @@ -572,7 +573,7 @@ class LocalStorageAccessor( } override suspend fun readSyncJournal(): List = withContext(ioDispatcher) { - val bytes = openReadSystemFile(SYNC_JOURNAL_FILENAME).use { it.readBytes() } + val bytes = readSystemFileBytesOrEmpty { openReadSystemFile(SYNC_JOURNAL_FILENAME) } if (bytes.isEmpty()) { return@withContext emptyList() } @@ -602,7 +603,7 @@ class LocalStorageAccessor( } override suspend fun readSyncLock(): StorageSyncLock? = withContext(ioDispatcher) { - val bytes = openReadSystemFile(SYNC_LOCK_FILENAME).use { it.readBytes() } + val bytes = readSystemFileBytesOrEmpty { openReadSystemFile(SYNC_LOCK_FILENAME) } if (bytes.isEmpty()) { return@withContext null } diff --git a/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/yandex/YandexStorageAccessor.kt b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/yandex/YandexStorageAccessor.kt index 4e21b7f..5e6d710 100644 --- a/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/yandex/YandexStorageAccessor.kt +++ b/domain-vault/src/main/java/com/github/nullptroma/wallenc/domain/vault/storages/yandex/YandexStorageAccessor.kt @@ -2,6 +2,7 @@ package com.github.nullptroma.wallenc.domain.vault.storages.yandex import com.github.nullptroma.wallenc.domain.errors.WallencException import com.github.nullptroma.wallenc.domain.vault.errors.toVaultWallencException +import com.github.nullptroma.wallenc.domain.vault.storages.common.readSystemFileBytesOrEmpty import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.github.nullptroma.wallenc.domain.vault.network.yandexdisk.YandexDiskAuthException @@ -597,7 +598,7 @@ class YandexStorageAccessor( } override suspend fun readSyncJournal(): List = withContext(ioDispatcher) { - val bytes = openReadSystemFile(SYNC_JOURNAL_FILENAME).use { it.readBytes() } + val bytes = readSystemFileBytesOrEmpty { openReadSystemFile(SYNC_JOURNAL_FILENAME) } if (bytes.isEmpty()) { return@withContext emptyList() } @@ -631,7 +632,7 @@ class YandexStorageAccessor( } override suspend fun readSyncLock(): StorageSyncLock? = withContext(ioDispatcher) { - val bytes = openReadSystemFile(SYNC_LOCK_FILENAME).use { it.readBytes() } + val bytes = readSystemFileBytesOrEmpty { openReadSystemFile(SYNC_LOCK_FILENAME) } if (bytes.isEmpty()) { return@withContext null }