Fix path getter API. 52/34952/6
authorJiwoong Im <jiwoong.im@samsung.com>
Thu, 5 Feb 2015 01:32:11 +0000 (10:32 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Tue, 10 Feb 2015 01:41:24 +0000 (10:41 +0900)
JIRA ticket : TC-2314

To get application's path, use pkgmgrinfo_pkginfo_get_root_path api.
aul_get_app_xxx_path api search GLOBAL_USER and regular user DB altogether.

Change-Id: I82389591b570cf1998a64e67ce4f3981dfdf3048
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
include/aul.h
src/aul_path.c

index 47b5866..c02f9fb 100644 (file)
@@ -1630,10 +1630,10 @@ int aul_get_app_shared_data_path_by_appid(const char *app_id, char **path);
 int aul_get_app_shared_resource_path_by_appid(const char *app_id, char **path);
 int aul_get_app_shared_trusted_path_by_appid(const char *app_id, char **path);
 int aul_get_app_external_shared_data_path_by_appid(const char *app_id, char **path);
-int aul_get_app_usr_shared_data_path_by_appid(const char *app_id, char **path, uid_t uid);
-int aul_get_app_usr_shared_resource_path_by_appid(const char *app_id, char **path, uid_t uid);
-int aul_get_app_usr_shared_trusted_path_by_appid(const char *app_id, char **path, uid_t uid);
-int aul_get_app_usr_external_shared_data_path_by_appid(const char *app_id, char **path, uid_t uid);
+int aul_get_usr_app_shared_data_path_by_appid(const char *app_id, char **path, uid_t uid);
+int aul_get_usr_app_shared_resource_path_by_appid(const char *app_id, char **path, uid_t uid);
+int aul_get_usr_app_shared_trusted_path_by_appid(const char *app_id, char **path, uid_t uid);
+int aul_get_usr_app_external_shared_data_path_by_appid(const char *app_id, char **path, uid_t uid);
 
 
 typedef int (*subapp_fn)(void *data);
index 508023a..16fbc3a 100755 (executable)
 #include "simple_util.h"
 #include "aul.h"
 
-#define _MAX_PACKAGE_ID_LEN 256
-#define _MAX_BASE_PATH_LEN 512
-
 #define _GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
-#define _EXTERNAL_APP_SPECIFIC_PATH tzplatform_mkpath(TZ_SYS_STORAGE, "/sdcard/apps/")
-#define _APP_SPECIFIC_PATH tzplatform_getname(TZ_USER_APP)
+#define _EXTERNAL_APP_SPECIFIC_PATH \
+       tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard/apps/")
+#define _APP_SPECIFIC_PATH tzplatform_getenv(TZ_USER_APP)
 
 static const char _DATA_DIR[] = "data/";
 static const char _CACHE_DIR[] = "cache/";
@@ -45,267 +43,209 @@ static const char _SHARED_DATA_DIR[] = "shared/data/";
 static const char _SHARED_TRUSTED_DIR[] = "shared/trusted/";
 static const char _SHARED_RESOURCE_DIR[] = "shared/res/";
 
-static int __get_pkgid_by_appid(char *pkgid, int pkgid_len, const char *appid, uid_t uid)
+static int __get_pkgid(char *pkgid, int len, const char *appid, uid_t uid)
 {
-       pkgmgrinfo_appinfo_h handle = NULL;
-       char *tmp_pkgid = NULL;
+       pkgmgrinfo_appinfo_h appinfo;
+       char *_pkgid;
+       int ret;
+       char buf[NAME_MAX];
+       const char *_appid;
+
+       if (appid == NULL) {
+               ret = aul_app_get_appid_bypid(getpid(), buf, sizeof(buf));
+               if (ret != AUL_R_OK)
+                       return ret;
+
+               _appid = buf;
+       } else {
+               _appid = appid;
+       }
 
-       // get pkginfo handle
-       int err = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, handle);
-       if (err != PMINFO_R_OK) {
-               _E("Failed to get app info. (err:%d)", err);
-               return AUL_R_ENOAPP;
+       ret = pkgmgrinfo_appinfo_get_appinfo(_appid, &appinfo);
+       if (ret != PMINFO_R_OK) {
+               ret = pkgmgrinfo_appinfo_get_usr_appinfo(_appid, uid, &appinfo);
+               if (ret != PMINFO_R_OK) {
+                       _E("Failed to get app info. (ret:%d)", ret);
+                       return AUL_R_ENOAPP;
+               }
        }
 
-       // get and set pkgid
-       err = pkgmgrinfo_appinfo_get_pkgid(handle, &tmp_pkgid);
-       if (err != PMINFO_R_OK) {
-               _E("Failed to get pkgid. (err:%d)", err);
-               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &_pkgid);
+       if (ret != PMINFO_R_OK) {
+               _E("Failed to get pkgid. (ret:%d)", ret);
+               pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
                return AUL_R_ENOAPP;
        }
 
-       strncat(pkgid, tmp_pkgid, pkgid_len);
-       pkgmgrinfo_appinfo_destroy_appinfo(handle);
+       snprintf(pkgid, len, "%s", _pkgid);
+       pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
 
        return AUL_R_OK;
 }
 
-static int __get_pkgid(char *pkgid, int pkgid_len)
+static int __get_external_path(char **path, const char *appid,
+               const char *dir_name, uid_t uid)
 {
-       char appid[_MAX_PACKAGE_ID_LEN] = {0,};
+       char buf[PATH_MAX];
+       char pkgid[NAME_MAX];
+       int ret;
 
-       // get appid
-       int err = aul_app_get_appid_bypid(getpid(), appid, _MAX_PACKAGE_ID_LEN - 1);
-       if (err != AUL_R_OK) {
-               _E("Failed to get appid. (err:%d)", err);
-               return err;
-       }
+       ret = __get_pkgid(pkgid, sizeof(pkgid), appid, uid);
+       if (ret != AUL_R_OK)
+               return ret;
+
+       snprintf(buf, sizeof(buf), "%s%s/%s", _EXTERNAL_APP_SPECIFIC_PATH,
+                       pkgid, dir_name ? dir_name : "");
+
+       assert(path);
+       *path = strdup(buf);
 
-       return __get_pkgid_by_appid(pkgid, pkgid_len, appid, getuid());
+       return AUL_R_OK;
 }
 
-static int __get_root_path(char *root_path, int root_path_len, bool external)
+static int __get_path(char **path, const char *appid, const char *dir_name,
+               uid_t uid)
 {
-       static char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
-       const char *specific_path = external ? _EXTERNAL_APP_SPECIFIC_PATH : _APP_SPECIFIC_PATH;
+       char *_path;
+       char buf[PATH_MAX];
+       char pkgid[NAME_MAX];
+       int ret;
+       pkgmgrinfo_pkginfo_h pkginfo;
+       int len;
+
+       ret = __get_pkgid(pkgid, sizeof(pkgid), appid, uid);
+       if (ret != AUL_R_OK)
+               return ret;
 
-       if (pkgid[0] == '\0') {
-               int err = __get_pkgid(pkgid, _MAX_PACKAGE_ID_LEN - 1);
-               if (err != AUL_R_OK) {
-                       return err;
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo);
+       if (ret != PMINFO_R_OK) {
+               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
+               if (ret != PMINFO_R_OK) {
+                       _E("Failed to get pkginfo. (ret:%d)", ret);
+                       pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
+                       return AUL_R_ENOAPP;
                }
        }
-       {
-               int specific_path_len = strlen(specific_path);
-               int pkgid_len = strlen(pkgid);
-               int total_len = specific_path_len + pkgid_len + 1;
-
-               if (total_len > root_path_len) {
-                       _E("Assert: path length %d is too long", total_len);
-                       assert(false);
-               }
+       ret = pkgmgrinfo_pkginfo_get_root_path(pkginfo, &_path);
+       if (ret != PMINFO_R_OK) {
+               _E("Failed to get root path. (ret:%d)", ret);
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
+               return AUL_R_ERROR;
+       }
 
-               strncat(root_path, specific_path, specific_path_len);
-               strncat(root_path + specific_path_len, pkgid, pkgid_len);
-               root_path[specific_path_len + pkgid_len] = '/';
+       len = _path ? strlen(_path) : 0;
+       if (len == 0) {
+               _E("Root path is null or empty");
+               pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
+               return AUL_R_ERROR;
        }
 
-       return AUL_R_OK;
-}
+       snprintf(buf, sizeof(buf), "%s%s%s", _path,
+                       _path[len - 1] == '/' ?  "" : "/",
+                       dir_name ? dir_name : "");
 
-static int __get_path(char *path, int path_len, const char *dir_name, bool external)
-{
-       if (dir_name == NULL) {
-               _E("Assert: dir name is NULL!");
-               assert(false);
-       }
-       {
-               const char *root_path = external ? aul_get_app_external_root_path() : aul_get_app_root_path();
-               if (root_path == NULL) {
-                       return AUL_R_ERROR;
-               }
-               else {
-                       int dir_name_len = strlen(dir_name);
-                       int root_path_len = strlen(root_path);
-                       int total_len = root_path_len + dir_name_len;
-
-                       if (total_len > path_len) {
-                               _E("Assert: path length %d is too long", total_len);
-                               assert(false);
-                       }
-
-                       strncpy(path, root_path, root_path_len);
-                       strncpy(path + root_path_len, dir_name, dir_name_len);
-               }
-       }
+       pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
+
+       assert(path);
+       *path = strdup(buf);
 
        return AUL_R_OK;
 }
 
-static int __get_path_by_appid(char **path, const char *appid, const char *dir_name, bool external, uid_t uid)
+static const char *__get(char **path, const char *appid,
+               const char *dir_name, uid_t uid,
+               int (*func_get)(char **, const char *, const char *, uid_t))
 {
-       char *tmp_path;
-       char pkgid[_MAX_PACKAGE_ID_LEN] = {0,};
-       int ret = AUL_R_OK;
-       pkgmgrinfo_pkginfo_h handle = NULL;
+       int r;
 
-       if (dir_name == NULL || path == NULL || appid == NULL) {
-               return AUL_R_EINVAL;
-       }
+       if (*path)
+               return *path;
 
-       ret = __get_pkgid_by_appid(pkgid, _MAX_PACKAGE_ID_LEN - 1, appid, uid);
-       if (ret != AUL_R_OK) {
-               return ret;
-       }
+       assert(func_get);
 
-       *path = (char *)calloc(PATH_MAX, sizeof(char));
-       if (*path == NULL) {
-               return AUL_R_ERROR;
-       }
+       r = func_get(path, appid, dir_name, uid);
+       if (r != AUL_R_OK)
+               return NULL;
 
-       if (external) {
-               snprintf(*path, PATH_MAX, "%s%s/%s", _EXTERNAL_APP_SPECIFIC_PATH, pkgid, dir_name);
-       }
-       else {
-               ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, handle);
-               if (ret != PMINFO_R_OK) {
-                       _E("Failed to get pkgid. (err:%d)", ret);
-                       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-                       return AUL_R_ENOAPP;
-               }
-               ret = pkgmgrinfo_pkginfo_get_root_path(handle, &tmp_path);
-               if (ret != PMINFO_R_OK) {
-                       _E("Failed to get pkgid. (err:%d)", ret);
-                       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-                       return AUL_R_ERROR;
-               }
-               snprintf(*path, PATH_MAX, "%s/%s", tmp_path, dir_name);
-       }
-
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return AUL_R_OK;
+       return *path;
 }
 
 SLPAPI const char *aul_get_app_external_root_path(void)
 {
-       static char external_root_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (external_root_path[0] == '\0') {
-               if (__get_root_path(external_root_path, _MAX_BASE_PATH_LEN - 1, true) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return external_root_path;
+       static char *path;
+
+       return __get(&path, NULL, NULL, getuid(), __get_external_path);
 }
 
 SLPAPI const char *aul_get_app_root_path(void)
 {
-       static char root_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (root_path[0] == '\0') {
-               if (__get_root_path(root_path, _MAX_BASE_PATH_LEN - 1, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return root_path;
+       static char *path;
+
+       return __get(&path, NULL, NULL, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_data_path(void)
 {
-       static char data_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (data_path[0] == '\0') {
-               if (__get_path(data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return data_path;
+       static char *path;
+
+       return __get(&path, NULL, _DATA_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_cache_path(void)
 {
-       static char cache_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (cache_path[0] == '\0') {
-               if (__get_path(cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return cache_path;
+       static char *path;
+
+       return __get(&path, NULL, _CACHE_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_resource_path(void)
 {
-       static char resource_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (resource_path[0] == '\0') {
-               if (__get_path(resource_path, _MAX_BASE_PATH_LEN - 1, _RESOURCE_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return resource_path;
+       static char *path;
+
+       return __get(&path, NULL, _RESOURCE_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_shared_data_path(void)
 {
-       static char shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (shared_data_path[0] == '\0') {
-               if (__get_path(shared_data_path, _MAX_BASE_PATH_LEN - 1, _SHARED_DATA_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return shared_data_path;
+       static char *path;
+
+       return __get(&path, NULL, _SHARED_DATA_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_shared_resource_path(void)
 {
-       static char shared_resource_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (shared_resource_path[0] == '\0') {
-               if (__get_path(shared_resource_path, _MAX_BASE_PATH_LEN - 1, _SHARED_RESOURCE_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return shared_resource_path;
+       static char *path;
+
+       return __get(&path, NULL, _SHARED_RESOURCE_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_shared_trusted_path(void)
 {
-       static char shared_trusted_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (shared_trusted_path[0] == '\0') {
-               if (__get_path(shared_trusted_path, _MAX_BASE_PATH_LEN - 1, _SHARED_TRUSTED_DIR, false) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return shared_trusted_path;
+       static char *path;
+
+       return __get(&path, NULL, _SHARED_TRUSTED_DIR, getuid(), __get_path);
 }
 
 SLPAPI const char *aul_get_app_external_data_path(void)
 {
-       static char external_data_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (external_data_path[0] == '\0') {
-               if (__get_path(external_data_path, _MAX_BASE_PATH_LEN - 1, _DATA_DIR, true) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return external_data_path;
+       static char *path;
+
+       return __get(&path, NULL, _DATA_DIR, getuid(), __get_external_path);
 }
 
 SLPAPI const char *aul_get_app_external_cache_path(void)
 {
-       static char external_cache_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (external_cache_path[0] == '\0') {
-               if (__get_path(external_cache_path, _MAX_BASE_PATH_LEN - 1, _CACHE_DIR, true) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return external_cache_path;
+       static char *path;
+
+       return __get(&path, NULL, _CACHE_DIR, getuid(), __get_external_path);
 }
 
 SLPAPI const char *aul_get_app_external_shared_data_path(void)
 {
-       static char external_shared_data_path[_MAX_BASE_PATH_LEN] = {0,};
-       if (external_shared_data_path[0] == '\0') {
-               if (__get_path(external_shared_data_path, _MAX_PACKAGE_ID_LEN - 1, _SHARED_DATA_DIR, true) != AUL_R_OK) {
-                       return NULL;
-               }
-       }
-       return external_shared_data_path;
+       static char *path;
+
+       return __get(&path, NULL, _SHARED_DATA_DIR, getuid(),
+                       __get_external_path);
 }
 
 SLPAPI const char *aul_get_app_specific_path(void)
@@ -320,40 +260,47 @@ SLPAPI const char *aul_get_app_external_specific_path(void)
 
 SLPAPI int aul_get_app_shared_data_path_by_appid(const char *appid, char **path)
 {
-       return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, false, _GLOBAL_USER);
+       return __get_path(path, appid, _SHARED_DATA_DIR, _GLOBAL_USER);
 }
 
-SLPAPI int aul_get_app_shared_resource_path_by_appid(const char *appid, char **path)
+SLPAPI int aul_get_app_shared_resource_path_by_appid(const char *appid,
+               char **path)
 {
-       return __get_path_by_appid(path, appid, _SHARED_RESOURCE_DIR, false, _GLOBAL_USER);
+       return __get_path(path, appid, _SHARED_RESOURCE_DIR, _GLOBAL_USER);
 }
 
-SLPAPI int aul_get_app_shared_trusted_path_by_appid(const char *appid, char **path)
+SLPAPI int aul_get_app_shared_trusted_path_by_appid(const char *appid,
+               char **path)
 {
-       return __get_path_by_appid(path, appid, _SHARED_TRUSTED_DIR, false, _GLOBAL_USER);
+       return __get_path(path, appid, _SHARED_TRUSTED_DIR, _GLOBAL_USER);
 }
 
-SLPAPI int aul_get_app_external_shared_data_path_by_appid(const char *appid, char **path)
+SLPAPI int aul_get_app_external_shared_data_path_by_appid(const char *appid,
+               char **path)
 {
-       return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, true, _GLOBAL_USER);
+       return __get_external_path(path, appid, _SHARED_DATA_DIR, _GLOBAL_USER);
 }
 
-SLPAPI int aul_get_usr_app_shared_data_path_by_appid(const char *appid, char **path, uid_t uid)
+SLPAPI int aul_get_usr_app_shared_data_path_by_appid(const char *appid,
+               char **path, uid_t uid)
 {
-       return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, false, uid);
+       return __get_path(path, appid, _SHARED_DATA_DIR, uid);
 }
 
-SLPAPI int aul_get_usr_app_shared_resource_path_by_appid(const char *appid, char **path, uid_t uid)
+SLPAPI int aul_get_usr_app_shared_resource_path_by_appid(const char *appid,
+               char **path, uid_t uid)
 {
-       return __get_path_by_appid(path, appid, _SHARED_RESOURCE_DIR, false, uid);
+       return __get_path(path, appid, _SHARED_RESOURCE_DIR, uid);
 }
 
-SLPAPI int aul_get_usr_app_shared_trusted_path_by_appid(const char *appid, char **path, uid_t uid)
+SLPAPI int aul_get_usr_app_shared_trusted_path_by_appid(const char *appid,
+               char **path, uid_t uid)
 {
-       return __get_path_by_appid(path, appid, _SHARED_TRUSTED_DIR, false, uid);
+       return __get_path(path, appid, _SHARED_TRUSTED_DIR, uid);
 }
 
-SLPAPI int aul_get_usr_app_external_shared_data_path_by_appid(const char *appid, char **path, uid_t uid)
+SLPAPI int aul_get_usr_app_external_shared_data_path_by_appid(const char *appid,
+               char **path, uid_t uid)
 {
-       return __get_path_by_appid(path, appid, _SHARED_DATA_DIR, true, uid);
+       return __get_external_path(path, appid, _SHARED_DATA_DIR, uid);
 }