From: Kamil Lysik Date: Tue, 17 Feb 2015 11:42:21 +0000 (+0100) Subject: [Filesystem] File_createSync native API X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~382^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f0d33870aa4b8c1ccea413be0e77473286877787;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] File_createSync native API File_createSync attempts to create new file. If file exists, then file is preserved. Change-Id: Ie4262504be19d61dd3f500608e02ab6fe2b36163 Signed-off-by: Kamil Lysik --- diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 23f6dfdd..b0917108 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -32,6 +32,7 @@ FilesystemInstance::FilesystemInstance() { RegisterHandler(c, std::bind(&FilesystemInstance::x, this, _1, _2)); REGISTER_ASYNC("File_stat", FileStat); REGISTER_SYNC("File_statSync", FileStatSync); + REGISTER_SYNC("File_createSync", FileCreateSync); REGISTER_SYNC("Filesystem_getWidgetPaths", FilesystemGetWidgetPaths); REGISTER_SYNC("FileSystemManager_fetchStorages", FileSystemManagerFetchStorages); @@ -47,6 +48,26 @@ FilesystemInstance::~FilesystemInstance() {} return; \ } +void FilesystemInstance::FileCreateSync(const picojson::value& args, picojson::object& out) +{ + LoggerD("enter"); + CHECK_EXIST(args, "location", out) + + const std::string& location = args.get("location").get(); + + auto onSuccess = [&](const FilesystemStat& data) { + LoggerD("enter"); + ReportSuccess(data.toJSON(), out); + }; + + auto onError = [&](FilesystemError e) { + LoggerD("enter"); + PrepareError(e, out); + }; + + FilesystemManager::GetInstance().CreateFile(location, onSuccess, onError); +} + void FilesystemInstance::FileStat(const picojson::value& args, picojson::object& out) { LoggerD("enter"); diff --git a/src/filesystem/filesystem_instance.h b/src/filesystem/filesystem_instance.h index 94ee6702..8eab6fcf 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -17,6 +17,7 @@ class FilesystemInstance : public common::ParsedInstance { virtual ~FilesystemInstance(); private: + void FileCreateSync(const picojson::value& args, picojson::object& out); void FileStat(const picojson::value& args, picojson::object& out); void FileStatSync(const picojson::value& args, picojson::object& out); void FilesystemGetWidgetPaths(const picojson::value& args, diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 6b258593..ebe15b1e 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include "common/logger.h" #include "common/scope_exit.h" @@ -132,5 +133,31 @@ void FilesystemManager::GetWidgetPaths( result["wgt-private-tmp"] = app_root + "/tmp"; success_cb(result); } + +void FilesystemManager::CreateFile( + const std::string& path, + const std::function& success_cb, + const std::function& error_cb) { + const mode_t create_mode = S_IRWXU | S_IRWXG | S_IRWXO; + int status; + status = + TEMP_FAILURE_RETRY(open(path.c_str(), O_RDWR | O_CREAT, create_mode)); + if (-1 == status) { + LoggerE("Cannot create or open file %s: %s", path.c_str(), strerror(errno)); + error_cb(FilesystemError::Other); + } + status = close(status); + if (0 != status) { + LoggerE("Cannot close file %s: %s", path.c_str(), strerror(errno)); + error_cb(FilesystemError::Other); + } + FilesystemStat stat = FilesystemStat::getStat(path); + if (stat.valid) { + success_cb(stat); + } else { + LoggerE("Cannot create stat data!"); + error_cb(FilesystemError::Other); + } } -} +} // namespace filesystem +} // namespace extension diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index 97028204..82b049ac 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -35,6 +35,10 @@ class FilesystemManager { const std::function&)>& success_cb, const std::function& error_cb); + + void CreateFile(const std::string& path, + const std::function& success_cb, + const std::function& error_cb); }; } // namespace filesystem } // namespace extension