Skip to content

Commit

Permalink
Fix improper deletion of stickers when restored from backup.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Sep 8, 2020
1 parent 3b925f8 commit b8c7e86
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static void processAttachment(@NonNull Context context, @NonNull Attachm
private static void processSticker(@NonNull Context context, @NonNull AttachmentSecret attachmentSecret, @NonNull SQLiteDatabase db, @NonNull Sticker sticker, BackupRecordInputStream inputStream)
throws IOException
{
File stickerDirectory = context.getDir(AttachmentDatabase.DIRECTORY, Context.MODE_PRIVATE);
File stickerDirectory = context.getDir(StickerDatabase.DIRECTORY, Context.MODE_PRIVATE);
File dataFile = File.createTempFile("sticker", ".mms", stickerDirectory);

Pair<byte[], OutputStream> output = ModernEncryptingPartOutputStream.createFor(attachmentSecret, dataFile, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ public void deleteAbandonedAttachmentFiles() {
}
}

filesInDb.addAll(DatabaseFactory.getStickerDatabase(context).getAllStickerFiles());

Set<String> onDiskButNotInDatabase = SetUtil.difference(filesOnDisk, filesInDb);

for (String filePath : onDiskButNotInDatabase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.sqlcipher.database.SQLiteDatabase;

import org.greenrobot.eventbus.EventBus;
Expand All @@ -23,16 +23,17 @@
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
import org.thoughtcrime.securesms.stickers.BlessedPacks;
import org.thoughtcrime.securesms.stickers.StickerPackInstallEvent;
import org.thoughtcrime.securesms.util.CursorUtil;
import org.thoughtcrime.securesms.util.Util;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class StickerDatabase extends Database {

Expand Down Expand Up @@ -77,7 +78,7 @@ public class StickerDatabase extends Database {
"CREATE INDEX IF NOT EXISTS sticker_sticker_id_index ON " + TABLE_NAME + " (" + STICKER_ID + ");"
};

private static final String DIRECTORY = "stickers";
public static final String DIRECTORY = "stickers";

private final AttachmentSecret attachmentSecret;

Expand Down Expand Up @@ -190,6 +191,19 @@ public void insertSticker(@NonNull IncomingSticker sticker, @NonNull InputStream
return cursor;
}

public @NonNull Set<String> getAllStickerFiles() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();

Set<String> files = new HashSet<>();
try (Cursor cursor = db.query(TABLE_NAME, new String[] { FILE_PATH }, null, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
files.add(CursorUtil.requireString(cursor, FILE_PATH));
}
}

return files;
}

public @Nullable InputStream getStickerStream(long rowId) throws IOException {
String selection = _ID + " = ?";
String[] args = new String[] { String.valueOf(rowId) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ public void trimAllThreads(int length, long trimBeforeDate) {
}

public void trimThread(long threadId, int length, long trimBeforeDate) {
if (length == NO_TRIM_MESSAGE_COUNT_SET && trimBeforeDate == NO_TRIM_BEFORE_DATE_SET) {
return;
}

SQLiteDatabase db = databaseHelper.getWritableDatabase();
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
GroupReceiptDatabase groupReceiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);
Expand Down

0 comments on commit b8c7e86

Please sign in to comment.