From 3ee2e20a5850c31993d49c70e0c576a4a1f13127 Mon Sep 17 00:00:00 2001 From: Junghyun Yeon Date: Fri, 11 Sep 2020 17:47:42 +0900 Subject: [PATCH] Fix recovery file syncing logic - Rename() is atomic itself so it doesn't have to be synced. - Change recovery writing logic using boost iostream and fsync() using file descriptor Change-Id: I3ef8fe8ba9fb649581d67c1f843c887cb4a57f3b Signed-off-by: Junghyun Yeon --- src/common/recovery_file.cc | 46 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/common/recovery_file.cc b/src/common/recovery_file.cc index f28f0f3..e092522 100644 --- a/src/common/recovery_file.cc +++ b/src/common/recovery_file.cc @@ -5,12 +5,15 @@ #include "common/recovery_file.h" #include +#include +#include #include #include #include #include +#include #include #include @@ -19,6 +22,7 @@ namespace bf = boost::filesystem; namespace bs = boost::system; +namespace bi = boost::iostreams; namespace ci = common_installer; namespace { @@ -219,54 +223,50 @@ bool RecoveryFile::WriteAndCommitFileContent() { ", error: " << error; return false; } - sync(); } - FILE* handle = fopen(path_.c_str(), "wx"); - if (!handle) { + bi::stream ofs(path_); + if (!ofs) { LOG(ERROR) << "Cannot write recovery file"; return false; } + switch (type_) { case RequestType::Install: - fputs(kRecoveryInstallString, handle); + ofs << kRecoveryInstallString << std::endl; break; case RequestType::Update: - fputs(kRecoveryUpdateString, handle); + ofs << kRecoveryUpdateString << std::endl; break; case RequestType::Uninstall: - fputs(kRecoveryUninstallationString, handle); + ofs << kRecoveryUninstallationString << std::endl; break; case RequestType::Reinstall: - fputs(kRecoveryRdsString, handle); + ofs << kRecoveryRdsString << std::endl; break; case RequestType::Delta: - fputs(kRecoveryDeltaString, handle); + ofs << kRecoveryDeltaString << std::endl; break; case RequestType::MountInstall: - fputs(kRecoveryMountInstallString, handle); + ofs << kRecoveryMountInstallString << std::endl; break; case RequestType::MountUpdate: - fputs(kRecoveryMountUpdateString, handle); + ofs << kRecoveryMountUpdateString << std::endl; break; case RequestType::ReadonlyUpdateInstall: - fputs(kRecoveryReadonlyUpdateInstallString, handle); + ofs << kRecoveryReadonlyUpdateInstallString << std::endl; break; default: - fputs(kRecoveryUnknownString, handle); + ofs << kRecoveryUnknownString << std::endl; break; } - fputs("\n", handle); - fputs(unpacked_dir_.c_str(), handle); - fputs("\n", handle); - fputs(pkgid_.c_str(), handle); - fputs("\n", handle); - fputs(backup_done_ ? "true" : "false", handle); - fputs("\n", handle); - fputs(cleanup_ ? "cleanup" : "rollback", handle); - fputs("\n", handle); - fclose(handle); - sync(); + ofs << unpacked_dir_ << std::endl; + ofs << pkgid_ << std::endl; + ofs << (backup_done_ ? "true" : "false") << std::endl; + ofs << (cleanup_ ? "cleanup" : "rollback") << std::endl; + ofs.flush(); + ::fsync(ofs->handle()); + ofs.close(); Remove(backup_path_); return true; -- 2.7.4