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