Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / step / backup / step_copy_backup.cc
index 09a2252..6460cf2 100644 (file)
@@ -7,28 +7,27 @@
 #include <sys/types.h>
 #include <tzplatform_config.h>
 
-#include <boost/filesystem/operations.hpp>
-#include <boost/system/error_code.hpp>
-
+#include <algorithm>
 #include <cassert>
 #include <cstdint>
+#include <filesystem>
 #include <string>
+#include <system_error>
 
 #include "common/utils/paths.h"
 #include "common/shared_dirs.h"
 #include "common/utils/file_util.h"
 #include "common/utils/user_util.h"
 
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
 namespace ci = common_installer;
+namespace fs = std::filesystem;
 
 namespace {
 
 const char kExternalMemoryMountPoint[] = ".mmc";
 const char kSharedResPath[] = "shared/res";
 
-bool CheckFreeSpace(const bf::path& backup_path, const bf::path& shared_path) {
+bool CheckFreeSpace(const fs::path& backup_path, const fs::path& shared_path) {
   int64_t shared_size = ci::GetDirectorySize(shared_path);
 
   if (!ci::CheckFreeSpaceAtPath(shared_size, backup_path))
@@ -37,10 +36,10 @@ bool CheckFreeSpace(const bf::path& backup_path, const bf::path& shared_path) {
   return true;
 }
 
-bool CreateSharedRes(const bf::path& src, const bf::path& dst) {
-  bs::error_code error;
+bool CreateSharedRes(const fs::path& src, const fs::path& dst) {
+  std::error_code error;
 
-  bf::create_directories(dst / kSharedResPath, error);
+  fs::create_directories(dst / kSharedResPath, error);
   if (error) {
     LOG(ERROR) << "Cannot create package directory";
     return false;
@@ -54,10 +53,10 @@ bool CreateSharedRes(const bf::path& src, const bf::path& dst) {
   return true;
 }
 
-bool Move(const boost::filesystem::path& from,
-    const boost::filesystem::path& to,
+bool Move(const std::filesystem::path& from,
+    const std::filesystem::path& to,
     common_installer::FSFlag flag = common_installer::FSFlag::FS_NONE) {
-  if (bf::is_directory(from)) {
+  if (fs::is_directory(from)) {
     if (!common_installer::MoveDir(from, to / from.filename(), flag)) {
       LOG(ERROR) << "Failed to move directory: " << from;
       return false;
@@ -93,8 +92,8 @@ Step::Status StepCopyBackup::precheck() {
 
   // We only "copy" shared dir for backup.
   // So if there is no shared dir, we don't need to check free space.
-  bf::path shared_dir = install_path_ / "shared";
-  if (!bf::exists(shared_dir))
+  fs::path shared_dir = install_path_ / "shared";
+  if (!fs::exists(shared_dir))
     return Status::OK;
 
   if (!CheckFreeSpace(backup_path_, shared_dir)) {
@@ -134,7 +133,7 @@ Step::Status StepCopyBackup::undo() {
   if (context_->external_storage)
     context_->external_storage->Abort();
 
-  if (!bf::exists(backup_path_))
+  if (!fs::exists(backup_path_))
     return Status::OK;
 
   if (!RollbackApplicationDirectory()) {
@@ -148,18 +147,18 @@ Step::Status StepCopyBackup::undo() {
 }
 
 bool StepCopyBackup::Backup() {
-  bs::error_code error;
+  std::error_code error;
 
-  if (!bf::exists(backup_path_)) {
-    bf::create_directories(backup_path_, error);
+  if (!fs::exists(backup_path_)) {
+    fs::create_directories(backup_path_, error);
     if (error) {
       LOG(ERROR) << "Failed to create backup directory: " << backup_path_;
       return false;
     }
   }
   // create copy of old package content skipping the external memory mount point
-  for (bf::directory_iterator iter(context_->GetPkgPath());
-       iter != bf::directory_iterator(); ++iter) {
+  for (fs::directory_iterator iter(context_->GetPkgPath());
+       iter != fs::directory_iterator(); ++iter) {
     if (iter->path().filename() == kExternalMemoryMountPoint)
       continue;
 
@@ -168,7 +167,7 @@ bool StepCopyBackup::Backup() {
     if (context_->external_storage) {
       auto& ext_dirs = context_->external_storage->external_dirs();
       auto found = std::find(ext_dirs.begin(), ext_dirs.end(),
-                             iter->path().filename());
+                             iter->path().filename().string());
       if (found != ext_dirs.end()) {
         bool done = MoveMountPointContent(iter->path(),
             backup_path_ / iter->path().filename());
@@ -191,15 +190,15 @@ bool StepCopyBackup::Backup() {
 }
 
 
-bool StepCopyBackup::MoveMountPointContent(const boost::filesystem::path& from,
-                                           const boost::filesystem::path& to) {
-  bs::error_code error;
-  bf::create_directories(to, error);
+bool StepCopyBackup::MoveMountPointContent(const std::filesystem::path& from,
+                                           const std::filesystem::path& to) {
+  std::error_code error;
+  fs::create_directories(to, error);
 
-  for (bf::directory_iterator iter(from);
-       iter != bf::directory_iterator(); ++iter) {
-    if (bf::is_symlink(symlink_status(iter->path()))) {
-      bf::copy_symlink(iter->path(), to / iter->path().filename(), error);
+  for (fs::directory_iterator iter(from);
+       iter != fs::directory_iterator(); ++iter) {
+    if (fs::is_symlink(symlink_status(iter->path()))) {
+      fs::copy_symlink(iter->path(), to / iter->path().filename(), error);
       if (error) {
         LOG(ERROR) << "Failed to backup package symlink: " << iter->path();
         return false;
@@ -217,11 +216,11 @@ bool StepCopyBackup::MoveMountPointContent(const boost::filesystem::path& from,
 
 void StepCopyBackup::RemoveContent() {
   if (context_->request_type.get() == RequestType::Update &&
-    !context_->external_storage && bf::exists(install_path_ / ".mmc")) {
+    !context_->external_storage && fs::exists(install_path_ / ".mmc")) {
     LOG(WARNING) << "Remove unnecessary files for external storage";
 
-    bs::error_code error;
-    bf::remove((install_path_ / ".mmc"), error);
+    std::error_code error;
+    fs::remove((install_path_ / ".mmc"), error);
     if (error)
       LOG(WARNING) << "error while remove files";
   }
@@ -230,8 +229,8 @@ void StepCopyBackup::RemoveContent() {
 bool StepCopyBackup::NewContent() {
   ci::RemoveRWDirectories(context_->unpacked_dir_path.get());
 
-  bs::error_code error;
-  bf::create_directories(install_path_.parent_path(), error);
+  std::error_code error;
+  fs::create_directories(install_path_.parent_path(), error);
   if (error) {
     LOG(ERROR) << "Cannot create package directory";
     return false;
@@ -242,8 +241,8 @@ bool StepCopyBackup::NewContent() {
       return false;
   }
 
-  for (bf::directory_iterator iter(context_->unpacked_dir_path.get());
-       iter != bf::directory_iterator(); ++iter) {
+  for (fs::directory_iterator iter(context_->unpacked_dir_path.get());
+       iter != fs::directory_iterator(); ++iter) {
     if (!Move(iter->path(), install_path_, FS_MERGE_SKIP))
       return false;
   }
@@ -252,12 +251,12 @@ bool StepCopyBackup::NewContent() {
   // it will be failed due to permission deny. Set its permission before
   // StepChangeOwnershipAndPermission to prevent it.
   uid_t uid = context_->uid.get();
-  boost::optional<gid_t> gid = common_installer::GetGidByUid(uid);
+  std::optional<gid_t> gid = common_installer::GetGidByUid(uid);
   if (!gid) {
     LOG(ERROR) << "Failed to get gid";
     return false;
   }
-  if (bf::exists(install_path_ / "shared/res") &&
+  if (fs::exists(install_path_ / "shared/res") &&
       !common_installer::SetOwnershipAll(
           install_path_ / "shared/res", uid, *gid)) {
     LOG(ERROR) << "Failed to set ownership";
@@ -275,19 +274,19 @@ bool StepCopyBackup::CleanBackupDirectory() {
 }
 
 bool StepCopyBackup::RollbackApplicationDirectory() {
-  bs::error_code error;
-  bf::path root_path = context_->GetPkgPath();
-  if (bf::exists(root_path)) {
-    for (bf::directory_iterator iter(root_path);
-         iter != bf::directory_iterator(); ++iter) {
-      bf::remove_all(iter->path(), error);
+  std::error_code error;
+  fs::path root_path = context_->GetPkgPath();
+  if (fs::exists(root_path)) {
+    for (fs::directory_iterator iter(root_path);
+         iter != fs::directory_iterator(); ++iter) {
+      fs::remove_all(iter->path(), error);
       if (error)
         return false;
     }
   }
 
-  for (bf::directory_iterator iter(backup_path_);
-       iter != bf::directory_iterator(); ++iter) {
+  for (fs::directory_iterator iter(backup_path_);
+       iter != fs::directory_iterator(); ++iter) {
     if (!Move(iter->path(), root_path)) {
       LOG(ERROR) << "Failed to recovery backup file(" << iter->path() << ")";
       return false;
@@ -311,8 +310,8 @@ void StepCopyBackup::AddRecoveryInfo() {
 }
 
 bool StepCopyBackup::ShouldBackupSharedRes() {
-  if (bf::exists(backup_path_ / kSharedResPath) &&
-      bf::exists(context_->unpacked_dir_path.get() / kSharedResPath))
+  if (fs::exists(backup_path_ / kSharedResPath) &&
+      fs::exists(context_->unpacked_dir_path.get() / kSharedResPath))
     return true;
 
   return false;