From 45eee7fe521901f4a2e271d34212267bc3974f84 Mon Sep 17 00:00:00 2001 From: Zhou Xuezan Date: Tue, 17 Jan 2023 11:32:40 +0800 Subject: [PATCH] OTA: make lp_metadata write more atomic Powerloss of OS failure occurs when super is merging, then the device can't bring up again for no lp_metadata sync, so make lp_metadata sync in time. Change-Id: I1ea31662b838e35022a566b614796f2d5e05df4b Signed-off-by: Jia Jia --- fs_mgr/libfiemap/metadata.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fs_mgr/libfiemap/metadata.cpp b/fs_mgr/libfiemap/metadata.cpp index b0dfb5c90..22b8afb7d 100644 --- a/fs_mgr/libfiemap/metadata.cpp +++ b/fs_mgr/libfiemap/metadata.cpp @@ -30,6 +30,7 @@ namespace android { namespace fiemap { using namespace android::fs_mgr; +using android::base::unique_fd; static constexpr uint32_t kMaxMetadataSize = 256 * 1024; @@ -109,10 +110,18 @@ bool SaveMetadata(MetadataBuilder* builder, const std::string& metadata_dir) { if (exported->partitions.empty() && android::base::RemoveFileIfExists(metadata_file)) { return true; } - if (!WriteToImageFile(metadata_file, *exported.get())) { + + unique_fd fd(open(metadata_file.c_str(), O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC | O_BINARY | O_SYNC, 0644)); + if (fd < 0) { + LOG(ERROR) << "open failed: " << metadata_file; + return false; + } + + if (!WriteToImageFile(fd, *exported.get())) { LOG(ERROR) << "Unable to save new metadata"; return false; } + return true; }