Remove compile warning messages
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
1 /*
2  * Copyright (c) 2000 - 2017 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
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/smack.h>
25 #include <sys/sysmacros.h>
26 #include <linux/limits.h>
27 #include <unistd.h>
28 #include <pwd.h>
29
30 #include <glib.h>
31 #include <gio/gio.h>
32 #include <sqlite3.h>
33
34 #include <tzplatform_config.h>
35 #include <system_info.h>
36
37 #include "pkgmgr-info.h"
38 #include "pkgmgrinfo_basic.h"
39 #include "pkgmgr_parser.h"
40 #include "pkgmgr_parser_db_queries.h"
41 #include "pkgmgr_parser_debug.h"
42 #include "pkgmgr_parser_internal.h"
43
44 #ifndef OWNER_ROOT
45 #define OWNER_ROOT 0
46 #endif
47 #ifndef GLOBAL_USER
48 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
49 #endif
50 #ifndef APPFW_USER
51 #define APPFW_USER "app_fw"
52 #endif
53
54 #define BUFSIZE 4096
55
56 #define LDPI "ldpi"
57 #define MDPI "mdpi"
58 #define HDPI "hdpi"
59 #define XHDPI "xhdpi"
60 #define XXHDPI "xxhdpi"
61
62 #define LDPI_MIN 0
63 #define LDPI_MAX 240
64 #define MDPI_MIN 241
65 #define MDPI_MAX 300
66 #define HDPI_MIN 301
67 #define HDPI_MAX 380
68 #define XHDPI_MIN 381
69 #define XHDPI_MAX 480
70 #define XXHDPI_MIN 481
71 #define XXHDPI_MAX 600
72
73 /* app background category value */
74 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
75 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL  0x00001
76 #define APP_BG_CATEGORY_MEDIA_VAL              0x00002
77 #define APP_BG_CATEGORY_DOWNLOAD_VAL           0x00004
78 #define APP_BG_CATEGORY_BGNETWORK_VAL          0x00008
79 #define APP_BG_CATEGORY_LOCATION_VAL           0x00010
80 #define APP_BG_CATEGORY_SENSOR_VAL             0x00020
81 #define APP_BG_CATEGORY_IOTCOMM_VAL            0x00040
82 #define APP_BG_CATEGORY_SYSTEM_VAL             0x00080
83
84 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
85 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR  "disable"
86 #define APP_BG_CATEGORY_MEDIA_STR              "media"
87 #define APP_BG_CATEGORY_DOWNLOAD_STR           "download"
88 #define APP_BG_CATEGORY_BGNETWORK_STR          "background-network"
89 #define APP_BG_CATEGORY_LOCATION_STR           "location"
90 #define APP_BG_CATEGORY_SENSOR_STR             "sensor"
91 #define APP_BG_CATEGORY_IOTCOMM_STR            "iot-communication"
92 #define APP_BG_CATEGORY_SYSTEM                 "system"
93
94 #define REGULAR_USER 5000
95 static inline uid_t __getuid(void)
96 {
97         uid_t uid = getuid();
98
99         if (uid < REGULAR_USER)
100                 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
101         else
102                 return uid;
103 }
104
105 static const char *__get_bool(char *value, bool is_true)
106 {
107         if (value != NULL) {
108                 if (!strcmp(value, ""))
109                         return (is_true) ? "true" : "false";
110                 return value;
111         }
112
113         return (is_true) ? "true" : "false";
114 }
115
116 #define __BEGIN_TRANSACTION(db)                                                \
117 do {                                                                           \
118         if (sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL) !=           \
119                         SQLITE_OK) {                                           \
120                 _LOGE("begin transaction failed: %s", sqlite3_errmsg(db));     \
121                 sqlite3_close_v2(db);                                          \
122                 return PM_PARSER_R_ERROR;                                      \
123         }                                                                      \
124 } while (0)                                                                    \
125
126 #define __DO_TRANSACTION(db, func)                                             \
127 do {                                                                           \
128         if (func) {                                                            \
129                 _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \
130                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
131                                 SQLITE_OK)                                     \
132                         _LOGE("roll back transaction failed: %s",              \
133                                         sqlite3_errmsg(db));                   \
134                 sqlite3_close_v2(db);                                          \
135                 return PM_PARSER_R_ERROR;                                      \
136         }                                                                      \
137 } while (0)                                                                    \
138
139 #define __END_TRANSACTION(db)                                                  \
140 do {                                                                           \
141         if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) !=                    \
142                         SQLITE_OK) {                                           \
143                 _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db));      \
144                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
145                                 SQLITE_OK)                                     \
146                         _LOGE("roll back transaction failed: %s",              \
147                                         sqlite3_errmsg(db));                   \
148                 sqlite3_close_v2(db);                                          \
149                 return PM_PARSER_R_ERROR;                                      \
150         }                                                                      \
151 } while (0)                                                                    \
152
153 #define __BIND_TEXT(db, stmt, i, text)                                         \
154 do {                                                                           \
155         if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\
156                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
157                 sqlite3_finalize(stmt);                                        \
158                 return -1;                                                     \
159         }                                                                      \
160 } while (0)
161
162 #define __BIND_INT(db, stmt, i, int)                                           \
163 do {                                                                           \
164         if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) {                     \
165                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
166                 sqlite3_finalize(stmt);                                        \
167                 return -1;                                                     \
168         }                                                                      \
169 } while (0)
170
171 static const char *__get_parser_db_path(uid_t uid)
172 {
173         char buf[PATH_MAX];
174         const char *path;
175
176         if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
177                 path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
178         } else {
179                 snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid);
180                 path = tzplatform_mkpath(TZ_SYS_DB, buf);
181         }
182
183         return path;
184 }
185
186 static const char *__get_cert_db_path(void)
187 {
188         return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
189 }
190
191 #define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
192 static int __set_db_version(sqlite3 *db)
193 {
194         int ret;
195         FILE *fp = NULL;
196         char version[PKG_STRING_LEN_MAX] = { 0 };
197         char *query = NULL;
198
199         fp = fopen(DB_VERSION_PATH, "r");
200         retvm_if(fp == NULL, -1, "Failed to open db version file");
201         if (fgets(version, sizeof(version), fp) == NULL) {
202                 _LOGE("Failed to get version information");
203                 fclose(fp);
204                 return -1;
205         }
206         fclose(fp);
207
208         query = sqlite3_mprintf("PRAGMA user_version=%Q", version);
209         if (!query) {
210                 _LOGE("Out of memory");
211                 return -1;
212         }
213
214         ret = sqlite3_exec(db, query, NULL, NULL, NULL);
215         if (ret != SQLITE_OK) {
216                 _LOGE("exec failed: %s", sqlite3_errmsg(db));
217                 sqlite3_free(query);
218                 return -1;
219         }
220         sqlite3_free(query);
221
222         return 0;
223 }
224
225 /* TODO: Do not labeling directly */
226 #define DB_LABEL "User::Home"
227 #define SET_SMACK_LABEL(x)                                                     \
228 do {                                                                           \
229         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS))                 \
230                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x);                 \
231         else                                                                   \
232                 _LOGD("chsmack -a %s %s", DB_LABEL, x);                        \
233 } while (0)
234
235 static int __set_db_permission(const char *path, uid_t uid)
236 {
237         int fd;
238         const char *files[2];
239         char journal_file[BUFSIZE];
240         struct stat sb;
241         mode_t mode;
242         struct passwd pwd;
243         struct passwd *result;
244         char buf[BUFSIZE];
245         int ret;
246         int i;
247
248         if (getuid() != OWNER_ROOT)
249                 return 0;
250
251         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
252                 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
253                 if (result == NULL) {
254                         if (ret == 0)
255                                 _LOGE("no such user: %d", uid);
256                         else
257                                 _LOGE("getpwuid_r failed: %d", errno);
258                         return -1;
259                 }
260                 uid = pwd.pw_uid;
261         }
262
263         snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
264         files[0] = path;
265         files[1] = journal_file;
266
267         ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
268         if (result == NULL) {
269                 if (ret == 0)
270                         _LOGE("no such user: %d", uid);
271                 else
272                         _LOGE("getpwuid_r failed: %d", errno);
273                 return -1;
274         }
275
276         for (i = 0; i < 2; i++) {
277                 fd = open(files[i], O_RDONLY);
278                 if (fd == -1) {
279                         _LOGE("open %s failed: %d", files[i], errno);
280                         return -1;
281                 }
282                 ret = fstat(fd, &sb);
283                 if (ret == -1) {
284                         _LOGE("stat %s failed: %d", files[i], errno);
285                         close(fd);
286                         return -1;
287                 }
288                 if (S_ISLNK(sb.st_mode)) {
289                         _LOGE("%s is symlink!", files[i]);
290                         close(fd);
291                         return -1;
292                 }
293                 ret = fchown(fd, uid, pwd.pw_gid);
294                 if (ret == -1) {
295                         _LOGE("fchown %s failed: %d", files[i], errno);
296                         close(fd);
297                         return -1;
298                 }
299
300                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
301                 if (!strcmp(path, __get_cert_db_path()))
302                         mode |= S_IWOTH;
303                 ret = fchmod(fd, mode);
304                 if (ret == -1) {
305                         _LOGD("fchmod %s failed: %d", files[i], errno);
306                         close(fd);
307                         return -1;
308                 }
309                 close(fd);
310                 SET_SMACK_LABEL(files[i]);
311         }
312
313         return 0;
314 }
315
316 static const char *parser_init_queries[] = {
317         QUERY_CREATE_TABLE_PACKAGE_INFO,
318         QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO,
319         QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO,
320         QUERY_CREATE_TABLE_PACKAGE_APPDEFINED_PRIVILEGE_INFO,
321         QUERY_CREATE_TABLE_PACKAGE_UPDATE_INFO,
322         QUERY_CREATE_TABLE_PACKAGE_APP_INFO,
323         QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO,
324         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL,
325         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL_PRIVILEGE,
326         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY,
327         QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA,
328         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL,
329         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL_PRIVILEGE,
330         QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID,
331         QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID,
332         QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN,
333         QUERY_CREATE_TABLE_PACKAGE_DEPENDENCY_INFO,
334         QUERY_CREATE_TABLE_PACKAGE_PLUGIN_INFO,
335         NULL,
336 };
337
338 static const char *cert_init_queries[] = {
339         QUERY_CREATE_TABLE_PACKAGE_CERT_INFO,
340         QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO,
341         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO,
342         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2,
343         QUERY_CREATE_TRIGGER_DELETE_CERT_INFO,
344         QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO,
345         NULL
346 };
347
348 static int __create_tables(sqlite3 *db, const char **queries)
349 {
350         int ret;
351         int i;
352         for (i = 0; queries[i] != NULL; i++) {
353                 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
354                 if (ret != SQLITE_OK) {
355                         _LOGE("exec failed: %s", sqlite3_errmsg(db));
356                         return -1;
357                 }
358         }
359         return 0;
360 }
361
362 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
363 {
364         const char **queries;
365
366         if (__set_db_version(db))
367                 return -1;
368
369         if (strstr(dbpath, ".pkgmgr_parser.db")) {
370                 queries = parser_init_queries;
371         } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
372                 queries = cert_init_queries;
373         } else {
374                 _LOGE("unexpected dbpath: %s", dbpath);
375                 return -1;
376         }
377
378         __BEGIN_TRANSACTION(db);
379         __DO_TRANSACTION(db, __create_tables(db, queries));
380         __END_TRANSACTION(db);
381
382         if (__set_db_permission(dbpath, uid))
383                 _LOGE("failed to set db permission");
384
385         return 0;
386 }
387
388 #define RESOURCED_BUS_NAME "org.tizen.resourced"
389 #define RESOURCED_PROC_PATH "/Org/Tizen/ResourceD/Process"
390 #define RESOURCED_PROC_INTERFACE "org.tizen.resourced.process"
391 #define RESOURCED_PROC_METHOD "ProcExclude"
392 static void __send_wakeup_signal_to_resourced(pid_t pid)
393 {
394         GError *error = NULL;
395         GDBusConnection *conn;
396         GDBusProxy *proxy;
397         GVariant *reply;
398
399         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
400         if (conn == NULL) {
401                 _LOGE("Failed to connect to dbus: %s", error->message);
402                 g_error_free(error);
403                 return;
404         }
405
406         proxy = g_dbus_proxy_new_sync(conn,
407                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
408                         NULL, RESOURCED_BUS_NAME,
409                         RESOURCED_PROC_PATH, RESOURCED_PROC_INTERFACE,
410                         NULL, &error);
411         if (proxy == NULL) {
412                 _LOGE("failed to get proxy object: %s", error->message);
413                 g_error_free(error);
414                 g_object_unref(conn);
415                 return;
416         }
417
418         reply = g_dbus_proxy_call_sync(proxy, RESOURCED_PROC_METHOD,
419                         g_variant_new("(si)", "wakeup", pid),
420                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
421         if (reply == NULL)
422                 _LOGE("failed to get reply from resourced");
423         if (error) {
424                 _LOGE("failed to send request: %s", error->message);
425                 g_error_free(error);
426         }
427
428         g_object_unref(proxy);
429         g_object_unref(conn);
430 }
431
432 static void __check_db_lock(const char *dbpath)
433 {
434         FILE *fp;
435         FILE *fp_cmdline;
436         struct stat sb;
437         char type[BUFSIZE];
438         int pid;
439         unsigned int maj;
440         unsigned int min;
441         ino_t ino;
442         char cmdline[BUFSIZE];
443         char name[BUFSIZE];
444         size_t len;
445
446         if (stat(dbpath, &sb) == -1) {
447                 _LOGE("get db file(%s) status failed: %d", dbpath, errno);
448                 return;
449         }
450
451         fp = fopen("/proc/locks", "r");
452         if (fp == NULL) {
453                 _LOGE("Failed to open lock info: %d", errno);
454                 return;
455         }
456
457         while (fscanf(fp, "%*s %*s %*s %5s %d %x:%x:%lu %*s %*s",
458                                 type, &pid, &maj, &min, &ino) != EOF) {
459                 if (maj != major(sb.st_dev) || min != minor(sb.st_dev) ||
460                                 ino != sb.st_ino || pid == getpid() ||
461                                 strcasecmp(type, "WRITE"))
462                         continue;
463
464                 snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
465                 fp_cmdline = fopen(cmdline, "r");
466                 name[0] = '\0';
467                 if (fp_cmdline != NULL) {
468                         len = fread(name, sizeof(char), sizeof(name) - 1,
469                                         fp_cmdline);
470                         if (len > 0) {
471                                 if (name[len - 1] == '\n')
472                                         name[len - 1] = '\0';
473                                 else
474                                         name[len] = '\0';
475                         }
476                         fclose(fp_cmdline);
477                 }
478
479                 _LOGE("%s (%d) has lock on pkgmgr db(%s)!", name, pid, dbpath);
480                 __send_wakeup_signal_to_resourced(pid);
481         }
482
483         fclose(fp);
484 }
485
486 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
487 #define BUSY_WAITING_MAX 40 /* wait for max 2 sec */
488 static int __db_busy_handler(void *data, int count)
489 {
490         if (count < (BUSY_WAITING_MAX / 2)) {
491                 usleep(BUSY_WAITING_USEC);
492                 return 1;
493         } else if (count == (BUSY_WAITING_MAX / 2)) {
494                 __check_db_lock((const char *)data);
495                 usleep(BUSY_WAITING_USEC);
496                 return 1;
497         } else if (count < BUSY_WAITING_MAX) {
498                 usleep(BUSY_WAITING_USEC);
499                 return 1;
500         } else {
501                 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
502                 return 0;
503         }
504 }
505
506 API int pkgmgr_parser_initialize_parser_db(uid_t uid)
507 {
508         int ret;
509         const char *dbpath;
510         sqlite3 *db;
511
512         dbpath = __get_parser_db_path(uid);
513         if (access(dbpath, F_OK) != -1) {
514                 _LOGE("Manifest db for user %d is already exists", uid);
515                 return PM_PARSER_R_ERROR;
516         }
517
518         ret = sqlite3_open_v2(dbpath, &db,
519                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
520         if (ret != SQLITE_OK) {
521                 _LOGE("open db failed: %d", ret);
522                 sqlite3_close_v2(db);
523                 return PM_PARSER_R_ERROR;
524         }
525
526         ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
527         if (ret != SQLITE_OK) {
528                 _LOGE("failed to register busy handler: %s",
529                                 sqlite3_errmsg(db));
530                 sqlite3_close_v2(db);
531                 return ret;
532         }
533
534         if (__initialize_db(db, dbpath, uid)) {
535                 sqlite3_close_v2(db);
536                 return PM_PARSER_R_ERROR;
537         }
538         sqlite3_close_v2(db);
539
540         return PM_PARSER_R_OK;
541 }
542
543 API int pkgmgr_parser_initialize_cert_db(void)
544 {
545         int ret;
546         const char *dbpath;
547         sqlite3 *db;
548
549         dbpath = __get_cert_db_path();
550         if (access(dbpath, F_OK) != -1) {
551                 _LOGE("Cert db is already exists");
552                 return PM_PARSER_R_ERROR;
553         }
554
555         ret = sqlite3_open_v2(dbpath, &db,
556                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
557         if (ret != SQLITE_OK) {
558                 _LOGE("open db failed: %d", ret);
559                 sqlite3_close_v2(db);
560                 return PM_PARSER_R_ERROR;
561         }
562
563         ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
564         if (ret != SQLITE_OK) {
565                 _LOGE("failed to register busy handler: %s",
566                                 sqlite3_errmsg(db));
567                 sqlite3_close_v2(db);
568                 return ret;
569         }
570
571         if (__initialize_db(db, dbpath, GLOBAL_USER)) {
572                 sqlite3_close_v2(db);
573                 return PM_PARSER_R_ERROR;
574         }
575         sqlite3_close_v2(db);
576
577         return PM_PARSER_R_OK;
578 }
579
580 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
581 {
582         int ret;
583         struct passwd pwd;
584         struct passwd *result;
585         char buf[BUFSIZE];
586
587         ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
588         if (result == NULL) {
589                 if (ret == 0)
590                         _LOGE("no such user: %s", APPFW_USER);
591                 else
592                         _LOGE("getpwnam_r failed: %d", errno);
593                 return PM_PARSER_R_ERROR;
594         }
595
596         if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
597                 _LOGE("Only root or app_fw user is allowed");
598                 return PM_PARSER_R_EINVAL;
599         }
600
601         if (pkgmgr_parser_initialize_parser_db(uid))
602                 return PM_PARSER_R_ERROR;
603
604         if (uid == OWNER_ROOT || uid == GLOBAL_USER)
605                 if (pkgmgr_parser_initialize_cert_db())
606                         return PM_PARSER_R_ERROR;
607
608         return PM_PARSER_R_OK;
609 }
610
611 static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
612 {
613         int ret;
614
615         /* FIXME: always open with OPEN_CREATE flag for keeping previous
616          * implementation
617          */
618         if (flags & SQLITE_OPEN_READWRITE)
619                 flags = flags | SQLITE_OPEN_CREATE;
620
621         ret = sqlite3_open_v2(path, db, flags, NULL);
622         if (ret != SQLITE_OK) {
623                 sqlite3_close_v2(*db);
624                 return ret;
625         }
626
627         ret = sqlite3_busy_handler(*db, __db_busy_handler, (void *)path);
628         if (ret != SQLITE_OK) {
629                 _LOGE("failed to register busy handler: %s",
630                                 sqlite3_errmsg(*db));
631                 sqlite3_close_v2(*db);
632                 return ret;
633         }
634
635         if (flags & SQLITE_OPEN_CREATE) {
636                 ret = __initialize_db(*db, path, uid);
637                 if (ret) {
638                         _LOGE("failed to initialize db: %s", path);
639                         sqlite3_close_v2(*db);
640                         return -1;
641                 }
642         }
643
644         ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
645         if (ret != SQLITE_OK) {
646                 _LOGE("failed to enable foreign key support: %s",
647                                 sqlite3_errmsg(*db));
648                 sqlite3_close_v2(*db);
649                 return ret;
650         }
651
652         return ret;
653 }
654
655
656 static int __convert_background_category(GList *category_list)
657 {
658         int ret = 0;
659         GList *tmp;
660         char *category_data;
661
662         if (category_list == NULL)
663                 return 0;
664
665         for (tmp = category_list; tmp; tmp = tmp->next) {
666                 category_data = (char *)tmp->data;
667                 if (category_data == NULL)
668                         continue;
669                 if (!strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR))
670                         ret |= APP_BG_CATEGORY_MEDIA_VAL;
671                 else if (!strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR))
672                         ret |= APP_BG_CATEGORY_DOWNLOAD_VAL;
673                 else if (!strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR))
674                         ret |= APP_BG_CATEGORY_BGNETWORK_VAL;
675                 else if (!strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR))
676                         ret |= APP_BG_CATEGORY_LOCATION_VAL;
677                 else if (!strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR))
678                         ret |= APP_BG_CATEGORY_SENSOR_VAL;
679                 else if (!strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR))
680                         ret |= APP_BG_CATEGORY_IOTCOMM_VAL;
681                 else if (!strcmp(category_data, APP_BG_CATEGORY_SYSTEM))
682                         ret |= APP_BG_CATEGORY_SYSTEM_VAL;
683                 else
684                         _LOGE("Unidentified category [%s]", category_data);
685         }
686
687         return ret;
688 }
689
690 #define EFFECTIVE_APPID_KEY "http://tizen.org/metadata/effective-appid"
691 static const char *__find_effective_appid(GList *metadata_list)
692 {
693         GList *tmp;
694         metadata_x *md;
695
696         for (tmp = metadata_list; tmp; tmp = tmp->next) {
697                 md = (metadata_x *)tmp->data;
698                 if (md == NULL || md->key == NULL)
699                         continue;
700
701                 if (strcmp(md->key, EFFECTIVE_APPID_KEY) == 0) {
702                         if (md->value)
703                                 return md->value;
704                 }
705         }
706
707         return NULL;
708 }
709
710 static int __insert_appcontrol_privilege_info(sqlite3 *db, const char *appid,
711                 appcontrol_x *ac)
712 {
713         static const char query[] =
714                 "INSERT INTO package_app_app_control_privilege (app_id,"
715                 "  app_control, privilege) VALUES (?, ?, ?)";
716         int ret;
717         sqlite3_stmt *stmt;
718         int idx;
719         char app_control[BUFSIZE];
720         GList *tmp;
721         char *privilege;
722
723         if (ac == NULL)
724                 return 0;
725
726         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
727         if (ret != SQLITE_OK) {
728                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
729                 return -1;
730         }
731
732         for (tmp = ac->privileges; tmp; tmp = tmp->next) {
733                 privilege = (char *)tmp->data;
734                 if (privilege == NULL || !strlen(privilege))
735                         continue;
736
737                 idx = 1;
738                 snprintf(app_control, sizeof(app_control), "%s|%s|%s",
739                                 ac->operation ? (strlen(ac->operation) > 0 ?
740                                         ac->operation : "NULL") : "NULL",
741                                 ac->uri ? (strlen(ac->uri) > 0 ?
742                                         ac->uri : "NULL") : "NULL",
743                                 ac->mime ? (strlen(ac->mime) > 0 ?
744                                         ac->mime : "NULL") : "NULL");
745                 __BIND_TEXT(db, stmt, idx++, appid);
746                 __BIND_TEXT(db, stmt, idx++, app_control);
747                 __BIND_TEXT(db, stmt, idx++, privilege);
748
749                 ret = sqlite3_step(stmt);
750                 if (ret != SQLITE_DONE) {
751                         _LOGE("step failed: %s", sqlite3_errmsg(db));
752                         sqlite3_finalize(stmt);
753                         return -1;
754                 }
755
756                 sqlite3_reset(stmt);
757         }
758
759         sqlite3_finalize(stmt);
760
761         return 0;
762 }
763
764 static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
765 {
766         static const char query[] =
767                 "INSERT INTO package_app_app_control (app_id, app_control,"
768                 "  visibility, app_control_id) "
769                 "VALUES (?, ?, ?, ?)";
770         int ret;
771         sqlite3_stmt *stmt;
772         int idx;
773         char app_control[BUFSIZE];
774         GList *tmp;
775         appcontrol_x *ac;
776
777         if (app->appcontrol == NULL)
778                 return 0;
779
780         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
781         if (ret != SQLITE_OK) {
782                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
783                 return -1;
784         }
785
786         for (tmp = app->appcontrol; tmp; tmp = tmp->next) {
787                 ac = (appcontrol_x *)tmp->data;
788                 if (ac == NULL)
789                         continue;
790                 idx = 1;
791                 snprintf(app_control, sizeof(app_control), "%s|%s|%s",
792                                 ac->operation ? (strlen(ac->operation) > 0 ?
793                                         ac->operation : "NULL") : "NULL",
794                                 ac->uri ? (strlen(ac->uri) > 0 ?
795                                         ac->uri : "NULL") : "NULL",
796                                 ac->mime ? (strlen(ac->mime) > 0 ?
797                                         ac->mime : "NULL") : "NULL");
798                 __BIND_TEXT(db, stmt, idx++, app->appid);
799                 __BIND_TEXT(db, stmt, idx++, app_control);
800                 __BIND_TEXT(db, stmt, idx++, ac->visibility);
801                 __BIND_TEXT(db, stmt, idx++, ac->id);
802
803                 ret = sqlite3_step(stmt);
804                 if (ret != SQLITE_DONE) {
805                         _LOGE("step failed: %s", sqlite3_errmsg(db));
806                         sqlite3_finalize(stmt);
807                         return -1;
808                 }
809
810                 if (__insert_appcontrol_privilege_info(db, app->appid, ac)) {
811                         sqlite3_finalize(stmt);
812                         return -1;
813                 }
814
815                 sqlite3_reset(stmt);
816         }
817
818         sqlite3_finalize(stmt);
819
820         return 0;
821 }
822
823 static int __insert_category_info(sqlite3 *db, application_x *app)
824 {
825         static const char query[] =
826                 "INSERT INTO package_app_app_category (app_id, category) "
827                 "VALUES (?, ?)";
828         int ret;
829         sqlite3_stmt *stmt;
830         int idx;
831         GList *tmp;
832         const char *category;
833
834         if (app->category == NULL)
835                 return 0;
836
837         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
838         if (ret != SQLITE_OK) {
839                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
840                 return -1;
841         }
842
843         for (tmp = app->category; tmp; tmp = tmp->next) {
844                 category = (const char *)tmp->data;
845                 if (category == NULL)
846                         continue;
847                 idx = 1;
848                 __BIND_TEXT(db, stmt, idx++, app->appid);
849                 __BIND_TEXT(db, stmt, idx++, category);
850
851                 ret = sqlite3_step(stmt);
852                 if (ret != SQLITE_DONE) {
853                         _LOGE("step failed: %s", sqlite3_errmsg(db));
854                         sqlite3_finalize(stmt);
855                         return -1;
856                 }
857
858                 sqlite3_reset(stmt);
859         }
860
861         sqlite3_finalize(stmt);
862
863         return 0;
864 }
865
866 static int __insert_metadata_info(sqlite3 *db, application_x *app)
867 {
868         static const char query[] =
869                 "INSERT INTO package_app_app_metadata (app_id,"
870                 "  md_key, md_value) VALUES (?, ?, ?)";
871         int ret;
872         sqlite3_stmt *stmt;
873         int idx;
874         GList *tmp;
875         metadata_x *md;
876
877         if (app->metadata == NULL)
878                 return 0;
879
880         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
881         if (ret != SQLITE_OK) {
882                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
883                 return -1;
884         }
885
886         for (tmp = app->metadata; tmp; tmp = tmp->next) {
887                 md = (metadata_x *)tmp->data;
888                 if (md == NULL)
889                         continue;
890                 idx = 1;
891                 __BIND_TEXT(db, stmt, idx++, app->appid);
892                 __BIND_TEXT(db, stmt, idx++, md->key);
893                 __BIND_TEXT(db, stmt, idx++, md->value);
894
895                 ret = sqlite3_step(stmt);
896                 if (ret != SQLITE_DONE) {
897                         _LOGE("step failed: %s", sqlite3_errmsg(db));
898                         sqlite3_finalize(stmt);
899                         return -1;
900                 }
901
902                 sqlite3_reset(stmt);
903         }
904
905         sqlite3_finalize(stmt);
906
907         return 0;
908 }
909
910 static int __insert_app_data_control_privilege_info(sqlite3 *db,
911                 datacontrol_x *datacontrol)
912 {
913         static const char query[] =
914                 "INSERT INTO package_app_data_control_privilege (providerid,"
915                 "  privilege, type) VALUES (?, ?, ?)";
916
917         int ret;
918         sqlite3_stmt *stmt;
919         int idx;
920         GList *privileges;
921         char *priv;
922
923         if (datacontrol == NULL)
924                 return 0;
925
926         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
927         if (ret != SQLITE_OK) {
928                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
929                 return -1;
930         }
931
932         for (privileges = datacontrol->privileges; privileges;
933                         privileges = privileges->next) {
934                 priv = (char *)privileges->data;
935                 if (priv == NULL)
936                         continue;
937
938                 idx = 1;
939                 __BIND_TEXT(db, stmt, idx++, datacontrol->providerid);
940                 __BIND_TEXT(db, stmt, idx++, priv);
941                 __BIND_TEXT(db, stmt, idx++, datacontrol->type);
942
943                 ret = sqlite3_step(stmt);
944                 if (ret != SQLITE_DONE) {
945                         _LOGE("step failed: %s", sqlite3_errmsg(db));
946                         sqlite3_finalize(stmt);
947                         return -1;
948                 }
949
950                 sqlite3_reset(stmt);
951         }
952
953         sqlite3_finalize(stmt);
954         return 0;
955 }
956
957 static int __insert_datacontrol_info(sqlite3 *db, application_x *app)
958 {
959         static const char query[] =
960                 "INSERT INTO package_app_data_control (app_id, providerid,"
961                 "  access, type, trusted) VALUES (?, ?, ?, ?, ?)";
962         int ret;
963         sqlite3_stmt *stmt;
964         int idx;
965         GList *tmp;
966         datacontrol_x *dc;
967
968         if (app->datacontrol == NULL)
969                 return 0;
970
971         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
972         if (ret != SQLITE_OK) {
973                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
974                 return -1;
975         }
976
977         for (tmp = app->datacontrol; tmp; tmp = tmp->next) {
978                 dc = (datacontrol_x *)tmp->data;
979                 if (dc == NULL)
980                         continue;
981                 idx = 1;
982                 __BIND_TEXT(db, stmt, idx++, app->appid);
983                 __BIND_TEXT(db, stmt, idx++, dc->providerid);
984                 __BIND_TEXT(db, stmt, idx++, dc->access);
985                 __BIND_TEXT(db, stmt, idx++, dc->type);
986                 __BIND_TEXT(db, stmt, idx++, dc->trusted);
987
988                 ret = sqlite3_step(stmt);
989                 if (ret != SQLITE_DONE) {
990                         _LOGE("step failed: %s", sqlite3_errmsg(db));
991                         sqlite3_finalize(stmt);
992                         return -1;
993                 }
994
995                 if (dc->privileges &&
996                                 __insert_app_data_control_privilege_info(db, dc)) {
997                         sqlite3_finalize(stmt);
998                         return -1;
999                 }
1000
1001                 sqlite3_reset(stmt);
1002         }
1003
1004         sqlite3_finalize(stmt);
1005
1006         return 0;
1007 }
1008
1009 /* TODO: move to installer */
1010 static int __check_dpi(const char *dpi_char, int dpi_int)
1011 {
1012         if (dpi_char == NULL)
1013                 return -1;
1014
1015         if (strcasecmp(dpi_char, LDPI) == 0) {
1016                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
1017                         return 0;
1018                 else
1019                         return -1;
1020         } else if (strcasecmp(dpi_char, MDPI) == 0) {
1021                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
1022                         return 0;
1023                 else
1024                         return -1;
1025         } else if (strcasecmp(dpi_char, HDPI) == 0) {
1026                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
1027                         return 0;
1028                 else
1029                         return -1;
1030         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
1031                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
1032                         return 0;
1033                 else
1034                         return -1;
1035         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
1036                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
1037                         return 0;
1038                 else
1039                         return -1;
1040         } else
1041                 return -1;
1042 }
1043
1044 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a,
1045                 gconstpointer b)
1046 {
1047         splashscreen_x *ss = (splashscreen_x *)a;
1048         const char *orientation = (const char *)b;
1049         int dpi = -1;
1050         int ret;
1051
1052         if (ss->operation || ss->dpi == NULL)
1053                 return -1;
1054
1055         ret = system_info_get_platform_int(
1056                         "http://tizen.org/feature/screen.dpi", &dpi);
1057         if (ret != SYSTEM_INFO_ERROR_NONE)
1058                 return -1;
1059
1060         if (strcasecmp(ss->orientation, orientation) == 0 &&
1061                         __check_dpi(ss->dpi, dpi) == 0)
1062                 return 0;
1063
1064         return -1;
1065 }
1066
1067 static gint __compare_splashscreen_with_orientation(gconstpointer a,
1068                 gconstpointer b)
1069 {
1070         splashscreen_x *ss = (splashscreen_x *)a;
1071         const char *orientation = (const char *)b;
1072
1073         if (ss->operation || ss->dpi)
1074                 return -1;
1075
1076         if (strcasecmp(ss->orientation, orientation) == 0)
1077                 return 0;
1078
1079         return -1;
1080 }
1081
1082 static splashscreen_x *__find_default_splashscreen(GList *splashscreens,
1083                 const char *orientation)
1084 {
1085         GList *tmp;
1086
1087         tmp = g_list_find_custom(splashscreens, orientation,
1088                         (GCompareFunc)
1089                         __compare_splashscreen_with_orientation_dpi);
1090         if (tmp)
1091                 return (splashscreen_x *)tmp->data;
1092
1093         tmp = g_list_find_custom(splashscreens, orientation,
1094                         (GCompareFunc)__compare_splashscreen_with_orientation);
1095         if (tmp)
1096                 return (splashscreen_x *)tmp->data;
1097
1098         return NULL;
1099 }
1100
1101 static void __find_appcontrol_splashscreen_with_dpi(gpointer data,
1102                 gpointer user_data)
1103 {
1104         splashscreen_x *ss = (splashscreen_x *)data;
1105         GList **list = (GList **)user_data;
1106         int dpi = -1;
1107         int ret;
1108
1109         if (ss->operation == NULL || ss->dpi == NULL)
1110                 return;
1111
1112         ret = system_info_get_platform_int(
1113                         "http://tizen.org/feature/screen.dpi", &dpi);
1114         if (ret != SYSTEM_INFO_ERROR_NONE)
1115                 return;
1116
1117         if (__check_dpi(ss->dpi, dpi) != 0)
1118                 return;
1119
1120         *list = g_list_append(*list, ss);
1121 }
1122
1123 static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data)
1124 {
1125         splashscreen_x *ss = (splashscreen_x *)data;
1126         GList **list = (GList **)user_data;
1127         splashscreen_x *ss_tmp;
1128         GList *tmp;
1129
1130         if (ss->operation == NULL || ss->dpi)
1131                 return;
1132
1133         for (tmp = *list; tmp; tmp = tmp->next) {
1134                 ss_tmp = (splashscreen_x *)tmp->data;
1135                 if (ss_tmp->operation
1136                         && strcmp(ss_tmp->operation, ss->operation) == 0
1137                         && strcmp(ss_tmp->orientation, ss->orientation) == 0)
1138                         return;
1139         }
1140
1141         *list = g_list_append(*list, ss);
1142 }
1143
1144 static GList *__find_splashscreens(GList *splashscreens)
1145 {
1146         GList *list = NULL;
1147         splashscreen_x *ss;
1148
1149         if (splashscreens == NULL)
1150                 return NULL;
1151
1152         g_list_foreach(splashscreens,
1153                         __find_appcontrol_splashscreen_with_dpi, &list);
1154         g_list_foreach(splashscreens,
1155                         __find_appcontrol_splashscreen, &list);
1156
1157         ss = __find_default_splashscreen(splashscreens, "portrait");
1158         if (ss)
1159                 list = g_list_append(list, ss);
1160         ss = __find_default_splashscreen(splashscreens, "landscape");
1161         if (ss)
1162                 list = g_list_append(list, ss);
1163
1164         return list;
1165 }
1166
1167 static int __insert_splashscreen_info(sqlite3 *db, application_x *app,
1168                 GList *ss_list)
1169 {
1170         static const char query[] =
1171                 "INSERT INTO package_app_splash_screen (app_id, src, type,"
1172                 "  orientation, indicatordisplay, operation, color_depth) "
1173                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1174         int ret;
1175         sqlite3_stmt *stmt;
1176         int idx;
1177         GList *tmp;
1178         splashscreen_x *ss;
1179
1180         if (app->splashscreens == NULL)
1181                 return 0;
1182
1183         if (ss_list == NULL)
1184                 return 0;
1185
1186         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1187         if (ret != SQLITE_OK) {
1188                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1189                 return -1;
1190         }
1191
1192         for (tmp = ss_list; tmp; tmp = tmp->next) {
1193                 ss = (splashscreen_x *)tmp->data;
1194                 if (ss == NULL)
1195                         continue;
1196                 idx = 1;
1197                 __BIND_TEXT(db, stmt, idx++, app->appid);
1198                 __BIND_TEXT(db, stmt, idx++, ss->src);
1199                 __BIND_TEXT(db, stmt, idx++, ss->type);
1200                 __BIND_TEXT(db, stmt, idx++, ss->orientation);
1201                 __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay);
1202                 __BIND_TEXT(db, stmt, idx++, ss->operation);
1203                 __BIND_TEXT(db, stmt, idx++, ss->color_depth);
1204
1205                 ret = sqlite3_step(stmt);
1206                 if (ret != SQLITE_DONE) {
1207                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1208                         sqlite3_finalize(stmt);
1209                         return -1;
1210                 }
1211
1212                 sqlite3_reset(stmt);
1213         }
1214
1215         sqlite3_finalize(stmt);
1216
1217         return 0;
1218 }
1219
1220 static void __trimfunc(GList *trim_list)
1221 {
1222         char *trim_data;
1223         char *prev = NULL;
1224         GList *list = g_list_first(trim_list);
1225
1226         while (list) {
1227                 trim_data = (char *)list->data;
1228                 if (trim_data) {
1229                         if (prev) {
1230                                 if (strcmp(trim_data, prev) == 0) {
1231                                         trim_list = g_list_remove(trim_list,
1232                                                         trim_data);
1233                                         list = g_list_first(trim_list);
1234                                         prev = NULL;
1235                                         continue;
1236                                 } else
1237                                         prev = trim_data;
1238                         } else {
1239                                 prev = trim_data;
1240                         }
1241                 }
1242                 list = g_list_next(list);
1243         }
1244 }
1245
1246 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
1247 {
1248         if (a == NULL || b == NULL)
1249                 return 0;
1250         if (strcmp((char *)a, (char *)b) == 0)
1251                 return 0;
1252         if (strcmp((char *)a, (char *)b) < 0)
1253                 return -1;
1254         if (strcmp((char *)a, (char *)b) > 0)
1255                 return 1;
1256         return 0;
1257 }
1258
1259 /* TODO: refactor inserting localized info */
1260 static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns,
1261                 GList *dcns, GList *aths)
1262 {
1263         GList *locale = NULL;
1264         GList *tmp;
1265         label_x *lbl;
1266         license_x *lcn;
1267         icon_x *icn;
1268         description_x *dcn;
1269         author_x *ath;
1270
1271         for (tmp = lbls; tmp; tmp = tmp->next) {
1272                 lbl = (label_x *)tmp->data;
1273                 if (lbl == NULL)
1274                         continue;
1275                 if (lbl->lang)
1276                         locale = g_list_insert_sorted_with_data(
1277                                         locale, (gpointer)lbl->lang,
1278                                         __comparefunc, NULL);
1279         }
1280         for (tmp = lcns; tmp; tmp = tmp->next) {
1281                 lcn = (license_x *)tmp->data;
1282                 if (lcn == NULL)
1283                         continue;
1284                 if (lcn->lang)
1285                         locale = g_list_insert_sorted_with_data(
1286                                         locale, (gpointer)lcn->lang,
1287                                         __comparefunc, NULL);
1288         }
1289         for (tmp = icns; tmp; tmp = tmp->next) {
1290                 icn = (icon_x *)tmp->data;
1291                 if (icn == NULL)
1292                         continue;
1293                 if (icn->lang)
1294                         locale = g_list_insert_sorted_with_data(
1295                                         locale, (gpointer)icn->lang,
1296                                         __comparefunc, NULL);
1297         }
1298         for (tmp = dcns; tmp; tmp = tmp->next) {
1299                 dcn = (description_x *)tmp->data;
1300                 if (dcn == NULL)
1301                         continue;
1302                 if (dcn->lang)
1303                         locale = g_list_insert_sorted_with_data(
1304                                         locale, (gpointer)dcn->lang,
1305                                         __comparefunc, NULL);
1306         }
1307         for (tmp = aths; tmp; tmp = tmp->next) {
1308                 ath = (author_x *)tmp->data;
1309                 if (ath == NULL)
1310                         continue;
1311                 if (ath->lang)
1312                         locale = g_list_insert_sorted_with_data(
1313                                         locale, (gpointer)ath->lang,
1314                                         __comparefunc, NULL);
1315         }
1316         __trimfunc(locale);
1317         return locale;
1318 }
1319
1320 static gint __check_icon_resolution(const char *orig_icon_path,
1321                 char **new_icon_path)
1322 {
1323         int ret;
1324         char *dpi_path[2];
1325         char *icon_filename;
1326         char modified_iconpath[BUFSIZE];
1327         char icon_path[BUFSIZE];
1328         int i;
1329         int dpi = -1;
1330
1331         if (orig_icon_path == NULL)
1332                 return -1;
1333
1334         ret = system_info_get_platform_int(
1335                         "http://tizen.org/feature/screen.dpi", &dpi);
1336         if (ret != SYSTEM_INFO_ERROR_NONE)
1337                 return -1;
1338
1339         if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) {
1340                 dpi_path[0] = "LDPI";
1341                 dpi_path[1] = "ldpi";
1342         } else if (dpi >= MDPI_MIN && dpi <= MDPI_MAX) {
1343                 dpi_path[0] = "MDPI";
1344                 dpi_path[1] = "mdpi";
1345         } else if (dpi >= HDPI_MIN && dpi <= HDPI_MAX) {
1346                 dpi_path[0] = "HDPI";
1347                 dpi_path[1] = "hdpi";
1348         } else if (dpi >= XHDPI_MIN && dpi <= XHDPI_MAX) {
1349                 dpi_path[0] = "XHDPI";
1350                 dpi_path[1] = "xhdpi";
1351         } else if (dpi >= XXHDPI_MIN && dpi <= XXHDPI_MAX) {
1352                 dpi_path[0] = "XXHDPI";
1353                 dpi_path[1] = "xxhdpi";
1354         } else {
1355                 _LOGE("Unidentified dpi[%d]", dpi);
1356                 return -1;
1357         }
1358
1359         icon_filename = strrchr(orig_icon_path, '/');
1360         if (icon_filename == NULL)
1361                 return -1;
1362
1363         snprintf(icon_path,
1364                         strlen(orig_icon_path) - (strlen(icon_filename) - 1),
1365                         "%s", orig_icon_path);
1366         for (i = 0; i < 2; i++) {
1367                 ret = snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s",
1368                                 icon_path, dpi_path[i], icon_filename);
1369                 if (ret < 0 || ret > BUFSIZE -1) {
1370                         _LOGE("snprintf fail");
1371                         return -1;
1372                 }
1373                 if (access(modified_iconpath, F_OK) != -1) {
1374                         /* if exists, return modified icon path */
1375                         *new_icon_path = strdup(modified_iconpath);
1376                         return 0;
1377                 }
1378         }
1379
1380         return -1;
1381 }
1382
1383 static gint __compare_icon(gconstpointer a, gconstpointer b)
1384 {
1385         icon_x *icon = (icon_x *)a;
1386         char *icon_path;
1387
1388         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1389                 return -1;
1390
1391         if (icon->dpi != NULL)
1392                 return -1;
1393
1394         if (__check_icon_resolution(icon->text, &icon_path) == 0) {
1395                 free(icon->text);
1396                 icon->text = icon_path;
1397         }
1398
1399         return 0;
1400 }
1401
1402 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
1403 {
1404         icon_x *icon = (icon_x *)a;
1405         int dpi = GPOINTER_TO_INT(b);
1406
1407         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1408                 return -1;
1409
1410         if (icon->dpi == NULL)
1411                 return -1;
1412
1413         if (__check_dpi(icon->dpi, dpi) == 0)
1414                 return 0;
1415
1416         return -1;
1417 }
1418
1419 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
1420 {
1421         icon_x *icon = (icon_x *)a;
1422         char *lang = (char *)b;
1423         char *icon_path;
1424
1425         if (icon->dpi != NULL)
1426                 return -1;
1427
1428         if (strcasecmp(icon->lang, lang) == 0) {
1429                 if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) {
1430                         /* icon for no locale. check existance of
1431                          * folder-hierachied default icons
1432                          */
1433                         if (__check_icon_resolution(icon->text,
1434                                                 &icon_path) == 0) {
1435                                 free(icon->text);
1436                                 icon->text = icon_path;
1437                         }
1438                 }
1439                 return 0;
1440         }
1441
1442         return -1;
1443 }
1444
1445 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
1446 {
1447         int ret;
1448         icon_x *icon = (icon_x *)a;
1449         char *lang = (char *)b;
1450         int dpi = -1;
1451
1452         ret = system_info_get_platform_int(
1453                         "http://tizen.org/feature/screen.dpi", &dpi);
1454         if (ret != SYSTEM_INFO_ERROR_NONE)
1455                 return -1;
1456
1457         if (strcasecmp(icon->lang, lang) == 0 &&
1458                         __check_dpi(icon->dpi, dpi) == 0)
1459                 return 0;
1460
1461         return -1;
1462 }
1463
1464 static char *__find_icon(GList *icons, const char *lang)
1465 {
1466         GList *tmp;
1467         icon_x *icon;
1468         int dpi = 0;
1469         int ret;
1470
1471         /* first, find icon whose locale and dpi with given lang and
1472          * system's dpi has matched
1473          */
1474         tmp = g_list_find_custom(icons, lang,
1475                         (GCompareFunc)__compare_icon_with_lang_dpi);
1476         if (tmp != NULL) {
1477                 icon = (icon_x *)tmp->data;
1478                 return (char *)icon->text;
1479         }
1480
1481         /* if first has failed, find icon whose locale has matched */
1482         tmp = g_list_find_custom(icons, lang,
1483                         (GCompareFunc)__compare_icon_with_lang);
1484         if (tmp != NULL) {
1485                 icon = (icon_x *)tmp->data;
1486                 return (char *)icon->text;
1487         }
1488
1489         /* if second has failed, find icon whose dpi has matched with
1490          * system's dpi
1491          */
1492         ret = system_info_get_platform_int(
1493                         "http://tizen.org/feature/screen.dpi", &dpi);
1494         if (ret == SYSTEM_INFO_ERROR_NONE) {
1495                 tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi),
1496                                 (GCompareFunc)__compare_icon_with_dpi);
1497                 if (tmp != NULL) {
1498                         icon = (icon_x *)tmp->data;
1499                         return (char *)icon->text;
1500                 }
1501         }
1502
1503         /* last, find default icon marked as "No Locale" */
1504         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
1505         if (tmp != NULL) {
1506                 icon = (icon_x *)tmp->data;
1507                 return (char *)icon->text;
1508         }
1509
1510         return NULL;
1511 }
1512
1513 static void __extract_data(const char *locale, GList *lbls, GList *lcns,
1514                 GList *icns, GList *dcns, GList *aths, char **label,
1515                 char **license, char **icon, char **description, char **author)
1516 {
1517         GList *tmp;
1518         label_x *lbl;
1519         license_x *lcn;
1520         description_x *dcn;
1521         author_x *ath;
1522
1523         for (tmp = lbls; tmp; tmp = tmp->next) {
1524                 lbl = (label_x *)tmp->data;
1525                 if (lbl == NULL)
1526                         continue;
1527                 if (lbl->lang) {
1528                         if (strcmp(lbl->lang, locale) == 0) {
1529                                 *label = (char *)lbl->text;
1530                                 break;
1531                         }
1532                 }
1533         }
1534         for (tmp = lcns; tmp; tmp = tmp->next) {
1535                 lcn = (license_x *)tmp->data;
1536                 if (lcn == NULL)
1537                         continue;
1538                 if (lcn->lang) {
1539                         if (strcmp(lcn->lang, locale) == 0) {
1540                                 *license = (char *)lcn->text;
1541                                 break;
1542                         }
1543                 }
1544         }
1545
1546         *icon = __find_icon(icns, locale);
1547
1548         for (tmp = dcns; tmp; tmp = tmp->next) {
1549                 dcn = (description_x *)tmp->data;
1550                 if (dcn == NULL)
1551                         continue;
1552                 if (dcn->lang) {
1553                         if (strcmp(dcn->lang, locale) == 0) {
1554                                 *description = (char *)dcn->text;
1555                                 break;
1556                         }
1557                 }
1558         }
1559         for (tmp = aths; tmp; tmp = tmp->next) {
1560                 ath = (author_x *)tmp->data;
1561                 if (ath == NULL)
1562                         continue;
1563                 if (ath->lang) {
1564                         if (strcmp(ath->lang, locale) == 0) {
1565                                 *author = (char *)ath->text;
1566                                 break;
1567                         }
1568                 }
1569         }
1570 }
1571
1572 static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app,
1573                 const char *locale, const char *label, const char *icon)
1574 {
1575         static const char query[] =
1576                 "INSERT OR REPLACE INTO package_localized_info ("
1577                 "  package, package_locale, package_label, package_icon,"
1578                 "  package_description, package_license, package_author) "
1579                 "VALUES (?, ?,"
1580                 "  COALESCE((SELECT package_label FROM package_localized_info"
1581                 "            WHERE package=? AND package_locale=?), ?),"
1582                 "  COALESCE((SELECT package_icon FROM package_localized_info"
1583                 "            WHERE package=? AND package_icon=?), ?),"
1584                 "  (SELECT package_description FROM package_localized_info"
1585                 "   WHERE package=? AND package_locale=?),"
1586                 "  (SELECT package_description FROM package_localized_info"
1587                 "   WHERE package=? AND package_locale=?),"
1588                 "  (SELECT package_description FROM package_localized_info"
1589                 "   WHERE package=? AND package_locale=?))";
1590         int ret;
1591         sqlite3_stmt *stmt;
1592         int idx = 1;
1593
1594         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1595         if (ret != SQLITE_OK) {
1596                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1597                 return -1;
1598         }
1599
1600         __BIND_TEXT(db, stmt, idx++, app->package);
1601         __BIND_TEXT(db, stmt, idx++, locale);
1602         __BIND_TEXT(db, stmt, idx++, app->package);
1603         __BIND_TEXT(db, stmt, idx++, locale);
1604         __BIND_TEXT(db, stmt, idx++, label);
1605         __BIND_TEXT(db, stmt, idx++, app->package);
1606         __BIND_TEXT(db, stmt, idx++, locale);
1607         __BIND_TEXT(db, stmt, idx++, icon);
1608         __BIND_TEXT(db, stmt, idx++, app->package);
1609         __BIND_TEXT(db, stmt, idx++, locale);
1610         __BIND_TEXT(db, stmt, idx++, app->package);
1611         __BIND_TEXT(db, stmt, idx++, locale);
1612         __BIND_TEXT(db, stmt, idx++, app->package);
1613         __BIND_TEXT(db, stmt, idx++, locale);
1614
1615         ret = sqlite3_step(stmt);
1616         if (ret != SQLITE_DONE) {
1617                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1618                 sqlite3_finalize(stmt);
1619                 return -1;
1620         }
1621
1622         sqlite3_finalize(stmt);
1623
1624         return 0;
1625 }
1626
1627 static int __insert_app_localized_info(sqlite3 *db, application_x *app)
1628 {
1629         static const char query[] =
1630                 "INSERT INTO package_app_localized_info (app_id, app_locale,"
1631                 "  app_label, app_icon) "
1632                 "VALUES (?, ?, ?, ?)";
1633         int ret;
1634         sqlite3_stmt *stmt;
1635         int idx;
1636         GList *tmp;
1637         GList *locales;
1638         const char *locale;
1639         char *label;
1640         char *icon;
1641
1642         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1643         if (ret != SQLITE_OK) {
1644                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1645                 return -1;
1646         }
1647
1648         locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL);
1649         for (tmp = locales; tmp; tmp = tmp->next) {
1650                 locale = (const char *)tmp->data;
1651                 label = NULL;
1652                 icon = NULL;
1653                 __extract_data(locale, app->label, NULL, app->icon, NULL, NULL,
1654                                 &label, NULL, &icon, NULL, NULL);
1655                 if (!label && !icon)
1656                         continue;
1657
1658                 idx = 1;
1659                 __BIND_TEXT(db, stmt, idx++, app->appid);
1660                 __BIND_TEXT(db, stmt, idx++, locale);
1661                 __BIND_TEXT(db, stmt, idx++, label);
1662                 __BIND_TEXT(db, stmt, idx++, icon);
1663
1664                 ret = sqlite3_step(stmt);
1665                 if (ret != SQLITE_DONE) {
1666                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1667                         g_list_free(locales);
1668                         sqlite3_finalize(stmt);
1669                         return -1;
1670                 }
1671
1672                 sqlite3_reset(stmt);
1673
1674                 if (strcasecmp(app->mainapp, "true") == 0) {
1675                         if (__insert_mainapp_localized_info(db, app, locale,
1676                                                 label, icon))
1677                                 _LOGE("insert mainapp localized info failed");
1678                 }
1679         }
1680
1681         g_list_free(locales);
1682         sqlite3_finalize(stmt);
1683
1684         return 0;
1685 }
1686
1687 static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx)
1688 {
1689         static const char query[] =
1690                 "INSERT INTO package_privilege_info (package, privilege, type) "
1691                 "VALUES (?, ?, ?)";
1692         int ret;
1693         sqlite3_stmt *stmt;
1694         int idx;
1695         GList *tmp;
1696         privilege_x *priv;
1697
1698         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1699         if (ret != SQLITE_OK) {
1700                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1701                 return -1;
1702         }
1703
1704         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1705                 priv = (privilege_x *)tmp->data;
1706                 if (priv == NULL)
1707                         continue;
1708
1709                 idx = 1;
1710                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1711                 __BIND_TEXT(db, stmt, idx++, priv->value);
1712                 __BIND_TEXT(db, stmt, idx++, priv->type);
1713
1714                 ret = sqlite3_step(stmt);
1715                 if (ret != SQLITE_DONE) {
1716                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1717                         sqlite3_finalize(stmt);
1718                         return -1;
1719                 }
1720                 sqlite3_reset(stmt);
1721         }
1722
1723         sqlite3_finalize(stmt);
1724
1725         return 0;
1726 }
1727
1728 static int __insert_package_plugin_execution_info(sqlite3 *db,
1729                 manifest_x *mfx)
1730 {
1731         static const char query[] =
1732                 "INSERT INTO package_plugin_info "
1733                 "(pkgid, appid, plugin_type, plugin_name) "
1734                 "VALUES (?, ?, ?, ?)";
1735         int ret;
1736         sqlite3_stmt *stmt;
1737         int idx;
1738         GList *tmp;
1739         plugin_x *plugin;
1740
1741         if (!mfx->plugin)
1742                 return 0;
1743
1744         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1745         if (ret != SQLITE_OK) {
1746                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1747                 return -1;
1748         }
1749
1750         for (tmp = mfx->plugin; tmp; tmp = tmp->next) {
1751                 plugin = (plugin_x *)tmp->data;
1752                 if (plugin == NULL)
1753                         continue;
1754
1755                 idx = 1;
1756                 __BIND_TEXT(db, stmt, idx++, plugin->pkgid);
1757                 __BIND_TEXT(db, stmt, idx++, plugin->appid);
1758                 __BIND_TEXT(db, stmt, idx++, plugin->plugin_type);
1759                 __BIND_TEXT(db, stmt, idx++, plugin->plugin_name);
1760
1761                 ret = sqlite3_step(stmt);
1762                 if (ret != SQLITE_DONE) {
1763                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1764                         sqlite3_finalize(stmt);
1765                         return -1;
1766                 }
1767                 sqlite3_reset(stmt);
1768         }
1769
1770         sqlite3_finalize(stmt);
1771
1772         return 0;
1773 }
1774
1775 static int __delete_package_plugin_execution_info(sqlite3 *db,
1776                 const char *pkgid)
1777 {
1778         static const char query[] =
1779                 "DELETE FROM package_plugin_info WHERE pkgid=?";
1780         int ret;
1781         sqlite3_stmt *stmt;
1782
1783         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1784         if (ret != SQLITE_OK) {
1785                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1786                 return -1;
1787         }
1788
1789         __BIND_TEXT(db, stmt, 1, pkgid);
1790
1791         ret = sqlite3_step(stmt);
1792         if (ret != SQLITE_DONE) {
1793                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1794                 sqlite3_finalize(stmt);
1795                 return -1;
1796         }
1797
1798         sqlite3_finalize(stmt);
1799
1800         return 0;
1801 }
1802
1803 static int __insert_package_appdefined_privilege_info(sqlite3 *db,
1804                 manifest_x *mfx)
1805 {
1806         static const char query[] =
1807                 "INSERT INTO package_appdefined_privilege_info "
1808                 "(package, privilege, license, type) "
1809                 "VALUES (?, ?, ?, ?)";
1810         int ret;
1811         sqlite3_stmt *stmt;
1812         int idx;
1813         GList *tmp;
1814         appdefined_privilege_x *priv;
1815
1816         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1817         if (ret != SQLITE_OK) {
1818                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1819                 return -1;
1820         }
1821
1822         for (tmp = mfx->appdefined_privileges; tmp; tmp = tmp->next) {
1823                 priv = (appdefined_privilege_x *)tmp->data;
1824                 if (priv == NULL)
1825                         continue;
1826
1827                 idx = 1;
1828                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1829                 __BIND_TEXT(db, stmt, idx++, priv->value);
1830                 __BIND_TEXT(db, stmt, idx++, priv->license);
1831                 __BIND_TEXT(db, stmt, idx++, priv->type);
1832
1833                 ret = sqlite3_step(stmt);
1834                 if (ret != SQLITE_DONE) {
1835                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1836                         sqlite3_finalize(stmt);
1837                         return -1;
1838                 }
1839                 sqlite3_reset(stmt);
1840         }
1841
1842         sqlite3_finalize(stmt);
1843
1844         return 0;
1845 }
1846
1847 static int __insert_package_dependency_info(sqlite3 *db, manifest_x *mfx)
1848 {
1849         static const char query[] =
1850                 "INSERT INTO package_dependency_info"
1851                 "  (package, depends_on, type, required_version) "
1852                 "VALUES (?, ?, ?, ?)";
1853         int ret;
1854         sqlite3_stmt *stmt;
1855         int idx;
1856         GList *tmp;
1857         dependency_x *dep;
1858
1859         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1860         if (ret != SQLITE_OK) {
1861                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1862                 return -1;
1863         }
1864
1865         for (tmp = mfx->dependencies; tmp; tmp = tmp->next) {
1866                 dep = (dependency_x *)tmp->data;
1867                 if (dep == NULL)
1868                         continue;
1869
1870                 idx = 1;
1871                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1872                 __BIND_TEXT(db, stmt, idx++, dep->depends_on);
1873                 __BIND_TEXT(db, stmt, idx++, dep->type);
1874                 __BIND_TEXT(db, stmt, idx++, dep->required_version);
1875
1876                 ret = sqlite3_step(stmt);
1877                 if (ret != SQLITE_DONE) {
1878                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1879                         sqlite3_finalize(stmt);
1880                         return -1;
1881                 }
1882                 sqlite3_reset(stmt);
1883         }
1884
1885         sqlite3_finalize(stmt);
1886
1887         return 0;
1888 }
1889
1890 /* _PRODUCT_LAUNCHING_ENHANCED_
1891  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
1892  *  app->guestmode_appstatus
1893  */
1894 static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
1895 {
1896         static const char query[] =
1897                 "INSERT INTO package_app_info (app_id, app_component,"
1898                 "  app_exec, app_nodisplay, app_type, app_onboot, app_multiple,"
1899                 "  app_autorestart, app_taskmanage, app_hwacceleration,"
1900                 "  app_screenreader, app_mainapp, app_recentimage,"
1901                 "  app_launchcondition, app_indicatordisplay, app_portraitimg,"
1902                 "  app_landscapeimg, app_guestmodevisibility,"
1903                 "  app_permissiontype, app_preload, app_submode,"
1904                 "  app_submode_mainid, app_installed_storage, app_process_pool,"
1905                 "  app_launch_mode, app_ui_gadget, app_support_mode,"
1906                 "  app_support_disable, component_type, package, app_tep_name,"
1907                 "  app_zip_mount_file, app_background_category,"
1908                 "  app_package_type, app_root_path, app_api_version,"
1909                 "  app_effective_appid, app_splash_screen_display,"
1910                 "  app_package_system, app_removable,"
1911                 "  app_package_installed_time, app_support_ambient,"
1912                 "  app_external_path, app_setup_appid) "
1913                 "VALUES (?, ?, "
1914                 "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
1915                 "  LOWER(?), LOWER(?), ?,"
1916                 "  ?, LOWER(?), ?,"
1917                 "  ?, LOWER(?), ?,"
1918                 "  ?, LOWER(?),"
1919                 "  ?, LOWER(?), LOWER(?),"
1920                 "  ?, ?, LOWER(?),"
1921                 "  COALESCE(?, 'single'), LOWER(?), ?,"
1922                 "  LOWER(?), ?, ?, ?,"
1923                 "  ?, ?,"
1924                 "  ?, ?, ?,"
1925                 "  ?, LOWER(?),"
1926                 "  LOWER(?), LOWER(?),"
1927                 "  ?, LOWER(?),"
1928                 "  ?, ?)";
1929         int ret;
1930         sqlite3_stmt *stmt;
1931         int idx;
1932         GList *tmp;
1933         application_x *app;
1934         int bg_category;
1935         const char *effective_appid;
1936         GList *ss_list;
1937
1938         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1939         if (ret != SQLITE_OK) {
1940                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1941                 return -1;
1942         }
1943
1944         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1945                 app = (application_x *)tmp->data;
1946                 if (app == NULL)
1947                         continue;
1948
1949                 bg_category = __convert_background_category(
1950                                 app->background_category);
1951                 effective_appid = __find_effective_appid(app->metadata);
1952
1953                 idx = 1;
1954                 __BIND_TEXT(db, stmt, idx++, app->appid);
1955                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1956                 __BIND_TEXT(db, stmt, idx++, app->exec);
1957                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false));
1958                 __BIND_TEXT(db, stmt, idx++, app->type);
1959                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false));
1960                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false));
1961                 __BIND_TEXT(db, stmt, idx++,
1962                                 __get_bool(app->autorestart, false));
1963                 __BIND_TEXT(db, stmt, idx++,
1964                                 __get_bool(app->taskmanage, false));
1965                 __BIND_TEXT(db, stmt, idx++, app->hwacceleration);
1966                 __BIND_TEXT(db, stmt, idx++, app->screenreader);
1967                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false));
1968                 __BIND_TEXT(db, stmt, idx++, app->recentimage);
1969                 __BIND_TEXT(db, stmt, idx++, app->launchcondition);
1970                 __BIND_TEXT(db, stmt, idx++,
1971                                 __get_bool(app->indicatordisplay, true));
1972                 __BIND_TEXT(db, stmt, idx++, app->portraitimg);
1973                 __BIND_TEXT(db, stmt, idx++, app->landscapeimg);
1974                 __BIND_TEXT(db, stmt, idx++,
1975                                 __get_bool(app->guestmode_visibility, true));
1976                 __BIND_TEXT(db, stmt, idx++, app->permission_type);
1977                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1978                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false));
1979                 __BIND_TEXT(db, stmt, idx++, app->submode_mainid);
1980                 __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1981                 __BIND_TEXT(db, stmt, idx++,
1982                                 __get_bool(app->process_pool, false));
1983                 __BIND_TEXT(db, stmt, idx++, app->launch_mode);
1984                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false));
1985                 __BIND_TEXT(db, stmt, idx++,
1986                                 app->support_mode ? app->support_mode : "0");
1987                 __BIND_TEXT(db, stmt, idx++,
1988                                 __get_bool(mfx->support_disable, false));
1989                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1990                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1991                 __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1992                 __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1993                 __BIND_INT(db, stmt, idx++, bg_category);
1994                 __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk");
1995                 __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1996                 __BIND_TEXT(db, stmt, idx++, app->api_version);
1997                 __BIND_TEXT(db, stmt, idx++, effective_appid);
1998                 __BIND_TEXT(db, stmt, idx++,
1999                                 __get_bool(app->splash_screen_display, true));
2000                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
2001                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false));
2002                 __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
2003                 __BIND_TEXT(db, stmt, idx++,
2004                                 __get_bool(app->support_ambient, false));
2005                 __BIND_TEXT(db, stmt, idx++, mfx->external_path);
2006                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
2007
2008                 ret = sqlite3_step(stmt);
2009                 if (ret != SQLITE_DONE) {
2010                         _LOGE("step failed: %s", sqlite3_errmsg(db));
2011                         sqlite3_finalize(stmt);
2012                         return -1;
2013                 }
2014
2015                 sqlite3_reset(stmt);
2016
2017                 if (__insert_appcontrol_info(db, app)) {
2018                         sqlite3_finalize(stmt);
2019                         return -1;
2020                 }
2021                 if (__insert_category_info(db, app)) {
2022                         sqlite3_finalize(stmt);
2023                         return -1;
2024                 }
2025                 if (__insert_metadata_info(db, app)) {
2026                         sqlite3_finalize(stmt);
2027                         return -1;
2028                 }
2029                 if (__insert_datacontrol_info(db, app)) {
2030                         sqlite3_finalize(stmt);
2031                         return -1;
2032                 }
2033                 ss_list = __find_splashscreens(app->splashscreens);
2034                 if (__insert_splashscreen_info(db, app, ss_list)) {
2035                         g_list_free(ss_list);
2036                         sqlite3_finalize(stmt);
2037                         return -1;
2038                 }
2039                 g_list_free(ss_list);
2040                 if (__insert_app_localized_info(db, app)) {
2041                         sqlite3_finalize(stmt);
2042                         return -1;
2043                 }
2044         }
2045
2046         sqlite3_finalize(stmt);
2047
2048         return 0;
2049 }
2050
2051 static int __insert_package_update_info(sqlite3 *db, manifest_x *mfx)
2052 {
2053         static const char query[] =
2054                 "INSERT INTO package_update_info (package, update_version) "
2055                 "VALUES (?, ?)";
2056         int ret;
2057         int idx;
2058         sqlite3_stmt *stmt;
2059
2060         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2061         if (ret != SQLITE_OK) {
2062                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2063                 return -1;
2064         }
2065
2066         idx = 1;
2067         __BIND_TEXT(db, stmt, idx++, mfx->package);
2068         __BIND_TEXT(db, stmt, idx, mfx->version);
2069         ret = sqlite3_step(stmt);
2070         if (ret != SQLITE_DONE) {
2071                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2072                 sqlite3_finalize(stmt);
2073                 return -1;
2074         }
2075         sqlite3_finalize(stmt);
2076
2077         return 0;
2078 }
2079
2080 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
2081 {
2082         static const char query[] =
2083                 "INSERT INTO package_localized_info (package, package_locale,"
2084                 "  package_label, package_icon, package_description,"
2085                 "  package_license, package_author) "
2086                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
2087         int ret;
2088         sqlite3_stmt *stmt;
2089         int idx;
2090         GList *tmp;
2091         GList *locales;
2092         const char *locale;
2093         char *label;
2094         char *icon;
2095         char *description;
2096         char *license;
2097         char *author;
2098
2099         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2100         if (ret != SQLITE_OK) {
2101                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2102                 return -1;
2103         }
2104
2105         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
2106                         mfx->description, mfx->author);
2107         for (tmp = locales; tmp; tmp = tmp->next) {
2108                 locale = (const char *)tmp->data;
2109                 label = NULL;
2110                 icon = NULL;
2111                 description = NULL;
2112                 license = NULL;
2113                 author = NULL;
2114                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
2115                                 mfx->description, mfx->author,
2116                                 &label, &license, &icon, &description, &author);
2117                 if (!label && !license && !icon && !description && !author)
2118                         continue;
2119
2120                 idx = 1;
2121                 __BIND_TEXT(db, stmt, idx++, mfx->package);
2122                 __BIND_TEXT(db, stmt, idx++, locale);
2123                 __BIND_TEXT(db, stmt, idx++, label);
2124                 __BIND_TEXT(db, stmt, idx++, icon);
2125                 __BIND_TEXT(db, stmt, idx++, description);
2126                 __BIND_TEXT(db, stmt, idx++, license);
2127                 __BIND_TEXT(db, stmt, idx++, author);
2128
2129                 ret = sqlite3_step(stmt);
2130                 if (ret != SQLITE_DONE) {
2131                         _LOGE("step failed: %s", sqlite3_errmsg(db));
2132                         g_list_free(locales);
2133                         sqlite3_finalize(stmt);
2134                         return -1;
2135                 }
2136
2137                 sqlite3_reset(stmt);
2138         }
2139
2140         g_list_free(locales);
2141         sqlite3_finalize(stmt);
2142
2143         return 0;
2144 }
2145
2146 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
2147 {
2148         static const char query[] =
2149                 "INSERT INTO package_info (package, package_type,"
2150                 "  package_version, package_api_version, package_tep_name,"
2151                 "  package_zip_mount_file, install_location, package_size,"
2152                 "  package_removable, package_preload, package_readonly,"
2153                 "  package_update, package_appsetting, package_nodisplay,"
2154                 "  package_system, author_name, author_email, author_href,"
2155                 "  installed_time, installed_storage, storeclient_id,"
2156                 "  mainapp_id, package_url, root_path, external_path,"
2157                 "  csc_path, package_support_mode, package_support_disable) "
2158                 "VALUES (?, ?,"
2159                 "  ?, ?, ?,"
2160                 "  ?, ?, ?,"
2161                 "  LOWER(?), LOWER(?), LOWER(?),"
2162                 "  LOWER(?), LOWER(?), LOWER(?),"
2163                 "  LOWER(?), ?, ?, ?,"
2164                 "  ?, ?, ?,"
2165                 "  ?, ?, ?, ?,"
2166                 "  ?, ?, LOWER(?))";
2167         int ret;
2168         sqlite3_stmt *stmt;
2169         int idx = 1;
2170         const char *author_name = NULL;
2171         const char *author_email = NULL;
2172         const char *author_href = NULL;
2173
2174         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2175         if (ret != SQLITE_OK) {
2176                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2177                 return -1;
2178         }
2179
2180         if (mfx->author && mfx->author->data) {
2181                 author_name = ((author_x *)mfx->author->data)->text;
2182                 author_email = ((author_x *)mfx->author->data)->email;
2183                 author_href = ((author_x *)mfx->author->data)->href;
2184         }
2185
2186         __BIND_TEXT(db, stmt, idx++, mfx->package);
2187         __BIND_TEXT(db, stmt, idx++, mfx->type);
2188         __BIND_TEXT(db, stmt, idx++, mfx->version);
2189         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
2190         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
2191         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
2192         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
2193         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
2194         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
2195         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
2196         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
2197         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
2198         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
2199         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
2200         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
2201         __BIND_TEXT(db, stmt, idx++, author_name);
2202         __BIND_TEXT(db, stmt, idx++, author_email);
2203         __BIND_TEXT(db, stmt, idx++, author_href);
2204         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
2205         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
2206         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
2207         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
2208         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
2209         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
2210         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
2211         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
2212         __BIND_TEXT(db, stmt, idx++,
2213                         mfx->support_mode ? mfx->support_mode : "0");
2214         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
2215
2216         ret = sqlite3_step(stmt);
2217         if (ret != SQLITE_DONE) {
2218                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2219                 sqlite3_finalize(stmt);
2220                 return -1;
2221         }
2222
2223         sqlite3_finalize(stmt);
2224
2225         if (__insert_package_update_info(db, mfx))
2226                 return -1;
2227         if (__insert_package_localized_info(db, mfx))
2228                 return -1;
2229         if (__insert_application_info(db, mfx))
2230                 return -1;
2231         if (__insert_package_privilege_info(db, mfx))
2232                 return -1;
2233         if (__insert_package_appdefined_privilege_info(db, mfx))
2234                 return -1;
2235         if (__insert_package_dependency_info(db, mfx))
2236                 return -1;
2237
2238         return 0;
2239 }
2240
2241 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2242 {
2243         int ret;
2244         const char *dbpath;
2245         sqlite3 *db;
2246
2247         if (mfx == NULL) {
2248                 _LOGE("invalid parameter");
2249                 return PM_PARSER_R_EINVAL;
2250         }
2251
2252         dbpath = __get_parser_db_path(uid);
2253
2254         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2255         if (ret != SQLITE_OK) {
2256                 _LOGE("open db failed: %d", ret);
2257                 return PM_PARSER_R_ERROR;
2258         }
2259
2260         __BEGIN_TRANSACTION(db);
2261         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
2262         __END_TRANSACTION(db);
2263
2264         sqlite3_close_v2(db);
2265
2266         return PM_PARSER_R_OK;
2267 }
2268
2269 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
2270 {
2271         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
2272 }
2273
2274 static int __delete_package_info(sqlite3 *db, const char *pkgid)
2275 {
2276         static const char query[] =
2277                 "DELETE FROM package_info WHERE package=?";
2278         int ret;
2279         sqlite3_stmt *stmt;
2280
2281         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2282         if (ret != SQLITE_OK) {
2283                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2284                 return -1;
2285         }
2286
2287         __BIND_TEXT(db, stmt, 1, pkgid);
2288
2289         ret = sqlite3_step(stmt);
2290         if (ret != SQLITE_DONE) {
2291                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2292                 sqlite3_finalize(stmt);
2293                 return -1;
2294         }
2295
2296         sqlite3_finalize(stmt);
2297
2298         return 0;
2299 }
2300
2301 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
2302         uid_t uid)
2303 {
2304         int ret;
2305         const char *dbpath;
2306         sqlite3 *db;
2307
2308         if (mfx == NULL) {
2309                 _LOGE("invalid parameter");
2310                 return PM_PARSER_R_EINVAL;
2311         }
2312
2313         dbpath = __get_parser_db_path(uid);
2314
2315         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2316         if (ret != SQLITE_OK) {
2317                 _LOGE("open db failed: %d", ret);
2318                 return PM_PARSER_R_ERROR;
2319         }
2320
2321         __BEGIN_TRANSACTION(db);
2322         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
2323         __END_TRANSACTION(db);
2324
2325         sqlite3_close_v2(db);
2326
2327         return PM_PARSER_R_OK;
2328 }
2329
2330 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2331 {
2332         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
2333 }
2334
2335 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2336 {
2337         int ret;
2338         const char *dbpath;
2339         sqlite3 *db;
2340
2341         if (mfx == NULL) {
2342                 _LOGE("invalid parameter");
2343                 return PM_PARSER_R_EINVAL;
2344         }
2345
2346         dbpath = __get_parser_db_path(uid);
2347
2348         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2349         if (ret != SQLITE_OK) {
2350                 _LOGE("open db failed: %d", ret);
2351                 return PM_PARSER_R_ERROR;
2352         }
2353
2354         __BEGIN_TRANSACTION(db);
2355         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
2356         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
2357         __END_TRANSACTION(db);
2358
2359         sqlite3_close_v2(db);
2360
2361         return PM_PARSER_R_OK;
2362 }
2363
2364 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2365 {
2366         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
2367 }
2368
2369 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
2370                 uid_t uid, bool is_disable)
2371 {
2372         static const char query[] =
2373                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
2374                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
2375                 "VALUES (?, ?, ?,"
2376                 "  (SELECT app_splash_screen_display FROM package_app_info"
2377                 "   WHERE app_id=?))";
2378         int ret;
2379         sqlite3_stmt *stmt;
2380         int idx = 1;
2381
2382         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2383         if (ret != SQLITE_OK) {
2384                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2385                 return -1;
2386         }
2387
2388         __BIND_TEXT(db, stmt, idx++, appid);
2389         __BIND_INT(db, stmt, idx++, uid);
2390         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2391         __BIND_TEXT(db, stmt, idx++, appid);
2392
2393         ret = sqlite3_step(stmt);
2394         if (ret != SQLITE_DONE) {
2395                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2396                 sqlite3_finalize(stmt);
2397                 return -1;
2398         }
2399
2400         sqlite3_finalize(stmt);
2401
2402         return 0;
2403 }
2404
2405 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
2406                 const char *appid, uid_t uid, int is_disable)
2407 {
2408         int ret;
2409         const char *dbpath;
2410         sqlite3 *db;
2411
2412         if (appid == NULL) {
2413                 _LOGE("invalid parameter");
2414                 return PM_PARSER_R_EINVAL;
2415         }
2416
2417         dbpath = __get_parser_db_path(GLOBAL_USER);
2418
2419         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2420         if (ret != SQLITE_OK) {
2421                 _LOGE("open db failed: %d", ret);
2422                 return PM_PARSER_R_ERROR;
2423         }
2424
2425         __BEGIN_TRANSACTION(db);
2426         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
2427                                 uid, (bool)is_disable));
2428         __END_TRANSACTION(db);
2429
2430         sqlite3_close_v2(db);
2431
2432         return PM_PARSER_R_OK;
2433 }
2434
2435 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
2436                 bool is_disable)
2437 {
2438         static const char query[] =
2439                 "UPDATE package_app_info SET app_disable=? "
2440                 "WHERE app_id=?";
2441         int ret;
2442         sqlite3_stmt *stmt;
2443         int idx = 1;
2444
2445         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2446         if (ret != SQLITE_OK) {
2447                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2448                 return -1;
2449         }
2450
2451         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2452         __BIND_TEXT(db, stmt, idx++, appid);
2453
2454         ret = sqlite3_step(stmt);
2455         if (ret != SQLITE_DONE) {
2456                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2457                 sqlite3_finalize(stmt);
2458                 return -1;
2459         }
2460
2461         sqlite3_finalize(stmt);
2462
2463         return 0;
2464 }
2465
2466 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
2467                 uid_t uid, int is_disable)
2468 {
2469         int ret;
2470         const char *dbpath;
2471         sqlite3 *db;
2472
2473         if (appid == NULL) {
2474                 _LOGE("invalid parameter");
2475                 return PM_PARSER_R_EINVAL;
2476         }
2477
2478         dbpath = __get_parser_db_path(uid);
2479
2480         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2481         if (ret != SQLITE_OK) {
2482                 _LOGE("open db failed: %d", ret);
2483                 return PM_PARSER_R_ERROR;
2484         }
2485
2486         __BEGIN_TRANSACTION(db);
2487         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
2488                                 (bool)is_disable));
2489         __END_TRANSACTION(db);
2490
2491         sqlite3_close_v2(db);
2492
2493         return PM_PARSER_R_OK;
2494 }
2495
2496 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
2497                 int is_disable)
2498 {
2499         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
2500                         __getuid(), is_disable);
2501 }
2502
2503 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
2504                 bool is_disable)
2505 {
2506         static const char query[] =
2507                 "UPDATE package_info SET package_disable=? "
2508                 "WHERE package=?";
2509         int ret;
2510         sqlite3_stmt *stmt;
2511         int idx = 1;
2512
2513         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2514         if (ret != SQLITE_OK) {
2515                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2516                 return -1;
2517         }
2518
2519         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
2520         __BIND_TEXT(db, stmt, idx++, pkgid);
2521
2522         ret = sqlite3_step(stmt);
2523         if (ret != SQLITE_DONE) {
2524                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2525                 sqlite3_finalize(stmt);
2526                 return -1;
2527         }
2528
2529         sqlite3_finalize(stmt);
2530
2531         return 0;
2532 }
2533
2534 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2535                 uid_t uid, int is_disable)
2536 {
2537         int ret;
2538         const char *dbpath;
2539         sqlite3 *db;
2540
2541         if (pkgid == NULL) {
2542                 _LOGE("invalid parameter");
2543                 return PM_PARSER_R_EINVAL;
2544         }
2545
2546         dbpath = __get_parser_db_path(uid);
2547
2548         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2549         if (ret != SQLITE_OK) {
2550                 _LOGE("open db failed: %d", ret);
2551                 return PM_PARSER_R_ERROR;
2552         }
2553
2554         __BEGIN_TRANSACTION(db);
2555         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2556                                 (bool)is_disable));
2557         __END_TRANSACTION(db);
2558
2559         sqlite3_close_v2(db);
2560
2561         return PM_PARSER_R_OK;
2562 }
2563
2564 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2565                 int is_disable)
2566 {
2567         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2568                         __getuid(), is_disable);
2569 }
2570
2571 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2572                 const char *appid, uid_t uid, bool is_enabled)
2573 {
2574         static const char query[] =
2575                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2576                 "  appid, uid, is_splash_screen_enabled) "
2577                 "VALUES (?, ?, ?)";
2578         int ret;
2579         sqlite3_stmt *stmt;
2580         int idx = 1;
2581
2582         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2583         if (ret != SQLITE_OK) {
2584                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2585                 return -1;
2586         }
2587
2588         __BIND_TEXT(db, stmt, idx++, appid);
2589         __BIND_INT(db, stmt, idx++, uid);
2590         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2591
2592         ret = sqlite3_step(stmt);
2593         if (ret != SQLITE_DONE) {
2594                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2595                 sqlite3_finalize(stmt);
2596                 return -1;
2597         }
2598
2599         sqlite3_finalize(stmt);
2600
2601         return 0;
2602 }
2603
2604 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2605                 const char *appid, uid_t uid, int flag)
2606 {
2607         int ret;
2608         const char *dbpath;
2609         sqlite3 *db;
2610
2611         if (appid == NULL) {
2612                 _LOGE("invalid parameter");
2613                 return PM_PARSER_R_EINVAL;
2614         }
2615
2616         dbpath = __get_parser_db_path(GLOBAL_USER);
2617
2618         ret = __open_db(GLOBAL_USER, dbpath, &db, SQLITE_OPEN_READWRITE);
2619         if (ret != SQLITE_OK) {
2620                 _LOGE("open db failed: %d", ret);
2621                 return PM_PARSER_R_ERROR;
2622         }
2623
2624         __BEGIN_TRANSACTION(db);
2625         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2626                                 appid, uid, (bool)flag));
2627         __END_TRANSACTION(db);
2628
2629         sqlite3_close_v2(db);
2630
2631         return PM_PARSER_R_OK;
2632 }
2633
2634 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2635                 bool is_enabled)
2636 {
2637         static const char query[] =
2638                 "UPDATE package_app_info SET app_splash_screen_display=? "
2639                 "WHERE app_id=?";
2640         int ret;
2641         sqlite3_stmt *stmt;
2642         int idx = 1;
2643
2644         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2645         if (ret != SQLITE_OK) {
2646                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2647                 return -1;
2648         }
2649
2650         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2651         __BIND_TEXT(db, stmt, idx++, appid);
2652
2653         ret = sqlite3_step(stmt);
2654         if (ret != SQLITE_DONE) {
2655                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2656                 sqlite3_finalize(stmt);
2657                 return -1;
2658         }
2659
2660         sqlite3_finalize(stmt);
2661
2662         return 0;
2663 }
2664
2665 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2666                 const char *appid, uid_t uid, int flag)
2667 {
2668         int ret;
2669         const char *dbpath;
2670         sqlite3 *db;
2671
2672         if (appid == NULL) {
2673                 _LOGE("invalid parameter");
2674                 return PM_PARSER_R_EINVAL;
2675         }
2676
2677         dbpath = __get_parser_db_path(uid);
2678
2679         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2680         if (ret != SQLITE_OK) {
2681                 _LOGE("open db failed: %d", ret);
2682                 return PM_PARSER_R_ERROR;
2683         }
2684
2685         __BEGIN_TRANSACTION(db);
2686         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2687         __END_TRANSACTION(db);
2688
2689         sqlite3_close_v2(db);
2690
2691         return PM_PARSER_R_OK;
2692 }
2693
2694 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2695                 const char *appid, int flag)
2696 {
2697         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2698                         appid, __getuid(), flag);
2699 }
2700
2701 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2702 {
2703         static const char query[] =
2704                 "UPDATE package_app_localized_info SET app_label=? "
2705                 "WHERE app_id=? AND app_label IS NOT NULL";
2706         int ret;
2707         sqlite3_stmt *stmt;
2708         int idx = 1;
2709
2710         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2711         if (ret != SQLITE_OK) {
2712                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2713                 return -1;
2714         }
2715
2716         __BIND_TEXT(db, stmt, idx++, label);
2717         __BIND_TEXT(db, stmt, idx++, appid);
2718
2719         ret = sqlite3_step(stmt);
2720         if (ret != SQLITE_DONE) {
2721                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2722                 sqlite3_finalize(stmt);
2723                 return -1;
2724         }
2725
2726         sqlite3_finalize(stmt);
2727
2728         return 0;
2729 }
2730
2731 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2732                 uid_t uid, const char *label)
2733 {
2734         int ret;
2735         const char *dbpath;
2736         sqlite3 *db;
2737
2738         if (appid == NULL) {
2739                 _LOGE("invalid parameter");
2740                 return PM_PARSER_R_EINVAL;
2741         }
2742
2743         dbpath = __get_parser_db_path(uid);
2744
2745         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2746         if (ret != SQLITE_OK) {
2747                 _LOGE("open db failed: %d", ret);
2748                 return PM_PARSER_R_ERROR;
2749         }
2750
2751         __BEGIN_TRANSACTION(db);
2752         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2753         __END_TRANSACTION(db);
2754
2755         sqlite3_close_v2(db);
2756
2757         return PM_PARSER_R_OK;
2758 }
2759
2760 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2761                 const char *label)
2762 {
2763         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2764                         label);
2765 }
2766
2767 static int __set_app_icon(sqlite3 *db, const char *appid, const char *icon_path)
2768 {
2769         static const char query[] =
2770                 "UPDATE package_app_localized_info SET app_icon=? "
2771                 "WHERE app_id=? AND app_icon IS NOT NULL";
2772         int ret;
2773         sqlite3_stmt *stmt;
2774         int idx = 1;
2775
2776         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2777         if (ret != SQLITE_OK) {
2778                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2779                 return -1;
2780         }
2781
2782         __BIND_TEXT(db, stmt, idx++, icon_path);
2783         __BIND_TEXT(db, stmt, idx++, appid);
2784
2785         ret = sqlite3_step(stmt);
2786         if (ret != SQLITE_DONE) {
2787                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2788                 sqlite3_finalize(stmt);
2789                 return -1;
2790         }
2791
2792         sqlite3_finalize(stmt);
2793
2794         return 0;
2795 }
2796
2797 API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid,
2798                 uid_t uid, const char *icon_path)
2799 {
2800         int ret;
2801         const char *dbpath;
2802         sqlite3 *db;
2803
2804         if (appid == NULL) {
2805                 _LOGE("invalid parameter");
2806                 return PM_PARSER_R_EINVAL;
2807         }
2808
2809         dbpath = __get_parser_db_path(uid);
2810
2811         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2812         if (ret != SQLITE_OK) {
2813                 _LOGE("open db failed: %d", ret);
2814                 return PM_PARSER_R_ERROR;
2815         }
2816
2817         __BEGIN_TRANSACTION(db);
2818         __DO_TRANSACTION(db, __set_app_icon(db, appid, icon_path));
2819         __END_TRANSACTION(db);
2820
2821         sqlite3_close_v2(db);
2822
2823         return PM_PARSER_R_OK;
2824 }
2825
2826 API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid,
2827                 const char *icon_path)
2828 {
2829         return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(),
2830                         icon_path);
2831 }
2832
2833 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2834 {
2835         static const char query[] =
2836                 "UPDATE package_info SET package_tep_name=? "
2837                 "WHERE package=?";
2838         int ret;
2839         sqlite3_stmt *stmt;
2840         int idx = 1;
2841
2842         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2843         if (ret != SQLITE_OK) {
2844                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2845                 return -1;
2846         }
2847
2848         __BIND_TEXT(db, stmt, idx++, tep_path);
2849         __BIND_TEXT(db, stmt, idx++, pkgid);
2850
2851         ret = sqlite3_step(stmt);
2852         if (ret != SQLITE_DONE) {
2853                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2854                 sqlite3_finalize(stmt);
2855                 return -1;
2856         }
2857
2858         sqlite3_finalize(stmt);
2859
2860         return 0;
2861 }
2862
2863 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2864                 const char *tep_path, uid_t uid)
2865 {
2866         int ret;
2867         const char *dbpath;
2868         sqlite3 *db;
2869
2870         if (pkgid == NULL) {
2871                 _LOGE("invalid parameter");
2872                 return PM_PARSER_R_EINVAL;
2873         }
2874
2875         dbpath = __get_parser_db_path(uid);
2876
2877         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2878         if (ret != SQLITE_OK) {
2879                 _LOGE("open db failed: %d", ret);
2880                 return PM_PARSER_R_ERROR;
2881         }
2882
2883         __BEGIN_TRANSACTION(db);
2884         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2885         __END_TRANSACTION(db);
2886
2887         sqlite3_close_v2(db);
2888
2889         return PM_PARSER_R_OK;
2890 }
2891
2892 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2893                 const char *tep_path)
2894 {
2895         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2896                         __getuid());
2897 }
2898
2899 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2900                 const char **update_type)
2901 {
2902         if (type == PMINFO_UPDATEINFO_NONE)
2903                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2904         else if (type == PMINFO_UPDATEINFO_FORCE)
2905                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2906         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2907                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2908         else
2909                 return -1;
2910         return 0;
2911 }
2912
2913 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2914                 const char *update_type)
2915 {
2916         static const char query[] =
2917                 "UPDATE package_update_info "
2918                 "SET update_version=?, update_type=? "
2919                 "WHERE package=?";
2920         int ret;
2921         sqlite3_stmt *stmt;
2922         int idx = 1;
2923
2924         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2925         if (ret != SQLITE_OK) {
2926                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2927                 return -1;
2928         }
2929
2930         __BIND_TEXT(db, stmt, idx++, info->version);
2931         __BIND_TEXT(db, stmt, idx++, update_type);
2932         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2933
2934         ret = sqlite3_step(stmt);
2935         if (ret != SQLITE_DONE) {
2936                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2937                 sqlite3_finalize(stmt);
2938                 return -1;
2939         }
2940
2941         sqlite3_finalize(stmt);
2942
2943         return 0;
2944 }
2945
2946 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2947                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2948 {
2949         int ret;
2950         updateinfo_x *update_info;
2951         updateinfo_x *prev_update_info;
2952         pkgmgrinfo_updateinfo_h prev_update_handle;
2953         pkgmgrinfo_pkginfo_h pkginfo;
2954         pkgmgrinfo_version_compare_type compare_result;
2955         bool is_global_pkg;
2956         const char *update_type;
2957         const char *dbpath;
2958         sqlite3 *db;
2959
2960         if (handle == NULL) {
2961                 _LOGE("invalid parameter");
2962                 return PM_PARSER_R_EINVAL;
2963         }
2964
2965         update_info = (updateinfo_x *)handle;
2966         if (update_info->pkgid == NULL || update_info->version == NULL)
2967                 return PM_PARSER_R_EINVAL;
2968         if (__convert_update_type(update_info->type, &update_type) != 0)
2969                 return PM_PARSER_R_EINVAL;
2970
2971         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2972                         &prev_update_handle, uid);
2973         if (ret != PMINFO_R_OK)
2974                 return PM_PARSER_R_ERROR;
2975
2976         prev_update_info = (updateinfo_x *)prev_update_handle;
2977         ret = pkgmgrinfo_compare_package_version(update_info->version,
2978                         prev_update_info->version, &compare_result);
2979         if (ret != PMINFO_R_OK) {
2980                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2981                 return PM_PARSER_R_ERROR;
2982         }
2983
2984         if (compare_result == PMINFO_VERSION_SAME &&
2985                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2986                 _LOGI("Given update info version[%s] of pkgid[%s] "
2987                                 "will be ignored",
2988                                 update_info->version, update_info->pkgid);
2989                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2990                 return PM_PARSER_R_OK;
2991         }
2992         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2993
2994         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2995                         &pkginfo);
2996         if (ret != PMINFO_R_OK)
2997                 return PM_PARSER_R_ERROR;
2998
2999         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
3000         if (ret != PMINFO_R_OK) {
3001                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
3002                 return PM_PARSER_R_ERROR;
3003         }
3004         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
3005
3006         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
3007
3008         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3009         if (ret != SQLITE_OK) {
3010                 _LOGE("open db failed: %d", ret);
3011                 return PM_PARSER_R_ERROR;
3012         }
3013
3014         __BEGIN_TRANSACTION(db);
3015         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
3016                                 update_type));
3017         __END_TRANSACTION(db);
3018
3019         sqlite3_close_v2(db);
3020
3021         return PM_PARSER_R_OK;
3022 }
3023
3024 API int pkgmgr_parser_register_pkg_update_info_in_db(
3025                 pkgmgrinfo_updateinfo_h handle)
3026 {
3027         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
3028                         __getuid());
3029 }
3030
3031 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
3032 {
3033         static const char query[] =
3034                 "UPDATE package_update_info SET update_type='none' "
3035                 "WHERE package=?";
3036         int ret;
3037         sqlite3_stmt *stmt;
3038         int idx = 1;
3039
3040         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
3041         if (ret != SQLITE_OK) {
3042                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
3043                 return -1;
3044         }
3045
3046         __BIND_TEXT(db, stmt, idx++, pkgid);
3047
3048         ret = sqlite3_step(stmt);
3049         if (ret != SQLITE_DONE) {
3050                 _LOGE("step failed: %s", sqlite3_errmsg(db));
3051                 sqlite3_finalize(stmt);
3052                 return -1;
3053         }
3054
3055         sqlite3_finalize(stmt);
3056
3057         return 0;
3058 }
3059
3060 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
3061                 uid_t uid)
3062 {
3063         int ret;
3064         const char *dbpath;
3065         sqlite3 *db;
3066         pkgmgrinfo_pkginfo_h pkginfo;
3067         bool is_global_pkg;
3068
3069         if (pkgid == NULL) {
3070                 _LOGE("invalid parameter");
3071                 return PM_PARSER_R_EINVAL;
3072         }
3073
3074         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
3075         if (ret != PMINFO_R_OK)
3076                 return PM_PARSER_R_EINVAL;
3077
3078         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
3079         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
3080         if (ret != PMINFO_R_OK)
3081                 return PM_PARSER_R_ERROR;
3082
3083         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
3084
3085         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3086         if (ret != SQLITE_OK) {
3087                 _LOGE("open db failed: %d", ret);
3088                 return PM_PARSER_R_ERROR;
3089         }
3090
3091         __BEGIN_TRANSACTION(db);
3092         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
3093         __END_TRANSACTION(db);
3094
3095         sqlite3_close_v2(db);
3096
3097         return PM_PARSER_R_OK;
3098 }
3099
3100 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
3101 {
3102         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
3103                         __getuid());
3104 }
3105
3106 static int __unregister_all_pkg_update_info(sqlite3 *db)
3107 {
3108         static const char query[] =
3109                 "UPDATE package_update_info SET update_type='none'";
3110         int ret;
3111         sqlite3_stmt *stmt;
3112
3113         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
3114         if (ret != SQLITE_OK) {
3115                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
3116                 return -1;
3117         }
3118
3119         ret = sqlite3_step(stmt);
3120         if (ret != SQLITE_DONE) {
3121                 _LOGE("step failed: %s", sqlite3_errmsg(db));
3122                 sqlite3_finalize(stmt);
3123                 return -1;
3124         }
3125
3126         sqlite3_finalize(stmt);
3127
3128         return 0;
3129 }
3130
3131 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
3132 {
3133         int ret;
3134         const char *dbpath;
3135         sqlite3 *db;
3136
3137         dbpath = __get_parser_db_path(uid);
3138
3139         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3140         if (ret != SQLITE_OK) {
3141                 _LOGE("open db failed: %d", ret);
3142                 return PM_PARSER_R_ERROR;
3143         }
3144
3145         __BEGIN_TRANSACTION(db);
3146         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
3147         __END_TRANSACTION(db);
3148
3149         sqlite3_close_v2(db);
3150
3151         return PM_PARSER_R_OK;
3152 }
3153
3154 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
3155 {
3156         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
3157                         __getuid());
3158 }
3159
3160 API int pkgmgr_parser_register_pkg_plugin_info_in_usr_db(
3161                 manifest_x *mfx, uid_t uid)
3162 {
3163         int ret;
3164         const char *dbpath;
3165         sqlite3 *db;
3166
3167         if (!mfx)
3168                 return PM_PARSER_R_EINVAL;
3169         dbpath = __get_parser_db_path(uid);
3170         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3171         if (ret != SQLITE_OK) {
3172                 _LOGE("open db failed: %d", ret);
3173                 return PM_PARSER_R_ERROR;
3174         }
3175
3176         __BEGIN_TRANSACTION(db);
3177         __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx));
3178         __END_TRANSACTION(db);
3179
3180         sqlite3_close_v2(db);
3181
3182         return PM_PARSER_R_OK;
3183 }
3184
3185 API int pkgmgr_parser_register_pkg_plugin_info_in_db(manifest_x *mfx)
3186 {
3187         return pkgmgr_parser_register_pkg_plugin_info_in_usr_db(mfx, __getuid());
3188 }
3189
3190 API int pkgmgr_parser_update_pkg_plugin_info_in_usr_db(
3191                 manifest_x *mfx, uid_t uid)
3192 {
3193         int ret;
3194         const char *dbpath;
3195         sqlite3 *db;
3196
3197         if (!mfx)
3198                 return PM_PARSER_R_EINVAL;
3199         dbpath = __get_parser_db_path(uid);
3200         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3201         if (ret != SQLITE_OK) {
3202                 _LOGE("open db failed: %d", ret);
3203                 return PM_PARSER_R_ERROR;
3204         }
3205
3206         __BEGIN_TRANSACTION(db);
3207         __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, mfx->package));
3208         __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx));
3209         __END_TRANSACTION(db);
3210
3211         sqlite3_close_v2(db);
3212
3213         return PM_PARSER_R_OK;
3214 }
3215
3216 API int pkgmgr_parser_update_pkg_plugin_info_in_db(manifest_x *mfx)
3217 {
3218         return pkgmgr_parser_update_pkg_plugin_info_in_usr_db(mfx, __getuid());
3219 }
3220
3221 API int pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(
3222                 const char *pkgid, uid_t uid)
3223 {
3224         int ret;
3225         const char *dbpath;
3226         sqlite3 *db;
3227
3228         dbpath = __get_parser_db_path(uid);
3229         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
3230         if (ret != SQLITE_OK) {
3231                 _LOGE("open db failed: %d", ret);
3232                 return PM_PARSER_R_ERROR;
3233         }
3234
3235         __BEGIN_TRANSACTION(db);
3236         __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, pkgid));
3237         __END_TRANSACTION(db);
3238
3239         sqlite3_close_v2(db);
3240
3241         return PM_PARSER_R_OK;
3242 }
3243
3244 API int pkgmgr_parser_unregister_pkg_plugin_info_in_db(const char *pkgid)
3245 {
3246         return pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid, __getuid());
3247 }