From 1588ae80ac2a2b9220d9295f509ccd66cb1945b2 Mon Sep 17 00:00:00 2001 From: Jin Seok Park Date: Tue, 13 Oct 2020 11:19:34 +0900 Subject: [PATCH] Change saveAttributes implementation Context: http://shortn/_b43rjiwjmE TLDR: saveAttributes renames and removes original file, which results in MediaProvider removing it from its DB and thus unintentionally removing other associated data. This CL changes saveAttributes implementation to copy the original file to a temp file and write over the original file instead to prevent it from being removed in the MediaProvider DB. See aosp/1428429 for reference. Bug: 168706052 Test: atest CtsMediaTestCases:android.media.cts.ExifInterfaceTest Change-Id: Ib01471e5fb8f1610a9a8eba741bea9a084021151 --- media/java/android/media/ExifInterface.java | 38 ++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index 8845d6954db20..f9cbdd42bc4f0 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -70,7 +70,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TimeZone; -import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.CRC32; @@ -2088,28 +2087,18 @@ public class ExifInterface { FileInputStream in = null; FileOutputStream out = null; - File originalFile = null; - if (mFilename != null) { - originalFile = new File(mFilename); - } File tempFile = null; try { - // Move the original file to temporary file. + // Copy the original file to temporary file. + tempFile = File.createTempFile("temp", "tmp"); if (mFilename != null) { - String parent = originalFile.getParent(); - String name = originalFile.getName(); - String tempPrefix = UUID.randomUUID().toString() + "_"; - tempFile = new File(parent, tempPrefix + name); - if (!originalFile.renameTo(tempFile)) { - throw new IOException("Couldn't rename to " + tempFile.getAbsolutePath()); - } + in = new FileInputStream(mFilename); } else if (mSeekableFileDescriptor != null) { - tempFile = File.createTempFile("temp", "tmp"); Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET); in = new FileInputStream(mSeekableFileDescriptor); - out = new FileOutputStream(tempFile); - copy(in, out); } + out = new FileOutputStream(tempFile); + copy(in, out); } catch (Exception e) { throw new IOException("Failed to copy original file to temp file", e); } finally { @@ -2139,12 +2128,23 @@ public class ExifInterface { } } } catch (Exception e) { + // Restore original file + in = new FileInputStream(tempFile); if (mFilename != null) { - if (!tempFile.renameTo(originalFile)) { - throw new IOException("Couldn't restore original file: " - + originalFile.getAbsolutePath()); + out = new FileOutputStream(mFilename); + } else if (mSeekableFileDescriptor != null) { + try { + Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET); + } catch (ErrnoException exception) { + throw new IOException("Failed to save new file. Original file may be " + + "corrupted since error occurred while trying to restore it.", + exception); } + out = new FileOutputStream(mSeekableFileDescriptor); } + copy(in, out); + closeQuietly(in); + closeQuietly(out); throw new IOException("Failed to save new file", e); } finally { closeQuietly(in);