2 * Copyright (c) 2019 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.
23 #include <json-glib/json-glib.h>
24 #include <pkgmgr-info.h>
26 #include <tzplatform_config.h>
27 #include <package_manager.h>
29 #include <sys/types.h>
35 #define EXPORT_API __attribute__((visibility("default")))
41 #define LOG_TAG "STICKER_PARSER"
43 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
44 #define STICKER_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_info(sticker_info_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id TEXT NOT NULL, type INTEGER NOT NULL, uri TEXT NOT NULL, thumbnail TEXT, description TEXT, group_name TEXT NOT NULL, date TEXT NOT NULL);"
45 #define STICKER_KEYWORD_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_keyword_info(keyword_id INTEGER PRIMARY KEY AUTOINCREMENT, sticker_info_id INTEGER, keyword TEXT NOT NULL, FOREIGN KEY (sticker_info_id) REFERENCES sticker_info(sticker_info_id) ON DELETE CASCADE)"
48 #define MAX_ERROR_BUFFER 256
50 static char error_buffer[MAX_ERROR_BUFFER];
51 static gboolean is_corrupted = FALSE;
53 typedef struct metadata {
58 static char* __get_string_from_object(JsonObject *object, const char *key)
60 if (json_object_has_member(object, key) == false)
63 const char *str = json_object_get_string_member(object, key);
70 static int __get_int_from_object(JsonObject *object, const char *key)
72 if (json_object_has_member(object, key) == false)
75 int type = json_object_get_int_member(object, key);
80 static void __recover_db()
85 const char *db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
87 LOGD("Start to recover sticker db");
88 if (unlink(db_path) == -1)
89 LOGE("Failed to remove db file");
91 ret = sqlite3_open_v2(db_path, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
92 if (ret != SQLITE_OK) {
93 LOGE("Failed to open db : %s", sqlite3_errmsg(db));
94 if (unlink(db_path) == -1)
95 LOGE("Failed to remove db file");
99 ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
100 if (ret != SQLITE_OK) {
101 LOGE("Failed to create sticker_info table : %s" , err);
105 ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
106 if (ret != SQLITE_OK) {
107 LOGE("Failed to create sticker_keyword_info table : %s", err);
111 is_corrupted = FALSE;
121 static int __integrity_check_cb(void *pid, int argc, char **argv, char **notUsed)
123 if (strcmp(argv[0], "ok") != 0) {
124 LOGE("DB integrity check failed : %s", argv[0]);
129 LOGD("Result integrity : %s", argv[0]);
133 static void __db_init()
138 const char *db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
140 ret = sqlite3_open_v2(db_path, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
141 if (ret != SQLITE_OK) {
142 LOGE("Failed to open db : %s", sqlite3_errmsg(db));
146 ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
147 if (ret != SQLITE_OK) {
148 LOGE("Failed to create sticker_info table : %s" , err);
152 ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
153 if (ret != SQLITE_OK) {
154 LOGE("Failed to create sticker_keyword_info table : %s", err);
158 ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err);
159 if (ret != SQLITE_OK) {
160 LOGE("Failed to set journal_mode : %s", err);
164 ret = sqlite3_exec(db, "PRAGMA integrity_check", __integrity_check_cb, NULL, &err);
165 if (ret != SQLITE_OK)
166 LOGE("Failed to check integrity : %s", err);
175 if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB || is_corrupted)
179 static sqlite3 *__db_open(const char *path)
185 ret = sqlite3_open(path, &db);
186 if (ret != SQLITE_OK) {
187 LOGE("Failed to open db : %s", sqlite3_errmsg(db));
191 ret = sqlite3_exec(db, "PRAGMA foreign_keys = ON", NULL, NULL, &err);
192 if (ret != SQLITE_OK) {
193 LOGE("Failed to turn on foreign keys : %s", err);
202 static int __change_ownership(const char *path, int user, int group)
205 uid_t uid = (uid_t)user;
206 gid_t gid = (gid_t)group;
208 ret = chown(path, uid, gid);
210 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
211 LOGE("chown() failed : %s", error_buffer);
217 static int __remove_app_path(char *uri, const char *app_path)
221 for (int i = 0; uri[i] != '\0'; i++) {
222 if (uri[i] == app_path[n]) {
224 while (uri[i] == app_path[n]) {
225 if (app_path[++n] == '\0') {
226 for(; uri[k + n] != '\0'; k++) {
241 static int __mkdirs(const char *path, mode_t mode)
244 char prev_path[2048];
245 const char *tmp = path;
247 if (!path || strlen(path) > 2048)
250 memset(prev_path, '\0', 2048);
251 while ((tmp = strchr(tmp, '/')) != NULL) {
258 strncpy(prev_path, path, len);
259 prev_path[len] = 0x00;
261 if (mkdir(prev_path, mode) == -1) {
262 if (errno != EEXIST) {
263 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
264 LOGE("directory create error : %s", error_buffer);
268 if (__change_ownership(prev_path, UIFW_ID, UIFW_ID) != 0)
269 LOGE("failed to change ownership");
273 if (mkdir(prev_path, mode) == -1) {
274 if (errno != EEXIST) {
275 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
276 LOGE("directory create error : %s", error_buffer);
280 if (__change_ownership(prev_path, UIFW_ID, UIFW_ID) != 0)
281 LOGE("failed to change ownership");
286 static int __file_copy(const char *src, const char *dest)
289 int fd = -1, n_fd = -1;
294 memset(buf, '\0', 4096);
295 fd = open(src, O_RDONLY);
296 n_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0755);
298 if (fd == -1 || n_fd == -1) {
304 while((size = read(fd, buf, 4096))) {
314 while(write(n_fd, buf, size) == -1) {
335 static char* __convert_sticker_uri(const char *uri, const char *appid, const char *app_path)
338 char *rel_path = strdup(uri);
340 __remove_app_path(rel_path, app_path);
341 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(rel_path) + 2;
342 char *new_path = (char *)calloc(len, sizeof(char));
343 if (new_path == NULL) {
349 snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, rel_path);
351 if (access(new_path, F_OK) == 0) {
352 LOGE("sticker file already exists");
357 ret = __mkdirs(new_path, 0755);
359 LOGE("directory create error");
363 if (__file_copy(uri, new_path) == -1) {
364 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
365 LOGE("failed to copy sticker file : %s", error_buffer);
372 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
373 LOGE("failed to remove sticker file : %s", error_buffer);
376 if (__change_ownership(new_path, UIFW_ID, UIFW_ID) != 0)
377 LOGE("failed to change ownership");
392 static void __insert_sticker_info(const char *app_id, int type, const char *uri, const char *group, const char *thumbnail, const char *description)
396 sqlite3_stmt *stmt = NULL;
399 db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
400 db = __db_open(db_path);
404 ret = sqlite3_prepare_v2(db, "INSERT INTO sticker_info (app_id, type, uri, thumbnail, description, group_name, date) VALUES (?, ?, ?, ?, ?, ?, DateTime('now','localtime'))",-1, &stmt, NULL);
405 if (ret == SQLITE_OK) {
406 sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
407 sqlite3_bind_int(stmt, 2, type);
408 sqlite3_bind_text(stmt, 3, uri, -1, SQLITE_TRANSIENT);
409 sqlite3_bind_text(stmt, 4, thumbnail, -1, SQLITE_TRANSIENT);
410 sqlite3_bind_text(stmt, 5, description, -1, SQLITE_TRANSIENT);
411 sqlite3_bind_text(stmt, 6, group, -1, SQLITE_TRANSIENT);
413 ret = sqlite3_step(stmt);
414 if (ret != SQLITE_OK && ret != SQLITE_DONE)
415 LOGE("sqlite3_step() failed : ret(%d)", ret);
417 if (sqlite3_changes(db) == 0)
418 LOGE("No changes to DB");
420 sqlite3_finalize(stmt);
423 LOGE("failed to insert sticker information : %s", sqlite3_errmsg(db));
424 sqlite3_finalize(stmt);
431 static void __insert_sticker_keyword_info(const char *keyword)
436 sqlite3_stmt *stmt = NULL;
439 db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
440 db = __db_open(db_path);
444 ret = sqlite3_prepare_v2(db, "SELECT sticker_info_id FROM sticker_info ORDER BY sticker_info_id DESC LIMIT 1", -1, &stmt, NULL);
445 if (ret == SQLITE_OK) {
446 ret = sqlite3_step(stmt);
447 if (ret == SQLITE_ERROR) {
448 LOGE("sqlite3_step() failed : ret(%d)", ret);
449 sqlite3_finalize(stmt);
452 record_id = sqlite3_column_int(stmt, 0);
454 sqlite3_finalize(stmt);
457 ret = sqlite3_prepare_v2(db, "INSERT INTO sticker_keyword_info (sticker_info_id, keyword) VALUES (?, ?)", -1, &stmt, NULL);
458 if (ret == SQLITE_OK) {
459 sqlite3_bind_int(stmt, 1, record_id);
460 sqlite3_bind_text(stmt, 2, keyword, -1, SQLITE_TRANSIENT);
462 ret = sqlite3_step(stmt);
463 if (ret != SQLITE_OK && ret != SQLITE_DONE)
464 LOGE("sqlite3_step() failed : ret(%d)", ret);
466 if (sqlite3_changes(db) == 0)
467 LOGE("No changes to DB");
469 sqlite3_finalize(stmt);
472 LOGE("fail to insert sticker keyword : %s", sqlite3_errmsg(db));
473 sqlite3_finalize(stmt);
478 LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
479 sqlite3_finalize(stmt);
486 static int __get_sticker_info_from_json(const char *appid, const char *file_path, const char *app_path)
489 JsonParser* parser = NULL;
490 GError* err_msg = NULL;
493 char *description = NULL;
494 char *thumbnail = NULL;
495 char *uri_path = NULL;
497 parser = json_parser_new();
498 json_parser_load_from_file(parser, file_path, &err_msg);
500 LOGE("failed to load json file. error message: %s", err_msg->message);
505 JsonNode *root = json_parser_get_root(parser);
507 LOGE("failed to get root");
512 JsonObject *root_obj = json_node_get_object(root);
513 if (root_obj == NULL) {
514 LOGE("failed to get object");
519 JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
520 if (sticker_arr == NULL) {
521 LOGE("failed to get array member");
526 int arr_len = json_array_get_length(sticker_arr);
527 for (int i = 0; i < arr_len; i++) {
528 JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
529 if (info_object != NULL) {
530 int type = __get_int_from_object(info_object, "type");
534 uri = __get_string_from_object(info_object, "uri");
538 group = __get_string_from_object(info_object, "group");
542 thumbnail = __get_string_from_object(info_object, "thumbnail");
543 description = __get_string_from_object(info_object, "description");
545 JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
546 int keyword_arr_len = json_array_get_length(keyword_arr);
547 if (keyword_arr_len < 1)
551 if (access(uri, F_OK) == 0) {
552 char *new_uri = __convert_sticker_uri(uri, appid, app_path);
555 __insert_sticker_info(appid, type, new_uri, group, thumbnail, description);
562 int path_len = strlen(app_path) + strlen(uri) + 2;
563 uri_path = (char *)calloc(path_len, sizeof(char));
565 LOGE("failed to alloc memory");
570 snprintf(uri_path, path_len, "%s%s",app_path, uri);
572 snprintf(uri_path, path_len, "%s%s%s",app_path, "/", uri);
574 if (access(uri_path, F_OK) == 0) {
575 char *new_uri = __convert_sticker_uri(uri_path, appid, app_path);
581 __insert_sticker_info(appid, type, new_uri, group, thumbnail, description);
589 LOGE("%s does not exist", uri_path);
596 __insert_sticker_info(appid, type, uri, group, thumbnail, description);
599 for (int j = 0; j < keyword_arr_len; j++) {
600 __insert_sticker_keyword_info(json_array_get_string_element(keyword_arr, j));
628 g_error_free(err_msg);
631 g_object_unref(parser);
636 static void __delete_sticker_info(const char *db_path, int record_id)
640 sqlite3_stmt *stmt = NULL;
642 db = __db_open(db_path);
646 ret = sqlite3_prepare_v2(db, "DELETE FROM sticker_info WHERE sticker_info_id = ?", -1, &stmt, NULL);
647 if (ret != SQLITE_OK) {
648 LOGE("failed to delete sticker information : %s", sqlite3_errmsg(db));
649 sqlite3_finalize(stmt);
654 sqlite3_bind_int(stmt, 1, record_id);
656 ret = sqlite3_step(stmt);
657 if (ret != SQLITE_OK && ret != SQLITE_DONE) {
658 LOGE("sqlite3_step() failed : ret(%d)", ret);
662 if (sqlite3_changes(db) == 0) {
663 LOGE("No changes to DB");
667 sqlite3_finalize(stmt);
672 sqlite3_finalize(stmt);
677 static int __remove_directory(const char *path, int is_error_stop)
680 struct dirent *file = NULL;
684 memset(filename, '\0', 1024);
686 if (__change_ownership(path, APPFW_ID, APPFW_ID) != 0)
687 LOGE("failed to change ownership");
689 dir_ptr = opendir(path);
693 while ((file = readdir(dir_ptr)) != NULL) {
694 if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
697 snprintf(filename, strlen(path) + strlen(file->d_name) + 2, "%s/%s", path, file->d_name);
699 if (lstat(filename, &buf) == -1)
702 if (S_ISDIR(buf.st_mode)) {
703 if (__remove_directory(filename, is_error_stop) == -1 && is_error_stop) {
707 } else if (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode)) {
708 if (unlink(filename) == -1 && is_error_stop) {
720 int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
722 LOGD("METADATA INSTALL");
723 LOGD("pkgid: %s, appid: %s", pkgid, appid);
726 GList *md_list = NULL;
727 package_info_h package_info = NULL;
728 char *app_path = NULL;
729 char *file_path = NULL;
734 ret = package_info_create(pkgid, &package_info);
735 if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
736 LOGE("failed to create package_info. ret: %d", ret);
740 ret = package_info_get_root_path(package_info, &app_path);
741 if (ret != PACKAGE_MANAGER_ERROR_NONE || app_path == NULL) {
742 LOGE("failed to create package_info. ret: %d", ret);
746 for(md_list = g_list_first(list); md_list != NULL; md_list = md_list->next) {
747 md = (metadata *)md_list->data;
748 int path_len = strlen(app_path) + strlen((char *)md->value) + 2;
749 file_path = (char *)calloc(path_len, sizeof(char));
751 LOGE("failed to alloc memory");
755 if ((char)md->value[0] == '/')
756 snprintf(file_path, path_len, "%s%s",app_path, (char *)md->value);
758 snprintf(file_path, path_len, "%s%s%s",app_path, "/", (char *)md->value);
760 if (__get_sticker_info_from_json(appid, file_path, app_path) == 0)
761 LOGE("failed to get sticker information [path : %s]", file_path);
769 package_info_destroy(package_info);
780 int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
782 LOGD("METADATA UNINSTALL");
783 LOGD("pkgid: %s, appid: %s", pkgid, appid);
788 sqlite3_stmt *stmt = NULL;
790 GList *id_list = NULL;
792 db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
793 db = __db_open(db_path);
797 ret = sqlite3_prepare_v2(db, "SELECT sticker_info_id from sticker_info WHERE app_id = ?", -1, &stmt, NULL);
798 if (ret == SQLITE_OK) {
799 sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
800 while (sqlite3_step(stmt) == SQLITE_ROW) {
801 const unsigned char *record_id = sqlite3_column_text(stmt, 0);
803 id_list = g_list_append(id_list, strdup((const char *)record_id));
806 sqlite3_finalize(stmt);
809 for(GList *tmp = g_list_first(id_list); tmp != NULL; tmp = tmp->next) {
810 del_id = atoi((const char*)tmp->data);
811 __delete_sticker_info(db_path, del_id);
815 g_list_free_full(id_list, free);
817 LOGE("failed to get sticker id");
818 sqlite3_finalize(stmt);
826 int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
828 LOGD("METADATA UPGRADE");
829 LOGD("pkgid: %s, appid: %s", pkgid, appid);
831 PKGMGR_MDPARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
832 PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgid, appid, list);
837 int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list)
839 LOGD("STICKER CATEGORY INSTALL");
840 LOGD("pkgid: %s, appid: %s", pkgid, appid);
842 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + 2;
843 char *dir_path = (char *)calloc(len, sizeof(char));
845 if (dir_path != NULL) {
846 snprintf(dir_path, len, "%s%s%s", STICKER_DIRECTORY, "/", appid);
847 LOGD("directory path : %s", dir_path);
849 if (mkdir(dir_path, 0755) == -1 && errno != EEXIST) {
850 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
851 LOGE("directory create error : %s", error_buffer);
853 if (__change_ownership(dir_path, UIFW_ID, UIFW_ID) != 0)
854 LOGE("failed to change ownership");
865 int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list)
867 LOGD("STICKER CATEGORY UNINSTALL");
868 LOGD("pkgid: %s, appid: %s", pkgid, appid);
870 int len = strlen(STICKER_DIRECTORY) + strlen(appid) + 2;
871 char * dir_path = (char *)calloc(len, sizeof(char));
873 if (dir_path != NULL) {
874 snprintf(dir_path, len, "%s%s%s", STICKER_DIRECTORY, "/", appid);
875 LOGD("directory path : %s", dir_path);
877 if (__remove_directory(dir_path, 0) < 0) {
878 strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
879 LOGE("directory remove error : %s", error_buffer);
890 int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
892 LOGD("STICKER CATEGORY UPGRADE");
893 LOGD("pkgid: %s, appid: %s", pkgid, appid);
895 PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(pkgid, appid, list);
896 PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(pkgid, appid, list);