Update change log and spec for wrt-plugins-tizen_0.2.71
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Filesystem / FilesystemUtils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <map>
19 #include <string>
20 #include <dpl/log/log.h>
21 #include <dpl/assert.h>
22 #include <Commons/Exception.h>
23 #include <API/Filesystem/Enums.h>
24 #include <API/Filesystem/IManager.h>
25 #include <Commons/WrtAccess/WrtAccess.h>
26 #include <WidgetDB/WidgetDBMgr.h>
27 #include "FilesystemUtils.h"
28
29 using namespace TizenApis::Api::Filesystem;
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::Commons;
32
33 namespace {
34 const std::string PATH_INVALID_COMPONENT_PARENT_DIR("..");
35 const std::string PATH_INVALID_COMPONENT_CURRENT_DIR(".");
36
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;
41 }
42
43 namespace TizenApis {
44 namespace Tizen1_0 {
45 namespace Utils {
46 const RootToPathMap& getRootToPathMap()
47 {
48         static RootToPathMap result;
49         if (result.empty()) {
50         IManager& manager = IManager::getInstance();
51                 std::map<std::string, Api::Filesystem::IPathPtr> locations = manager.getStorageList();
52
53                 std::map<std::string, Api::Filesystem::IPathPtr>::const_iterator it;
54
55                 for (it = locations.begin(); it != locations.end(); ++it) {
56                         result[it->first] = it->second->getFullPath();
57                 }
58         }
59         return result;
60 }
61
62 const PathToRootMap& getPathToRootMap()
63 {
64         static PathToRootMap result;
65         if (result.empty()) {
66                 IManager& manager = IManager::getInstance();
67                 std::map<std::string, Api::Filesystem::IPathPtr> locations = manager.getStorageList();
68
69                 std::map<std::string, Api::Filesystem::IPathPtr>::const_iterator it;
70
71                 for (it = locations.begin(); it != locations.end(); ++it) {
72                         result[it->second->getFullPath()] = it->first;
73                 }
74         }
75         return result;
76 }
77
78 IPathPtr fromVirtualPath(JSContextRef context,
79                          const std::string& arg)
80 {
81         LogDebug("arg:[" << arg << "]");
82
83         if (!isPathValid(arg)) {
84                 LogDebug("virtual path is invalid:[" << arg << "]");
85                 ThrowMsg(Commons::ConversionException, "Not found path component.");
86         }
87
88         std::string root;
89         std::string tail;
90         std::string::size_type separatorPosition = arg.find(IPath::getSeparator());
91         if (separatorPosition != std::string::npos) {
92                 root = arg.substr(0, separatorPosition);
93                 tail = arg.substr(separatorPosition + 1, arg.size() - 1);
94         } else {
95                 root = arg;
96         }
97
98         int widgetId = WrtAccessSingleton::Instance().getWidgetId();
99         WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId);
100
101         RootToPathMap rootToPath = getRootToPathMap();
102         rootToPath["wgt-package"] = widgetDB->getWidgetInstallationPath();
103         rootToPath["wgt-private"] = widgetDB->getWidgetPersistentStoragePath();
104         rootToPath["wgt-private-tmp"] = widgetDB->getWidgetTemporaryStoragePath();
105         RootToPathMapIterator it = rootToPath.find(root);
106         if (it == rootToPath.end()) {
107                 ThrowMsg(Commons::NotFoundException, "Location not found.");
108         }
109         IPathPtr result = IPath::create(it->second);
110
111         if (!tail.empty()) {
112                 result->append(tail);
113         }
114
115         return result;
116 }
117
118 std::string toVirtualPath(JSContextRef context, const std::string& arg) {
119
120     int widgetId = WrtAccessSingleton::Instance().getWidgetId();
121     WidgetDB::Api::IWidgetDBPtr widgetDB =
122         WidgetDB::Api::getWidgetDB(widgetId);
123
124         PathToRootMap pathToRoot = getPathToRootMap();
125         pathToRoot[widgetDB->getWidgetInstallationPath()] = "wgt-package";
126         pathToRoot[widgetDB->getWidgetPersistentStoragePath()] = "wgt-private";
127         pathToRoot[widgetDB->getWidgetTemporaryStoragePath()] = "wgt-private-tmp";
128
129         std::string path = arg;
130         std::string::size_type pos = path.size();
131         while (std::string::npos != (pos = path.rfind(IPath::getSeparator(), pos))) {
132                 PathToRootMapIterator it = pathToRoot.find(path);
133                 if (pathToRoot.end() != it) {
134                         return it->second + arg.substr(path.size());
135                 }
136                 path.erase(pos, path.size());
137         }
138         ThrowMsg(Commons::ConversionException, "Path doesn't contain a valid location type.");
139 }
140
141 bool isPathValid(const std::string& path) {
142         static const std::string currentDirBegin(PATH_INVALID_COMPONENT_CURRENT_DIR + Api::Filesystem::IPath::getSeparator());
143         static const std::string parentDirBegin(PATH_INVALID_COMPONENT_PARENT_DIR +
144                 Api::Filesystem::IPath::getSeparator());
145         static const std::string currentDirMiddle(Api::Filesystem::IPath::getSeparator() +
146                 PATH_INVALID_COMPONENT_CURRENT_DIR +Api::Filesystem::IPath::getSeparator());
147         static const std::string parentDirMiddle(Api::Filesystem::IPath::getSeparator() +
148                 PATH_INVALID_COMPONENT_PARENT_DIR +Api::Filesystem::IPath::getSeparator());
149
150         if (path.find(parentDirBegin) == 0 ||
151                         path.find(currentDirBegin) == 0 ||
152                 path.find(parentDirMiddle) != std::string::npos ||
153                 path.find(currentDirMiddle) != std::string::npos) {
154                 return false;
155         }
156
157         return true;
158 }
159
160 }
161 }
162 }