From c7eebdb071764e6aa78c75e4490e9bb5ea03ebf1 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 11 May 2021 10:06:47 +0900 Subject: [PATCH] Add ExternalDirectoryInfo class While getting external path, AUL uses storage API to get external storage path. And, the storage API uses gdbus API to get primary sdcard path. While using gdbus API, two threads are created by gdbus. Currently, threads are created when the process is calling aul_get_app_root_path(). The codes are separated from DirectoryInfo class for backward compatibility. Change-Id: I83e1e931afa85002fd239fd2f4459cf08f58eef9 Signed-off-by: Hwankyu Jhun --- aul/app_info/directory_info.cc | 88 +------------------ aul/app_info/directory_info.hh | 47 +++------- aul/app_info/external_directory_info.cc | 148 ++++++++++++++++++++++++++++++++ aul/app_info/external_directory_info.hh | 66 ++++++++++++++ src/aul_path.cc | 39 ++++++--- 5 files changed, 257 insertions(+), 131 deletions(-) create mode 100644 aul/app_info/external_directory_info.cc create mode 100644 aul/app_info/external_directory_info.hh diff --git a/aul/app_info/directory_info.cc b/aul/app_info/directory_info.cc index a238435..67346e6 100644 --- a/aul/app_info/directory_info.cc +++ b/aul/app_info/directory_info.cc @@ -15,7 +15,6 @@ * */ -#include #include #include #include @@ -31,7 +30,6 @@ namespace aul { namespace { -constexpr const char kDefaultExternalStorage[] = "/opt/media/sdcard"; constexpr const char kDataDir[] = "data/"; constexpr const char kCacheDir[] = "cache/"; constexpr const char kResourceDir[] = "res/"; @@ -40,30 +38,6 @@ constexpr const char kSharedDataDir[] = "shared/data/"; constexpr const char kSharedTrustedDir[] = "shared/trusted/"; constexpr const char kSharedResourceDir[] = "shared/res/"; -std::string GetSdCardPath() { - int storage_id = 0; - char* path = nullptr; - int ret = storage_get_primary_sdcard(&storage_id, &path); - if (ret != STORAGE_ERROR_NONE) - _W("Failed to get primary sdcard. error(%d)", ret); - - auto ptr = std::unique_ptr(path, std::free); - if (path) - return std::string(path); - - return std::string(kDefaultExternalStorage); -} - -std::string GetExternalPath(const std::string& pkg_id, uid_t uid) { - std::string sdcard_path = GetSdCardPath(); - tzplatform_set_user(uid); - std::string path = sdcard_path + "/apps/" + - std::string(tzplatform_getenv(TZ_USER_NAME)) + "/apps_rw/" + - pkg_id; - tzplatform_reset_user(); - return path; -} - std::string GetRWPath(const std::string& pkg_id, uid_t uid) { tzplatform_set_user(uid); std::string path = std::string(tzplatform_getenv(TZ_USER_APP)) + "/" + pkg_id; @@ -126,35 +100,10 @@ DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedTrustedPath( return *this; } -DirectoryInfo::Builder& DirectoryInfo::Builder::SetExternalRootPath( - std::string external_root_path) { - external_root_path_ = std::move(external_root_path); - return *this; -} - -DirectoryInfo::Builder& DirectoryInfo::Builder::SetExternalDataPath( - std::string external_data_path) { - external_data_path_ = std::move(external_data_path); - return *this; -} - -DirectoryInfo::Builder& DirectoryInfo::Builder::SetExternalCachePath( - std::string external_cache_path) { - external_cache_path_ = std::move(external_cache_path); - return *this; -} - -DirectoryInfo::Builder& DirectoryInfo::Builder::SetExternalSharedDataPath( - std::string external_shared_data_path) { - external_shared_data_path_ = std::move(external_shared_data_path); - return *this; -} - DirectoryInfo::Builder::operator DirectoryInfo*() const { return new (std::nothrow) DirectoryInfo(root_path_, data_path_, cache_path_, resource_path_, tep_resource_path_, shared_data_path_, - shared_resource_path_, shared_trusted_path_, external_root_path_, - external_data_path_, external_cache_path_, external_shared_data_path_); + shared_resource_path_, shared_trusted_path_); } DirectoryInfo* DirectoryInfo::Get(const std::string app_id, @@ -165,7 +114,6 @@ DirectoryInfo* DirectoryInfo::Get(const std::string app_id, std::string root_path = info->GetRootPath(); std::string rw_path = GetRWPath(info->GetPkgId(), uid); - std::string external_path = GetExternalPath(info->GetPkgId(), uid); return Builder().SetRootPath(info->GetRootPath()) .SetDataPath(GetPath(rw_path, kDataDir)) .SetCachePath(GetPath(rw_path, kCacheDir)) @@ -173,11 +121,7 @@ DirectoryInfo* DirectoryInfo::Get(const std::string app_id, .SetTepResourcePath(GetPath(root_path, kTepResourceDir)) .SetSharedDataPath(GetPath(rw_path, kSharedDataDir)) .SetSharedResourcePath(GetPath(root_path, kSharedResourceDir)) - .SetSharedTrustedPath(GetPath(rw_path, kSharedTrustedDir)) - .SetExternalRootPath(std::string(external_path) + "/") - .SetExternalDataPath(GetPath(external_path, kDataDir)) - .SetExternalCachePath(GetPath(external_path, kCacheDir)) - .SetExternalSharedDataPath(GetPath(external_path, kSharedDataDir)); + .SetSharedTrustedPath(GetPath(rw_path, kSharedTrustedDir)); } DirectoryInfo* DirectoryInfo::Get() { @@ -196,11 +140,7 @@ DirectoryInfo::DirectoryInfo(std::string root_path, std::string tep_resource_path, std::string shared_data_path, std::string shared_resource_path, - std::string shared_trusted_path, - std::string external_root_path, - std::string external_data_path, - std::string external_cache_path, - std::string external_shared_data_path) + std::string shared_trusted_path) : root_path_(std::move(root_path)), data_path_(std::move(data_path)), cache_path_(std::move(cache_path)), @@ -208,11 +148,7 @@ DirectoryInfo::DirectoryInfo(std::string root_path, tep_resource_path_(std::move(tep_resource_path)), shared_data_path_(std::move(shared_data_path)), shared_resource_path_(std::move(shared_resource_path)), - shared_trusted_path_(std::move(shared_trusted_path)), - external_root_path_(std::move(external_root_path)), - external_data_path_(std::move(external_data_path)), - external_cache_path_(std::move(external_cache_path)), - external_shared_data_path_(std::move(external_shared_data_path)) { + shared_trusted_path_(std::move(shared_trusted_path)) { } const std::string& DirectoryInfo::GetRootPath() const { @@ -247,20 +183,4 @@ const std::string& DirectoryInfo::GetSharedTrustedPath() const { return shared_trusted_path_; } -const std::string& DirectoryInfo::GetExternalRootPath() const { - return external_root_path_; -} - -const std::string& DirectoryInfo::GetExternalDataPath() const { - return external_data_path_; -} - -const std::string& DirectoryInfo::GetExternalCachePath() const { - return external_cache_path_; -} - -const std::string& DirectoryInfo::GetExternalSharedDataPath() const { - return external_shared_data_path_; -} - } // namespace aul diff --git a/aul/app_info/directory_info.hh b/aul/app_info/directory_info.hh index 1a43679..1409678 100644 --- a/aul/app_info/directory_info.hh +++ b/aul/app_info/directory_info.hh @@ -24,6 +24,19 @@ namespace aul { class DirectoryInfo { public: + static DirectoryInfo* Get(const std::string app_id, uid_t uid); + static DirectoryInfo* Get(); + + const std::string& GetRootPath() const; + const std::string& GetDataPath() const; + const std::string& GetCachePath() const; + const std::string& GetResourcePath() const; + const std::string& GetTepResourcePath() const; + const std::string& GetSharedDataPath() const; + const std::string& GetSharedResourcePath() const; + const std::string& GetSharedTrustedPath() const; + + private: class Builder { public: Builder& SetRootPath(std::string root_path); @@ -34,10 +47,6 @@ class DirectoryInfo { Builder& SetSharedDataPath(std::string shared_data_path); Builder& SetSharedResourcePath(std::string shared_resource_path); Builder& SetSharedTrustedPath(std::string shared_trusted_path); - Builder& SetExternalRootPath(std::string external_root_path); - Builder& SetExternalDataPath(std::string external_data_path); - Builder& SetExternalCachePath(std::string external_cache_path); - Builder& SetExternalSharedDataPath(std::string external_shared_data_path); operator DirectoryInfo*() const; @@ -51,15 +60,8 @@ class DirectoryInfo { std::string shared_data_path_; std::string shared_resource_path_; std::string shared_trusted_path_; - std::string external_root_path_; - std::string external_data_path_; - std::string external_cache_path_; - std::string external_shared_data_path_; }; - static DirectoryInfo* Get(const std::string app_id, uid_t uid); - static DirectoryInfo* Get(); - DirectoryInfo(std::string root_path, std::string data_path, std::string cache_path, @@ -67,24 +69,7 @@ class DirectoryInfo { std::string tep_resource_path, std::string shared_data_path, std::string shared_resource_path, - std::string shared_trusted_path, - std::string external_root_path, - std::string external_data_path, - std::string external_cache_path, - std::string external_shared_data_path); - - const std::string& GetRootPath() const; - const std::string& GetDataPath() const; - const std::string& GetCachePath() const; - const std::string& GetResourcePath() const; - const std::string& GetTepResourcePath() const; - const std::string& GetSharedDataPath() const; - const std::string& GetSharedResourcePath() const; - const std::string& GetSharedTrustedPath() const; - const std::string& GetExternalRootPath() const; - const std::string& GetExternalDataPath() const; - const std::string& GetExternalCachePath() const; - const std::string& GetExternalSharedDataPath() const; + std::string shared_trusted_path); private: std::string root_path_; @@ -95,10 +80,6 @@ class DirectoryInfo { std::string shared_data_path_; std::string shared_resource_path_; std::string shared_trusted_path_; - std::string external_root_path_; - std::string external_data_path_; - std::string external_cache_path_; - std::string external_shared_data_path_; }; } // namespace aul diff --git a/aul/app_info/external_directory_info.cc b/aul/app_info/external_directory_info.cc new file mode 100644 index 0000000..69722ed --- /dev/null +++ b/aul/app_info/external_directory_info.cc @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include + +#include + +#include "aul/app_info/app_info.hh" +#include "aul/app_info/external_directory_info.hh" +#include "aul/common/common.hh" +#include "aul/common/log_private.hh" +#include "include/aul.h" + +namespace aul { +namespace { + +constexpr const char kDefaultExternalStorage[] = "/opt/media/sdcard"; +constexpr const char kDataDir[] = "data/"; +constexpr const char kCacheDir[] = "cache/"; +constexpr const char kSharedDataDir[] = "shared/data/"; + +std::string GetSdCardPath() { + int storage_id = 0; + char* path = nullptr; + int ret = storage_get_primary_sdcard(&storage_id, &path); + if (ret != STORAGE_ERROR_NONE) + _W("Failed to get primary sdcard. error(%d)", ret); + + auto ptr = std::unique_ptr(path, std::free); + if (path) + return std::string(path); + + return std::string(kDefaultExternalStorage); +} + +std::string GetExternalPath(const std::string& pkg_id, uid_t uid) { + std::string sdcard_path = GetSdCardPath(); + tzplatform_set_user(uid); + std::string path = sdcard_path + "/apps/" + + std::string(tzplatform_getenv(TZ_USER_NAME)) + "/apps_rw/" + + pkg_id; + tzplatform_reset_user(); + return path; +} + +std::string GetPath(const std::string& path, + const std::string& dir_name) { + return path + "/" + dir_name; +} + +} // namespace + +ExternalDirectoryInfo::Builder& ExternalDirectoryInfo::Builder::SetRootPath( + std::string root_path) { + root_path_ = std::move(root_path); + return *this; +} + +ExternalDirectoryInfo::Builder& ExternalDirectoryInfo::Builder::SetDataPath( + std::string data_path) { + data_path_ = std::move(data_path); + return *this; +} + +ExternalDirectoryInfo::Builder& ExternalDirectoryInfo::Builder::SetCachePath( + std::string cache_path) { + cache_path_ = std::move(cache_path); + return *this; +} + +ExternalDirectoryInfo::Builder& +ExternalDirectoryInfo::Builder::SetSharedDataPath( + std::string shared_data_path) { + shared_data_path_ = std::move(shared_data_path); + return *this; +} + +ExternalDirectoryInfo::Builder::operator ExternalDirectoryInfo*() const { + return new (std::nothrow) ExternalDirectoryInfo(root_path_, data_path_, + cache_path_, shared_data_path_); +} + +ExternalDirectoryInfo* ExternalDirectoryInfo::Get(const std::string app_id, + uid_t uid) { + std::unique_ptr info(AppInfo::Get(app_id, uid)); + if (info.get() == nullptr) + return nullptr; + + std::string external_path = GetExternalPath(info->GetPkgId(), uid); + return Builder().SetRootPath(std::string(external_path) + "/") + .SetDataPath(GetPath(external_path, kDataDir)) + .SetCachePath(GetPath(external_path, kCacheDir)) + .SetSharedDataPath(GetPath(external_path, kSharedDataDir)); +} + +ExternalDirectoryInfo* ExternalDirectoryInfo::Get() { + char app_id[256] = { 0, }; + int ret = aul_app_get_appid_bypid(getpid(), app_id, sizeof(app_id)); + if (ret != AUL_R_OK) + return nullptr; + + return Get(app_id, getuid()); +} + +ExternalDirectoryInfo::ExternalDirectoryInfo(std::string root_path, + std::string data_path, + std::string cache_path, + std::string shared_data_path) + : root_path_(std::move(root_path)), + data_path_(std::move(data_path)), + cache_path_(std::move(cache_path)), + shared_data_path_(std::move(shared_data_path)) { +} + +const std::string& ExternalDirectoryInfo::GetRootPath() const { + return root_path_; +} + +const std::string& ExternalDirectoryInfo::GetDataPath() const { + return data_path_; +} + +const std::string& ExternalDirectoryInfo::GetCachePath() const { + return cache_path_; +} + +const std::string& ExternalDirectoryInfo::GetSharedDataPath() const { + return shared_data_path_; +} + +} // namespace aul diff --git a/aul/app_info/external_directory_info.hh b/aul/app_info/external_directory_info.hh new file mode 100644 index 0000000..564382d --- /dev/null +++ b/aul/app_info/external_directory_info.hh @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef AUL_APP_INFO_EXTERNAL_DIRECTORY_INFO_HH +#define AUL_APP_INFO_EXTERNAL_DIRECTORY_INFO_HH + +#include + +namespace aul { + +class ExternalDirectoryInfo { + public: + static ExternalDirectoryInfo* Get(const std::string app_id, uid_t uid); + static ExternalDirectoryInfo* Get(); + + const std::string& GetRootPath() const; + const std::string& GetDataPath() const; + const std::string& GetCachePath() const; + const std::string& GetSharedDataPath() const; + + private: + class Builder { + public: + Builder& SetRootPath(std::string root_path); + Builder& SetDataPath(std::string data_path); + Builder& SetCachePath(std::string cache_path); + Builder& SetSharedDataPath(std::string shared_data_path); + + operator ExternalDirectoryInfo*() const; + + private: + std::string root_path_; + std::string data_path_; + std::string cache_path_; + std::string shared_data_path_; + }; + + ExternalDirectoryInfo(std::string root_path, + std::string data_path, + std::string cache_path, + std::string shared_data_path); + + private: + std::string root_path_; + std::string data_path_; + std::string cache_path_; + std::string shared_data_path_; +}; + +} // namespace aul + +#endif // AUL_APP_INFO_EXTERNAL_DIRECTORY_INFO_HH diff --git a/src/aul_path.cc b/src/aul_path.cc index 976d9a9..d42e4a3 100644 --- a/src/aul_path.cc +++ b/src/aul_path.cc @@ -20,6 +20,7 @@ #include #include "aul/app_info/directory_info.hh" +#include "aul/app_info/external_directory_info.hh" #include "aul_api.h" #include "aul_util.h" #include "include/aul.h" @@ -29,7 +30,9 @@ namespace { std::unique_ptr context(nullptr); +std::unique_ptr ext_context(nullptr); std::mutex mutex; +std::mutex ext_mutex; const aul::DirectoryInfo* GetCurrentContext() { std::unique_lock lock(mutex); @@ -39,14 +42,22 @@ const aul::DirectoryInfo* GetCurrentContext() { return context.get(); } +const aul::ExternalDirectoryInfo* GetCurrentExtContext() { + std::unique_lock lock(ext_mutex); + if (ext_context.get() == nullptr) + ext_context.reset(aul::ExternalDirectoryInfo::Get()); + + return ext_context.get(); +} + } // namespace AUL_API const char* aul_get_app_external_root_path(void) { - auto* context = GetCurrentContext(); + auto* context = GetCurrentExtContext(); if (context == nullptr) return nullptr; - return context->GetExternalRootPath().c_str(); + return context->GetRootPath().c_str(); } AUL_API const char* aul_get_app_root_path(void) { @@ -118,27 +129,27 @@ AUL_API const char* aul_get_app_shared_trusted_path(void) { } AUL_API const char* aul_get_app_external_data_path(void) { - auto* context = GetCurrentContext(); + auto* context = GetCurrentExtContext(); if (context == nullptr) return nullptr; - return context->GetExternalDataPath().c_str(); + return context->GetDataPath().c_str(); } AUL_API const char* aul_get_app_external_cache_path(void) { - auto* context = GetCurrentContext(); + auto* context = GetCurrentExtContext(); if (context == nullptr) return nullptr; - return context->GetExternalCachePath().c_str(); + return context->GetCachePath().c_str(); } AUL_API const char* aul_get_app_external_shared_data_path(void) { - auto* context = GetCurrentContext(); + auto* context = GetCurrentExtContext(); if (context == nullptr) return nullptr; - return context->GetExternalSharedDataPath().c_str(); + return context->GetSharedDataPath().c_str(); } AUL_API const char* aul_get_app_specific_path(void) { @@ -201,12 +212,12 @@ AUL_API int aul_get_app_external_shared_data_path_by_appid(const char* appid, if (appid == nullptr || path == nullptr) return AUL_R_EINVAL; - std::unique_ptr info( - aul::DirectoryInfo::Get(appid, getuid())); + std::unique_ptr info( + aul::ExternalDirectoryInfo::Get(appid, getuid())); if (info.get() == nullptr) return AUL_R_ENOAPP; - *path = strdup(info->GetExternalSharedDataPath().c_str()); + *path = strdup(info->GetSharedDataPath().c_str()); return AUL_R_OK; } @@ -260,11 +271,11 @@ AUL_API int aul_get_usr_app_external_shared_data_path_by_appid( if (appid == nullptr || path == nullptr) return AUL_R_EINVAL; - std::unique_ptr info( - aul::DirectoryInfo::Get(appid, uid)); + std::unique_ptr info( + aul::ExternalDirectoryInfo::Get(appid, uid)); if (info.get() == nullptr) return AUL_R_ENOAPP; - *path = strdup(info->GetExternalSharedDataPath().c_str()); + *path = strdup(info->GetSharedDataPath().c_str()); return AUL_R_OK; } -- 2.7.4