From: Wojciech Kosowicz Date: Tue, 17 Feb 2015 12:26:43 +0000 (+0100) Subject: [Filesystem] ReadDir native implementation X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~377 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b08637c2d8bffb51beaa58b9d6d26abfca575683;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] ReadDir native implementation Change-Id: I5d7be72190aae37cf0200a2eeb980cecbe1f8d16 Signed-off-by: Wojciech Kosowicz --- diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 1140fd9c..d326344a 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -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(); + const std::string& pathToDir = args.get("pathToDir").get(); + + auto onSuccess = [this, callback_id](const std::vector& paths) { + LoggerD("enter"); + picojson::value result = picojson::value(picojson::array());; + picojson::array& statPaths = result.get(); + picojson::value response = picojson::value(picojson::object()); + picojson::object& obj = response.get(); + 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(); + 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"); diff --git a/src/filesystem/filesystem_instance.h b/src/filesystem/filesystem_instance.h index 889591b7..fc5a342b 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -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); }; diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 455e262e..4121811b 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -219,5 +220,37 @@ void FilesystemManager::Rename( error_cb(FilesystemError::Other); } } + +void FilesystemManager::ReadDir( + const std::string& path, + const std::function&)>& success_cb, + const std::function& error_cb) { + LoggerD("entered"); + + std::vector 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 diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index 02a472c2..175c04f7 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -47,6 +47,11 @@ class FilesystemManager { void MakeDirectory(const std::string& path, const std::function& result_cb); + + void ReadDir( + const std::string& path, + const std::function&)>& success_cb, + const std::function& error_cb); }; } // namespace filesystem } // namespace extension