Skip to content

Commit

Permalink
Make better effort to delete leftover temporary backup files.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed Jan 27, 2020
1 parent 5d54ebf commit 3589fa3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,54 +77,58 @@ public class FullBackupExporter extends FullBackupBase {
JobDatabase.DEPENDENCIES_TABLE_NAME
);

public static void export(@NonNull Context context,
@NonNull AttachmentSecret attachmentSecret,
public static void export(@NonNull Context context,
@NonNull AttachmentSecret attachmentSecret,
@NonNull SQLiteDatabase input,
@NonNull File output,
@NonNull String passphrase)
throws IOException
{
BackupFrameOutputStream outputStream = new BackupFrameOutputStream(output, passphrase);
outputStream.writeDatabaseVersion(input.getVersion());

List<String> tables = exportSchema(input, outputStream);
int count = 0;

Stopwatch stopwatch = new Stopwatch("Backup");

for (String table : tables) {
if (table.equals(MmsDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, FullBackupExporter::isNonExpiringMessage, null, count);
} else if (table.equals(GroupReceiptDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(GroupReceiptDatabase.MMS_ID))), null, count);
} else if (table.equals(AttachmentDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.MMS_ID))), cursor -> exportAttachment(attachmentSecret, cursor, outputStream), count);
} else if (table.equals(StickerDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> true, cursor -> exportSticker(attachmentSecret, cursor, outputStream), count);
} else if (!BLACKLISTED_TABLES.contains(table) && !table.startsWith("sqlite_")) {
count = exportTable(table, input, outputStream, null, null, count);
int count = 0;

try {
outputStream.writeDatabaseVersion(input.getVersion());

List<String> tables = exportSchema(input, outputStream);

Stopwatch stopwatch = new Stopwatch("Backup");

for (String table : tables) {
if (table.equals(MmsDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, FullBackupExporter::isNonExpiringMessage, null, count);
} else if (table.equals(GroupReceiptDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(GroupReceiptDatabase.MMS_ID))), null, count);
} else if (table.equals(AttachmentDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.MMS_ID))), cursor -> exportAttachment(attachmentSecret, cursor, outputStream), count);
} else if (table.equals(StickerDatabase.TABLE_NAME)) {
count = exportTable(table, input, outputStream, cursor -> true, cursor -> exportSticker(attachmentSecret, cursor, outputStream), count);
} else if (!BLACKLISTED_TABLES.contains(table) && !table.startsWith("sqlite_")) {
count = exportTable(table, input, outputStream, null, null, count);
}
stopwatch.split("table::" + table);
}
stopwatch.split("table::" + table);
}

for (BackupProtos.SharedPreference preference : IdentityKeyUtil.getBackupRecord(context)) {
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));
outputStream.write(preference);
}
for (BackupProtos.SharedPreference preference : IdentityKeyUtil.getBackupRecord(context)) {
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));
outputStream.write(preference);
}

stopwatch.split("prefs");
stopwatch.split("prefs");

for (File avatar : AvatarHelper.getAvatarFiles(context)) {
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));
outputStream.write(avatar.getName(), new FileInputStream(avatar), avatar.length());
}
for (File avatar : AvatarHelper.getAvatarFiles(context)) {
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));
outputStream.write(avatar.getName(), new FileInputStream(avatar), avatar.length());
}

stopwatch.split("avatars");
stopwatch.stop(TAG);
stopwatch.split("avatars");
stopwatch.stop(TAG);

outputStream.writeEnd();
outputStream.close();
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, ++count));
outputStream.writeEnd();
} finally {
outputStream.close();
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, ++count));
}
}

private static List<String> exportSchema(@NonNull SQLiteDatabase input, @NonNull BackupFrameOutputStream outputStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,25 @@ public void onRun() throws NoExternalStorageException, IOException {

File tempFile = File.createTempFile("backup", "tmp", StorageUtil.getBackupCacheDirectory(context));

FullBackupExporter.export(context,
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
DatabaseFactory.getBackupDatabase(context),
tempFile,
backupPassword);

if (!tempFile.renameTo(backupFile)) {
tempFile.delete();
throw new IOException("Renaming temporary backup file failed!");
try {
FullBackupExporter.export(context,
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
DatabaseFactory.getBackupDatabase(context),
tempFile,
backupPassword);

if (!tempFile.renameTo(backupFile)) {
Log.w(TAG, "Failed to rename temp file");
throw new IOException("Renaming temporary backup file failed!");
}
} finally {
if (tempFile.exists()) {
if (tempFile.delete()) {
Log.w(TAG, "Backup failed. Deleted temp file");
} else {
Log.w(TAG, "Backup failed. Failed to delete temp file " + tempFile);
}
}
}

BackupUtil.deleteOldBackups();
Expand Down

0 comments on commit 3589fa3

Please sign in to comment.