Add APIs for getting path related Resource Control
[platform/core/appfw/aul-1.git] / src / aul_path.cc
1 /*
2  * Copyright (c) 2021 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 <memory>
19 #include <mutex>
20 #include <string>
21
22 #include "aul/app_info/directory_info.hh"
23 #include "aul/app_info/external_directory_info.hh"
24 #include "aul_api.h"
25 #include "aul_util.h"
26 #include "include/aul.h"
27
28 #undef AUL_API
29 #define AUL_API extern "C" API
30
31 namespace {
32 std::unique_ptr<aul::DirectoryInfo> context(nullptr);
33 std::unique_ptr<aul::ExternalDirectoryInfo> ext_context(nullptr);
34 std::mutex mutex;
35 std::mutex ext_mutex;
36
37 const aul::DirectoryInfo* GetCurrentContext() {
38   std::unique_lock<std::mutex> lock(mutex);
39   if (context.get() == nullptr)
40     context.reset(aul::DirectoryInfo::Get());
41
42   return context.get();
43 }
44
45 const aul::ExternalDirectoryInfo* GetCurrentExtContext() {
46   std::unique_lock<std::mutex> lock(ext_mutex);
47   if (ext_context.get() == nullptr)
48     ext_context.reset(aul::ExternalDirectoryInfo::Get());
49
50   return ext_context.get();
51 }
52
53 }  // namespace
54
55 AUL_API const char* aul_get_app_external_root_path(void) {
56   auto* context = GetCurrentExtContext();
57   if (context == nullptr)
58     return nullptr;
59
60   return context->GetRootPath().c_str();
61 }
62
63 AUL_API const char* aul_get_app_root_path(void) {
64   auto* context = GetCurrentContext();
65   if (context == nullptr)
66     return nullptr;
67
68   return context->GetRootPath().c_str();
69 }
70
71 AUL_API const char* aul_get_app_data_path(void) {
72   auto* context = GetCurrentContext();
73   if (context == nullptr)
74     return nullptr;
75
76   return context->GetDataPath().c_str();
77 }
78
79 AUL_API const char* aul_get_app_cache_path(void) {
80   auto* context = GetCurrentContext();
81   if (context == nullptr)
82     return nullptr;
83
84   return context->GetCachePath().c_str();
85 }
86
87 AUL_API const char* aul_get_app_resource_path(void) {
88   auto* context = GetCurrentContext();
89   if (context == nullptr)
90     return nullptr;
91
92   return context->GetResourcePath().c_str();
93 }
94
95 AUL_API const char* aul_get_app_tep_resource_path(void) {
96   auto* context = GetCurrentContext();
97   if (context == nullptr)
98     return nullptr;
99
100   return context->GetTepResourcePath().c_str();
101 }
102
103 AUL_API int aul_get_app_shared_data_path(char** path) {
104   auto* context = GetCurrentContext();
105   if (context == nullptr)
106     return AUL_R_ERROR;
107
108   if (access(context->GetSharedDataPath().c_str(), F_OK) != 0)
109     return AUL_R_EREJECTED;
110
111   *path = strdup(context->GetSharedDataPath().c_str());
112   return AUL_R_OK;
113 }
114
115 AUL_API const char* aul_get_app_shared_resource_path(void) {
116   auto* context = GetCurrentContext();
117   if (context == nullptr)
118     return nullptr;
119
120   return context->GetSharedResourcePath().c_str();
121 }
122
123 AUL_API const char* aul_get_app_shared_trusted_path(void) {
124   auto* context = GetCurrentContext();
125   if (context == nullptr)
126     return nullptr;
127
128   return context->GetSharedTrustedPath().c_str();
129 }
130
131 AUL_API const char* aul_get_app_external_data_path(void) {
132   auto* context = GetCurrentExtContext();
133   if (context == nullptr)
134     return nullptr;
135
136   return context->GetDataPath().c_str();
137 }
138
139 AUL_API const char* aul_get_app_external_cache_path(void) {
140   auto* context = GetCurrentExtContext();
141   if (context == nullptr)
142     return nullptr;
143
144   return context->GetCachePath().c_str();
145 }
146
147 AUL_API const char* aul_get_app_external_shared_data_path(void) {
148   auto* context = GetCurrentExtContext();
149   if (context == nullptr)
150     return nullptr;
151
152   return context->GetSharedDataPath().c_str();
153 }
154
155 AUL_API const char* aul_get_app_specific_path(void) {
156   if (GetCurrentContext() == nullptr)
157     return nullptr;
158
159   return tzplatform_getenv(TZ_USER_APP);
160 }
161
162 AUL_API int aul_get_app_res_control_allowed_resource_path(const char* res_type,
163     char** path) {
164   if (res_type == nullptr || path == nullptr) {
165     _E("Invalid parameter");
166     return AUL_R_EINVAL;
167   }
168
169   auto* context = GetCurrentContext();
170   if (context == nullptr)
171     return AUL_R_ERROR;
172
173   std::string result = context->GetResControlAllowedResPath() + res_type + "/";
174
175   if (access(result.c_str(), F_OK) != 0)
176     return AUL_R_EREJECTED;
177
178   *path = strdup(result.c_str());
179   if (*path == nullptr) {
180     LOGE("Out of memory");
181     return AUL_R_ENOMEM;
182   }
183
184   return AUL_R_OK;
185 }
186
187 AUL_API int aul_get_app_res_control_global_resource_path(const char* res_type,
188     char** path) {
189   if (res_type == nullptr || path == nullptr) {
190     _E("Invalid parameter");
191     return AUL_R_EINVAL;
192   }
193
194   auto* context = GetCurrentContext();
195   if (context == nullptr)
196     return AUL_R_ERROR;
197
198   std::string result = context->GetResControlGlobalResPath() + res_type + "/";
199
200   if (access(result.c_str(), F_OK) != 0)
201     return AUL_R_EREJECTED;
202
203   *path = strdup(result.c_str());
204   if (*path == nullptr) {
205     LOGE("Out of memory");
206     return AUL_R_ENOMEM;
207   }
208
209   return AUL_R_OK;
210 }
211
212 AUL_API int aul_get_app_shared_data_path_by_appid(const char* appid,
213     char** path) {
214   if (path == nullptr)
215     return AUL_R_EINVAL;
216
217   if (appid == nullptr)
218     return aul_get_app_shared_data_path(path);
219
220   std::unique_ptr<aul::DirectoryInfo> info(
221       aul::DirectoryInfo::Get(appid, getuid()));
222   if (info.get() == nullptr)
223     return AUL_R_ENOAPP;
224
225   if (access(info->GetSharedDataPath().c_str(), F_OK) != 0)
226     return AUL_R_EREJECTED;
227
228   *path = strdup(info->GetSharedDataPath().c_str());
229   return AUL_R_OK;
230 }
231
232 AUL_API int aul_get_app_shared_resource_path_by_appid(const char* appid,
233     char** path) {
234   if (appid == nullptr || path == nullptr)
235     return AUL_R_EINVAL;
236
237   std::unique_ptr<aul::DirectoryInfo> info(
238       aul::DirectoryInfo::Get(appid, getuid()));
239   if (info.get() == nullptr)
240     return AUL_R_ENOAPP;
241
242   *path = strdup(info->GetSharedResourcePath().c_str());
243   return AUL_R_OK;
244 }
245
246 AUL_API int aul_get_app_shared_trusted_path_by_appid(const char* appid,
247     char** path) {
248   if (appid == nullptr || path == nullptr)
249     return AUL_R_EINVAL;
250
251   std::unique_ptr<aul::DirectoryInfo> info(
252       aul::DirectoryInfo::Get(appid, getuid()));
253   if (info.get() == nullptr)
254     return AUL_R_ENOAPP;
255
256   *path = strdup(info->GetSharedTrustedPath().c_str());
257   return AUL_R_OK;
258 }
259
260 AUL_API int aul_get_app_external_shared_data_path_by_appid(const char* appid,
261     char** path) {
262   if (appid == nullptr || path == nullptr)
263     return AUL_R_EINVAL;
264
265   std::unique_ptr<aul::ExternalDirectoryInfo> info(
266       aul::ExternalDirectoryInfo::Get(appid, getuid()));
267   if (info.get() == nullptr)
268     return AUL_R_ENOAPP;
269
270   *path = strdup(info->GetSharedDataPath().c_str());
271   return AUL_R_OK;
272 }
273
274 AUL_API int aul_get_usr_app_shared_data_path_by_appid(const char* appid,
275     char** path, uid_t uid) {
276   if (appid == nullptr || path == nullptr)
277     return AUL_R_EINVAL;
278
279   std::unique_ptr<aul::DirectoryInfo> info(
280       aul::DirectoryInfo::Get(appid, uid));
281   if (info.get() == nullptr)
282     return AUL_R_ENOAPP;
283
284   if (access(info->GetSharedDataPath().c_str(), F_OK) != 0)
285     return AUL_R_EREJECTED;
286
287   *path = strdup(info->GetSharedDataPath().c_str());
288   return AUL_R_OK;
289 }
290
291 AUL_API int aul_get_usr_app_shared_resource_path_by_appid(const char* appid,
292     char** path, uid_t uid) {
293   if (appid == nullptr || path == nullptr)
294     return AUL_R_EINVAL;
295
296   std::unique_ptr<aul::DirectoryInfo> info(
297       aul::DirectoryInfo::Get(appid, uid));
298   if (info.get() == nullptr)
299     return AUL_R_ENOAPP;
300
301   *path = strdup(info->GetSharedResourcePath().c_str());
302   return AUL_R_OK;
303 }
304
305 AUL_API int aul_get_usr_app_shared_trusted_path_by_appid(const char* appid,
306     char** path, uid_t uid) {
307   if (appid == nullptr || path == nullptr)
308     return AUL_R_EINVAL;
309
310   std::unique_ptr<aul::DirectoryInfo> info(
311       aul::DirectoryInfo::Get(appid, uid));
312   if (info.get() == nullptr)
313     return AUL_R_ENOAPP;
314
315   *path = strdup(info->GetSharedTrustedPath().c_str());
316   return AUL_R_OK;
317 }
318
319 AUL_API int aul_get_usr_app_external_shared_data_path_by_appid(
320     const char* appid, char** path, uid_t uid) {
321   if (appid == nullptr || path == nullptr)
322     return AUL_R_EINVAL;
323
324   std::unique_ptr<aul::ExternalDirectoryInfo> info(
325       aul::ExternalDirectoryInfo::Get(appid, uid));
326   if (info.get() == nullptr)
327     return AUL_R_ENOAPP;
328
329   *path = strdup(info->GetSharedDataPath().c_str());
330   return AUL_R_OK;
331 }