Fix recovery file syncing logic 42/243942/2
authorJunghyun Yeon <jungh.yeon@samsung.com>
Fri, 11 Sep 2020 08:47:42 +0000 (17:47 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 15 Sep 2020 06:46:05 +0000 (15:46 +0900)
- 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 <jungh.yeon@samsung.com>
src/common/recovery_file.cc

index f28f0f3..e092522 100644 (file)
@@ -5,12 +5,15 @@
 #include "common/recovery_file.h"
 
 #include <boost/filesystem/operations.hpp>
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/device/file_descriptor.hpp>
 #include <boost/system/error_code.hpp>
 
 #include <manifest_parser/utils/logging.h>
 
 #include <array>
 #include <cstring>
+#include <string>
 #include <map>
 #include <utility>
 
@@ -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<bi::file_descriptor_sink> 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;