Merge branch 'gir-use-gstrv' into 'main'

girepository: Return enumerated versions and search paths as a GStrv

See merge request GNOME/glib!3773
This commit is contained in:
Marco Trevisan
2023-12-21 14:37:07 +00:00
6 changed files with 266 additions and 74 deletions

View File

@@ -58,7 +58,7 @@
*/ */
static GIRepository *default_repository = NULL; static GIRepository *default_repository = NULL;
static GSList *typelib_search_path = NULL; static GPtrArray *typelib_search_path = NULL;
typedef struct { typedef struct {
guint n_interfaces; guint n_interfaces;
@@ -198,32 +198,24 @@ init_globals (void)
*/ */
type_lib_path_env = g_getenv ("GI_TYPELIB_PATH"); type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
typelib_search_path = NULL;
if (type_lib_path_env) if (type_lib_path_env)
{ {
gchar **custom_dirs; gchar **custom_dirs;
gchar **d;
custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0); custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
typelib_search_path =
d = custom_dirs; g_ptr_array_new_take_null_terminated ((gpointer) g_steal_pointer (&custom_dirs), g_free);
while (*d) }
{ else
typelib_search_path = g_slist_prepend (typelib_search_path, *d); {
d++; typelib_search_path = g_ptr_array_new_null_terminated (1, g_free, TRUE);
}
/* ownership of the array content was passed to the list */
g_free (custom_dirs);
} }
libdir = GOBJECT_INTROSPECTION_LIBDIR; libdir = GOBJECT_INTROSPECTION_LIBDIR;
typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL); typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
typelib_search_path = g_slist_prepend (typelib_search_path, typelib_dir); g_ptr_array_add (typelib_search_path, g_steal_pointer (&typelib_dir));
typelib_search_path = g_slist_reverse (typelib_search_path);
} }
g_once_init_leave (&initialized, 1); g_once_init_leave (&initialized, 1);
@@ -244,11 +236,12 @@ void
gi_repository_prepend_search_path (const char *directory) gi_repository_prepend_search_path (const char *directory)
{ {
init_globals (); init_globals ();
typelib_search_path = g_slist_prepend (typelib_search_path, g_strdup (directory)); g_ptr_array_insert (typelib_search_path, 0, g_strdup (directory));
} }
/** /**
* gi_repository_get_search_path: * gi_repository_get_search_path:
* @n_paths_out: (optional) (out): The number of search paths returned.
* *
* Returns the current search path [class@GIRepository.Repository] will use when * Returns the current search path [class@GIRepository.Repository] will use when
* loading typelib files. * loading typelib files.
@@ -256,14 +249,27 @@ gi_repository_prepend_search_path (const char *directory)
* The list is internal to [class@GIRepository.Repository] and should not be * The list is internal to [class@GIRepository.Repository] and should not be
* freed, nor should its string elements. * freed, nor should its string elements.
* *
* Returns: (element-type filename) (transfer none): list of search paths, most * Returns: (element-type filename) (transfer none) (array length=n_paths_out): list of search paths, most
* important first * important first
* Since: 2.80 * Since: 2.80
*/ */
GSList * const char * const *
gi_repository_get_search_path (void) gi_repository_get_search_path (size_t *n_paths_out)
{ {
return typelib_search_path; if G_UNLIKELY (!typelib_search_path || !typelib_search_path->pdata)
{
static const char * const empty_search_path[] = {NULL};
if (n_paths_out)
*n_paths_out = 0;
return empty_search_path;
}
if (n_paths_out)
*n_paths_out = typelib_search_path->len;
return (const char * const *) typelib_search_path->pdata;
} }
static char * static char *
@@ -550,7 +556,7 @@ get_typelib_dependencies_transitive (GIRepository *repository,
* Retrieves all (transitive) versioned dependencies for * Retrieves all (transitive) versioned dependencies for
* @namespace_. * @namespace_.
* *
* The strings are of the form `namespace-version`. * The returned strings are of the form `namespace-version`.
* *
* Note: @namespace_ must have already been loaded using a function * Note: @namespace_ must have already been loaded using a function
* such as [method@GIRepository.Repository.require] before calling this * such as [method@GIRepository.Repository.require] before calling this
@@ -587,8 +593,8 @@ gi_repository_get_dependencies (GIRepository *repository,
transitive_dependencies); transitive_dependencies);
/* Convert to a string array. */ /* Convert to a string array. */
out = g_ptr_array_new_full (g_hash_table_size (transitive_dependencies), out = g_ptr_array_new_null_terminated (g_hash_table_size (transitive_dependencies),
g_free); g_free, TRUE);
g_hash_table_iter_init (&iter, transitive_dependencies); g_hash_table_iter_init (&iter, transitive_dependencies);
while (g_hash_table_iter_next (&iter, (gpointer) &dependency, NULL)) while (g_hash_table_iter_next (&iter, (gpointer) &dependency, NULL))
@@ -599,9 +605,6 @@ gi_repository_get_dependencies (GIRepository *repository,
g_hash_table_unref (transitive_dependencies); g_hash_table_unref (transitive_dependencies);
/* Add a NULL terminator. */
g_ptr_array_add (out, NULL);
return (gchar **) g_ptr_array_free (out, FALSE); return (gchar **) g_ptr_array_free (out, FALSE);
} }
@@ -1310,21 +1313,21 @@ gi_repository_get_typelib_path (GIRepository *repository,
/* This simple search function looks for a specified namespace-version; /* This simple search function looks for a specified namespace-version;
it's faster than the full directory listing required for latest version. */ it's faster than the full directory listing required for latest version. */
static GMappedFile * static GMappedFile *
find_namespace_version (const gchar *namespace, find_namespace_version (const char *namespace,
const gchar *version, const char *version,
GSList *search_path, const char * const *search_paths,
gchar **path_ret) size_t n_search_paths,
char **path_ret)
{ {
GSList *ldir;
GError *error = NULL; GError *error = NULL;
GMappedFile *mfile = NULL; GMappedFile *mfile = NULL;
char *fname; char *fname;
fname = g_strdup_printf ("%s-%s.typelib", namespace, version); fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
for (ldir = search_path; ldir; ldir = ldir->next) for (size_t i = 0; i < n_search_paths; ++i)
{ {
char *path = g_build_filename (ldir->data, fname, NULL); char *path = g_build_filename (search_paths[i], fname, NULL);
mfile = g_mapped_file_new (path, FALSE, &error); mfile = g_mapped_file_new (path, FALSE, &error);
if (error) if (error)
@@ -1434,14 +1437,14 @@ free_candidate (struct NamespaceVersionCandidadate *candidate)
} }
static GSList * static GSList *
enumerate_namespace_versions (const gchar *namespace, enumerate_namespace_versions (const char *namespace,
GSList *search_path) const char * const *search_paths,
size_t n_search_paths)
{ {
GSList *candidates = NULL; GSList *candidates = NULL;
GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal); GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
char *namespace_dash; char *namespace_dash;
char *namespace_typelib; char *namespace_typelib;
GSList *ldir;
GError *error = NULL; GError *error = NULL;
int index; int index;
@@ -1449,13 +1452,13 @@ enumerate_namespace_versions (const gchar *namespace,
namespace_typelib = g_strdup_printf ("%s.typelib", namespace); namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
index = 0; index = 0;
for (ldir = search_path; ldir; ldir = ldir->next) for (size_t i = 0; i < n_search_paths; ++i)
{ {
GDir *dir; GDir *dir;
const char *dirname; const char *dirname;
const char *entry; const char *entry;
dirname = (const char*)ldir->data; dirname = search_paths[i];
dir = g_dir_open (dirname, 0, NULL); dir = g_dir_open (dirname, 0, NULL);
if (dir == NULL) if (dir == NULL)
continue; continue;
@@ -1521,10 +1524,11 @@ enumerate_namespace_versions (const gchar *namespace,
} }
static GMappedFile * static GMappedFile *
find_namespace_latest (const gchar *namespace, find_namespace_latest (const char *namespace,
GSList *search_path, const char * const *search_paths,
gchar **version_ret, size_t n_search_paths,
gchar **path_ret) char **version_ret,
char **path_ret)
{ {
GSList *candidates; GSList *candidates;
GMappedFile *result = NULL; GMappedFile *result = NULL;
@@ -1532,7 +1536,7 @@ find_namespace_latest (const gchar *namespace,
*version_ret = NULL; *version_ret = NULL;
*path_ret = NULL; *path_ret = NULL;
candidates = enumerate_namespace_versions (namespace, search_path); candidates = enumerate_namespace_versions (namespace, search_paths, n_search_paths);
if (candidates != NULL) if (candidates != NULL)
{ {
@@ -1558,28 +1562,41 @@ find_namespace_latest (const gchar *namespace,
* @repository: (nullable): A #GIRepository, or `NULL` for the singleton * @repository: (nullable): A #GIRepository, or `NULL` for the singleton
* process-global default #GIRepository * process-global default #GIRepository
* @namespace_: GI namespace, e.g. `Gtk` * @namespace_: GI namespace, e.g. `Gtk`
* @n_versions_out: (optional) (out): The number of versions returned.
* *
* Obtain an unordered list of versions (either currently loaded or * Obtain an unordered list of versions (either currently loaded or
* available) for @namespace_ in this @repository. * available) for @namespace_ in this @repository.
* *
* Returns: (element-type utf8) (transfer full): the array of versions. * Returns: (element-type utf8) (transfer full) (array length=n_versions_out): the array of versions.
* Since: 2.80 * Since: 2.80
*/ */
GList * char **
gi_repository_enumerate_versions (GIRepository *repository, gi_repository_enumerate_versions (GIRepository *repository,
const gchar *namespace_) const gchar *namespace_,
size_t *n_versions_out)
{ {
GList *ret = NULL; GPtrArray *versions;
GSList *candidates, *link; GSList *candidates, *link;
const gchar *loaded_version; const gchar *loaded_version;
char **ret;
init_globals (); init_globals ();
candidates = enumerate_namespace_versions (namespace_, typelib_search_path); candidates = enumerate_namespace_versions (namespace_,
(const char * const *) typelib_search_path->pdata,
typelib_search_path->len);
if (!candidates)
{
if (n_versions_out)
*n_versions_out = 0;
return g_strdupv ((char *[]) {NULL});
}
versions = g_ptr_array_new_null_terminated (1, g_free, TRUE);
for (link = candidates; link; link = link->next) for (link = candidates; link; link = link->next)
{ {
struct NamespaceVersionCandidadate *candidate = link->data; struct NamespaceVersionCandidadate *candidate = link->data;
ret = g_list_prepend (ret, g_strdup (candidate->version)); g_ptr_array_add (versions, g_steal_pointer (&candidate->version));
free_candidate (candidate); free_candidate (candidate);
} }
g_slist_free (candidates); g_slist_free (candidates);
@@ -1591,20 +1608,25 @@ gi_repository_enumerate_versions (GIRepository *repository,
if (gi_repository_is_registered (repository, namespace_, NULL)) if (gi_repository_is_registered (repository, namespace_, NULL))
{ {
loaded_version = gi_repository_get_version (repository, namespace_); loaded_version = gi_repository_get_version (repository, namespace_);
if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal)) if (loaded_version &&
ret = g_list_prepend (ret, g_strdup (loaded_version)); !g_ptr_array_find_with_equal_func (versions, loaded_version, g_str_equal, NULL))
g_ptr_array_add (versions, g_strdup (loaded_version));
} }
ret = (char **) g_ptr_array_steal (versions, n_versions_out);
g_ptr_array_unref (g_steal_pointer (&versions));
return ret; return ret;
} }
static GITypelib * static GITypelib *
require_internal (GIRepository *repository, require_internal (GIRepository *repository,
const gchar *namespace, const char *namespace,
const gchar *version, const char *version,
GIRepositoryLoadFlags flags, GIRepositoryLoadFlags flags,
GSList *search_path, const char * const *search_paths,
GError **error) size_t n_search_paths,
GError **error)
{ {
GMappedFile *mfile; GMappedFile *mfile;
GITypelib *ret = NULL; GITypelib *ret = NULL;
@@ -1637,14 +1659,14 @@ require_internal (GIRepository *repository,
if (version != NULL) if (version != NULL)
{ {
mfile = find_namespace_version (namespace, version, mfile = find_namespace_version (namespace, version, search_paths,
search_path, &path); n_search_paths, &path);
tmp_version = g_strdup (version); tmp_version = g_strdup (version);
} }
else else
{ {
mfile = find_namespace_latest (namespace, search_path, mfile = find_namespace_latest (namespace, search_paths, n_search_paths,
&tmp_version, &path); &tmp_version, &path);
} }
if (mfile == NULL) if (mfile == NULL)
@@ -1744,7 +1766,8 @@ gi_repository_require (GIRepository *repository,
init_globals (); init_globals ();
typelib = require_internal (repository, namespace, version, flags, typelib = require_internal (repository, namespace, version, flags,
typelib_search_path, error); (const char * const *) typelib_search_path->pdata,
typelib_search_path->len, error);
return typelib; return typelib;
} }
@@ -1779,10 +1802,10 @@ gi_repository_require_private (GIRepository *repository,
GIRepositoryLoadFlags flags, GIRepositoryLoadFlags flags,
GError **error) GError **error)
{ {
GSList search_path = { (gpointer) typelib_dir, NULL }; const char * const search_path[] = { typelib_dir, NULL };
return require_internal (repository, namespace, version, flags, return require_internal (repository, namespace, version, flags,
&search_path, error); search_path, 1, error);
} }
static gboolean static gboolean

View File

@@ -109,7 +109,7 @@ GI_AVAILABLE_IN_ALL
void gi_repository_prepend_library_path (const char *directory); void gi_repository_prepend_library_path (const char *directory);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GSList * gi_repository_get_search_path (void); const char * const * gi_repository_get_search_path (size_t *n_paths_out);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
const char * gi_repository_load_typelib (GIRepository *repository, const char * gi_repository_load_typelib (GIRepository *repository,
@@ -128,8 +128,9 @@ GIBaseInfo * gi_repository_find_by_name (GIRepository *repository,
const gchar *name); const gchar *name);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GList * gi_repository_enumerate_versions (GIRepository *repository, char ** gi_repository_enumerate_versions (GIRepository *repository,
const gchar *namespace_); const gchar *namespace_,
size_t *n_versions_out);
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GITypelib * gi_repository_require (GIRepository *repository, GITypelib * gi_repository_require (GIRepository *repository,

View File

@@ -256,4 +256,4 @@ endif
if build_tests if build_tests
subdir('tests') subdir('tests')
endif endif

View File

@@ -6,6 +6,10 @@ if enable_gir
'repository' : { 'repository' : {
'depends': [glib_gir, gobject_gir], 'depends': [glib_gir, gobject_gir],
}, },
'repository-search-paths' : {
'c_args': '-DGOBJECT_INTROSPECTION_LIBDIR="@0@"'.format(glib_libdir),
'depends': [glib_gir],
},
} }
endif endif
@@ -82,4 +86,4 @@ foreach test_name, extra_args : girepository_tests
suite: suite, suite: suite,
should_fail: extra_args.get('should_fail', false), should_fail: extra_args.get('should_fail', false),
) )
endforeach endforeach

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2023 Canonical Ltd.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marco Trevisan <marco.trevisan@canonical.com>
*/
#include "glib.h"
#include "girepository.h"
/* Keep this test first, not to add search paths to other tests */
static void
test_repository_search_paths_unset (void)
{
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpstrv (search_paths, ((char *[]){NULL}));
g_assert_cmpuint (n_search_paths, ==, 0);
search_paths = gi_repository_get_search_path (NULL);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 0);
}
static void
test_repository_search_paths_default (void)
{
const char * const *search_paths;
size_t n_search_paths;
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
/* Init default paths */
g_assert_nonnull (gi_repository_get_default ());
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 2);
g_assert_cmpstr (search_paths[0], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (search_paths[1], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
}
static void
test_repository_search_paths_prepend (void)
{
const char * const *search_paths;
size_t n_search_paths;
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_BUILT));
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 3);
g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (search_paths[1], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
char *expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (search_paths[2], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
gi_repository_prepend_search_path (g_test_get_dir (G_TEST_DIST));
search_paths = gi_repository_get_search_path (&n_search_paths);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), ==, 4);
g_assert_cmpstr (search_paths[0], ==, g_test_get_dir (G_TEST_DIST));
g_assert_cmpstr (search_paths[1], ==, g_test_get_dir (G_TEST_BUILT));
g_assert_cmpstr (search_paths[2], ==, g_get_tmp_dir ());
#ifndef G_PLATFORM_WIN32
expected_path = g_build_filename (GOBJECT_INTROSPECTION_LIBDIR, "girepository-1.0", NULL);
g_assert_cmpstr (search_paths[3], ==, expected_path);
g_clear_pointer (&expected_path, g_free);
#endif
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
/* Isolate from the system typelibs and GIRs. */
g_setenv ("GI_TYPELIB_PATH", g_get_tmp_dir (), TRUE);
g_setenv ("GI_GIR_PATH", g_get_user_cache_dir (), TRUE);
g_test_add_func ("/repository-search-paths/unset", test_repository_search_paths_unset);
g_test_add_func ("/repository-search-paths/default", test_repository_search_paths_default);
g_test_add_func ("/repository-search-paths/prepend", test_repository_search_paths_prepend);
return g_test_run ();
}

View File

@@ -27,11 +27,13 @@ test_repository_basic (void)
{ {
GIRepository *repository; GIRepository *repository;
char *gobject_typelib_dir = NULL; char *gobject_typelib_dir = NULL;
GSList *search_path; const char * const * search_paths;
GITypelib *typelib = NULL; GITypelib *typelib = NULL;
char **namespaces = NULL; char **namespaces = NULL;
const char *expected_namespaces[] = { "GLib", NULL }; const char *expected_namespaces[] = { "GLib", NULL };
GError *local_error = NULL; GError *local_error = NULL;
char **versions;
size_t n_versions;
g_test_summary ("Test basic opening of a repository and requiring a typelib"); g_test_summary ("Test basic opening of a repository and requiring a typelib");
@@ -42,9 +44,21 @@ test_repository_basic (void)
repository = gi_repository_new (); repository = gi_repository_new ();
g_assert_nonnull (repository); g_assert_nonnull (repository);
search_path = gi_repository_get_search_path (); versions = gi_repository_enumerate_versions (repository, "SomeInvalidNamespace", &n_versions);
g_assert_nonnull (search_path); g_assert_nonnull (versions);
g_assert_cmpstr (search_path->data, ==, gobject_typelib_dir); g_assert_cmpstrv (versions, ((char *[]){NULL}));
g_assert_cmpuint (n_versions, ==, 0);
g_clear_pointer (&versions, g_strfreev);
versions = gi_repository_enumerate_versions (repository, "GLib", NULL);
g_assert_nonnull (versions);
g_assert_cmpstrv (versions, ((char *[]){"2.0", NULL}));
g_clear_pointer (&versions, g_strfreev);
search_paths = gi_repository_get_search_path (NULL);
g_assert_nonnull (search_paths);
g_assert_cmpuint (g_strv_length ((char **) search_paths), >, 0);
g_assert_cmpstr (search_paths[0], ==, gobject_typelib_dir);
typelib = gi_repository_require (repository, "GLib", "2.0", 0, &local_error); typelib = gi_repository_require (repository, "GLib", "2.0", 0, &local_error);
g_assert_no_error (local_error); g_assert_no_error (local_error);
@@ -100,6 +114,38 @@ test_repository_info (void)
g_clear_object (&repository); g_clear_object (&repository);
} }
static void
test_repository_dependencies (void)
{
GIRepository *repository;
GITypelib *typelib;
GError *error = NULL;
char *gobject_typelib_dir = NULL;
char **dependencies;
g_test_summary ("Test ensures namespace dependencies are correctly exposed");
gobject_typelib_dir = g_test_build_filename (G_TEST_BUILT, "..", "..", "gobject", NULL);
g_test_message ("Using GI_TYPELIB_DIR = %s", gobject_typelib_dir);
gi_repository_prepend_search_path (gobject_typelib_dir);
g_free (gobject_typelib_dir);
repository = gi_repository_new ();
g_assert_nonnull (repository);
typelib = gi_repository_require (repository, "GObject", "2.0", 0, &error);
g_assert_no_error (error);
g_assert_nonnull (typelib);
dependencies = gi_repository_get_dependencies (repository, "GObject");
g_assert_cmpuint (g_strv_length (dependencies), ==, 1);
g_assert_true (g_strv_contains ((const char **) dependencies, "GLib-2.0"));
g_clear_error (&error);
g_clear_object (&repository);
g_clear_pointer (&dependencies, g_strfreev);
}
int int
main (int argc, main (int argc,
char *argv[]) char *argv[])
@@ -112,6 +158,7 @@ main (int argc,
g_test_add_func ("/repository/basic", test_repository_basic); g_test_add_func ("/repository/basic", test_repository_basic);
g_test_add_func ("/repository/info", test_repository_info); g_test_add_func ("/repository/info", test_repository_info);
g_test_add_func ("/repository/dependencies", test_repository_dependencies);
return g_test_run (); return g_test_run ();
} }