From 3e4b9defdc35dcf76f5c733cdb1788b85815ceef Mon Sep 17 00:00:00 2001 From: fuleyi Date: Wed, 12 Nov 2025 10:23:58 +0800 Subject: [PATCH] feat: implement sd_bus_read_dict function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added missing sd_bus_read_dict implementation for D-Bus message parsing. This function reads a dictionary from a D-Bus message and stores it in a GHashTable. The implementation handles the D-Bus array of dictionary entries with variant values, properly navigating the message containers and extracting key-value pairs as strings. The function was missing from the codebase but needed for complete D-Bus functionality. It follows the standard sd-bus API patterns and includes proper error handling for all D-Bus operations. Influence: 1. Test with valid D-Bus messages containing dictionary data 2. Verify error handling with malformed or empty dictionaries 3. Test memory management and hash table creation 4. Verify proper container navigation and cleanup 5. Test with different dictionary sizes and key-value types feat: 实现sd_bus_read_dict函数 添加了缺失的sd_bus_read_dict实现,用于解析D-Bus消息。该函数从D-Bus消息中 读取字典并将其存储在GHashTable中。实现处理带有变体值的字典条目数组,正确 导航消息容器并提取字符串类型的键值对。 该函数在代码库中缺失但为完整的D-Bus功能所必需。它遵循标准sd-bus API模 式,并包含对所有D-Bus操作的适当错误处理。 Influence: 1. 测试包含字典数据的有效D-Bus消息 2. 验证对格式错误或空字典的错误处理 3. 测试内存管理和哈希表创建 4. 验证正确的容器导航和清理 5. 测试不同字典大小和键值类型 --- .reuse/dep5 | 2 +- src/plugin-sdbus/plugin-ipwatchd/service.c | 41 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.reuse/dep5 b/.reuse/dep5 index c0a9bed8..fd2cbcc3 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -10,7 +10,7 @@ Source: https://github.com/linuxdeepin/dde-services # License: ... # Config files -Files: .clang-format *.in *.json *.conf *.xml *.service +Files: .clang-format *.in *.json *.conf *.xml *.service *.desktop Copyright: None License: CC0-1.0 diff --git a/src/plugin-sdbus/plugin-ipwatchd/service.c b/src/plugin-sdbus/plugin-ipwatchd/service.c index 9173b010..9f5a9ef5 100644 --- a/src/plugin-sdbus/plugin-ipwatchd/service.c +++ b/src/plugin-sdbus/plugin-ipwatchd/service.c @@ -63,6 +63,47 @@ int sd_bus_message_get_data(sd_bus_message *msg, ...) { return r; } +int sd_bus_read_dict(sd_bus_message *msg, GHashTable **map) { + int r = 0; + + *map = g_hash_table_new(g_str_hash, g_str_equal); + r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_ARRAY, "{sv}"); + if (r < 0) + return r; + + while ((r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_DICT_ENTRY, + "sv")) > 0) { + const char *key; + const char *value; + const char *contents; + + r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &key); + if (r < 0) + return r; + + r = sd_bus_message_peek_type(msg, NULL, &contents); + if (r < 0) + return r; + + r = sd_bus_message_enter_container(msg, SD_BUS_TYPE_VARIANT, contents); + if (r < 0) + return r; + + r = sd_bus_message_read_basic(msg, SD_BUS_TYPE_STRING, &value); + if (r < 0) + return r; + g_hash_table_insert(*map, (gpointer)key, (gpointer)value); + r = sd_bus_message_exit_container(msg); + if (r < 0) + return r; + + r = sd_bus_message_exit_container(msg); + if (r < 0) + return r; + } + return sd_bus_message_exit_container(msg); +} + int sd_bus_message_get_datav(sd_bus_message *msg, va_list ap) { char type = 0; const char *contents = NULL;