Create .pref directory 35/259235/6
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 3 Jun 2021 00:51:37 +0000 (09:51 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 3 Jun 2021 02:34:43 +0000 (11:34 +0900)
The application can delete the .pref directory.
This patch always checks and creates the .pref directory for
backward compatibility.

Change-Id: I87ecc4bf09dd78d88df3a138c5ec36b5a0f90178
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
preference/file-internal.cc
preference/file-internal.hh
preference/path-internal.cc

index dd653ca867834af7b97bb2912adbbf9d81a10efe..6627aad24cdd9b93c47d0395c5d9085daa184c7a 100644 (file)
@@ -166,14 +166,25 @@ bool File::IsExisting() {
   return false;
 }
 
+void File::CreateDir() {
+  std::string pref_dir = Path::Get();
+  if (access(pref_dir.c_str(), F_OK) != 0) {
+    mode_t dir_mode = 0664 | 0111;
+    if (mkdir(pref_dir.c_str(), dir_mode) != 0)
+      _E("mkdir() is failed. path(%s), errno(%d)", pref_dir.c_str(), errno);
+  }
+}
+
 int File::Open(bool readonly) {
   if (readonly)
     fd_ = open(path_.c_str(), O_RDONLY);
   else
     fd_ = open(path_.c_str(), (O_CREAT | O_WRONLY | O_TRUNC), 0644);
   if (fd_ < 0) {
-    _E("Failed to open %s. errno(%d)", path_.c_str(), errno);
-    return -1;
+    int ret = -errno;
+    if (ret != -ENOENT)
+      _E("Failed to open %s. errno(%d)", path_.c_str(), errno);
+    return ret;
   }
 
   return 0;
@@ -223,8 +234,11 @@ void File::Unlock() {
 
 int File::TryWrite() {
   int ret = Open(false);
-  if (ret < 0)
+  if (ret < 0) {
+    if (ret == -ENOENT)
+      CreateDir();
     return static_cast<int>(Error::ERROR_FILE_OPEN);
+  }
 
   if (!Lock(false)) {
     Close();
index 0386cece6880599b9c8442df2b0fd062c3fc2b35..c455c2613f127c22d3a674478c4fe1c7fb439947 100644 (file)
@@ -61,6 +61,7 @@ class File {
   void Close();
 
  private:
+  void CreateDir();
   int Open(bool readonly = true);
   int Sync();
   bool Lock(bool readonly = true);
index d6c63e8342b987e0d0245d5a7d5a87b1a7ada68f..58ce0fff4c171c5badb08a57f51ea1fc2e97bea3 100644 (file)
  */
 
 #include <app_common.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <mutex>
+
 #include "preference/log-internal.hh"
 #include "preference/path-internal.hh"
 
@@ -28,10 +27,12 @@ namespace preference {
 namespace internal {
 
 std::string Path::Get() {
+  static std::mutex mtx;
   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/";
@@ -40,15 +41,6 @@ std::string Path::Get() {
     path = "/tmp/." + std::to_string(getpid()) + "_pref/";
   }
 
-  if (access(path.c_str(), F_OK) != 0) {
-    int ret = mkdir(path.c_str(),
-        (S_IRWXU | S_IRGRP | S_IXGRP | S_ISGID | S_IROTH | S_IXOTH));
-    if (ret != 0) {
-      _E("mkdir(%s) is failed. errno(%d)", path.c_str(), errno);
-      path = "";
-    }
-  }
-
   return path;
 }