2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <linux/limits.h>
27 #include <pkgmgr-info.h>
28 #include <pkgmgrinfo_zone.h>
32 #include "simple_util.h"
36 #define _MAX_PACKAGE_ID_LEN 256
37 #define _MAX_BASE_PATH_LEN 512
39 static const char _EXTERNAL_APP_SPECIFIC_PATH[] = "/opt/storage/sdcard/apps/";
40 static const char _APP_SPECIFIC_PATH[] = "/opt/usr/apps/";
41 static const char _PRELOADED_APP_SPECIFIC_PATH[] = "/usr/apps/";
43 static const char _DATA_DIR[] = "data/";
44 static const char _CACHE_DIR[] = "cache/";
45 static const char _RESOURCE_DIR[] = "res/";
46 static const char _TEP_RESOURCE_DIR[] = "res/tep/";
47 static const char _SHARED_DATA_DIR[] = "shared/data/";
48 static const char _SHARED_TRUSTED_DIR[] = "shared/trusted/";
49 static const char _SHARED_RESOURCE_DIR[] = "shared/res/";
51 static char external_root_path[_MAX_BASE_PATH_LEN] = {0,};
52 static char root_path[_MAX_BASE_PATH_LEN] = {0,};
53 static char data_path[_MAX_BASE_PATH_LEN] = {0,};
54 static char cache_path[_MAX_BASE_PATH_LEN] = {0,};
55 static char resource_path[_MAX_BASE_PATH_LEN] = {0,};
56 static char tep_resource_path[_MAX_BASE_PATH_LEN] = {0,};
57 static char shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
58 static char shared_resource_path[_MAX_BASE_PATH_LEN] = {0,};
59 static char shared_trusted_path[_MAX_BASE_PATH_LEN] = {0,};
60 static char external_data_path[_MAX_BASE_PATH_LEN] = {0,};
61 static char external_cache_path[_MAX_BASE_PATH_LEN] = {0,};
62 static char external_shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
63 static char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
65 static int __get_pkgid_by_appid(char *pkgid, int pkgid_len, const char *appid)
67 pkgmgrinfo_appinfo_h handle = NULL;
68 char *tmp_pkgid = NULL;
71 int err = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
72 if (err != PMINFO_R_OK) {
73 _E("Failed to get app info. (err:%d)", err);
78 err = pkgmgrinfo_appinfo_get_pkgid(handle, &tmp_pkgid);
79 if (err != PMINFO_R_OK) {
80 _E("Failed to get pkgid. (err:%d)", err);
81 pkgmgrinfo_appinfo_destroy_appinfo(handle);
84 strncpy(pkgid, tmp_pkgid, pkgid_len);
85 pkgmgrinfo_appinfo_destroy_appinfo(handle);
90 static int __get_pkgid(char* pkgid, int pkgid_len)
92 char appid[_MAX_PACKAGE_ID_LEN] = {0,};
93 const char *preinit_pkgid = NULL;
95 preinit_pkgid = aul_get_preinit_pkgid();
96 if (preinit_pkgid != NULL) {
97 strncpy(pkgid, preinit_pkgid, pkgid_len);
102 int err = aul_app_get_appid_bypid(getpid(), appid, _MAX_PACKAGE_ID_LEN - 1);
103 if (err != AUL_R_OK) {
104 _E("Failed to get appid. (err:%d)", err);
108 return __get_pkgid_by_appid(pkgid, pkgid_len, appid);
111 static const char* __get_app_specific_path(const char *pkgid)
113 pkgmgrinfo_pkginfo_h handle;
114 int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
115 if (ret != PMINFO_R_OK)
119 ret = pkgmgrinfo_pkginfo_is_preload(handle, &preload);
120 if (ret != PMINFO_R_OK) {
121 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
125 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
128 return _PRELOADED_APP_SPECIFIC_PATH;
131 return _APP_SPECIFIC_PATH;
134 static int __get_root_path(char *root_path, int root_path_len, bool external,
137 char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
138 const char *specific_path = NULL;
140 int err = __get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1);
141 if (err != AUL_R_OK) {
146 specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH;
148 specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : __get_app_specific_path(pkgid);
149 if (specific_path == NULL) {
155 int specific_path_len = strlen(specific_path);
156 int pkgid_len = strlen(pkgid);
157 int total_len = specific_path_len + pkgid_len + 1;
159 if (total_len > root_path_len) {
160 _E("Assert: path length %d is too long", total_len);
164 strncat(root_path, specific_path, specific_path_len);
165 strncat(root_path + specific_path_len, pkgid, pkgid_len);
166 root_path[specific_path_len + pkgid_len] = '/';
167 root_path[specific_path_len + pkgid_len + 1] = '\0';
172 static char* __get_base_path(bool can_write)
174 static char base_path[_MAX_BASE_PATH_LEN] = {0,};
177 if (__get_root_path(base_path, _MAX_BASE_PATH_LEN - 1, false,
178 can_write) != AUL_R_OK)
184 static int __get_path(char *path, int path_len, const char *dir_name,
185 bool external, bool can_write)
187 char* root_path = NULL;
188 pkgmgrinfo_pkginfo_h pkginfo = NULL;
191 if (dir_name == NULL)
193 _E("Assert: dir name is NULL!");
196 else if (strncmp(dir_name, _RESOURCE_DIR, strlen(_RESOURCE_DIR)) == 0 ||
197 strncmp(dir_name, _SHARED_RESOURCE_DIR, strlen(_SHARED_RESOURCE_DIR)) == 0)
199 char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
200 if (__get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1) != AUL_R_OK)
202 _E("Assert: failed to get the package id!");
206 if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo) != PMINFO_R_OK)
208 _E("Failed to get the package info from pkgid!");
212 if (pkgmgrinfo_pkginfo_get_root_path(pkginfo, &root_path) != PMINFO_R_OK)
214 _E("Failed to get the root path from pkginfo!");
215 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
223 root_path = (char *) (external ? aul_get_app_external_root_path() :
224 __get_base_path(can_write));
225 if (root_path == NULL)
227 _E("root_path is NULL!");
232 int dir_name_len = strlen(dir_name);
233 int root_path_len = strlen(root_path);
234 int total_len = root_path_len + dir_name_len;
236 if (total_len > path_len)
238 _E("Assert: path length %d is too long", total_len);
242 strncpy(path, root_path, root_path_len);
245 strncat(path, "/", 1);
248 strncpy(path + root_path_len, dir_name, dir_name_len);
252 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
258 static int __get_path_by_appid(char **path, const char *appid,
259 const char *dir_name, bool external)
261 if (dir_name == NULL) {
262 _E("Assert: dir name is NULL!");
266 if (path == NULL || appid == NULL) {
270 pkgmgrinfo_pkginfo_h pkginfo = NULL;
271 char *specific_path = NULL;
274 char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
275 int err = __get_pkgid_by_appid(pkgid, _MAX_PACKAGE_ID_LEN - 1, appid);
276 if (err != AUL_R_OK) {
280 if (strncmp(dir_name, _RESOURCE_DIR, strlen(_RESOURCE_DIR)) == 0 ||
281 strncmp(dir_name, _SHARED_RESOURCE_DIR, strlen(_SHARED_RESOURCE_DIR)) == 0) {
282 if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo) != PMINFO_R_OK) {
283 _E("Failed to get the package info from pkgid!");
287 if (pkgmgrinfo_pkginfo_get_root_path(pkginfo, &specific_path) != PMINFO_R_OK) {
288 _E("Failed to get the root path from pkginfo!");
289 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
293 total_len = strlen(specific_path) + strlen(dir_name) + 1;
296 specific_path = (char *) (external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH);
297 total_len = strlen(specific_path) + strlen(pkgid) + 1 + strlen(dir_name);
300 if (total_len > _MAX_BASE_PATH_LEN - 1) {
301 _E("Assert: path length %d is too long", total_len);
305 *path = (char *)calloc(total_len + 1, sizeof(char));
307 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
311 if (pkginfo != NULL) {
312 snprintf(*path, total_len + 1, "%s/%s", specific_path, dir_name);
313 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
315 snprintf(*path, total_len + 1, "%s%s/%s", specific_path, pkgid, dir_name);
321 void _clear_path_cache(void)
323 external_root_path[0] = '\0';
326 cache_path[0] = '\0';
327 resource_path[0] = '\0';
328 shared_data_path[0] = '\0';
329 shared_resource_path[0] = '\0';
330 shared_trusted_path[0] = '\0';
331 external_data_path[0] = '\0';
332 external_cache_path[0] = '\0';
333 external_shared_data_path[0] = '\0';
337 SLPAPI const char *aul_get_app_external_root_path(void)
339 if (external_root_path[0] == '\0') {
340 char oldZone[64] = { 0, };
342 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
343 int ret = __get_root_path(external_root_path, _MAX_BASE_PATH_LEN - 1, true,
345 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
350 return external_root_path;
353 SLPAPI const char *aul_get_app_root_path(void)
355 if (root_path[0] == '\0') {
356 char oldZone[64] = { 0, };
358 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
359 int ret = __get_root_path(root_path, _MAX_BASE_PATH_LEN - 1, false, false);
361 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
363 if ( ret != AUL_R_OK)
369 SLPAPI const char *aul_get_app_data_path(void)
371 if (data_path[0] == '\0') {
372 char oldZone[64] = { 0, };
374 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
375 int ret = __get_path(data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, false, true);
377 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
379 if ( ret != AUL_R_OK)
385 SLPAPI const char *aul_get_app_cache_path(void)
387 if (cache_path[0] == '\0') {
388 char oldZone[64] = { 0, };
390 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
391 int ret = __get_path(cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, false,
394 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
396 if ( ret != AUL_R_OK)
402 SLPAPI const char *aul_get_app_resource_path(void)
404 if (resource_path[0] == '\0') {
405 char oldZone[64] = { 0, };
407 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
408 int ret = __get_path(resource_path, _MAX_BASE_PATH_LEN - 1, _RESOURCE_DIR,
412 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
414 if ( ret != AUL_R_OK)
417 return resource_path;
420 SLPAPI const char *aul_get_app_tep_resource_path(void)
422 if (tep_resource_path[0] == '\0') {
423 char oldZone[64] = { 0, };
425 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
426 int ret = __get_path(tep_resource_path, _MAX_BASE_PATH_LEN - 1, _TEP_RESOURCE_DIR,
430 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
432 if ( ret != AUL_R_OK)
435 return tep_resource_path;
438 SLPAPI const char *aul_get_app_shared_data_path(void)
440 if (shared_data_path[0] == '\0') {
441 char oldZone[64] = { 0, };
443 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
444 int ret = __get_path(shared_data_path, _MAX_BASE_PATH_LEN - 1, _SHARED_DATA_DIR,
447 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
449 if ( ret != AUL_R_OK)
453 return shared_data_path;
456 SLPAPI const char *aul_get_app_shared_resource_path(void)
458 if (shared_resource_path[0] == '\0') {
459 char oldZone[64] = { 0, };
461 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
462 int ret = __get_path(shared_resource_path, _MAX_BASE_PATH_LEN - 1,
463 _SHARED_RESOURCE_DIR, false, false);
465 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
467 if ( ret != AUL_R_OK)
470 return shared_resource_path;
473 SLPAPI const char *aul_get_app_shared_trusted_path(void)
475 if (shared_trusted_path[0] == '\0') {
476 char oldZone[64] = { 0, };
478 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
479 int ret = __get_path(shared_trusted_path, _MAX_BASE_PATH_LEN - 1,
480 _SHARED_TRUSTED_DIR, false, true);
482 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
484 if ( ret != AUL_R_OK)
488 return shared_trusted_path;
491 SLPAPI const char *aul_get_app_external_data_path(void)
493 if (external_data_path[0] == '\0') {
494 char oldZone[64] = { 0, };
496 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
497 int ret = __get_path(external_data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR,
501 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
503 if ( ret != AUL_R_OK)
507 return external_data_path;
510 SLPAPI const char *aul_get_app_external_cache_path(void)
512 if (external_cache_path[0] == '\0') {
513 char oldZone[64] = { 0, };
515 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
516 int ret = __get_path(external_cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR,
520 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
522 if ( ret != AUL_R_OK)
526 return external_cache_path;
529 SLPAPI const char *aul_get_app_external_shared_data_path(void)
531 if (external_shared_data_path[0] == '\0') {
532 char oldZone[64] = { 0, };
534 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
535 int ret = __get_path(external_shared_data_path, _MAX_PACKAGE_ID_LEN - 1,
536 _SHARED_DATA_DIR, true, true);
538 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
540 if ( ret != AUL_R_OK)
544 return external_shared_data_path;
547 SLPAPI const char *aul_get_app_specific_path(void)
549 const char *specific_path = NULL;
550 char oldZone[64] = { 0, };
552 if (pkgid[0] == '\0') {
553 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
554 int err = __get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1);
556 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
558 if (err != AUL_R_OK) {
563 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
564 specific_path = __get_app_specific_path(pkgid);
565 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
567 return specific_path;
570 SLPAPI const char *aul_get_app_external_specific_path(void)
572 return _EXTERNAL_APP_SPECIFIC_PATH;
575 SLPAPI int aul_get_app_shared_data_path_by_appid(const char *appid, char **path)
577 char oldZone[64] = { 0, };
579 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
580 int ret = __get_path_by_appid(path, appid, _SHARED_DATA_DIR, false);
581 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
586 SLPAPI int aul_get_app_shared_resource_path_by_appid(const char *appid,
589 char oldZone[64] = { 0, };
591 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
592 int ret = __get_path_by_appid(path, appid, _SHARED_RESOURCE_DIR, false);
593 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
598 SLPAPI int aul_get_app_shared_trusted_path_by_appid(const char *appid,
601 char oldZone[64] = { 0, };
603 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
604 int ret = __get_path_by_appid(path, appid, _SHARED_TRUSTED_DIR, false);
605 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
610 SLPAPI int aul_get_app_external_shared_data_path_by_appid(const char *appid,
613 char oldZone[64] = { 0, };
615 pkgmgrinfo_pkginfo_set_zone(_cur_zone, oldZone, 64);
616 int ret = __get_path_by_appid(path, appid, _SHARED_DATA_DIR, true);
617 pkgmgrinfo_pkginfo_set_zone(oldZone, NULL, 0);
622 SLPAPI char *aul_get_cmdline_bypid(int pid)
624 return __proc_get_cmdline_bypid(pid);