Improve StepCopyBackup 25/208025/5
authorJunghyun Yeon <jungh.yeon@samsung.com>
Mon, 17 Jun 2019 07:28:16 +0000 (16:28 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 25 Jun 2019 02:11:18 +0000 (02:11 +0000)
Replace copy operation to move improves performance.

Change-Id: I623059a21e311420942ab860b3b151c95d371562
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/common/step/backup/step_copy_backup.cc

index d32f7d9..531ff33 100644 (file)
@@ -26,38 +26,27 @@ namespace ci = common_installer;
 namespace {
 
 const char kExternalMemoryMountPoint[] = ".mmc";
+const char kSharedResPath[] = "shared/res";
+
+bool CheckFreeSpace(const bf::path& backup_path, const bf::path& shared_path) {
+  int64_t shared_size = ci::GetDirectorySize(shared_path);
+  if (!ci::CheckFreeSpaceAtPath(shared_size, backup_path))
+    return false;
 
-bool RemoveContents(const bf::path& path) {
-  for (bf::directory_iterator iter(path);
-      iter != bf::directory_iterator(); ++iter) {
-    if (!ci::RemoveAll(iter->path()))
-      return false;
-  }
   return true;
 }
 
-bool BackupContents(const bf::path& src, const bf::path& backup_path) {
-  if (!ci::CopyDir(src, backup_path,
-      ci::FS_PRESERVE_OWNERSHIP_AND_PERMISSIONS, false))
+bool CreateSharedRes(const bf::path& src, const bf::path& dst) {
+  bs::error_code error;
+  bf::create_directories(dst / kSharedResPath, error);
+  if (error) {
+    LOG(ERROR) << "Cannot create package directory";
     return false;
-  for (bf::directory_iterator iter(src);
-      iter != bf::directory_iterator(); ++iter) {
-    if (iter->path().filename() == "res") {
-      if (!RemoveContents(iter->path()))
-        return false;
-      continue;
-    }
-
-    if (!ci::RemoveAll(iter->path()))
-      return false;
   }
 
-  return true;
-}
-
-bool CheckFreeSpace(const bf::path& backup_path, const bf::path& shared_path) {
-  int64_t shared_size = ci::GetDirectorySize(shared_path);
-  if (!ci::CheckFreeSpaceAtPath(shared_size, backup_path))
+  if (!ci::CopyOwnershipAndPermissions(src / "shared", dst / "shared") ||
+      !ci::CopyOwnershipAndPermissions(src / kSharedResPath,
+          dst / kSharedResPath))
     return false;
 
   return true;
@@ -165,11 +154,6 @@ bool StepCopyBackup::Backup() {
       }
     }
 
-    if (iter->path() == context_->GetPkgPath() / "shared") {
-      if (!BackupContents(iter->path(), backup_path_ / "shared"))
-        return false;
-      continue;
-    }
     if (bf::is_directory(iter->path())) {
       if (!MoveDir(iter->path(), backup_path_ / iter->path().filename())) {
         LOG(ERROR) << "Fail to backup package directory of: " << iter->path();
@@ -182,6 +166,12 @@ bool StepCopyBackup::Backup() {
       }
     }
   }
+
+  if (bf::exists(backup_path_ / kSharedResPath) &&
+      bf::exists(context_->unpacked_dir_path.get() / kSharedResPath))
+    if (!CreateSharedRes(backup_path_, context_->GetPkgPath()))
+      return false;
+
   recovery::RecoveryFile* recovery_file =
       context_->recovery_info.get().recovery_file.get();
   recovery_file->set_backup_done(true);