From 0553b7620f81a8b5ad23cadd369a24507d843e6a Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis <ludovico.denittis@collabora.com> Date: Wed, 19 May 2021 17:41:09 +0200 Subject: [PATCH] utils: Move pv_get_path_after to libsteam-runtime-tools Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com> --- pressure-vessel/launch.c | 2 +- pressure-vessel/utils.c | 48 ++-------------------------- pressure-vessel/utils.h | 3 -- steam-runtime-tools/utils-internal.h | 3 ++ steam-runtime-tools/utils.c | 44 +++++++++++++++++++++++++ tests/pressure-vessel/utils.c | 42 ------------------------ tests/utils.c | 42 ++++++++++++++++++++++++ 7 files changed, 92 insertions(+), 92 deletions(-) diff --git a/pressure-vessel/launch.c b/pressure-vessel/launch.c index d0c337995..7a4f72b72 100644 --- a/pressure-vessel/launch.c +++ b/pressure-vessel/launch.c @@ -512,7 +512,7 @@ path_to_handle (GUnixFDList *fd_list, real = realpath (path, NULL); if (real != NULL) - after = pv_get_path_after (real, home_realpath); + after = _srt_get_path_after (real, home_realpath); if (after != NULL) { diff --git a/pressure-vessel/utils.c b/pressure-vessel/utils.c index ae76f8ff1..322ca0ff6 100644 --- a/pressure-vessel/utils.c +++ b/pressure-vessel/utils.c @@ -743,50 +743,6 @@ pv_terminate_all_child_processes (GTimeSpan wait_period, return TRUE; } -/* - * @str: A path - * @prefix: A possible prefix - * - * The same as flatpak_has_path_prefix(), but instead of a boolean, - * return the part of @str after @prefix (non-%NULL but possibly empty) - * if @str has prefix @prefix, or %NULL if it does not. - * - * Returns: (nullable) (transfer none): the part of @str after @prefix, - * or %NULL if @str is not below @prefix - */ -const char * -pv_get_path_after (const char *str, - const char *prefix) -{ - while (TRUE) - { - /* Skip consecutive slashes to reach next path - element */ - while (*str == '/') - str++; - while (*prefix == '/') - prefix++; - - /* No more prefix path elements? Done! */ - if (*prefix == 0) - return str; - - /* Compare path element */ - while (*prefix != 0 && *prefix != '/') - { - if (*str != *prefix) - return NULL; - str++; - prefix++; - } - - /* Matched prefix path element, - must be entire str path element */ - if (*str != '/' && *str != 0) - return NULL; - } -} - /** * pv_current_namespace_path_to_host_path: * @current_env_path: a path in the current environment @@ -814,7 +770,7 @@ pv_current_namespace_path_to_host_path (const gchar *current_env_path) home = g_get_home_dir (); if (home != NULL) - after = pv_get_path_after (current_env_path, home); + after = _srt_get_path_after (current_env_path, home); /* If we are inside a Flatpak container, usually, the home * folder is '${HOME}/.var/app/${FLATPAK_ID}' on the host system */ @@ -844,7 +800,7 @@ pv_current_namespace_path_to_host_path (const gchar *current_env_path) } } - after = pv_get_path_after (current_env_path, "/run/host"); + after = _srt_get_path_after (current_env_path, "/run/host"); /* In a Flatpak container, usually, '/run/host' is the root of the * host system */ diff --git a/pressure-vessel/utils.h b/pressure-vessel/utils.h index a2d11fb23..97e1a0a8d 100644 --- a/pressure-vessel/utils.h +++ b/pressure-vessel/utils.h @@ -76,9 +76,6 @@ gboolean pv_terminate_all_child_processes (GTimeSpan wait_period, gchar *pv_current_namespace_path_to_host_path (const gchar *current_env_path); -const char *pv_get_path_after (const char *str, - const char *prefix); - void pv_set_up_logging (gboolean opt_verbose); void pv_delete_dangling_symlink (int dirfd, diff --git a/steam-runtime-tools/utils-internal.h b/steam-runtime-tools/utils-internal.h index daca86ed1..21b98a79a 100644 --- a/steam-runtime-tools/utils-internal.h +++ b/steam-runtime-tools/utils-internal.h @@ -119,6 +119,9 @@ G_GNUC_INTERNAL gchar **_srt_recursive_list_content (const gchar *sysroot, const gchar *directory, gchar ***messages_out); +G_GNUC_INTERNAL const char *_srt_get_path_after (const char *str, + const char *prefix); + /* * _srt_is_same_stat: * @a: a stat buffer diff --git a/steam-runtime-tools/utils.c b/steam-runtime-tools/utils.c index 53ca2d3a8..e6baf9a32 100644 --- a/steam-runtime-tools/utils.c +++ b/steam-runtime-tools/utils.c @@ -1326,3 +1326,47 @@ out: return (GStrv) g_ptr_array_free (g_steal_pointer (&content), FALSE); } + +/* + * @str: A path + * @prefix: A possible prefix + * + * The same as flatpak_has_path_prefix(), but instead of a boolean, + * return the part of @str after @prefix (non-%NULL but possibly empty) + * if @str has prefix @prefix, or %NULL if it does not. + * + * Returns: (nullable) (transfer none): the part of @str after @prefix, + * or %NULL if @str is not below @prefix + */ +const char * +_srt_get_path_after (const char *str, + const char *prefix) +{ + while (TRUE) + { + /* Skip consecutive slashes to reach next path + element */ + while (*str == '/') + str++; + while (*prefix == '/') + prefix++; + + /* No more prefix path elements? Done! */ + if (*prefix == 0) + return str; + + /* Compare path element */ + while (*prefix != 0 && *prefix != '/') + { + if (*str != *prefix) + return NULL; + str++; + prefix++; + } + + /* Matched prefix path element, + must be entire str path element */ + if (*str != '/' && *str != 0) + return NULL; + } +} diff --git a/tests/pressure-vessel/utils.c b/tests/pressure-vessel/utils.c index b10ac4dc7..b70475b57 100644 --- a/tests/pressure-vessel/utils.c +++ b/tests/pressure-vessel/utils.c @@ -306,46 +306,6 @@ test_envp_cmp (Fixture *f, g_free (sort_this); } -static void -test_get_path_after (Fixture *f, - gconstpointer context) -{ - static const struct - { - const char *str; - const char *prefix; - const char *expected; - } tests[] = - { - { "/run/host/usr", "/run/host", "usr" }, - { "/run/host/usr", "/run/host/", "usr" }, - { "/run/host", "/run/host", "" }, - { "////run///host////usr", "//run//host", "usr" }, - { "////run///host////usr", "//run//host////", "usr" }, - { "/run/hostage", "/run/host", NULL }, - /* Any number of leading slashes is ignored, even zero */ - { "foo/bar", "/foo", "bar" }, - { "/foo/bar", "foo", "bar" }, - }; - gsize i; - - for (i = 0; i < G_N_ELEMENTS (tests); i++) - { - const char *str = tests[i].str; - const char *prefix = tests[i].prefix; - const char *expected = tests[i].expected; - - if (expected == NULL) - g_test_message ("%s should not have path prefix %s", - str, prefix); - else - g_test_message ("%s should have path prefix %s followed by %s", - str, prefix, expected); - - g_assert_cmpstr (pv_get_path_after (str, prefix), ==, expected); - } -} - static void test_mtree_entry_parse (Fixture *f, gconstpointer context) @@ -508,8 +468,6 @@ main (int argc, g_test_add ("/delete-dangling-symlink", Fixture, NULL, setup, test_delete_dangling_symlink, teardown); g_test_add ("/envp-cmp", Fixture, NULL, setup, test_envp_cmp, teardown); - g_test_add ("/get-path-after", Fixture, NULL, - setup, test_get_path_after, teardown); g_test_add ("/mtree-entry-parse", Fixture, NULL, setup, test_mtree_entry_parse, teardown); g_test_add ("/search-path-append", Fixture, NULL, diff --git a/tests/utils.c b/tests/utils.c index 312913182..1e9140bf1 100644 --- a/tests/utils.c +++ b/tests/utils.c @@ -264,6 +264,46 @@ test_file_in_sysroot (Fixture *f, } } +static void +test_get_path_after (Fixture *f, + gconstpointer context) +{ + static const struct + { + const char *str; + const char *prefix; + const char *expected; + } tests[] = + { + { "/run/host/usr", "/run/host", "usr" }, + { "/run/host/usr", "/run/host/", "usr" }, + { "/run/host", "/run/host", "" }, + { "////run///host////usr", "//run//host", "usr" }, + { "////run///host////usr", "//run//host////", "usr" }, + { "/run/hostage", "/run/host", NULL }, + /* Any number of leading slashes is ignored, even zero */ + { "foo/bar", "/foo", "bar" }, + { "/foo/bar", "foo", "bar" }, + }; + gsize i; + + for (i = 0; i < G_N_ELEMENTS (tests); i++) + { + const char *str = tests[i].str; + const char *prefix = tests[i].prefix; + const char *expected = tests[i].expected; + + if (expected == NULL) + g_test_message ("%s should not have path prefix %s", + str, prefix); + else + g_test_message ("%s should have path prefix %s followed by %s", + str, prefix, expected); + + g_assert_cmpstr (_srt_get_path_after (str, prefix), ==, expected); + } +} + /* * Test _srt_filter_gameoverlayrenderer function. */ @@ -430,6 +470,8 @@ main (int argc, setup, test_file_in_sysroot, teardown); g_test_add ("/utils/filter_gameoverlayrenderer", Fixture, NULL, setup, filter_gameoverlayrenderer, teardown); + g_test_add ("/utils/get-path-after", Fixture, NULL, + setup, test_get_path_after, teardown); g_test_add ("/utils/same-file", Fixture, NULL, setup, test_same_file, teardown); g_test_add ("/utils/str_is_integer", Fixture, NULL, -- GitLab