Refactor creating shared/data directory 25/213425/3
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 5 Sep 2019 07:26:17 +0000 (16:26 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Thu, 5 Sep 2019 08:11:57 +0000 (17:11 +0900)
Change-Id: I2b849df574fab2c17fb44c482abbd24b278f5f0a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/shared_dirs.cc
src/common/shared_dirs.h
src/common/step/filesystem/step_create_storage_directories.cc
src/common/step/filesystem/step_create_storage_directories.h
src/common/step/filesystem/step_update_storage_directories.cc

index 8989802..ff98850 100644 (file)
@@ -271,7 +271,6 @@ bool DeleteSymlinkFiles(const bf::path& src_dir, const bf::path& dst_dir) {
 }
 
 bool CreateStorageDirectories(const boost::filesystem::path& path,
-                              const std::string& api_version,
                               bool trusted, bool shareddata,
                               const std::vector<const char*> additional_dirs) {
   if (!bf::exists(path)) {
@@ -284,12 +283,11 @@ bool CreateStorageDirectories(const boost::filesystem::path& path,
     }
   }
 
-  utils::VersionNumber api_ver(api_version);
   std::vector<const char*> dirs(kEntries);
   dirs.insert(dirs.end(), additional_dirs.begin(), additional_dirs.end());
   if (trusted)
     dirs.push_back(kSharedTrustedDir);
-  if (api_ver < ver30 || shareddata) {
+  if (shareddata) {
     dirs.push_back(kSharedDataDir);
   } else {
     bf::path shared_data_path = path / kSharedDataDir;
@@ -443,9 +441,8 @@ bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid) {
 
 bool CreateStorageDirectories(const boost::filesystem::path& path,
                               const std::string& pkgid, uid_t uid,
-                              const std::string& api_version,
                               bool trusted, bool shareddata) {
-  if (!::CreateStorageDirectories(path, api_version, trusted, shareddata,
+  if (!::CreateStorageDirectories(path, trusted, shareddata,
                                   std::vector<const char*>())) {
     LOG(ERROR) << "Failed to create storage directory for path: " << path;
     return false;
@@ -477,14 +474,13 @@ bool DeleteStorageDirectories(const boost::filesystem::path& path) {
 }
 
 bool CreateSkelDirectories(const std::string& pkgid,
-                           const std::string& api_version,
                            bool trusted, bool shareddata, bool is_readonly,
                            const std::vector<const char*> additional_dirs) {
   bf::path path = bf::path(tzplatform_getenv(TZ_SYS_ETC)) /
                   bf::path(kSkelAppDir) / pkgid;
   LOG(DEBUG) << "Creating directories in: " << path;
 
-  if (!::CreateStorageDirectories(path, api_version, trusted, shareddata,
+  if (!::CreateStorageDirectories(path, trusted, shareddata,
                                   additional_dirs)) {
     LOG(ERROR) << "Failed to create storage directory for path: " << path;
     return false;
@@ -780,4 +776,12 @@ bool SetPackageDirectoryOwnerAndPermissions(const bf::path& path, uid_t uid) {
   return true;
 }
 
+bool ShouldSupportLegacySharedDataDir(const std::string& api_version) {
+  utils::VersionNumber api_ver(api_version);
+  if (api_ver < ver30)
+    return true;
+  else
+    return false;
+}
+
 }  // namespace common_installer
index 35ea19a..e1f0b3c 100644 (file)
@@ -69,7 +69,6 @@ bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid);
  * \param path base path, where storage directories will be created in
  * \param pkgid package id
  * \param uid user id
- * \param api_version package api version
  * \param trusted signed package flag
  * \param shareddata shared data privilege flag
  *
@@ -77,7 +76,6 @@ bool PerformExternalDirectoryDeletionForAllUsers(const std::string& pkgid);
  */
 bool CreateStorageDirectories(const boost::filesystem::path& path,
                               const std::string& pkgid, uid_t uid,
-                              const std::string& api_version,
                               bool trusted, bool shareddata);
 
 /**
@@ -93,7 +91,6 @@ bool DeleteStorageDirectories(const boost::filesystem::path& path);
  * \brief Create skeleton directories for package
  *
  * \param pkgid package id
- * \param api_version package api version
  * \param trusted signed package flag
  * \param shareddata shared data privilege flag
  * \param is_readonly readonly flag
@@ -103,7 +100,6 @@ bool DeleteStorageDirectories(const boost::filesystem::path& path);
  *
  */
 bool CreateSkelDirectories(const std::string& pkgid,
-                           const std::string& api_version,
                            bool trusted, bool shareddata, bool is_readonly,
                            const std::vector<const char*>
                            additional_dirs = std::vector<const char*>());
@@ -231,6 +227,15 @@ bool DeleteGlobalAppSymlinksForUser(const std::string& pkgid, uid_t uid);
 bool SetPackageDirectoryOwnerAndPermissions(const boost::filesystem::path& path,
                                             uid_t uid);
 
+/**
+ * \brief Return true when installer should support legacy shared data directory.
+ *
+ * \param api_version package api version
+ *
+ * \return true when should support legacy shared data directory, false otherwise
+ */
+bool ShouldSupportLegacySharedDataDir(const std::string& api_version);
+
 }  // namespace common_installer
 
 #endif  // COMMON_SHARED_DIRS_H_
index 546175f..de305e5 100644 (file)
@@ -4,7 +4,6 @@
 
 #include "common/step/filesystem/step_create_storage_directories.h"
 
-#include <manifest_parser/utils/version_number.h>
 #include <pkgmgrinfo_basic.h>
 
 #include <boost/filesystem/operations.hpp>
@@ -26,7 +25,7 @@ namespace common_installer {
 namespace filesystem {
 
 bool StepCreateStorageDirectories::CreatePerUserStorageDirs(
-    manifest_x *manifest, bool trusted, bool shareddata) {
+    bool trusted, bool shareddata) {
   LOG(INFO) << "Creating per-user directories for package: "
             << context_->pkgid.get();
 
@@ -42,7 +41,7 @@ bool StepCreateStorageDirectories::CreatePerUserStorageDirs(
 
   bool is_readonly = context_->is_readonly_package.get();
   if (!CreateSkelDirectories(context_->pkgid.get(),
-      manifest->api_version, trusted, shareddata, is_readonly,
+      trusted, shareddata, is_readonly,
       additional_shared_dirs_)) {
     LOG(ERROR) << "Failed to create skel dirs";
     return false;
@@ -57,10 +56,9 @@ bool StepCreateStorageDirectories::CreatePerUserStorageDirs(
 }
 
 bool StepCreateStorageDirectories::CreateStorageDirs(
-    manifest_x* manifest, bool trusted, bool shareddata) {
+    bool trusted, bool shareddata) {
   if (!CreateStorageDirectories(context_->GetPkgPath(),
                                 context_->pkgid.get(), context_->uid.get(),
-                                manifest->api_version,
                                 trusted, shareddata)) {
     LOG(ERROR) << "Failed to create storage directories";
     return false;
@@ -75,7 +73,7 @@ Step::Status StepCreateStorageDirectories::process() {
     trusted = true;
 
   manifest_x* manifest = context_->manifest_data.get();
-  bool shareddata = false;
+  bool shareddata = ShouldSupportLegacySharedDataDir(manifest->api_version);
   for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
     if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
       shareddata = true;
@@ -84,10 +82,10 @@ Step::Status StepCreateStorageDirectories::process() {
   }
 
   if (context_->request_mode.get() == RequestMode::GLOBAL) {
-    if (!CreatePerUserStorageDirs(manifest, trusted, shareddata))
+    if (!CreatePerUserStorageDirs(trusted, shareddata))
       return Status::APP_DIR_ERROR;
   } else {
-    if (!CreateStorageDirs(manifest, trusted, shareddata))
+    if (!CreateStorageDirs(trusted, shareddata))
       return Status::APP_DIR_ERROR;
   }
   return Status::OK;
index 59a2b8d..cab1310 100644 (file)
@@ -47,9 +47,8 @@ class StepCreateStorageDirectories : public common_installer::Step {
   Status precheck() override { return Status::OK; }
 
  private:
-  bool CreatePerUserStorageDirs(manifest_x* manifest, bool trusted,
-      bool shareddata);
-  bool CreateStorageDirs(manifest_x* manifest, bool trusted, bool shareddata);
+  bool CreatePerUserStorageDirs(bool trusted, bool shareddata);
+  bool CreateStorageDirs(bool trusted, bool shareddata);
 
   std::vector<const char*> additional_shared_dirs_;
 
index 29ba789..3ebc00a 100644 (file)
@@ -4,8 +4,6 @@
 
 #include "common/step/filesystem/step_update_storage_directories.h"
 
-#include <manifest_parser/utils/version_number.h>
-
 #include <boost/filesystem/operations.hpp>
 #include <boost/filesystem/path.hpp>
 #include <boost/system/error_code.hpp>
 
 namespace bf = boost::filesystem;
 namespace bs = boost::system;
+namespace ci = common_installer;
 
 namespace {
 
 const char kSharedData[] = "shared/data";
 const char kSkelApp[] = "skel/apps_rw";
-const utils::VersionNumber ver30("3.0");
 
 bool CreateSharedDir(bf::path shareddir_path) {
   if (bf::exists(shareddir_path))
@@ -70,6 +68,17 @@ bool CreatePerUserSharedDir(const std::string& pkgid) {
   return true;
 }
 
+bool ShouldCreateSharedDataDir(manifest_x* manifest) {
+  bool shareddata = ci::ShouldSupportLegacySharedDataDir(manifest->api_version);
+  for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
+    if (!strcmp(priv->value, ci::privileges::kPrivForSharedData)) {
+      shareddata = true;
+      break;
+    }
+  }
+  return shareddata;
+}
+
 }  // namespace
 
 namespace common_installer {
@@ -77,50 +86,20 @@ namespace filesystem {
 
 bool StepUpdateStorageDirectories::UpdatePerUserStorageDirectories() {
   manifest_x* manifest = context_->manifest_data.get();
-  std::string package_id = context_->pkgid.get();
-  utils::VersionNumber api_version(manifest->api_version);
-
-  if (api_version < ver30) {
-    return CreatePerUserSharedDir(package_id);
-  } else {
-    bool shareddata = false;
-    for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
-      if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
-        shareddata = true;
-        break;
-      }
-    }
-
-    if (shareddata)
-      return CreatePerUserSharedDir(package_id);
-    else
-      return RemovePerUserSharedDir(package_id);
-  }
-  return true;
+  if (ShouldCreateSharedDataDir(manifest))
+    return CreatePerUserSharedDir(context_->pkgid.get());
+  else
+    return RemovePerUserSharedDir(context_->pkgid.get());
 }
 
 bool StepUpdateStorageDirectories::UpdateStorageDirectories() {
   manifest_x* manifest = context_->manifest_data.get();
-  std::string str_ver(manifest->api_version);
-  utils::VersionNumber api_version(str_ver);
-
   bf::path shared_data_path = context_->GetPkgPath() / kSharedData;
-  if (api_version < ver30) {
+  if (ShouldCreateSharedDataDir(manifest)) {
     return CreateSharedDir(shared_data_path);
   } else {
-    bool shareddata = false;
-    for (auto& priv : GListRange<privilege_x*>(manifest->privileges)) {
-      if (!strcmp(priv->value, privileges::kPrivForSharedData)) {
-        shareddata = true;
-        break;
-      }
-    }
-    if (shareddata) {
-      return CreateSharedDir(shared_data_path);
-    } else {
-      if (!RemoveAll(shared_data_path))
-        return false;
-    }
+    if (!RemoveAll(shared_data_path))
+      return false;
   }
   return true;
 }