Modify Path::Get() method 79/306479/3
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 22 Feb 2024 03:54:37 +0000 (12:54 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 22 Feb 2024 04:02:08 +0000 (04:02 +0000)
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 <h.jhun@samsung.com>
CMakeLists.txt
packaging/capi-appfw-preference.spec
preference/CMakeLists.txt
preference/file-internal.cc
preference/file-internal.hh
preference/path-internal.cc

index 5c6cb94f0b6168ad03c5f2eac25efa5bd1e7e984..38769a9dd0edbbe4a07dcf4981bc3bf7149e4631 100644 (file)
@@ -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)
index efdac9daf95d7f7adbf896b3cafa11d14b607647..7e43110c3e74146c74c45f6485b59a59c126c7bb 100644 (file)
@@ -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
index fce886054bd59513c60196908820c252c6d400dc..eec9512a84de0c3a30a2e0de936cb2adef2efbfa 100644 (file)
@@ -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}
index 6b1040da68e7f31867194c182846c82da5baffeb..b32e832015277853add78ddb633349a78acf9462 100644 (file)
@@ -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<int>(Error::ERROR_FILE_OPEN);
   }
 
index 3d2183a3034f7ead0e3cc64532a212c3957db5f1..62085464f982ce672fb37bc5c621e3b998d521e4 100644 (file)
@@ -20,6 +20,7 @@
 #include <list>
 #include <memory>
 #include <string>
+#include <vector>
 
 #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);
index 58ce0fff4c171c5badb08a57f51ea1fc2e97bea3..b387877c05da27599605f6d8d8ba328b2da461d6 100644 (file)
  * limitations under the License.
  */
 
-#include <app_common.h>
+#include "preference/path-internal.hh"
+
+#include <aul.h>
 #include <sys/types.h>
+#include <tzplatform_config.h>
 #include <unistd.h>
 
 #include <mutex>
 
 #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<std::mutex> 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<std::mutex> 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;