[Copy] Move files instead of copy if possible 55/37255/2
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 12 Mar 2015 14:25:06 +0000 (15:25 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Thu, 26 Mar 2015 13:02:49 +0000 (06:02 -0700)
If move is not possible then fall back to copy/remove.

Change-Id: Icf2dbbf63b772ae6eccb4682a4295b397ca91548

src/common/step/step_copy.cc

index b29bf40..5fffc07 100644 (file)
 namespace common_installer {
 namespace copy {
 
-namespace fs = boost::filesystem;
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
 
 Step::Status StepCopy::process() {
   assert(!context_->pkgid().empty());
 
-  fs::path install_path = fs::path(context_->GetApplicationPath());
-
+  bf::path install_path = bf::path(context_->GetApplicationPath());
 
   context_->set_pkg_path(install_path.string());
 
@@ -27,28 +27,43 @@ Step::Status StepCopy::process() {
   // considering that multiple apps data are already separated in folders.
   if (context_->manifest_data()->uiapplication &&
       !context_->manifest_data()->uiapplication->next)
-    install_path /= fs::path(context_->manifest_data()->mainapp_id);
+    install_path /= bf::path(context_->manifest_data()->mainapp_id);
 
-  if (!utils::CopyDir(fs::path(context_->unpacked_dir_path()), install_path)) {
-    LOG(ERROR) << "Fail to copy tmp dir: " << context_->unpacked_dir_path()
-        << " to dst dir: " << install_path.string();
+  bs::error_code error;
+  bf::create_directories(install_path.parent_path(), error);
+  if (error) {
+    LOG(ERROR) << "Cannot create directory: "
+               << install_path.parent_path().string();
     return Step::Status::ERROR;
   }
-
-  LOG(INFO) << "Successfully copy: " << context_->unpacked_dir_path()
-      << " to: " << install_path.string() << " directory";
+  bf::rename(context_->unpacked_dir_path(), install_path, error);
+  if (error) {
+    LOG(DEBUG) << "Cannot move directory. Will try to copy...";
+    if (!utils::CopyDir(bf::path(context_->unpacked_dir_path()),
+        install_path)) {
+      LOG(ERROR) << "Fail to copy tmp dir: " << context_->unpacked_dir_path()
+                 << " to dst dir: " << install_path.string();
+      return Step::Status::ERROR;
+    }
+    bs::error_code error;
+    bf::remove_all(context_->unpacked_dir_path(), error);
+    if (error) {
+      LOG(WARNING) << "Cannot remove temporary directory: "
+                   << context_->unpacked_dir_path();
+    }
+  }
+  LOG(INFO) << "Successfully move/copy: " << context_->unpacked_dir_path()
+            << " to: " << install_path.string() << " directory";
   return Status::OK;
 }
 
 Step::Status StepCopy::clean() {
-  LOG(DEBUG) << "Remove tmp dir: " << context_->unpacked_dir_path();
-  fs::remove_all(context_->unpacked_dir_path());
   return Status::OK;
 }
 
 Step::Status StepCopy::undo() {
-  if (fs::exists(context_->pkg_path()))
-    fs::remove_all(context_->pkg_path());
+  if (bf::exists(context_->pkg_path()))
+    bf::remove_all(context_->pkg_path());
   return Status::OK;
 }