From 2b45b3caa2a52cdbb1d750c6a9855e9ed5451c98 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 6 Jun 2018 20:12:17 -0700 Subject: [PATCH] Fixed export and restore of FTS tables. First, FTS index contents do not need to be exported. They will be recreated naturally. Second, we can't export the secret FTS tables, or SQLite will think it's corrupted. --- .../securesms/backup/FullBackupExporter.java | 26 ++++++++++++------- .../securesms/backup/FullBackupImporter.java | 11 ++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java index d3aa10b07b..9108dfc8a3 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java @@ -24,6 +24,7 @@ import org.thoughtcrime.securesms.database.AttachmentDatabase; import org.thoughtcrime.securesms.database.MmsDatabase; import org.thoughtcrime.securesms.database.MmsSmsColumns; import org.thoughtcrime.securesms.database.OneTimePreKeyDatabase; +import org.thoughtcrime.securesms.database.SearchDatabase; import org.thoughtcrime.securesms.database.SessionDatabase; import org.thoughtcrime.securesms.database.SignedPreKeyDatabase; import org.thoughtcrime.securesms.database.SmsDatabase; @@ -76,9 +77,11 @@ public class FullBackupExporter extends FullBackupBase { count = exportTable(table, input, outputStream, cursor -> cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsColumns.EXPIRES_IN)) <= 0, null, count); } else if (table.equals(AttachmentDatabase.TABLE_NAME)) { count = exportTable(table, input, outputStream, null, cursor -> exportAttachment(attachmentSecret, cursor, outputStream), count); - } else if (!table.equals(SignedPreKeyDatabase.TABLE_NAME) && - !table.equals(OneTimePreKeyDatabase.TABLE_NAME) && - !table.equals(SessionDatabase.TABLE_NAME)) + } else if (!table.equals(SignedPreKeyDatabase.TABLE_NAME) && + !table.equals(OneTimePreKeyDatabase.TABLE_NAME) && + !table.equals(SessionDatabase.TABLE_NAME) && + !table.startsWith(SearchDatabase.SMS_FTS_TABLE_NAME) && + !table.startsWith(SearchDatabase.MMS_FTS_TABLE_NAME)) { count = exportTable(table, input, outputStream, null, null, count); } @@ -111,14 +114,17 @@ public class FullBackupExporter extends FullBackupBase { String type = cursor.getString(2); if (sql != null) { - if ("table".equals(type)) { - outputStream.write(BackupProtos.SqlStatement.newBuilder().setStatement("DROP TABLE IF EXISTS " + name).build()); - tables.add(name); - } else if ("index".equals(type)) { - outputStream.write(BackupProtos.SqlStatement.newBuilder().setStatement("DROP INDEX IF EXISTS " + name).build()); - } - outputStream.write(BackupProtos.SqlStatement.newBuilder().setStatement(cursor.getString(0)).build()); + boolean isSmsFtsSecretTable = name != null && !name.equals(SearchDatabase.SMS_FTS_TABLE_NAME) && name.startsWith(SearchDatabase.SMS_FTS_TABLE_NAME); + boolean isMmsFtsSecretTable = name != null && !name.equals(SearchDatabase.MMS_FTS_TABLE_NAME) && name.startsWith(SearchDatabase.MMS_FTS_TABLE_NAME); + + if (!isSmsFtsSecretTable && !isMmsFtsSecretTable) { + if ("table".equals(type)) { + tables.add(name); + } + + outputStream.write(BackupProtos.SqlStatement.newBuilder().setStatement(cursor.getString(0)).build()); + } } } } diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java index 2ac0224ea8..44f6e32bcc 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupImporter.java @@ -7,6 +7,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; import android.support.annotation.NonNull; +import android.util.Log; import android.util.Pair; import net.sqlcipher.database.SQLiteDatabase; @@ -21,6 +22,7 @@ import org.thoughtcrime.securesms.crypto.AttachmentSecret; import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.AttachmentDatabase; +import org.thoughtcrime.securesms.database.SearchDatabase; import org.thoughtcrime.securesms.profiles.AvatarHelper; import org.thoughtcrime.securesms.util.Conversions; import org.thoughtcrime.securesms.util.Util; @@ -37,6 +39,7 @@ import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -90,6 +93,14 @@ public class FullBackupImporter extends FullBackupBase { } private static void processStatement(@NonNull SQLiteDatabase db, SqlStatement statement) { + boolean isForSmsFtsSecretTable = statement.getStatement().contains(SearchDatabase.SMS_FTS_TABLE_NAME + "_"); + boolean isForMmsFtsSecretTable = statement.getStatement().contains(SearchDatabase.MMS_FTS_TABLE_NAME + "_"); + + if (isForSmsFtsSecretTable || isForMmsFtsSecretTable) { + Log.i(TAG, "Ignoring import for statement: " + statement.getStatement()); + return; + } + List parameters = new LinkedList<>(); for (SqlStatement.SqlParameter parameter : statement.getParametersList()) {