From: Hwankyu Jhun Date: Thu, 22 Feb 2024 03:54:37 +0000 (+0900) Subject: Modify Path::Get() method X-Git-Tag: accepted/tizen/8.0/unified/20240223.155819~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7fb71e6ba15bebf7a75d3f4175eec2184e5c8431;p=platform%2Fcore%2Fapi%2Fpreference.git Modify Path::Get() method The method uses aul API to get the data path. If it's failed, the method tries to get the data path using tzplatform_config() API. Change-Id: I63168e61ed3f7b8f5b206393ab6890c9b3949034 Signed-off-by: Hwankyu Jhun --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c6cb94..38769a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ ADD_TEST(NAME ${TARGET_UNIT_TESTS} INCLUDE(FindPkgConfig) INCLUDE(ApplyPkgConfig) +PKG_CHECK_MODULES(AUL_DEPS REQUIRED aul) PKG_CHECK_MODULES(CAPI_APPFW_APP_COMMON_DEPS REQUIRED capi-appfw-app-common) PKG_CHECK_MODULES(CAPI_BASE_COMMON_DEPS REQUIRED capi-base-common) PKG_CHECK_MODULES(DLOG_DEPS REQUIRED dlog) diff --git a/packaging/capi-appfw-preference.spec b/packaging/capi-appfw-preference.spec index efdac9d..7e43110 100644 --- a/packaging/capi-appfw-preference.spec +++ b/packaging/capi-appfw-preference.spec @@ -15,6 +15,7 @@ BuildRequires: pkgconfig(capi-appfw-app-common) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(aul) %if 0%{?gcov:1} BuildRequires: lcov diff --git a/preference/CMakeLists.txt b/preference/CMakeLists.txt index fce8860..eec9512 100644 --- a/preference/CMakeLists.txt +++ b/preference/CMakeLists.txt @@ -10,10 +10,12 @@ TARGET_INCLUDE_DIRECTORIES(${TARGET_PREFERENCE} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include/) APPLY_PKG_CONFIG(${TARGET_PREFERENCE} PUBLIC + AUL_DEPS CAPI_APPFW_APP_COMMON_DEPS CAPI_BASE_COMMON_DEPS DLOG_DEPS GLIB_DEPS + LIBTZPLATFORM_CONFIG_DEPS ) INSTALL(TARGETS ${TARGET_PREFERENCE} diff --git a/preference/file-internal.cc b/preference/file-internal.cc index 6b1040d..b32e832 100644 --- a/preference/file-internal.cc +++ b/preference/file-internal.cc @@ -191,13 +191,20 @@ bool File::IsExisting() { return false; } -void File::CreateDir() { +bool File::CreateDir() { std::string pref_dir = Path::Get(); + if (pref_dir.empty()) return false; + if (access(pref_dir.c_str(), F_OK) != 0) { mode_t dir_mode = 0664 | 0111; - if (mkdir(pref_dir.c_str(), dir_mode) != 0) + if (mkdir(pref_dir.c_str(), dir_mode) != 0) { + if (errno == EEXIST) return true; _E("mkdir() is failed. path(%s), errno(%d)", pref_dir.c_str(), errno); + return false; + } } + + return true; } int File::Open(bool readonly) { @@ -260,8 +267,10 @@ void File::Unlock() { int File::TryWrite() { int ret = Open(false); if (ret < 0) { - if (ret == -ENOENT) - CreateDir(); + if (ret == -ENOENT) { + if (!CreateDir()) + _E("Failed to create directory"); + } return static_cast(Error::ERROR_FILE_OPEN); } diff --git a/preference/file-internal.hh b/preference/file-internal.hh index 3d2183a..6208546 100644 --- a/preference/file-internal.hh +++ b/preference/file-internal.hh @@ -20,6 +20,7 @@ #include #include #include +#include #include "preference/data-internal.hh" @@ -39,7 +40,7 @@ class File { ERROR_FILE_LOCK = -26, }; - File(std::string name, bool is_checksum = false); + explicit File(std::string name, bool is_checksum = false); File(std::string name, std::string pref_path, bool is_checksum = false); ~File(); @@ -61,7 +62,7 @@ class File { void Close(); private: - void CreateDir(); + bool CreateDir(); int Open(bool readonly = true); int Sync(); bool Lock(bool readonly = true); diff --git a/preference/path-internal.cc b/preference/path-internal.cc index 58ce0ff..b387877 100644 --- a/preference/path-internal.cc +++ b/preference/path-internal.cc @@ -14,31 +14,44 @@ * limitations under the License. */ -#include +#include "preference/path-internal.hh" + +#include #include +#include #include #include #include "preference/log-internal.hh" -#include "preference/path-internal.hh" namespace preference { namespace internal { std::string Path::Get() { - static std::mutex mtx; + static std::mutex mutex; static std::string path; if (!path.empty()) return path; - std::lock_guard lock(mtx); - char* data_path = app_get_data_path(); - if (data_path != nullptr) { - path = std::string(data_path) + ".pref/"; - free(data_path); + std::lock_guard lock(mutex); + const char* data_path = aul_get_app_data_path(); + if (data_path == nullptr) { + _E("Failed to get data path. uid(%u)", getuid()); + if (getuid() < 5000) { + path = "/tmp/." + std::to_string(getpid()) + "_pref/"; + } else { + char pkgid[256] = { 0, }; + if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) != AUL_R_OK) { + _E("Failed to get pkgid by pid(%d)", getpid()); + return path; + } + + path = std::string(tzplatform_getenv(TZ_USER_APP)) + "/" + + std::string(pkgid) + "/data/.pref/"; + } } else { - path = "/tmp/." + std::to_string(getpid()) + "_pref/"; + path = std::string(data_path) + ".pref/"; } return path;