[Filesystem] RemoveDirectory
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:55 +0000 (09:00 +0100)
Change-Id: Id66f138bc69b175afa284168b8617df2a52f69a6
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 e7088d32ff822d43ae87fd61372524bc7adc2ac9..9da40b8fa1eb3d8f6f89088f1d4b268db8733987 100644 (file)
@@ -42,6 +42,7 @@ FilesystemInstance::FilesystemInstance() {
   REGISTER_SYNC("FileSystemManager_mkdirSync",
                 FileSystemManagerMakeDirectorySync);
   REGISTER_ASYNC("File_unlinkFile", UnlinkFile);
+  REGISTER_ASYNC("File_removeDirectory", RemoveDirectory);
 #undef REGISTER_SYNC
 #undef REGISTER_ASYNC
 }
@@ -316,6 +317,38 @@ void FilesystemInstance::UnlinkFile(const picojson::value& args,
       &FilesystemManager::UnlinkFile, &fm, pathToFile, onSuccess, onError));
 }
 
+void FilesystemInstance::RemoveDirectory(const picojson::value& args,
+                                  picojson::object& out) {
+  LoggerD("enter");
+  CHECK_EXIST(args, "pathToDelete", out)
+
+  double callback_id = args.get("callbackId").get<double>();
+  const std::string& pathToDelete = args.get("pathToDelete").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::RemoveDirectory, &fm, pathToDelete, onSuccess, onError));
+}
+
 void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::object& out)
 {
   LoggerD("enter");
@@ -344,6 +377,7 @@ void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::ob
   }
 }
 
+
 #undef CHECK_EXIST
 
 }  // namespace filesystem
index 162d77a742f8d5abebdc567ba88615f2972f7436..0b6fc86fab68ddf0f3175b06b9ab11fd5c5c46c8 100644 (file)
@@ -31,6 +31,7 @@ class FilesystemInstance : public common::ParsedInstance {
                                           picojson::object& out);
   void ReadDir(const picojson::value& args, picojson::object& out);
   void UnlinkFile(const picojson::value& args, picojson::object& out);
+  void RemoveDirectory(const picojson::value& args, picojson::object& out);
   void PrepareError(const FilesystemError& error, picojson::object& out);
 };
 
index 66676717697d61937c21875f35ae38c59b3ab42c..b06e45b2e9b6c27fef469ed35eea690283ae7a02 100644 (file)
@@ -13,6 +13,9 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#define _XOPEN_SOURCE 500
+#include <ftw.h>
+#undef _XOPEN_SOURCE
 
 #include "common/logger.h"
 #include "common/scope_exit.h"
@@ -23,6 +26,14 @@ namespace filesystem {
 
 namespace {
 
+int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
+{
+  int result = remove(fpath);
+  if (result)
+    LoggerE("error occured");
+  return result;
+}
+
 bool fetch_storages_cb(int storage_id,
                        storage_type_e type,
                        storage_state_e state,
@@ -264,5 +275,18 @@ void FilesystemManager::UnlinkFile(
   }
   success_cb();
 }
+
+void FilesystemManager::RemoveDirectory(
+        const std::string& path,
+        const std::function<void()>& success_cb,
+        const std::function<void(FilesystemError)>& error_cb) {
+  const int maxDirOpened = 64;
+  if (nftw(path.c_str(), unlink_cb, maxDirOpened, FTW_DEPTH | FTW_PHYS) != 0) {
+    LoggerE("Error occured");
+    error_cb(FilesystemError::Other);
+  }
+  success_cb();
+  return;
+}
 }  // namespace filesystem
 }  // namespace extension
index 30b5420fd3add4476becbe93ab69ad45221be8b9..cffae4a59a5439064239b628a4cecac17224b654 100644 (file)
@@ -57,6 +57,11 @@ class FilesystemManager {
           const std::string& path,
           const std::function<void(const std::vector<std::string>&)>& success_cb,
           const std::function<void(FilesystemError)>& error_cb);
+
+  void RemoveDirectory(
+          const std::string& path,
+          const std::function<void()>& success_cb,
+          const std::function<void(FilesystemError)>& error_cb);
 };
 }  // namespace filesystem
 }  // namespace extension