Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1afd1c90 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Maybe fix issue #2457218: Corrupt batterystats.bin file preventing phone boot - LIBtt68127

No steps to repro, but makes the code more robust by using the standard
JournaledFile class and doing sanity checks on the input it reads.

This required moving the JournaledFile class in to the framework (and
we really should get rid of either it or AtomicFile, but they have
different recovery semantics so that is tough).  Also went through and
cleaned up the file management in various places.

Change-Id: Ieb7268d8435e77dff66b6e67bb63b62e5dea572e
parent b58fd822
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -2935,10 +2935,15 @@ class ContextImpl extends Context {
        private boolean writeFileLocked() {
            // Rename the current file so it may be used as a backup during the next read
            if (mFile.exists()) {
                if (!mBackupFile.exists()) {
                    if (!mFile.renameTo(mBackupFile)) {
                    Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile);
                        Log.e(TAG, "Couldn't rename file " + mFile
                                + " to backup file " + mBackupFile);
                        return false;
                    }
                } else {
                    mFile.delete();
                }
            }
            
            // Attempt to write the file, delete the backup and return true as atomically as
+3 −0
Original line number Diff line number Diff line
@@ -115,6 +115,9 @@ public class FileUtils
     */
    public static boolean copyToFile(InputStream inputStream, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            OutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
+3 −0
Original line number Diff line number Diff line
@@ -546,6 +546,9 @@ public class ViewDebug {
        recyclerDump = new File(Environment.getExternalStorageDirectory(), "view-recycler/");
        recyclerDump = new File(recyclerDump, sRecyclerTracePrefix + ".traces");
        try {
            if (recyclerDump.exists()) {
                recyclerDump.delete();
            }
            final FileOutputStream file = new FileOutputStream(recyclerDump);
            final DataOutputStream out = new DataOutputStream(file);

+3 −0
Original line number Diff line number Diff line
@@ -105,6 +105,9 @@ public class LocalTransport extends IBackupTransport.Stub {
                        + " key64=" + base64Key);

                if (dataSize >= 0) {
                    if (entityFile.exists()) {
                        entityFile.delete();
                    }
                    FileOutputStream entity = new FileOutputStream(entityFile);

                    if (dataSize > bufSize) {
+3 −2
Original line number Diff line number Diff line
@@ -45,12 +45,13 @@ public class AtomicFile {
    public FileOutputStream startWrite() throws IOException {
        // Rename the current file so it may be used as a backup during the next read
        if (mBaseName.exists()) {
            if (!mBaseName.renameTo(mBackupName)) {
                mBackupName.delete();
            if (!mBackupName.exists()) {
                if (!mBaseName.renameTo(mBackupName)) {
                    Log.w("AtomicFile", "Couldn't rename file " + mBaseName
                            + " to backup file " + mBackupName);
                }
            } else {
                mBaseName.delete();
            }
        }
        FileOutputStream str = null;
Loading