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
}
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<double>();
+ const std::string& location = args.get("location").get<std::string>();
+
+ auto onResult = [this, callback_id](FilesystemError e) {
+ LoggerD("enter");
+ picojson::value response = picojson::value(picojson::object());
+ picojson::object& obj = response.get<picojson::object>();
+ 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<std::string>();
+
+ 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");
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;
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);
};
#include <storage.h>
#include <fcntl.h>
#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include "common/logger.h"
#include "common/scope_exit.h"
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(
}
}
+void FilesystemManager::MakeDirectory(
+ const std::string& path,
+ const std::function<void(FilesystemError)>& result_cb) {
+ LoggerD("enter");
+ result_cb(make_directory_worker(path));
+}
+
void FilesystemManager::Rename(
const std::string& oldPath,
const std::string& newPath,
const std::string& newPath,
const std::function<void(const FilesystemStat&)>& success_cb,
const std::function<void(FilesystemError)>& error_cb);
+
+ void MakeDirectory(const std::string& path,
+ const std::function<void(FilesystemError)>& result_cb);
};
} // namespace filesystem
} // namespace extension
#include "filesystem_utils.h"
+#include <libgen.h>
#include "common/logger.h"
namespace std {
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<char*>(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<char*>(buf.c_str())));
+}
}
enum class FilesystemError {
None,
NotFound,
+ FileExists,
+ DirectoryExists,
PermissionDenied,
Other
};
*
*/
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