From 0bd15960568cdfc2f01f6c922495bcf09042bbf4 Mon Sep 17 00:00:00 2001 From: Steve Soltys Date: Wed, 8 Nov 2017 23:21:27 -0500 Subject: [PATCH] Fix #2 The output file descriptor was not being closed after each chunk was written. 1. The output stream will no longer be stored in the restore state. 2. The output file descriptor will be closed after a chunk is transferred. --- .../ConfigurableBackupTransportService.java | 9 +---- .../ContentProviderRestoreComponent.java | 34 +++++++++---------- .../provider/ContentProviderRestoreState.java | 11 ------ 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransportService.java b/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransportService.java index dfa9e7fe..1e42b012 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransportService.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransportService.java @@ -13,14 +13,7 @@ public class ConfigurableBackupTransportService extends Service { private static ConfigurableBackupTransport backupTransport; public ConfigurableBackupTransportService() { - backupTransport = null; - } - - @Override - public void onCreate() { - if (backupTransport == null) { - backupTransport = new ConfigurableBackupTransport(); - } + backupTransport = new ConfigurableBackupTransport(); } @Override diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreComponent.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreComponent.java index 08b4dd02..a6c95a20 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreComponent.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreComponent.java @@ -85,7 +85,8 @@ public class ContentProviderRestoreComponent implements RestoreComponent { ZipInputStream inputStream = buildInputStream(inputFileDescriptor); Optional zipEntry = seekToEntry(inputStream, fileName); - IoUtils.closeQuietly(inputFileDescriptor.getFileDescriptor()); + IoUtils.closeQuietly(inputFileDescriptor); + IoUtils.closeQuietly(inputStream); return zipEntry.isPresent(); } @@ -120,10 +121,9 @@ public class ContentProviderRestoreComponent implements RestoreComponent { "getRestoreData() for non-key/value dataset"); PackageInfo packageInfo = restoreState.getPackages()[restoreState.getPackageIndex()]; - BackupDataOutput backupDataOutput = new BackupDataOutput(outputFileDescriptor.getFileDescriptor()); try { - return transferIncrementalRestoreData(packageInfo.packageName, backupDataOutput); + return transferIncrementalRestoreData(packageInfo.packageName, outputFileDescriptor); } catch (Exception ex) { Log.e(TAG, "Unable to read backup records: ", ex); @@ -131,10 +131,12 @@ public class ContentProviderRestoreComponent implements RestoreComponent { } } - private int transferIncrementalRestoreData(String packageName, BackupDataOutput backupDataOutput) + private int transferIncrementalRestoreData(String packageName, ParcelFileDescriptor outputFileDescriptor) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException { + ParcelFileDescriptor inputFileDescriptor = buildInputFileDescriptor(); ZipInputStream inputStream = buildInputStream(inputFileDescriptor); + BackupDataOutput backupDataOutput = new BackupDataOutput(outputFileDescriptor.getFileDescriptor()); Optional zipEntryOptional = seekToEntry(inputStream, configuration.getIncrementalBackupDirectory() + packageName); @@ -150,7 +152,8 @@ public class ContentProviderRestoreComponent implements RestoreComponent { zipEntryOptional = seekToEntry(inputStream, configuration.getIncrementalBackupDirectory() + packageName); } - IoUtils.closeQuietly(inputFileDescriptor.getFileDescriptor()); + IoUtils.closeQuietly(inputFileDescriptor); + IoUtils.closeQuietly(outputFileDescriptor); return TRANSPORT_OK; } @@ -172,30 +175,26 @@ public class ContentProviderRestoreComponent implements RestoreComponent { restoreState.setInputStream(inputStream); if (!seekToEntry(inputStream, configuration.getFullBackupDirectory() + name).isPresent()) { - IoUtils.closeQuietly(inputFileDescriptor.getFileDescriptor()); + IoUtils.closeQuietly(inputFileDescriptor); + IoUtils.closeQuietly(outputFileDescriptor); return TRANSPORT_PACKAGE_REJECTED; } } catch (IOException ex) { Log.e(TAG, "Unable to read archive for " + name, ex); - if (inputFileDescriptor != null) { - IoUtils.closeQuietly(inputFileDescriptor.getFileDescriptor()); - } + IoUtils.closeQuietly(inputFileDescriptor); + IoUtils.closeQuietly(outputFileDescriptor); return TRANSPORT_PACKAGE_REJECTED; } } - if (restoreState.getOutputStream() == null) { - restoreState.setOutputStream(new FileOutputStream(outputFileDescriptor.getFileDescriptor())); - } - return transferFullRestoreData(outputFileDescriptor); } private int transferFullRestoreData(ParcelFileDescriptor outputFileDescriptor) { ZipInputStream inputStream = restoreState.getInputStream(); - OutputStream outputStream = restoreState.getOutputStream(); + OutputStream outputStream = new FileOutputStream(outputFileDescriptor.getFileDescriptor()); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int bytesRead = NO_MORE_DATA; @@ -217,14 +216,14 @@ public class ContentProviderRestoreComponent implements RestoreComponent { if (bytesRead == NO_MORE_DATA) { if (restoreState.getInputFileDescriptor() != null) { - IoUtils.closeQuietly(restoreState.getInputFileDescriptor().getFileDescriptor()); + IoUtils.closeQuietly(restoreState.getInputFileDescriptor()); } - IoUtils.closeQuietly(outputFileDescriptor.getFileDescriptor()); restoreState.setInputFileDescriptor(null); restoreState.setInputStream(null); - restoreState.setOutputStream(null); } + + IoUtils.closeQuietly(outputFileDescriptor); } return bytesRead; @@ -260,7 +259,6 @@ public class ContentProviderRestoreComponent implements RestoreComponent { Preconditions.checkState(restoreState.getRestoreType() == TYPE_FULL_STREAM); IoUtils.closeQuietly(restoreState.getInputFileDescriptor()); - IoUtils.closeQuietly(restoreState.getOutputStream()); restoreState = null; } } diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreState.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreState.java index e1f14164..05b49f1a 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreState.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderRestoreState.java @@ -3,7 +3,6 @@ package com.stevesoltys.backup.transport.component.provider; import android.content.pm.PackageInfo; import android.os.ParcelFileDescriptor; -import java.io.OutputStream; import java.util.zip.ZipInputStream; /** @@ -21,8 +20,6 @@ class ContentProviderRestoreState { private ZipInputStream inputStream; - private OutputStream outputStream; - PackageInfo[] getPackages() { return packages; } @@ -47,14 +44,6 @@ class ContentProviderRestoreState { this.restoreType = restoreType; } - OutputStream getOutputStream() { - return outputStream; - } - - void setOutputStream(OutputStream outputStream) { - this.outputStream = outputStream; - } - ZipInputStream getInputStream() { return inputStream; }