From 3d71a0611841c6ef30c1c13212ec7e27aab8188c Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Thu, 3 Jun 2021 09:51:37 +0900 Subject: [PATCH] Create .pref directory 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 --- preference/file-internal.cc | 20 +++++++++++++++++--- preference/file-internal.hh | 1 + preference/path-internal.cc | 16 ++++------------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/preference/file-internal.cc b/preference/file-internal.cc index dd653ca..6627aad 100644 --- a/preference/file-internal.cc +++ b/preference/file-internal.cc @@ -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(Error::ERROR_FILE_OPEN); + } if (!Lock(false)) { Close(); diff --git a/preference/file-internal.hh b/preference/file-internal.hh index 0386cec..c455c26 100644 --- a/preference/file-internal.hh +++ b/preference/file-internal.hh @@ -61,6 +61,7 @@ class File { void Close(); private: + void 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 d6c63e8..58ce0ff 100644 --- a/preference/path-internal.cc +++ b/preference/path-internal.cc @@ -15,12 +15,11 @@ */ #include -#include -#include -#include #include #include +#include + #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 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; } -- 2.34.1