Revert "Change pkg db journaling mode to WAL"
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_private.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <dlfcn.h>
28
29 #include <vconf.h>
30 #include <sqlite3.h>
31 #include <glib.h>
32 #include <unzip.h>
33
34 #include "pkgmgr-info.h"
35 #include "pkgmgrinfo_debug.h"
36 #include "pkgmgrinfo_private.h"
37 #include "pkgmgr_parser.h"
38
39 static GHashTable *plugin_set_list;
40
41 struct _pkginfo_str_map_t {
42         pkgmgrinfo_pkginfo_filter_prop_str prop;
43         const char *property;
44 };
45
46 static struct _pkginfo_str_map_t pkginfo_str_prop_map[] = {
47         {E_PMINFO_PKGINFO_PROP_PACKAGE_ID,      PMINFO_PKGINFO_PROP_PACKAGE_ID},
48         {E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE,    PMINFO_PKGINFO_PROP_PACKAGE_TYPE},
49         {E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION, PMINFO_PKGINFO_PROP_PACKAGE_VERSION},
50         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION,        PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION},
51         {E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE,       PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE},
52         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME},
53         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL,    PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL},
54         {E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF,     PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF},
55         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE,       PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE}
56 };
57
58 struct _pkginfo_int_map_t {
59         pkgmgrinfo_pkginfo_filter_prop_int prop;
60         const char *property;
61 };
62
63 static struct _pkginfo_int_map_t pkginfo_int_prop_map[] = {
64         {E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE,    PMINFO_PKGINFO_PROP_PACKAGE_SIZE}
65 };
66
67 struct _pkginfo_bool_map_t {
68         pkgmgrinfo_pkginfo_filter_prop_bool prop;
69         const char *property;
70 };
71
72 static struct _pkginfo_bool_map_t pkginfo_bool_prop_map[] = {
73         {E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE,       PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE},
74         {E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD,         PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD},
75         {E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY,        PMINFO_PKGINFO_PROP_PACKAGE_READONLY},
76         {E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE,          PMINFO_PKGINFO_PROP_PACKAGE_UPDATE},
77         {E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING,      PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING},
78         {E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING,       PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING},
79         {E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE},
80         {E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE, PMINFO_PKGINFO_PROP_PACKAGE_DISABLE},
81         {E_PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE,   PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE},
82         {E_PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM,          PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM}
83 };
84
85 struct _appinfo_str_map_t {
86         pkgmgrinfo_appinfo_filter_prop_str prop;
87         const char *property;
88 };
89
90 static struct _appinfo_str_map_t appinfo_str_prop_map[] = {
91         {E_PMINFO_APPINFO_PROP_APP_ID,          PMINFO_APPINFO_PROP_APP_ID},
92         {E_PMINFO_APPINFO_PROP_APP_COMPONENT,   PMINFO_APPINFO_PROP_APP_COMPONENT},
93         {E_PMINFO_APPINFO_PROP_APP_EXEC,        PMINFO_APPINFO_PROP_APP_EXEC},
94         {E_PMINFO_APPINFO_PROP_APP_ICON,        PMINFO_APPINFO_PROP_APP_ICON},
95         {E_PMINFO_APPINFO_PROP_APP_TYPE,        PMINFO_APPINFO_PROP_APP_TYPE},
96         {E_PMINFO_APPINFO_PROP_APP_OPERATION,   PMINFO_APPINFO_PROP_APP_OPERATION},
97         {E_PMINFO_APPINFO_PROP_APP_URI, PMINFO_APPINFO_PROP_APP_URI},
98         {E_PMINFO_APPINFO_PROP_APP_MIME,        PMINFO_APPINFO_PROP_APP_MIME},
99         {E_PMINFO_APPINFO_PROP_APP_CATEGORY,    PMINFO_APPINFO_PROP_APP_CATEGORY},
100         {E_PMINFO_APPINFO_PROP_APP_HWACCELERATION,      PMINFO_APPINFO_PROP_APP_HWACCELERATION},
101         {E_PMINFO_APPINFO_PROP_APP_SCREENREADER,        PMINFO_APPINFO_PROP_APP_SCREENREADER},
102         {E_PMINFO_APPINFO_PROP_APP_PACKAGE,     PMINFO_APPINFO_PROP_APP_PACKAGE},
103         {E_PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE,   PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE},
104         {E_PMINFO_APPINFO_PROP_PRIVILEGE,       PMINFO_APPINFO_PROP_PRIVILEGE}
105 };
106
107 struct _appinfo_int_map_t {
108         pkgmgrinfo_appinfo_filter_prop_int prop;
109         const char *property;
110 };
111
112 static struct _appinfo_int_map_t appinfo_int_prop_map[] = {
113         {E_PMINFO_APPINFO_PROP_APP_SUPPORT_MODE,        PMINFO_APPINFO_PROP_APP_SUPPORT_MODE}
114 };
115
116 struct _appinfo_bool_map_t {
117         pkgmgrinfo_appinfo_filter_prop_bool prop;
118         const char *property;
119 };
120
121 static struct _appinfo_bool_map_t appinfo_bool_prop_map[] = {
122         {E_PMINFO_APPINFO_PROP_APP_NODISPLAY,           PMINFO_APPINFO_PROP_APP_NODISPLAY},
123         {E_PMINFO_APPINFO_PROP_APP_MULTIPLE,            PMINFO_APPINFO_PROP_APP_MULTIPLE},
124         {E_PMINFO_APPINFO_PROP_APP_ONBOOT,              PMINFO_APPINFO_PROP_APP_ONBOOT},
125         {E_PMINFO_APPINFO_PROP_APP_AUTORESTART,         PMINFO_APPINFO_PROP_APP_AUTORESTART},
126         {E_PMINFO_APPINFO_PROP_APP_TASKMANAGE,          PMINFO_APPINFO_PROP_APP_TASKMANAGE},
127         {E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION,             PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION},
128         {E_PMINFO_APPINFO_PROP_APP_UI_GADGET,           PMINFO_APPINFO_PROP_APP_UI_GADGET},
129         {E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE,             PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE},
130         {E_PMINFO_APPINFO_PROP_APP_DISABLE,             PMINFO_APPINFO_PROP_APP_DISABLE},
131         {E_PMINFO_APPINFO_PROP_APP_CHECK_STORAGE,               PMINFO_APPINFO_PROP_APP_CHECK_STORAGE},
132         {E_PMINFO_APPINFO_PROP_PKG_DISABLE,             PMINFO_APPINFO_PROP_PKG_DISABLE}
133 };
134
135 inline pkgmgrinfo_pkginfo_filter_prop_str _pminfo_pkginfo_convert_to_prop_str(const char *property)
136 {
137         int i = 0;
138         int max = 0;
139         pkgmgrinfo_pkginfo_filter_prop_str prop = -1;
140
141         if (property == NULL)
142                 return -1;
143         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_STR - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_STR + 1;
144         for (i = 0 ; i < max; i++) {
145                 if (strcmp(property, pkginfo_str_prop_map[i].property) == 0) {
146                         prop =  pkginfo_str_prop_map[i].prop;
147                         break;
148                 }
149         }
150         return prop;
151 }
152
153 inline pkgmgrinfo_pkginfo_filter_prop_int _pminfo_pkginfo_convert_to_prop_int(const char *property)
154 {
155         int i = 0;
156         int max = 0;
157         pkgmgrinfo_pkginfo_filter_prop_int prop = -1;
158
159         if (property == NULL)
160                 return -1;
161         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_INT - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_INT + 1;
162         for (i = 0 ; i < max; i++) {
163                 if (strcmp(property, pkginfo_int_prop_map[i].property) == 0) {
164                         prop =  pkginfo_int_prop_map[i].prop;
165                         break;
166                 }
167         }
168         return prop;
169 }
170
171 inline pkgmgrinfo_pkginfo_filter_prop_bool _pminfo_pkginfo_convert_to_prop_bool(const char *property)
172 {
173         int i = 0;
174         int max = 0;
175         pkgmgrinfo_pkginfo_filter_prop_bool prop = -1;
176
177         if (property == NULL)
178                 return -1;
179         max = E_PMINFO_PKGINFO_PROP_PACKAGE_MAX_BOOL - E_PMINFO_PKGINFO_PROP_PACKAGE_MIN_BOOL + 1;
180         for (i = 0 ; i < max; i++) {
181                 if (strcmp(property, pkginfo_bool_prop_map[i].property) == 0) {
182                         prop =  pkginfo_bool_prop_map[i].prop;
183                         break;
184                 }
185         }
186         return prop;
187 }
188
189 inline pkgmgrinfo_appinfo_filter_prop_str _pminfo_appinfo_convert_to_prop_str(const char *property)
190 {
191         int i = 0;
192         int max = 0;
193         pkgmgrinfo_appinfo_filter_prop_str prop = -1;
194
195         if (property == NULL)
196                 return -1;
197         max = E_PMINFO_APPINFO_PROP_APP_MAX_STR - E_PMINFO_APPINFO_PROP_APP_MIN_STR + 1;
198         for (i = 0 ; i < max; i++) {
199                 if (strcmp(property, appinfo_str_prop_map[i].property) == 0) {
200                         prop =  appinfo_str_prop_map[i].prop;
201                         break;
202                 }
203         }
204         return prop;
205 }
206
207 inline pkgmgrinfo_appinfo_filter_prop_int _pminfo_appinfo_convert_to_prop_int(const char *property)
208 {
209         int i = 0;
210         int max = 0;
211         pkgmgrinfo_appinfo_filter_prop_int prop = -1;
212
213         if (property == NULL)
214                 return -1;
215         max = E_PMINFO_APPINFO_PROP_APP_MAX_INT - E_PMINFO_APPINFO_PROP_APP_MIN_INT + 1;
216         for (i = 0 ; i < max; i++) {
217                 if (strcmp(property, appinfo_int_prop_map[i].property) == 0) {
218                         prop =  appinfo_int_prop_map[i].prop;
219                         break;
220                 }
221         }
222         return prop;
223 }
224
225 inline pkgmgrinfo_appinfo_filter_prop_bool _pminfo_appinfo_convert_to_prop_bool(const char *property)
226 {
227         int i = 0;
228         int max = 0;
229         pkgmgrinfo_appinfo_filter_prop_bool prop = -1;
230
231         if (property == NULL)
232                 return -1;
233         max = E_PMINFO_APPINFO_PROP_APP_MAX_BOOL - E_PMINFO_APPINFO_PROP_APP_MIN_BOOL + 1;
234         for (i = 0 ; i < max; i++) {
235                 if (strcmp(property, appinfo_bool_prop_map[i].property) == 0) {
236                         prop =  appinfo_bool_prop_map[i].prop;
237                         break;
238                 }
239         }
240         return prop;
241 }
242
243 int __get_filter_condition(gpointer data, uid_t uid, char **condition, GList **params)
244 {
245         pkgmgrinfo_node_x *node = (pkgmgrinfo_node_x *)data;
246         char buf[MAX_QUERY_LEN] = {'\0'};
247         int flag = 0;
248         char *value;
249         char *ptr = NULL;
250         char *saveptr = NULL;
251
252         switch (node->prop) {
253         case E_PMINFO_PKGINFO_PROP_PACKAGE_ID:
254                 snprintf(buf, sizeof(buf), "pi.package=?");
255                 break;
256         case E_PMINFO_PKGINFO_PROP_PACKAGE_TYPE:
257                 snprintf(buf, sizeof(buf), "pi.package_type=?");
258                 break;
259         case E_PMINFO_PKGINFO_PROP_PACKAGE_VERSION:
260                 snprintf(buf, sizeof(buf), "pi.package_version=?");
261                 break;
262         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALL_LOCATION:
263                 snprintf(buf, sizeof(buf), "pi.install_location=?");
264                 break;
265         case E_PMINFO_PKGINFO_PROP_PACKAGE_INSTALLED_STORAGE:
266                 snprintf(buf, sizeof(buf), "pi.installed_storage=?");
267                 break;
268         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_NAME:
269                 snprintf(buf, sizeof(buf), "pi.author_name=?");
270                 break;
271         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_HREF:
272                 snprintf(buf, sizeof(buf), "pi.author_href=?");
273                 break;
274         case E_PMINFO_PKGINFO_PROP_PACKAGE_AUTHOR_EMAIL:
275                 snprintf(buf, sizeof(buf), "pi.author_email=?");
276                 break;
277         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE:
278                 snprintf(buf, sizeof(buf), "package_privilege_info.privilege=?");
279                 flag = E_PMINFO_PKGINFO_JOIN_PRIVILEGE_INFO;
280                 break;
281         case E_PMINFO_PKGINFO_PROP_PACKAGE_SIZE:
282                 snprintf(buf, sizeof(buf), "pi.package_size=?");
283                 break;
284         case E_PMINFO_PKGINFO_PROP_PACKAGE_REMOVABLE:
285                 snprintf(buf, sizeof(buf), "pi.package_removable=? COLLATE NOCASE");
286                 break;
287         case E_PMINFO_PKGINFO_PROP_PACKAGE_PRELOAD:
288                 snprintf(buf, sizeof(buf), "pi.package_preload=? COLLATE NOCASE");
289                 break;
290         case E_PMINFO_PKGINFO_PROP_PACKAGE_READONLY:
291                 snprintf(buf, sizeof(buf), "pi.package_readonly=? COLLATE NOCASE");
292                 break;
293         case E_PMINFO_PKGINFO_PROP_PACKAGE_UPDATE:
294                 snprintf(buf, sizeof(buf), "pi.package_update=? COLLATE NOCASE");
295                 break;
296         case E_PMINFO_PKGINFO_PROP_PACKAGE_APPSETTING:
297                 snprintf(buf, sizeof(buf), "pi.package_appsetting=? COLLATE NOCASE");
298                 break;
299         case E_PMINFO_PKGINFO_PROP_PACKAGE_NODISPLAY_SETTING:
300                 snprintf(buf, sizeof(buf), "pi.package_nodisplay=? COLLATE NOCASE");
301                 break;
302         case E_PMINFO_PKGINFO_PROP_PACKAGE_SUPPORT_DISABLE:
303                 snprintf(buf, sizeof(buf), "pi.package_support_disable=? COLLATE NOCASE");
304                 break;
305         case E_PMINFO_PKGINFO_PROP_PACKAGE_DISABLE:
306                 snprintf(buf, sizeof(buf), "pi.package_disable=? COLLATE NOCASE");
307                 break;
308         case E_PMINFO_PKGINFO_PROP_PACKAGE_SYSTEM:
309                 snprintf(buf, sizeof(buf), "pi.package_system=? COLLATE NOCASE");
310                 break;
311
312         case E_PMINFO_APPINFO_PROP_APP_ID:
313                 snprintf(buf, sizeof(buf), "ai.app_id=?");
314                 break;
315         case E_PMINFO_APPINFO_PROP_APP_COMPONENT:
316                 snprintf(buf, sizeof(buf), "ai.app_component=?");
317                 break;
318         case E_PMINFO_APPINFO_PROP_APP_EXEC:
319                 snprintf(buf, sizeof(buf), "ai.app_exec=?");
320                 break;
321         case E_PMINFO_APPINFO_PROP_APP_ICON:
322                 snprintf(buf, sizeof(buf), "package_app_localized_info.app_icon=?");
323                 flag = E_PMINFO_APPINFO_JOIN_LOCALIZED_INFO;
324                 break;
325         case E_PMINFO_APPINFO_PROP_APP_TYPE:
326                 snprintf(buf, sizeof(buf), "ai.app_type=?");
327                 break;
328         case E_PMINFO_APPINFO_PROP_APP_OPERATION:
329                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE ?||'|%%%%|%%%%'");
330                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
331                 break;
332         case E_PMINFO_APPINFO_PROP_APP_URI:
333                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE '%%%%|'||?||'|%%%%'");
334                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
335                 break;
336         case E_PMINFO_APPINFO_PROP_APP_MIME:
337                 snprintf(buf, sizeof(buf), "package_app_app_control.app_control LIKE '%%%%|%%%%|'||?");
338                 flag = E_PMINFO_APPINFO_JOIN_APP_CONTROL;
339                 break;
340         case E_PMINFO_APPINFO_PROP_APP_CATEGORY:
341                 snprintf(buf, sizeof(buf), "package_app_app_category.category IN (");
342                 value = strdup(node->value);
343                 if (value == NULL) {
344                         _LOGE("out of memeory");
345                         return 0;
346                 }
347                 ptr = strtok_r(value, ",", &saveptr);
348                 if (ptr == NULL) {
349                         free(value);
350                         return 0;
351                 }
352                 strncat(buf, "?", sizeof(buf) - strlen(buf) - 1);
353                 *params = g_list_append(*params, strdup(ptr));
354                 while ((ptr = strtok_r(NULL, ",", &saveptr))) {
355                         strncat(buf, ", ?", sizeof(buf) - strlen(buf) - 1);
356                         *params = g_list_append(*params, strdup(ptr));
357                 }
358                 strncat(buf, ")", sizeof(buf) - strlen(buf) - 1);
359                 *condition = strdup(buf);
360                 flag = E_PMINFO_APPINFO_JOIN_CATEGORY;
361                 free(value);
362
363                 return flag;
364         case E_PMINFO_APPINFO_PROP_APP_NODISPLAY:
365                 snprintf(buf, sizeof(buf), "ai.app_nodisplay=? COLLATE NOCASE");
366                 break;
367         case E_PMINFO_APPINFO_PROP_APP_MULTIPLE:
368                 snprintf(buf, sizeof(buf), "ai.app_multiple=? COLLATE NOCASE");
369                 break;
370         case E_PMINFO_APPINFO_PROP_APP_ONBOOT:
371                 snprintf(buf, sizeof(buf), "ai.app_onboot=? COLLATE NOCASE");
372                 break;
373         case E_PMINFO_APPINFO_PROP_APP_AUTORESTART:
374                 snprintf(buf, sizeof(buf), "ai.app_autorestart=? COLLATE NOCASE");
375                 break;
376         case E_PMINFO_APPINFO_PROP_APP_TASKMANAGE:
377                 snprintf(buf, sizeof(buf), "ai.app_taskmanage=? COLLATE NOCASE");
378                 break;
379         case E_PMINFO_APPINFO_PROP_APP_HWACCELERATION:
380                 snprintf(buf, sizeof(buf), "ai.app_hwacceleration=?");
381                 break;
382         case E_PMINFO_APPINFO_PROP_APP_SCREENREADER:
383                 snprintf(buf, sizeof(buf), "ai.app_screenreader=?");
384                 break;
385         case E_PMINFO_APPINFO_PROP_APP_LAUNCHCONDITION:
386                 snprintf(buf, sizeof(buf), "ai.app_launchcondition=? COLLATE NOCASE");
387                 break;
388         case E_PMINFO_APPINFO_PROP_APP_PACKAGE:
389                 snprintf(buf, sizeof(buf), "ai.package=?");
390                 break;
391         case E_PMINFO_APPINFO_PROP_APP_INSTALLED_STORAGE:
392                 snprintf(buf, sizeof(buf), "ai.app_installed_storage=?");
393                 break;
394         case E_PMINFO_APPINFO_PROP_APP_UI_GADGET:
395                 snprintf(buf, sizeof(buf), "ai.app_ui_gadget=? COLLATE NOCASE");
396                 break;
397         case E_PMINFO_APPINFO_PROP_APP_DISABLE:
398                 if (strcasecmp(node->value, "true") == 0)
399                         snprintf(buf, MAX_QUERY_LEN, "(ai.app_disable=? COLLATE NOCASE " \
400                                         "OR ui.is_disabled='true' COLLATE NOCASE)");
401                 else
402                         snprintf(buf, MAX_QUERY_LEN, "(ai.app_disable=? COLLATE NOCASE " \
403                                         "AND (ui.is_disabled='false' COLLATE NOCASE " \
404                                         "OR ui.is_disabled IS NULL))");
405                 break;
406         case E_PMINFO_APPINFO_PROP_APP_SUPPORT_DISABLE:
407                 snprintf(buf, MAX_QUERY_LEN, "ai.app_support_disable=? COLLATE NOCASE");
408                 break;
409         case E_PMINFO_APPINFO_PROP_PKG_DISABLE:
410                 snprintf(buf, MAX_QUERY_LEN,
411                                 "ai.package IN (SELECT package FROM " \
412                                 "package_info WHERE package_disable=?)");
413                 break;
414         case E_PMINFO_APPINFO_PROP_APP_SUPPORT_MODE:
415                 snprintf(buf, sizeof(buf), "ai.app_support_mode=?");
416                 break;
417         case E_PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE:
418         case E_PMINFO_APPINFO_PROP_APP_CHECK_STORAGE:
419                 *condition = NULL;
420                 return 0;
421         case E_PMINFO_APPINFO_PROP_PRIVILEGE:
422                 snprintf(buf, sizeof(buf), "package_privilege_info.privilege=?");
423                 flag = E_PMINFO_APPINFO_JOIN_PRIVILEGE;
424                 break;
425         default:
426                 _LOGE("Invalid Property Type\n");
427                 *condition = NULL;
428                 return 0;
429         }
430         *condition = strdup(buf);
431         *params = g_list_append(*params, strdup(node->value));
432         return flag;
433 }
434
435 int __get_metadata_filter_condition(gpointer data, char **condition,
436                 GList **params)
437 {
438         pkgmgrinfo_metadata_node_x *node = (pkgmgrinfo_metadata_node_x *)data;
439         char buf[MAX_QUERY_LEN];
440
441         snprintf(buf, sizeof(buf), "(package_app_app_metadata.md_key=?");
442         if (node->value)
443                 strncat(buf, " AND package_app_app_metadata.md_value=?",
444                                 sizeof(buf) - strlen(buf) - 1);
445         strncat(buf, ")", sizeof(buf) - strlen(buf) - 1);
446
447         *condition = strdup(buf);
448         *params = g_list_append(*params, strdup(node->key));
449         if (node->value)
450                 *params = g_list_append(*params, strdup(node->value));
451
452         return E_PMINFO_APPINFO_JOIN_METADATA;
453 }
454
455 int _add_icon_info_into_list(const char *locale, char *value, GList **icon)
456 {
457         icon_x *info;
458
459         info = calloc(1, sizeof(icon_x));
460         if (info == NULL) {
461                 LOGE("out of memory");
462                 return PMINFO_R_ERROR;
463         }
464         info->text = value;
465         info->lang = strdup(locale);
466         *icon = g_list_append(*icon, info);
467
468         return PMINFO_R_OK;
469 }
470
471 int _add_label_info_into_list(const char *locale, char *value, GList **label)
472 {
473         label_x *info;
474
475         info = calloc(1, sizeof(label_x));
476         if (info == NULL) {
477                 LOGE("out of memory");
478                 return PMINFO_R_ERROR;
479         }
480         info->text = value;
481         info->lang = strdup(locale);
482         *label = g_list_append(*label, info);
483
484         return PMINFO_R_OK;
485 }
486
487 char *_get_system_locale(void)
488 {
489         char *lang;
490         char *locale;
491
492         lang = vconf_get_str(VCONFKEY_LANGSET);
493         if (lang == NULL) {
494                 locale = strdup(DEFAULT_LOCALE);
495                 if (locale == NULL) {
496                         LOGE("out of memory");
497                         return NULL;
498                 }
499                 return locale;
500         }
501
502         locale = malloc(sizeof(char) * 6);
503         if (locale == NULL) {
504                 LOGE("out of memory");
505                 free(lang);
506                 return NULL;
507         }
508
509         strncpy(locale, lang, 2);
510         locale[2] = '-';
511         locale[3] = tolower(lang[3]);
512         locale[4] = tolower(lang[4]);
513         locale[5] = '\0';
514
515         free(lang);
516
517         return locale;
518 }
519
520 int __pkginfo_check_installed_storage(package_x *pkginfo)
521 {
522         char buf[MAX_QUERY_LEN] = {'\0'};
523         int ret;
524
525         retvm_if(pkginfo->package == NULL, PMINFO_R_OK, "pkgid is NULL\n");
526         retvm_if(pkginfo->installed_storage == NULL, PMINFO_R_ERROR,
527                         "installed_storage is NULL\n");
528
529         if (strcmp(pkginfo->installed_storage, "installed_external") == 0) {
530                 snprintf(buf, MAX_QUERY_LEN - 1, "%s", pkginfo->external_path);
531                 ret = access(buf, R_OK);
532                 if (ret != 0) {
533                         _LOGE("can not access [%s] - %d", buf, ret);
534                         return PMINFO_R_ERROR;
535                 }
536         }
537
538         return PMINFO_R_OK;
539 }
540
541 int __appinfo_check_installed_storage(application_x *appinfo)
542 {
543         retvm_if(appinfo->installed_storage == NULL, PMINFO_R_ERROR,
544                         "installed_storage is NULL\n");
545
546         if (strcmp(appinfo->installed_storage, "installed_external") == 0) {
547                 retvm_if(appinfo->external_path == NULL, PMINFO_R_ERROR,
548                                 "external path is NULL\n");
549                 if (access(appinfo->external_path, R_OK) != 0) {
550                         _LOGE("can not access [%s]", appinfo->external_path);
551                         return PMINFO_R_ERROR;
552                 }
553         }
554
555         return PMINFO_R_OK;
556 }
557
558 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
559 #define BUSY_WAITING_MAX 100 /* wait for max 5 sec */
560 static int __db_busy_handler(void *data, int count)
561 {
562         if (count < BUSY_WAITING_MAX) {
563                 usleep(BUSY_WAITING_USEC);
564                 return 1;
565         } else {
566                 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
567                 return 0;
568         }
569 }
570
571 int __open_db(const char *path, sqlite3 **db, int flags)
572 {
573         int ret;
574
575         ret = sqlite3_open_v2(path, db, flags, NULL);
576         if (ret != SQLITE_OK) {
577                 sqlite3_close_v2(*db);
578                 return ret;
579         }
580
581         ret = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
582         if (ret != SQLITE_OK) {
583                 _LOGE("failed to register busy handler: %s",
584                                 sqlite3_errmsg(*db));
585                 sqlite3_close_v2(*db);
586                 return ret;
587         }
588
589         return ret;
590 }
591
592 struct type_map {
593         const char *manifest;
594         const char *type;
595 };
596
597 struct type_map type_map[] = {
598         { "res/wgt/config.xml", "wgt" },
599         { "config.xml", "wgt" },
600         { "tizen-manifest.xml", "tpk" },
601         { NULL, NULL}
602 };
603
604 char *__get_type_from_path(const char *pkg_path)
605 {
606         const char *type = NULL;
607         unzFile uf;
608         int i;
609
610         uf = unzOpen(pkg_path);
611         if (uf == NULL) {
612                 _LOGE("failed to open zip file %s", pkg_path);
613                 return NULL;
614         }
615
616         for (i = 0; type_map[i].manifest != NULL; i++) {
617                 if (unzLocateFile(uf, type_map[i].manifest, 0) == UNZ_OK) {
618                         _LOGD("pkgtype of %s: [%s]", pkg_path,
619                                         type_map[i].type);
620                         type = type_map[i].type;
621                         break;
622                 }
623         }
624         unzClose(uf);
625
626         if (type == NULL) {
627                 _LOGE("cannot get pkg type of file %s", pkg_path);
628                 return NULL;
629         }
630
631         return strdup(type);
632 }
633
634 static const char *__get_library_path(const char *pkg_type)
635 {
636         char buf[64];
637         const char *path;
638
639         snprintf(buf, sizeof(buf), "package-manager/backendlib/lib%s.so",
640                         pkg_type);
641         path = tzplatform_mkpath(TZ_SYS_RO_ETC, buf);
642
643         return path;
644 }
645
646 pkg_plugin_set *__load_library(const char *pkg_type)
647 {
648         void *library_handle;
649         pkg_plugin_set *plugin_set;
650         bool (*on_load)(pkg_plugin_set *plugin);
651         const char *library_path;
652
653         library_path = __get_library_path(pkg_type);
654         if (library_path == NULL) {
655                 _LOGE("cannot get library path for %s", pkg_type);
656                 return NULL;
657         }
658
659         if (plugin_set_list == NULL) {
660                 plugin_set_list = g_hash_table_new_full(g_str_hash,
661                                 g_str_equal, free, free);
662                 if (plugin_set_list == NULL) {
663                         _LOGE("out of memory");
664                         return NULL;
665                 }
666         }
667
668         plugin_set = (pkg_plugin_set *)g_hash_table_lookup(plugin_set_list,
669                         (gconstpointer)pkg_type);
670         if (plugin_set) {
671                 _LOGD("already loaded [%s]", library_path);
672                 return plugin_set;
673         }
674
675         if ((library_handle = dlopen(library_path, RTLD_LAZY)) == NULL) {
676                 _LOGE("dlopen is failed library_path[%s]", library_path);
677                 return NULL;
678         }
679
680         if ((on_load = dlsym(library_handle, "pkg_plugin_on_load")) == NULL ||
681                         dlerror() != NULL) {
682                 _LOGE("cannot find symbol");
683                 dlclose(library_handle);
684                 return NULL;
685         }
686
687         plugin_set = (pkg_plugin_set *)calloc(1, sizeof(pkg_plugin_set));
688         if (plugin_set == NULL) {
689                 _LOGE("out of memory");
690                 dlclose(library_handle);
691                 return NULL;
692         }
693
694         if (on_load(plugin_set) != 0) {
695                 _LOGE("pkg_plugin_on_load failed");
696                 free(plugin_set);
697                 dlclose(library_handle);
698                 plugin_set = NULL;
699                 return NULL;
700         }
701
702         plugin_set->plugin_handle = library_handle;
703         snprintf(plugin_set->pkg_type,
704                         sizeof(plugin_set->pkg_type), "%s", pkg_type);
705
706         g_hash_table_insert(plugin_set_list, (gpointer)strdup(pkg_type),
707                         (gpointer)plugin_set);
708
709         _LOGD("library [%s] is loaded", library_path);
710
711         return plugin_set;
712 }
713
714 void __unload_library(const char *pkg_type)
715 {
716         pkg_plugin_set *plugin_set;
717
718         if (plugin_set_list == NULL)
719                 return;
720
721         plugin_set = (pkg_plugin_set *)g_hash_table_lookup(plugin_set_list,
722                         (gconstpointer)pkg_type);
723
724         if (plugin_set == NULL) {
725                 _LOGE("pkg plugin for %s is not loaded", pkg_type);
726                 return;
727         }
728
729         plugin_set->plugin_on_unload();
730         dlclose(plugin_set->plugin_handle);
731         g_hash_table_remove(plugin_set_list, (gconstpointer)pkg_type);
732 }