Handling external appdata directories 06/74806/4
authorTomasz Iwanek <t.iwanek@samsung.com>
Tue, 14 Jun 2016 08:55:48 +0000 (10:55 +0200)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Thu, 16 Jun 2016 23:42:04 +0000 (16:42 -0700)
Submit together:
 - https://review.tizen.org/gerrit/74805
 - https://review.tizen.org/gerrit/74806

Change-Id: Ib352dcd4bf5990f729bc1157c612db4906e121c3

src/tpk/CMakeLists.txt
src/tpk/external_dirs.cc [new file with mode: 0644]
src/tpk/external_dirs.h [new file with mode: 0644]
src/tpk/step/filesystem/step_create_external_storage_directories.cc [new file with mode: 0644]
src/tpk/step/filesystem/step_create_external_storage_directories.h [new file with mode: 0644]
src/tpk/step/filesystem/step_remove_external_storage_directories.cc [new file with mode: 0644]
src/tpk/step/filesystem/step_remove_external_storage_directories.h [new file with mode: 0644]
src/tpk/step/filesystem/step_update_external_storage_directories.cc [new file with mode: 0644]
src/tpk/step/filesystem/step_update_external_storage_directories.h [new file with mode: 0644]
src/tpk/tpk_installer.cc

index ed540809b6eb9126efa314b95b0d680e81a6cc14..0a8181d85d212b1fc5f60e8c5f53b991a2dc34cc 100644 (file)
@@ -1,17 +1,21 @@
 SET(SRCS
-  tpk_mount_path.cc
+  external_dirs.cc
   step/configuration/step_parse_preload.cc
   step/filesystem/step_check_pkg_directory_path.cc
+  step/filesystem/step_create_external_storage_directories.cc
   step/filesystem/step_create_symbolic_link.cc
   step/filesystem/step_grant_permission.cc
+  step/filesystem/step_remove_external_storage_directories.cc
   step/filesystem/step_tpk_patch_icons.cc
   step/filesystem/step_tpk_prepare_package_directory.cc
   step/filesystem/step_tpk_update_package_directory.cc
+  step/filesystem/step_update_external_storage_directories.cc
   step/pkgmgr/step_convert_xml.cc
   step/pkgmgr/step_manifest_adjustment.cc
   step/rds/step_tpk_rds_modify.cc
   step/security/step_check_tpk_background_category.cc
   tpk_app_query_interface.cc
+  tpk_mount_path.cc
   tpk_installer.cc
 )
 ADD_LIBRARY(${TARGET_LIBNAME_TPK} SHARED ${SRCS})
diff --git a/src/tpk/external_dirs.cc b/src/tpk/external_dirs.cc
new file mode 100644 (file)
index 0000000..e97863b
--- /dev/null
@@ -0,0 +1,65 @@
+// 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 "tpk/external_dirs.h"
+
+#include <common/pkgdir_tool_request.h>
+#include <common/privileges.h>
+#include <common/pkgmgr_registration.h>
+#include <common/shared_dirs.h>
+#include <common/utils/glist_range.h>
+#include <manifest_parser/utils/logging.h>
+
+#include <algorithm>
+
+namespace ci = common_installer;
+
+namespace tpk {
+
+bool HasExternalAppdataPrivilege(manifest_x* manifest) {
+  auto privileges = GListRange<char*>(manifest->privileges);
+  return std::find(privileges.begin(), privileges.end(),
+      std::string(common::privileges::kPrivForExternalAppData))
+          != privileges.end();
+}
+
+bool CreateExternalAppdataDirectories(const std::string& pkgid,
+                                      ci::RequestMode request_mode, uid_t uid) {
+  switch (request_mode) {
+    case ci::RequestMode::GLOBAL: {
+      LOG(DEBUG) << "Creating external directories for all users";
+      ci::RequestCreateExternalDirectories(pkgid);
+      break;
+    }
+    case ci::RequestMode::USER: {
+      LOG(DEBUG) << "Creating external directories for user: " << uid;
+      ci::PerformExternalDirectoryCreationForUser(uid, pkgid);
+      break;
+    }
+  }
+  return true;
+}
+
+bool DeleteExternalAppdataDirectories(const std::string& pkgid,
+                                      ci::RequestMode request_mode, uid_t uid) {
+  switch (request_mode) {
+    case ci::RequestMode::GLOBAL: {
+      LOG(DEBUG) << "Removing external directories for all users";
+      ci::RequestDeleteExternalDirectories(pkgid);
+      break;
+    }
+    case ci::RequestMode::USER: {
+      // if package is globally installed, leave directories
+      if (ci::IsPackageInstalled(pkgid, GLOBAL_USER))
+        return true;
+
+      LOG(DEBUG) << "Removing external directories for user: " << uid;
+      ci::PerformExternalDirectoryDeletionForUser(uid, pkgid);
+      break;
+    }
+  }
+  return true;
+}
+
+}  // namespace tpk
diff --git a/src/tpk/external_dirs.h b/src/tpk/external_dirs.h
new file mode 100644 (file)
index 0000000..470a798
--- /dev/null
@@ -0,0 +1,54 @@
+// 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 TPK_EXTERNAL_DIRS_H_
+#define TPK_EXTERNAL_DIRS_H_
+
+#include <common/request.h>
+#include <pkgmgrinfo_basic.h>
+
+#include <string>
+
+namespace tpk {
+
+/**
+ * @brief HasExternalAppdataPrivilege
+ *        Defines conditions upon which package has privilege to create external
+ *        appdata directories
+ *
+ * @return true if package has privilege to directories
+ */
+bool HasExternalAppdataPrivilege(manifest_x* manifest);
+
+/**
+ * @brief CreateExternalAppdataDirectories
+ *        Creates external appdata directories according to installation
+ *        context's uid and request mode
+ *
+ * @param pkgid package id
+ * @param request_mode request mode - user or global
+ * @param uid user id
+ *
+ * @return true if operation succeeded
+ */
+bool CreateExternalAppdataDirectories(const std::string& pkgid,
+    common_installer::RequestMode request_mode, uid_t uid);
+
+/**
+ * @brief RemoveExternalAppdataDirectories
+ *        Removes external appdata directories according to installation
+ *        context's uid and request mode
+ *
+ * @param pkgid package id
+ * @param request_mode request mode - user or global
+ * @param uid user id
+ *
+ * @return true if operation succeeded
+ */
+bool DeleteExternalAppdataDirectories(const std::string& pkgid,
+    common_installer::RequestMode request_mode, uid_t uid);
+
+}  // namespace tpk
+
+#endif  // TPK_EXTERNAL_DIRS_H_
diff --git a/src/tpk/step/filesystem/step_create_external_storage_directories.cc b/src/tpk/step/filesystem/step_create_external_storage_directories.cc
new file mode 100644 (file)
index 0000000..bbe3d5c
--- /dev/null
@@ -0,0 +1,43 @@
+// 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 "tpk/step/filesystem/step_create_external_storage_directories.h"
+
+#include <common/privileges.h>
+#include <common/shared_dirs.h>
+#include <common/utils/glist_range.h>
+
+#include <string>
+#include <vector>
+
+#include "tpk/external_dirs.h"
+
+namespace ci = common_installer;
+
+namespace tpk {
+namespace filesystem {
+
+ci::Step::Status StepCreateExternalStorageDirectories::process() {
+  if (!HasExternalAppdataPrivilege(context_->manifest_data.get())) {
+    LOG(DEBUG) << "External storage privilege not found, skipping.";
+    return Status::OK;
+  } else {
+    if (!CreateExternalAppdataDirectories(context_->pkgid.get(),
+                                          context_->request_mode.get(),
+                                          context_->uid.get()))
+      return Status::APP_DIR_ERROR;
+  }
+  return Status::OK;
+}
+
+ci::Step::Status StepCreateExternalStorageDirectories::precheck() {
+  if (!context_->manifest_data.get()) {
+    LOG(ERROR) << "Manifest data not set";
+    return Status::INVALID_VALUE;
+  }
+  return Status::OK;
+}
+
+}  // namespace filesystem
+}  // namespace tpk
diff --git a/src/tpk/step/filesystem/step_create_external_storage_directories.h b/src/tpk/step/filesystem/step_create_external_storage_directories.h
new file mode 100644 (file)
index 0000000..9a2f991
--- /dev/null
@@ -0,0 +1,30 @@
+// 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 TPK_STEP_FILESYSTEM_STEP_CREATE_EXTERNAL_STORAGE_DIRECTORIES_H_
+#define TPK_STEP_FILESYSTEM_STEP_CREATE_EXTERNAL_STORAGE_DIRECTORIES_H_
+
+#include <common/installer_context.h>
+#include <common/step/step.h>
+#include <manifest_parser/utils/logging.h>
+
+namespace tpk {
+namespace filesystem {
+
+class StepCreateExternalStorageDirectories : public common_installer::Step {
+ public:
+  using Step::Step;
+
+  Status process() override;
+  Status clean() override { return Status::OK; }
+  Status undo() override  { return Status::OK; }
+  Status precheck() override;
+
+  STEP_NAME(CreateExternalStorageDirectories)
+};
+
+}  // namespace filesystem
+}  // namespace tpk
+
+#endif  // TPK_STEP_FILESYSTEM_STEP_CREATE_EXTERNAL_STORAGE_DIRECTORIES_H_
diff --git a/src/tpk/step/filesystem/step_remove_external_storage_directories.cc b/src/tpk/step/filesystem/step_remove_external_storage_directories.cc
new file mode 100644 (file)
index 0000000..9cfe748
--- /dev/null
@@ -0,0 +1,23 @@
+// 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 "tpk/step/filesystem/step_remove_external_storage_directories.h"
+
+#include "tpk/external_dirs.h"
+
+namespace ci = common_installer;
+
+namespace tpk {
+namespace filesystem {
+
+ci::Step::Status StepRemoveExternalStorageDirectories::process() {
+  if (!DeleteExternalAppdataDirectories(context_->pkgid.get(),
+                                        context_->request_mode.get(),
+                                        context_->uid.get()))
+    return Status::APP_DIR_ERROR;
+  return Status::OK;
+}
+
+}  // namespace filesystem
+}  // namespace tpk
diff --git a/src/tpk/step/filesystem/step_remove_external_storage_directories.h b/src/tpk/step/filesystem/step_remove_external_storage_directories.h
new file mode 100644 (file)
index 0000000..a13dfc2
--- /dev/null
@@ -0,0 +1,30 @@
+// 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 TPK_STEP_FILESYSTEM_STEP_REMOVE_EXTERNAL_STORAGE_DIRECTORIES_H_
+#define TPK_STEP_FILESYSTEM_STEP_REMOVE_EXTERNAL_STORAGE_DIRECTORIES_H_
+
+#include <common/installer_context.h>
+#include <common/step/step.h>
+#include <manifest_parser/utils/logging.h>
+
+namespace tpk {
+namespace filesystem {
+
+class StepRemoveExternalStorageDirectories : public common_installer::Step {
+ public:
+  using Step::Step;
+
+  Status process() override;
+  Status clean() override { return Status::OK; }
+  Status undo() override { return Status::OK; }
+  Status precheck() override { return Status::OK; }
+
+  STEP_NAME(RemoveExternalStorageDirectories)
+};
+
+}  // namespace filesystem
+}  // namespace tpk
+
+#endif  // TPK_STEP_FILESYSTEM_STEP_REMOVE_EXTERNAL_STORAGE_DIRECTORIES_H_
diff --git a/src/tpk/step/filesystem/step_update_external_storage_directories.cc b/src/tpk/step/filesystem/step_update_external_storage_directories.cc
new file mode 100644 (file)
index 0000000..bd0150c
--- /dev/null
@@ -0,0 +1,38 @@
+// 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 "tpk/step/filesystem/step_update_external_storage_directories.h"
+
+#include "tpk/external_dirs.h"
+
+namespace ci = common_installer;
+
+namespace tpk {
+namespace filesystem {
+
+ci::Step::Status StepUpdateExternalStorageDirectories::process() {
+  if (!HasExternalAppdataPrivilege(context_->manifest_data.get())) {
+    if (!DeleteExternalAppdataDirectories(context_->pkgid.get(),
+                                          context_->request_mode.get(),
+                                          context_->uid.get()))
+      return Status::APP_DIR_ERROR;
+  } else {
+    if (!CreateExternalAppdataDirectories(context_->pkgid.get(),
+                                          context_->request_mode.get(),
+                                          context_->uid.get()))
+      return Status::APP_DIR_ERROR;
+  }
+  return Status::OK;
+}
+
+ci::Step::Status StepUpdateExternalStorageDirectories::precheck() {
+  if (!context_->manifest_data.get()) {
+    LOG(ERROR) << "Manifest data not set";
+    return Status::INVALID_VALUE;
+  }
+  return Status::OK;
+}
+
+}  // namespace filesystem
+}  // namespace tpk
diff --git a/src/tpk/step/filesystem/step_update_external_storage_directories.h b/src/tpk/step/filesystem/step_update_external_storage_directories.h
new file mode 100644 (file)
index 0000000..9316d78
--- /dev/null
@@ -0,0 +1,30 @@
+// 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 TPK_STEP_FILESYSTEM_STEP_UPDATE_EXTERNAL_STORAGE_DIRECTORIES_H_
+#define TPK_STEP_FILESYSTEM_STEP_UPDATE_EXTERNAL_STORAGE_DIRECTORIES_H_
+
+#include <common/installer_context.h>
+#include <common/step/step.h>
+#include <manifest_parser/utils/logging.h>
+
+namespace tpk {
+namespace filesystem {
+
+class StepUpdateExternalStorageDirectories : public common_installer::Step {
+ public:
+  using Step::Step;
+
+  Status process() override;
+  Status clean() override { return Status::OK; }
+  Status undo() override  { return Status::OK; }
+  Status precheck() override;
+
+  STEP_NAME(UpdateExternalStorageDirectories)
+};
+
+}  // namespace filesystem
+}  // namespace tpk
+
+#endif  // TPK_STEP_FILESYSTEM_STEP_UPDATE_EXTERNAL_STORAGE_DIRECTORIES_H_
index 24cd4db95a40311e0178b9ebc451141482d4b176..6866a0ffea60e779da2248e47fa12bbfe237a65d 100644 (file)
 #include <common/step/security/step_update_security.h>
 
 #include "tpk/step/configuration/step_parse_preload.h"
+#include "tpk/step/filesystem/step_create_external_storage_directories.h"
 #include "tpk/step/filesystem/step_create_symbolic_link.h"
 #include "tpk/step/filesystem/step_check_pkg_directory_path.h"
 #include "tpk/step/filesystem/step_grant_permission.h"
+#include "tpk/step/filesystem/step_remove_external_storage_directories.h"
 #include "tpk/step/filesystem/step_tpk_patch_icons.h"
 #include "tpk/step/filesystem/step_tpk_prepare_package_directory.h"
 #include "tpk/step/filesystem/step_tpk_update_package_directory.h"
+#include "tpk/step/filesystem/step_update_external_storage_directories.h"
 #include "tpk/step/pkgmgr/step_convert_xml.h"
 #include "tpk/step/pkgmgr/step_manifest_adjustment.h"
 #include "tpk/step/rds/step_tpk_rds_modify.h"
@@ -147,6 +150,7 @@ void TpkInstaller::InstallSteps() {
   AddStep<ci::filesystem::StepCopy>();
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::filesystem::StepCreateStorageDirectories>();
+  AddStep<tpk::filesystem::StepCreateExternalStorageDirectories>();
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
   AddStep<ci::filesystem::StepCreateIcons>();
@@ -184,6 +188,7 @@ void TpkInstaller::UpdateSteps() {
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::pkgmgr::StepUpdateTep>();
   AddStep<ci::filesystem::StepCopyStorageDirectories>();
+  AddStep<tpk::filesystem::StepUpdateExternalStorageDirectories>();
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
   AddStep<ci::filesystem::StepCreateIcons>();
@@ -207,6 +212,7 @@ void TpkInstaller::UninstallSteps() {
   AddStep<ci::pkgmgr::StepKillApps>();
   AddStep<ci::filesystem::StepAcquireExternalStorage>();
   AddStep<ci::filesystem::StepRemovePerUserStorageDirectories>();
+  AddStep<tpk::filesystem::StepRemoveExternalStorageDirectories>();
   AddStep<ci::pkgmgr::StepUnregisterApplication>();
   AddStep<ci::security::StepRollbackDeinstallationSecurity>();
   AddStep<ci::filesystem::StepRemoveFiles>();
@@ -243,6 +249,7 @@ void TpkInstaller::ReinstallSteps() {
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
   AddStep<ci::filesystem::StepCreateIcons>();
+  AddStep<tpk::filesystem::StepUpdateExternalStorageDirectories>();
   AddStep<ci::security::StepUpdateSecurity>();
   AddStep<tpk::pkgmgr::StepConvertXml>();
   AddStep<tpk::filesystem::StepTpkGrantPermission>();
@@ -273,6 +280,7 @@ void TpkInstaller::DeltaSteps() {
   AddStep<ci::filesystem::StepAcquireExternalStorage>();
   AddStep<ci::backup::StepCopyBackup>();
   AddStep<ci::filesystem::StepCopyStorageDirectories>();
+  AddStep<tpk::filesystem::StepUpdateExternalStorageDirectories>();
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
   AddStep<ci::filesystem::StepCreateIcons>();
@@ -327,6 +335,7 @@ void TpkInstaller::MountInstallSteps() {
   AddStep<tpk::filesystem::StepTpkPreparePackageDirectory>();
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::filesystem::StepCreateStorageDirectories>();
+  AddStep<tpk::filesystem::StepCreateExternalStorageDirectories>();
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
   AddStep<ci::filesystem::StepCreateIcons>();
@@ -366,6 +375,7 @@ void TpkInstaller::MountUpdateSteps() {
   AddStep<ci::pkgmgr::StepUpdateTep>();
   AddStep<tpk::filesystem::StepCreateSymbolicLink>();
   AddStep<tpk::filesystem::StepTpkPatchIcons>();
+  AddStep<tpk::filesystem::StepUpdateExternalStorageDirectories>();
   AddStep<ci::filesystem::StepCreateIcons>();
   AddStep<ci::security::StepUpdateSecurity>();
   AddStep<tpk::pkgmgr::StepConvertXml>();