From e7159c9d36f01e31dcff2af520ce59ea37102226 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 26 Aug 2020 10:39:56 +0200 Subject: [PATCH] Try to fix decompression --- .../com/kunzisoft/keepass/activities/EntryEditActivity.kt | 4 +++- .../keepass/database/action/SaveDatabaseRunnable.kt | 4 +++- .../action/UpdateCompressionBinariesDatabaseRunnable.kt | 5 +++++ .../java/com/kunzisoft/keepass/database/element/Database.kt | 6 ++++-- .../keepass/database/element/database/DatabaseKDBX.kt | 6 +++--- .../keepass/database/file/input/DatabaseInputKDBX.kt | 2 +- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index 5bea9edc7..4312a42a2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -42,6 +42,7 @@ import com.kunzisoft.keepass.activities.dialogs.FileTooBigDialogFragment.Compani import com.kunzisoft.keepass.activities.helpers.SelectFileHelper import com.kunzisoft.keepass.activities.lock.LockingActivity import com.kunzisoft.keepass.database.element.* +import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.icon.IconImageStandard import com.kunzisoft.keepass.database.element.node.NodeId @@ -442,7 +443,8 @@ class EntryEditActivity : LockingActivity(), } private fun buildNewAttachment(attachmentToUploadUri: Uri, fileName: String) { - mDatabase?.buildNewBinary(applicationContext.filesDir)?.let { binaryAttachment -> + val compression: Boolean = mDatabase?.compressionAlgorithm == CompressionAlgorithm.GZip + mDatabase?.buildNewBinary(applicationContext.filesDir, false, compression)?.let { binaryAttachment -> val entryAttachment = Attachment(fileName, binaryAttachment) // Ask to replace the current attachment if ((mDatabase?.allowMultipleAttachments != true && entryEditContentsView?.containsAttachment() == true) || diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/SaveDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/SaveDatabaseRunnable.kt index 6951a345e..d9e778d0e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/SaveDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/SaveDatabaseRunnable.kt @@ -30,11 +30,13 @@ open class SaveDatabaseRunnable(protected var context: Context, : ActionRunnable() { var mAfterSaveDatabase: ((Result) -> Unit)? = null + protected var mAllowRemoveUnlinkedAttachments = true override fun onStartRun() {} override fun onActionRun() { - database.removeUnlinkedAttachment() + if (mAllowRemoveUnlinkedAttachments) + database.removeUnlinkedAttachment() if (saveDatabase && result.isSuccess) { try { database.saveData(context.contentResolver) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/action/UpdateCompressionBinariesDatabaseRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/database/action/UpdateCompressionBinariesDatabaseRunnable.kt index 5b0e279c4..c88c288ac 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/action/UpdateCompressionBinariesDatabaseRunnable.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/action/UpdateCompressionBinariesDatabaseRunnable.kt @@ -31,6 +31,11 @@ class UpdateCompressionBinariesDatabaseRunnable ( saveDatabase: Boolean) : SaveDatabaseRunnable(context, database, saveDatabase) { + init { + // To prevent attachment corruption during compression + mAllowRemoveUnlinkedAttachments = false + } + override fun onStartRun() { // Set new compression if (database.allowDataCompression) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index 33ed695b3..2d64efa80 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -439,9 +439,11 @@ class Database { return false } - fun buildNewBinary(cacheDirectory: File): BinaryAttachment? { + fun buildNewBinary(cacheDirectory: File, + enableProtection: Boolean = false, + compressed: Boolean? = null): BinaryAttachment? { return mDatabaseKDB?.buildNewBinary(cacheDirectory) - ?: mDatabaseKDBX?.buildNewBinary(cacheDirectory) + ?: mDatabaseKDBX?.buildNewBinary(cacheDirectory, enableProtection, compressed) } fun removeAttachmentIfNotUsed(attachment: Attachment) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index 3b38cf31c..4fe067ace 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -203,7 +203,7 @@ class DatabaseKDBX : DatabaseVersioned { } } } catch (e: Exception) { - Log.e(TAG, "Unable to change compression for $key") + Log.e(TAG, "Unable to change compression for $key", e) } } } @@ -541,8 +541,8 @@ class DatabaseKDBX : DatabaseVersioned { } fun buildNewBinary(cacheDirectory: File, - protection: Boolean = false, - compression: Boolean? = null, + protection: Boolean, + compression: Boolean?, cacheId: String? = null): BinaryAttachment { // Unused cache key if needed val binaryId = cacheId ?: binaryPool.findUnusedKey().toString() diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt index cded4dc29..9231e4c95 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt @@ -253,7 +253,7 @@ class DatabaseInputKDBX(cacheDirectory: File, val byteLength = size - 1 // Read in a file val protectedFlag = dataInputStream.readBytes(1)[0].toInt() != 0 - val protectedBinary = mDatabase.buildNewBinary(cacheDirectory, protectedFlag) + val protectedBinary = mDatabase.buildNewBinary(cacheDirectory, protectedFlag, false) protectedBinary.getOutputDataStream().use { outputStream -> dataInputStream.readBytes(byteLength, DatabaseKDBX.BUFFER_SIZE_BYTES) { buffer -> outputStream.write(buffer)