From 7d533fdf0b45994a33f144e09b75b6ce1381c38f Mon Sep 17 00:00:00 2001 From: Wojciech Kosowicz Date: Tue, 17 Feb 2015 13:48:32 +0100 Subject: [PATCH] [Filesystem] Unlink file Change-Id: I7856188132ba7fce9699a8e8169e2882c6026333 Signed-off-by: Wojciech Kosowicz --- src/filesystem/filesystem_instance.cc | 33 +++++++++++++++++++++++++++ src/filesystem/filesystem_instance.h | 1 + src/filesystem/filesystem_manager.cc | 12 ++++++++++ src/filesystem/filesystem_manager.h | 5 ++++ 4 files changed, 51 insertions(+) diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index d326344a..e7088d32 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -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(); + const std::string& pathToFile = args.get("pathToFile").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::UnlinkFile, &fm, pathToFile, 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 fc5a342b..162d77a7 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -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); }; diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 4121811b..66676717 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -252,5 +252,17 @@ void FilesystemManager::ReadDir( return; } } + +void FilesystemManager::UnlinkFile( + const std::string& path, + const std::function& success_cb, + const std::function& 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 diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index 175c04f7..30b5420f 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -23,6 +23,11 @@ class FilesystemManager { public: static FilesystemManager& GetInstance(); + void UnlinkFile( + const std::string& path, + const std::function& success_cb, + const std::function& error_cb); + void StatPath(const std::string& path, const std::function& success_cb, const std::function& error_cb); -- 2.34.1