diff --git a/pressure-vessel/launch.c b/pressure-vessel/launch.c index d0c337995b97b5e26ad1bcd9001a88e61a9082df..7a4f72b72cd96a98a0e2b574c239a47194cc930b 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 ae76f8ff153bfb849b27a911269d16237c9a3585..322ca0ff62632909dece050f3323f2a4225455f9 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 a2d11fb230424fbb37f47d3acc3c86ccc6f3ece8..97e1a0a8d3faafa034df6ee0d625af869f5b8875 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 daca86ed1b70325dc7fe472464ccb8fd0f1302f1..21b98a79a5901706f375b00239b9bdbd0993cb6b 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 53ca2d3a8639967dbc7f10e15a57dc1f43a2d741..e6baf9a32eaa567a0d45dc0102b8bc08e7080475 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 b10ac4dc7d055c729a4cf7e4cd49136b212ef3af..b70475b575cc920904beb946a42262d378767e30 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 312913182662f1900d3c155cddb6754ed1417226..1e9140bf1d9fa9d6e6968456bd52fdb269693eb2 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,