Skip to content

Commit

Permalink
Create the temporary backup file hidden in the final location.
Browse files Browse the repository at this point in the history
Fixes #10003
  • Loading branch information
alan-signal authored and greyson-signal committed Sep 23, 2020
1 parent e89285a commit aff57fb
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import java.util.Date;
import java.util.Locale;

public class LocalBackupJob extends BaseJob {
public final class LocalBackupJob extends BaseJob {

public static final String KEY = "LocalBackupJob";

private static final String TAG = LocalBackupJob.class.getSimpleName();
private static final String TAG = Log.tag(LocalBackupJob.class);

public static final String TEMP_BACKUP_FILE_PREFIX = ".backup";
public static final String TEMP_BACKUP_FILE_SUFFIX = ".tmp";

public LocalBackupJob() {
this(new Job.Parameters.Builder()
Expand Down Expand Up @@ -76,6 +79,8 @@ public void onRun() throws NoExternalStorageException, IOException {
String fileName = String.format("signal-%s.backup", timestamp);
File backupFile = new File(backupDirectory, fileName);

deleteOldTemporaryBackups(backupDirectory);

if (backupFile.exists()) {
throw new IOException("Backup file already exists?");
}
Expand All @@ -84,7 +89,7 @@ public void onRun() throws NoExternalStorageException, IOException {
throw new IOException("Backup password is null");
}

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

try {
FullBackupExporter.export(context,
Expand All @@ -111,6 +116,21 @@ public void onRun() throws NoExternalStorageException, IOException {
}
}

private static void deleteOldTemporaryBackups(@NonNull File backupDirectory) {
for (File file : backupDirectory.listFiles()) {
if (file.isFile()) {
String name = file.getName();
if (name.startsWith(TEMP_BACKUP_FILE_PREFIX) && name.endsWith(TEMP_BACKUP_FILE_SUFFIX)) {
if (file.delete()) {
Log.w(TAG, "Deleted old temporary backup file");
} else {
Log.w(TAG, "Could not delete old temporary backup file");
}
}
}
}
}

@Override
public boolean onShouldRetry(@NonNull Exception e) {
return false;
Expand Down

0 comments on commit aff57fb

Please sign in to comment.