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