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

Commit 9481bbbf authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Maintain compatibility by deleting target directory before renaming...

Merge "Maintain compatibility by deleting target directory before renaming into it." into rvc-dev am: 5c77ec2e

Change-Id: If013b8d953efa837a041b9f6cfb72f57cfbe8559
parents f9765818 5c77ec2e
Loading
Loading
Loading
Loading
+20 −11
Original line number Original line Diff line number Diff line
@@ -116,10 +116,7 @@ public class AtomicFile {
        mStartTime = startTime;
        mStartTime = startTime;


        if (mLegacyBackupName.exists()) {
        if (mLegacyBackupName.exists()) {
            if (!mLegacyBackupName.renameTo(mBaseName)) {
            rename(mLegacyBackupName, mBaseName);
                Log.e(LOG_TAG, "Failed to rename legacy backup file " + mLegacyBackupName
                        + " to base file " + mBaseName);
            }
        }
        }


        try {
        try {
@@ -157,9 +154,7 @@ public class AtomicFile {
        } catch (IOException e) {
        } catch (IOException e) {
            Log.e(LOG_TAG, "Failed to close file output stream", e);
            Log.e(LOG_TAG, "Failed to close file output stream", e);
        }
        }
        if (!mNewName.renameTo(mBaseName)) {
        rename(mNewName, mBaseName);
            Log.e(LOG_TAG, "Failed to rename new file " + mNewName + " to base file " + mBaseName);
        }
        if (mCommitTag != null) {
        if (mCommitTag != null) {
            com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
            com.android.internal.logging.EventLogTags.writeCommitSysConfigFile(
                    mCommitTag, SystemClock.uptimeMillis() - mStartTime);
                    mCommitTag, SystemClock.uptimeMillis() - mStartTime);
@@ -221,10 +216,7 @@ public class AtomicFile {
     */
     */
    public FileInputStream openRead() throws FileNotFoundException {
    public FileInputStream openRead() throws FileNotFoundException {
        if (mLegacyBackupName.exists()) {
        if (mLegacyBackupName.exists()) {
            if (!mLegacyBackupName.renameTo(mBaseName)) {
            rename(mLegacyBackupName, mBaseName);
                Log.e(LOG_TAG, "Failed to rename legacy backup file " + mLegacyBackupName
                        + " to base file " + mBaseName);
            }
        }
        }


        // It was okay to call openRead() between startWrite() and finishWrite() for the first time
        // It was okay to call openRead() between startWrite() and finishWrite() for the first time
@@ -311,4 +303,21 @@ public class AtomicFile {
            IoUtils.closeQuietly(out);
            IoUtils.closeQuietly(out);
        }
        }
    }
    }

    private static void rename(File source, File target) {
        // We used to delete the target file before rename, but that isn't atomic, and the rename()
        // syscall should atomically replace the target file. However in the case where the target
        // file is a directory, a simple rename() won't work. We need to delete the file in this
        // case because there are callers who erroneously called mBaseName.mkdirs() (instead of
        // mBaseName.getParentFile().mkdirs()) before creating the AtomicFile, and it worked
        // regardless, so this deletion became some kind of API.
        if (target.isDirectory()) {
            if (!target.delete()) {
                Log.e(LOG_TAG, "Failed to delete file which is a directory " + target);
            }
        }
        if (!source.renameTo(target)) {
            Log.e(LOG_TAG, "Failed to rename " + source + " to " + target);
        }
    }
}
}