Synchronize save, delete signature 68/207768/7
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 12 Jun 2019 07:18:13 +0000 (16:18 +0900)
committerilho kim <ilho159.kim@samsung.com>
Tue, 2 Jul 2019 03:59:57 +0000 (03:59 +0000)
To prepare for sudden power loss,
write the signature infomation to the physical disk
and then synchronously delete the unpacked signature file using fsync()

Change-Id: I2a23f518cc7e71634ad93e00b594505223caee4c
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
CMakeLists.txt
src/common/signature.cc
src/common/signature.h

index 79a9822..8af046e 100644 (file)
@@ -67,7 +67,7 @@ PKG_CHECK_MODULES(TTRACE_DEPS REQUIRED ttrace)
 PKG_CHECK_MODULES(TRUST_ANCHOR_DEPS REQUIRED tanchor)
 PKG_CHECK_MODULES(GUM_DEPS REQUIRED libgum)
 
-FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem program_options)
+FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem program_options iostreams)
 FIND_PACKAGE(GTest REQUIRED)
 
 ADD_SUBDIRECTORY(src)
index 21d0e52..15da992 100644 (file)
@@ -5,6 +5,7 @@
 #include "common/signature.h"
 
 #include <tzplatform_config.h>
+#include <unistd.h>
 
 #include <string>
 #include <regex>
@@ -16,6 +17,7 @@
 #include "common/utils/file_util.h"
 
 namespace bf = boost::filesystem;
+namespace bi = boost::iostreams;
 namespace ci = common_installer;
 
 namespace common_installer {
@@ -115,13 +117,12 @@ bool Signature::CheckMetadataPrivilege(PrivilegeLevel level,
   return true;
 }
 
-bool Signature::StoreSignature(std::ofstream* ofs,
+bool Signature::StoreSignature(bi::stream<bi::file_descriptor_sink> *ofs,
     const ValidationCore::CertificatePtr& cert,
     const ValidationCore::CertificatePtr& im_cert,
     const ValidationCore::CertificatePtr& root_cert) {
   if (!ofs)
     return false;
-
   *ofs << ((cert) ? cert->getBase64().c_str() : "") << std::endl;
   *ofs << ((im_cert) ? im_cert->getBase64().c_str() : "") << std::endl;
   *ofs << ((root_cert) ? root_cert->getBase64().c_str() : "") << std::endl;
@@ -130,8 +131,7 @@ bool Signature::StoreSignature(std::ofstream* ofs,
 
 bool Signature::Store() {
   bool ret = true;
-  std::ofstream ofs(file_path_.c_str(),
-      std::ios::out | std::ios::trunc);
+  bi::stream<bi::file_descriptor_sink> ofs(file_path_);
   if (!StoreSignature(&ofs,
                       cert_info_->dist2_cert.get(),
                       cert_info_->dist2_im_cert.get(),
@@ -141,6 +141,8 @@ bool Signature::Store() {
                       cert_info_->dist_im_cert.get(),
                       cert_info_->dist_root_cert.get()))
     ret = false;
+  ofs.flush();
+  ::fsync(ofs->handle());
   ofs.close();
   return ret;
 }
index a73d767..689a537 100644 (file)
@@ -7,11 +7,15 @@
 
 #include <pkgmgrinfo_basic.h>
 
+#include <boost/iostreams/stream.hpp>
+#include <boost/iostreams/device/file_descriptor.hpp>
+
 #include <string>
 
 #include "common/installer_context.h"
 
 namespace bf = boost::filesystem;
+namespace bi = boost::iostreams;
 
 namespace common_installer {
 
@@ -35,7 +39,7 @@ class Signature {
                              std::string* error_message);
   bool CheckSignatureMismatch(std::string* error_message);
   bool SetPath();
-  bool StoreSignature(std::ofstream* ofs,
+  bool StoreSignature(bi::stream<bi::file_descriptor_sink> *ofs,
       const ValidationCore::CertificatePtr& cert,
       const ValidationCore::CertificatePtr& im_cert,
       const ValidationCore::CertificatePtr& root_cert);