From: Wojciech Kosowicz Date: Tue, 17 Feb 2015 12:48:32 +0000 (+0100) Subject: [Filesystem] RemoveDirectory X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~375 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=204b5b96e44ab4e01ae58f84ee2cb762e583d3a8;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] RemoveDirectory Change-Id: Id66f138bc69b175afa284168b8617df2a52f69a6 Signed-off-by: Wojciech Kosowicz --- diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index e7088d32..9da40b8f 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -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(); + const std::string& pathToDelete = args.get("pathToDelete").get(); + + auto onSuccess = [this, callback_id]() { + LoggerD("enter"); + picojson::value result = picojson::value(); + picojson::value response = picojson::value(picojson::object()); + picojson::object& obj = response.get(); + 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(); + 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 diff --git a/src/filesystem/filesystem_instance.h b/src/filesystem/filesystem_instance.h index 162d77a7..0b6fc86f 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -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); }; diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 66676717..b06e45b2 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -13,6 +13,9 @@ #include #include #include +#define _XOPEN_SOURCE 500 +#include +#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& success_cb, + const std::function& 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 diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index 30b5420f..cffae4a5 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -57,6 +57,11 @@ class FilesystemManager { const std::string& path, const std::function&)>& success_cb, const std::function& error_cb); + + void RemoveDirectory( + const std::string& path, + const std::function& success_cb, + const std::function& error_cb); }; } // namespace filesystem } // namespace extension