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