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