Second pass to fix attachment deleted in history

This commit is contained in:
J-Jamet
2020-08-28 13:12:54 +02:00
parent b72d858480
commit a10ccc1eb0
5 changed files with 41 additions and 44 deletions

View File

@@ -34,7 +34,7 @@ class DeleteEntryHistoryDatabaseRunnable (
override fun onStartRun() { override fun onStartRun() {
try { try {
mainEntry.removeEntryFromHistory(entryHistoryPosition) database.removeEntryHistory(mainEntry, entryHistoryPosition)
} catch (e: Exception) { } catch (e: Exception) {
setError(e) setError(e)
} }

View File

@@ -42,8 +42,8 @@ class UpdateEntryRunnable constructor(
mNewEntry.addParentFrom(mOldEntry) mNewEntry.addParentFrom(mOldEntry)
// Build oldest attachments // Build oldest attachments
val oldEntryAttachments = mOldEntry.getAttachments(database.binaryPool) val oldEntryAttachments = mOldEntry.getAttachments(database.binaryPool, true)
val newEntryAttachments = mNewEntry.getAttachments(database.binaryPool) val newEntryAttachments = mNewEntry.getAttachments(database.binaryPool, true)
val attachmentsToRemove = ArrayList<Attachment>(oldEntryAttachments) val attachmentsToRemove = ArrayList<Attachment>(oldEntryAttachments)
// Not use equals because only check name // Not use equals because only check name
newEntryAttachments.forEach { newAttachment -> newEntryAttachments.forEach { newAttachment ->

View File

@@ -30,7 +30,6 @@ import com.kunzisoft.keepass.database.element.icon.IconImageFactory
import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.NodeIdInt import com.kunzisoft.keepass.database.element.node.NodeIdInt
import com.kunzisoft.keepass.database.element.node.NodeIdUUID import com.kunzisoft.keepass.database.element.node.NodeIdUUID
import com.kunzisoft.keepass.database.element.database.BinaryAttachment
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
import com.kunzisoft.keepass.database.exception.DatabaseOutputException import com.kunzisoft.keepass.database.exception.DatabaseOutputException
import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException
@@ -854,21 +853,6 @@ class Database {
) )
} }
fun removeEachEntryHistory() {
rootGroup?.doForEachChildAndForIt(
object : NodeHandler<Entry>() {
override fun operate(node: Entry): Boolean {
node.removeAllHistory()
return true
}
},
object : NodeHandler<Group>() {
override fun operate(node: Group): Boolean {
return true
}
})
}
/** /**
* Remove oldest history if more than max items or max memory * Remove oldest history if more than max items or max memory
*/ */
@@ -877,7 +861,7 @@ class Database {
val maxItems = historyMaxItems val maxItems = historyMaxItems
if (maxItems >= 0) { if (maxItems >= 0) {
while (entry.getHistory().size > maxItems) { while (entry.getHistory().size > maxItems) {
entry.removeOldestEntryFromHistory() removeOldestEntryHistory(entry)
} }
} }
@@ -888,9 +872,8 @@ class Database {
for (entryHistory in entry.getHistory()) { for (entryHistory in entry.getHistory()) {
historySize += entryHistory.getSize(binaryPool) historySize += entryHistory.getSize(binaryPool)
} }
if (historySize > maxSize) { if (historySize > maxSize) {
entry.removeOldestEntryFromHistory() removeOldestEntryHistory(entry)
} else { } else {
break break
} }
@@ -899,6 +882,22 @@ class Database {
} }
} }
private fun removeOldestEntryHistory(entry: Entry) {
entry.removeOldestEntryFromHistory()?.let {
it.getAttachments(binaryPool, false).forEach { attachmentToRemove ->
removeAttachmentIfNotUsed(attachmentToRemove)
}
}
}
fun removeEntryHistory(entry: Entry, entryHistoryPosition: Int) {
entry.removeEntryFromHistory(entryHistoryPosition)?.let {
it.getAttachments(binaryPool, false).forEach { attachmentToRemove ->
removeAttachmentIfNotUsed(attachmentToRemove)
}
}
}
companion object : SingletonHolder<Database>(::Database) { companion object : SingletonHolder<Database>(::Database) {
private val TAG = Database::class.java.name private val TAG = Database::class.java.name

View File

@@ -325,12 +325,12 @@ class Entry : Node, EntryVersionedInterface<Group> {
entryKDBX?.stopToManageFieldReferences() entryKDBX?.stopToManageFieldReferences()
} }
fun getAttachments(binaryPool: BinaryPool): Set<Attachment> { fun getAttachments(binaryPool: BinaryPool, inHistory: Boolean = false): Set<Attachment> {
val attachments = HashSet<Attachment>() val attachments = HashSet<Attachment>()
entryKDB?.getAttachments()?.let { entryKDB?.getAttachments()?.let {
attachments.addAll(it) attachments.addAll(it)
} }
entryKDBX?.getAttachments(binaryPool)?.let { entryKDBX?.getAttachments(binaryPool, inHistory)?.let {
attachments.addAll(it) attachments.addAll(it)
} }
return attachments return attachments
@@ -366,16 +366,18 @@ class Entry : Node, EntryVersionedInterface<Group> {
} }
} }
fun removeEntryFromHistory(position: Int) { fun removeEntryFromHistory(position: Int): Entry? {
entryKDBX?.removeEntryFromHistory(position) entryKDBX?.removeEntryFromHistory(position)?.let {
return Entry(it)
}
return null
} }
fun removeAllHistory() { fun removeOldestEntryFromHistory(): Entry? {
entryKDBX?.removeAllHistory() entryKDBX?.removeOldestEntryFromHistory()?.let {
} return Entry(it)
}
fun removeOldestEntryFromHistory() { return null
entryKDBX?.removeOldestEntryFromHistory()
} }
fun getSize(binaryPool: BinaryPool): Long { fun getSize(binaryPool: BinaryPool): Long {

View File

@@ -21,7 +21,9 @@ package com.kunzisoft.keepass.database.element.entry
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.database.BinaryPool
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX
import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.icon.IconImage
@@ -32,8 +34,6 @@ import com.kunzisoft.keepass.database.element.node.NodeIdUUID
import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface
import com.kunzisoft.keepass.database.element.node.Type import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.database.element.security.ProtectedString
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.database.BinaryPool
import com.kunzisoft.keepass.utils.ParcelableUtil import com.kunzisoft.keepass.utils.ParcelableUtil
import com.kunzisoft.keepass.utils.UnsignedLong import com.kunzisoft.keepass.utils.UnsignedLong
import java.util.* import java.util.*
@@ -337,15 +337,11 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
history.add(entry) history.add(entry)
} }
fun removeEntryFromHistory(position: Int) { fun removeEntryFromHistory(position: Int): EntryKDBX? {
history.removeAt(position) return history.removeAt(position)
} }
fun removeAllHistory() { fun removeOldestEntryFromHistory(): EntryKDBX? {
history.clear()
}
fun removeOldestEntryFromHistory() {
var min: Date? = null var min: Date? = null
var index = -1 var index = -1
@@ -358,9 +354,9 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
} }
} }
if (index != -1) { return if (index != -1) {
history.removeAt(index) history.removeAt(index)
} } else null
} }
override fun touch(modified: Boolean, touchParents: Boolean) { override fun touch(modified: Boolean, touchParents: Boolean) {