Extract MoveDir() to utils 56/38256/1
authorTomasz Iwanek <t.iwanek@samsung.com>
Fri, 27 Mar 2015 08:26:38 +0000 (09:26 +0100)
committerTomasz Iwanek <t.iwanek@samsung.com>
Wed, 15 Apr 2015 11:21:07 +0000 (13:21 +0200)
Change-Id: Iae4d60d42b9c90003de4ed23070c9e34d358991d

src/common/step/step_copy.cc
src/utils/file_util.cc
src/utils/file_util.h

index 8a3e8b7..27db6e8 100644 (file)
@@ -40,25 +40,13 @@ Step::Status StepCopy::process() {
                << install_path.parent_path().string();
     return Step::Status::ERROR;
   }
-  bf::rename(context_->unpacked_dir_path.get(), install_path, error);
-  if (error) {
-    LOG(DEBUG) << "Cannot move directory. Will try to copy...";
-    if (!utils::CopyDir(bf::path(context_->unpacked_dir_path.get()),
-        install_path)) {
-      LOG(ERROR) << "Fail to copy tmp dir: "
-          << context_->unpacked_dir_path.get()
-          << " to dst dir: " << install_path.string();
-      return Step::Status::ERROR;
-    }
-    bs::error_code error;
-    bf::remove_all(context_->unpacked_dir_path.get(), error);
-    if (error) {
-      LOG(WARNING) << "Cannot remove temporary directory: "
-                   << context_->unpacked_dir_path.get();
-    }
+  if (!utils::MoveDir(context_->unpacked_dir_path.get(), install_path)) {
+    LOG(ERROR) << "Cannot move widget directory to install path";
+    return Status::ERROR;
   }
-  LOG(INFO) << "Successfully move/copy: " << context_->unpacked_dir_path.get()
-            << " to: " << install_path.string() << " directory";
+
+  LOG(INFO) << "Successfully move: " << context_->unpacked_dir_path.get()
+            << " to: " << install_path << " directory";
   return Status::OK;
 }
 
index 5357223..b61ec84 100644 (file)
@@ -3,6 +3,7 @@
 #include "utils/file_util.h"
 
 #include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
 #include <string>
 
 #include "utils/logging.h"
@@ -10,6 +11,7 @@
 namespace common_installer {
 namespace utils {
 
+namespace bs = boost::system;
 namespace fs = boost::filesystem;
 
 bool CreateDir(const fs::path& path) {
@@ -79,5 +81,44 @@ bool CopyDir(const fs::path& src, const fs::path& dst) {
   return true;
 }
 
+bool MoveDir(const fs::path& src, const fs::path& dst) {
+  if (fs::exists(dst))
+    return false;
+  bs::error_code error;
+  fs::rename(src, dst, error);
+  if (error) {
+    LOG(WARNING) << "Cannot move directory: " << src << ". Will copy/remove...";
+    if (!utils::CopyDir(src, dst)) {
+      LOG(ERROR) << "Cannot copy directory: " << src;
+      return false;
+    }
+    fs::remove_all(src, error);
+    if (error) {
+      LOG(ERROR) << "Cannot remove old directory when coping: " << src;
+      return false;
+    }
+  }
+  return true;
+}
+
+bool MoveFile(const fs::path& src, const fs::path& dst) {
+  if (fs::exists(dst))
+    return false;
+  bs::error_code error;
+  fs::rename(src, dst, error);
+  if (error) {
+    LOG(WARNING) << "Cannot move file: " << src << ". Will copy/remove...";
+    fs::copy_file(src, dst, fs::copy_option::overwrite_if_exists, error);
+    if (error) {
+      return false;
+    }
+    fs::remove_all(src, error);
+    if (error) {
+      LOG(ERROR) << "Cannot remove old file when coping: " << src;
+    }
+  }
+  return true;
+}
+
 }  // namespace utils
 }  // namespace common_installer
index c46b64f..e45bb86 100644 (file)
@@ -13,6 +13,12 @@ bool CreateDir(const boost::filesystem::path& path);
 bool CopyDir(const boost::filesystem::path& src,
              const boost::filesystem::path& dst);
 
+bool MoveDir(const boost::filesystem::path& src,
+             const boost::filesystem::path& dst);
+
+bool MoveFile(const boost::filesystem::path& src,
+              const boost::filesystem::path& dst);
+
 }  // namespace utils
 }  // namespace common_installer