Start merge database v1

This commit is contained in:
J-Jamet
2022-01-17 22:35:27 +01:00
parent 1e43a65743
commit 6971cd1a6b
3 changed files with 27 additions and 12 deletions

View File

@@ -634,8 +634,7 @@ class Database {
} }
) )
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
Log.e(TAG, "Unable to load keyfile", e) throw FileNotFoundDatabaseException("Unable to load the keyfile")
throw FileNotFoundDatabaseException()
} catch (e: LoadDatabaseException) { } catch (e: LoadDatabaseException) {
throw e throw e
} catch (e: Exception) { } catch (e: Exception) {
@@ -650,6 +649,10 @@ class Database {
isRAMSufficient: (memoryWanted: Long) -> Boolean, isRAMSufficient: (memoryWanted: Long) -> Boolean,
progressTaskUpdater: ProgressTaskUpdater?) { progressTaskUpdater: ProgressTaskUpdater?) {
mDatabaseKDB?.let {
throw IODatabaseException("Unable to merge from a database V1")
}
// New database instance to get new changes // New database instance to get new changes
val databaseToMerge = Database() val databaseToMerge = Database()
databaseToMerge.fileUri = this.fileUri databaseToMerge.fileUri = this.fileUri
@@ -679,18 +682,24 @@ class Database {
} }
) )
// TODO Merge KDB
mDatabaseKDBX?.let { currentDatabaseKDBX -> mDatabaseKDBX?.let { currentDatabaseKDBX ->
databaseToMerge.mDatabaseKDBX?.let { databaseKDBXToMerge -> val databaseMerger = DatabaseKDBXMerger(currentDatabaseKDBX).apply {
DatabaseKDBXMerger(currentDatabaseKDBX).apply {
this.isRAMSufficient = isRAMSufficient this.isRAMSufficient = isRAMSufficient
}.merge(databaseKDBXToMerge) }
databaseToMerge.mDatabaseKDB?.let { databaseKDBToMerge ->
databaseMerger.merge(databaseKDBToMerge)
}
databaseToMerge.mDatabaseKDBX?.let { databaseKDBXToMerge ->
databaseMerger.merge(databaseKDBXToMerge)
} }
} }
} ?: run { } ?: run {
Log.e(TAG, "Database URI is null, database cannot be reloaded") throw IODatabaseException("Database URI is null, database cannot be reloaded")
throw IODatabaseException()
} }
} catch (e: FileNotFoundException) {
throw FileNotFoundDatabaseException("Unable to load the keyfile")
} catch (e: LoadDatabaseException) {
throw e
} catch (e: Exception) { } catch (e: Exception) {
throw LoadDatabaseException(e) throw LoadDatabaseException(e)
} finally { } finally {
@@ -733,12 +742,10 @@ class Database {
} }
) )
} ?: run { } ?: run {
Log.e(TAG, "Database URI is null, database cannot be reloaded") throw IODatabaseException("Database URI is null, database cannot be reloaded")
throw IODatabaseException()
} }
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
Log.e(TAG, "Unable to load keyfile", e) throw FileNotFoundDatabaseException("Unable to load the keyfile")
throw FileNotFoundDatabaseException()
} catch (e: LoadDatabaseException) { } catch (e: LoadDatabaseException) {
throw e throw e
} catch (e: Exception) { } catch (e: Exception) {

View File

@@ -46,6 +46,7 @@ open class LoadDatabaseException : DatabaseException {
@StringRes @StringRes
override var errorId: Int = R.string.error_load_database override var errorId: Int = R.string.error_load_database
constructor() : super() constructor() : super()
constructor(string: String) : super(string)
constructor(throwable: Throwable) : super(throwable) constructor(throwable: Throwable) : super(throwable)
} }
@@ -53,6 +54,7 @@ class FileNotFoundDatabaseException : LoadDatabaseException {
@StringRes @StringRes
override var errorId: Int = R.string.file_not_found_content override var errorId: Int = R.string.file_not_found_content
constructor() : super() constructor() : super()
constructor(string: String) : super(string)
constructor(exception: Throwable) : super(exception) constructor(exception: Throwable) : super(exception)
} }
@@ -76,6 +78,7 @@ class IODatabaseException : LoadDatabaseException {
@StringRes @StringRes
override var errorId: Int = R.string.error_load_database override var errorId: Int = R.string.error_load_database
constructor() : super() constructor() : super()
constructor(string: String) : super(string)
constructor(exception: Throwable) : super(exception) constructor(exception: Throwable) : super(exception)
} }

View File

@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.database.merge
import com.kunzisoft.keepass.database.action.node.NodeHandler import com.kunzisoft.keepass.database.action.node.NodeHandler
import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.database.DatabaseKDB
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX
@@ -31,6 +32,10 @@ class DatabaseKDBXMerger(private var database: DatabaseKDBX) {
var isRAMSufficient: (memoryWanted: Long) -> Boolean = {true} var isRAMSufficient: (memoryWanted: Long) -> Boolean = {true}
fun merge(databaseToMerge: DatabaseKDB) {
// TODO merge KDB
}
fun merge(databaseToMerge: DatabaseKDBX) { fun merge(databaseToMerge: DatabaseKDBX) {
if (database.nameChanged.date.before(databaseToMerge.nameChanged.date)) { if (database.nameChanged.date.before(databaseToMerge.nameChanged.date)) {