tool: allow prefix "http://" for get/set/clear key 72/262072/2
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 2 Aug 2021 08:51:59 +0000 (17:51 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Mon, 2 Aug 2021 08:55:15 +0000 (17:55 +0900)
Change-Id: Ie4c504706d1de818dbf1b84878cb57e95009ea0f
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
tool/system-info-tool-get.c
tool/system-info-tool-set.c
tool/system-info-tool.c
tool/system-info-tool.h

index c6662bddeabfbe6d219d41fe56232cb5c7ea8ca4..a448a12b86ac685614d896e3ddfbe6064a96cfb2 100644 (file)
@@ -176,11 +176,16 @@ out:
        return ret;
 }
 
-void system_info_tool_get(const char *key, bool verbose)
+void system_info_tool_get(const char *rawkey, bool verbose)
 {
        struct value api;
+       char key[BUFFER_MAX];
        int retval;
 
+       retval = convert_raw_key(rawkey, key, sizeof(key));
+       if (retval < 0)
+               return;
+
        retval = system_info_tool_get_api(key, &api);
        if (retval < 0)
                return;
index a893b45ade268ea529e9d510940df53f2c8e19b2..1d1f4ed1bae24354e3e28af55890685a207ee75d 100644 (file)
@@ -20,7 +20,7 @@ void system_info_tool_set_help(void)
        printf("  -C|--clear-all                 Clear all user-defined keys\n");
 }
 
-static int convert_raw_optargs(const char *rawtype, const char *rawvalue, struct value *out)
+static int convert_raw_value(const char *rawtype, const char *rawvalue, struct value *out)
 {
        system_info_type_e type = string_to_type(rawtype);
 
@@ -229,7 +229,7 @@ static int system_info_tool_init_rw_database(void)
 int system_info_tool_set(int argc, char *argv[])
 {
        int ret;
-       char *key;
+       char key[BUFFER_MAX];
        struct value value;
 
        /* argv[optind]    : key
@@ -247,21 +247,25 @@ int system_info_tool_set(int argc, char *argv[])
                        return -ENOTSUP;
        }
 
-       key = argv[optind];
-       ret = convert_raw_optargs(argv[optind + 1], argv[optind + 2], &value);
+       ret = convert_raw_key(argv[optind], key, sizeof(key));
+       if (ret < 0)
+               return ret;
+
+       ret = convert_raw_value(argv[optind + 1], argv[optind + 2], &value);
        if (ret < 0)
                return ret;
 
        return system_info_tool_set_entry(key, value);
 }
 
-int system_info_tool_clear_key(const char *key)
+int system_info_tool_clear_key(const char *rawkey)
 {
        int retval;
        int ret;
        int match = 0;
        int i;
        int select;
+       char key[BUFFER_MAX];
 
        struct cache {
                int match;
@@ -273,6 +277,10 @@ int system_info_tool_clear_key(const char *key)
                { -1, },
        };
 
+       ret = convert_raw_key(rawkey, key, sizeof(key));
+       if (ret < 0)
+               return ret;
+
        for (i = 0; i < SYSTEM_INFO_TYPE_MAX; ++i) {
                retval = system_info_tool_get_raw(key, i, DB_DEFAULT_RW, &cache[i].dbentry);
                if (retval == 0)
index 538d533768ae1ede9c1bdda4a7d792a568d06fa5..4f031ca39a1b591a669d66b35e43c5b17c5dae06 100644 (file)
@@ -7,6 +7,8 @@
 #include "system-info-tool-get.h"
 #include "system-info-tool-set.h"
 
+#define KEY_PREFIX    "http://"
+
 int runtime_env = C;
 const struct db db[DB_END] = {
 /*                       { dbpath,                     dbname,         ticker } */
@@ -44,6 +46,24 @@ system_info_type_e string_to_type(const char *type)
                return SYSTEM_INFO_TYPE_MAX;
 }
 
+int convert_raw_key(const char *rawkey, char *buffer, int len)
+{
+       int retval;
+
+       if (!rawkey || !buffer)
+               return -EINVAL;
+
+       if (strstr(rawkey, KEY_PREFIX) == rawkey)
+               retval = snprintf(buffer, len, "%s", rawkey + strlen(KEY_PREFIX));
+       else
+               retval = snprintf(buffer, len, "%s", rawkey);
+
+       if (retval >= len)
+               return -ENAMETOOLONG;
+
+       return 0;
+}
+
 void print_value(struct value value)
 {
        if (value.type == SYSTEM_INFO_BOOL)
index 7b966d2171b59cdc97f291ece55181a39c46f380..ce8daa6d7d8afd9e3e8bf6bf1e9acfe980c503e8 100644 (file)
@@ -76,5 +76,6 @@ static inline void free_dbentry(struct dbentry *entry)
 const char *type_to_string(system_info_type_e type);
 system_info_type_e string_to_type(const char *type);
 void print_value(struct value value);
+int convert_raw_key(const char *key, char *buffer, int len);
 
 #endif