77150e48bf6bcc19d763cedabc11ff073333e97a
[framework/appfw/aul-1.git] / src / aul_path.c
1 /*
2  * Copyright (c) 2014 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 <unistd.h>
19 #include <linux/limits.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include <pkgmgr-info.h>
27
28 #include "aul_api.h"
29 #include "aul_util.h"
30 #include "simple_util.h"
31 #include "aul.h"
32
33 #define _MAX_PACKAGE_ID_LEN 256
34 #define _MAX_BASE_PATH_LEN 512
35
36 static const char _EXTERNAL_APP_SPECIFIC_PATH[] = "/opt/storage/sdcard/apps/";
37 static const char _APP_SPECIFIC_PATH[] = "/opt/usr/apps/";
38
39 static const char _DATA_DIR[] = "data/";
40 static const char _CACHE_DIR[] = "cache/";
41 static const char _RESOURCE_DIR[] = "res/";
42 static const char _SHARED_DATA_DIR[] = "shared/data/";
43 static const char _SHARED_TRUSTED_DIR[] = "shared/trusted/";
44 static const char _SHARED_RESOURCE_DIR[] = "shared/res/";
45
46 static int __get_pkgid_by_appid(char *pkgid, int pkgid_len, const char *appid)
47 {
48         pkgmgrinfo_appinfo_h handle = NULL;
49         char *tmp_pkgid = NULL;
50
51         // get pkginfo handle
52         int err = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
53         if (err != PMINFO_R_OK)
54         {
55                 _E("Failed to get app info. (err:%d)", err);
56                 return AUL_R_ENOAPP;
57         }
58
59         // get and set pkgid
60         err = pkgmgrinfo_appinfo_get_pkgid(handle, &tmp_pkgid);
61         if (err != PMINFO_R_OK)
62         {
63                 _E("Failed to get pkgid. (err:%d)", err);
64                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
65                 return AUL_R_ENOAPP;
66         }
67
68         strncat(pkgid, tmp_pkgid, pkgid_len);
69         pkgmgrinfo_appinfo_destroy_appinfo(handle);
70
71         return AUL_R_OK;
72 }
73
74 static int __get_pkgid(char* pkgid, int pkgid_len)
75 {
76         char appid[_MAX_PACKAGE_ID_LEN] = {0,};
77
78         // get appid
79         int err = aul_app_get_appid_bypid(getpid(), appid, _MAX_PACKAGE_ID_LEN - 1);
80         if (err != AUL_R_OK)
81         {
82                 _E("Failed to get appid. (err:%d)", err);
83                 return err;
84         }
85
86         return __get_pkgid_by_appid(pkgid, pkgid_len, appid);
87 }
88
89 static int __get_root_path(char *root_path, int root_path_len, bool external)
90 {
91         static char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
92         const char *specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH;
93
94         if (pkgid[0] == '\0')
95         {
96                 int err = __get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1);
97                 if (err != AUL_R_OK)
98                 {
99                         return err;
100                 }
101         }
102         {
103                 int specific_path_len = strlen(specific_path);
104                 int pkgid_len = strlen(pkgid);
105                 int total_len = specific_path_len + pkgid_len + 1;
106
107                 if (total_len > root_path_len)
108                 {
109                         _E("Assert: path length %d is too long", total_len);
110                         assert(false);
111                 }
112
113                 strncat(root_path, specific_path, specific_path_len);
114                 strncat(root_path + specific_path_len, pkgid, pkgid_len);
115                 root_path[specific_path_len + pkgid_len] = '/';
116         }
117
118         return AUL_R_OK;
119 }
120
121 static int __get_path(char *path, int path_len, const char *dir_name, bool external)
122 {
123         if (dir_name == NULL)
124         {
125                 _E("Assert: dir name is NULL!");
126                 assert(false);
127         }
128         {
129                 const char *root_path = external ? aul_get_app_external_root_path() : aul_get_app_root_path();
130                 if (root_path == NULL)
131                 {
132                         return AUL_R_ERROR;
133                 }
134                 else
135                 {
136                         int dir_name_len = strlen(dir_name);
137                         int root_path_len = strlen(root_path);
138                         int total_len = root_path_len + dir_name_len;
139
140                         if (total_len > path_len)
141                         {
142                                 _E("Assert: path length %d is too long", total_len);
143                                 assert(false);
144                         }
145
146                         strncpy(path, root_path, root_path_len);
147                         strncpy(path + root_path_len, dir_name, dir_name_len);
148                 }
149         }
150
151         return AUL_R_OK;
152 }
153
154 static int __get_path_by_appid(char **path, const char *appid, const char *dir_name, bool external)
155 {
156         if (dir_name == NULL)
157         {
158                 _E("Assert: dir name is NULL!");
159                 assert(false);
160         }
161
162         if (path == NULL || appid == NULL)
163         {
164                 return AUL_R_EINVAL;
165         }
166
167         {
168                 char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
169                 int err = __get_pkgid_by_appid(pkgid, _MAX_PACKAGE_ID_LEN - 1, appid);
170                 if (err != AUL_R_OK)
171                 {
172                         return err;
173                 }
174                 {
175                         const char *specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH;
176                         int specific_path_len = strlen(specific_path);
177                         int pkgid_len = strlen(pkgid);
178                         int dir_name_len = strlen(dir_name);
179                         int total_len = specific_path_len + pkgid_len + 1 + dir_name_len;
180
181                         if (total_len > _MAX_BASE_PATH_LEN - 1)
182                         {
183                                 _E("Assert: path length %d is too long", total_len);
184                                 assert(false);
185                         }
186
187                         *path = (char *)calloc(total_len + 1, sizeof(char));
188                         if (*path == NULL)
189                         {
190                                 return AUL_R_ERROR;
191                         }
192
193                         snprintf(*path, total_len + 1, "%s%s/%s", specific_path, pkgid, dir_name);
194                 }
195         }
196
197         return AUL_R_OK;
198 }
199
200 SLPAPI const char *aul_get_app_external_root_path(void)
201 {
202         static char external_root_path[_MAX_BASE_PATH_LEN] = {0,};
203         if (external_root_path[0] == '\0')
204         {
205                 if (__get_root_path(external_root_path, _MAX_BASE_PATH_LEN - 1, true) != AUL_R_OK)
206                 {
207                         return NULL;
208                 }
209         }
210         return external_root_path;
211 }
212
213 SLPAPI const char *aul_get_app_root_path(void)
214 {
215         static char root_path[_MAX_BASE_PATH_LEN] = {0,};
216         if (root_path[0] == '\0')
217         {
218                 if (__get_root_path(root_path, _MAX_BASE_PATH_LEN - 1, false) != AUL_R_OK)
219                 {
220                         return NULL;
221                 }
222         }
223         return root_path;
224 }
225
226 SLPAPI const char *aul_get_app_data_path(void)
227 {
228         static char data_path[_MAX_BASE_PATH_LEN] = {0,};
229         if (data_path[0] == '\0')
230         {
231                 if (__get_path(data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, false) != AUL_R_OK)
232                 {
233                         return NULL;
234                 }
235         }
236         return data_path;
237 }
238
239 SLPAPI const char *aul_get_app_cache_path(void)
240 {
241         static char cache_path[_MAX_BASE_PATH_LEN] = {0,};
242         if (cache_path[0] == '\0')
243         {
244                 if (__get_path(cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, false) != AUL_R_OK)
245                 {
246                         return NULL;
247                 }
248         }
249         return cache_path;
250 }
251
252 SLPAPI const char *aul_get_app_resource_path(void)
253 {
254         static char resource_path[_MAX_BASE_PATH_LEN] = {0,};
255         if (resource_path[0] == '\0')
256         {
257                 if (__get_path(resource_path, _MAX_BASE_PATH_LEN - 1, _RESOURCE_DIR, false) != AUL_R_OK)
258                 {
259                         return NULL;
260                 }
261         }
262         return resource_path;
263 }
264
265 SLPAPI const char *aul_get_app_shared_data_path(void)
266 {
267         static char shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
268         if (shared_data_path[0] == '\0')
269         {
270                 if (__get_path(shared_data_path, _MAX_BASE_PATH_LEN - 1, _SHARED_DATA_DIR, false) != AUL_R_OK)
271                 {
272                         return NULL;
273                 }
274         }
275         return shared_data_path;
276 }
277
278 SLPAPI const char *aul_get_app_shared_resource_path(void)
279 {
280         static char shared_resource_path[_MAX_BASE_PATH_LEN] = {0,};
281         if (shared_resource_path[0] == '\0')
282         {
283                 if (__get_path(shared_resource_path, _MAX_BASE_PATH_LEN - 1, _SHARED_RESOURCE_DIR, false) != AUL_R_OK)
284                 {
285                         return NULL;
286                 }
287         }
288         return shared_resource_path;
289 }
290
291 SLPAPI const char *aul_get_app_shared_trusted_path(void)
292 {
293         static char shared_trusted_path[_MAX_BASE_PATH_LEN] = {0,};
294         if (shared_trusted_path[0] == '\0')
295         {
296                 if (__get_path(shared_trusted_path, _MAX_BASE_PATH_LEN - 1, _SHARED_TRUSTED_DIR, false) != AUL_R_OK)
297                 {
298                         return NULL;
299                 }
300         }
301         return shared_trusted_path;
302 }
303
304 SLPAPI const char *aul_get_app_external_data_path(void)
305 {
306         static char external_data_path[_MAX_BASE_PATH_LEN] = {0,};
307         if (external_data_path[0] == '\0')
308         {
309                 if (__get_path(external_data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, true) != AUL_R_OK)
310                 {
311                         return NULL;
312                 }
313         }
314         return external_data_path;
315 }
316
317 SLPAPI const char *aul_get_app_external_cache_path(void)
318 {
319         static char external_cache_path[_MAX_BASE_PATH_LEN] = {0,};
320         if (external_cache_path[0] == '\0')
321         {
322                 if (__get_path(external_cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, true) != AUL_R_OK)
323                 {
324                         return NULL;
325                 }
326         }
327         return external_cache_path;
328 }
329
330 SLPAPI const char *aul_get_app_external_shared_data_path(void)
331 {
332         static char external_shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
333         if (external_shared_data_path[0] == '\0')
334         {
335                 if (__get_path(external_shared_data_path, _MAX_PACKAGE_ID_LEN - 1, _SHARED_DATA_DIR, true) != AUL_R_OK)
336                 {
337                         return NULL;
338                 }
339         }
340         return external_shared_data_path;
341 }
342
343 SLPAPI const char *aul_get_app_specific_path(void)
344 {
345         return _APP_SPECIFIC_PATH;
346 }
347
348 SLPAPI const char *aul_get_app_external_specific_path(void)
349 {
350         return _EXTERNAL_APP_SPECIFIC_PATH;
351 }
352
353 SLPAPI int aul_get_app_shared_data_path_by_appid(const char *appid, char **path)
354 {
355         return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, false);
356 }
357
358 SLPAPI int aul_get_app_shared_resource_path_by_appid(const char *appid, char **path)
359 {
360         return __get_path_by_appid(path, appid, _SHARED_RESOURCE_DIR, false);
361 }
362
363 SLPAPI int aul_get_app_shared_trusted_path_by_appid(const char *appid, char **path)
364 {
365         return __get_path_by_appid(path, appid, _SHARED_TRUSTED_DIR, false);
366 }
367
368 SLPAPI int aul_get_app_external_shared_data_path_by_appid(const char *appid, char **path)
369 {
370         return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, true);
371 }
372
373 SLPAPI char *aul_get_cmdline_bypid(int pid)
374 {
375         return __proc_get_cmdline_bypid(pid);
376 }