diff options
Diffstat (limited to 'mobile_src/Filesystem/FilesystemUtils.cpp')
-rw-r--r-- | mobile_src/Filesystem/FilesystemUtils.cpp | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/mobile_src/Filesystem/FilesystemUtils.cpp b/mobile_src/Filesystem/FilesystemUtils.cpp new file mode 100644 index 0000000..40f00ad --- /dev/null +++ b/mobile_src/Filesystem/FilesystemUtils.cpp @@ -0,0 +1,272 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include <map> +#include <string> +#include <Commons/Exception.h> +#include "Enums.h" +#include "IManager.h" +#include <Commons/WrtAccess/WrtAccess.h> +#include <WidgetDB/WidgetDBMgr.h> +#include <iconv.h> +#include "FilesystemUtils.h" +#include <Export.h> +#include <Logger.h> + +using namespace WrtDeviceApis; +using namespace WrtDeviceApis::Commons; + +namespace { +const std::string PATH_INVALID_COMPONENT_PARENT_DIR(".."); +const std::string PATH_INVALID_COMPONENT_CURRENT_DIR("."); + +typedef std::map<std::string, std::string> RootToPathMap; +typedef RootToPathMap::const_iterator RootToPathMapIterator; +typedef std::map<std::string, std::string> PathToRootMap; +typedef PathToRootMap::const_iterator PathToRootMapIterator; +} + +namespace DeviceAPI { +namespace Filesystem { +namespace Utils{ +const RootToPathMap DLL_EXPORT & getRootToPathMap() +{ + static RootToPathMap result; + if (result.empty()) { + IManager& manager = IManager::getInstance(); + std::map<std::string, IPathPtr> locations = manager.getStorageList(); + + std::map<std::string, IPathPtr>::const_iterator it; + + for (it = locations.begin(); it != locations.end(); ++it) { + result[it->first] = it->second->getFullPath(); + } + } + return result; +} + +const PathToRootMap DLL_EXPORT & getPathToRootMap() +{ + static PathToRootMap result; + if (result.empty()) { + IManager& manager = IManager::getInstance(); + std::map<std::string, IPathPtr> locations = manager.getStorageList(); + + std::map<std::string, IPathPtr>::const_iterator it; + + for (it = locations.begin(); it != locations.end(); ++it) { + result[it->second->getFullPath()] = it->first; + } + } + return result; +} + +IPathPtr DLL_EXPORT fromVirtualPath(JSContextRef context, + const std::string arg) +{ + LoggerD("arg:[" << arg << "]"); + + // uri path, strip file:// + if (isUriPath(arg)) { + std::string stripPath = arg.substr(strlen("file://")); + LoggerD("uri absolute path" << stripPath); + IPathPtr result = IPath::create(stripPath); + + return result; + } + + if (!isPathValid(arg)) { + LoggerD("virtual path is invalid:[" << arg << "]"); + ThrowMsg(Commons::ConversionException, "Not found path component."); + } + + std::string root; + std::string tail; + std::string::size_type separatorPosition = arg.find(IPath::getSeparator()); + if (separatorPosition != std::string::npos) { + root = arg.substr(0, separatorPosition); + tail = arg.substr(separatorPosition + 1, arg.size() - 1); + } else { + root = arg; + } + + int widgetId = WrtAccessSingleton::Instance().getWidgetId(); + WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); + + RootToPathMap rootToPath = getRootToPathMap(); + rootToPath["wgt-package"] = widgetDB->getWidgetInstallationPath(); + rootToPath["wgt-private"] = widgetDB->getWidgetPersistentStoragePath(); + rootToPath["wgt-private-tmp"] = widgetDB->getWidgetTemporaryStoragePath(); + RootToPathMapIterator it = rootToPath.find(root); + + if (it == rootToPath.end()) { +// ThrowMsg(Commons::NotFoundException, "Location not found."); + LoggerD("Allow non virtual root path " << arg); + return IPath::create(arg); + } + + IPathPtr result = IPath::create(it->second); + + if (!tail.empty()) { + result->append(tail); + } + + return result; +} + +std::string DLL_EXPORT toVirtualPath(JSContextRef context, const std::string arg) { + + int widgetId = WrtAccessSingleton::Instance().getWidgetId(); + WidgetDB::Api::IWidgetDBPtr widgetDB = + WidgetDB::Api::getWidgetDB(widgetId); + + PathToRootMap pathToRoot = getPathToRootMap(); + pathToRoot[widgetDB->getWidgetInstallationPath()] = "wgt-package"; + pathToRoot[widgetDB->getWidgetPersistentStoragePath()] = "wgt-private"; + pathToRoot[widgetDB->getWidgetTemporaryStoragePath()] = "wgt-private-tmp"; + + std::string path = arg; + + std::string::size_type pos = path.size(); + while (std::string::npos != (pos = path.rfind(IPath::getSeparator(), pos))) { + PathToRootMapIterator it = pathToRoot.find(path); + if (pathToRoot.end() != it) { + return it->second + arg.substr(path.size()); + } + path.erase(pos, path.size()); + } + LoggerD("Allow non virtual root path"); + + return arg; +// ThrowMsg(Commons::ConversionException, "Path doesn't contain a valid location type."); +} + +bool DLL_EXPORT isVirtualPath(const std::string& path) { + std::string root; + std::string::size_type separatorPosition = path.find(IPath::getSeparator()); + + if (separatorPosition != std::string::npos) { + root = path.substr(0, separatorPosition); + } else { + root = path; + } + + int widgetId = WrtAccessSingleton::Instance().getWidgetId(); + WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId); + + RootToPathMap rootToPath = getRootToPathMap(); + rootToPath["wgt-package"] = widgetDB->getWidgetInstallationPath(); + rootToPath["wgt-private"] = widgetDB->getWidgetPersistentStoragePath(); + rootToPath["wgt-private-tmp"] = widgetDB->getWidgetTemporaryStoragePath(); + RootToPathMapIterator it = rootToPath.find(root); + + + if (it == rootToPath.end()) { + return false; + } + else { + return true; + } +} +bool DLL_EXPORT isUriPath(const std::string& path) { + const char* uriPrefix = "file://"; + const char* stringFromPath = path.c_str(); + + if (!strncmp(uriPrefix, stringFromPath, strlen(uriPrefix)) && path[strlen(uriPrefix)] == '/') { + return true; + } + + return false; +} + +bool DLL_EXPORT isPathValid(const std::string& path) { + static const std::string currentDirBegin(PATH_INVALID_COMPONENT_CURRENT_DIR + IPath::getSeparator()); + static const std::string parentDirBegin(PATH_INVALID_COMPONENT_PARENT_DIR + + IPath::getSeparator()); + static const std::string currentDirMiddle(IPath::getSeparator() + + PATH_INVALID_COMPONENT_CURRENT_DIR +IPath::getSeparator()); + static const std::string parentDirMiddle(IPath::getSeparator() + + PATH_INVALID_COMPONENT_PARENT_DIR +IPath::getSeparator()); + + if (path.find(parentDirBegin) == 0 || + path.find(currentDirBegin) == 0 || + path.find(parentDirMiddle) != std::string::npos || + path.find(currentDirMiddle) != std::string::npos) { + return false; + } + + return true; +} + +void DLL_EXPORT toUTF8String(std::string fromEncoding, const char* from, const size_t fromLength, std::string &outputString) +{ + const char *fromEncodingSet = fromEncoding.c_str(); + char *outputBuf = NULL; + char *buf = NULL; + int ret = 0; + iconv_t cd; + size_t outputLength= 0; + + try { +// LoggerD("from " << fromEncodingSet << " to UTF8 conversion " << fromLength); + + cd = iconv_open("UTF-8", fromEncodingSet); + + if (cd == (iconv_t) -1) + { + LoggerD("charset conversion exception iconv -1"); + ThrowMsg(Commons::PlatformException, "charset conversion exception"); + } + + if (fromLength == 0) + { + LoggerD("from length 0"); + ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer."); + } + + outputBuf = new char[fromLength * 4 + 1]; + outputLength = fromLength * 4; + memset(outputBuf, 0, outputLength + 1); + buf = outputBuf; + + + ret = iconv(cd, (char**)&from, (size_t*)&fromLength, &buf, &outputLength); + + LoggerD(fromLength << " " << outputLength); + + if (ret < 0) + { + iconv_close(cd); + LoggerD("charset conversion exception ret " << ret); + ThrowMsg(Commons::PlatformException, "charset conversion exception"); + } + + iconv_close(cd); + outputString = outputBuf; + + if (outputBuf) + delete[] outputBuf; + + } + Catch(std::bad_alloc) { + LoggerD("Couldn't allocate output buffer."); + ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer."); + } +} +} +} +} |