From 8039d946272ba4cb39177abfa4260775c3981351 Mon Sep 17 00:00:00 2001 From: Jaeyun Jung Date: Thu, 3 Aug 2023 18:20:01 +0900 Subject: [PATCH] [CodeClean/Service] util to get app-info Code clean, add util to get app-info string and reduce duplicated code. Signed-off-by: Jaeyun Jung --- daemon/pkg-mgr.cc | 74 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/daemon/pkg-mgr.cc b/daemon/pkg-mgr.cc index c133d82..2f8dc4d 100644 --- a/daemon/pkg-mgr.cc +++ b/daemon/pkg-mgr.cc @@ -18,9 +18,43 @@ #include "pkg-mgr.h" #include "service-db.hh" +/** + * @brief Global handle for Tizen package manager. + */ static package_manager_h pkg_mgr = NULL; /** + * @brief Get app-info json string. + */ +static gchar * +_get_app_info (const gchar *package_name, const gchar *res_type, const gchar *res_version) +{ + g_autoptr (JsonBuilder) builder = json_builder_new (); + json_builder_begin_object (builder); + + json_builder_set_member_name (builder, "is_rpk"); + json_builder_add_string_value (builder, "T"); + + json_builder_set_member_name (builder, "app_id"); + json_builder_add_string_value (builder, package_name); + + json_builder_set_member_name (builder, "res_type"); + json_builder_add_string_value (builder, res_type); + + json_builder_set_member_name (builder, "res_version"); + json_builder_add_string_value (builder, res_version); + + json_builder_end_object (builder); + + g_autoptr (JsonNode) root = json_builder_get_root (builder); + g_autoptr (JsonGenerator) gen = json_generator_new (); + json_generator_set_root (gen, root); + json_generator_set_pretty (gen, TRUE); + + return json_generator_to_data (gen, NULL); +} + +/** * @brief A simple package manager event handler for temporary use * @param pkg_path The path where the target package is installed */ @@ -75,8 +109,12 @@ _pkg_mgr_event_cb (const char *type, const char *package_name, if (g_ascii_strcasecmp (type, "rpk") != 0) return; - /* TODO handle allowed resources. Currently this only supports global resources */ - pkg_path = g_strdup_printf ("/opt/usr/globalapps/%s/res/global/", package_name); + /** + * @todo package path + * 1. Handle allowed resources. Currently this only supports global resources. + * 2. Find API to get this hardcoded path prefix (/opt/usr/globalapps/) + */ + pkg_path = g_strdup_printf ("/opt/usr/globalapps/%s/res/global", package_name); if (event_type == PACKAGE_MANAGER_EVENT_TYPE_INSTALL && event_state == PACKAGE_MANAGER_EVENT_STATE_COMPLETED) { @@ -119,9 +157,10 @@ _pkg_mgr_event_cb (const char *type, const char *package_name, } } - /* TODO: find API to get this hardcoded path prefix (/opt/usr/globalapps/) */ - g_autofree gchar *json_file_path = g_strdup_printf ( - "/opt/usr/globalapps/%s/res/global/%s/model_description.json", package_name, res_type); + g_autofree gchar *app_info = _get_app_info (package_name, res_type, res_version); + + g_autofree gchar *json_file_path + = g_strdup_printf ("%s/%s/model_description.json", pkg_path, res_type); if (!g_file_test (json_file_path, G_FILE_TEST_IS_REGULAR)) { _E ("Failed to find json file '%s'. RPK using ML Service APIs should provide this json file.", @@ -170,31 +209,6 @@ _pkg_mgr_event_cb (const char *type, const char *package_name, return; } - /* Fill out the app_info column of DB */ - g_autoptr (JsonBuilder) builder = json_builder_new (); - json_builder_begin_object (builder); - - json_builder_set_member_name (builder, "is_rpk"); - json_builder_add_string_value (builder, "T"); - - json_builder_set_member_name (builder, "app_id"); - json_builder_add_string_value (builder, package_name); - - json_builder_set_member_name (builder, "res_type"); - json_builder_add_string_value (builder, res_type); - - json_builder_set_member_name (builder, "res_version"); - json_builder_add_string_value (builder, res_version); - - json_builder_end_object (builder); - - g_autoptr (JsonNode) root = json_builder_get_root (builder); - - g_autoptr (JsonGenerator) gen = json_generator_new (); - json_generator_set_root (gen, root); - json_generator_set_pretty (gen, TRUE); - g_autofree gchar *app_info = json_generator_to_data (gen, NULL); - guint version; db.set_model (name, model, true, description, app_info, &version); _I ("The model with app_info (%s) is registered as version %u", app_info, version); -- 2.7.4