Fix UpdateCache related with storage check
[platform/core/appfw/pkgmgr-info.git] / src / server / appinfo_internal.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <dlfcn.h>
25 #include <sqlite3.h>
26 #include <glib.h>
27
28 #include <memory>
29 #include <vector>
30
31 #include "pkgmgr-info.h"
32 #include "pkgmgrinfo_debug.h"
33 #include "pkgmgrinfo_private.h"
34 #include "pkgmgr_parser.h"
35 #include "pkgmgrinfo_internal.h"
36
37 namespace {
38
39 void __parse_appcontrol(GList** appcontrol,
40     char* appcontrol_str, char* visibility, char* id) {
41   char* dup;
42   char* token;
43   char* ptr = nullptr;
44   appcontrol_x* ac;
45
46   if (appcontrol_str == nullptr)
47     return;
48
49   dup = strdup(appcontrol_str);
50   if (dup == nullptr) {
51     _LOGE("out of memory");
52     return;
53   }
54
55   do {
56     ac = static_cast<appcontrol_x*>(calloc(1, sizeof(appcontrol_x)));
57     if (ac == nullptr) {
58       _LOGE("out of memory");
59       break;
60     }
61     token = strtok_r(dup, "|", &ptr);
62     if (token && strcmp(token, "NULL"))
63       ac->operation = strdup(token);
64     token = strtok_r(nullptr, "|", &ptr);
65     if (token && strcmp(token, "NULL"))
66       ac->uri = strdup(token);
67     token = strtok_r(nullptr, "|", &ptr);
68     if (token && strcmp(token, "NULL"))
69       ac->mime = strdup(token);
70     ac->visibility = strdup(visibility);
71     ac->id = strdup(id);
72     *appcontrol = g_list_prepend(*appcontrol, ac);
73   } while ((token = strtok_r(nullptr, ";", &ptr)));
74
75   free(dup);
76 }
77
78 int _appinfo_get_splashscreens(sqlite3* db,
79     const char* appid, GList** splashscreens) {
80   static const char query_raw[] =
81       "SELECT src, type, orientation, indicatordisplay, "
82       "operation, color_depth "
83       "FROM package_app_splash_screen WHERE app_id=%Q";
84   int ret;
85   char* query;
86   sqlite3_stmt* stmt;
87   int idx;
88   splashscreen_x* info;
89
90   query = sqlite3_mprintf(query_raw, appid);
91   if (query == nullptr) {
92     LOGE("out of memory");
93     return PMINFO_R_ERROR;
94   }
95
96   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
97   sqlite3_free(query);
98   if (ret != SQLITE_OK) {
99     LOGE("prepare failed: %s", sqlite3_errmsg(db));
100     return PMINFO_R_ERROR;
101   }
102
103   while (sqlite3_step(stmt) == SQLITE_ROW) {
104     info = static_cast<splashscreen_x*>(calloc(1, sizeof(splashscreen_x)));
105     if (info == nullptr) {
106       LOGE("out of memory");
107       sqlite3_finalize(stmt);
108       return PMINFO_R_ERROR;
109     }
110     idx = 0;
111     _save_column_str(stmt, idx++, &info->src);
112     _save_column_str(stmt, idx++, &info->type);
113     _save_column_str(stmt, idx++, &info->orientation);
114     _save_column_str(stmt, idx++, &info->indicatordisplay);
115     _save_column_str(stmt, idx++, &info->operation);
116     _save_column_str(stmt, idx++, &info->color_depth);
117     *splashscreens = g_list_prepend(*splashscreens, info);
118   }
119
120   sqlite3_finalize(stmt);
121
122   return PMINFO_R_OK;
123 }
124
125 int _appinfo_get_metadata(sqlite3* db,
126     const char* appid, GList** metadata) {
127   static const char query_raw[] =
128       "SELECT md_key, md_value "
129       "FROM package_app_app_metadata WHERE app_id=%Q";
130   int ret;
131   char* query;
132   sqlite3_stmt* stmt;
133   int idx;
134   metadata_x* info;
135
136   query = sqlite3_mprintf(query_raw, appid);
137   if (query == nullptr) {
138     LOGE("out of memory");
139     return PMINFO_R_ERROR;
140   }
141
142   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
143   sqlite3_free(query);
144   if (ret != SQLITE_OK) {
145     LOGE("prepare failed: %s", sqlite3_errmsg(db));
146     return PMINFO_R_ERROR;
147   }
148
149   while (sqlite3_step(stmt) == SQLITE_ROW) {
150     info = static_cast<metadata_x*>(calloc(1, sizeof(metadata_x)));
151     if (info == nullptr) {
152       LOGE("out of memory");
153       sqlite3_finalize(stmt);
154       return PMINFO_R_ERROR;
155     }
156     idx = 0;
157     _save_column_str(stmt, idx++, &info->key);
158     _save_column_str(stmt, idx++, &info->value);
159     *metadata = g_list_prepend(*metadata, info);
160   }
161
162   sqlite3_finalize(stmt);
163
164   return PMINFO_R_OK;
165 }
166
167 int _appinfo_get_app_control(sqlite3* db,
168     const char* appid, GList** appcontrol) {
169   static const char query_raw[] =
170       "SELECT app_control, visibility, app_control_id "
171       "FROM package_app_app_control WHERE app_id=%Q";
172   int ret;
173   int idx;
174   char *query;
175   sqlite3_stmt *stmt;
176   char *str;
177   char *visibility;
178   char *id;
179
180   query = sqlite3_mprintf(query_raw, appid);
181   if (query == nullptr) {
182     LOGE("out of memory");
183     return PMINFO_R_ERROR;
184   }
185
186   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
187   sqlite3_free(query);
188   if (ret != SQLITE_OK) {
189     LOGE("prepare failed: %s", sqlite3_errmsg(db));
190     return PMINFO_R_ERROR;
191   }
192
193   while (sqlite3_step(stmt) == SQLITE_ROW) {
194     str = nullptr;
195     visibility = nullptr;
196     id = nullptr;
197     idx = 0;
198     _save_column_str(stmt, idx++, &str);
199     _save_column_str(stmt, idx++, &visibility);
200     _save_column_str(stmt, idx++, &id);
201     /* TODO: revise */
202     __parse_appcontrol(appcontrol, str, visibility, id);
203     free(str);
204     free(visibility);
205     free(id);
206   }
207
208   sqlite3_finalize(stmt);
209
210   return PMINFO_R_OK;
211 }
212
213 int _appinfo_get_category(sqlite3* db,
214     const char* appid, GList** category) {
215   static const char query_raw[] =
216       "SELECT category "
217       "FROM package_app_app_category WHERE app_id=%Q";
218   int ret;
219   char* query;
220   sqlite3_stmt* stmt;
221   char* val;
222
223   query = sqlite3_mprintf(query_raw, appid);
224   if (query == nullptr) {
225     LOGE("out of memory");
226     return PMINFO_R_ERROR;
227   }
228
229   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
230   sqlite3_free(query);
231   if (ret != SQLITE_OK) {
232     LOGE("prepare failed: %s", sqlite3_errmsg(db));
233     return PMINFO_R_ERROR;
234   }
235
236   while (sqlite3_step(stmt) == SQLITE_ROW) {
237     val = nullptr;
238     _save_column_str(stmt, 0, &val);
239     if (val)
240       *category = g_list_prepend(*category, (gpointer)val);
241   }
242
243   sqlite3_finalize(stmt);
244
245   return PMINFO_R_OK;
246 }
247
248 int _appinfo_get_res_control(sqlite3* db, const char* appid,
249     GList** res_control) {
250   static const char query_raw[] =
251     "SELECT res_type, min_res_version, max_res_version, auto_close "
252     "FROM package_app_res_control WHERE app_id=%Q";
253   int ret;
254   char* query;
255   sqlite3_stmt* stmt;
256   int idx;
257   res_control_x* info;
258
259   query = sqlite3_mprintf(query_raw, appid);
260   if (query == nullptr) {
261     LOGE("out of memory");
262     return PMINFO_R_ERROR;
263   }
264
265   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
266   sqlite3_free(query);
267   if (ret != SQLITE_OK) {
268     LOGE("prepare failed: %s", sqlite3_errmsg(db));
269     return PMINFO_R_ERROR;
270   }
271
272   while (sqlite3_step(stmt) == SQLITE_ROW) {
273     info = static_cast<res_control_x*>(calloc(1, sizeof(res_control_x)));
274     if (info == nullptr) {
275       LOGE("out of memory");
276       sqlite3_finalize(stmt);
277       return PMINFO_R_ERROR;
278     }
279     idx = 0;
280     _save_column_str(stmt, idx++, &info->res_type);
281     _save_column_str(stmt, idx++, &info->min_res_version);
282     _save_column_str(stmt, idx++, &info->max_res_version);
283     _save_column_str(stmt, idx++, &info->auto_close);
284     *res_control = g_list_prepend(*res_control, info);
285   }
286
287   sqlite3_finalize(stmt);
288
289   return PMINFO_R_OK;
290 }
291
292 GList* __get_background_category(const char* value) {
293   GList* category_list = nullptr;
294   int convert_value = 0;
295
296   if (!value || strlen(value) == 0)
297     return nullptr;
298
299   convert_value = atoi(value);
300   if (convert_value < 0)
301     return nullptr;
302
303   if (convert_value & APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL)
304     category_list = g_list_prepend(category_list,
305         strdup(APP_BG_CATEGORY_USER_DISABLE_TRUE_STR));
306   else
307     category_list = g_list_prepend(category_list,
308         strdup(APP_BG_CATEGORY_USER_DISABLE_FALSE_STR));
309
310   if (convert_value & APP_BG_CATEGORY_MEDIA_VAL)
311     category_list = g_list_prepend(category_list,
312         strdup(APP_BG_CATEGORY_MEDIA_STR));
313
314   if (convert_value & APP_BG_CATEGORY_DOWNLOAD_VAL)
315     category_list = g_list_prepend(category_list,
316         strdup(APP_BG_CATEGORY_DOWNLOAD_STR));
317
318   if (convert_value & APP_BG_CATEGORY_BGNETWORK_VAL)
319     category_list = g_list_prepend(category_list,
320         strdup(APP_BG_CATEGORY_BGNETWORK_STR));
321
322   if (convert_value & APP_BG_CATEGORY_LOCATION_VAL)
323     category_list = g_list_prepend(category_list,
324         strdup(APP_BG_CATEGORY_LOCATION_STR));
325
326   if (convert_value & APP_BG_CATEGORY_SENSOR_VAL)
327     category_list = g_list_prepend(category_list,
328         strdup(APP_BG_CATEGORY_SENSOR_STR));
329
330   if (convert_value & APP_BG_CATEGORY_IOTCOMM_VAL)
331     category_list = g_list_prepend(category_list,
332         strdup(APP_BG_CATEGORY_IOTCOMM_STR));
333
334   if (convert_value & APP_BG_CATEGORY_SYSTEM_VAL)
335     category_list = g_list_prepend(category_list,
336         strdup(APP_BG_CATEGORY_SYSTEM));
337
338   return category_list;
339 }
340
341 int __bind_params(sqlite3_stmt* stmt, GList* params) {
342   GList* tmp_list = nullptr;
343   int idx = 0;
344   int ret;
345
346   if (stmt == nullptr || params == nullptr)
347     return PMINFO_R_EINVAL;
348
349   tmp_list = params;
350   while (tmp_list) {
351     ret = sqlite3_bind_text(stmt, ++idx,
352         (char*)tmp_list->data, -1, SQLITE_STATIC);
353     if (ret != SQLITE_OK)
354       return PMINFO_R_ERROR;
355     tmp_list = tmp_list->next;
356   }
357
358   return PMINFO_R_OK;
359 }
360
361 constexpr const char join_localized_info[] =
362     " LEFT OUTER JOIN package_app_localized_info"
363     " ON ai.app_id=package_app_localized_info.app_id"
364     " AND package_app_localized_info.app_locale=?";
365 constexpr const char join_category[] =
366     " LEFT OUTER JOIN package_app_app_category"
367     " ON ai.app_id=package_app_app_category.app_id";
368 constexpr const char join_app_control[] =
369     " LEFT OUTER JOIN package_app_app_control"
370     " ON ai.app_id=package_app_app_control.app_id";
371 constexpr const char join_metadata[] =
372     " LEFT OUTER JOIN package_app_app_metadata"
373     " ON ai.app_id=package_app_app_metadata.app_id ";
374 constexpr const char join_privilege[] =
375     " LEFT OUTER JOIN package_privilege_info"
376     " ON ai.package=package_privilege_info.package ";
377
378 int _get_filtered_query(pkgmgrinfo_filter_x* filter, const char* locale,
379     uid_t uid, char** query, GList** bind_params) {
380   int joined = 0;
381   int size;
382   char* condition = nullptr;
383   char buf[MAX_QUERY_LEN] = {'\0'};
384   char tmp_query[MAX_QUERY_LEN] = {'\0'};
385   GSList* list;
386
387   if (!filter)
388     return PMINFO_R_OK;
389
390   if (filter->cache_flag) {
391     joined = E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO |
392       E_PMINFO_APPINFO_JOIN_CATEGORY |
393       E_PMINFO_APPINFO_JOIN_APP_CONTROL |
394       E_PMINFO_APPINFO_JOIN_METADATA |
395       E_PMINFO_APPINFO_JOIN_PRIVILEGE;
396   }
397
398   strncat(buf, " WHERE 1=1", sizeof(buf) - strlen(buf) - 1);
399
400   for (list = filter->list; list; list = list->next) {
401     joined |= __get_filter_condition(list->data,
402         uid, &condition, bind_params);
403     if (condition == nullptr)
404       continue;
405
406     strncat(buf, " AND ", sizeof(buf) - strlen(buf) - 1);
407
408     strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
409     free(condition);
410     condition = nullptr;
411   }
412
413   if (filter->list_metadata)
414     strncat(buf, " AND (", sizeof(buf) - strlen(buf) - 1);
415   for (list = filter->list_metadata; list; list = list->next) {
416     joined |= __get_metadata_filter_condition(list->data,
417         &condition, bind_params);
418     if (condition == nullptr)
419       continue;
420     strncat(buf, condition, sizeof(buf) - strlen(buf) - 1);
421     free(condition);
422     condition = nullptr;
423
424     strncat(buf, " OR ", sizeof(buf) - strlen(buf) - 1);
425   }
426   if (filter->list_metadata)
427     strncat(buf, "1=0)", sizeof(buf) - strlen(buf) - 1);
428
429   if (joined & E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO) {
430     strncat(tmp_query, join_localized_info,
431         sizeof(tmp_query) - strlen(tmp_query) - 1);
432     *bind_params = g_list_append(*bind_params, strdup(locale));
433   }
434   if (joined & E_PMINFO_APPINFO_JOIN_CATEGORY)
435     strncat(tmp_query, join_category,
436         sizeof(tmp_query) - strlen(tmp_query) - 1);
437   if (joined & E_PMINFO_APPINFO_JOIN_APP_CONTROL)
438     strncat(tmp_query, join_app_control,
439         sizeof(tmp_query) - strlen(tmp_query) - 1);
440   if (joined & E_PMINFO_APPINFO_JOIN_METADATA)
441     strncat(tmp_query, join_metadata,
442         sizeof(tmp_query) - strlen(tmp_query) - 1);
443   if (joined & E_PMINFO_APPINFO_JOIN_PRIVILEGE)
444     strncat(tmp_query, join_privilege,
445         sizeof(tmp_query) - strlen(tmp_query) - 1);
446
447   size = strlen(tmp_query) + strlen(buf) + 1;
448   *query = static_cast<char*>(calloc(1, size));
449   if (*query == nullptr)
450     return PMINFO_R_ERROR;
451   snprintf(*query, size, "%s%s", tmp_query, buf);
452
453   return PMINFO_R_OK;
454 }
455
456 bool __check_app_storage_status(pkgmgrinfo_filter_x* tmp_filter) {
457   GSList* tmp_list = nullptr;
458   pkgmgrinfo_node_x* tmp_node = nullptr;
459   int property = -1;
460
461   if (tmp_filter == nullptr)
462     return true;
463
464   if (tmp_filter->cache_flag)
465     return false;
466
467   property = _pminfo_appinfo_convert_to_prop_bool(
468       PMINFO_APPINFO_PROP_APP_CHECK_STORAGE);
469   for (tmp_list = tmp_filter->list; tmp_list != nullptr;
470       tmp_list = g_slist_next(tmp_list)) {
471     tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
472     if (property == tmp_node->prop) {
473       if (strcmp(tmp_node->value, "true") == 0)
474         return true;
475       else
476         return false;
477     }
478   }
479
480   return true;
481 }
482
483 int _appinfo_get_applications(sqlite3* db, uid_t db_uid, uid_t uid,
484     const char* locale, pkgmgrinfo_filter_x* filter, int flag,
485     std::vector<std::shared_ptr<application_x>>& applications) {
486   static const char query_raw[] =
487       "SELECT DISTINCT ai.app_id, ai.app_installed_storage, "
488       "ai.app_external_path";
489   static const char query_basic[] =
490       ", ai.app_component, ai.app_exec, "
491       "ai.app_nodisplay, ai.app_type, ai.app_onboot, "
492       "ai.app_multiple, ai.app_autorestart, ai.app_taskmanage, "
493       "ai.app_hwacceleration, ai.app_screenreader, "
494       "ai.app_mainapp, ai.app_recentimage, ai.app_launchcondition, "
495       "ai.app_indicatordisplay, ai.app_portraitimg, "
496       "ai.app_landscapeimg, ai.app_guestmodevisibility, "
497       "ai.app_permissiontype, ai.app_preload, ai.app_submode, "
498       "ai.app_submode_mainid, ai.app_launch_mode, ai.app_ui_gadget, "
499       "ai.app_support_disable, ai.app_process_pool, "
500       "ai.app_background_category, ai.app_package_type, "
501       "ai.app_root_path, ai.app_api_version, ai.app_effective_appid, "
502       "ai.app_disable, ai.app_splash_screen_display, ai.app_tep_name, "
503       "ai.app_zip_mount_file, ai.component_type, ai.package, "
504       "ai.app_package_system, ai.app_removable, "
505       "ai.app_package_installed_time, ai.app_support_mode, "
506       "ai.app_support_ambient, ai.app_setup_appid, ai.light_user_switch_mode";
507   static const char query_uid_info[] =
508       ", ui.is_disabled, ui.is_splash_screen_enabled";
509   static const char query_label[] =
510       ", COALESCE("
511       "(SELECT app_label FROM package_app_localized_info WHERE "
512       "ai.app_id=app_id AND app_locale=?), "
513       "(SELECT app_label FROM package_app_localized_info WHERE "
514       "ai.app_id=app_id AND app_locale='No Locale'))";
515   static const char query_icon[] =
516       ", COALESCE("
517       "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id "
518       "AND app_locale=?), "
519       "(SELECT app_icon FROM package_app_localized_info WHERE ai.app_id=app_id "
520       "AND app_locale='No Locale'))";
521   static const char query_from_clause[] = " FROM package_app_info as ai";
522   static const char query_uid_info_clause[] =
523       " LEFT OUTER JOIN package_app_info_for_uid AS ui "
524       "ON (ai.app_id=ui.app_id AND ui.uid=?)";
525   int ret = PMINFO_R_ERROR;
526   int idx;
527   char* bg_category_str = nullptr;
528   char* constraint = nullptr;
529   char* tmp_record = nullptr;
530   char query[MAX_QUERY_LEN] = {'\0'};
531   char buf[BUFSIZE] = {'\0'};
532   application_x* info = nullptr;
533   GList* bind_params = nullptr;
534   sqlite3_stmt* stmt = nullptr;
535   bool is_check_storage = true;
536   const uid_t global_user_uid = GLOBAL_USER;
537
538   snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
539
540   if (flag & PMINFO_APPINFO_GET_BASICINFO) {
541     strncat(query, query_basic, sizeof(query) - strlen(query) - 1);
542     strncat(query, query_uid_info,
543         sizeof(query) - strlen(query) - 1);
544   }
545   if (flag & PMINFO_APPINFO_GET_LABEL) {
546     strncat(query, query_label, sizeof(query) - strlen(query) - 1);
547     bind_params = g_list_append(bind_params, strdup(locale));
548   }
549   if (flag & PMINFO_APPINFO_GET_ICON) {
550     strncat(query, query_icon, sizeof(query) - strlen(query) - 1);
551     bind_params = g_list_append(bind_params, strdup(locale));
552   }
553
554   snprintf(buf, MAX_QUERY_LEN - 1, "%d", uid);
555   bind_params = g_list_append(bind_params, strdup(buf));
556
557   is_check_storage = __check_app_storage_status(filter);
558
559   ret = _get_filtered_query(filter, locale,
560       uid, &constraint, &bind_params);
561   if (ret != PMINFO_R_OK) {
562     LOGE("Failed to get WHERE clause");
563     goto __catch;
564   }
565   strncat(query, query_from_clause, sizeof(query) - strlen(query) - 1);
566
567   strncat(query, query_uid_info_clause,
568       sizeof(query) - strlen(query) - 1);
569
570   if (constraint)
571     strncat(query, constraint, sizeof(query) - strlen(query) - 1);
572
573   ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, nullptr);
574   if (ret != SQLITE_OK) {
575     LOGE("prepare failed: %s", sqlite3_errmsg(db));
576     ret = PMINFO_R_ERROR;
577     goto __catch;
578   }
579
580   if (g_list_length(bind_params) != 0) {
581     ret = __bind_params(stmt, bind_params);
582     if (ret != SQLITE_OK) {
583       LOGE("Failed to bind parameters");
584       goto __catch;
585     }
586   }
587
588   while (sqlite3_step(stmt) == SQLITE_ROW) {
589     info = static_cast<application_x*>(calloc(1, sizeof(application_x)));
590     if (info == nullptr) {
591       LOGE("out of memory");
592       ret = PMINFO_R_ERROR;
593       goto __catch;
594     }
595     info->locale = strdup(locale);
596     if (info->locale == nullptr) {
597       LOGE("Out of memory");
598       ret = PMINFO_R_ERROR;
599       goto __catch;
600     }
601
602     idx = 0;
603     _save_column_str(stmt, idx++, &info->appid);
604     _save_column_str(stmt, idx++, &info->installed_storage);
605     _save_column_str(stmt, idx++, &info->external_path);
606
607     if (flag & PMINFO_APPINFO_GET_BASICINFO) {
608       _save_column_str(stmt, idx++, &info->component);
609       _save_column_str(stmt, idx++, &info->exec);
610       _save_column_str(stmt, idx++, &info->nodisplay);
611       _save_column_str(stmt, idx++, &info->type);
612       _save_column_str(stmt, idx++, &info->onboot);
613       _save_column_str(stmt, idx++, &info->multiple);
614       _save_column_str(stmt, idx++, &info->autorestart);
615       _save_column_str(stmt, idx++, &info->taskmanage);
616       _save_column_str(stmt, idx++, &info->hwacceleration);
617       _save_column_str(stmt, idx++, &info->screenreader);
618       _save_column_str(stmt, idx++, &info->mainapp);
619       _save_column_str(stmt, idx++, &info->recentimage);
620       _save_column_str(stmt, idx++, &info->launchcondition);
621       _save_column_str(stmt, idx++, &info->indicatordisplay);
622       _save_column_str(stmt, idx++, &info->portraitimg);
623       _save_column_str(stmt, idx++, &info->landscapeimg);
624       _save_column_str(stmt, idx++,
625           &info->guestmode_visibility);
626       _save_column_str(stmt, idx++, &info->permission_type);
627       _save_column_str(stmt, idx++, &info->preload);
628       _save_column_str(stmt, idx++, &info->submode);
629       _save_column_str(stmt, idx++, &info->submode_mainid);
630       _save_column_str(stmt, idx++, &info->launch_mode);
631       _save_column_str(stmt, idx++, &info->ui_gadget);
632       _save_column_str(stmt, idx++, &info->support_disable);
633       _save_column_str(stmt, idx++, &info->process_pool);
634       _save_column_str(stmt, idx++, &bg_category_str);
635       _save_column_str(stmt, idx++, &info->package_type);
636       _save_column_str(stmt, idx++, &info->root_path);
637       _save_column_str(stmt, idx++, &info->api_version);
638       _save_column_str(stmt, idx++, &info->effective_appid);
639       _save_column_str(stmt, idx++, &info->is_disabled);
640       _save_column_str(stmt, idx++,
641           &info->splash_screen_display);
642       _save_column_str(stmt, idx++, &info->tep_name);
643       _save_column_str(stmt, idx++, &info->zip_mount_file);
644       _save_column_str(stmt, idx++, &info->component_type);
645       _save_column_str(stmt, idx++, &info->package);
646       _save_column_str(stmt, idx++, &info->package_system);
647       _save_column_str(stmt, idx++, &info->removable);
648       _save_column_str(stmt, idx++,
649           &info->package_installed_time);
650       _save_column_str(stmt, idx++, &info->support_mode);
651       _save_column_str(stmt, idx++, &info->support_ambient);
652       _save_column_str(stmt, idx++, &info->setup_appid);
653       _save_column_str(stmt, idx++, &info->light_user_switch_mode);
654       info->background_category = __get_background_category(
655             bg_category_str);
656       free(bg_category_str);
657       bg_category_str = nullptr;
658     }
659
660     info->for_all_users =
661         strdup((db_uid != global_user_uid) ?
662             "false" : "true");
663
664     if (db_uid != global_user_uid) {
665       idx = idx + 2;
666     } else {
667       tmp_record = nullptr;
668       _save_column_str(stmt, idx++, &tmp_record);
669       if (tmp_record != nullptr) {
670         if (strcasecmp(info->is_disabled, "false") == 0 &&
671             strcasecmp(tmp_record, "false") == 0) {
672           free(info->is_disabled);
673           info->is_disabled = tmp_record;
674         } else {
675           free(tmp_record);
676         }
677       }
678       tmp_record = nullptr;
679       _save_column_str(stmt, idx++, &tmp_record);
680       if (tmp_record != nullptr) {
681         if (strcasecmp(info->splash_screen_display, "false") == 0 &&
682             strcasecmp(tmp_record, "false") == 0) {
683           free(info->splash_screen_display);
684           info->splash_screen_display = tmp_record;
685         } else {
686           free(tmp_record);
687         }
688       }
689     }
690
691     if (flag & PMINFO_APPINFO_GET_LABEL) {
692       tmp_record = nullptr;
693       _save_column_str(stmt, idx++, &tmp_record);
694       if (_add_label_info_into_list(locale, tmp_record,
695           &info->label)) {
696         ret = PMINFO_R_ERROR;
697         goto __catch;
698       }
699     }
700
701     if (flag & PMINFO_APPINFO_GET_ICON) {
702       tmp_record = nullptr;
703       _save_column_str(stmt, idx++, &tmp_record);
704       if (_add_icon_info_into_list(locale, tmp_record,
705           &info->icon)) {
706         ret = PMINFO_R_ERROR;
707         goto __catch;
708       }
709     }
710
711     if (flag & PMINFO_APPINFO_GET_CATEGORY) {
712       if (_appinfo_get_category(db, info->appid,
713           &info->category)) {
714         ret = PMINFO_R_ERROR;
715         goto __catch;
716       }
717     }
718
719     if (flag & PMINFO_APPINFO_GET_APP_CONTROL) {
720       if (_appinfo_get_app_control(db, info->appid,
721           &info->appcontrol)) {
722         ret = PMINFO_R_ERROR;
723         goto __catch;
724       }
725     }
726
727     if (flag & PMINFO_APPINFO_GET_METADATA) {
728       if (_appinfo_get_metadata(db, info->appid,
729           &info->metadata)) {
730         ret = PMINFO_R_ERROR;
731         goto __catch;
732       }
733     }
734
735     if (flag & PMINFO_APPINFO_GET_SPLASH_SCREEN) {
736       if (_appinfo_get_splashscreens(db, info->appid,
737           &info->splashscreens)) {
738         ret = PMINFO_R_ERROR;
739         goto __catch;
740       }
741     }
742
743     if (flag & PMINFO_APPINFO_GET_RES_CONTROL) {
744       if (_appinfo_get_res_control(db, info->appid,
745           &info->res_control)) {
746         ret = PMINFO_R_ERROR;
747         goto __catch;
748       }
749     }
750
751     if (is_check_storage &&
752         __appinfo_check_installed_storage(info) !=
753             PMINFO_R_OK) {
754       ret = PMINFO_R_ERROR;
755       pkgmgrinfo_basic_free_application(info);
756       info = nullptr;
757       continue;
758     }
759
760     applications.emplace_back(info, pkgmgrinfo_basic_free_application);
761   }
762
763   ret = PMINFO_R_OK;
764
765 __catch:
766   sqlite3_finalize(stmt);
767
768   if (constraint)
769     free(constraint);
770
771   if (ret != PMINFO_R_OK && info != nullptr)
772     pkgmgrinfo_basic_free_application(info);
773
774   g_list_free_full(bind_params, free);
775
776   return ret;
777 }
778
779 }  // namespace
780
781 namespace pkgmgr_server {
782 namespace internal {
783
784 API bool check_app_storage_status(pkgmgrinfo_filter_x* tmp_filter) {
785   return ::__check_app_storage_status(tmp_filter);
786 }
787
788 API int appinfo_internal_filter_get_list(sqlite3* db,
789     pkgmgrinfo_appinfo_filter_h filter, uid_t db_uid, uid_t uid,
790     const char* locale,
791     std::vector<std::shared_ptr<application_x>>& appinfo_list) {
792   if (db == nullptr || filter == nullptr) {
793     LOGE("Invalid argument");
794     return PMINFO_R_EINVAL;
795   }
796
797   return ::_appinfo_get_applications(db, db_uid, uid, locale,
798       static_cast<pkgmgrinfo_filter_x*>(filter), PMINFO_APPINFO_GET_ALL,
799       appinfo_list);
800 }
801
802 }  // namesapce internal
803 }  // namesapce pkgmgr_server