Unexport fu_common_instance_id_strsafe()

We want plugins to use the instance ID builder instead now.
This commit is contained in:
Richard Hughes 2022-06-06 14:57:02 +01:00 committed by Mario Limonciello
parent f759569a9f
commit fd9b9db5b3
6 changed files with 69 additions and 67 deletions

View File

@ -72,3 +72,4 @@ Remember: Plugins should be upstream!
* `fu_common_bytes_compare_raw()`: Use `fu_memcmp_safe()` instead
* `fu_common_spawn_sync()`: Use `g_spawn_sync()` instead, or ideally not at all!
* `fu_common_extract_archive()`: Use `FuArchiveFirmware()` instead.
* `fu_common_instance_id_strsafe()`: Use `fu_device_add_instance_strsafe()` instead.

View File

@ -1946,65 +1946,3 @@ fu_common_reset_firmware_search_path(GError **error)
return fu_common_set_firmware_search_path(contents, error);
}
static gboolean
fu_strsafe_instance_id_is_valid_char(gchar c)
{
if (c == ' ')
return FALSE;
if (c == '_')
return FALSE;
if (c == '&')
return FALSE;
if (c == '/')
return FALSE;
if (c == '\\')
return FALSE;
return g_ascii_isprint(c);
}
/**
* fu_common_instance_id_strsafe:
* @str: (nullable): part of the string to sanitize
*
* Sanitize the string used as part of the InstanceID.
*
* Returns: a string, or %NULL if invalid
*
* Since: 1.7.6
**/
gchar *
fu_common_instance_id_strsafe(const gchar *str)
{
g_autoptr(GString) tmp = g_string_new(NULL);
gboolean has_content = FALSE;
/* sanity check */
if (str == NULL)
return NULL;
/* use - to replace problematic chars -- but only once per section */
for (guint i = 0; str[i] != '\0'; i++) {
gchar c = str[i];
if (!fu_strsafe_instance_id_is_valid_char(c)) {
if (has_content) {
g_string_append_c(tmp, '-');
has_content = FALSE;
}
} else {
g_string_append_c(tmp, c);
has_content = TRUE;
}
}
/* remove any trailing replacements */
if (tmp->len > 0 && tmp->str[tmp->len - 1] == '-')
g_string_truncate(tmp, tmp->len - 1);
/* nothing left! */
if (tmp->len == 0)
return NULL;
/* success */
return g_string_free(g_steal_pointer(&tmp), FALSE);
}

View File

@ -207,8 +207,6 @@ fu_common_dump_full(const gchar *log_domain,
FuDumpFlags flags);
void
fu_common_dump_bytes(const gchar *log_domain, const gchar *title, GBytes *bytes);
gchar *
fu_common_instance_id_strsafe(const gchar *str);
gboolean
fu_common_kernel_locked_down(void);
gboolean

View File

@ -5005,6 +5005,59 @@ fu_device_add_instance_str(FuDevice *self, const gchar *key, const gchar *value)
g_hash_table_insert(priv->instance_hash, g_strdup(key), g_strdup(value));
}
static gboolean
fu_strsafe_instance_id_is_valid_char(gchar c)
{
if (c == ' ')
return FALSE;
if (c == '_')
return FALSE;
if (c == '&')
return FALSE;
if (c == '/')
return FALSE;
if (c == '\\')
return FALSE;
return g_ascii_isprint(c);
}
/* NOTE: we can't use fu_strsafe as this behavior is now effectively ABI */
static gchar *
fu_common_instance_id_strsafe(const gchar *str)
{
g_autoptr(GString) tmp = g_string_new(NULL);
gboolean has_content = FALSE;
/* sanity check */
if (str == NULL)
return NULL;
/* use - to replace problematic chars -- but only once per section */
for (guint i = 0; str[i] != '\0'; i++) {
gchar c = str[i];
if (!fu_strsafe_instance_id_is_valid_char(c)) {
if (has_content) {
g_string_append_c(tmp, '-');
has_content = FALSE;
}
} else {
g_string_append_c(tmp, c);
has_content = TRUE;
}
}
/* remove any trailing replacements */
if (tmp->len > 0 && tmp->str[tmp->len - 1] == '-')
g_string_truncate(tmp, tmp->len - 1);
/* nothing left! */
if (tmp->len == 0)
return NULL;
/* success */
return g_string_free(g_steal_pointer(&tmp), FALSE);
}
/**
* fu_device_add_instance_strsafe:
* @self: a #FuDevice

View File

@ -579,8 +579,21 @@ fu_strsafe_func(void)
{"dave\x03\x04XXX", "dave..X"},
{"\x03\x03", NULL},
{NULL, NULL}};
g_autofree gchar *id_part = fu_common_instance_id_strsafe("_ _LEN&VO&\\&");
g_assert_cmpstr(id_part, ==, "LEN-VO");
GPtrArray *instance_ids;
gboolean ret;
g_autoptr(FuContext) ctx = fu_context_new();
g_autoptr(FuDevice) dev = fu_device_new(ctx);
g_autoptr(GError) error = NULL;
/* check bespoke legacy instance ID behavior */
fu_device_add_instance_strsafe(dev, "KEY", "_ _LEN&VO&\\&");
ret = fu_device_build_instance_id(dev, &error, "SUB", "KEY", NULL);
g_assert_no_error(error);
g_assert_true(ret);
instance_ids = fu_device_get_instance_ids(dev);
g_assert_cmpint(instance_ids->len, ==, 1);
g_assert_cmpstr(g_ptr_array_index(instance_ids, 0), ==, "SUB\\KEY_LEN-VO");
for (guint i = 0; strs[i].in != NULL; i++) {
g_autofree gchar *tmp = fu_strsafe(strs[i].in, 7);
g_assert_cmpstr(tmp, ==, strs[i].op);

View File

@ -913,7 +913,6 @@ LIBFWUPDPLUGIN_1.7.4 {
LIBFWUPDPLUGIN_1.7.6 {
global:
fu_common_instance_id_strsafe;
fu_common_version_ensure_semver_full;
fu_udev_device_get_parent_with_subsystem;
local: *;