Step recovery change owner 95/87795/5
authorBartlomiej Kunikowski <b.kunikowski@partner.samsung.com>
Fri, 9 Sep 2016 08:38:27 +0000 (10:38 +0200)
committerTomasz Iwanek <t.iwanek@samsung.com>
Mon, 12 Sep 2016 10:18:54 +0000 (03:18 -0700)
Needed to correct setting ownership of files in recovery mode

Change-Id: I11a8034d1089e2671db5928123121b2895e8ffc5

src/common/CMakeLists.txt
src/common/paths.cc
src/common/paths.h
src/common/step/filesystem/step_change_owner.cc
src/common/step/filesystem/step_recover_change_owner.cc [new file with mode: 0644]
src/common/step/filesystem/step_recover_change_owner.h [new file with mode: 0644]

index 774697d..59e4e47 100644 (file)
@@ -52,6 +52,7 @@ SET(SRCS
   step/filesystem/step_disable_external_mount.cc
   step/filesystem/step_enable_external_mount.cc
   step/filesystem/step_move_installed_storage.cc
+  step/filesystem/step_recover_change_owner.cc
   step/filesystem/step_recover_files.cc
   step/filesystem/step_recover_icons.cc
   step/filesystem/step_recover_manifest.cc
index 4c55b89..506ae5c 100644 (file)
@@ -10,6 +10,8 @@
 #include <tzplatform_config.h>
 #include <storage-internal.h>
 
+#include <vector>
+
 #include "common/utils/user_util.h"
 
 namespace bf = boost::filesystem;
@@ -107,4 +109,25 @@ boost::filesystem::path GetInternalTepPath(
   return pkg_path / "tep";
 }
 
+boost::filesystem::path GetIconPath(const bf::path& base_path,
+                      const std::string& pkgid,
+                      const bf::path& icon_filename,
+                      const bf::path& root_path) {
+  std::vector<bf::path> paths;
+  bf::path system_path = base_path / icon_filename;
+  bf::path small_system_path = base_path / "default" / "small" / icon_filename;
+  bf::path res_path = root_path / pkgid / "res" / "icons" / icon_filename;
+
+  paths.push_back(system_path);
+  paths.push_back(small_system_path);
+  paths.push_back(res_path);
+
+  for (auto& path : paths) {
+    if (bf::exists(path))
+      return path;
+  }
+
+  return {};
+}
+
 }  // namespace common_installer
index 4f6edab..66eb6bd 100644 (file)
@@ -119,6 +119,21 @@ boost::filesystem::path GetExternalTepPath(RequestMode request_mode, uid_t uid);
 boost::filesystem::path GetInternalTepPath(
     const boost::filesystem::path& pkg_path);
 
+/**
+ * @brief GetIconPath
+ *        Returns internal location for preinstalled global app icon
+ * @param base_path path of icon in application
+ * @param pkgid package id
+ * @param icon_filename filename of icon
+ * @param root_path root path of application
+ *
+ * @return full icon path
+ */
+boost::filesystem::path GetIconPath(const boost::filesystem::path& base_path,
+                      const std::string& pkgid,
+                      const boost::filesystem::path& icon_filename,
+                      const boost::filesystem::path& root_path);
+
 }  // namespace common_installer
 
 #endif  // COMMON_PATHS_H_
index 5cfa7fd..df53592 100644 (file)
 #include <string>
 #include <vector>
 
+#include "common/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 bs = boost::system;
 namespace ci = common_installer;
 
-namespace {
-
-bool SetOwner(const bf::path& subpath, uid_t uid, gid_t gid) {
-  int fd = open(subpath.c_str(), O_RDONLY);
-  if (fd < 0) {
-    LOG(ERROR) << "Can't open directory : " << subpath;
-    return false;
-  }
-
-  int ret = fchown(fd, uid, gid);
-  close(fd);
-  if (ret != 0) {
-    LOG(ERROR) << "Failed to change owner of: " << subpath;
-    return false;
-  }
-  return true;
-}
-
-bf::path FindIconPath(const bf::path& base_path, const std::string& pkgid,
-                      const bf::path& icon_filename,
-                      const bf::path& root_path) {
-  std::vector<bf::path> paths;
-  bf::path system_path = base_path / icon_filename;
-  bf::path small_system_path = base_path / "default" / "small" / icon_filename;
-  bf::path res_path = root_path / pkgid / "res" / "icons" / icon_filename;
-
-  paths.push_back(system_path);
-  paths.push_back(small_system_path);
-  paths.push_back(res_path);
-
-  for (auto& path : paths) {
-    if (bf::exists(path))
-      return path;
-  }
-
-  return {};
-}
-
-}  // namespace
-
 namespace common_installer {
 namespace filesystem {
 
@@ -95,16 +55,8 @@ Step::Status StepChangeOwner::process() {
     return Status::ERROR;
 
   // Change owner of files at root path
-  bf::path start_path = context_->pkg_path.get();
-  if (!SetOwner(start_path, uid, *gid))
+  if (!ci::SetOwnershipAll(context_->pkg_path.get(), uid, *gid))
     return Status::ERROR;
-  for (bf::recursive_directory_iterator iter(start_path);
-      iter != bf::recursive_directory_iterator(); ++iter) {
-        if (bf::is_symlink(symlink_status(iter->path())))
-          continue;
-        if (!SetOwner(iter->path(), uid, *gid))
-          return Status::ERROR;
-  }
 
   // For icon files
   const char *iconpath = getIconPath(uid, context_->is_preload_request.get());
@@ -118,11 +70,11 @@ Step::Status StepChangeOwner::process() {
       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 = FindIconPath(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 (!SetOwner(found_icon_path, uid, *gid))
+        if (!ci::SetOwnership(found_icon_path, uid, *gid))
           return Status::ERROR;
       }
     }
@@ -130,7 +82,7 @@ Step::Status StepChangeOwner::process() {
 
   // Manifest files for global apps
   if (!context_->xml_path.get().empty()) {
-    if (!SetOwner(context_->xml_path.get(), uid, *gid))
+    if (!ci::SetOwnership(context_->xml_path.get(), uid, *gid))
       return Status::ERROR;
   }
 
diff --git a/src/common/step/filesystem/step_recover_change_owner.cc b/src/common/step/filesystem/step_recover_change_owner.cc
new file mode 100644 (file)
index 0000000..8cf8c04
--- /dev/null
@@ -0,0 +1,72 @@
+// 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 <unistd.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <pkgmgr-info.h>
+#include <cassert>
+
+#include <cstring>
+#include <string>
+#include <vector>
+
+#include "common/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 common_installer {
+namespace filesystem {
+
+Step::Status StepRecoverChangeOwner::RecoveryUpdate() {
+  uid_t uid = context_->uid.get();
+  boost::optional<gid_t> gid = ci::GetGidByUid(uid);
+  if (!gid)
+    return Step::Status::ERROR;
+
+  // Change owner of files at root path
+  if (!ci::SetOwnershipAll(context_->pkg_path.get(), uid, *gid))
+    return Step::Status::ERROR;
+
+  // For icon files
+  const char *iconpath = getIconPath(uid, context_->is_preload_request.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
new file mode 100644 (file)
index 0000000..edca887
--- /dev/null
@@ -0,0 +1,36 @@
+// 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;
+
+  STEP_NAME(RecoverChangeOwner)
+};
+
+}  // namespace filesystem
+}  // namespace common_installer
+
+#endif  // COMMON_STEP_FILESYSTEM_STEP_RECOVER_CHANGE_OWNER_H_