Rework db insertion code
[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 <sqlite3.h>
31
32 #include <tzplatform_config.h>
33 #include <system_info.h>
34
35 #include "pkgmgr-info.h"
36 #include "pkgmgrinfo_basic.h"
37 #include "pkgmgr_parser.h"
38 #include "pkgmgr_parser_db_queries.h"
39 #include "pkgmgr_parser_debug.h"
40 #include "pkgmgr_parser_internal.h"
41
42 #ifndef OWNER_ROOT
43 #define OWNER_ROOT 0
44 #endif
45 #ifndef GLOBAL_USER
46 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
47 #endif
48 #ifndef APPFW_USER
49 #define APPFW_USER "app_fw"
50 #endif
51
52 #define BUFSIZE 4096
53
54 #define LDPI "ldpi"
55 #define MDPI "mdpi"
56 #define HDPI "hdpi"
57 #define XHDPI "xhdpi"
58 #define XXHDPI "xxhdpi"
59
60 #define LDPI_MIN 0
61 #define LDPI_MAX 240
62 #define MDPI_MIN 241
63 #define MDPI_MAX 300
64 #define HDPI_MIN 301
65 #define HDPI_MAX 380
66 #define XHDPI_MIN 381
67 #define XHDPI_MAX 480
68 #define XXHDPI_MIN 481
69 #define XXHDPI_MAX 600
70
71 /* app background category value */
72 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
73 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL  0x00001
74 #define APP_BG_CATEGORY_MEDIA_VAL              0x00002
75 #define APP_BG_CATEGORY_DOWNLOAD_VAL           0x00004
76 #define APP_BG_CATEGORY_BGNETWORK_VAL          0x00008
77 #define APP_BG_CATEGORY_LOCATION_VAL           0x00010
78 #define APP_BG_CATEGORY_SENSOR_VAL             0x00020
79 #define APP_BG_CATEGORY_IOTCOMM_VAL            0x00040
80 #define APP_BG_CATEGORY_SYSTEM_VAL             0x00080
81
82 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
83 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR  "disable"
84 #define APP_BG_CATEGORY_MEDIA_STR              "media"
85 #define APP_BG_CATEGORY_DOWNLOAD_STR           "download"
86 #define APP_BG_CATEGORY_BGNETWORK_STR          "background-network"
87 #define APP_BG_CATEGORY_LOCATION_STR           "location"
88 #define APP_BG_CATEGORY_SENSOR_STR             "sensor"
89 #define APP_BG_CATEGORY_IOTCOMM_STR            "iot-communication"
90 #define APP_BG_CATEGORY_SYSTEM                 "system"
91
92 #define REGULAR_USER 5000
93 static inline uid_t __getuid(void)
94 {
95         uid_t uid = getuid();
96
97         if (uid < REGULAR_USER)
98                 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
99         else
100                 return uid;
101 }
102
103 static const char *__get_bool(char *value, bool is_true)
104 {
105         if (value != NULL) {
106                 if (!strcmp(value, ""))
107                         return (is_true) ? "true" : "false";
108                 return value;
109         }
110
111         return (is_true) ? "true" : "false";
112 }
113
114 #define __BEGIN_TRANSACTION(db)                                                \
115 do {                                                                           \
116         if (sqlite3_exec(db, "BEGIN EXCLUSIVE", NULL, NULL, NULL) !=           \
117                         SQLITE_OK) {                                           \
118                 _LOGE("begin transaction failed: %s", sqlite3_errmsg(db));     \
119                 sqlite3_close_v2(db);                                          \
120                 return PM_PARSER_R_ERROR;                                      \
121         }                                                                      \
122 } while (0)                                                                    \
123
124 #define __DO_TRANSACTION(db, func)                                             \
125 do {                                                                           \
126         if (func) {                                                            \
127                 _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \
128                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
129                                 SQLITE_OK)                                     \
130                         _LOGE("roll back transaction failed: %s",              \
131                                         sqlite3_errmsg(db));                   \
132                 sqlite3_close_v2(db);                                          \
133                 return PM_PARSER_R_ERROR;                                      \
134         }                                                                      \
135 } while (0)                                                                    \
136
137 #define __END_TRANSACTION(db)                                                  \
138 do {                                                                           \
139         if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) !=                    \
140                         SQLITE_OK) {                                           \
141                 _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db));      \
142                 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) !=          \
143                                 SQLITE_OK)                                     \
144                         _LOGE("roll back transaction failed: %s",              \
145                                         sqlite3_errmsg(db));                   \
146                 sqlite3_close_v2(db);                                          \
147                 return PM_PARSER_R_ERROR;                                      \
148         }                                                                      \
149 } while (0)                                                                    \
150
151 #define __BIND_TEXT(db, stmt, i, text)                                         \
152 do {                                                                           \
153         if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\
154                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
155                 sqlite3_finalize(stmt);                                        \
156                 return -1;                                                     \
157         }                                                                      \
158 } while (0)
159
160 #define __BIND_INT(db, stmt, i, int)                                           \
161 do {                                                                           \
162         if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) {                     \
163                 _LOGE("bind error(index %d): %s", i, sqlite3_errmsg(db));      \
164                 sqlite3_finalize(stmt);                                        \
165                 return -1;                                                     \
166         }                                                                      \
167 } while (0)
168
169 static const char *__get_parser_db_path(uid_t uid)
170 {
171         char buf[PATH_MAX];
172         const char *path;
173
174         if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
175                 path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
176         } else {
177                 snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid);
178                 path = tzplatform_mkpath(TZ_SYS_DB, buf);
179         }
180
181         return path;
182 }
183
184 static const char *__get_cert_db_path(void)
185 {
186         return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
187 }
188
189 static int __set_db_version(sqlite3 *db)
190 {
191         static const char query_raw[] = "PRAGMA user_version=";
192         char query[BUFSIZE];
193         int ret;
194         int version;
195
196         version = atoi(TIZEN_MAJOR_VER) * 10000 + atoi(TIZEN_MINOR_VER) * 100 +
197                 atoi(TIZEN_PATCH_VER);
198         snprintf(query, sizeof(query), "%s%d", query_raw, version);
199
200         ret = sqlite3_exec(db, query, NULL, NULL, NULL);
201         if (ret != SQLITE_OK) {
202                 _LOGE("exec failed: %s", sqlite3_errmsg(db));
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 /* TODO: Do not labeling directly */
210 #define DB_LABEL "User::Home"
211 #define SET_SMACK_LABEL(x)                                                     \
212 do {                                                                           \
213         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS))                 \
214                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x);                 \
215         else                                                                   \
216                 _LOGD("chsmack -a %s %s", DB_LABEL, x);                        \
217 } while (0)
218
219 static int __set_db_permission(const char *path, uid_t uid)
220 {
221         int fd;
222         const char *files[2];
223         char journal_file[BUFSIZE];
224         struct stat sb;
225         mode_t mode;
226         struct passwd pwd;
227         struct passwd *result;
228         char buf[BUFSIZE];
229         int ret;
230         int i;
231
232         if (getuid() != OWNER_ROOT)
233                 return 0;
234
235         if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
236                 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
237                 if (result == NULL) {
238                         if (ret == 0)
239                                 _LOGE("no such user: %d", uid);
240                         else
241                                 _LOGE("getpwuid_r failed: %d", errno);
242                         return -1;
243                 }
244                 uid = pwd.pw_uid;
245         }
246
247         snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
248         files[0] = path;
249         files[1] = journal_file;
250
251         ret = getpwuid_r(uid, &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
260         for (i = 0; i < 2; i++) {
261                 fd = open(files[i], O_RDONLY);
262                 if (fd == -1) {
263                         _LOGE("open %s failed: %d", files[i], errno);
264                         return -1;
265                 }
266                 ret = fstat(fd, &sb);
267                 if (ret == -1) {
268                         _LOGE("stat %s failed: %d", files[i], errno);
269                         close(fd);
270                         return -1;
271                 }
272                 if (S_ISLNK(sb.st_mode)) {
273                         _LOGE("%s is symlink!", files[i]);
274                         close(fd);
275                         return -1;
276                 }
277                 ret = fchown(fd, uid, pwd.pw_gid);
278                 if (ret == -1) {
279                         _LOGE("fchown %s failed: %d", files[i], errno);
280                         close(fd);
281                         return -1;
282                 }
283
284                 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
285                 if (!strcmp(path, __get_cert_db_path()))
286                         mode |= S_IWOTH;
287                 ret = fchmod(fd, mode);
288                 if (ret == -1) {
289                         _LOGD("fchmod %s failed: %d", files[i], errno);
290                         close(fd);
291                         return -1;
292                 }
293                 close(fd);
294                 SET_SMACK_LABEL(files[i]);
295         }
296
297         return 0;
298 }
299
300 static const char *parser_init_queries[] = {
301         QUERY_CREATE_TABLE_PACKAGE_INFO,
302         QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO,
303         QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO,
304         QUERY_CREATE_TABLE_PACKAGE_APP_INFO,
305         QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO,
306         QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO, /* ? */
307         QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO, /* ? */
308         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL,
309         QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY,
310         QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA,
311         QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION, /* ? */
312         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED, /* ? */
313         QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST, /* ? */
314         QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL,
315         QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID,
316         QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID,
317         QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN,
318         NULL,
319 };
320
321 static const char *cert_init_queries[] = {
322         QUERY_CREATE_TABLE_PACKAGE_CERT_INFO,
323         QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO,
324         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO,
325         QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO2,
326         QUERY_CREATE_TRIGGER_DELETE_CERT_INFO,
327         QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO,
328         NULL
329 };
330
331 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
332 {
333         int ret;
334         const char **queries;
335         int i;
336
337         if (__set_db_version(db))
338                 return -1;
339
340         if (strstr(dbpath, ".pkgmgr_parser.db")) {
341                 queries = parser_init_queries;
342         } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
343                 queries = cert_init_queries;
344         } else {
345                 _LOGE("unexpected dbpath: %s", dbpath);
346                 return -1;
347         }
348
349         for (i = 0; queries[i] != NULL; i++) {
350                 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
351                 if (ret != SQLITE_OK) {
352                         _LOGE("exec failed: %s", sqlite3_errmsg(db));
353                         return -1;
354                 }
355         }
356
357         if (__set_db_permission(dbpath, uid))
358                 _LOGE("failed to set db permission");
359
360         return 0;
361 }
362
363 API int pkgmgr_parser_initialize_parser_db(uid_t uid)
364 {
365         int ret;
366         const char *dbpath;
367         sqlite3 *db;
368
369         dbpath = __get_parser_db_path(uid);
370         if (access(dbpath, F_OK) != -1) {
371                 _LOGE("Manifest db for user %d is already exists", uid);
372                 return PM_PARSER_R_ERROR;
373         }
374
375         ret = sqlite3_open_v2(dbpath, &db,
376                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
377         if (ret != SQLITE_OK) {
378                 _LOGE("open db failed: %d", ret);
379                 return PM_PARSER_R_ERROR;
380         }
381
382         if (__initialize_db(db, dbpath, uid)) {
383                 sqlite3_close_v2(db);
384                 return PM_PARSER_R_ERROR;
385         }
386         sqlite3_close_v2(db);
387
388         return PM_PARSER_R_OK;
389 }
390
391 API int pkgmgr_parser_initialize_cert_db(void)
392 {
393         int ret;
394         const char *dbpath;
395         sqlite3 *db;
396
397         dbpath = __get_cert_db_path();
398         if (access(dbpath, F_OK) != -1) {
399                 _LOGE("Cert db is already exists");
400                 return PM_PARSER_R_ERROR;
401         }
402
403         ret = sqlite3_open_v2(dbpath, &db,
404                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
405         if (ret != SQLITE_OK) {
406                 _LOGE("open db failed: %d", ret);
407                 return PM_PARSER_R_ERROR;
408         }
409
410         if (__initialize_db(db, dbpath, GLOBAL_USER)) {
411                 sqlite3_close_v2(db);
412                 return PM_PARSER_R_ERROR;
413         }
414         sqlite3_close_v2(db);
415
416         return PM_PARSER_R_OK;
417 }
418
419 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
420 {
421         int ret;
422         struct passwd pwd;
423         struct passwd *result;
424         char buf[BUFSIZE];
425
426         ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
427         if (result == NULL) {
428                 if (ret == 0)
429                         _LOGE("no such user: %s", APPFW_USER);
430                 else
431                         _LOGE("getpwnam_r failed: %d", errno);
432                 return PM_PARSER_R_ERROR;
433         }
434
435         if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
436                 _LOGE("Only root or app_fw user is allowed");
437                 return PM_PARSER_R_EINVAL;
438         }
439
440         if (pkgmgr_parser_initialize_parser_db(uid))
441                 return PM_PARSER_R_ERROR;
442
443         if (pkgmgr_parser_initialize_cert_db())
444                 return PM_PARSER_R_ERROR;
445
446         return PM_PARSER_R_OK;
447 }
448
449 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
450 #define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
451 static int __db_busy_handler(void *data, int count)
452 {
453         if (count < BUSY_WAITING_MAX) {
454                 usleep(BUSY_WAITING_USEC);
455                 return 1;
456         } else {
457                 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
458                 return 0;
459         }
460 }
461
462 static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
463 {
464         int ret;
465
466         /* FIXME: always open with OPEN_CREATE flag for keeping previous
467          * implementation
468          */
469         if (flags & SQLITE_OPEN_READWRITE)
470                 flags = flags | SQLITE_OPEN_CREATE;
471
472         ret = sqlite3_open_v2(path, db, flags, NULL);
473         if (ret != SQLITE_OK)
474                 return ret;
475
476         if (flags & SQLITE_OPEN_CREATE) {
477                 ret = __initialize_db(*db, path, uid);
478                 if (ret) {
479                         _LOGE("failed to initialize db: %s\n");
480                         sqlite3_close_v2(*db);
481                         return -1;
482                 }
483         }
484
485         ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
486         if (ret != SQLITE_OK) {
487                 _LOGE("failed to enable foreign key support: %s",
488                                 sqlite3_errmsg(*db));
489                 sqlite3_close_v2(*db);
490                 return ret;
491         }
492
493         ret = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
494         if (ret != SQLITE_OK) {
495                 _LOGE("failed to register busy handler: %s",
496                                 sqlite3_errmsg(*db));
497                 sqlite3_close_v2(*db);
498                 return ret;
499         }
500
501         return ret;
502 }
503
504
505 static int __convert_background_category(GList *category_list)
506 {
507         int ret = 0;
508         GList *tmp;
509         char *category_data;
510
511         if (category_list == NULL)
512                 return 0;
513
514         for (tmp = category_list; tmp; tmp = tmp->next) {
515                 category_data = (char *)tmp->data;
516                 if (category_data == NULL)
517                         continue;
518                 if (!strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR))
519                         ret |= APP_BG_CATEGORY_MEDIA_VAL;
520                 else if (!strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR))
521                         ret |= APP_BG_CATEGORY_DOWNLOAD_VAL;
522                 else if (!strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR))
523                         ret |= APP_BG_CATEGORY_BGNETWORK_VAL;
524                 else if (!strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR))
525                         ret |= APP_BG_CATEGORY_LOCATION_VAL;
526                 else if (!strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR))
527                         ret |= APP_BG_CATEGORY_SENSOR_VAL;
528                 else if (!strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR))
529                         ret |= APP_BG_CATEGORY_IOTCOMM_VAL;
530                 else if (!strcmp(category_data, APP_BG_CATEGORY_SYSTEM))
531                         ret |= APP_BG_CATEGORY_SYSTEM_VAL;
532                 else
533                         _LOGE("Unidentified category [%s]", category_data);
534         }
535
536         return ret;
537 }
538
539 #define EFFECTIVE_APPID_KEY "http://tizen.org/metadata/effective-appid"
540 static const char *__find_effective_appid(GList *metadata_list)
541 {
542         GList *tmp;
543         metadata_x *md;
544
545         for (tmp = metadata_list; tmp; tmp = tmp->next) {
546                 md = (metadata_x *)tmp->data;
547                 if (md == NULL || md->key == NULL)
548                         continue;
549
550                 if (strcmp(md->key, EFFECTIVE_APPID_KEY) == 0) {
551                         if (md->value)
552                                 return md->value;
553                 }
554         }
555
556         return NULL;
557 }
558
559 static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
560 {
561         static const char query[] =
562                 "INSERT INTO package_app_app_control (app_id, app_control) "
563                 "VALUES (?, ?)";
564         int ret;
565         sqlite3_stmt *stmt;
566         int idx;
567         char app_control[BUFSIZE];
568         GList *tmp;
569         appcontrol_x *ac;
570
571         if (app->appcontrol == NULL)
572                 return 0;
573
574         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
575         if (ret != SQLITE_OK) {
576                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
577                 return -1;
578         }
579
580         for (tmp = app->appcontrol; tmp; tmp = tmp->next) {
581                 ac = (appcontrol_x *)tmp->data;
582                 if (ac == NULL)
583                         continue;
584                 idx = 1;
585                 snprintf(app_control, sizeof(app_control), "%s|%s|%s",
586                                 ac->operation ? (strlen(ac->operation) > 0 ?
587                                         ac->operation : "NULL") : "NULL",
588                                 ac->uri ? (strlen(ac->uri) > 0 ?
589                                         ac->uri : "NULL") : "NULL",
590                                 ac->mime ? (strlen(ac->mime) > 0 ?
591                                         ac->mime : "NULL") : "NULL");
592                 __BIND_TEXT(db, stmt, idx++, app->appid);
593                 __BIND_TEXT(db, stmt, idx++, app_control);
594
595                 ret = sqlite3_step(stmt);
596                 if (ret != SQLITE_DONE) {
597                         _LOGE("step failed: %s", sqlite3_errmsg(db));
598                         sqlite3_finalize(stmt);
599                         return -1;
600                 }
601
602                 sqlite3_reset(stmt);
603         }
604
605         sqlite3_finalize(stmt);
606
607         return 0;
608 }
609
610 static int __insert_category_info(sqlite3 *db, application_x *app)
611 {
612         static const char query[] =
613                 "INSERT INTO package_app_app_category (app_id, category) "
614                 "VALUES (?, ?)";
615         int ret;
616         sqlite3_stmt *stmt;
617         int idx;
618         GList *tmp;
619         const char *category;
620
621         if (app->category == NULL)
622                 return 0;
623
624         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
625         if (ret != SQLITE_OK) {
626                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
627                 return -1;
628         }
629
630         for (tmp = app->category; tmp; tmp = tmp->next) {
631                 category = (const char *)tmp->data;
632                 if (category == NULL)
633                         continue;
634                 idx = 1;
635                 __BIND_TEXT(db, stmt, idx++, app->appid);
636                 __BIND_TEXT(db, stmt, idx++, category);
637
638                 ret = sqlite3_step(stmt);
639                 if (ret != SQLITE_DONE) {
640                         _LOGE("step failed: %s", sqlite3_errmsg(db));
641                         sqlite3_finalize(stmt);
642                         return -1;
643                 }
644
645                 sqlite3_reset(stmt);
646         }
647
648         sqlite3_finalize(stmt);
649
650         return 0;
651 }
652
653 static int __insert_metadata_info(sqlite3 *db, application_x *app)
654 {
655         static const char query[] =
656                 "INSERT INTO package_app_app_metadata (app_id,"
657                 "  md_key, md_value) VALUES (?, ?, ?)";
658         int ret;
659         sqlite3_stmt *stmt;
660         int idx;
661         GList *tmp;
662         metadata_x *md;
663
664         if (app->metadata == NULL)
665                 return 0;
666
667         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
668         if (ret != SQLITE_OK) {
669                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
670                 return -1;
671         }
672
673         for (tmp = app->metadata; tmp; tmp = tmp->next) {
674                 md = (metadata_x *)tmp->data;
675                 if (md == NULL)
676                         continue;
677                 idx = 1;
678                 __BIND_TEXT(db, stmt, idx++, app->appid);
679                 __BIND_TEXT(db, stmt, idx++, md->key);
680                 __BIND_TEXT(db, stmt, idx++, md->value);
681
682                 ret = sqlite3_step(stmt);
683                 if (ret != SQLITE_DONE) {
684                         _LOGE("step failed: %s", sqlite3_errmsg(db));
685                         sqlite3_finalize(stmt);
686                         return -1;
687                 }
688
689                 sqlite3_reset(stmt);
690         }
691
692         sqlite3_finalize(stmt);
693
694         return 0;
695 }
696
697 static int __insert_datacontrol_info(sqlite3 *db, application_x *app)
698 {
699         static const char query[] =
700                 "INSERT INTO package_app_data_control (app_id, providerid,"
701                 "  access, type, trusted) VALUES (?, ?, ?, ?, ?)";
702         int ret;
703         sqlite3_stmt *stmt;
704         int idx;
705         GList *tmp;
706         datacontrol_x *dc;
707
708         if (app->datacontrol == NULL)
709                 return 0;
710
711         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
712         if (ret != SQLITE_OK) {
713                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
714                 return -1;
715         }
716
717         for (tmp = app->datacontrol; tmp; tmp = tmp->next) {
718                 dc = (datacontrol_x *)tmp->data;
719                 if (dc == NULL)
720                         continue;
721                 idx = 1;
722                 __BIND_TEXT(db, stmt, idx++, app->appid);
723                 __BIND_TEXT(db, stmt, idx++, dc->providerid);
724                 __BIND_TEXT(db, stmt, idx++, dc->access);
725                 __BIND_TEXT(db, stmt, idx++, dc->type);
726                 __BIND_TEXT(db, stmt, idx++, dc->trusted);
727
728                 ret = sqlite3_step(stmt);
729                 if (ret != SQLITE_DONE) {
730                         _LOGE("step failed: %s", sqlite3_errmsg(db));
731                         sqlite3_finalize(stmt);
732                         return -1;
733                 }
734
735                 sqlite3_reset(stmt);
736         }
737
738         sqlite3_finalize(stmt);
739
740         return 0;
741 }
742
743 /* TODO: move to installer */
744 static int __check_dpi(const char *dpi_char, int dpi_int)
745 {
746         if (dpi_char == NULL)
747                 return -1;
748
749         if (strcasecmp(dpi_char, LDPI) == 0) {
750                 if (dpi_int >= LDPI_MIN && dpi_int <= LDPI_MAX)
751                         return 0;
752                 else
753                         return -1;
754         } else if (strcasecmp(dpi_char, MDPI) == 0) {
755                 if (dpi_int >= MDPI_MIN && dpi_int <= MDPI_MAX)
756                         return 0;
757                 else
758                         return -1;
759         } else if (strcasecmp(dpi_char, HDPI) == 0) {
760                 if (dpi_int >= HDPI_MIN && dpi_int <= HDPI_MAX)
761                         return 0;
762                 else
763                         return -1;
764         } else if (strcasecmp(dpi_char, XHDPI) == 0) {
765                 if (dpi_int >= XHDPI_MIN && dpi_int <= XHDPI_MAX)
766                         return 0;
767                 else
768                         return -1;
769         } else if (strcasecmp(dpi_char, XXHDPI) == 0) {
770                 if (dpi_int >= XXHDPI_MIN && dpi_int <= XXHDPI_MAX)
771                         return 0;
772                 else
773                         return -1;
774         } else
775                 return -1;
776 }
777
778 static gint __compare_splashscreen_with_orientation_dpi(gconstpointer a,
779                 gconstpointer b)
780 {
781         splashscreen_x *ss = (splashscreen_x *)a;
782         const char *orientation = (const char *)b;
783         int dpi = -1;
784         int ret;
785
786         if (ss->operation || ss->dpi == NULL)
787                 return -1;
788
789         ret = system_info_get_platform_int(
790                         "http://tizen.org/feature/screen.dpi", &dpi);
791         if (ret != SYSTEM_INFO_ERROR_NONE)
792                 return -1;
793
794         if (strcasecmp(ss->orientation, orientation) == 0 &&
795                         __check_dpi(ss->dpi, dpi) == 0)
796                 return 0;
797
798         return -1;
799 }
800
801 static gint __compare_splashscreen_with_orientation(gconstpointer a,
802                 gconstpointer b)
803 {
804         splashscreen_x *ss = (splashscreen_x *)a;
805         const char *orientation = (const char *)b;
806
807         if (ss->operation || ss->dpi)
808                 return -1;
809
810         if (strcasecmp(ss->orientation, orientation) == 0)
811                 return 0;
812
813         return -1;
814 }
815
816 static splashscreen_x *__find_default_splashscreen(GList *splashscreens,
817                 const char *orientation)
818 {
819         GList *tmp;
820
821         tmp = g_list_find_custom(splashscreens, orientation,
822                         (GCompareFunc)
823                         __compare_splashscreen_with_orientation_dpi);
824         if (tmp)
825                 return (splashscreen_x *)tmp->data;
826
827         tmp = g_list_find_custom(splashscreens, orientation,
828                         (GCompareFunc)__compare_splashscreen_with_orientation);
829         if (tmp)
830                 return (splashscreen_x *)tmp->data;
831
832         return NULL;
833 }
834
835 static void __find_appcontrol_splashscreen_with_dpi(gpointer data,
836                 gpointer user_data)
837 {
838         splashscreen_x *ss = (splashscreen_x *)data;
839         GList **list = (GList **)user_data;
840         int dpi = -1;
841         int ret;
842
843         if (ss->operation == NULL || ss->dpi == NULL)
844                 return;
845
846         ret = system_info_get_platform_int(
847                         "http://tizen.org/feature/screen.dpi", &dpi);
848         if (ret != SYSTEM_INFO_ERROR_NONE)
849                 return;
850
851         if (__check_dpi(ss->dpi, dpi) != 0)
852                 return;
853
854         *list = g_list_append(*list, ss);
855 }
856
857 static void __find_appcontrol_splashscreen(gpointer data, gpointer user_data)
858 {
859         splashscreen_x *ss = (splashscreen_x *)data;
860         GList **list = (GList **)user_data;
861         splashscreen_x *ss_tmp;
862         GList *tmp;
863
864         if (ss->operation == NULL || ss->dpi)
865                 return;
866
867         for (tmp = *list; tmp; tmp = tmp->next) {
868                 ss_tmp = (splashscreen_x *)tmp->data;
869                 if (ss_tmp->operation
870                         && strcmp(ss_tmp->operation, ss->operation) == 0
871                         && strcmp(ss_tmp->orientation, ss->orientation) == 0)
872                         return;
873         }
874
875         *list = g_list_append(*list, ss);
876 }
877
878 static GList *__find_splashscreens(GList *splashscreens)
879 {
880         GList *list = NULL;
881         splashscreen_x *ss;
882
883         g_list_foreach(splashscreens,
884                         __find_appcontrol_splashscreen_with_dpi, &list);
885         g_list_foreach(splashscreens,
886                         __find_appcontrol_splashscreen, &list);
887
888         ss = __find_default_splashscreen(splashscreens, "portrait");
889         if (ss)
890                 list = g_list_append(list, ss);
891         ss = __find_default_splashscreen(splashscreens, "landscape");
892         if (ss)
893                 list = g_list_append(list, ss);
894
895         return list;
896 }
897
898 static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
899 {
900         static const char query[] =
901                 "INSERT INTO package_app_splash_screen (app_id, src, type,"
902                 "  orientation, indicatordisplay, operation, color_depth) "
903                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
904         int ret;
905         sqlite3_stmt *stmt;
906         int idx;
907         GList *tmp;
908         splashscreen_x *ss;
909         GList *ss_list;
910
911         if (app->splashscreens == NULL)
912                 return 0;
913
914         ss_list = __find_splashscreens(app->splashscreens);
915         if (ss_list == NULL)
916                 return 0;
917
918         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
919         if (ret != SQLITE_OK) {
920                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
921                 return -1;
922         }
923
924         for (tmp = ss_list; tmp; tmp = tmp->next) {
925                 ss = (splashscreen_x *)tmp->data;
926                 if (ss == NULL)
927                         continue;
928                 idx = 1;
929                 __BIND_TEXT(db, stmt, idx++, app->appid);
930                 __BIND_TEXT(db, stmt, idx++, ss->src);
931                 __BIND_TEXT(db, stmt, idx++, ss->type);
932                 __BIND_TEXT(db, stmt, idx++, ss->orientation);
933                 __BIND_TEXT(db, stmt, idx++, ss->indicatordisplay);
934                 __BIND_TEXT(db, stmt, idx++, ss->operation);
935                 __BIND_TEXT(db, stmt, idx++, ss->color_depth);
936
937                 ret = sqlite3_step(stmt);
938                 if (ret != SQLITE_DONE) {
939                         _LOGE("step failed: %s", sqlite3_errmsg(db));
940                         sqlite3_finalize(stmt);
941                         return -1;
942                 }
943
944                 sqlite3_reset(stmt);
945         }
946
947         sqlite3_finalize(stmt);
948
949         return 0;
950 }
951
952 static void __trimfunc(GList *trim_list)
953 {
954         char *trim_data;
955         char *prev = NULL;
956         GList *list = g_list_first(trim_list);
957
958         while (list) {
959                 trim_data = (char *)list->data;
960                 if (trim_data) {
961                         if (prev) {
962                                 if (strcmp(trim_data, prev) == 0) {
963                                         trim_list = g_list_remove(trim_list,
964                                                         trim_data);
965                                         list = g_list_first(trim_list);
966                                         prev = NULL;
967                                         continue;
968                                 } else
969                                         prev = trim_data;
970                         } else {
971                                 prev = trim_data;
972                         }
973                 }
974                 list = g_list_next(list);
975         }
976 }
977
978 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
979 {
980         if (a == NULL || b == NULL)
981                 return 0;
982         if (strcmp((char *)a, (char *)b) == 0)
983                 return 0;
984         if (strcmp((char *)a, (char *)b) < 0)
985                 return -1;
986         if (strcmp((char *)a, (char *)b) > 0)
987                 return 1;
988         return 0;
989 }
990
991 /* TODO: refactor inserting localized info */
992 static GList *__create_locale_list(GList *lbls, GList *lcns, GList *icns,
993                 GList *dcns, GList *aths)
994 {
995         GList *locale = NULL;
996         GList *tmp;
997         label_x *lbl;
998         license_x *lcn;
999         icon_x *icn;
1000         description_x *dcn;
1001         author_x *ath;
1002
1003         for (tmp = lbls; tmp; tmp = tmp->next) {
1004                 lbl = (label_x *)tmp->data;
1005                 if (lbl == NULL)
1006                         continue;
1007                 if (lbl->lang)
1008                         locale = g_list_insert_sorted_with_data(
1009                                         locale, (gpointer)lbl->lang,
1010                                         __comparefunc, NULL);
1011         }
1012         for (tmp = lcns; tmp; tmp = tmp->next) {
1013                 lcn = (license_x *)tmp->data;
1014                 if (lcn == NULL)
1015                         continue;
1016                 if (lcn->lang)
1017                         locale = g_list_insert_sorted_with_data(
1018                                         locale, (gpointer)lcn->lang,
1019                                         __comparefunc, NULL);
1020         }
1021         for (tmp = icns; tmp; tmp = tmp->next) {
1022                 icn = (icon_x *)tmp->data;
1023                 if (icn == NULL)
1024                         continue;
1025                 if (icn->lang)
1026                         locale = g_list_insert_sorted_with_data(
1027                                         locale, (gpointer)icn->lang,
1028                                         __comparefunc, NULL);
1029         }
1030         for (tmp = dcns; tmp; tmp = tmp->next) {
1031                 dcn = (description_x *)tmp->data;
1032                 if (dcn == NULL)
1033                         continue;
1034                 if (dcn->lang)
1035                         locale = g_list_insert_sorted_with_data(
1036                                         locale, (gpointer)dcn->lang,
1037                                         __comparefunc, NULL);
1038         }
1039         for (tmp = aths; tmp; tmp = tmp->next) {
1040                 ath = (author_x *)tmp->data;
1041                 if (ath == NULL)
1042                         continue;
1043                 if (ath->lang)
1044                         locale = g_list_insert_sorted_with_data(
1045                                         locale, (gpointer)ath->lang,
1046                                         __comparefunc, NULL);
1047         }
1048         __trimfunc(locale);
1049         return locale;
1050 }
1051
1052 static gint __check_icon_resolution(const char *orig_icon_path,
1053                 char **new_icon_path)
1054 {
1055         int ret;
1056         char *dpi_path[2];
1057         char *icon_filename;
1058         char modified_iconpath[BUFSIZE];
1059         char icon_path[BUFSIZE];
1060         int i;
1061         int dpi = -1;
1062
1063         if (orig_icon_path == NULL)
1064                 return -1;
1065
1066         ret = system_info_get_platform_int(
1067                         "http://tizen.org/feature/screen.dpi", &dpi);
1068         if (ret != SYSTEM_INFO_ERROR_NONE)
1069                 return -1;
1070
1071         if (dpi >= LDPI_MIN && dpi <= LDPI_MAX) {
1072                 dpi_path[0] = "LDPI";
1073                 dpi_path[1] = "ldpi";
1074         } else if (dpi >= MDPI_MIN && dpi <= MDPI_MAX) {
1075                 dpi_path[0] = "MDPI";
1076                 dpi_path[1] = "mdpi";
1077         } else if (dpi >= HDPI_MIN && dpi <= HDPI_MAX) {
1078                 dpi_path[0] = "HDPI";
1079                 dpi_path[1] = "hdpi";
1080         } else if (dpi >= XHDPI_MIN && dpi <= XHDPI_MAX) {
1081                 dpi_path[0] = "XHDPI";
1082                 dpi_path[1] = "xhdpi";
1083         } else if (dpi >= XXHDPI_MIN && dpi <= XXHDPI_MAX) {
1084                 dpi_path[0] = "XXHDPI";
1085                 dpi_path[1] = "xxhdpi";
1086         } else {
1087                 _LOGE("Unidentified dpi[%d]", dpi);
1088                 return -1;
1089         }
1090
1091         icon_filename = strrchr(orig_icon_path, '/');
1092         if (icon_filename == NULL)
1093                 return -1;
1094
1095         snprintf(icon_path,
1096                         strlen(orig_icon_path) - (strlen(icon_filename) - 1),
1097                         "%s", orig_icon_path);
1098         for (i = 0; i < 2; i++) {
1099                 snprintf(modified_iconpath, BUFSIZE - 1, "%s/%s%s",
1100                                 icon_path, dpi_path[i], icon_filename);
1101                 if (access(modified_iconpath, F_OK) != -1) {
1102                         /* if exists, return modified icon path */
1103                         *new_icon_path = strdup(modified_iconpath);
1104                         return 0;
1105                 }
1106         }
1107
1108         return -1;
1109 }
1110
1111 static gint __compare_icon(gconstpointer a, gconstpointer b)
1112 {
1113         icon_x *icon = (icon_x *)a;
1114         char *icon_path;
1115
1116         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1117                 return -1;
1118
1119         if (icon->dpi != NULL)
1120                 return -1;
1121
1122         if (__check_icon_resolution(icon->text, &icon_path) == 0) {
1123                 free(icon->text);
1124                 icon->text = icon_path;
1125         }
1126
1127         return 0;
1128 }
1129
1130 static gint __compare_icon_with_dpi(gconstpointer a, gconstpointer b)
1131 {
1132         icon_x *icon = (icon_x *)a;
1133         int dpi = GPOINTER_TO_INT(b);
1134
1135         if (icon->lang != NULL && strcasecmp(icon->lang, DEFAULT_LOCALE) != 0)
1136                 return -1;
1137
1138         if (icon->dpi == NULL)
1139                 return -1;
1140
1141         if (__check_dpi(icon->dpi, dpi) == 0)
1142                 return 0;
1143
1144         return -1;
1145 }
1146
1147 static gint __compare_icon_with_lang(gconstpointer a, gconstpointer b)
1148 {
1149         icon_x *icon = (icon_x *)a;
1150         char *lang = (char *)b;
1151         char *icon_path;
1152
1153         if (icon->dpi != NULL)
1154                 return -1;
1155
1156         if (strcasecmp(icon->lang, lang) == 0) {
1157                 if (strcasecmp(icon->lang, DEFAULT_LOCALE) == 0) {
1158                         /* icon for no locale. check existance of
1159                          * folder-hierachied default icons
1160                          */
1161                         if (__check_icon_resolution(icon->text,
1162                                                 &icon_path) == 0) {
1163                                 free(icon->text);
1164                                 icon->text = icon_path;
1165                         }
1166                 }
1167                 return 0;
1168         }
1169
1170         return -1;
1171 }
1172
1173 static gint __compare_icon_with_lang_dpi(gconstpointer a, gconstpointer b)
1174 {
1175         int ret;
1176         icon_x *icon = (icon_x *)a;
1177         char *lang = (char *)b;
1178         int dpi = -1;
1179
1180         ret = system_info_get_platform_int(
1181                         "http://tizen.org/feature/screen.dpi", &dpi);
1182         if (ret != SYSTEM_INFO_ERROR_NONE)
1183                 return -1;
1184
1185         if (strcasecmp(icon->lang, lang) == 0 &&
1186                         __check_dpi(icon->dpi, dpi) == 0)
1187                 return 0;
1188
1189         return -1;
1190 }
1191
1192 static char *__find_icon(GList *icons, const char *lang)
1193 {
1194         GList *tmp;
1195         icon_x *icon;
1196         int dpi = 0;
1197         int ret;
1198
1199         /* first, find icon whose locale and dpi with given lang and
1200          * system's dpi has matched
1201          */
1202         tmp = g_list_find_custom(icons, lang,
1203                         (GCompareFunc)__compare_icon_with_lang_dpi);
1204         if (tmp != NULL) {
1205                 icon = (icon_x *)tmp->data;
1206                 return (char *)icon->text;
1207         }
1208
1209         /* if first has failed, find icon whose locale has matched */
1210         tmp = g_list_find_custom(icons, lang,
1211                         (GCompareFunc)__compare_icon_with_lang);
1212         if (tmp != NULL) {
1213                 icon = (icon_x *)tmp->data;
1214                 return (char *)icon->text;
1215         }
1216
1217         /* if second has failed, find icon whose dpi has matched with
1218          * system's dpi
1219          */
1220         ret = system_info_get_platform_int(
1221                         "http://tizen.org/feature/screen.dpi", &dpi);
1222         if (ret == SYSTEM_INFO_ERROR_NONE) {
1223                 tmp = g_list_find_custom(icons, GINT_TO_POINTER(dpi),
1224                                 (GCompareFunc)__compare_icon_with_dpi);
1225                 if (tmp != NULL) {
1226                         icon = (icon_x *)tmp->data;
1227                         return (char *)icon->text;
1228                 }
1229         }
1230
1231         /* last, find default icon marked as "No Locale" */
1232         tmp = g_list_find_custom(icons, NULL, (GCompareFunc)__compare_icon);
1233         if (tmp != NULL) {
1234                 icon = (icon_x *)tmp->data;
1235                 return (char *)icon->text;
1236         }
1237
1238         return NULL;
1239 }
1240
1241 static void __extract_data(const char *locale, GList *lbls, GList *lcns,
1242                 GList *icns, GList *dcns, GList *aths, char **label,
1243                 char **license, char **icon, char **description, char **author)
1244 {
1245         GList *tmp;
1246         label_x *lbl;
1247         license_x *lcn;
1248         description_x *dcn;
1249         author_x *ath;
1250
1251         for (tmp = lbls; tmp; tmp = tmp->next) {
1252                 lbl = (label_x *)tmp->data;
1253                 if (lbl == NULL)
1254                         continue;
1255                 if (lbl->lang) {
1256                         if (strcmp(lbl->lang, locale) == 0) {
1257                                 *label = (char *)lbl->text;
1258                                 break;
1259                         }
1260                 }
1261         }
1262         for (tmp = lcns; tmp; tmp = tmp->next) {
1263                 lcn = (license_x *)tmp->data;
1264                 if (lcn == NULL)
1265                         continue;
1266                 if (lcn->lang) {
1267                         if (strcmp(lcn->lang, locale) == 0) {
1268                                 *license = (char *)lcn->text;
1269                                 break;
1270                         }
1271                 }
1272         }
1273
1274         *icon = __find_icon(icns, locale);
1275
1276         for (tmp = dcns; tmp; tmp = tmp->next) {
1277                 dcn = (description_x *)tmp->data;
1278                 if (dcn == NULL)
1279                         continue;
1280                 if (dcn->lang) {
1281                         if (strcmp(dcn->lang, locale) == 0) {
1282                                 *description = (char *)dcn->text;
1283                                 break;
1284                         }
1285                 }
1286         }
1287         for (tmp = aths; tmp; tmp = tmp->next) {
1288                 ath = (author_x *)tmp->data;
1289                 if (ath == NULL)
1290                         continue;
1291                 if (ath->lang) {
1292                         if (strcmp(ath->lang, locale) == 0) {
1293                                 *author = (char *)ath->text;
1294                                 break;
1295                         }
1296                 }
1297         }
1298 }
1299
1300 static int __insert_mainapp_localized_info(sqlite3 *db, application_x *app,
1301                 const char *locale, const char *label, const char *icon)
1302 {
1303         static const char query[] =
1304                 "INSERT OR REPLACE INTO package_localized_info ("
1305                 "  package, package_locale, package_label, package_icon,"
1306                 "  package_description, package_license, package_author) "
1307                 "VALUES (?, ?,"
1308                 "  COALESCE((SELECT package_label FROM package_localized_info"
1309                 "            WHERE package=? AND package_locale=?), ?),"
1310                 "  COALESCE((SELECT package_icon FROM package_localized_info"
1311                 "            WHERE package=? AND package_icon=?), ?),"
1312                 "  (SELECT package_description FROM package_localized_info"
1313                 "   WHERE package=? AND package_locale=?),"
1314                 "  (SELECT package_description FROM package_localized_info"
1315                 "   WHERE package=? AND package_locale=?),"
1316                 "  (SELECT package_description FROM package_localized_info"
1317                 "   WHERE package=? AND package_locale=?))";
1318         int ret;
1319         sqlite3_stmt *stmt;
1320         int idx = 1;
1321
1322         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1323         if (ret != SQLITE_OK) {
1324                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1325                 return -1;
1326         }
1327
1328         __BIND_TEXT(db, stmt, idx++, app->package);
1329         __BIND_TEXT(db, stmt, idx++, locale);
1330         __BIND_TEXT(db, stmt, idx++, app->package);
1331         __BIND_TEXT(db, stmt, idx++, locale);
1332         __BIND_TEXT(db, stmt, idx++, label);
1333         __BIND_TEXT(db, stmt, idx++, app->package);
1334         __BIND_TEXT(db, stmt, idx++, locale);
1335         __BIND_TEXT(db, stmt, idx++, icon);
1336         __BIND_TEXT(db, stmt, idx++, app->package);
1337         __BIND_TEXT(db, stmt, idx++, locale);
1338         __BIND_TEXT(db, stmt, idx++, app->package);
1339         __BIND_TEXT(db, stmt, idx++, locale);
1340         __BIND_TEXT(db, stmt, idx++, app->package);
1341         __BIND_TEXT(db, stmt, idx++, locale);
1342
1343         ret = sqlite3_step(stmt);
1344         if (ret != SQLITE_DONE) {
1345                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1346                 sqlite3_finalize(stmt);
1347                 return -1;
1348         }
1349
1350         sqlite3_finalize(stmt);
1351
1352         return 0;
1353 }
1354
1355 static int __insert_app_localized_info(sqlite3 *db, application_x *app)
1356 {
1357         static const char query[] =
1358                 "INSERT INTO package_app_localized_info (app_id, app_locale,"
1359                 "  app_label, app_icon) "
1360                 "VALUES (?, ?, ?, ?)";
1361         int ret;
1362         sqlite3_stmt *stmt;
1363         int idx;
1364         GList *tmp;
1365         GList *locales;
1366         const char *locale;
1367         char *label;
1368         char *icon;
1369
1370         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1371         if (ret != SQLITE_OK) {
1372                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1373                 return -1;
1374         }
1375
1376         locales = __create_locale_list(app->label, NULL, app->icon, NULL, NULL);
1377         for (tmp = locales; tmp; tmp = tmp->next) {
1378                 locale = (const char *)tmp->data;
1379                 label = NULL;
1380                 icon = NULL;
1381                 __extract_data(locale, app->label, NULL, app->icon, NULL, NULL,
1382                                 &label, NULL, &icon, NULL, NULL);
1383                 if (!label && !icon)
1384                         continue;
1385
1386                 idx = 1;
1387                 __BIND_TEXT(db, stmt, idx++, app->appid);
1388                 __BIND_TEXT(db, stmt, idx++, locale);
1389                 __BIND_TEXT(db, stmt, idx++, label);
1390                 __BIND_TEXT(db, stmt, idx++, icon);
1391
1392                 ret = sqlite3_step(stmt);
1393                 if (ret != SQLITE_DONE) {
1394                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1395                         g_list_free(locales);
1396                         sqlite3_finalize(stmt);
1397                         return -1;
1398                 }
1399
1400                 sqlite3_reset(stmt);
1401
1402                 if (strcasecmp(app->mainapp, "true")) {
1403                         if (__insert_mainapp_localized_info(db, app, locale,
1404                                                 label, icon))
1405                                 _LOGE("insert mainapp localized info failed");
1406                 }
1407         }
1408
1409         g_list_free(locales);
1410         sqlite3_finalize(stmt);
1411
1412         return 0;
1413 }
1414
1415 /* _PRODUCT_LAUNCHING_ENHANCED_
1416  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
1417  *  app->guestmode_appstatus
1418  */
1419 static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
1420 {
1421         static const char query[] =
1422                 "INSERT INTO package_app_info (app_id, app_component,"
1423                 "  app_exec, app_nodisplay, app_type, app_onboot, app_multiple,"
1424                 "  app_autorestart, app_taskmanage, app_hwacceleration,"
1425                 "  app_screenreader, app_mainapp, app_recentimage,"
1426                 "  app_launchcondition, app_indicatordisplay, app_portraitimg,"
1427                 "  app_landscapeimg, app_guestmodevisibility,"
1428                 "  app_permissiontype, app_preload, app_submode,"
1429                 "  app_submode_mainid, app_installed_storage, app_process_pool,"
1430                 "  app_launch_mode, app_ui_gadget, app_support_mode,"
1431                 "  app_support_disable, component_type, package, app_tep_name,"
1432                 "  app_zip_mount_file, app_background_category,"
1433                 "  app_package_type, app_root_path, app_api_version,"
1434                 "  app_effective_appid, app_splash_screen_display,"
1435                 "  app_package_system, app_removable,"
1436                 "  app_package_installed_time, app_support_ambient,"
1437                 "  app_setup_appid) "
1438                 "VALUES (?, ?, "
1439                 "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
1440                 "  LOWER(?), LOWER(?), ?,"
1441                 "  ?, LOWER(?), ?,"
1442                 "  ?, LOWER(?), ?,"
1443                 "  ?, LOWER(?),"
1444                 "  ?, LOWER(?), LOWER(?),"
1445                 "  ?, ?, LOWER(?),"
1446                 "  ?, LOWER(?), ?,"
1447                 "  LOWER(?), ?, ?, ?,"
1448                 "  ?, ?,"
1449                 "  ?, ?, ?,"
1450                 "  ?, LOWER(?),"
1451                 "  LOWER(?), LOWER(?),"
1452                 "  ?, LOWER(?),"
1453                 "  ?)";
1454         int ret;
1455         sqlite3_stmt *stmt;
1456         int idx;
1457         GList *tmp;
1458         application_x *app;
1459         int bg_category;
1460         const char *effective_appid;
1461
1462         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1463         if (ret != SQLITE_OK) {
1464                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1465                 return -1;
1466         }
1467
1468         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1469                 app = (application_x *)tmp->data;
1470                 if (app == NULL)
1471                         continue;
1472
1473                 bg_category = __convert_background_category(
1474                                 app->background_category);
1475                 effective_appid = __find_effective_appid(app->metadata);
1476
1477                 idx = 1;
1478                 __BIND_TEXT(db, stmt, idx++, app->appid);
1479                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1480                 __BIND_TEXT(db, stmt, idx++, app->exec);
1481                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->nodisplay, false));
1482                 __BIND_TEXT(db, stmt, idx++, app->type);
1483                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->onboot, false));
1484                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->multiple, false));
1485                 __BIND_TEXT(db, stmt, idx++,
1486                                 __get_bool(app->autorestart, false));
1487                 __BIND_TEXT(db, stmt, idx++,
1488                                 __get_bool(app->taskmanage, false));
1489                 __BIND_TEXT(db, stmt, idx++, app->hwacceleration);
1490                 __BIND_TEXT(db, stmt, idx++, app->screenreader);
1491                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->mainapp, false));
1492                 __BIND_TEXT(db, stmt, idx++, app->recentimage);
1493                 __BIND_TEXT(db, stmt, idx++, app->launchcondition);
1494                 __BIND_TEXT(db, stmt, idx++,
1495                                 __get_bool(app->indicatordisplay, true));
1496                 __BIND_TEXT(db, stmt, idx++, app->portraitimg);
1497                 __BIND_TEXT(db, stmt, idx++, app->landscapeimg);
1498                 __BIND_TEXT(db, stmt, idx++,
1499                                 __get_bool(app->guestmode_visibility, true));
1500                 __BIND_TEXT(db, stmt, idx++, app->permission_type);
1501                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1502                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->submode, false));
1503                 __BIND_TEXT(db, stmt, idx++, app->submode_mainid);
1504                 __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1505                 __BIND_TEXT(db, stmt, idx++,
1506                                 __get_bool(app->process_pool, false));
1507                 __BIND_TEXT(db, stmt, idx++, app->launch_mode);
1508                 __BIND_TEXT(db, stmt, idx++, __get_bool(app->ui_gadget, false));
1509                 __BIND_TEXT(db, stmt, idx++, app->support_mode);
1510                 __BIND_TEXT(db, stmt, idx++,
1511                                 __get_bool(mfx->support_disable, false));
1512                 __BIND_TEXT(db, stmt, idx++, app->component_type);
1513                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1514                 __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1515                 __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1516                 __BIND_INT(db, stmt, idx++, bg_category);
1517                 __BIND_TEXT(db, stmt, idx++, mfx->type ? mfx->type : "tpk");
1518                 __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1519                 __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1520                 __BIND_TEXT(db, stmt, idx++, effective_appid);
1521                 __BIND_TEXT(db, stmt, idx++,
1522                                 __get_bool(app->splash_screen_display, false));
1523                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1524                 __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, false));
1525                 __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1526                 __BIND_TEXT(db, stmt, idx++,
1527                                 __get_bool(app->support_ambient, false));
1528                 __BIND_TEXT(db, stmt, idx++, app->setup_appid);
1529
1530                 ret = sqlite3_step(stmt);
1531                 if (ret != SQLITE_DONE) {
1532                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1533                         sqlite3_finalize(stmt);
1534                         return -1;
1535                 }
1536
1537                 sqlite3_reset(stmt);
1538
1539                 if (__insert_appcontrol_info(db, app)) {
1540                         sqlite3_finalize(stmt);
1541                         return -1;
1542                 }
1543                 if (__insert_category_info(db, app)) {
1544                         sqlite3_finalize(stmt);
1545                         return -1;
1546                 }
1547                 if (__insert_metadata_info(db, app)) {
1548                         sqlite3_finalize(stmt);
1549                         return -1;
1550                 }
1551                 if (__insert_datacontrol_info(db, app)) {
1552                         sqlite3_finalize(stmt);
1553                         return -1;
1554                 }
1555                 if (__insert_splashscreen_info(db, app)) {
1556                         sqlite3_finalize(stmt);
1557                         return -1;
1558                 }
1559                 if (__insert_app_localized_info(db, app)) {
1560                         sqlite3_finalize(stmt);
1561                         return -1;
1562                 }
1563         }
1564
1565         sqlite3_finalize(stmt);
1566
1567         return 0;
1568 }
1569
1570 static int __insert_package_localized_info(sqlite3 *db, manifest_x *mfx)
1571 {
1572         static const char query[] =
1573                 "INSERT INTO package_localized_info (package, package_locale,"
1574                 "  package_label, package_icon, package_description,"
1575                 "  package_license, package_author) "
1576                 "VALUES (?, ?, ?, ?, ?, ?, ?)";
1577         int ret;
1578         sqlite3_stmt *stmt;
1579         int idx;
1580         GList *tmp;
1581         GList *locales;
1582         const char *locale;
1583         char *label;
1584         char *icon;
1585         char *description;
1586         char *license;
1587         char *author;
1588
1589         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1590         if (ret != SQLITE_OK) {
1591                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1592                 return -1;
1593         }
1594
1595         locales = __create_locale_list(mfx->label, mfx->license, mfx->icon,
1596                         mfx->description, mfx->author);
1597         for (tmp = locales; tmp; tmp = tmp->next) {
1598                 locale = (const char *)tmp->data;
1599                 label = NULL;
1600                 icon = NULL;
1601                 description = NULL;
1602                 license = NULL;
1603                 author = NULL;
1604                 __extract_data(locale, mfx->label, mfx->license, mfx->icon,
1605                                 mfx->description, mfx->author,
1606                                 &label, &license, &icon, &description, &author);
1607                 if (!label && !license && !icon && !description && !author)
1608                         continue;
1609
1610                 idx = 1;
1611                 __BIND_TEXT(db, stmt, idx++, mfx->package);
1612                 __BIND_TEXT(db, stmt, idx++, locale);
1613                 __BIND_TEXT(db, stmt, idx++, label);
1614                 __BIND_TEXT(db, stmt, idx++, icon);
1615                 __BIND_TEXT(db, stmt, idx++, description);
1616                 __BIND_TEXT(db, stmt, idx++, license);
1617                 __BIND_TEXT(db, stmt, idx++, author);
1618
1619                 ret = sqlite3_step(stmt);
1620                 if (ret != SQLITE_DONE) {
1621                         _LOGE("step failed: %s", sqlite3_errmsg(db));
1622                         g_list_free(locales);
1623                         sqlite3_finalize(stmt);
1624                         return -1;
1625                 }
1626
1627                 sqlite3_reset(stmt);
1628         }
1629
1630         g_list_free(locales);
1631         sqlite3_finalize(stmt);
1632
1633         return 0;
1634 }
1635
1636 static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
1637 {
1638         static const char query[] =
1639                 "INSERT INTO package_info (package, package_type,"
1640                 "  package_version, package_api_version, package_tep_name,"
1641                 "  package_zip_mount_file, install_location, package_size,"
1642                 "  package_removable, package_preload, package_readonly,"
1643                 "  package_update, package_appsetting, package_nodisplay,"
1644                 "  package_system, author_name, author_email, author_href,"
1645                 "  installed_time, installed_storage, storeclient_id,"
1646                 "  mainapp_id, package_url, root_path, external_path,"
1647                 "  csc_path, package_support_mode, package_support_disable) "
1648                 "VALUES (?, ?,"
1649                 "  ?, ?, ?,"
1650                 "  ?, ?, ?,"
1651                 "  LOWER(?), LOWER(?), LOWER(?),"
1652                 "  LOWER(?), LOWER(?), LOWER(?),"
1653                 "  LOWER(?), ?, ?, ?,"
1654                 "  ?, ?, ?,"
1655                 "  ?, ?, ?, ?,"
1656                 "  ?, ?, LOWER(?))";
1657         int ret;
1658         sqlite3_stmt *stmt;
1659         int idx = 1;
1660         const char *author_name = NULL;
1661         const char *author_email = NULL;
1662         const char *author_href = NULL;
1663
1664         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1665         if (ret != SQLITE_OK) {
1666                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1667                 return -1;
1668         }
1669
1670         if (mfx->author && mfx->author->data) {
1671                 author_name = ((author_x *)mfx->author->data)->text;
1672                 author_email = ((author_x *)mfx->author->data)->email;
1673                 author_href = ((author_x *)mfx->author->data)->href;
1674         }
1675
1676         __BIND_TEXT(db, stmt, idx++, mfx->package);
1677         __BIND_TEXT(db, stmt, idx++, mfx->type);
1678         __BIND_TEXT(db, stmt, idx++, mfx->version);
1679         __BIND_TEXT(db, stmt, idx++, mfx->api_version);
1680         __BIND_TEXT(db, stmt, idx++, mfx->tep_name);
1681         __BIND_TEXT(db, stmt, idx++, mfx->zip_mount_file);
1682         __BIND_TEXT(db, stmt, idx++, mfx->installlocation);
1683         __BIND_TEXT(db, stmt, idx++, mfx->package_size);
1684         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->removable, true));
1685         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->preload, false));
1686         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->readonly, false));
1687         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->update, false));
1688         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->appsetting, false));
1689         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->nodisplay_setting, false));
1690         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->system, false));
1691         __BIND_TEXT(db, stmt, idx++, author_name);
1692         __BIND_TEXT(db, stmt, idx++, author_email);
1693         __BIND_TEXT(db, stmt, idx++, author_href);
1694         __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
1695         __BIND_TEXT(db, stmt, idx++, mfx->installed_storage);
1696         __BIND_TEXT(db, stmt, idx++, mfx->storeclient_id);
1697         __BIND_TEXT(db, stmt, idx++, mfx->mainapp_id);
1698         __BIND_TEXT(db, stmt, idx++, mfx->package_url);
1699         __BIND_TEXT(db, stmt, idx++, mfx->root_path);
1700         __BIND_TEXT(db, stmt, idx++, mfx->external_path);
1701         __BIND_TEXT(db, stmt, idx++, mfx->csc_path);
1702         __BIND_TEXT(db, stmt, idx++, mfx->support_mode);
1703         __BIND_TEXT(db, stmt, idx++, __get_bool(mfx->support_disable, false));
1704
1705         ret = sqlite3_step(stmt);
1706         if (ret != SQLITE_DONE) {
1707                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1708                 sqlite3_finalize(stmt);
1709                 return -1;
1710         }
1711
1712         sqlite3_finalize(stmt);
1713
1714         if (__insert_package_localized_info(db, mfx))
1715                 return -1;
1716         if (__insert_application_info(db, mfx))
1717                 return -1;
1718
1719         return 0;
1720 }
1721
1722 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1723 {
1724         int ret;
1725         const char *dbpath;
1726         sqlite3 *db;
1727
1728         if (mfx == NULL) {
1729                 _LOGE("invalid parameter");
1730                 return PM_PARSER_R_EINVAL;
1731         }
1732
1733         dbpath = __get_parser_db_path(uid);
1734
1735         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1736         if (ret != SQLITE_OK) {
1737                 _LOGE("open db failed: %d", ret);
1738                 return PM_PARSER_R_ERROR;
1739         }
1740
1741         __BEGIN_TRANSACTION(db);
1742         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1743         __END_TRANSACTION(db);
1744
1745         sqlite3_close_v2(db);
1746
1747         return PM_PARSER_R_OK;
1748 }
1749
1750 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1751 {
1752         return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
1753 }
1754
1755 static int __delete_package_info(sqlite3 *db, const char *pkgid)
1756 {
1757         static const char query[] =
1758                 "DELETE FROM package_info WHERE package=?";
1759         int ret;
1760         sqlite3_stmt *stmt;
1761
1762         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1763         if (ret != SQLITE_OK) {
1764                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1765                 return -1;
1766         }
1767
1768         __BIND_TEXT(db, stmt, 1, pkgid);
1769
1770         ret = sqlite3_step(stmt);
1771         if (ret != SQLITE_DONE) {
1772                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1773                 sqlite3_finalize(stmt);
1774                 return -1;
1775         }
1776
1777         return 0;
1778 }
1779
1780 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
1781         uid_t uid)
1782 {
1783         int ret;
1784         const char *dbpath;
1785         sqlite3 *db;
1786
1787         if (mfx == NULL) {
1788                 _LOGE("invalid parameter");
1789                 return PM_PARSER_R_EINVAL;
1790         }
1791
1792         dbpath = __get_parser_db_path(uid);
1793
1794         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1795         if (ret != SQLITE_OK) {
1796                 _LOGE("open db failed: %d", ret);
1797                 return PM_PARSER_R_ERROR;
1798         }
1799
1800         __BEGIN_TRANSACTION(db);
1801         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1802         __END_TRANSACTION(db);
1803
1804         sqlite3_close_v2(db);
1805
1806         return PM_PARSER_R_OK;
1807 }
1808
1809 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1810 {
1811         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
1812 }
1813
1814 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1815 {
1816         int ret;
1817         const char *dbpath;
1818         sqlite3 *db;
1819
1820         if (mfx == NULL) {
1821                 _LOGE("invalid parameter");
1822                 return PM_PARSER_R_EINVAL;
1823         }
1824
1825         dbpath = __get_parser_db_path(uid);
1826
1827         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1828         if (ret != SQLITE_OK) {
1829                 _LOGE("open db failed: %d", ret);
1830                 return PM_PARSER_R_ERROR;
1831         }
1832
1833         __BEGIN_TRANSACTION(db);
1834         __DO_TRANSACTION(db, __delete_package_info(db, mfx->package));
1835         __DO_TRANSACTION(db, __insert_package_info(db, mfx));
1836         __END_TRANSACTION(db);
1837
1838         sqlite3_close_v2(db);
1839
1840         return PM_PARSER_R_OK;
1841 }
1842
1843 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1844 {
1845         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
1846 }
1847
1848 static int __set_global_app_disable_for_uid(sqlite3 *db, const char *appid,
1849                 uid_t uid, bool is_disable)
1850 {
1851         static const char query[] =
1852                 "INSERT OR REPLACE INTO package_app_info_for_uid ("
1853                 "  app_id, uid, is_disabled, is_splash_screen_enabled) "
1854                 "VALUES (?, ?, ?,"
1855                 "  (SELECT app_splash_screen_display FROM package_app_info"
1856                 "   WHERE app_id=?))";
1857         int ret;
1858         sqlite3_stmt *stmt;
1859         int idx = 1;
1860
1861         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1862         if (ret != SQLITE_OK) {
1863                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1864                 return -1;
1865         }
1866
1867         __BIND_TEXT(db, stmt, idx++, appid);
1868         __BIND_INT(db, stmt, idx++, uid);
1869         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
1870         __BIND_TEXT(db, stmt, idx++, appid);
1871
1872         ret = sqlite3_step(stmt);
1873         if (ret != SQLITE_DONE) {
1874                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1875                 sqlite3_finalize(stmt);
1876                 return -1;
1877         }
1878
1879         sqlite3_finalize(stmt);
1880
1881         return 0;
1882 }
1883
1884 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
1885                 const char *appid, uid_t uid, int is_disable)
1886 {
1887         int ret;
1888         const char *dbpath;
1889         sqlite3 *db;
1890
1891         if (appid == NULL) {
1892                 _LOGE("invalid parameter");
1893                 return PM_PARSER_R_EINVAL;
1894         }
1895
1896         dbpath = __get_parser_db_path(uid);
1897
1898         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1899         if (ret != SQLITE_OK) {
1900                 _LOGE("open db failed: %d", ret);
1901                 return PM_PARSER_R_ERROR;
1902         }
1903
1904         __BEGIN_TRANSACTION(db);
1905         __DO_TRANSACTION(db, __set_global_app_disable_for_uid(db, appid,
1906                                 uid, (bool)is_disable));
1907         __END_TRANSACTION(db);
1908
1909         sqlite3_close_v2(db);
1910
1911         return PM_PARSER_R_OK;
1912 }
1913
1914 static int __set_app_disable(sqlite3 *db, const char *appid, uid_t uid,
1915                 bool is_disable)
1916 {
1917         static const char query[] =
1918                 "UPDATE package_app_info SET app_disable=? "
1919                 "WHERE app_id=?";
1920         int ret;
1921         sqlite3_stmt *stmt;
1922         int idx = 1;
1923
1924         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1925         if (ret != SQLITE_OK) {
1926                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1927                 return -1;
1928         }
1929
1930         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
1931         __BIND_TEXT(db, stmt, idx++, appid);
1932
1933         ret = sqlite3_step(stmt);
1934         if (ret != SQLITE_DONE) {
1935                 _LOGE("step failed: %s", sqlite3_errmsg(db));
1936                 sqlite3_finalize(stmt);
1937                 return -1;
1938         }
1939
1940         sqlite3_finalize(stmt);
1941
1942         return 0;
1943 }
1944
1945 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
1946                 uid_t uid, int is_disable)
1947 {
1948         int ret;
1949         const char *dbpath;
1950         sqlite3 *db;
1951
1952         if (appid == NULL) {
1953                 _LOGE("invalid parameter");
1954                 return PM_PARSER_R_EINVAL;
1955         }
1956
1957         dbpath = __get_parser_db_path(uid);
1958
1959         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
1960         if (ret != SQLITE_OK) {
1961                 _LOGE("open db failed: %d", ret);
1962                 return PM_PARSER_R_ERROR;
1963         }
1964
1965         __BEGIN_TRANSACTION(db);
1966         __DO_TRANSACTION(db, __set_app_disable(db, appid, uid,
1967                                 (bool)is_disable));
1968         __END_TRANSACTION(db);
1969
1970         sqlite3_close_v2(db);
1971
1972         return PM_PARSER_R_OK;
1973 }
1974
1975 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
1976                 int is_disable)
1977 {
1978         return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
1979                         __getuid(), is_disable);
1980 }
1981
1982 static int __set_pkg_disable(sqlite3 *db, const char *pkgid, uid_t uid,
1983                 bool is_disable)
1984 {
1985         static const char query[] =
1986                 "UPDATE package_info SET package_disable=? "
1987                 "WHERE package=?";
1988         int ret;
1989         sqlite3_stmt *stmt;
1990         int idx = 1;
1991
1992         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1993         if (ret != SQLITE_OK) {
1994                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
1995                 return -1;
1996         }
1997
1998         __BIND_TEXT(db, stmt, idx++, is_disable ? "true" : "false");
1999         __BIND_TEXT(db, stmt, idx++, pkgid);
2000
2001         ret = sqlite3_step(stmt);
2002         if (ret != SQLITE_DONE) {
2003                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2004                 sqlite3_finalize(stmt);
2005                 return -1;
2006         }
2007
2008         sqlite3_finalize(stmt);
2009
2010         return 0;
2011 }
2012
2013 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
2014                 uid_t uid, int is_disable)
2015 {
2016         int ret;
2017         const char *dbpath;
2018         sqlite3 *db;
2019
2020         if (pkgid == NULL) {
2021                 _LOGE("invalid parameter");
2022                 return PM_PARSER_R_EINVAL;
2023         }
2024
2025         dbpath = __get_parser_db_path(uid);
2026
2027         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2028         if (ret != SQLITE_OK) {
2029                 _LOGE("open db failed: %d", ret);
2030                 return PM_PARSER_R_ERROR;
2031         }
2032
2033         __BEGIN_TRANSACTION(db);
2034         __DO_TRANSACTION(db, __set_pkg_disable(db, pkgid, uid,
2035                                 (bool)is_disable));
2036         __END_TRANSACTION(db);
2037
2038         sqlite3_close_v2(db);
2039
2040         return PM_PARSER_R_OK;
2041 }
2042
2043 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
2044                 int is_disable)
2045 {
2046         return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
2047                         __getuid(), is_disable);
2048 }
2049
2050 static int __set_global_app_splash_screen_for_uid(sqlite3 *db,
2051                 const char *appid, uid_t uid, bool is_enabled)
2052 {
2053         static const char query[] =
2054                 "INSERT OR REPLACE INTO package_app_info_for_uid("
2055                 "  appid, uid, is_splash_screen_enabled) "
2056                 "VALUES (?, ?, ?)";
2057         int ret;
2058         sqlite3_stmt *stmt;
2059         int idx = 1;
2060
2061         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2062         if (ret != SQLITE_OK) {
2063                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2064                 return -1;
2065         }
2066
2067         __BIND_TEXT(db, stmt, idx++, appid);
2068         __BIND_INT(db, stmt, idx++, uid);
2069         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2070
2071         ret = sqlite3_step(stmt);
2072         if (ret != SQLITE_DONE) {
2073                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2074                 sqlite3_finalize(stmt);
2075                 return -1;
2076         }
2077
2078         sqlite3_finalize(stmt);
2079
2080         return 0;
2081 }
2082
2083 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
2084                 const char *appid, uid_t uid, int flag)
2085 {
2086         int ret;
2087         const char *dbpath;
2088         sqlite3 *db;
2089
2090         if (appid == NULL) {
2091                 _LOGE("invalid parameter");
2092                 return PM_PARSER_R_EINVAL;
2093         }
2094
2095         dbpath = __get_parser_db_path(uid);
2096
2097         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2098         if (ret != SQLITE_OK) {
2099                 _LOGE("open db failed: %d", ret);
2100                 return PM_PARSER_R_ERROR;
2101         }
2102
2103         __BEGIN_TRANSACTION(db);
2104         __DO_TRANSACTION(db, __set_global_app_splash_screen_for_uid(db,
2105                                 appid, uid, (bool)flag));
2106         __END_TRANSACTION(db);
2107
2108         sqlite3_close_v2(db);
2109
2110         return PM_PARSER_R_OK;
2111 }
2112
2113 static int __set_app_splash_screen(sqlite3 *db, const char *appid,
2114                 bool is_enabled)
2115 {
2116         static const char query[] =
2117                 "UPDATE package_app_info SET app_splash_screen_display=? "
2118                 "WHERE app_id=?";
2119         int ret;
2120         sqlite3_stmt *stmt;
2121         int idx = 1;
2122
2123         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2124         if (ret != SQLITE_OK) {
2125                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2126                 return -1;
2127         }
2128
2129         __BIND_TEXT(db, stmt, idx++, is_enabled ? "true" : "false");
2130         __BIND_TEXT(db, stmt, idx++, appid);
2131
2132         ret = sqlite3_step(stmt);
2133         if (ret != SQLITE_DONE) {
2134                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2135                 sqlite3_finalize(stmt);
2136                 return -1;
2137         }
2138
2139         sqlite3_finalize(stmt);
2140
2141         return 0;
2142 }
2143
2144 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2145                 const char *appid, uid_t uid, int flag)
2146 {
2147         int ret;
2148         const char *dbpath;
2149         sqlite3 *db;
2150
2151         if (appid == NULL) {
2152                 _LOGE("invalid parameter");
2153                 return PM_PARSER_R_EINVAL;
2154         }
2155
2156         dbpath = __get_parser_db_path(uid);
2157
2158         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2159         if (ret != SQLITE_OK) {
2160                 _LOGE("open db failed: %d", ret);
2161                 return PM_PARSER_R_ERROR;
2162         }
2163
2164         __BEGIN_TRANSACTION(db);
2165         __DO_TRANSACTION(db, __set_app_splash_screen(db, appid, (bool)flag));
2166         __END_TRANSACTION(db);
2167
2168         sqlite3_close_v2(db);
2169
2170         return PM_PARSER_R_OK;
2171 }
2172
2173 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
2174                 const char *appid, int flag)
2175 {
2176         return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
2177                         appid, __getuid(), flag);
2178 }
2179
2180 static int __set_app_label(sqlite3 *db, const char *appid, const char *label)
2181 {
2182         static const char query[] =
2183                 "UPDATE package_app_localized_info SET app_label=? "
2184                 "WHERE app_id=? AND app_label IS NOT NULL";
2185         int ret;
2186         sqlite3_stmt *stmt;
2187         int idx = 1;
2188
2189         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2190         if (ret != SQLITE_OK) {
2191                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2192                 return -1;
2193         }
2194
2195         __BIND_TEXT(db, stmt, idx++, label);
2196         __BIND_TEXT(db, stmt, idx++, appid);
2197
2198         ret = sqlite3_step(stmt);
2199         if (ret != SQLITE_DONE) {
2200                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2201                 sqlite3_finalize(stmt);
2202                 return -1;
2203         }
2204
2205         sqlite3_finalize(stmt);
2206
2207         return 0;
2208 }
2209
2210 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
2211                 uid_t uid, const char *label)
2212 {
2213         int ret;
2214         const char *dbpath;
2215         sqlite3 *db;
2216
2217         if (appid == NULL) {
2218                 _LOGE("invalid parameter");
2219                 return PM_PARSER_R_EINVAL;
2220         }
2221
2222         dbpath = __get_parser_db_path(uid);
2223
2224         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2225         if (ret != SQLITE_OK) {
2226                 _LOGE("open db failed: %d", ret);
2227                 return PM_PARSER_R_ERROR;
2228         }
2229
2230         __BEGIN_TRANSACTION(db);
2231         __DO_TRANSACTION(db, __set_app_label(db, appid, label));
2232         __END_TRANSACTION(db);
2233
2234         sqlite3_close_v2(db);
2235
2236         return PM_PARSER_R_OK;
2237 }
2238
2239 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
2240                 const char *label)
2241 {
2242         return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
2243                         label);
2244 }
2245
2246 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
2247 {
2248         static const char query[] =
2249                 "UPDATE package_info SET package_tep_name=? "
2250                 "WHERE package=?";
2251         int ret;
2252         sqlite3_stmt *stmt;
2253         int idx = 1;
2254
2255         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2256         if (ret != SQLITE_OK) {
2257                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2258                 return -1;
2259         }
2260
2261         __BIND_TEXT(db, stmt, idx++, tep_path);
2262         __BIND_TEXT(db, stmt, idx++, pkgid);
2263
2264         ret = sqlite3_step(stmt);
2265         if (ret != SQLITE_DONE) {
2266                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2267                 sqlite3_finalize(stmt);
2268                 return -1;
2269         }
2270
2271         sqlite3_finalize(stmt);
2272
2273         return 0;
2274 }
2275
2276 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
2277                 const char *tep_path, uid_t uid)
2278 {
2279         int ret;
2280         const char *dbpath;
2281         sqlite3 *db;
2282
2283         if (pkgid == NULL) {
2284                 _LOGE("invalid parameter");
2285                 return PM_PARSER_R_EINVAL;
2286         }
2287
2288         dbpath = __get_parser_db_path(uid);
2289
2290         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2291         if (ret != SQLITE_OK) {
2292                 _LOGE("open db failed: %d", ret);
2293                 return PM_PARSER_R_ERROR;
2294         }
2295
2296         __BEGIN_TRANSACTION(db);
2297         __DO_TRANSACTION(db, __set_tep_path(db, pkgid, tep_path));
2298         __END_TRANSACTION(db);
2299
2300         sqlite3_close_v2(db);
2301
2302         return PM_PARSER_R_OK;
2303 }
2304
2305 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
2306                 const char *tep_path)
2307 {
2308         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
2309                         __getuid());
2310 }
2311
2312 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
2313                 const char **update_type)
2314 {
2315         if (type == PMINFO_UPDATEINFO_NONE)
2316                 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
2317         else if (type == PMINFO_UPDATEINFO_FORCE)
2318                 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
2319         else if (type == PMINFO_UPDATEINFO_OPTIONAL)
2320                 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
2321         else
2322                 return -1;
2323         return 0;
2324 }
2325
2326 static int __register_pkg_update_info(sqlite3 *db, updateinfo_x *info,
2327                 const char *update_type)
2328 {
2329         static const char query[] =
2330                 "UPDATE package_update_info "
2331                 "SET update_version=?, update_type=? "
2332                 "WHERE package=?";
2333         int ret;
2334         sqlite3_stmt *stmt;
2335         int idx = 1;
2336
2337         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2338         if (ret != SQLITE_OK) {
2339                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2340                 return -1;
2341         }
2342
2343         __BIND_TEXT(db, stmt, idx++, info->version);
2344         __BIND_TEXT(db, stmt, idx++, update_type);
2345         __BIND_TEXT(db, stmt, idx++, info->pkgid);
2346
2347         ret = sqlite3_step(stmt);
2348         if (ret != SQLITE_DONE) {
2349                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2350                 sqlite3_finalize(stmt);
2351                 return -1;
2352         }
2353
2354         sqlite3_finalize(stmt);
2355
2356         return 0;
2357 }
2358
2359 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
2360                 pkgmgrinfo_updateinfo_h handle, uid_t uid)
2361 {
2362         int ret;
2363         updateinfo_x *update_info;
2364         updateinfo_x *prev_update_info;
2365         pkgmgrinfo_updateinfo_h prev_update_handle;
2366         pkgmgrinfo_pkginfo_h pkginfo;
2367         pkgmgrinfo_version_compare_type compare_result;
2368         bool is_global_pkg;
2369         const char *update_type;
2370         const char *dbpath;
2371         sqlite3 *db;
2372
2373         if (handle == NULL) {
2374                 _LOGE("invalid parameter");
2375                 return PM_PARSER_R_EINVAL;
2376         }
2377
2378         update_info = (updateinfo_x *)handle;
2379         if (update_info->pkgid == NULL || update_info->version == NULL)
2380                 return PM_PARSER_R_EINVAL;
2381         if (__convert_update_type(update_info->type, &update_type) != 0)
2382                 return PM_PARSER_R_EINVAL;
2383
2384         ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
2385                         &prev_update_handle, uid);
2386         if (ret != PMINFO_R_OK)
2387                 return PM_PARSER_R_ERROR;
2388
2389         prev_update_info = (updateinfo_x *)prev_update_handle;
2390         ret = pkgmgrinfo_compare_package_version(update_info->version,
2391                         prev_update_info->version, &compare_result);
2392         if (ret != PMINFO_R_OK) {
2393                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2394                 return PM_PARSER_R_ERROR;
2395         }
2396
2397         if (compare_result == PMINFO_VERSION_SAME &&
2398                         prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
2399                 _LOGI("Given update info version[%s] of pkgid[%s] "
2400                                 "will be ignored",
2401                                 update_info->version, update_info->pkgid);
2402                 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2403                 return PM_PARSER_R_OK;
2404         }
2405         pkgmgrinfo_updateinfo_destroy(prev_update_handle);
2406
2407         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
2408                         &pkginfo);
2409         if (ret != PMINFO_R_OK)
2410                 return PM_PARSER_R_ERROR;
2411
2412         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2413         if (ret != PMINFO_R_OK) {
2414                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2415                 return PM_PARSER_R_ERROR;
2416         }
2417         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2418
2419         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2420
2421         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2422         if (ret != SQLITE_OK) {
2423                 _LOGE("open db failed: %d", ret);
2424                 return PM_PARSER_R_ERROR;
2425         }
2426
2427         __BEGIN_TRANSACTION(db);
2428         __DO_TRANSACTION(db, __register_pkg_update_info(db, update_info,
2429                                 update_type));
2430         __END_TRANSACTION(db);
2431
2432         sqlite3_close_v2(db);
2433
2434         return PM_PARSER_R_OK;
2435 }
2436
2437 API int pkgmgr_parser_register_pkg_update_info_in_db(
2438                 pkgmgrinfo_updateinfo_h handle)
2439 {
2440         return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
2441                         __getuid());
2442 }
2443
2444 static int __unregister_pkg_update_info(sqlite3 *db, const char *pkgid)
2445 {
2446         static const char query[] =
2447                 "UPDATE package_update_info SET update_type='none' "
2448                 "WHERE package=?";
2449         int ret;
2450         sqlite3_stmt *stmt;
2451         int idx = 1;
2452
2453         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2454         if (ret != SQLITE_OK) {
2455                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2456                 return -1;
2457         }
2458
2459         __BIND_TEXT(db, stmt, idx++, pkgid);
2460
2461         ret = sqlite3_step(stmt);
2462         if (ret != SQLITE_DONE) {
2463                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2464                 sqlite3_finalize(stmt);
2465                 return -1;
2466         }
2467
2468         sqlite3_finalize(stmt);
2469
2470         return 0;
2471 }
2472
2473 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
2474                 uid_t uid)
2475 {
2476         int ret;
2477         const char *dbpath;
2478         sqlite3 *db;
2479         pkgmgrinfo_pkginfo_h pkginfo;
2480         bool is_global_pkg;
2481
2482         if (pkgid == NULL) {
2483                 _LOGE("invalid parameter");
2484                 return PM_PARSER_R_EINVAL;
2485         }
2486
2487         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
2488         if (ret != PMINFO_R_OK)
2489                 return PM_PARSER_R_EINVAL;
2490
2491         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
2492         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2493         if (ret != PMINFO_R_OK)
2494                 return PM_PARSER_R_ERROR;
2495
2496         dbpath = __get_parser_db_path(is_global_pkg ? GLOBAL_USER : uid);
2497
2498         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2499         if (ret != SQLITE_OK) {
2500                 _LOGE("open db failed: %d", ret);
2501                 return PM_PARSER_R_ERROR;
2502         }
2503
2504         __BEGIN_TRANSACTION(db);
2505         __DO_TRANSACTION(db, __unregister_pkg_update_info(db, pkgid));
2506         __END_TRANSACTION(db);
2507
2508         sqlite3_close_v2(db);
2509
2510         return PM_PARSER_R_OK;
2511 }
2512
2513 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
2514 {
2515         return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
2516                         __getuid());
2517 }
2518
2519 static int __unregister_all_pkg_update_info(sqlite3 *db)
2520 {
2521         static const char query[] =
2522                 "UPDATE package_update_info SET update_type='none'";
2523         int ret;
2524         sqlite3_stmt *stmt;
2525
2526         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2527         if (ret != SQLITE_OK) {
2528                 _LOGE("prepare failed: %s", sqlite3_errmsg(db));
2529                 return -1;
2530         }
2531
2532         ret = sqlite3_step(stmt);
2533         if (ret != SQLITE_DONE) {
2534                 _LOGE("step failed: %s", sqlite3_errmsg(db));
2535                 sqlite3_finalize(stmt);
2536                 return -1;
2537         }
2538
2539         sqlite3_finalize(stmt);
2540
2541         return 0;
2542 }
2543
2544 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
2545 {
2546         int ret;
2547         const char *dbpath;
2548         sqlite3 *db;
2549
2550         dbpath = __get_parser_db_path(uid);
2551
2552         ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
2553         if (ret != SQLITE_OK) {
2554                 _LOGE("open db failed: %d", ret);
2555                 return PM_PARSER_R_ERROR;
2556         }
2557
2558         __BEGIN_TRANSACTION(db);
2559         __DO_TRANSACTION(db, __unregister_all_pkg_update_info(db));
2560         __END_TRANSACTION(db);
2561
2562         sqlite3_close_v2(db);
2563
2564         return PM_PARSER_R_OK;
2565 }
2566
2567 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
2568 {
2569         return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
2570                         __getuid());
2571 }