From c5375f2f23f0ae4813c35502b4a4989815605d2d Mon Sep 17 00:00:00 2001 From: JW Wang Date: Thu, 9 Sep 2021 16:05:57 +0800 Subject: [PATCH] Fix that the file is not correctly flushed to the disk Fix a bug where rollback is corrupted when system_server crasshes and reboots into RAMDUMP mode. AtomicFile.finishWrite() calls FileUtils.sync() on the stream internally which requires the stream is flushed but not yet closed. pw.close() will close the stream which conflicts the use of AtomicFile.finishWrite(). Let's remove the use of PrintWriter and flush the stream before calling AtomicFile.finishWrite() so the file is correctly persisted to the disk. Bug: 199124163 Test: atest RollbackManagerHostTest#testNativeWatchdogTriggersRollback and check no "AtomicFile: Failed to sync file output stream" appears in the log messages. Test: atest CtsRootRollbackManagerHostTestCases \ CtsRollbackManagerHostTestCases \ CtsRollbackManagerTestCases Change-Id: I6c93f20949f6103a7fe28fa2955155cd19df018a --- .../java/com/android/server/rollback/RollbackStore.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/rollback/RollbackStore.java b/services/core/java/com/android/server/rollback/RollbackStore.java index 2cfc7856c197b..f973f5ce9ce03 100644 --- a/services/core/java/com/android/server/rollback/RollbackStore.java +++ b/services/core/java/com/android/server/rollback/RollbackStore.java @@ -43,7 +43,6 @@ import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.PrintWriter; import java.nio.file.Files; import java.text.ParseException; import java.time.Instant; @@ -323,9 +322,8 @@ class RollbackStore { "extensionVersions", extensionVersionsToJson(rollback.getExtensionVersions())); fos = file.startWrite(); - PrintWriter pw = new PrintWriter(fos); - pw.println(dataJson.toString()); - pw.close(); + fos.write(dataJson.toString().getBytes()); + fos.flush(); file.finishWrite(fos); } catch (JSONException | IOException e) { Slog.e(TAG, "Unable to save rollback for: " + rollback.info.getRollbackId(), e);