From f63cc613b8317eae0bd7db020171dd50464a4722 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 23 Mar 2021 10:39:44 +0900 Subject: [PATCH] Refactor AUL Path API Change-Id: I3fbf85e5895b0f0e40d96ffe8b965fb631ffd904 Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 2 + aul/app_info/app_info.cc | 108 +++++++++++ aul/app_info/app_info.hh | 62 ++++++ aul/app_info/directory_info.cc | 266 +++++++++++++++++++++++++ aul/app_info/directory_info.hh | 106 ++++++++++ src/aul_path.c | 427 ----------------------------------------- src/aul_path.cc | 270 ++++++++++++++++++++++++++ 7 files changed, 814 insertions(+), 427 deletions(-) create mode 100644 aul/app_info/app_info.cc create mode 100644 aul/app_info/app_info.hh create mode 100644 aul/app_info/directory_info.cc create mode 100644 aul/app_info/directory_info.hh delete mode 100644 src/aul_path.c create mode 100644 src/aul_path.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 284c975..bc0e00a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ AUX_SOURCE_DIRECTORY(aul AUL_SRCS) AUX_SOURCE_DIRECTORY(aul/api AUL_API_SRCS) AUX_SOURCE_DIRECTORY(aul/app_control AUL_APP_CONTROL_SRCS) AUX_SOURCE_DIRECTORY(aul/app_manager AUL_APP_MANAGER_SRCS) +AUX_SOURCE_DIRECTORY(aul/app_info AUL_APP_INFO_SRCS) AUX_SOURCE_DIRECTORY(aul/common AUL_COMMON_SRCS) AUX_SOURCE_DIRECTORY(aul/component AUL_COMPONENT_SRCS) AUX_SOURCE_DIRECTORY(aul/socket AUL_SOCKET_SRCS) @@ -69,6 +70,7 @@ ADD_LIBRARY(${TARGET_AUL} SHARED ${AUL_API_SRCS} ${AUL_APP_CONTROL_SRCS} ${AUL_APP_MANAGER_SRCS} + ${AUL_APP_INFO_SRCS} ${AUL_COMMON_SRCS} ${AUL_COMPONENT_SRCS} ${AUL_SOCKET_SRCS} diff --git a/aul/app_info/app_info.cc b/aul/app_info/app_info.cc new file mode 100644 index 0000000..cac07dd --- /dev/null +++ b/aul/app_info/app_info.cc @@ -0,0 +1,108 @@ +/* + * 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 "aul/app_info/app_info.hh" +#include "aul/common/exception.hh" +#include "aul/socket/client.hh" +#include "include/aul.h" +#include "include/aul_cmd.h" +#include "include/aul_error.h" +#include "include/aul_sock.h" + +namespace aul { + +AppInfo::Builder& AppInfo::Builder::SetAppId(std::string app_id) { + app_id_ = std::move(app_id); + return *this; +} + +AppInfo::Builder& AppInfo::Builder::SetPkgId(std::string pkg_id) { + pkg_id_ = std::move(pkg_id); + return *this; +} + +AppInfo::Builder& AppInfo::Builder::SetRootPath(std::string root_path) { + root_path_ = std::move(root_path); + return *this; +} + +AppInfo::Builder::operator AppInfo*() const { + return new (std::nothrow) AppInfo(app_id_, pkg_id_, root_path_); +} + +AppInfo* AppInfo::Get(const std::string& app_id, uid_t uid) { + pkgmgrinfo_appinfo_h handle; + int ret = pkgmgrinfo_appinfo_get_usr_appinfo(app_id.c_str(), uid, &handle); + if (ret != PMINFO_R_OK) { + _E("Failed to get appinfo. app_id(%s), uid(%u), error(%d)", + app_id.c_str(), uid, ret); + return nullptr; + } + + std::unique_ptr::type, + decltype(pkgmgrinfo_appinfo_destroy_appinfo)*> ptr(handle, + pkgmgrinfo_appinfo_destroy_appinfo); + + char* pkg_id; + ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkg_id); + if (ret != PMINFO_R_OK) { + _E("Failed to get pkgid. error(%d)", ret); + return nullptr; + } + + char* root_path; + ret = pkgmgrinfo_appinfo_get_root_path(handle, &root_path); + if (ret != PMINFO_R_OK) { + _E("Failed to get root path. error(%d)", ret); + return nullptr; + } + + return Builder().SetAppId(app_id).SetPkgId(pkg_id).SetRootPath(root_path); +} + +AppInfo* AppInfo::Get(int pid) { + char app_id[256] = { 0, }; + int ret = aul_app_get_appid_bypid(pid, app_id, sizeof(app_id)); + if (ret != AUL_R_OK) + return nullptr; + + return Get(app_id, getuid()); +} + +AppInfo::AppInfo(std::string app_id, std::string pkg_id, std::string root_path) + : app_id_(std::move(app_id)), + pkg_id_(std::move(pkg_id)), + root_path_(std::move(root_path)) { +} + +const std::string& AppInfo::GetAppId() const { + return app_id_; +} + +const std::string& AppInfo::GetPkgId() const { + return pkg_id_; +} + +const std::string& AppInfo::GetRootPath() const { + return root_path_; +} + +} // namespace aul diff --git a/aul/app_info/app_info.hh b/aul/app_info/app_info.hh new file mode 100644 index 0000000..3139487 --- /dev/null +++ b/aul/app_info/app_info.hh @@ -0,0 +1,62 @@ +/* + * 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_APP_INFO_HH +#define AUL_APP_INFO_APP_INFO_HH + +#include +#include + +#include + +namespace aul { + +class AppInfo { + public: + class Builder { + public: + Builder& SetAppId(std::string app_id); + Builder& SetPkgId(std::string pkg_id); + Builder& SetRootPath(std::string root_path); + operator AppInfo*() const; + + private: + std::string app_id_; + std::string pkg_id_; + std::string root_path_; + }; + + static AppInfo* Get(const std::string& app_id, uid_t uid); + static AppInfo* Get(int pid); + + AppInfo(std::string app_id, + std::string pkg_id, + std::string root_path); + + const std::string& GetAppId() const; + const std::string& GetPkgId() const; + const std::string& GetRootPath() const; + + private: + std::string app_id_; + std::string pkg_id_; + std::string root_path_; +}; + +} // namespace aul + +#endif // AUL_APP_INFO_APP_INFO_HH diff --git a/aul/app_info/directory_info.cc b/aul/app_info/directory_info.cc new file mode 100644 index 0000000..a238435 --- /dev/null +++ b/aul/app_info/directory_info.cc @@ -0,0 +1,266 @@ +/* + * 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/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 kResourceDir[] = "res/"; +constexpr const char kTepResourceDir[] = "tep/mount/"; +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; + tzplatform_reset_user(); + return path; +} + +std::string GetPath(const std::string& path, + const std::string& dir_name) { + return path + "/" + dir_name; +} + +} // namespace + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetRootPath( + std::string root_path) { + root_path_ = std::move(root_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetDataPath( + std::string data_path) { + data_path_ = std::move(data_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetCachePath( + std::string cache_path) { + cache_path_ = std::move(cache_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetResourcePath( + std::string resource_path) { + resource_path_ = std::move(resource_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetTepResourcePath( + std::string tep_resource_path) { + tep_resource_path_ = std::move(tep_resource_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedDataPath( + std::string shared_data_path) { + shared_data_path_ = std::move(shared_data_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedResourcePath( + std::string shared_resource_path) { + shared_resource_path_ = std::move(shared_resource_path); + return *this; +} + +DirectoryInfo::Builder& DirectoryInfo::Builder::SetSharedTrustedPath( + std::string shared_trusted_path) { + shared_trusted_path_ = std::move(shared_trusted_path); + 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_); +} + +DirectoryInfo* DirectoryInfo::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 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)) + .SetResourcePath(GetPath(root_path, kResourceDir)) + .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)); +} + +DirectoryInfo* DirectoryInfo::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()); +} + +DirectoryInfo::DirectoryInfo(std::string root_path, + std::string data_path, + std::string cache_path, + std::string resource_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) + : root_path_(std::move(root_path)), + data_path_(std::move(data_path)), + cache_path_(std::move(cache_path)), + resource_path_(std::move(resource_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)) { +} + +const std::string& DirectoryInfo::GetRootPath() const { + return root_path_; +} + +const std::string& DirectoryInfo::GetDataPath() const { + return data_path_; +} + +const std::string& DirectoryInfo::GetCachePath() const { + return cache_path_; +} + +const std::string& DirectoryInfo::GetResourcePath() const { + return resource_path_; +} + +const std::string& DirectoryInfo::GetTepResourcePath() const { + return tep_resource_path_; +} + +const std::string& DirectoryInfo::GetSharedDataPath() const { + return shared_data_path_; +} + +const std::string& DirectoryInfo::GetSharedResourcePath() const { + return shared_resource_path_; +} + +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 new file mode 100644 index 0000000..1a43679 --- /dev/null +++ b/aul/app_info/directory_info.hh @@ -0,0 +1,106 @@ +/* + * 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_DIRECTORY_INFO_HH +#define AUL_APP_INFO_DIRECTORY_INFO_HH + +#include + +namespace aul { + +class DirectoryInfo { + public: + class Builder { + public: + Builder& SetRootPath(std::string root_path); + Builder& SetDataPath(std::string data_path); + Builder& SetCachePath(std::string cache_path); + Builder& SetResourcePath(std::string resource_path); + Builder& SetTepResourcePath(std::string tep_resource_path); + 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; + + private: + std::string root_path_; + std::string rw_path_; + std::string data_path_; + std::string cache_path_; + std::string resource_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_; + }; + + 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, + std::string resource_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); + + 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; + + private: + std::string root_path_; + std::string data_path_; + std::string cache_path_; + std::string resource_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_; +}; + +} // namespace aul + +#endif // AUL_APP_INFO_DIRECTORY_INFO_HH diff --git a/src/aul_path.c b/src/aul_path.c deleted file mode 100644 index 127144a..0000000 --- a/src/aul_path.c +++ /dev/null @@ -1,427 +0,0 @@ -/* - * Copyright (c) 2014 - 2015 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. - * - */ - -#define _GNU_SOURCE -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - -#include "aul_api.h" -#include "aul_util.h" -#include "aul.h" - -#define ROOT_UID 0 -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) -#define DEFAULT_EXTERNAL_STORAGE "/opt/media/sdcard" - -static const char _DATA_DIR[] = "data/"; -static const char _CACHE_DIR[] = "cache/"; -static const char _RESOURCE_DIR[] = "res/"; -static const char _TEP_RESOURCE_DIR[] = "tep/mount/"; -static const char _SHARED_DATA_DIR[] = "shared/data/"; -static const char _SHARED_TRUSTED_DIR[] = "shared/trusted/"; -static const char _SHARED_RESOURCE_DIR[] = "shared/res/"; - -static const char * __get_specific_path(const char *pkgid, uid_t uid) -{ - const char * path; - char buf[PATH_MAX]; - - if (uid == ROOT_UID || uid == GLOBAL_USER) { - path = tzplatform_getenv(TZ_SYS_RO_APP); - snprintf(buf, sizeof(buf), "%s/%s", path, pkgid); - if (access(buf, R_OK) != 0) - path = tzplatform_getenv(TZ_SYS_RW_APP); - } else { - tzplatform_set_user(uid); - path = tzplatform_getenv(TZ_USER_APP); - tzplatform_reset_user(); - } - return path; -} - -static int __get_pkgid(char *pkgid, int len, const char *appid, uid_t uid) -{ - pkgmgrinfo_appinfo_h appinfo; - char *_pkgid; - int ret; - - if (appid == NULL) - return aul_app_get_pkgid_bypid(getpid(), pkgid, len); - - ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &appinfo); - if (ret != PMINFO_R_OK) { - _E("Failed to get app info. (ret:%d)", ret); - return AUL_R_ENOAPP; - } - - ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &_pkgid); - if (ret != PMINFO_R_OK) { - _E("Failed to get pkgid. (ret:%d)", ret); - pkgmgrinfo_appinfo_destroy_appinfo(appinfo); - return AUL_R_ENOAPP; - } - - snprintf(pkgid, len, "%s", _pkgid); - pkgmgrinfo_appinfo_destroy_appinfo(appinfo); - - return AUL_R_OK; -} - -/* return value should be freed after use. */ -static char *__get_sdcard_path(void) -{ - char *sdpath = NULL; - char *result_path = NULL; - int storage_id = 0; - int ret; - - ret = storage_get_primary_sdcard(&storage_id, &sdpath); - if (ret != STORAGE_ERROR_NONE) - _W("failed to get primary sdcard (%d)", ret); - - if (sdpath) - result_path = sdpath; - else - result_path = strdup(DEFAULT_EXTERNAL_STORAGE); - - return result_path; -} - -static int __get_external_path(char **path, const char *appid, - const char *dir_name, uid_t uid) -{ - char buf[PATH_MAX]; - char pkgid[NAME_MAX]; - char *ext_path; - int ret; - - ret = __get_pkgid(pkgid, sizeof(pkgid), appid, uid); - if (ret != AUL_R_OK) - return ret; - - assert(path); - - *path = NULL; - ext_path = __get_sdcard_path(); - if (ext_path) { - tzplatform_set_user(uid); - snprintf(buf, sizeof(buf), "%s/apps/%s/apps_rw/%s/%s", - ext_path, tzplatform_getenv(TZ_USER_NAME), - pkgid, dir_name ? dir_name : ""); - tzplatform_reset_user(); - free(ext_path); - ext_path = NULL; - - *path = strdup(buf); - } - ret = AUL_R_OK; - - return ret; -} - -static int __get_path(char **path, const char *appid, const char *dir_name, - uid_t uid) -{ - char buf[PATH_MAX]; - char pkgid[NAME_MAX]; - int ret; - - ret = __get_pkgid(pkgid, sizeof(pkgid), appid, uid); - if (ret != AUL_R_OK) - return ret; - - snprintf(buf, sizeof(buf), "%s/%s/%s", __get_specific_path(pkgid, uid), - pkgid, dir_name ? dir_name : ""); - *path = strdup(buf); - - return AUL_R_OK; -} - -static int __get_path_from_db(char **path, const char *appid, const char *dir_name, - uid_t uid) -{ - char *_path; - char buf[PATH_MAX]; - char appid_buf[NAME_MAX]; - int ret; - pkgmgrinfo_appinfo_h appinfo; - int len; - const char *root_path; - - root_path = aul_get_preinit_root_path(); - if (appid == NULL && root_path) { - len = strlen(root_path); - snprintf(buf, sizeof(buf), "%s%s%s", root_path, - root_path[len - 1] == '/' ? "" : "/", - dir_name ? dir_name : ""); - *path = strdup(buf); - return AUL_R_OK; - } - - if (appid == NULL) { - ret = aul_app_get_appid_bypid(getpid(), appid_buf, - sizeof(appid_buf)); - if (ret != AUL_R_OK) { - _E("appid is not given and failed to get appid"); - return ret; - } - } - - ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid ? appid : appid_buf, uid, - &appinfo); - if (ret != PMINFO_R_OK) { - _E("Failed to get appinfo. (ret:%d)", ret); - return AUL_R_ENOAPP; - } - ret = pkgmgrinfo_appinfo_get_root_path(appinfo, &_path); - if (ret != PMINFO_R_OK) { - _E("Failed to get root path. (ret:%d)", ret); - pkgmgrinfo_appinfo_destroy_appinfo(appinfo); - return AUL_R_ERROR; - } - - len = _path ? strlen(_path) : 0; - if (len == 0) { - _E("Root path is null or empty"); - pkgmgrinfo_appinfo_destroy_appinfo(appinfo); - return AUL_R_ERROR; - } - - snprintf(buf, sizeof(buf), "%s%s%s", _path, - _path[len - 1] == '/' ? "" : "/", - dir_name ? dir_name : ""); - - pkgmgrinfo_appinfo_destroy_appinfo(appinfo); - - assert(path); - *path = strdup(buf); - - return AUL_R_OK; -} - -static const char *__get(char **path, const char *appid, - const char *dir_name, uid_t uid, - int (*func_get)(char **, const char *, const char *, uid_t)) -{ - int r; - - if (*path) - return *path; - - assert(func_get); - - r = func_get(path, appid, dir_name, uid); - if (r != AUL_R_OK) - return NULL; - - return *path; -} - -API const char *aul_get_app_external_root_path(void) -{ - static char *path; - - return __get(&path, NULL, NULL, getuid(), __get_external_path); -} - -API const char *aul_get_app_root_path(void) -{ - static char *path; - - return __get(&path, NULL, NULL, getuid(), __get_path_from_db); -} - -API const char *aul_get_app_data_path(void) -{ - static char *path; - - return __get(&path, NULL, _DATA_DIR, getuid(), __get_path); -} - -API const char *aul_get_app_cache_path(void) -{ - static char *path; - - return __get(&path, NULL, _CACHE_DIR, getuid(), __get_path); -} - -API const char *aul_get_app_resource_path(void) -{ - static char *path; - - return __get(&path, NULL, _RESOURCE_DIR, getuid(), __get_path_from_db); -} - -API const char *aul_get_app_tep_resource_path(void) -{ - static char *path; - - return __get(&path, NULL, _TEP_RESOURCE_DIR, getuid(), __get_path_from_db); -} - -API int aul_get_app_shared_data_path(char **path) -{ - return aul_get_app_shared_data_path_by_appid(NULL, path); -} - -API const char *aul_get_app_shared_resource_path(void) -{ - static char *path; - - return __get(&path, NULL, _SHARED_RESOURCE_DIR, getuid(), __get_path_from_db); -} - -API const char *aul_get_app_shared_trusted_path(void) -{ - static char *path; - - return __get(&path, NULL, _SHARED_TRUSTED_DIR, getuid(), __get_path); -} - -API const char *aul_get_app_external_data_path(void) -{ - static char *path; - - return __get(&path, NULL, _DATA_DIR, getuid(), __get_external_path); -} - -API const char *aul_get_app_external_cache_path(void) -{ - static char *path; - - return __get(&path, NULL, _CACHE_DIR, getuid(), __get_external_path); -} - -API const char *aul_get_app_external_shared_data_path(void) -{ - static char *path; - - return __get(&path, NULL, _SHARED_DATA_DIR, getuid(), - __get_external_path); -} - -API const char *aul_get_app_specific_path(void) -{ - char appid[NAME_MAX]; - char pkgid[NAME_MAX]; - int ret; - - ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)); - if (ret != AUL_R_OK) - return NULL; - - ret = __get_pkgid(pkgid, sizeof(pkgid), appid, getuid()); - if (ret != AUL_R_OK) - return NULL; - - return __get_specific_path(pkgid, getuid()); -} - -API int aul_get_app_shared_data_path_by_appid(const char *appid, char **path) -{ - int ret; - char *ret_path; - - if (path == NULL) - return AUL_R_EINVAL; - - ret = __get_path(&ret_path, appid, _SHARED_DATA_DIR, getuid()); - if (ret != AUL_R_OK) - return ret; - - ret = access(ret_path, F_OK); - if (ret == 0) { - *path = ret_path; - } else { - free(ret_path); - return AUL_R_EREJECTED; - } - - return AUL_R_OK; -} - -API int aul_get_app_shared_resource_path_by_appid(const char *appid, - char **path) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_path_from_db(path, appid, _SHARED_RESOURCE_DIR, getuid()); -} - -API int aul_get_app_shared_trusted_path_by_appid(const char *appid, - char **path) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_path(path, appid, _SHARED_TRUSTED_DIR, getuid()); -} - -API int aul_get_app_external_shared_data_path_by_appid(const char *appid, - char **path) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_external_path(path, appid, _SHARED_DATA_DIR, getuid()); -} - -API int aul_get_usr_app_shared_data_path_by_appid(const char *appid, - char **path, uid_t uid) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_path_from_db(path, appid, _SHARED_DATA_DIR, uid); -} - -API int aul_get_usr_app_shared_resource_path_by_appid(const char *appid, - char **path, uid_t uid) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_path_from_db(path, appid, _SHARED_RESOURCE_DIR, uid); -} - -API int aul_get_usr_app_shared_trusted_path_by_appid(const char *appid, - char **path, uid_t uid) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_path_from_db(path, appid, _SHARED_TRUSTED_DIR, uid); -} - -API int aul_get_usr_app_external_shared_data_path_by_appid(const char *appid, - char **path, uid_t uid) -{ - if (appid == NULL || path == NULL) - return AUL_R_EINVAL; - - return __get_external_path(path, appid, _SHARED_DATA_DIR, uid); -} diff --git a/src/aul_path.cc b/src/aul_path.cc new file mode 100644 index 0000000..976d9a9 --- /dev/null +++ b/src/aul_path.cc @@ -0,0 +1,270 @@ +/* + * 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 "aul/app_info/directory_info.hh" +#include "aul_api.h" +#include "aul_util.h" +#include "include/aul.h" + +#undef AUL_API +#define AUL_API extern "C" API + +namespace { +std::unique_ptr context(nullptr); +std::mutex mutex; + +const aul::DirectoryInfo* GetCurrentContext() { + std::unique_lock lock(mutex); + if (context.get() == nullptr) + context.reset(aul::DirectoryInfo::Get()); + + return context.get(); +} + +} // namespace + +AUL_API const char* aul_get_app_external_root_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetExternalRootPath().c_str(); +} + +AUL_API const char* aul_get_app_root_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetRootPath().c_str(); +} + +AUL_API const char* aul_get_app_data_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetDataPath().c_str(); +} + +AUL_API const char* aul_get_app_cache_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetCachePath().c_str(); +} + +AUL_API const char* aul_get_app_resource_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetResourcePath().c_str(); +} + +AUL_API const char* aul_get_app_tep_resource_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetTepResourcePath().c_str(); +} + +AUL_API int aul_get_app_shared_data_path(char** path) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return AUL_R_ERROR; + + if (access(context->GetSharedDataPath().c_str(), F_OK) != 0) + return AUL_R_EREJECTED; + + *path = strdup(context->GetSharedDataPath().c_str()); + return AUL_R_OK; +} + +AUL_API const char* aul_get_app_shared_resource_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetSharedResourcePath().c_str(); +} + +AUL_API const char* aul_get_app_shared_trusted_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetSharedTrustedPath().c_str(); +} + +AUL_API const char* aul_get_app_external_data_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetExternalDataPath().c_str(); +} + +AUL_API const char* aul_get_app_external_cache_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetExternalCachePath().c_str(); +} + +AUL_API const char* aul_get_app_external_shared_data_path(void) { + auto* context = GetCurrentContext(); + if (context == nullptr) + return nullptr; + + return context->GetExternalSharedDataPath().c_str(); +} + +AUL_API const char* aul_get_app_specific_path(void) { + if (GetCurrentContext() == nullptr) + return nullptr; + + return tzplatform_getenv(TZ_USER_APP); +} + +AUL_API int aul_get_app_shared_data_path_by_appid(const char* appid, + char** path) { + if (path == nullptr) + return AUL_R_EINVAL; + + if (appid == nullptr) + return aul_get_app_shared_data_path(path); + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, getuid())); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + if (access(info->GetSharedDataPath().c_str(), F_OK) != 0) + return AUL_R_EREJECTED; + + *path = strdup(info->GetSharedDataPath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_app_shared_resource_path_by_appid(const char* appid, + char** path) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, getuid())); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetSharedResourcePath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_app_shared_trusted_path_by_appid(const char* appid, + char** path) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, getuid())); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetSharedTrustedPath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_app_external_shared_data_path_by_appid(const char* appid, + char** path) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, getuid())); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetExternalSharedDataPath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_usr_app_shared_data_path_by_appid(const char* appid, + char** path, uid_t uid) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, uid)); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + if (access(info->GetSharedDataPath().c_str(), F_OK) != 0) + return AUL_R_EREJECTED; + + *path = strdup(info->GetSharedDataPath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_usr_app_shared_resource_path_by_appid(const char* appid, + char** path, uid_t uid) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, uid)); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetSharedResourcePath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_usr_app_shared_trusted_path_by_appid(const char* appid, + char** path, uid_t uid) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, uid)); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetSharedTrustedPath().c_str()); + return AUL_R_OK; +} + +AUL_API int aul_get_usr_app_external_shared_data_path_by_appid( + const char* appid, char** path, uid_t uid) { + if (appid == nullptr || path == nullptr) + return AUL_R_EINVAL; + + std::unique_ptr info( + aul::DirectoryInfo::Get(appid, uid)); + if (info.get() == nullptr) + return AUL_R_ENOAPP; + + *path = strdup(info->GetExternalSharedDataPath().c_str()); + return AUL_R_OK; +} -- 2.7.4