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