Fix StepConvertXml 00/114700/1
authorSangyoon Jang <s89.jang@samsung.com>
Tue, 14 Feb 2017 12:05:07 +0000 (21:05 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Tue, 14 Feb 2017 12:05:07 +0000 (21:05 +0900)
Backup and restore original manifest file if needed.

Change-Id: Ib5aba7f85c624242ec4ee9320b907322eb73a700
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
src/tpk/step/pkgmgr/step_convert_xml.cc
src/tpk/step/pkgmgr/step_convert_xml.h

index 15298bcbc9f1cf867c9821e4fb14926df238d606..6fedec9cfd2d0488995058f1d48ac16755d85f7a 100644 (file)
@@ -47,6 +47,18 @@ common_installer::Step::Status StepConvertXml::precheck() {
     return Step::Status::MANIFEST_NOT_FOUND;
   }
 
+  new_path_ = bf::path(getUserManifestPath(context_->uid.get(),
+      context_->is_readonly_package.get())) / bf::path(context_->pkgid.get());
+  new_path_ += ".xml";
+  if (bf::exists(new_path_)) {
+    backup_path_ = new_path_.string() + ".bck";
+    bs::error_code error;
+    bf::copy_file(new_path_, backup_path_, error);
+    if (error)
+      LOG(WARNING) << "Failed to backup original manifest file " << new_path_
+                   << " due to error: " << error;
+  }
+
   return Step::Status::OK;
 }
 
@@ -90,22 +102,17 @@ common_installer::Step::Status StepConvertXml::process() {
   if (!ConvertXml(doc))
     return Step::Status::MANIFEST_ERROR;
 
-  bf::path new_path =
-      bf::path(getUserManifestPath(context_->uid.get(),
-         context_->is_readonly_package.get()))
-      / bf::path(context_->pkgid.get());
-  new_path += ".xml";
-  if (!bf::exists(new_path.parent_path())) {
+  if (!bf::exists(new_path_.parent_path())) {
     bs::error_code error;
-    bf::create_directories(new_path.parent_path(), error);
+    bf::create_directories(new_path_.parent_path(), error);
   }
 
-  if (xmlSaveFile(new_path.string().c_str(), doc) == -1) {
+  if (xmlSaveFile(new_path_.string().c_str(), doc) == -1) {
     LOG(ERROR) << "Failed to write xml file";
     return Step::Status::MANIFEST_ERROR;
   }
 
-  context_->xml_path.set(new_path.string());
+  context_->xml_path.set(new_path_.string());
 
   if (pkgmgr_parser_check_manifest_validation(
       context_->xml_path.get().c_str()) != 0) {
@@ -120,14 +127,30 @@ common_installer::Step::Status StepConvertXml::process() {
 }
 
 common_installer::Step::Status StepConvertXml::clean() {
+  if (!backup_path_.empty()) {
+    bs::error_code error;
+    bf::remove(backup_path_, error);
+    LOG(WARNING) << "Failed to remove backup file " << backup_path_
+                 << " due to error: " << error;
+  }
+
   return Step::Status::OK;
 }
 
 common_installer::Step::Status StepConvertXml::undo() {
-  bs::error_code error;
-
-  if (bf::exists(context_->xml_path.get()))
-    bf::remove_all(context_->xml_path.get(), error);
+  if (!backup_path_.empty()) {
+    bs::error_code error;
+    bf::remove(new_path_, error);
+    if (error) {
+      LOG(WARNING) << "Failed to remove " << new_path_
+                   << " due to error " << error;
+    } else {
+      bf::rename(backup_path_, new_path_, error);
+      if (error)
+        LOG(WARNING) << "Failed to restore " << new_path_
+                     << " due to error " << error;
+    }
+  }
 
   return Step::Status::OK;
 }
index f734c3ad6e1c3b4d46317fd366f47f0eb24760d3..ae4393265378a5da03b46f801077e9666aa40ed1 100644 (file)
@@ -26,6 +26,8 @@ class StepConvertXml : public common_installer::Step {
 
  private:
   boost::filesystem::path xml_path_;
+  boost::filesystem::path new_path_;
+  boost::filesystem::path backup_path_;
   bool ConvertXml(xmlDocPtr doc);
 
   STEP_NAME(ConvertXml)