[Filesystem] Unlink file
authorWojciech Kosowicz <w.kosowicz@samsung.com>
Tue, 17 Feb 2015 12:48:32 +0000 (13:48 +0100)
committerWojciech Kosowicz <w.kosowicz@samsung.com>
Tue, 24 Feb 2015 08:00:24 +0000 (09:00 +0100)
Change-Id: I7856188132ba7fce9699a8e8169e2882c6026333
Signed-off-by: Wojciech Kosowicz <w.kosowicz@samsung.com>
src/filesystem/filesystem_instance.cc
src/filesystem/filesystem_instance.h
src/filesystem/filesystem_manager.cc
src/filesystem/filesystem_manager.h

index d326344a38972b329a6ec7078b51c004a04f7f32..e7088d32ff822d43ae87fd61372524bc7adc2ac9 100644 (file)
@@ -41,6 +41,7 @@ FilesystemInstance::FilesystemInstance() {
   REGISTER_ASYNC("FileSystemManager_mkdir", FileSystemManagerMakeDirectory);
   REGISTER_SYNC("FileSystemManager_mkdirSync",
                 FileSystemManagerMakeDirectorySync);
+  REGISTER_ASYNC("File_unlinkFile", UnlinkFile);
 #undef REGISTER_SYNC
 #undef REGISTER_ASYNC
 }
@@ -283,6 +284,38 @@ void FilesystemInstance::ReadDir(const picojson::value& args,
       &FilesystemManager::ReadDir, &fm, pathToDir, onSuccess, onError));
 }
 
+void FilesystemInstance::UnlinkFile(const picojson::value& args,
+                                  picojson::object& out) {
+  LoggerD("enter");
+  CHECK_EXIST(args, "pathToFile", out)
+
+  double callback_id = args.get("callbackId").get<double>();
+  const std::string& pathToFile = args.get("pathToFile").get<std::string>();
+
+  auto onSuccess = [this, callback_id]() {
+    LoggerD("enter");
+    picojson::value result = picojson::value();
+    picojson::value response = picojson::value(picojson::object());
+    picojson::object& obj = response.get<picojson::object>();
+    obj["callbackId"] = picojson::value(callback_id);
+    ReportSuccess(result, obj);
+    PostMessage(response.serialize().c_str());
+  };
+
+  auto onError = [this, callback_id](FilesystemError e) {
+    LoggerD("enter");
+    picojson::value response = picojson::value(picojson::object());
+    picojson::object& obj = response.get<picojson::object>();
+    obj["callbackId"] = picojson::value(callback_id);
+    PrepareError(e, obj);
+    PostMessage(response.serialize().c_str());
+  };
+
+  FilesystemManager& fm = FilesystemManager::GetInstance();
+  common::TaskQueue::GetInstance().Async(std::bind(
+      &FilesystemManager::UnlinkFile, &fm, pathToFile, onSuccess, onError));
+}
+
 void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::object& out)
 {
   LoggerD("enter");
index fc5a342bf26470193473df062b9ca39b2752ad05..162d77a742f8d5abebdc567ba88615f2972f7436 100644 (file)
@@ -30,6 +30,7 @@ class FilesystemInstance : public common::ParsedInstance {
   void FileSystemManagerMakeDirectorySync(const picojson::value& args,
                                           picojson::object& out);
   void ReadDir(const picojson::value& args, picojson::object& out);
+  void UnlinkFile(const picojson::value& args, picojson::object& out);
   void PrepareError(const FilesystemError& error, picojson::object& out);
 };
 
index 4121811b059e826bc854ea260264ea2506d40273..66676717697d61937c21875f35ae38c59b3ab42c 100644 (file)
@@ -252,5 +252,17 @@ void FilesystemManager::ReadDir(
     return;
   }
 }
+
+void FilesystemManager::UnlinkFile(
+        const std::string& path,
+        const std::function<void()>& success_cb,
+        const std::function<void(FilesystemError)>& error_cb) {
+  if (unlink(path.c_str()) != 0) {
+      LoggerE("Error occured while deleting file");
+      error_cb(FilesystemError::Other);
+      return;
+  }
+  success_cb();
+}
 }  // namespace filesystem
 }  // namespace extension
index 175c04f7854e2dd9a9a313dcda820c24e587c7fc..30b5420fd3add4476becbe93ab69ad45221be8b9 100644 (file)
@@ -23,6 +23,11 @@ class FilesystemManager {
  public:
   static FilesystemManager& GetInstance();
 
+  void UnlinkFile(
+          const std::string& path,
+          const std::function<void()>& success_cb,
+          const std::function<void(FilesystemError)>& error_cb);
+
   void StatPath(const std::string& path,
                 const std::function<void(const FilesystemStat&)>& success_cb,
                 const std::function<void(FilesystemError)>& error_cb);