Skip recover change owner
[platform/core/appfw/app-installers.git] / src / common / step / filesystem / step_recover_files.cc
index 5f2f6ca..6eb28cb 100644 (file)
 namespace bf = boost::filesystem;
 namespace bs = boost::system;
 
+namespace {
+
+const char kExternalMemoryMountPoint[] = ".mmc";
+
+bool Move(const boost::filesystem::path& from,
+    const boost::filesystem::path& to,
+    common_installer::FSFlag flag = common_installer::FSFlag::FS_NONE) {
+  if (bf::is_directory(from)) {
+    if (!common_installer::MoveDir(from, to / from.filename(), flag)) {
+      LOG(ERROR) << "Failed to move directory: " << from;
+      return false;
+    }
+  } else {
+    if (!common_installer::MoveFile(from, to / from.filename())) {
+      LOG(ERROR) << "Fail to move file: " << from;
+      return false;
+    }
+  }
+
+  return true;
+}
+
+}  // namespace
+
 namespace common_installer {
 namespace filesystem {
 
 Step::Status StepRecoverFiles::RecoveryNew() {
-  if (!RemoveAll(context_->GetPkgPath()))
+  if (!ClearPath(context_->GetPkgPath()))
     return Status::RECOVERY_ERROR;
 
   LOG(INFO) << "Package files recovery done";
@@ -35,15 +59,29 @@ Step::Status StepRecoverFiles::RecoveryUpdate() {
   if (bf::exists(backup_path)) {
     // Remove pkgdir only if backup original contents is completely done.
     if (backup_done) {
-      if (!RemoveAll(context_->GetPkgPath())) {
-        LOG(ERROR) << "Cannot restore widget files to its correct location";
-        return Status::RECOVERY_ERROR;
+      for (bf::directory_iterator iter(context_->GetPkgPath());
+           iter != bf::directory_iterator(); ++iter) {
+        if (iter->path().filename() == kExternalMemoryMountPoint)
+          continue;
+
+        if (!ClearPath(iter->path())) {
+          LOG(ERROR) << "Cannot restore widget files to its correct location";
+          return Status::RECOVERY_ERROR;
+        }
       }
       // it may fail during recovery.
       recovery_file->set_backup_done(false);
       recovery_file->WriteAndCommitFileContent();
     }
-    (void) MoveDir(backup_path, context_->GetPkgPath(), FS_MERGE_OVERWRITE);
+
+    // create copy of old package content skipping the external memory mount point
+    for (bf::directory_iterator iter(backup_path);
+         iter != bf::directory_iterator(); ++iter) {
+      if (!Move(iter->path(), context_->GetPkgPath()))
+        return Status::RECOVERY_ERROR;
+    }
+
+    ClearPath(backup_path);
   }
   LOG(INFO) << "Package files recovery done";
   return Status::OK;
@@ -53,7 +91,7 @@ Step::Status StepRecoverFiles::RecoveryMountNew() {
   bf::path zip_location = GetZipPackageLocation(
         context_->GetPkgPath(), context_->pkgid.get());
   Remove(zip_location);
-  RemoveAll(context_->GetPkgPath());
+  ClearPath(context_->GetPkgPath());
   LOG(INFO) << "Package files recovery done";
   return Status::OK;
 }
@@ -74,12 +112,16 @@ Step::Status StepRecoverFiles::RecoveryMountUpdate() {
   // still unpacked due to necessity.
   bf::path backup_path = GetBackupPathForPackagePath(context_->GetPkgPath());
   if (bf::exists(backup_path)) {
-    if (!MoveDir(backup_path, context_->GetPkgPath(),
-                 FS_MERGE_OVERWRITE | FS_COMMIT_COPY_FILE)) {
-      LOG(ERROR) << "Failed to recovery backup file "
-                 << "in recovery of mount update";
-      return Status::APP_DIR_ERROR;
+    for (bf::directory_iterator iter(backup_path);
+         iter != bf::directory_iterator(); ++iter) {
+      ClearPath(context_->GetPkgPath() / iter->path().filename());
+      if (!Move(iter->path(), context_->GetPkgPath())) {
+        LOG(ERROR) << "Failed to recovery backup file(" << iter->path()
+            << ") in recovery of mount update";
+        return Status::RECOVERY_ERROR;
+      }
     }
+    ClearPath(backup_path);
   }
 
   LOG(INFO) << "Package files recovery done";
@@ -91,7 +133,7 @@ Step::Status StepRecoverFiles::RecoveryReadonlyUpdateInstall() {
   bf::path pkg_path =
       bf::path(GetRootAppPath(false, context_->uid.get())) /
       context_->pkgid.get();
-  if (!RemoveAll(pkg_path))
+  if (!ClearPath(pkg_path))
     return Status::RECOVERY_ERROR;
 
   LOG(INFO) << "Package files recovery done";
@@ -108,7 +150,7 @@ Step::Status StepRecoverFiles::Cleanup() {
   if (!bf::exists(backup_path))
     return Status::OK;
 
-  if (!RemoveAll(backup_path)) {
+  if (!ClearPath(backup_path)) {
     LOG(ERROR) << "Failed to remove backup path";
     return Status::RECOVERY_ERROR;
   }
@@ -116,6 +158,10 @@ Step::Status StepRecoverFiles::Cleanup() {
   return Status::OK;
 }
 
+bool StepRecoverFiles::ClearPath(const bf::path& path) {
+  return RemoveAll(path);
+}
+
 }  // namespace filesystem
 }  // namespace common_installer