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
diff --git a/services/core/java/com/android/server/rollback/RollbackStore.java b/services/core/java/com/android/server/rollback/RollbackStore.java
index 2cfc785..f973f5c 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 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 @@
"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);