[Filesystem] ReadDir native implementation
authorWojciech Kosowicz <w.kosowicz@samsung.com>
Tue, 17 Feb 2015 12:26:43 +0000 (13:26 +0100)
committerWojciech Kosowicz <w.kosowicz@samsung.com>
Tue, 24 Feb 2015 07:59:54 +0000 (08:59 +0100)
Change-Id: I5d7be72190aae37cf0200a2eeb980cecbe1f8d16
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 1140fd9c3b97cc770bcfe47b035f2f83f62fd20b..d326344a38972b329a6ec7078b51c004a04f7f32 100644 (file)
@@ -33,6 +33,7 @@ FilesystemInstance::FilesystemInstance() {
   REGISTER_ASYNC("File_stat", FileStat);
   REGISTER_SYNC("File_statSync", FileStatSync);
   REGISTER_SYNC("File_createSync", FileCreateSync);
+  REGISTER_ASYNC("File_readDir", ReadDir);
   REGISTER_ASYNC("File_rename", FileRename);
   REGISTER_SYNC("Filesystem_getWidgetPaths", FilesystemGetWidgetPaths);
   REGISTER_SYNC("FileSystemManager_fetchStorages",
@@ -244,6 +245,44 @@ void FilesystemInstance::FileSystemManagerMakeDirectorySync(
   FilesystemManager::GetInstance().MakeDirectory(location, onResult);
 }
 
+void FilesystemInstance::ReadDir(const picojson::value& args,
+                                  picojson::object& out) {
+  LoggerD("enter");
+  CHECK_EXIST(args, "pathToDir", out)
+  CHECK_EXIST(args, "callbackId", out)
+
+  double callback_id = args.get("callbackId").get<double>();
+  const std::string& pathToDir = args.get("pathToDir").get<std::string>();
+
+  auto onSuccess = [this, callback_id](const std::vector<std::string>& paths) {
+    LoggerD("enter");
+    picojson::value result = picojson::value(picojson::array());;
+    picojson::array& statPaths = result.get<picojson::array>();
+    picojson::value response = picojson::value(picojson::object());
+    picojson::object& obj = response.get<picojson::object>();
+    obj["callbackId"] = picojson::value(callback_id);
+    for(auto path : paths) {
+      FilesystemStat stat = FilesystemStat::getStat(path);
+      statPaths.push_back(stat.toJSON());
+    }
+    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::ReadDir, &fm, pathToDir, onSuccess, onError));
+}
+
 void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::object& out)
 {
   LoggerD("enter");
index 889591b79c603d817cf827839c1e73c7c70a78c7..fc5a342bf26470193473df062b9ca39b2752ad05 100644 (file)
@@ -29,6 +29,7 @@ class FilesystemInstance : public common::ParsedInstance {
                                       picojson::object& out);
   void FileSystemManagerMakeDirectorySync(const picojson::value& args,
                                           picojson::object& out);
+  void ReadDir(const picojson::value& args, picojson::object& out);
   void PrepareError(const FilesystemError& error, picojson::object& out);
 };
 
index 455e262eea1753b15f1d9f859fd82faf46e0973b..4121811b059e826bc854ea260264ea2506d40273 100644 (file)
@@ -8,6 +8,7 @@
 #include <package_manager.h>
 #include <storage-expand.h>
 #include <storage.h>
+#include <dirent.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -219,5 +220,37 @@ void FilesystemManager::Rename(
     error_cb(FilesystemError::Other);
   }
 }
+
+void FilesystemManager::ReadDir(
+        const std::string& path,
+        const std::function<void(const std::vector<std::string>&)>& success_cb,
+        const std::function<void(FilesystemError)>& error_cb) {
+  LoggerD("entered");
+
+  std::vector<std::string> fileList;
+  DIR *dp = nullptr;
+  struct dirent entry;
+  struct dirent *result = nullptr;
+  int status = 0;
+
+  dp = opendir(path.c_str());
+  if (dp != NULL) {
+    while ((status = readdir_r(dp, &entry, &result)) == 0 && result != nullptr) {
+        if (strcmp(result->d_name, ".") != 0 && strcmp(result->d_name, "..") != 0)
+          fileList.push_back(path + "/" + std::string(result->d_name));
+    }
+    (void)closedir(dp);
+    if (status == 0) {
+      success_cb(fileList);
+    } else {
+      LoggerE("error occured");
+      error_cb(FilesystemError::Other);
+    }
+  } else {
+    LoggerE("Couldn't open the directory");
+    error_cb(FilesystemError::Other);
+    return;
+  }
+}
 }  // namespace filesystem
 }  // namespace extension
index 02a472c219d19f7bc08a2dee51f88a41ada6b38e..175c04f7854e2dd9a9a313dcda820c24e587c7fc 100644 (file)
@@ -47,6 +47,11 @@ class FilesystemManager {
 
   void MakeDirectory(const std::string& path,
                      const std::function<void(FilesystemError)>& result_cb);
+  
+  void ReadDir(
+          const std::string& path,
+          const std::function<void(const std::vector<std::string>&)>& success_cb,
+          const std::function<void(FilesystemError)>& error_cb);
 };
 }  // namespace filesystem
 }  // namespace extension