Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/4806
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Fix resource leak in CopyAndUploadContentUrisTask

Input and output streams have been closed per URI iteration using try-with-resources to prevent file descriptor and memory leaks when copying multiple content URIs.

https://github.com/owncloud/android/issues/4797
https://github.com/owncloud/android/pull/4806
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ protected ResultCode doInBackground(Object[] params) {

ResultCode result = ResultCode.UNKNOWN_ERROR;

InputStream inputStream = null;
FileOutputStream outputStream = null;
String fullTempPath = null;
Uri currentUri = null;

Expand All @@ -148,19 +146,20 @@ protected ResultCode doInBackground(Object[] params) {
currentRemotePath = uploadPath + UriUtils.getDisplayNameForUri(currentUri, mAppContext);

fullTempPath = FileStorageUtils.getTemporalPath(account.name, spaceId) + currentRemotePath;
inputStream = leakedContentResolver.openInputStream(currentUri);
File cacheFile = new File(fullTempPath);
File tempDir = cacheFile.getParentFile();
if (!tempDir.exists()) {
tempDir.mkdirs();
}
cacheFile.createNewFile();
outputStream = new FileOutputStream(fullTempPath);
byte[] buffer = new byte[4096];

int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
try (InputStream inputStream = leakedContentResolver.openInputStream(currentUri);
FileOutputStream outputStream = new FileOutputStream(fullTempPath)) {
byte[] buffer = new byte[4096];
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
}
}

filesToUpload.add(fullTempPath);
Expand Down Expand Up @@ -208,22 +207,6 @@ protected ResultCode doInBackground(Object[] params) {
}
}

} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
Timber.w("Ignoring exception of inputStream closure");
}
}

if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
Timber.w("Ignoring exception of outStream closure");
}
}
}

return result;
Expand Down
Loading