Make better effort to delete leftover temporary backup files.

This commit is contained in:
Alan Evans 2020-01-27 09:28:17 -05:00 committed by Greyson Parrelli
parent 5d54ebfaa0
commit 3589fa381d
2 changed files with 59 additions and 45 deletions

View file

@ -77,18 +77,20 @@ 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);
int count = 0;
try {
outputStream.writeDatabaseVersion(input.getVersion());
List<String> tables = exportSchema(input, outputStream);
int count = 0;
Stopwatch stopwatch = new Stopwatch("Backup");
@ -123,9 +125,11 @@ public static void export(@NonNull Context context,
stopwatch.stop(TAG);
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)
throws IOException

View file

@ -86,6 +86,7 @@ public class LocalBackupJob extends BaseJob {
File tempFile = File.createTempFile("backup", "tmp", StorageUtil.getBackupCacheDirectory(context));
try {
FullBackupExporter.export(context,
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
DatabaseFactory.getBackupDatabase(context),
@ -93,9 +94,18 @@ public class LocalBackupJob extends BaseJob {
backupPassword);
if (!tempFile.renameTo(backupFile)) {
tempFile.delete();
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();
}