2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 #include <Commons/Exception.h>
23 #include <Commons/WrtAccess/WrtAccess.h>
24 #include <WidgetDB/WidgetDBMgr.h>
26 #include "FilesystemUtils.h"
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
34 const std::string PATH_INVALID_COMPONENT_PARENT_DIR("..");
35 const std::string PATH_INVALID_COMPONENT_CURRENT_DIR(".");
37 typedef std::map<std::string, std::string> RootToPathMap;
38 typedef RootToPathMap::const_iterator RootToPathMapIterator;
39 typedef std::map<std::string, std::string> PathToRootMap;
40 typedef PathToRootMap::const_iterator PathToRootMapIterator;
44 namespace Filesystem {
46 const RootToPathMap DLL_EXPORT & getRootToPathMap()
48 static RootToPathMap result;
50 IManager& manager = IManager::getInstance();
51 std::map<std::string, IPathPtr> locations = manager.getStorageList();
53 std::map<std::string, IPathPtr>::const_iterator it;
55 for (it = locations.begin(); it != locations.end(); ++it) {
56 result[it->first] = it->second->getFullPath();
62 const PathToRootMap DLL_EXPORT & getPathToRootMap()
64 static PathToRootMap result;
66 IManager& manager = IManager::getInstance();
67 std::map<std::string, IPathPtr> locations = manager.getStorageList();
69 std::map<std::string, IPathPtr>::const_iterator it;
71 for (it = locations.begin(); it != locations.end(); ++it) {
72 result[it->second->getFullPath()] = it->first;
78 IPathPtr DLL_EXPORT fromVirtualPath(JSContextRef context,
79 const std::string& arg)
81 LoggerD("arg:[" << arg << "]");
83 // uri path, strip file://
85 std::string stripPath = arg.substr(strlen("file://"));
86 LoggerD("uri absolute path" << stripPath);
87 IPathPtr result = IPath::create(stripPath);
92 if (!isPathValid(arg)) {
93 LoggerD("virtual path is invalid:[" << arg << "]");
94 ThrowMsg(Commons::ConversionException, "Not found path component.");
99 std::string::size_type separatorPosition = arg.find(IPath::getSeparator());
100 if (separatorPosition != std::string::npos) {
101 root = arg.substr(0, separatorPosition);
102 tail = arg.substr(separatorPosition + 1, arg.size() - 1);
107 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
108 WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId);
110 RootToPathMap rootToPath = getRootToPathMap();
111 rootToPath["wgt-package"] = widgetDB->getWidgetInstallationPath();
112 rootToPath["wgt-private"] = widgetDB->getWidgetPersistentStoragePath();
113 rootToPath["wgt-private-tmp"] = widgetDB->getWidgetTemporaryStoragePath();
114 RootToPathMapIterator it = rootToPath.find(root);
116 if (it == rootToPath.end()) {
117 //ThrowMsg(Commons::NotFoundException, "Location not found.");
118 LoggerD("Allow non virtual root path " << arg);
119 return IPath::create(arg);
122 IPathPtr result = IPath::create(it->second);
125 result->append(tail);
131 std::string DLL_EXPORT toVirtualPath(JSContextRef context, const std::string& arg) {
133 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
134 WidgetDB::Api::IWidgetDBPtr widgetDB =
135 WidgetDB::Api::getWidgetDB(widgetId);
137 PathToRootMap pathToRoot = getPathToRootMap();
138 pathToRoot[widgetDB->getWidgetInstallationPath()] = "wgt-package";
139 pathToRoot[widgetDB->getWidgetPersistentStoragePath()] = "wgt-private";
140 pathToRoot[widgetDB->getWidgetTemporaryStoragePath()] = "wgt-private-tmp";
142 std::string path = arg;
144 std::string::size_type pos = path.size();
145 while (std::string::npos != (pos = path.rfind(IPath::getSeparator(), pos))) {
146 PathToRootMapIterator it = pathToRoot.find(path);
147 if (pathToRoot.end() != it) {
148 return it->second + arg.substr(path.size());
150 path.erase(pos, path.size());
152 LoggerD("Allow non virtual root path");
155 // ThrowMsg(Commons::ConversionException, "Path doesn't contain a valid location type.");
158 bool DLL_EXPORT isUriPath(const std::string& path) {
159 const char* uriPrefix = "file://";
160 const char* stringFromPath = path.c_str();
162 if (!strncmp(uriPrefix, stringFromPath, strlen(uriPrefix)) && path[strlen(uriPrefix)] == '/') {
169 bool DLL_EXPORT isPathValid(const std::string& path) {
170 static const std::string currentDirBegin(PATH_INVALID_COMPONENT_CURRENT_DIR + IPath::getSeparator());
171 static const std::string parentDirBegin(PATH_INVALID_COMPONENT_PARENT_DIR +
172 IPath::getSeparator());
173 static const std::string currentDirMiddle(IPath::getSeparator() +
174 PATH_INVALID_COMPONENT_CURRENT_DIR +IPath::getSeparator());
175 static const std::string parentDirMiddle(IPath::getSeparator() +
176 PATH_INVALID_COMPONENT_PARENT_DIR +IPath::getSeparator());
178 if (path.find(parentDirBegin) == 0 ||
179 path.find(currentDirBegin) == 0 ||
180 path.find(parentDirMiddle) != std::string::npos ||
181 path.find(currentDirMiddle) != std::string::npos) {
188 void DLL_EXPORT toUTF8String(std::string fromEncoding, const char* from, const size_t fromLength, std::string &outputString)
190 const char *fromEncodingSet = fromEncoding.c_str();
191 char *outputBuf = NULL;
195 size_t outputLength= 0;
198 // LoggerD("from " << fromEncodingSet << " to UTF8 conversion " << fromLength);
200 cd = iconv_open("UTF-8", fromEncodingSet);
202 if (cd == (iconv_t) -1)
204 LoggerD("charset conversion exception iconv -1");
205 ThrowMsg(Commons::PlatformException, "charset conversion exception");
210 LoggerD("from length 0");
211 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");
214 outputBuf = new char[fromLength * 4 + 1];
215 outputLength = fromLength * 4;
216 memset(outputBuf, 0, outputLength + 1);
220 ret = iconv(cd, (char**)&from, (size_t*)&fromLength, &buf, &outputLength);
222 LoggerD(fromLength << " " << outputLength);
227 LoggerD("charset conversion exception ret " << ret);
228 ThrowMsg(Commons::PlatformException, "charset conversion exception");
232 outputString = outputBuf;
238 Catch(std::bad_alloc) {
239 LoggerD("Couldn't allocate output buffer.");
240 ThrowMsg(Commons::PlatformException, "Couldn't allocate output buffer.");