From 7820b6ef5ac3f2b42511cb3d190229b99484b2ad Mon Sep 17 00:00:00 2001 From: Mohamad Jaara Date: Wed, 22 Jan 2025 17:27:11 +0100 Subject: [PATCH] detekt --- .../logic/feature/backup/BackupScope.kt | 5 ++--- ...Case.kt => CreateObfuscatedCopyUseCase.kt} | 21 +++++++++++++++---- .../kalium/persistence/db/UserDatabase.kt | 2 -- ...yExporter.kt => ObfuscatedCopyExporter.kt} | 8 ++++--- .../kalium/persistence/db/UserDatabase.kt | 1 - 5 files changed, 24 insertions(+), 13 deletions(-) rename logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/{CreateUnEncryptedCopyUseCase.kt => CreateObfuscatedCopyUseCase.kt} (94%) rename persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/{ObfiscatedCopyExporter.kt => ObfuscatedCopyExporter.kt} (97%) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/BackupScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/BackupScope.kt index 23984f237d..8e6616cc98 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/BackupScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/BackupScope.kt @@ -73,10 +73,9 @@ class BackupScope internal constructor( restoreWeb ) - @DelicateKaliumApi("this is NOT a backup feature, but a feature to create an unencrypted and obfuscated copy of the database") - val createUnEncryptedCopy: CreateUnEncryptedCopyUseCase - get() = CreateUnEncryptedCopyUseCase( + val createUnEncryptedCopy: CreateObfuscatedCopyUseCase + get() = CreateObfuscatedCopyUseCase( userId, clientIdProvider, userRepository, diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateUnEncryptedCopyUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateObfuscatedCopyUseCase.kt similarity index 94% rename from logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateUnEncryptedCopyUseCase.kt rename to logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateObfuscatedCopyUseCase.kt index e9f0942826..5e21370942 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateUnEncryptedCopyUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/CreateObfuscatedCopyUseCase.kt @@ -51,8 +51,13 @@ import okio.Source import okio.buffer import okio.use +/** + * Creates an obfuscated copy of the user's data. + * TO BE USED FOR DEBUGGING PURPOSES ONLY. + */ @DelicateKaliumApi("This class is used for debugging purposes only") -class CreateUnEncryptedCopyUseCase internal constructor( +@Suppress("LongParameterList") +class CreateObfuscatedCopyUseCase internal constructor( private val userId: UserId, private val clientIdProvider: CurrentClientIdProvider, private val userRepository: UserRepository, @@ -75,13 +80,21 @@ class CreateUnEncryptedCopyUseCase internal constructor( ?: return@withContext CreateBackupResult.Failure(StorageFailure.DataNotFound) try { - createBackupFile(userId, plainDBPath, backupFilePath).fold( + createBackupFile( + userId, + plainDBPath, + backupFilePath + ).fold( { error -> CreateBackupResult.Failure(error) }, { (backupFilePath, backupSize) -> if (password != null) { encryptAndCompressFile(backupFilePath, password) - } else CreateBackupResult.Success(backupFilePath, backupSize, backupFilePath.name) - }) + } else { + CreateBackupResult.Success(backupFilePath, backupSize, backupFilePath.name) + } + } + + ) } finally { obfuscatedCopyExporter.deleteCopyFile() } diff --git a/persistence/src/androidMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt b/persistence/src/androidMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt index cc157b0ef1..0daca25873 100644 --- a/persistence/src/androidMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt +++ b/persistence/src/androidMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt @@ -28,7 +28,6 @@ import com.wire.kalium.persistence.UserDatabase import com.wire.kalium.persistence.dao.UserIDEntity import com.wire.kalium.persistence.db.support.SupportOpenHelperFactory import com.wire.kalium.persistence.util.FileNameUtil -import com.wire.kalium.util.FileUtil import kotlinx.coroutines.CoroutineDispatcher import net.zetetic.database.sqlcipher.SQLiteDatabase import java.io.File @@ -137,4 +136,3 @@ internal actual fun createEmptyDatabaseFile( it.absolutePath } } - diff --git a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfiscatedCopyExporter.kt b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfuscatedCopyExporter.kt similarity index 97% rename from persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfiscatedCopyExporter.kt rename to persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfuscatedCopyExporter.kt index e0c49b844d..522f259cdb 100644 --- a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfiscatedCopyExporter.kt +++ b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/backup/ObfuscatedCopyExporter.kt @@ -83,7 +83,7 @@ class ObfuscatedCopyExporter internal constructor( ) } - @Suppress("TooGenericExceptionCaught", "ReturnCount") + @Suppress("TooGenericExceptionCaught", "LongMethod") private suspend fun obfuscatePlainCopy( plainDBPath: String, ): Boolean = withContext(kaliumDispatcher.io) { @@ -96,13 +96,15 @@ class ObfuscatedCopyExporter internal constructor( try { plainDBDriver.execute( - null, "UPDATE MessageTextContent " + + null, + "UPDATE MessageTextContent " + "SET text_body = CASE " + " WHEN text_body IS NOT NULL " + " THEN substr(hex(randomblob(length(text_body))), 1, length(text_body)) " + " ELSE NULL " + "END " + - "WHERE text_body IS NOT NULL;", 0 + "WHERE text_body IS NOT NULL;", + 0 ) plainDBDriver.execute( diff --git a/persistence/src/jvmMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt b/persistence/src/jvmMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt index 71ca541820..57808db700 100644 --- a/persistence/src/jvmMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt +++ b/persistence/src/jvmMain/kotlin/com/wire/kalium/persistence/db/UserDatabase.kt @@ -91,7 +91,6 @@ internal actual fun createEmptyDatabaseFile( userId: UserIDEntity, ): String? = TODO() - /** * Creates an in-memory user database, * or returns an existing one if it already exists.