#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
+#include <linux/limits.h>
#ifndef EXPORT_API
#define EXPORT_API __attribute__((visibility("default")))
#endif
#define LOG_TAG "STICKER_PARSER"
-#define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
-#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);"
+#define STICKER_DIRECTORY "/opt/usr/share/sticker-data"
+#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, display_type INTEGER)"
#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)"
+#define STICKER_WHITELIST_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_whitelist_info(whitelist_id INTEGER PRIMARY KEY AUTOINCREMENT, provider_id TEXT NOT NULL, consumer_id TEXT NOT NULL)"
+#define STICKER_RECENT_HISTORY_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_recent_history_info(history_id INTEGER PRIMARY KEY AUTOINCREMENT, sticker_info_id INTEGER, count INTEGER NOT NULL, timestamp TEXT NOT NULL, FOREIGN KEY (sticker_info_id) REFERENCES sticker_info(sticker_info_id) ON DELETE CASCADE)"
#define UIFW_ID 502
#define APPFW_ID 301
#define MAX_ERROR_BUFFER 256
return type;
}
+static int __change_ownership(const char *path, int user, int group)
+{
+ int ret;
+ uid_t uid = (uid_t)user;
+ gid_t gid = (gid_t)group;
+
+ ret = chown(path, uid, gid);
+ if (ret != 0) {
+ strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
+ LOGE("chown() failed : %s", error_buffer);
+ }
+
+ return ret;
+}
+
static void __recover_db()
{
int ret;
goto cleanup;
}
+ ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ LOGE("Failed to create sticker_whitelist_info table : %s", err);
+ goto cleanup;
+ }
+
+ ret = sqlite3_exec(db, STICKER_RECENT_HISTORY_INFO_CREATE_TABLE, NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ LOGE("Failed to create sticker_recent_history_info table : %s", err);
+ goto cleanup;
+ }
+
is_corrupted = FALSE;
cleanup:
goto cleanup;
}
+ if (__change_ownership(db_path, UIFW_ID, UIFW_ID) != 0)
+ LOGE("failed to change ownership");
+
ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
if (ret != SQLITE_OK) {
LOGE("Failed to create sticker_info table : %s" , err);
goto cleanup;
}
+ ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ LOGE("Failed to create sticker_whitelist_info table : %s", err);
+ goto cleanup;
+ }
+
+ ret = sqlite3_exec(db, STICKER_RECENT_HISTORY_INFO_CREATE_TABLE, NULL, NULL, &err);
+ if (ret != SQLITE_OK) {
+ LOGE("Failed to create sticker_recent_history_info table : %s", err);
+ goto cleanup;
+ }
+
ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err);
if (ret != SQLITE_OK) {
LOGE("Failed to set journal_mode : %s", err);
return db;
}
-static int __change_ownership(const char *path, int user, int group)
-{
- int ret;
- uid_t uid = (uid_t)user;
- gid_t gid = (gid_t)group;
-
- ret = chown(path, uid, gid);
- if (ret != 0) {
- strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
- LOGE("chown() failed : %s", error_buffer);
- }
-
- return ret;
-}
-
static int __remove_app_path(char *uri, const char *app_path)
{
int n = 0;
return ret;
}
-static char* __convert_sticker_uri(const char *uri, const char *appid, const char *app_path)
+static char* __convert_sticker_uri(char *uri, const char *appid, const char *app_path)
{
int ret;
- char *rel_path = strdup(uri);
+ char *copy_uri = (char *)calloc(PATH_MAX, sizeof(char));
+ if (copy_uri == NULL) {
+ LOGE("failed to allocate memory");
+ return NULL;
+ }
- __remove_app_path(rel_path, app_path);
- int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(rel_path) + 2;
+ strncpy(copy_uri, uri, PATH_MAX - 1);
+ __remove_app_path(uri, app_path);
+ int len = strlen(STICKER_DIRECTORY) + strlen(appid) + strlen(uri) + 2;
char *new_path = (char *)calloc(len, sizeof(char));
if (new_path == NULL) {
- free(rel_path);
- rel_path = NULL;
- return NULL;
+ LOGE("failed to allocate memory");
+ ret = -1;
+ goto cleanup;
}
- snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, rel_path);
+ snprintf(new_path, len, "%s/%s%s",STICKER_DIRECTORY, appid, uri);
if (access(new_path, F_OK) == 0) {
LOGE("sticker file already exists");
goto cleanup;
}
- if (__file_copy(uri, new_path) == -1) {
+ if (__file_copy(copy_uri, new_path) == -1) {
strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
LOGE("failed to copy sticker file : %s", error_buffer);
ret = -1;
goto cleanup;
}
- ret = unlink(uri);
+ ret = unlink(copy_uri);
if (ret != 0) {
strerror_r(errno, error_buffer, MAX_ERROR_BUFFER);
LOGE("failed to remove sticker file : %s", error_buffer);
LOGE("failed to change ownership");
cleanup:
- free(rel_path);
- rel_path = NULL;
+ free(copy_uri);
+ copy_uri = NULL;
if (ret == 0) {
return new_path;
} else {
- free(new_path);
- new_path = NULL;
+ if (new_path) {
+ free(new_path);
+ new_path = NULL;
+ }
return NULL;
}
}
-static void __insert_sticker_info(const char *app_id, int type, const char *uri, const char *group, const char *thumbnail, const char *description)
+static void __insert_sticker_info(const char *app_id, int type, const char *uri, const char *group, const char *thumbnail, const char *description, int disp_type)
{
int ret;
sqlite3 *db = NULL;
if (!db)
return;
- 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);
+ ret = sqlite3_prepare_v2(db, "INSERT INTO sticker_info (app_id, type, uri, thumbnail, description, group_name, date, display_type) VALUES (?, ?, ?, ?, ?, ?, DateTime('now','localtime'), ?)",-1, &stmt, NULL);
if (ret == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, type);
sqlite3_bind_text(stmt, 4, thumbnail, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 5, description, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 6, group, -1, SQLITE_TRANSIENT);
+ sqlite3_bind_int(stmt, 7, disp_type);
ret = sqlite3_step(stmt);
if (ret != SQLITE_OK && ret != SQLITE_DONE)
return;
}
+static void __insert_sticker_allowlist_info(const char *provider, const char *consumer) {
+ int ret;
+ sqlite3 *db = NULL;
+ sqlite3_stmt *stmt = NULL;
+ const char *db_path;
+
+ db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
+ db = __db_open(db_path);
+ if (!db)
+ return;
+
+ ret = sqlite3_prepare_v2(db, "INSERT INTO sticker_whitelist_info (provider_id, consumer_id) VALUES (?, ?)", -1, &stmt, NULL);
+ if (ret == SQLITE_OK) {
+ sqlite3_bind_text(stmt, 1, provider, -1, SQLITE_TRANSIENT);
+ sqlite3_bind_text(stmt, 2, consumer, -1, SQLITE_TRANSIENT);
+
+ ret = sqlite3_step(stmt);
+ if (ret != SQLITE_OK && ret != SQLITE_DONE)
+ LOGE("sqlite3_step() failed : ret(%d)", ret);
+
+ if (sqlite3_changes(db) == 0)
+ LOGE("No changes to DB");
+
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ } else {
+ LOGE("fail to insert sticker allowlist : %s", sqlite3_errmsg(db));
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ }
+
+ return;
+}
+
static int __get_sticker_info_from_json(const char *appid, const char *file_path, const char *app_path)
{
int ret = 1;
+ int arr_len = 0;
JsonParser* parser = NULL;
GError* err_msg = NULL;
char *uri = NULL;
char *group = NULL;
char *description = NULL;
- char *thumbnail = NULL;
+ char *thumbnail_path = NULL;
char *uri_path = NULL;
parser = json_parser_new();
goto cleanup;
}
+ JsonArray *allowlist_arr = json_object_get_array_member(root_obj, "whitelist");
+ if (allowlist_arr != NULL) {
+ for (int i = 0; i < json_array_get_length(allowlist_arr); i++) {
+ __insert_sticker_allowlist_info(appid, json_array_get_string_element(allowlist_arr, i));
+ }
+ }
+
JsonArray *sticker_arr = json_object_get_array_member(root_obj, "sticker");
if (sticker_arr == NULL) {
LOGE("failed to get array member");
goto cleanup;
}
- int arr_len = json_array_get_length(sticker_arr);
+ arr_len = json_array_get_length(sticker_arr);
for (int i = 0; i < arr_len; i++) {
JsonObject *info_object = json_array_get_object_element(sticker_arr, i);
if (info_object != NULL) {
if (!group)
goto free_memory;
- thumbnail = __get_string_from_object(info_object, "thumbnail");
+ char *rel_thumbnail = __get_string_from_object(info_object, "thumbnail");
+ if (rel_thumbnail) {
+ if (rel_thumbnail[0] != '\0') {
+ if (access(rel_thumbnail, F_OK) == 0) {
+ thumbnail_path = __convert_sticker_uri(rel_thumbnail, appid, app_path);
+ } else {
+ int len = strlen(app_path) + strlen(rel_thumbnail) + 2;
+ char *new_thumbnail_path = (char *)calloc(len, sizeof(char));
+ if (new_thumbnail_path) {
+ if (rel_thumbnail[0] == '/')
+ snprintf(new_thumbnail_path, len, "%s%s",app_path, rel_thumbnail);
+ else
+ snprintf(new_thumbnail_path, len, "%s%s%s",app_path, "/", rel_thumbnail);
+
+ if (access(new_thumbnail_path, F_OK) == 0)
+ thumbnail_path = __convert_sticker_uri(new_thumbnail_path, appid, app_path);
+
+ free(new_thumbnail_path);
+ new_thumbnail_path = NULL;
+ }
+ }
+ }
+
+ free(rel_thumbnail);
+ rel_thumbnail = NULL;
+ }
+
description = __get_string_from_object(info_object, "description");
+ int disp_type = __get_int_from_object(info_object, "display_type");
JsonArray *keyword_arr = json_object_get_array_member(info_object, "keyword");
int keyword_arr_len = json_array_get_length(keyword_arr);
char *new_uri = __convert_sticker_uri(uri, appid, app_path);
if (!new_uri)
goto free_memory;
- __insert_sticker_info(appid, type, new_uri, group, thumbnail, description);
+ __insert_sticker_info(appid, type, new_uri, group, thumbnail_path, description, disp_type);
if (new_uri) {
free(new_uri);
uri_path = NULL;
goto free_memory;
}
- __insert_sticker_info(appid, type, new_uri, group, thumbnail, description);
+ __insert_sticker_info(appid, type, new_uri, group, thumbnail_path, description, disp_type);
free(new_uri);
new_uri = NULL;
}
}
} else {
- __insert_sticker_info(appid, type, uri, group, thumbnail, description);
+ __insert_sticker_info(appid, type, uri, group, thumbnail_path, description, disp_type);
}
for (int j = 0; j < keyword_arr_len; j++) {
group = NULL;
}
- if (thumbnail) {
- free(thumbnail);
- thumbnail = NULL;
+ if (thumbnail_path) {
+ free(thumbnail_path);
+ thumbnail_path = NULL;
}
if (description) {
return ret;
}
+static void __delete_sticker_allowlist(const char *db_path, const char *app_id)
+{
+ int ret;
+ sqlite3 *db = NULL;
+ sqlite3_stmt *stmt = NULL;
+
+ db = __db_open(db_path);
+ if (!db)
+ return;
+
+ ret = sqlite3_prepare_v2(db, "DELETE FROM sticker_whitelist_info WHERE provider_id = ?", -1, &stmt, NULL);
+ if (ret != SQLITE_OK) {
+ LOGE("failed to delete sticker allowlist : %s", sqlite3_errmsg(db));
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ goto cleanup;
+ }
+
+ sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
+
+ ret = sqlite3_step(stmt);
+ if (ret != SQLITE_OK && ret != SQLITE_DONE) {
+ LOGE("sqlite3_step() failed : ret(%d)", ret);
+ goto cleanup;
+ }
+
+ if (sqlite3_changes(db) == 0) {
+ LOGE("No changes to DB");
+ goto cleanup;
+ }
+
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ return;
+
+cleanup:
+ sqlite3_finalize(stmt);
+ sqlite3_close(db);
+ return;
+}
+
static void __delete_sticker_info(const char *db_path, int record_id)
{
int ret;
LOGD("METADATA INSTALL");
LOGD("pkgid: %s, appid: %s", pkgid, appid);
- metadata *md = NULL;
- GList *md_list = NULL;
+ metadata *md = (metadata *)list->data;
package_info_h package_info = NULL;
char *app_path = NULL;
char *file_path = NULL;
goto cleanup;
}
- for(md_list = g_list_first(list); md_list != NULL; md_list = md_list->next) {
- md = (metadata *)md_list->data;
- int path_len = strlen(app_path) + strlen((char *)md->value) + 2;
- file_path = (char *)calloc(path_len, sizeof(char));
- if (!file_path) {
- LOGE("failed to alloc memory");
- continue;
- }
+ int path_len = strlen(app_path) + strlen((char *)md->value) + 2;
+ file_path = (char *)calloc(path_len, sizeof(char));
+ if (!file_path) {
+ LOGE("failed to alloc memory");
+ goto cleanup;
+ }
- if ((char)md->value[0] == '/')
- snprintf(file_path, path_len, "%s%s",app_path, (char *)md->value);
- else
- snprintf(file_path, path_len, "%s%s%s",app_path, "/", (char *)md->value);
+ if ((char)md->value[0] == '/')
+ snprintf(file_path, path_len, "%s%s",app_path, (char *)md->value);
+ else
+ snprintf(file_path, path_len, "%s%s%s",app_path, "/", (char *)md->value);
- if (__get_sticker_info_from_json(appid, file_path, app_path) == 0)
+ if (__get_sticker_info_from_json(appid, file_path, app_path) == 0)
LOGE("failed to get sticker information [path : %s]", file_path);
- free(file_path);
- file_path = NULL;
- }
-
cleanup:
if (package_info)
package_info_destroy(package_info);
app_path = NULL;
}
+ if (file_path) {
+ free(file_path);
+ file_path = NULL;
+ }
+
return 0;
}
sqlite3_close(db);
}
+ __delete_sticker_allowlist(db_path, appid);
+
return 0;
}