From db9bab6b76f44d48caadf2ab004b8b7109be83b2 Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Thu, 5 Sep 2019 16:26:17 +0900 Subject: [PATCH] Refactor creating shared/data directory Change-Id: I2b849df574fab2c17fb44c482abbd24b278f5f0a Signed-off-by: Sangyoon Jang --- src/common/shared_dirs.cc | 18 ++++--- src/common/shared_dirs.h | 13 +++-- .../filesystem/step_create_storage_directories.cc | 14 +++-- .../filesystem/step_create_storage_directories.h | 5 +- .../filesystem/step_update_storage_directories.cc | 59 +++++++--------------- 5 files changed, 47 insertions(+), 62 deletions(-) diff --git a/src/common/shared_dirs.cc b/src/common/shared_dirs.cc index 8989802..ff98850 100644 --- a/src/common/shared_dirs.cc +++ b/src/common/shared_dirs.cc @@ -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 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 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())) { 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 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 diff --git a/src/common/shared_dirs.h b/src/common/shared_dirs.h index 35ea19a..e1f0b3c 100644 --- a/src/common/shared_dirs.h +++ b/src/common/shared_dirs.h @@ -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 additional_dirs = std::vector()); @@ -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_ diff --git a/src/common/step/filesystem/step_create_storage_directories.cc b/src/common/step/filesystem/step_create_storage_directories.cc index 546175f..de305e5 100644 --- a/src/common/step/filesystem/step_create_storage_directories.cc +++ b/src/common/step/filesystem/step_create_storage_directories.cc @@ -4,7 +4,6 @@ #include "common/step/filesystem/step_create_storage_directories.h" -#include #include #include @@ -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(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; diff --git a/src/common/step/filesystem/step_create_storage_directories.h b/src/common/step/filesystem/step_create_storage_directories.h index 59a2b8d..cab1310 100644 --- a/src/common/step/filesystem/step_create_storage_directories.h +++ b/src/common/step/filesystem/step_create_storage_directories.h @@ -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 additional_shared_dirs_; diff --git a/src/common/step/filesystem/step_update_storage_directories.cc b/src/common/step/filesystem/step_update_storage_directories.cc index 29ba789..3ebc00a 100644 --- a/src/common/step/filesystem/step_update_storage_directories.cc +++ b/src/common/step/filesystem/step_update_storage_directories.cc @@ -4,8 +4,6 @@ #include "common/step/filesystem/step_update_storage_directories.h" -#include - #include #include #include @@ -20,12 +18,12 @@ 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(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(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(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; } -- 2.7.4