From: Kamil Lysik Date: Mon, 16 Feb 2015 15:23:17 +0000 (+0100) Subject: [Filesystem] FileSystemManager_mkdir native API X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~378 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f12c1e3b17fa3a761c50599af8393ed70a7a2906;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] FileSystemManager_mkdir native API Added API to create directory. This API create full path when it is required. There are 2 versions: sync and async. Change-Id: I81483c15bbd85d5501c3fd65b3f0399f3e2ad831 Signed-off-by: Kamil Lysik Signed-off-by: Pawel Sikorski --- diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index efcdfa09..1140fd9c 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -37,6 +37,9 @@ FilesystemInstance::FilesystemInstance() { REGISTER_SYNC("Filesystem_getWidgetPaths", FilesystemGetWidgetPaths); REGISTER_SYNC("FileSystemManager_fetchStorages", FileSystemManagerFetchStorages); + REGISTER_ASYNC("FileSystemManager_mkdir", FileSystemManagerMakeDirectory); + REGISTER_SYNC("FileSystemManager_mkdirSync", + FileSystemManagerMakeDirectorySync); #undef REGISTER_SYNC #undef REGISTER_ASYNC } @@ -199,6 +202,48 @@ void FilesystemInstance::FileSystemManagerFetchStorages( FilesystemManager::GetInstance().FetchStorages(onSuccess, onError); } +void FilesystemInstance::FileSystemManagerMakeDirectory( + const picojson::value& args, + picojson::object& out) { + LoggerD("enter"); + CHECK_EXIST(args, "callbackId", out) + CHECK_EXIST(args, "location", out) + + double callback_id = args.get("callbackId").get(); + const std::string& location = args.get("location").get(); + + auto onResult = [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()); + }; + + auto onAction = [location, onResult]() { + FilesystemManager::GetInstance().MakeDirectory(location, onResult); + }; + + common::TaskQueue::GetInstance().Async(onAction); +} + +void FilesystemInstance::FileSystemManagerMakeDirectorySync( + const picojson::value& args, + picojson::object& out) { + LoggerD("enter"); + CHECK_EXIST(args, "location", out) + + const std::string& location = args.get("location").get(); + + auto onResult = [&](FilesystemError e) { + LoggerD("enter"); + PrepareError(e, out); + }; + + FilesystemManager::GetInstance().MakeDirectory(location, onResult); +} + void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::object& out) { LoggerD("enter"); @@ -209,6 +254,12 @@ void FilesystemInstance::PrepareError(const FilesystemError& error, picojson::ob case FilesystemError::NotFound: ReportError(NotFoundException("PLATFORM ERROR"), out); break; + case FilesystemError::FileExists: + ReportError(IOException("File already exists"), out); + break; + case FilesystemError::DirectoryExists: + ReportError(IOException("Directory already exists"), out); + break; case FilesystemError::PermissionDenied: ReportError(IOException("Permission denied"), out); break; diff --git a/src/filesystem/filesystem_instance.h b/src/filesystem/filesystem_instance.h index c2eb6120..889591b7 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -25,7 +25,10 @@ class FilesystemInstance : public common::ParsedInstance { picojson::object& out); void FileSystemManagerFetchStorages(const picojson::value& args, picojson::object& out); - + void FileSystemManagerMakeDirectory(const picojson::value& args, + picojson::object& out); + void FileSystemManagerMakeDirectorySync(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 db130a6b..455e262e 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include "common/logger.h" #include "common/scope_exit.h" @@ -36,6 +38,35 @@ bool fetch_storages_cb(int storage_id, result->push_back(FilesystemStorage(storage_id, type, state, path)); return true; } + +FilesystemError make_directory_worker(const std::string& path) { + LoggerD("enter: %s", path.c_str()); + auto fsstat = FilesystemStat::getStat(path); + if (fsstat.valid) { + if (fsstat.isDirectory) { + LoggerD("Directory exists"); + return FilesystemError::DirectoryExists; + } else { + LoggerD("It is a file and exists"); + return FilesystemError::FileExists; + } + } + + std::string parent_path = FilesystemUtils::get_dirname(path); + auto parent_result = make_directory_worker(parent_path); + + if (parent_result == FilesystemError::DirectoryExists) { + LoggerD("Creating directrory: %s", path.c_str()); + mode_t create_mode = S_IRWXU | S_IRWXG | S_IRWXO; + int r = mkdir(path.c_str(), create_mode); + if (r == 0) { + return FilesystemError::DirectoryExists; + } + LoggerD("Cannot create directory: %s", strerror(errno)); + return FilesystemError::Other; + } + return parent_result; +} } // namespace void FilesystemManager::FetchStorages( @@ -161,6 +192,13 @@ void FilesystemManager::CreateFile( } } +void FilesystemManager::MakeDirectory( + const std::string& path, + const std::function& result_cb) { + LoggerD("enter"); + result_cb(make_directory_worker(path)); +} + void FilesystemManager::Rename( const std::string& oldPath, const std::string& newPath, diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index db82462a..02a472c2 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -44,6 +44,9 @@ class FilesystemManager { const std::string& newPath, const std::function& success_cb, const std::function& error_cb); + + void MakeDirectory(const std::string& path, + const std::function& result_cb); }; } // namespace filesystem } // namespace extension diff --git a/src/filesystem/filesystem_utils.cc b/src/filesystem/filesystem_utils.cc index c6bc60d1..32dfefeb 100644 --- a/src/filesystem/filesystem_utils.cc +++ b/src/filesystem/filesystem_utils.cc @@ -4,6 +4,7 @@ #include "filesystem_utils.h" +#include #include "common/logger.h" namespace std { @@ -50,4 +51,16 @@ std::string get_storage_dir_path(int id, storage_directory_e typeToCheck) { free(platformPath); return path; } + +std::string get_dirname(const std::string& path) { + // dirname will modify content: pass a copy + std::string buf = path.c_str(); + return std::string(dirname(const_cast(buf.c_str()))); +} + +std::string get_basename(const std::string& path) { + // basename will modify content: pass a copy + std::string buf = path.c_str(); + return std::string(basename(const_cast(buf.c_str()))); +} } diff --git a/src/filesystem/filesystem_utils.h b/src/filesystem/filesystem_utils.h index e83ae406..2de420b1 100644 --- a/src/filesystem/filesystem_utils.h +++ b/src/filesystem/filesystem_utils.h @@ -15,6 +15,8 @@ namespace filesystem { enum class FilesystemError { None, NotFound, + FileExists, + DirectoryExists, PermissionDenied, Other }; @@ -35,6 +37,9 @@ namespace FilesystemUtils { * */ std::string get_storage_dir_path(int id, storage_directory_e typeToCheck); + +std::string get_dirname(const std::string& path); +std::string get_basename(const std::string& path); } #endif // FILESYSTEM_FILESYSTEM_UTILS_H