mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-09 17:58:34 +00:00
Make better effort to delete leftover temporary backup files.
This commit is contained in:
parent
5d54ebfaa0
commit
3589fa381d
@ -77,54 +77,58 @@ public class FullBackupExporter extends FullBackupBase {
|
|||||||
JobDatabase.DEPENDENCIES_TABLE_NAME
|
JobDatabase.DEPENDENCIES_TABLE_NAME
|
||||||
);
|
);
|
||||||
|
|
||||||
public static void export(@NonNull Context context,
|
public static void export(@NonNull Context context,
|
||||||
@NonNull AttachmentSecret attachmentSecret,
|
@NonNull AttachmentSecret attachmentSecret,
|
||||||
@NonNull SQLiteDatabase input,
|
@NonNull SQLiteDatabase input,
|
||||||
@NonNull File output,
|
@NonNull File output,
|
||||||
@NonNull String passphrase)
|
@NonNull String passphrase)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
BackupFrameOutputStream outputStream = new BackupFrameOutputStream(output, passphrase);
|
BackupFrameOutputStream outputStream = new BackupFrameOutputStream(output, passphrase);
|
||||||
outputStream.writeDatabaseVersion(input.getVersion());
|
int count = 0;
|
||||||
|
|
||||||
List<String> tables = exportSchema(input, outputStream);
|
try {
|
||||||
int count = 0;
|
outputStream.writeDatabaseVersion(input.getVersion());
|
||||||
|
|
||||||
Stopwatch stopwatch = new Stopwatch("Backup");
|
List<String> tables = exportSchema(input, outputStream);
|
||||||
|
|
||||||
for (String table : tables) {
|
Stopwatch stopwatch = new Stopwatch("Backup");
|
||||||
if (table.equals(MmsDatabase.TABLE_NAME)) {
|
|
||||||
count = exportTable(table, input, outputStream, FullBackupExporter::isNonExpiringMessage, null, count);
|
for (String table : tables) {
|
||||||
} else if (table.equals(GroupReceiptDatabase.TABLE_NAME)) {
|
if (table.equals(MmsDatabase.TABLE_NAME)) {
|
||||||
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(GroupReceiptDatabase.MMS_ID))), null, count);
|
count = exportTable(table, input, outputStream, FullBackupExporter::isNonExpiringMessage, null, count);
|
||||||
} else if (table.equals(AttachmentDatabase.TABLE_NAME)) {
|
} else if (table.equals(GroupReceiptDatabase.TABLE_NAME)) {
|
||||||
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.MMS_ID))), cursor -> exportAttachment(attachmentSecret, cursor, outputStream), count);
|
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(GroupReceiptDatabase.MMS_ID))), null, count);
|
||||||
} else if (table.equals(StickerDatabase.TABLE_NAME)) {
|
} else if (table.equals(AttachmentDatabase.TABLE_NAME)) {
|
||||||
count = exportTable(table, input, outputStream, cursor -> true, cursor -> exportSticker(attachmentSecret, cursor, outputStream), count);
|
count = exportTable(table, input, outputStream, cursor -> isForNonExpiringMessage(input, cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.MMS_ID))), cursor -> exportAttachment(attachmentSecret, cursor, outputStream), count);
|
||||||
} else if (!BLACKLISTED_TABLES.contains(table) && !table.startsWith("sqlite_")) {
|
} else if (table.equals(StickerDatabase.TABLE_NAME)) {
|
||||||
count = exportTable(table, input, outputStream, null, null, count);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
stopwatch.split("avatars");
|
||||||
|
stopwatch.stop(TAG);
|
||||||
|
|
||||||
|
outputStream.writeEnd();
|
||||||
|
} finally {
|
||||||
|
outputStream.close();
|
||||||
|
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, ++count));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (BackupProtos.SharedPreference preference : IdentityKeyUtil.getBackupRecord(context)) {
|
|
||||||
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));
|
|
||||||
outputStream.write(preference);
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
stopwatch.split("avatars");
|
|
||||||
stopwatch.stop(TAG);
|
|
||||||
|
|
||||||
outputStream.writeEnd();
|
|
||||||
outputStream.close();
|
|
||||||
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, ++count));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> exportSchema(@NonNull SQLiteDatabase input, @NonNull BackupFrameOutputStream outputStream)
|
private static List<String> exportSchema(@NonNull SQLiteDatabase input, @NonNull BackupFrameOutputStream outputStream)
|
||||||
|
@ -86,15 +86,25 @@ public class LocalBackupJob extends BaseJob {
|
|||||||
|
|
||||||
File tempFile = File.createTempFile("backup", "tmp", StorageUtil.getBackupCacheDirectory(context));
|
File tempFile = File.createTempFile("backup", "tmp", StorageUtil.getBackupCacheDirectory(context));
|
||||||
|
|
||||||
FullBackupExporter.export(context,
|
try {
|
||||||
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
|
FullBackupExporter.export(context,
|
||||||
DatabaseFactory.getBackupDatabase(context),
|
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
|
||||||
tempFile,
|
DatabaseFactory.getBackupDatabase(context),
|
||||||
backupPassword);
|
tempFile,
|
||||||
|
backupPassword);
|
||||||
|
|
||||||
if (!tempFile.renameTo(backupFile)) {
|
if (!tempFile.renameTo(backupFile)) {
|
||||||
tempFile.delete();
|
Log.w(TAG, "Failed to rename temp file");
|
||||||
throw new IOException("Renaming temporary backup file failed!");
|
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();
|
BackupUtil.deleteOldBackups();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user