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.
19 #include <linux/limits.h>
26 #include <pkgmgr-info.h>
30 #include "simple_util.h"
33 #define _MAX_PACKAGE_ID_LEN 256
34 #define _MAX_BASE_PATH_LEN 512
36 static const char _EXTERNAL_APP_SPECIFIC_PATH[] = "/opt/storage/sdcard/apps/";
37 static const char _APP_SPECIFIC_PATH[] = "/opt/usr/apps/";
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/";
46 static int __get_pkgid_by_appid(char *pkgid, int pkgid_len, const char *appid)
48 pkgmgrinfo_appinfo_h handle = NULL;
49 char *tmp_pkgid = NULL;
52 int err = pkgmgrinfo_appinfo_get_appinfo(appid, &handle);
53 if (err != PMINFO_R_OK)
55 _E("Failed to get app info. (err:%d)", err);
60 err = pkgmgrinfo_appinfo_get_pkgid(handle, &tmp_pkgid);
61 if (err != PMINFO_R_OK)
63 _E("Failed to get pkgid. (err:%d)", err);
64 pkgmgrinfo_appinfo_destroy_appinfo(handle);
68 strncat(pkgid, tmp_pkgid, pkgid_len);
69 pkgmgrinfo_appinfo_destroy_appinfo(handle);
74 static int __get_pkgid(char* pkgid, int pkgid_len)
76 char appid[_MAX_PACKAGE_ID_LEN] = {0,};
79 int err = aul_app_get_appid_bypid(getpid(), appid, _MAX_PACKAGE_ID_LEN - 1);
82 _E("Failed to get appid. (err:%d)", err);
86 return __get_pkgid_by_appid(pkgid, pkgid_len, appid);
89 static int __get_root_path(char *root_path, int root_path_len, bool external)
91 static char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
92 const char *specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH;
96 int err = __get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1);
103 int specific_path_len = strlen(specific_path);
104 int pkgid_len = strlen(pkgid);
105 int total_len = specific_path_len + pkgid_len + 1;
107 if (total_len > root_path_len)
109 _E("Assert: path length %d is too long", total_len);
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] = '/';
121 static int __get_path(char *path, int path_len, const char *dir_name, bool external)
123 if (dir_name == NULL)
125 _E("Assert: dir name is NULL!");
129 const char *root_path = external ? aul_get_app_external_root_path() : aul_get_app_root_path();
130 if (root_path == NULL)
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;
140 if (total_len > path_len)
142 _E("Assert: path length %d is too long", total_len);
146 strncpy(path, root_path, root_path_len);
147 strncpy(path + root_path_len, dir_name, dir_name_len);
154 static int __get_path_by_appid(char **path, const char *appid, const char *dir_name, bool external)
156 if (dir_name == NULL)
158 _E("Assert: dir name is NULL!");
162 if (path == NULL || appid == NULL)
168 char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
169 int err = __get_pkgid_by_appid(pkgid, _MAX_PACKAGE_ID_LEN - 1, appid);
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;
181 if (total_len > _MAX_BASE_PATH_LEN - 1)
183 _E("Assert: path length %d is too long", total_len);
187 *path = (char *)calloc(total_len + 1, sizeof(char));
193 snprintf(*path, total_len + 1, "%s%s/%s", specific_path, pkgid, dir_name);
200 SLPAPI const char *aul_get_app_external_root_path(void)
202 static char external_root_path[_MAX_BASE_PATH_LEN] = {0,};
203 if (external_root_path[0] == '\0')
205 if (__get_root_path(external_root_path, _MAX_BASE_PATH_LEN - 1, true) != AUL_R_OK)
210 return external_root_path;
213 SLPAPI const char *aul_get_app_root_path(void)
215 static char root_path[_MAX_BASE_PATH_LEN] = {0,};
216 if (root_path[0] == '\0')
218 if (__get_root_path(root_path, _MAX_BASE_PATH_LEN - 1, false) != AUL_R_OK)
226 SLPAPI const char *aul_get_app_data_path(void)
228 static char data_path[_MAX_BASE_PATH_LEN] = {0,};
229 if (data_path[0] == '\0')
231 if (__get_path(data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, false) != AUL_R_OK)
239 SLPAPI const char *aul_get_app_cache_path(void)
241 static char cache_path[_MAX_BASE_PATH_LEN] = {0,};
242 if (cache_path[0] == '\0')
244 if (__get_path(cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, false) != AUL_R_OK)
252 SLPAPI const char *aul_get_app_resource_path(void)
254 static char resource_path[_MAX_BASE_PATH_LEN] = {0,};
255 if (resource_path[0] == '\0')
257 if (__get_path(resource_path, _MAX_BASE_PATH_LEN - 1, _RESOURCE_DIR, false) != AUL_R_OK)
262 return resource_path;
265 SLPAPI const char *aul_get_app_shared_data_path(void)
267 static char shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
268 if (shared_data_path[0] == '\0')
270 if (__get_path(shared_data_path, _MAX_BASE_PATH_LEN - 1, _SHARED_DATA_DIR, false) != AUL_R_OK)
275 return shared_data_path;
278 SLPAPI const char *aul_get_app_shared_resource_path(void)
280 static char shared_resource_path[_MAX_BASE_PATH_LEN] = {0,};
281 if (shared_resource_path[0] == '\0')
283 if (__get_path(shared_resource_path, _MAX_BASE_PATH_LEN - 1, _SHARED_RESOURCE_DIR, false) != AUL_R_OK)
288 return shared_resource_path;
291 SLPAPI const char *aul_get_app_shared_trusted_path(void)
293 static char shared_trusted_path[_MAX_BASE_PATH_LEN] = {0,};
294 if (shared_trusted_path[0] == '\0')
296 if (__get_path(shared_trusted_path, _MAX_BASE_PATH_LEN - 1, _SHARED_TRUSTED_DIR, false) != AUL_R_OK)
301 return shared_trusted_path;
304 SLPAPI const char *aul_get_app_external_data_path(void)
306 static char external_data_path[_MAX_BASE_PATH_LEN] = {0,};
307 if (external_data_path[0] == '\0')
309 if (__get_path(external_data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, true) != AUL_R_OK)
314 return external_data_path;
317 SLPAPI const char *aul_get_app_external_cache_path(void)
319 static char external_cache_path[_MAX_BASE_PATH_LEN] = {0,};
320 if (external_cache_path[0] == '\0')
322 if (__get_path(external_cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, true) != AUL_R_OK)
327 return external_cache_path;
330 SLPAPI const char *aul_get_app_external_shared_data_path(void)
332 static char external_shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
333 if (external_shared_data_path[0] == '\0')
335 if (__get_path(external_shared_data_path, _MAX_PACKAGE_ID_LEN - 1, _SHARED_DATA_DIR, true) != AUL_R_OK)
340 return external_shared_data_path;
343 SLPAPI const char *aul_get_app_specific_path(void)
345 return _APP_SPECIFIC_PATH;
348 SLPAPI const char *aul_get_app_external_specific_path(void)
350 return _EXTERNAL_APP_SPECIFIC_PATH;
353 SLPAPI int aul_get_app_shared_data_path_by_appid(const char *appid, char **path)
355 return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, false);
358 SLPAPI int aul_get_app_shared_resource_path_by_appid(const char *appid, char **path)
360 return __get_path_by_appid(path, appid, _SHARED_RESOURCE_DIR, false);
363 SLPAPI int aul_get_app_shared_trusted_path_by_appid(const char *appid, char **path)
365 return __get_path_by_appid(path, appid, _SHARED_TRUSTED_DIR, false);
368 SLPAPI int aul_get_app_external_shared_data_path_by_appid(const char *appid, char **path)
370 return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, true);
373 SLPAPI char *aul_get_cmdline_bypid(int pid)
375 return __proc_get_cmdline_bypid(pid);