Adjust some log levels
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_pkginfo.c
index ea64f4b..62a60d2 100644 (file)
@@ -86,6 +86,16 @@ static void __destroy_each_node(gpointer data, gpointer user_data)
        node = NULL;
 }
 
+static void __destroy_metadata_node(gpointer data)
+{
+       pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
+       if (node->key)
+               free(node->key);
+       if (node->value)
+               free(node->value);
+       free(node);
+}
+
 static void __cleanup_pkginfo(pkgmgr_pkginfo_x *data)
 {
        ret_if(data == NULL);
@@ -107,7 +117,7 @@ long long _pkgmgr_calculate_dir_size(char *dirname)
        int q = 0; /*quotient*/
        int r = 0; /*remainder*/
        DIR *dp = NULL;
-       struct dirent ep, *result;
+       struct dirent *ep;
        struct stat fileinfo;
        char abs_filename[FILENAME_MAX] = { 0, };
        retvm_if(dirname == NULL, PMINFO_R_ERROR, "dirname is NULL");
@@ -118,22 +128,20 @@ long long _pkgmgr_calculate_dir_size(char *dirname)
                return -1;
        }
 
-       for (ret = readdir_r(dp, &ep, &result);
-                       ret == 0 && result != NULL;
-                       ret = readdir_r(dp, &ep, &result)) {
-               if (!strcmp(ep.d_name, ".") ||
-                       !strcmp(ep.d_name, "..")) {
+       for (ep = readdir(dp); ep != NULL; ep = readdir(dp)) {
+               if (!strcmp(ep->d_name, ".") ||
+                       !strcmp(ep->d_name, "..")) {
                        continue;
                }
                snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
-                        ep.d_name);
+                        ep->d_name);
                if (lstat(abs_filename, &fileinfo) < 0)
                        perror(abs_filename);
                else {
                        if (S_ISDIR(fileinfo.st_mode)) {
                                total += fileinfo.st_size;
-                               if (strcmp(ep.d_name, ".")
-                                   && strcmp(ep.d_name, "..")) {
+                               if (strcmp(ep->d_name, ".")
+                                   && strcmp(ep->d_name, "..")) {
                                        ret = _pkgmgr_calculate_dir_size
                                            (abs_filename);
                                        total = total + ret;
@@ -232,19 +240,19 @@ static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
        if (filter == NULL)
                return PMINFO_R_OK;
 
-       len += strlen(" WHERE 1=1 ");
        strncat(buf, " WHERE 1=1 ", MAX_QUERY_LEN - len - 1);
+       len += strlen(" WHERE 1=1 ");
        for (list = filter->list; list; list = list->next) {
                joined |= __get_filter_condition(list->data, uid, &condition,
                                bind_params);
                if (condition == NULL)
                        continue;
 
-               len += strlen(" AND ");
                strncat(buf, " AND ", MAX_QUERY_LEN - len - 1);
+               len += strlen(" AND ");
 
-               len += strlen(condition);
                strncat(buf, condition, sizeof(buf) - len - 1);
+               len += strlen(condition);
                free(condition);
                condition = NULL;
        }
@@ -330,16 +338,16 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages)
 {
        static const char query_raw[] =
-               "SELECT DISTINCT pi.package, pi.package_version, "
-               "pi.install_location, pi.package_removable, "
-               "pi.package_preload, pi.package_readonly, pi.package_update, "
-               "pi.package_appsetting, pi.package_system, pi.package_type, "
-               "pi.package_size, pi.installed_time, pi.installed_storage, "
-               "pi.storeclient_id, pi.mainapp_id, pi.package_url, "
-               "pi.root_path, pi.csc_path, pi.package_nodisplay, "
-               "pi.package_api_version, pi.package_support_disable, "
-               "pi.package_tep_name, pi.package_zip_mount_file, pi.external_path, "
-               "pi.package_support_mode";
+               "SELECT DISTINCT pi.package, pi.installed_storage, pi.external_path";
+       static const char query_basic[] =
+               ", pi.package_version, pi.install_location, "
+               "pi.package_removable, pi.package_preload, pi.package_readonly, "
+               "pi.package_update, pi.package_appsetting, pi.package_system, "
+               "pi.package_type, pi.package_size, pi.installed_time, "
+               "pi.storeclient_id, pi.mainapp_id, pi.package_url, pi.root_path, "
+               "pi.csc_path, pi.package_nodisplay, pi.package_api_version, "
+               "pi.package_support_disable, pi.package_tep_name, "
+               "pi.package_zip_mount_file, pi.package_support_mode";
        static const char query_author[] =
                ", pi.author_name, pi.author_email, pi.author_href";
        static const char query_label[] =
@@ -366,9 +374,10 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
        author_x *author = NULL;
        GList *bind_params = NULL;
        sqlite3 *db;
-       sqlite3_stmt *stmt;
+       sqlite3_stmt *stmt = NULL;
        pkgmgrinfo_filter_x *tmp_filter = NULL;
        bool is_check_storage = true;
+       const uid_t global_user_uid = GLOBAL_USER;
 
        dbpath = getUserPkgParserDBPathUID(uid);
        if (dbpath == NULL)
@@ -376,7 +385,7 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
 
        ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
        if (ret != SQLITE_OK) {
-               _LOGD("failed to open db: %d", ret);
+               _LOGD("failed to open db(%s): %d", dbpath, ret);
                free(dbpath);
                return PMINFO_R_ERROR;
        }
@@ -396,6 +405,10 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
 
        query_len = strlen(query_raw);
        snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
+       if (flag & PMINFO_APPINFO_GET_BASICINFO) {
+               strncat(query, query_basic, MAX_QUERY_LEN - query_len - 1);
+               query_len += strlen(query_basic);
+       }
        if (flag & PMINFO_PKGINFO_GET_AUTHOR) {
                strncat(query, query_author, MAX_QUERY_LEN - query_len - 1);
                query_len += strlen(query_author);
@@ -450,39 +463,36 @@ static int _pkginfo_get_packages(uid_t uid, const char *locale,
                }
                idx = 0;
                _save_column_str(stmt, idx++, &info->package);
-               if (g_hash_table_contains(packages,
-                                       (gconstpointer)info->package)) {
-                       free(info->package);
-                       free(info);
-                       info = NULL;
-                       continue;
-               }
-               _save_column_str(stmt, idx++, &info->version);
-               _save_column_str(stmt, idx++, &info->installlocation);
-               _save_column_str(stmt, idx++, &info->removable);
-               _save_column_str(stmt, idx++, &info->preload);
-               _save_column_str(stmt, idx++, &info->readonly);
-               _save_column_str(stmt, idx++, &info->update);
-               _save_column_str(stmt, idx++, &info->appsetting);
-               _save_column_str(stmt, idx++, &info->system);
-               _save_column_str(stmt, idx++, &info->type);
-               _save_column_str(stmt, idx++, &info->package_size);
-               _save_column_str(stmt, idx++, &info->installed_time);
                _save_column_str(stmt, idx++, &info->installed_storage);
-               _save_column_str(stmt, idx++, &info->storeclient_id);
-               _save_column_str(stmt, idx++, &info->mainapp_id);
-               _save_column_str(stmt, idx++, &info->package_url);
-               _save_column_str(stmt, idx++, &info->root_path);
-               _save_column_str(stmt, idx++, &info->csc_path);
-               _save_column_str(stmt, idx++, &info->nodisplay_setting);
-               _save_column_str(stmt, idx++, &info->api_version);
-               _save_column_str(stmt, idx++, &info->support_disable);
-               _save_column_str(stmt, idx++, &info->tep_name);
-               _save_column_str(stmt, idx++, &info->zip_mount_file);
                _save_column_str(stmt, idx++, &info->external_path);
-               _save_column_str(stmt, idx++, &info->support_mode);
+
+               if (flag & PMINFO_APPINFO_GET_BASICINFO) {
+                       _save_column_str(stmt, idx++, &info->version);
+                       _save_column_str(stmt, idx++, &info->installlocation);
+                       _save_column_str(stmt, idx++, &info->removable);
+                       _save_column_str(stmt, idx++, &info->preload);
+                       _save_column_str(stmt, idx++, &info->readonly);
+                       _save_column_str(stmt, idx++, &info->update);
+                       _save_column_str(stmt, idx++, &info->appsetting);
+                       _save_column_str(stmt, idx++, &info->system);
+                       _save_column_str(stmt, idx++, &info->type);
+                       _save_column_str(stmt, idx++, &info->package_size);
+                       _save_column_str(stmt, idx++, &info->installed_time);
+                       _save_column_str(stmt, idx++, &info->storeclient_id);
+                       _save_column_str(stmt, idx++, &info->mainapp_id);
+                       _save_column_str(stmt, idx++, &info->package_url);
+                       _save_column_str(stmt, idx++, &info->root_path);
+                       _save_column_str(stmt, idx++, &info->csc_path);
+                       _save_column_str(stmt, idx++, &info->nodisplay_setting);
+                       _save_column_str(stmt, idx++, &info->api_version);
+                       _save_column_str(stmt, idx++, &info->support_disable);
+                       _save_column_str(stmt, idx++, &info->tep_name);
+                       _save_column_str(stmt, idx++, &info->zip_mount_file);
+                       _save_column_str(stmt, idx++, &info->support_mode);
+               }
+
                info->for_all_users =
-                       strdup((uid != GLOBAL_USER) ? "false" : "true");
+                       strdup((uid != global_user_uid) ? "false" : "true");
 
                if (flag & PMINFO_PKGINFO_GET_AUTHOR) {
                        /* TODO : author should be retrieved at package_localized_info */
@@ -560,7 +570,8 @@ catch:
 
        g_list_free_full(bind_params, free);
        sqlite3_close_v2(db);
-       sqlite3_finalize(stmt);
+       if (stmt)
+               sqlite3_finalize(stmt);
 
        return ret;
 }
@@ -604,10 +615,11 @@ static int _pkginfo_get_filtered_foreach_pkginfo(uid_t uid,
                pkgmgrinfo_pkginfo_filter_add_bool(tmp_filter,
                                PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, false);
 
-       ret = _pkginfo_get_packages(uid, locale, tmp_filter, flag, list);
+       ret = _pkginfo_get_packages(uid, locale, tmp_filter,
+                       flag | PMINFO_PKGINFO_GET_BASICINFO, list);
        if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
                ret = _pkginfo_get_packages(GLOBAL_USER, locale, tmp_filter,
-                               flag, list);
+                               flag | PMINFO_PKGINFO_GET_BASICINFO, list);
 
        if (ret != PMINFO_R_OK) {
                g_hash_table_destroy(list);
@@ -666,7 +678,7 @@ static int _pkgmgrinfo_get_pkginfo(const char *pkgid, uid_t uid,
                                PMINFO_PKGINFO_GET_ALL, list);
 
        if (!g_hash_table_size(list)) {
-               _LOGI("pkginfo for [%s] is not existed for user [%d]",
+               _LOGD("pkginfo for [%s] is not existed for user [%d]",
                                pkgid, uid);
                g_hash_table_destroy(list);
                free(locale);
@@ -795,7 +807,7 @@ API int pkgmgrinfo_pkginfo_get_usr_disabled_pkginfo(const char *pkgid,
        }
 
        if (!g_hash_table_size(list)) {
-               _LOGI("disabled pkginfo for [%s] is not existed for user [%d]",
+               _LOGD("disabled pkginfo for [%s] is not existed for user [%d]",
                                pkgid, uid);
                g_hash_table_destroy(list);
                free(locale);
@@ -1087,6 +1099,24 @@ API int pkgmgrinfo_pkginfo_get_zip_mount_file(pkgmgrinfo_pkginfo_h handle, char
        return PMINFO_R_OK;
 }
 
+API int pkgmgrinfo_pkginfo_get_external_image_path(pkgmgrinfo_pkginfo_h handle, char **ext_image_path)
+{
+       pkgmgr_pkginfo_x *info = (pkgmgr_pkginfo_x *)handle;
+
+       retvm_if(handle == NULL, PMINFO_R_EINVAL, "pkginfo handle is NULL\n");
+       retvm_if(ext_image_path == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
+
+       if (info->pkg_info == NULL)
+               return PMINFO_R_ERROR;
+
+       if (info->pkg_info->external_path == NULL)
+               return PMINFO_R_ENOENT;
+
+       *ext_image_path = (char *)info->pkg_info->external_path;
+
+       return PMINFO_R_OK;
+}
+
 API int pkgmgrinfo_pkginfo_get_install_location(pkgmgrinfo_pkginfo_h handle, pkgmgrinfo_install_location *location)
 {
        char *val;
@@ -1641,6 +1671,8 @@ API int pkgmgrinfo_pkginfo_filter_destroy(pkgmgrinfo_pkginfo_filter_h handle)
                g_slist_free(filter->list);
        }
 
+       g_slist_free_full(filter->list_metadata, __destroy_metadata_node);
+
        free(filter);
 
        return PMINFO_R_OK;