Merge branch '2876-api-public-or-internal' into 'main'

gutils: Make g_find_program_for_path() a proper private API

Closes #2876

See merge request GNOME/glib!3267
This commit is contained in:
Philip Withnall 2023-02-13 17:31:28 +00:00
commit f5b2aeaf39
5 changed files with 39 additions and 26 deletions

View File

@ -1914,7 +1914,7 @@ g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
{ {
char *t; char *t;
/* Use the desktop file path (if any) as working dir to search program */ /* Use the desktop file path (if any) as working dir to search program */
t = g_find_program_for_path (try_exec, NULL, path); t = GLIB_PRIVATE_CALL (g_find_program_for_path) (try_exec, NULL, path);
if (t == NULL) if (t == NULL)
{ {
g_free (path); g_free (path);
@ -1947,7 +1947,7 @@ g_desktop_app_info_load_from_keyfile (GDesktopAppInfo *info,
* argument, so dereferencing argv[0] should return non-NULL. */ * argument, so dereferencing argv[0] should return non-NULL. */
g_assert (argc > 0); g_assert (argc > 0);
/* Use the desktop file path (if any) as working dir to search program */ /* Use the desktop file path (if any) as working dir to search program */
t = g_find_program_for_path (argv[0], NULL, path); t = GLIB_PRIVATE_CALL (g_find_program_for_path) (argv[0], NULL, path);
g_strfreev (argv); g_strfreev (argv);
if (t == NULL) if (t == NULL)
@ -2731,7 +2731,7 @@ prepend_terminal_to_vector (int *argc,
for (i = 0, found_terminal = NULL; i < G_N_ELEMENTS (known_terminals); i++) for (i = 0, found_terminal = NULL; i < G_N_ELEMENTS (known_terminals); i++)
{ {
found_terminal = g_find_program_for_path (known_terminals[i].exec, found_terminal = GLIB_PRIVATE_CALL (g_find_program_for_path) (known_terminals[i].exec,
path, working_dir); path, working_dir);
if (found_terminal != NULL) if (found_terminal != NULL)
{ {
@ -2984,7 +2984,7 @@ g_desktop_app_info_launch_uris_with_spawn (GDesktopAppInfo *info,
{ {
const char *env_path = g_environ_getenv (envp, "PATH"); const char *env_path = g_environ_getenv (envp, "PATH");
program_path = g_find_program_for_path (program, program_path = GLIB_PRIVATE_CALL (g_find_program_for_path) (program,
env_path, env_path,
info->path); info->path);
} }

View File

@ -67,6 +67,8 @@ glib__private__ (void)
g_win32_push_empty_invalid_parameter_handler, g_win32_push_empty_invalid_parameter_handler,
g_win32_pop_invalid_parameter_handler, g_win32_pop_invalid_parameter_handler,
g_find_program_for_path,
}; };
return &table; return &table;

View File

@ -154,6 +154,10 @@ typedef struct _GWin32InvalidParameterHandler GWin32InvalidParameterHandler;
void g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *items); void g_win32_push_empty_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
void g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *items); void g_win32_pop_invalid_parameter_handler (GWin32InvalidParameterHandler *items);
char *g_find_program_for_path (const char *program,
const char *path,
const char *working_dir);
#define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol) #define GLIB_PRIVATE_CALL(symbol) (glib__private__()->symbol)
@ -213,6 +217,11 @@ typedef struct {
void (* g_win32_pop_invalid_parameter_handler) (GWin32InvalidParameterHandler *items); void (* g_win32_pop_invalid_parameter_handler) (GWin32InvalidParameterHandler *items);
/* See gutils.c */
char *(* g_find_program_for_path) (const char *program,
const char *path,
const char *working_dir);
/* Add other private functions here, initialize them in glib-private.c */ /* Add other private functions here, initialize them in glib-private.c */
} GLibPrivateVTable; } GLibPrivateVTable;

View File

@ -28,15 +28,9 @@
G_BEGIN_DECLS G_BEGIN_DECLS
GLIB_AVAILABLE_IN_2_60
void g_set_user_dirs (const gchar *first_dir_type, void g_set_user_dirs (const gchar *first_dir_type,
...) G_GNUC_NULL_TERMINATED; ...) G_GNUC_NULL_TERMINATED;
GLIB_AVAILABLE_IN_2_76
char * g_find_program_for_path (const char *program,
const char *path,
const char *working_dir);
/* Returns the smallest power of 2 greater than or equal to n, /* Returns the smallest power of 2 greater than or equal to n,
* or 0 if such power does not fit in a gsize * or 0 if such power does not fit in a gsize
*/ */

View File

@ -487,6 +487,14 @@ test_find_program (void)
g_assert (res == NULL); g_assert (res == NULL);
} }
static char *
find_program_for_path (const char *program,
const char *path,
const char *working_dir)
{
return GLIB_PRIVATE_CALL(g_find_program_for_path) (program, path, working_dir);
}
static void static void
test_find_program_for_path (void) test_find_program_for_path (void)
{ {
@ -517,9 +525,9 @@ test_find_program_for_path (void)
g_assert_true (g_file_test (exe_path, G_FILE_TEST_IS_EXECUTABLE)); g_assert_true (g_file_test (exe_path, G_FILE_TEST_IS_EXECUTABLE));
g_assert_null (g_find_program_in_path (command_to_find)); g_assert_null (g_find_program_in_path (command_to_find));
g_assert_null (g_find_program_for_path (command_to_find, NULL, NULL)); g_assert_null (find_program_for_path (command_to_find, NULL, NULL));
found_path = g_find_program_for_path (command_to_find, path, NULL); found_path = find_program_for_path (command_to_find, path, NULL);
#ifdef __APPLE__ #ifdef __APPLE__
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_assert_true (g_str_has_suffix (found_path, exe_path)); g_assert_true (g_str_has_suffix (found_path, exe_path));
@ -528,7 +536,7 @@ test_find_program_for_path (void)
#endif #endif
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
found_path = g_find_program_for_path (command_to_find, path, path); found_path = find_program_for_path (command_to_find, path, path);
#ifdef __APPLE__ #ifdef __APPLE__
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_assert_true (g_str_has_suffix (found_path, exe_path)); g_assert_true (g_str_has_suffix (found_path, exe_path));
@ -537,7 +545,7 @@ test_find_program_for_path (void)
#endif #endif
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
found_path = g_find_program_for_path (command_to_find, NULL, path); found_path = find_program_for_path (command_to_find, NULL, path);
#ifdef __APPLE__ #ifdef __APPLE__
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_assert_true (g_str_has_suffix (found_path, exe_path)); g_assert_true (g_str_has_suffix (found_path, exe_path));
@ -546,7 +554,7 @@ test_find_program_for_path (void)
#endif #endif
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
found_path = g_find_program_for_path (command_to_find, "::", path); found_path = find_program_for_path (command_to_find, "::", path);
#ifdef __APPLE__ #ifdef __APPLE__
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_assert_true (g_str_has_suffix (found_path, exe_path)); g_assert_true (g_str_has_suffix (found_path, exe_path));
@ -558,7 +566,7 @@ test_find_program_for_path (void)
old_cwd = g_get_current_dir (); old_cwd = g_get_current_dir ();
g_chdir (path); g_chdir (path);
found_path = found_path =
g_find_program_for_path (command_to_find, find_program_for_path (command_to_find,
G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, NULL); G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, NULL);
g_chdir (old_cwd); g_chdir (old_cwd);
g_clear_pointer (&old_cwd, g_free); g_clear_pointer (&old_cwd, g_free);
@ -573,7 +581,7 @@ test_find_program_for_path (void)
old_cwd = g_get_current_dir (); old_cwd = g_get_current_dir ();
g_chdir (tmp); g_chdir (tmp);
found_path = found_path =
g_find_program_for_path (command_to_find, find_program_for_path (command_to_find,
G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, "sub-path"); G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, "sub-path");
g_chdir (old_cwd); g_chdir (old_cwd);
g_clear_pointer (&old_cwd, g_free); g_clear_pointer (&old_cwd, g_free);
@ -586,10 +594,10 @@ test_find_program_for_path (void)
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
g_assert_null ( g_assert_null (
g_find_program_for_path (command_to_find, find_program_for_path (command_to_find,
G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, "other-sub-path")); G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S, "other-sub-path"));
found_path = g_find_program_for_path (command_to_find, found_path = find_program_for_path (command_to_find,
G_SEARCHPATH_SEPARATOR_S "sub-path" G_SEARCHPATH_SEPARATOR_S, tmp); G_SEARCHPATH_SEPARATOR_S "sub-path" G_SEARCHPATH_SEPARATOR_S, tmp);
#ifdef __APPLE__ #ifdef __APPLE__
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
@ -599,23 +607,23 @@ test_find_program_for_path (void)
#endif #endif
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
g_assert_null (g_find_program_for_path (command_to_find, g_assert_null (find_program_for_path (command_to_find,
G_SEARCHPATH_SEPARATOR_S "other-sub-path" G_SEARCHPATH_SEPARATOR_S, tmp)); G_SEARCHPATH_SEPARATOR_S "other-sub-path" G_SEARCHPATH_SEPARATOR_S, tmp));
#ifdef G_OS_UNIX #ifdef G_OS_UNIX
found_path = g_find_program_for_path ("sh", NULL, tmp); found_path = find_program_for_path ("sh", NULL, tmp);
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
old_cwd = g_get_current_dir (); old_cwd = g_get_current_dir ();
g_chdir ("/"); g_chdir ("/");
found_path = g_find_program_for_path ("sh", "sbin:bin:usr/bin:usr/sbin", NULL); found_path = find_program_for_path ("sh", "sbin:bin:usr/bin:usr/sbin", NULL);
g_chdir (old_cwd); g_chdir (old_cwd);
g_clear_pointer (&old_cwd, g_free); g_clear_pointer (&old_cwd, g_free);
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
found_path = g_find_program_for_path ("sh", "sbin:bin:usr/bin:usr/sbin", "/"); found_path = find_program_for_path ("sh", "sbin:bin:usr/bin:usr/sbin", "/");
g_assert_nonnull (found_path); g_assert_nonnull (found_path);
g_clear_pointer (&found_path, g_free); g_clear_pointer (&found_path, g_free);
#endif /* G_OS_UNIX */ #endif /* G_OS_UNIX */