d42e4a3c6d0fc25b6710c340ab6934d716eea7f2
[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_shared_data_path_by_appid(const char* appid,
163     char** path) {
164   if (path == nullptr)
165     return AUL_R_EINVAL;
166
167   if (appid == nullptr)
168     return aul_get_app_shared_data_path(path);
169
170   std::unique_ptr<aul::DirectoryInfo> info(
171       aul::DirectoryInfo::Get(appid, getuid()));
172   if (info.get() == nullptr)
173     return AUL_R_ENOAPP;
174
175   if (access(info->GetSharedDataPath().c_str(), F_OK) != 0)
176     return AUL_R_EREJECTED;
177
178   *path = strdup(info->GetSharedDataPath().c_str());
179   return AUL_R_OK;
180 }
181
182 AUL_API int aul_get_app_shared_resource_path_by_appid(const char* appid,
183     char** path) {
184   if (appid == nullptr || path == nullptr)
185     return AUL_R_EINVAL;
186
187   std::unique_ptr<aul::DirectoryInfo> info(
188       aul::DirectoryInfo::Get(appid, getuid()));
189   if (info.get() == nullptr)
190     return AUL_R_ENOAPP;
191
192   *path = strdup(info->GetSharedResourcePath().c_str());
193   return AUL_R_OK;
194 }
195
196 AUL_API int aul_get_app_shared_trusted_path_by_appid(const char* appid,
197     char** path) {
198   if (appid == nullptr || path == nullptr)
199     return AUL_R_EINVAL;
200
201   std::unique_ptr<aul::DirectoryInfo> info(
202       aul::DirectoryInfo::Get(appid, getuid()));
203   if (info.get() == nullptr)
204     return AUL_R_ENOAPP;
205
206   *path = strdup(info->GetSharedTrustedPath().c_str());
207   return AUL_R_OK;
208 }
209
210 AUL_API int aul_get_app_external_shared_data_path_by_appid(const char* appid,
211     char** path) {
212   if (appid == nullptr || path == nullptr)
213     return AUL_R_EINVAL;
214
215   std::unique_ptr<aul::ExternalDirectoryInfo> info(
216       aul::ExternalDirectoryInfo::Get(appid, getuid()));
217   if (info.get() == nullptr)
218     return AUL_R_ENOAPP;
219
220   *path = strdup(info->GetSharedDataPath().c_str());
221   return AUL_R_OK;
222 }
223
224 AUL_API int aul_get_usr_app_shared_data_path_by_appid(const char* appid,
225     char** path, uid_t uid) {
226   if (appid == nullptr || path == nullptr)
227     return AUL_R_EINVAL;
228
229   std::unique_ptr<aul::DirectoryInfo> info(
230       aul::DirectoryInfo::Get(appid, uid));
231   if (info.get() == nullptr)
232     return AUL_R_ENOAPP;
233
234   if (access(info->GetSharedDataPath().c_str(), F_OK) != 0)
235     return AUL_R_EREJECTED;
236
237   *path = strdup(info->GetSharedDataPath().c_str());
238   return AUL_R_OK;
239 }
240
241 AUL_API int aul_get_usr_app_shared_resource_path_by_appid(const char* appid,
242     char** path, uid_t uid) {
243   if (appid == nullptr || path == nullptr)
244     return AUL_R_EINVAL;
245
246   std::unique_ptr<aul::DirectoryInfo> info(
247       aul::DirectoryInfo::Get(appid, uid));
248   if (info.get() == nullptr)
249     return AUL_R_ENOAPP;
250
251   *path = strdup(info->GetSharedResourcePath().c_str());
252   return AUL_R_OK;
253 }
254
255 AUL_API int aul_get_usr_app_shared_trusted_path_by_appid(const char* appid,
256     char** path, uid_t uid) {
257   if (appid == nullptr || path == nullptr)
258     return AUL_R_EINVAL;
259
260   std::unique_ptr<aul::DirectoryInfo> info(
261       aul::DirectoryInfo::Get(appid, uid));
262   if (info.get() == nullptr)
263     return AUL_R_ENOAPP;
264
265   *path = strdup(info->GetSharedTrustedPath().c_str());
266   return AUL_R_OK;
267 }
268
269 AUL_API int aul_get_usr_app_external_shared_data_path_by_appid(
270     const char* appid, char** path, uid_t uid) {
271   if (appid == nullptr || path == nullptr)
272     return AUL_R_EINVAL;
273
274   std::unique_ptr<aul::ExternalDirectoryInfo> info(
275       aul::ExternalDirectoryInfo::Get(appid, uid));
276   if (info.get() == nullptr)
277     return AUL_R_ENOAPP;
278
279   *path = strdup(info->GetSharedDataPath().c_str());
280   return AUL_R_OK;
281 }