Skip recover change owner 96/286396/3
authorilho kim <ilho159.kim@samsung.com>
Mon, 2 Jan 2023 02:37:00 +0000 (11:37 +0900)
committerilho kim <ilho159.kim@samsung.com>
Tue, 10 Jan 2023 07:44:27 +0000 (16:44 +0900)
If the ownership of files is preserved when backup files
there is no need to set the ownership at package recovery
when backup files, preserve ownership using move operation

Change-Id: I63f5c4695f536050fba4db3aa0bde98a6e7d5377
Signed-off-by: ilho kim <ilho159.kim@samsung.com>
src/common/installer/app_installer.cc
src/common/step/filesystem/step_recover_change_owner.cc [deleted file]
src/common/step/filesystem/step_recover_change_owner.h [deleted file]
src/common/step/filesystem/step_recover_files.cc
src/common/step/mount/step_mount_update.cc
src/common/step/mount/step_mount_update.h

index 0cad189..0162b2e 100644 (file)
@@ -43,7 +43,6 @@
 #include "common/step/filesystem/step_migrate_legacy_external_image.h"
 #include "common/step/filesystem/step_move_installed_storage.h"
 #include "common/step/filesystem/step_optional_acquire_external_storage.h"
-#include "common/step/filesystem/step_recover_change_owner.h"
 #include "common/step/filesystem/step_recover_external_storage.h"
 #include "common/step/filesystem/step_recover_files.h"
 #include "common/step/filesystem/step_recover_globalapp_symlinks.h"
@@ -516,7 +515,6 @@ void AppInstaller::RecoverySteps() {
   AddStep<ci::filesystem::StepRecoverFiles>();
   AddStep<ci::pkgmgr::StepRecoverPrivSharedres>();
   AddStep<ci::mount::StepMountRecover>();
-  AddStep<ci::filesystem::StepRecoverChangeOwner>();
   AddStep<ci::pkgmgr::StepRecoverApplication>();
   AddStep<ci::security::StepRecoverTrustAnchor>();
   AddStep<ci::security::StepRecoverSecurity>();
diff --git a/src/common/step/filesystem/step_recover_change_owner.cc b/src/common/step/filesystem/step_recover_change_owner.cc
deleted file mode 100644 (file)
index e486db6..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-// Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by an apache-2.0 license that can be
-// found in the LICENSE file.
-
-#include "common/step/filesystem/step_recover_change_owner.h"
-
-#include <boost/filesystem/path.hpp>
-#include <boost/optional/optional.hpp>
-
-#include <unistd.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <pkgmgr-info.h>
-#include <tzplatform_config.h>
-
-#include <cassert>
-#include <cstring>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "common/utils/paths.h"
-#include "common/shared_dirs.h"
-#include "common/utils/file_util.h"
-#include "common/utils/glist_range.h"
-#include "common/utils/user_util.h"
-
-namespace bf = boost::filesystem;
-namespace ci = common_installer;
-
-namespace {
-
-const char kSkelAppDir[] = "skel/apps_rw";
-const char kData[] = "data";
-const char kShared[] = ".shared";
-const char kSharedTmp[] = ".shared_tmp";
-const char kSystemShareGroupName[] = "system_share";
-const char kSubssesionDir[] = "subsession";
-const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
-const gid_t kGlobalUserGid = tzplatform_getgid(TZ_SYS_GLOBALAPP_USER);
-
-bool SetSharedDirOwnershipAndPermissions(const bf::path& apps_rw,
-    const std::string& pkgid, uid_t uid, gid_t gid) {
-  bf::perms perms = (bf::all_all | bf::set_uid_on_exe) ^ bf::others_write;
-  bf::path shared = apps_rw / kShared / pkgid / kData;
-  if (!bf::exists(shared))
-    return true;
-  boost::optional<gid_t> system_share =
-      ci::GetGidByGroupName(kSystemShareGroupName);
-  if (!system_share)
-    return false;
-
-  if (!ci::SetDirOwnershipAndPermissions(shared, perms, uid, *system_share))
-    return false;
-
-  perms = bf::all_all ^ bf::group_write ^ bf::others_write;
-  bf::path shared_tmp = apps_rw / kSharedTmp / pkgid;
-  if (!ci::SetDirOwnershipAndPermissions(shared_tmp, perms, uid, gid))
-    return false;
-
-  return true;
-}
-
-}  // namespace
-
-namespace common_installer {
-namespace filesystem {
-
-Step::Status StepRecoverChangeOwner::RecoveryUpdate() {
-  uid_t uid = context_->uid.get();
-  // Change owner of files at root path
-  if (!ci::SetPackageDirectoryOwnerAndPermissions(context_->GetPkgPath(),
-      uid))
-    return Step::Status::ERROR;
-
-  boost::optional<gid_t> gid = ci::GetGidByUid(uid);
-  if (!gid)
-    return Step::Status::ERROR;
-
-  if (context_->request_mode.get() == RequestMode::GLOBAL) {
-    bf::path skel_apps_rw = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
-        bf::path(kSkelAppDir);
-    if (!SetSharedDirOwnershipAndPermissions(skel_apps_rw,
-            context_->pkgid.get(), kGlobalUserUid, kGlobalUserGid))
-      return Step::Status::ERROR;
-
-    ci::UserList list = ci::GetUserList();
-    for (auto l : list) {
-      uid_t lu = std::get<0>(l);
-      gid_t lg = std::get<1>(l);
-      bf::path owner_apps_rw = ci::GetRootAppPath(false, lu);
-      std::vector<bf::path> apps_rw_paths;
-      apps_rw_paths.push_back(std::move(owner_apps_rw));
-      for (auto& lw_user : GetLightUserList(uid))
-        apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
-                                "apps_rw");
-
-      for (auto& apps_rw : apps_rw_paths) {
-        bf::path pkg_root = apps_rw / context_->pkgid.get();
-        if (!ci::SetPackageDirectoryOwnerAndPermissions(pkg_root, lu))
-          return Step::Status::ERROR;
-
-        if (!SetSharedDirOwnershipAndPermissions(apps_rw,
-                context_->pkgid.get(), lu, lg))
-          return Step::Status::ERROR;
-      }
-    }
-  } else {
-    ci::UserList list = ci::GetUserList();
-    for (auto& l : list) {
-      uid_t lu = std::get<0>(l);
-      if (lu != uid)
-        continue;
-
-      bf::path owner_apps_rw = ci::GetRootAppPath(false, uid);
-      std::vector<bf::path> apps_rw_paths;
-      apps_rw_paths.push_back(std::move(owner_apps_rw));
-      for (auto& lw_user : GetLightUserList(uid))
-        apps_rw_paths.push_back(std::get<2>(l) / kSubssesionDir / lw_user /
-                                "apps_rw");
-
-      for (auto& apps_rw : apps_rw_paths) {
-        if (!SetSharedDirOwnershipAndPermissions(apps_rw,
-                context_->pkgid.get(), context_->uid.get(), *gid))
-          return Step::Status::ERROR;
-      }
-    }
-  }
-
-  return ChangeOwnershipIconsAndManifest(gid, uid);
-}
-
-Step::Status StepRecoverChangeOwner::ChangeOwnershipIconsAndManifest(
-    const boost::optional<gid_t> gid, const uid_t uid) {
-  if (!gid)
-    return Step::Status::ERROR;
-
-  // For icon files
-  const char *iconpath = getIconPath(uid, context_->is_readonly_package.get());
-  if (iconpath) {
-    for (application_x* app :
-        GListRange<application_x*>(
-        context_->manifest_data.get()->application)) {
-      if (!app->icon)
-        continue;
-
-      icon_x* icon = reinterpret_cast<icon_x*>(app->icon->data);
-      bf::path icon_path = icon->text;
-      //  bf::path base_path = iconpath;
-      bf::path found_icon_path = GetIconPath(iconpath,
-          context_->pkgid.get(), icon_path.filename(),
-          context_->root_application_path.get());
-      if (!found_icon_path.empty()) {
-        if (!ci::SetOwnership(found_icon_path, uid, *gid))
-          return Step::Status::ERROR;
-      }
-    }
-  }
-
-  // Manifest files for global apps
-  if (!context_->xml_path.get().empty()) {
-    if (!ci::SetOwnership(context_->xml_path.get(), uid, *gid))
-      return Step::Status::ERROR;
-  }
-
-  return Step::Status::OK;
-}
-
-}  // namespace filesystem
-}  // namespace common_installer
diff --git a/src/common/step/filesystem/step_recover_change_owner.h b/src/common/step/filesystem/step_recover_change_owner.h
deleted file mode 100644 (file)
index d97ffe7..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by an apache-2.0 license that can be
-// found in the LICENSE file.
-
-#ifndef COMMON_STEP_FILESYSTEM_STEP_RECOVER_CHANGE_OWNER_H_
-#define COMMON_STEP_FILESYSTEM_STEP_RECOVER_CHANGE_OWNER_H_
-
-#include <manifest_parser/utils/logging.h>
-
-#include "common/installer_context.h"
-#include "common/step/recovery/step_recovery.h"
-
-namespace common_installer {
-namespace filesystem {
-
-/**
- * @brief step responsible for changing ownership.
- *
- * Part of Recovery Mode. Is responsible for changing ownership of package
- * directories during package recovery.
- * Used by WGT and TPK.
- */
-class StepRecoverChangeOwner : public recovery::StepRecovery {
- public:
-  using StepRecovery::StepRecovery;
-
-  Status RecoveryNew() override { return Status::OK; };
-  Status RecoveryUpdate() override;
-  Status RecoveryReadonlyUpdateInstall() override { return Status::OK; };
-
- private:
-  Step::Status ChangeOwnershipIconsAndManifest(const boost::optional<gid_t> gid,
-      const uid_t uid);
-
-  STEP_NAME(RecoverChangeOwner)
-};
-
-}  // namespace filesystem
-}  // namespace common_installer
-
-#endif  // COMMON_STEP_FILESYSTEM_STEP_RECOVER_CHANGE_OWNER_H_
index 8c22cba..6eb28cb 100644 (file)
@@ -112,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";
index b21d4dd..fcba92d 100644 (file)
@@ -24,6 +24,16 @@ namespace common_installer {
 namespace mount {
 
 Step::Status StepMountUpdate::process() {
+  bs::error_code error;
+
+  if (!bf::exists(backup_path_)) {
+    bf::create_directories(backup_path_, error);
+    if (error) {
+      LOG(ERROR) << "Failed to create backup directory: " << backup_path_;
+      return Status::APP_DIR_ERROR;
+    }
+  }
+
   auto zip_unpack = CreateZipInterface(context_->unpacked_dir_path.get());
   if (!zip_unpack->UnmountZip()) {
     LOG(ERROR) << "Failed to unmount zip package from temporary path";
@@ -74,6 +84,9 @@ Step::Status StepMountUpdate::clean() {
           context_->GetPkgPath(), context_->pkgid.get()));
   Remove(backup_zip_location);
 
+  if (bf::exists(backup_path_))
+    RemoveAll(backup_path_);
+
   return Status::OK;
 }
 
@@ -103,6 +116,10 @@ Step::Status StepMountUpdate::undo() {
       return Status::APP_DIR_ERROR;
     }
   }
+
+  if (bf::exists(backup_path_))
+    RemoveAll(backup_path_);
+
   return Status::OK;
 }
 
@@ -134,6 +151,8 @@ Step::Status StepMountUpdate::precheck() {
     return Step::Status::PACKAGE_NOT_FOUND;
   }
 
+  backup_path_ = GetBackupPathForPackagePath(context_->GetPkgPath());
+
   return Step::Status::OK;
 }
 
index 26d91c7..99ed4fe 100644 (file)
@@ -5,6 +5,8 @@
 #ifndef COMMON_STEP_MOUNT_STEP_MOUNT_UPDATE_H_
 #define COMMON_STEP_MOUNT_STEP_MOUNT_UPDATE_H_
 
+#include <boost/filesystem/path.hpp>
+
 #include <manifest_parser/utils/logging.h>
 
 #include "common/installer_context.h"
@@ -42,6 +44,9 @@ class StepMountUpdate : public MountBase, public Step {
       const boost::filesystem::path& mount_path) override;
   Status UmountPackagePath();
 
+ private:
+  boost::filesystem::path backup_path_;
+
   STEP_NAME(MountUpdate)
 };