Fix resource leak
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_pkginfo.c
index a5b47d6..43f2d87 100644 (file)
@@ -219,6 +219,49 @@ static int _pkginfo_get_privilege(sqlite3 *db, const char *pkgid,
        return PMINFO_R_OK;
 }
 
+static int _pkginfo_get_appdefined_privilege(sqlite3 *db, const char *pkgid,
+               GList **privileges)
+{
+       static const char query_raw[] =
+               "SELECT DISTINCT privilege, license, type FROM "
+               "package_appdefined_privilege_info WHERE package=%Q";
+       int ret;
+       char *query;
+       sqlite3_stmt *stmt;
+       appdefined_privilege_x *privilege;
+
+       query = sqlite3_mprintf(query_raw, pkgid);
+       if (query == NULL) {
+               LOGE("out of memory");
+               return PMINFO_R_ERROR;
+       }
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query),
+                       &stmt, NULL);
+       sqlite3_free(query);
+       if (ret != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return PMINFO_R_ERROR;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               privilege = calloc(1, sizeof(appdefined_privilege_x));
+               if (!privilege) {
+                       LOGE("failed to alloc memory");
+                       return PMINFO_R_ERROR;
+               }
+               _save_column_str(stmt, 0, &privilege->value);
+               _save_column_str(stmt, 1, &privilege->license);
+               _save_column_str(stmt, 2, &privilege->type);
+               *privileges = g_list_append(*privileges,
+                               (gpointer)privilege);
+       }
+
+       sqlite3_finalize(stmt);
+
+       return PMINFO_R_OK;
+}
+
 static const char join_localized_info[] =
        " LEFT OUTER JOIN package_localized_info"
        "  ON pi.package=package_localized_info.package"
@@ -527,6 +570,14 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                        }
                }
 
+               if (flag & PMINFO_PKGINFO_GET_APPDEFINED_PRIVILEGE) {
+                       if (_pkginfo_get_appdefined_privilege(db, info->package,
+                                               &info->appdefined_privileges)) {
+                               ret = PMINFO_R_ERROR;
+                               goto catch;
+                       }
+               }
+
                if (is_check_storage &&
                                __pkginfo_check_installed_storage(info) != PMINFO_R_OK) {
                        ret = PMINFO_R_ERROR;
@@ -660,6 +711,12 @@ static int _pkgmgrinfo_get_pkginfo(const char *pkgid, uid_t uid,
                ret = _pkginfo_get_packages(GLOBAL_USER, locale, filter,
                                PMINFO_PKGINFO_GET_ALL, list);
 
+       if (ret != PMINFO_R_OK) {
+               g_hash_table_destroy(list);
+               free(locale);
+               return ret;
+       }
+
        if (!g_hash_table_size(list)) {
                _LOGD("pkginfo for [%s] is not existed for user [%d]",
                                pkgid, uid);
@@ -1302,6 +1359,8 @@ API int pkgmgrinfo_pkginfo_get_installed_storage(pkgmgrinfo_pkginfo_h handle, pk
                *storage = PMINFO_INTERNAL_STORAGE;
        else if (strcmp(info->pkg_info->installed_storage, "installed_external") == 0)
                *storage = PMINFO_EXTERNAL_STORAGE;
+       else if (strcmp(info->pkg_info->installed_storage, "installed_extended") == 0)
+               *storage = PMINFO_EXTENDED_STORAGE;
        else
                return PMINFO_R_ERROR;
 
@@ -1899,6 +1958,35 @@ API int pkgmgrinfo_pkginfo_foreach_privilege(pkgmgrinfo_pkginfo_h handle,
        return PMINFO_R_OK;
 }
 
+API int pkgmgrinfo_pkginfo_foreach_appdefined_privilege(
+               pkgmgrinfo_pkginfo_h handle,
+               pkgmgrinfo_pkg_appdefined_privilege_list_cb privilege_func,
+               void *user_data)
+{
+       retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL");
+       retvm_if(privilege_func == NULL, PMINFO_R_EINVAL,
+                       "Callback function is NULL");
+       int ret;
+       appdefined_privilege_x *privilege;
+       GList *tmp;
+       pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
+
+       if (info->pkg_info == NULL)
+               return PMINFO_R_ERROR;
+
+       for (tmp = info->pkg_info->appdefined_privileges; tmp;
+                       tmp = tmp->next) {
+               privilege = (appdefined_privilege_x *)tmp->data;
+               if (privilege == NULL)
+                       continue;
+               ret = privilege_func(privilege->value, privilege->license,
+                               user_data);
+               if (ret < 0)
+                       break;
+       }
+       return PMINFO_R_OK;
+}
+
 int __compare_package_version(const char *version, int *major,
                int *minor, int *macro, int *nano)
 {