[Filesystem] File_createSync native API
authorKamil Lysik <k.lysik@samsung.com>
Tue, 17 Feb 2015 11:42:21 +0000 (12:42 +0100)
committerKamil Lysik <k.lysik@samsung.com>
Mon, 23 Feb 2015 14:39:21 +0000 (15:39 +0100)
File_createSync attempts to create new file.
If file exists, then file is preserved.

Change-Id: Ie4262504be19d61dd3f500608e02ab6fe2b36163
Signed-off-by: Kamil Lysik <k.lysik@samsung.com>
src/filesystem/filesystem_instance.cc
src/filesystem/filesystem_instance.h
src/filesystem/filesystem_manager.cc
src/filesystem/filesystem_manager.h

index 23f6dfddc5ce8206ba0f000c04bbf51383e40db2..b09171080649e6943cdc2c25be842745d2ee25c2 100644 (file)
@@ -32,6 +32,7 @@ FilesystemInstance::FilesystemInstance() {
   RegisterHandler(c, std::bind(&FilesystemInstance::x, this, _1, _2));
   REGISTER_ASYNC("File_stat", FileStat);
   REGISTER_SYNC("File_statSync", FileStatSync);
+  REGISTER_SYNC("File_createSync", FileCreateSync);
   REGISTER_SYNC("Filesystem_getWidgetPaths", FilesystemGetWidgetPaths);
   REGISTER_SYNC("FileSystemManager_fetchStorages",
                 FileSystemManagerFetchStorages);
@@ -47,6 +48,26 @@ FilesystemInstance::~FilesystemInstance() {}
     return;                                                                \
   }
 
+void FilesystemInstance::FileCreateSync(const picojson::value& args, picojson::object& out)
+{
+  LoggerD("enter");
+  CHECK_EXIST(args, "location", out)
+
+  const std::string& location = args.get("location").get<std::string>();
+
+  auto onSuccess = [&](const FilesystemStat& data) {
+    LoggerD("enter");
+    ReportSuccess(data.toJSON(), out);
+  };
+
+  auto onError = [&](FilesystemError e) {
+    LoggerD("enter");
+    PrepareError(e, out);
+  };
+
+  FilesystemManager::GetInstance().CreateFile(location, onSuccess, onError);
+}
+
 void FilesystemInstance::FileStat(const picojson::value& args,
                                   picojson::object& out) {
   LoggerD("enter");
index 94ee6702cceb20ad8ed12dfc6a63ff0c92102da2..8eab6fcf6b70da35a2225fb74e7789ade999d9d0 100644 (file)
@@ -17,6 +17,7 @@ class FilesystemInstance : public common::ParsedInstance {
   virtual ~FilesystemInstance();
 
  private:
+  void FileCreateSync(const picojson::value& args, picojson::object& out);
   void FileStat(const picojson::value& args, picojson::object& out);
   void FileStatSync(const picojson::value& args, picojson::object& out);
   void FilesystemGetWidgetPaths(const picojson::value& args,
index 6b2585935c860cad5e55db81848482df482e1000..ebe15b1eb788f565cc9977e93c0b39085e8c22ec 100644 (file)
@@ -8,6 +8,7 @@
 #include <package_manager.h>
 #include <storage-expand.h>
 #include <storage.h>
+#include <fcntl.h>
 
 #include "common/logger.h"
 #include "common/scope_exit.h"
@@ -132,5 +133,31 @@ void FilesystemManager::GetWidgetPaths(
   result["wgt-private-tmp"] = app_root + "/tmp";
   success_cb(result);
 }
+
+void FilesystemManager::CreateFile(
+    const std::string& path,
+    const std::function<void(const FilesystemStat&)>& success_cb,
+    const std::function<void(FilesystemError)>& error_cb) {
+  const mode_t create_mode = S_IRWXU | S_IRWXG | S_IRWXO;
+  int status;
+  status =
+      TEMP_FAILURE_RETRY(open(path.c_str(), O_RDWR | O_CREAT, create_mode));
+  if (-1 == status) {
+    LoggerE("Cannot create or open file %s: %s", path.c_str(), strerror(errno));
+    error_cb(FilesystemError::Other);
+  }
+  status = close(status);
+  if (0 != status) {
+    LoggerE("Cannot close file %s: %s", path.c_str(), strerror(errno));
+    error_cb(FilesystemError::Other);
+  }
+  FilesystemStat stat = FilesystemStat::getStat(path);
+  if (stat.valid) {
+    success_cb(stat);
+  } else {
+    LoggerE("Cannot create stat data!");
+    error_cb(FilesystemError::Other);
+  }
 }
-}
+}  // namespace filesystem
+}  // namespace extension
index 9702820440a227ea6ecb6a43dbc20691bac1c1d9..82b049acd23c05b362b5dd24a1b168cf00515247 100644 (file)
@@ -35,6 +35,10 @@ class FilesystemManager {
       const std::function<void(const std::map<std::string, std::string>&)>&
           success_cb,
       const std::function<void(FilesystemError)>& error_cb);
+
+  void CreateFile(const std::string& path,
+                  const std::function<void(const FilesystemStat&)>& success_cb,
+                  const std::function<void(FilesystemError)>& error_cb);
 };
 }  // namespace filesystem
 }  // namespace extension