2 * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
24 #include <sys/smack.h>
25 #include <sys/sysmacros.h>
26 #include <linux/limits.h>
34 #include <tzplatform_config.h>
35 #include <system_info.h>
37 #include "pkgmgr-info.h"
38 #include "pkgmgrinfo_basic.h"
39 #include "pkgmgr_parser.h"
40 #include "pkgmgr_parser_db_queries.h"
41 #include "pkgmgr_parser_debug.h"
42 #include "pkgmgr_parser_internal.h"
43 #include "manager/pkginfo_manager.h"
49 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
52 #define APPFW_USER "app_fw"
61 #define XXHDPI "xxhdpi"
71 #define XXHDPI_MIN 481
72 #define XXHDPI_MAX 600
74 /* app background category value */
75 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_VAL 0x00000
76 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_VAL 0x00001
77 #define APP_BG_CATEGORY_MEDIA_VAL 0x00002
78 #define APP_BG_CATEGORY_DOWNLOAD_VAL 0x00004
79 #define APP_BG_CATEGORY_BGNETWORK_VAL 0x00008
80 #define APP_BG_CATEGORY_LOCATION_VAL 0x00010
81 #define APP_BG_CATEGORY_SENSOR_VAL 0x00020
82 #define APP_BG_CATEGORY_IOTCOMM_VAL 0x00040
83 #define APP_BG_CATEGORY_SYSTEM_VAL 0x00080
85 #define APP_BG_CATEGORY_USER_DISABLE_FALSE_STR "enable"
86 #define APP_BG_CATEGORY_USER_DISABLE_TRUE_STR "disable"
87 #define APP_BG_CATEGORY_MEDIA_STR "media"
88 #define APP_BG_CATEGORY_DOWNLOAD_STR "download"
89 #define APP_BG_CATEGORY_BGNETWORK_STR "background-network"
90 #define APP_BG_CATEGORY_LOCATION_STR "location"
91 #define APP_BG_CATEGORY_SENSOR_STR "sensor"
92 #define APP_BG_CATEGORY_IOTCOMM_STR "iot-communication"
93 #define APP_BG_CATEGORY_SYSTEM "system"
95 #define REGULAR_USER 5000
96 static inline uid_t __getuid(void)
100 if (uid < REGULAR_USER)
101 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
106 #define __BEGIN_TRANSACTION(db) \
108 if (sqlite3_exec(db, "BEGIN DEFERRED", NULL, NULL, NULL) != \
110 _LOGE("begin transaction failed: %s", sqlite3_errmsg(db)); \
111 return PM_PARSER_R_ERROR; \
115 #define __DO_TRANSACTION(db, func) \
118 _LOGE("transaction failed: %s, rollback", sqlite3_errmsg(db)); \
119 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) != \
121 _LOGE("roll back transaction failed: %s", \
122 sqlite3_errmsg(db)); \
123 return PM_PARSER_R_ERROR; \
127 #define __END_TRANSACTION(db) \
129 if (sqlite3_exec(db, "COMMIT", NULL, NULL, NULL) != \
131 _LOGE("commit failed: %s, rollback", sqlite3_errmsg(db)); \
132 if (sqlite3_exec(db, "ROLLBACK", NULL, NULL, NULL) != \
134 _LOGE("roll back transaction failed: %s", \
135 sqlite3_errmsg(db)); \
136 return PM_PARSER_R_ERROR; \
140 static const char *__get_parser_db_path(uid_t uid)
145 if (uid == GLOBAL_USER || uid == OWNER_ROOT) {
146 path = tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db");
148 snprintf(buf, sizeof(buf), "user/%d/.pkgmgr_parser.db", uid);
149 path = tzplatform_mkpath(TZ_SYS_DB, buf);
155 static const char *__get_cert_db_path(void)
157 return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
160 #define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
161 static int __set_db_version(sqlite3 *db)
165 char version[PKG_STRING_LEN_MAX] = { 0 };
168 fp = fopen(DB_VERSION_PATH, "r");
169 retvm_if(fp == NULL, -1, "Failed to open db version file");
170 if (fgets(version, sizeof(version), fp) == NULL) {
171 _LOGE("Failed to get version information");
177 query = sqlite3_mprintf("PRAGMA user_version=%Q", version);
179 _LOGE("Out of memory");
183 ret = sqlite3_exec(db, query, NULL, NULL, NULL);
184 if (ret != SQLITE_OK) {
185 _LOGE("exec failed: %s", sqlite3_errmsg(db));
194 /* TODO: Do not labeling directly */
195 #define DB_LABEL "User::Home"
196 #define SET_SMACK_LABEL(x) \
198 if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
199 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
201 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
204 static int __set_db_permission(const char *path, uid_t uid)
207 const char *files[2];
208 char journal_file[BUFSIZE];
212 struct passwd *result;
217 if (getuid() != OWNER_ROOT)
220 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
221 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
222 if (result == NULL) {
224 _LOGE("no such user: %d", uid);
226 _LOGE("getpwuid_r failed: %d", errno);
232 snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
234 files[1] = journal_file;
236 ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
237 if (result == NULL) {
239 _LOGE("no such user: %d", uid);
241 _LOGE("getpwuid_r failed: %d", errno);
245 for (i = 0; i < 2; i++) {
246 fd = open(files[i], O_RDONLY);
248 _LOGE("open %s failed: %d", files[i], errno);
251 ret = fstat(fd, &sb);
253 _LOGE("stat %s failed: %d", files[i], errno);
257 if (S_ISLNK(sb.st_mode)) {
258 _LOGE("%s is symlink!", files[i]);
262 ret = fchown(fd, uid, pwd.pw_gid);
264 _LOGE("fchown %s failed: %d", files[i], errno);
269 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
270 if (!strcmp(path, __get_cert_db_path()))
272 ret = fchmod(fd, mode);
274 _LOGD("fchmod %s failed: %d", files[i], errno);
279 SET_SMACK_LABEL(files[i]);
285 static int __create_tables(sqlite3 *db, const char **queries)
289 for (i = 0; queries[i] != NULL; i++) {
290 ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
291 if (ret != SQLITE_OK) {
292 _LOGE("exec failed: %s", sqlite3_errmsg(db));
299 static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
301 const char **queries;
303 if (__set_db_version(db))
306 if (strstr(dbpath, ".pkgmgr_parser.db")) {
307 queries = PARSER_INIT_QUERIES;
308 } else if (strstr(dbpath, ".pkgmgr_cert.db")) {
309 queries = CERT_INIT_QUERIES;
311 _LOGE("unexpected dbpath: %s", dbpath);
315 __BEGIN_TRANSACTION(db);
316 __DO_TRANSACTION(db, __create_tables(db, queries));
317 __END_TRANSACTION(db);
319 if (__set_db_permission(dbpath, uid))
320 _LOGE("failed to set db permission");
325 #define RESOURCED_BUS_NAME "org.tizen.resourced"
326 #define RESOURCED_PROC_PATH "/Org/Tizen/ResourceD/Process"
327 #define RESOURCED_PROC_INTERFACE "org.tizen.resourced.process"
328 #define RESOURCED_PROC_METHOD "ProcExclude"
329 // This should be removed when the client server structure is complete
330 static void __send_wakeup_signal_to_resourced(pid_t pid)
332 GError *error = NULL;
333 GDBusConnection *conn;
337 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
339 _LOGE("Failed to connect to dbus: %s", error->message);
344 proxy = g_dbus_proxy_new_sync(conn,
345 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
346 NULL, RESOURCED_BUS_NAME,
347 RESOURCED_PROC_PATH, RESOURCED_PROC_INTERFACE,
350 _LOGE("failed to get proxy object: %s", error->message);
352 g_object_unref(conn);
356 reply = g_dbus_proxy_call_sync(proxy, RESOURCED_PROC_METHOD,
357 g_variant_new("(si)", "wakeup", pid),
358 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
360 _LOGE("failed to get reply from resourced");
362 _LOGE("failed to send request: %s", error->message);
366 g_object_unref(proxy);
367 g_object_unref(conn);
370 // This should be removed when the client server structure is complete
371 static void __check_db_lock(const char *dbpath)
380 unsigned long long ino;
381 char cmdline[BUFSIZE];
385 if (stat(dbpath, &sb) == -1) {
386 _LOGE("get db file(%s) status failed: %d", dbpath, errno);
390 fp = fopen("/proc/locks", "r");
392 _LOGE("Failed to open lock info: %d", errno);
396 while (fscanf(fp, "%*s %*s %*s %5s %d %x:%x:%llu %*s %*s",
397 type, &pid, &maj, &min, &ino) != EOF) {
398 if (maj != major(sb.st_dev) || min != minor(sb.st_dev) ||
399 ino != sb.st_ino || pid == getpid() ||
400 strcasecmp(type, "WRITE"))
403 snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid);
404 fp_cmdline = fopen(cmdline, "r");
406 if (fp_cmdline != NULL) {
407 len = fread(name, sizeof(char), sizeof(name) - 1,
410 if (name[len - 1] == '\n')
411 name[len - 1] = '\0';
418 _LOGE("%s (%d) has lock on pkgmgr db(%s)!", name, pid, dbpath);
419 __send_wakeup_signal_to_resourced(pid);
425 #define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
426 #define BUSY_WAITING_MAX 100 /* wait for max 5 sec */
428 // This should be removed when the client server structure is complete
429 static int __db_busy_handler(void *data, int count)
431 if (count < (BUSY_WAITING_MAX / 2)) {
432 usleep(BUSY_WAITING_USEC);
434 } else if (count == (BUSY_WAITING_MAX / 2)) {
435 __check_db_lock((const char *)data);
436 usleep(BUSY_WAITING_USEC);
438 } else if (count < BUSY_WAITING_MAX) {
439 usleep(BUSY_WAITING_USEC);
442 /* sqlite3_prepare_v2 will return SQLITE_BUSY */
447 API int pkgmgr_parser_initialize_parser_db(uid_t uid)
453 dbpath = __get_parser_db_path(uid);
454 if (access(dbpath, F_OK) != -1) {
455 _LOGE("Manifest db for user %d is already exists", uid);
456 return PM_PARSER_R_ERROR;
459 ret = sqlite3_open_v2(dbpath, &db,
460 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
461 if (ret != SQLITE_OK) {
462 _LOGE("open db failed: %d", ret);
463 sqlite3_close_v2(db);
464 return PM_PARSER_R_ERROR;
467 ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
468 if (ret != SQLITE_OK) {
469 _LOGE("failed to register busy handler: %s",
471 sqlite3_close_v2(db);
475 if (__initialize_db(db, dbpath, uid)) {
476 sqlite3_close_v2(db);
477 return PM_PARSER_R_ERROR;
479 sqlite3_close_v2(db);
481 return PM_PARSER_R_OK;
484 API int pkgmgr_parser_initialize_cert_db(void)
490 dbpath = __get_cert_db_path();
491 if (access(dbpath, F_OK) != -1) {
492 _LOGE("Cert db is already exists");
493 return PM_PARSER_R_ERROR;
496 ret = sqlite3_open_v2(dbpath, &db,
497 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
498 if (ret != SQLITE_OK) {
499 _LOGE("open db failed: %d", ret);
500 sqlite3_close_v2(db);
501 return PM_PARSER_R_ERROR;
504 ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
505 if (ret != SQLITE_OK) {
506 _LOGE("failed to register busy handler: %s",
508 sqlite3_close_v2(db);
512 if (__initialize_db(db, dbpath, GLOBAL_USER)) {
513 sqlite3_close_v2(db);
514 return PM_PARSER_R_ERROR;
516 sqlite3_close_v2(db);
518 return PM_PARSER_R_OK;
521 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
525 struct passwd *result;
528 ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
529 if (result == NULL) {
531 _LOGE("no such user: %s", APPFW_USER);
533 _LOGE("getpwnam_r failed: %d", errno);
534 return PM_PARSER_R_ERROR;
537 if (getuid() != OWNER_ROOT && getuid() != pwd.pw_uid) {
538 _LOGE("Only root or app_fw user is allowed");
539 return PM_PARSER_R_EINVAL;
542 if (pkgmgr_parser_initialize_parser_db(uid))
543 return PM_PARSER_R_ERROR;
545 if (uid == OWNER_ROOT || uid == GLOBAL_USER)
546 if (pkgmgr_parser_initialize_cert_db())
547 return PM_PARSER_R_ERROR;
549 return PM_PARSER_R_OK;
552 /* TODO: move to installer */
554 /* TODO: refactor inserting localized info */
556 static int __insert_package_plugin_execution_info(
557 manifest_x *mfx, uid_t uid)
561 char **queries = NULL;
566 return PM_PARSER_R_OK;
568 queries = calloc(g_list_length(mfx->plugin), sizeof (char *));
569 if (queries == NULL) {
570 _LOGE("Out of memory");
571 return PM_PARSER_R_ERROR;
574 for (tmp = mfx->plugin; tmp; tmp = tmp->next) {
575 plugin = (plugin_x *)tmp->data;
579 queries[idx] = sqlite3_mprintf("INSERT INTO package_plugin_info "
580 "(pkgid, appid, plugin_type, plugin_name) "
581 "VALUES (%Q, %Q, %Q, %Q)",
582 plugin->pkgid, plugin->appid,
583 plugin->plugin_type, plugin->plugin_name);
584 if (queries[idx] == NULL) {
585 _LOGE("Out of memory");
586 for (i = 0; i < idx; ++i)
587 sqlite3_free(queries[i]);
589 return PM_PARSER_R_ERROR;
595 if (_parser_execute_write_queries((const char **)queries, idx, uid) < 0) {
596 _LOGE("Fail to write to db");
597 for (i = 0; i < idx; ++i)
598 sqlite3_free(queries[i]);
600 return PM_PARSER_R_ERROR;
603 for (i = 0; i < idx; ++i)
604 sqlite3_free(queries[i]);
607 return PM_PARSER_R_OK;
610 static int __delete_package_plugin_execution_info(const char *pkgid, uid_t uid)
614 query = sqlite3_mprintf(
615 "DELETE FROM package_plugin_info WHERE pkgid=%Q", pkgid);
617 _LOGE("Out of memory");
618 return PM_PARSER_R_ERROR;
621 if (_parser_execute_write_query(query, uid) < 0) {
622 _LOGE("Fail to write to db");
624 return PM_PARSER_R_ERROR;
628 return PM_PARSER_R_OK;
631 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
633 if (_parser_insert_manifest_info(mfx, uid) < 0) {
634 _LOGE("Fail to insert manifest uid[%d]", uid);
635 return PM_PARSER_R_ERROR;
638 return PM_PARSER_R_OK;
641 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
643 return pkgmgr_parser_insert_manifest_info_in_usr_db(mfx, __getuid());
646 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx,
649 if (_parser_delete_manifest_info(mfx, uid) < 0) {
650 _LOGE("Fail to update manifest uid[%d]", uid);
651 return PM_PARSER_R_ERROR;
654 return PM_PARSER_R_OK;
657 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
659 return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, __getuid());
662 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
664 if (_parser_update_manifest_info(mfx, uid) < 0) {
665 _LOGE("Fail to update manifest uid[%d]", uid);
666 return PM_PARSER_R_ERROR;
669 return PM_PARSER_R_OK;
672 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
674 return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, __getuid());
677 API int pkgmgr_parser_update_global_app_disable_for_uid_info_in_db(
678 const char *appid, uid_t uid, int is_disable)
680 // TODO: construct query at here and send it to server
683 query = sqlite3_mprintf("INSERT OR REPLACE INTO package_app_info_for_uid ("
684 " app_id, uid, is_disabled, is_splash_screen_enabled) "
685 "VALUES (%Q, %u, %Q,"
686 " (SELECT app_splash_screen_display FROM package_app_info"
687 " WHERE app_id=%Q))", appid, uid,
688 (is_disable ? "true" : "false"), appid);
690 _LOGE("Out of memory");
691 return PM_PARSER_R_ERROR;
694 if (_parser_execute_write_query(query, uid) < 0) {
695 _LOGE("Fail to write to db");
697 return PM_PARSER_R_ERROR;
701 return PM_PARSER_R_OK;
704 API int pkgmgr_parser_update_app_disable_info_in_usr_db(const char *appid,
705 uid_t uid, int is_disable)
709 query = sqlite3_mprintf("UPDATE package_app_info SET app_disable=%Q "
710 "WHERE app_id=%Q", (is_disable ? "true" : "false"), appid);
712 _LOGE("Out of memory");
713 return PM_PARSER_R_ERROR;
716 if (_parser_execute_write_query(query, uid) < 0) {
717 _LOGE("Fail to write to db");
719 return PM_PARSER_R_ERROR;
723 return PM_PARSER_R_OK;
726 API int pkgmgr_parser_update_app_disable_info_in_db(const char *appid,
729 return pkgmgr_parser_update_app_disable_info_in_usr_db(appid,
730 __getuid(), is_disable);
733 API int pkgmgr_parser_update_pkg_disable_info_in_usr_db(const char *pkgid,
734 uid_t uid, int is_disable)
738 query = sqlite3_mprintf("UPDATE package_info SET package_disable=%Q "
739 "WHERE package=%Q", (is_disable ? "true" : "false"), pkgid);
741 _LOGE("Out of memory");
742 return PM_PARSER_R_ERROR;
745 if (_parser_execute_write_query(query, uid) < 0) {
746 _LOGE("Fail to write to db");
748 return PM_PARSER_R_ERROR;
752 return PM_PARSER_R_OK;
755 API int pkgmgr_parser_update_pkg_disable_info_in_db(const char *pkgid,
758 return pkgmgr_parser_update_pkg_disable_info_in_usr_db(pkgid,
759 __getuid(), is_disable);
762 API int pkgmgr_parser_update_global_app_splash_screen_display_info_in_usr_db(
763 const char *appid, uid_t uid, int flag)
767 query = sqlite3_mprintf("INSERT OR REPLACE INTO package_app_info_for_uid("
768 " app_id, uid, is_splash_screen_enabled) "
769 "VALUES (%Q, %u, %Q)", appid, uid, (flag == 1 ? "true" : "false"));
771 _LOGE("Out of memory");
772 return PM_PARSER_R_ERROR;
775 if (_parser_execute_write_query(query, uid) < 0) {
776 _LOGE("Fail to write to db");
778 return PM_PARSER_R_ERROR;
782 return PM_PARSER_R_OK;
785 API int pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
786 const char *appid, uid_t uid, int flag)
790 query = sqlite3_mprintf("UPDATE package_app_info SET app_splash_screen_display=%Q "
791 "WHERE app_id=%Q", (flag == 1 ? "true" : "false"), appid);
793 _LOGE("Out of memory");
794 return PM_PARSER_R_ERROR;
797 if (_parser_execute_write_query(query, uid) < 0) {
798 _LOGE("Fail to write to db");
800 return PM_PARSER_R_ERROR;
804 return PM_PARSER_R_OK;
807 API int pkgmgr_parser_update_app_splash_screen_display_info_in_db(
808 const char *appid, int flag)
810 return pkgmgr_parser_update_app_splash_screen_display_info_in_usr_db(
811 appid, __getuid(), flag);
814 API int pkgmgr_parser_update_app_label_info_in_usr_db(const char *appid,
815 uid_t uid, const char *label)
819 query = sqlite3_mprintf("UPDATE package_app_localized_info SET app_label=%Q "
820 "WHERE app_id=%Q AND app_label IS NOT NULL", label, appid);
822 _LOGE("Out of memory");
823 return PM_PARSER_R_ERROR;
826 if (_parser_execute_write_query(query, uid) < 0) {
827 _LOGE("Fail to write to db");
829 return PM_PARSER_R_ERROR;
833 return PM_PARSER_R_OK;
836 API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
839 return pkgmgr_parser_update_app_label_info_in_usr_db(appid, __getuid(),
843 API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid,
844 uid_t uid, const char *icon_path)
848 query = sqlite3_mprintf("UPDATE package_app_localized_info SET app_icon=%Q "
849 "WHERE app_id=%Q AND app_icon IS NOT NULL", icon_path, appid);
851 _LOGE("Out of memory");
852 return PM_PARSER_R_ERROR;
855 if (_parser_execute_write_query(query, uid) < 0) {
856 _LOGE("Fail to write to db");
858 return PM_PARSER_R_ERROR;
862 return PM_PARSER_R_OK;
865 API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid,
866 const char *icon_path)
868 return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(),
872 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid,
873 const char *tep_path, uid_t uid)
877 query = sqlite3_mprintf("UPDATE package_info SET package_tep_name=%Q "
878 "WHERE package=%Q", tep_path, pkgid);
880 _LOGE("Out of memory");
881 return PM_PARSER_R_ERROR;
884 if (_parser_execute_write_query(query, uid) < 0) {
885 _LOGE("Fail to write to db");
887 return PM_PARSER_R_ERROR;
891 return PM_PARSER_R_OK;
894 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid,
895 const char *tep_path)
897 return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path,
901 static int __convert_update_type(pkgmgrinfo_updateinfo_update_type type,
902 const char **update_type)
904 if (type == PMINFO_UPDATEINFO_NONE)
905 *update_type = PMINFO_UPDATEINFO_TYPE_NONE;
906 else if (type == PMINFO_UPDATEINFO_FORCE)
907 *update_type = PMINFO_UPDATEINFO_TYPE_FORCE;
908 else if (type == PMINFO_UPDATEINFO_OPTIONAL)
909 *update_type = PMINFO_UPDATEINFO_TYPE_OPTIONAL;
915 static int __register_pkg_update_info(updateinfo_x *info,
916 const char *update_type, uid_t uid)
920 query = sqlite3_mprintf("UPDATE package_update_info "
921 "SET update_version=%Q, update_type=%Q "
922 "WHERE package=%Q", info->version, update_type, info->pkgid);
924 _LOGE("Out of memory");
925 return PM_PARSER_R_ERROR;
928 if (_parser_execute_write_query(query, uid) < 0) {
929 _LOGE("Fail to write to db");
931 return PM_PARSER_R_ERROR;
935 return PM_PARSER_R_OK;
938 API int pkgmgr_parser_register_pkg_update_info_in_usr_db(
939 pkgmgrinfo_updateinfo_h handle, uid_t uid)
942 updateinfo_x *update_info;
943 updateinfo_x *prev_update_info;
944 pkgmgrinfo_updateinfo_h prev_update_handle;
945 pkgmgrinfo_pkginfo_h pkginfo;
946 pkgmgrinfo_version_compare_type compare_result;
948 const char *update_type;
950 if (handle == NULL) {
951 _LOGE("invalid parameter");
952 return PM_PARSER_R_EINVAL;
955 update_info = (updateinfo_x *)handle;
956 if (update_info->pkgid == NULL || update_info->version == NULL)
957 return PM_PARSER_R_EINVAL;
958 if (__convert_update_type(update_info->type, &update_type) != 0)
959 return PM_PARSER_R_EINVAL;
961 ret = pkgmgrinfo_updateinfo_get_usr_updateinfo(update_info->pkgid,
962 &prev_update_handle, uid);
963 if (ret != PMINFO_R_OK)
964 return PM_PARSER_R_ERROR;
966 prev_update_info = (updateinfo_x *)prev_update_handle;
967 ret = pkgmgrinfo_compare_package_version(update_info->version,
968 prev_update_info->version, &compare_result);
969 if (ret != PMINFO_R_OK) {
970 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
971 return PM_PARSER_R_ERROR;
974 if (compare_result == PMINFO_VERSION_SAME &&
975 prev_update_info->type == PMINFO_UPDATEINFO_NONE) {
976 _LOGI("Given update info version[%s] of pkgid[%s] "
978 update_info->version, update_info->pkgid);
979 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
980 return PM_PARSER_R_OK;
982 pkgmgrinfo_updateinfo_destroy(prev_update_handle);
984 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(update_info->pkgid, uid,
986 if (ret != PMINFO_R_OK)
987 return PM_PARSER_R_ERROR;
989 ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
990 if (ret != PMINFO_R_OK) {
991 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
992 return PM_PARSER_R_ERROR;
994 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
996 return __register_pkg_update_info(update_info, update_type, uid);
999 API int pkgmgr_parser_register_pkg_update_info_in_db(
1000 pkgmgrinfo_updateinfo_h handle)
1002 return pkgmgr_parser_register_pkg_update_info_in_usr_db(handle,
1006 static int __unregister_pkg_update_info(const char *pkgid, uid_t uid)
1010 query = sqlite3_mprintf("UPDATE package_update_info "
1011 "SET update_type='none' WHERE package=%Q", pkgid);
1012 if (query == NULL) {
1013 _LOGE("Out of memory");
1014 return PM_PARSER_R_ERROR;
1017 if (_parser_execute_write_query(query, uid) < 0) {
1018 _LOGE("Fail to write to db");
1019 sqlite3_free(query);
1020 return PM_PARSER_R_ERROR;
1022 sqlite3_free(query);
1024 return PM_PARSER_R_OK;
1027 API int pkgmgr_parser_unregister_pkg_update_info_in_usr_db(const char *pkgid,
1031 pkgmgrinfo_pkginfo_h pkginfo;
1035 if (pkgid == NULL) {
1036 _LOGE("invalid parameter");
1037 return PM_PARSER_R_EINVAL;
1040 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
1041 if (ret != PMINFO_R_OK)
1042 return PM_PARSER_R_EINVAL;
1044 ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
1045 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
1046 if (ret != PMINFO_R_OK)
1047 return PM_PARSER_R_ERROR;
1049 target_uid = is_global_pkg ? GLOBAL_USER : uid;
1051 // TODO: what we do with is_global_pkg?
1052 return __unregister_pkg_update_info(pkgid, target_uid);
1055 API int pkgmgr_parser_unregister_pkg_update_info_in_db(const char *pkgid)
1057 return pkgmgr_parser_unregister_pkg_update_info_in_usr_db(pkgid,
1061 API int pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(uid_t uid)
1065 query = sqlite3_mprintf("UPDATE package_update_info "
1066 "SET update_type='none'");
1067 if (query == NULL) {
1068 _LOGE("Out of memory");
1069 return PM_PARSER_R_ERROR;
1072 if (_parser_execute_write_query(query, uid) < 0) {
1073 _LOGE("Fail to write to db");
1074 sqlite3_free(query);
1075 return PM_PARSER_R_ERROR;
1077 sqlite3_free(query);
1079 return PM_PARSER_R_OK;
1082 API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
1084 return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
1088 API int pkgmgr_parser_register_pkg_plugin_info_in_usr_db(
1089 manifest_x *mfx, uid_t uid)
1091 return __insert_package_plugin_execution_info(mfx, uid);
1094 API int pkgmgr_parser_register_pkg_plugin_info_in_db(manifest_x *mfx)
1096 return pkgmgr_parser_register_pkg_plugin_info_in_usr_db(mfx, __getuid());
1099 API int pkgmgr_parser_update_pkg_plugin_info_in_usr_db(
1100 manifest_x *mfx, uid_t uid)
1102 // TODO: construct query and send to server
1105 ret = __delete_package_plugin_execution_info(mfx->package, uid);
1106 if (ret != PM_PARSER_R_OK)
1108 ret = __insert_package_plugin_execution_info(mfx, uid);
1109 if (ret != PM_PARSER_R_OK)
1112 return PM_PARSER_R_OK;
1115 API int pkgmgr_parser_update_pkg_plugin_info_in_db(manifest_x *mfx)
1117 return pkgmgr_parser_update_pkg_plugin_info_in_usr_db(mfx, __getuid());
1120 API int pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(
1121 const char *pkgid, uid_t uid)
1123 return __delete_package_plugin_execution_info(pkgid, uid);
1126 API int pkgmgr_parser_unregister_pkg_plugin_info_in_db(const char *pkgid)
1128 return pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid, __getuid());
1131 API int pkgmgr_parser_clear_cache_memory_db()
1133 return pkgmgr_parser_clear_cache_usr_memory_db(__getuid());
1136 API int pkgmgr_parser_clear_cache_usr_memory_db(uid_t uid)
1138 return _parser_clear_cache_memory_db(uid);