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;
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();
*/
#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"
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/";
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;
}