/src/tinysparql/subprojects/glib-2.80.3/gio/gresource.c
Line | Count | Source |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright © 2011 Red Hat, Inc |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Authors: Alexander Larsson <alexl@redhat.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include <string.h> |
26 | | |
27 | | #include "gresource.h" |
28 | | #include <gvdb/gvdb-reader.h> |
29 | | #include <gi18n-lib.h> |
30 | | #include <gstdio.h> |
31 | | #include <gio/gfile.h> |
32 | | #include <gio/gioerror.h> |
33 | | #include <gio/gmemoryinputstream.h> |
34 | | #include <gio/gzlibdecompressor.h> |
35 | | #include <gio/gconverterinputstream.h> |
36 | | |
37 | | #include "glib-private.h" |
38 | | |
39 | | struct _GResource |
40 | | { |
41 | | int ref_count; |
42 | | |
43 | | GvdbTable *table; |
44 | | }; |
45 | | |
46 | | static void register_lazy_static_resources (void); |
47 | | |
48 | 0 | G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref) |
49 | 0 |
|
50 | 0 | /** |
51 | 0 | * GResource: |
52 | 0 | * |
53 | 0 | * Applications and libraries often contain binary or textual data that is |
54 | 0 | * really part of the application, rather than user data. For instance |
55 | 0 | * [`GtkBuilder`](https://docs.gtk.org/gtk4/class.Builder.html) `.ui` files, |
56 | 0 | * splashscreen images, [class@Gio.Menu] markup XML, CSS files, icons, etc. |
57 | 0 | * These are often shipped as files in `$datadir/appname`, or manually |
58 | 0 | * included as literal strings in the code. |
59 | 0 | * |
60 | 0 | * The `GResource` API and the |
61 | 0 | * [`glib-compile-resources`](glib-compile-resources.html) program provide a |
62 | 0 | * convenient and efficient alternative to this which has some nice properties. |
63 | 0 | * You maintain the files as normal files, so it’s easy to edit them, but during |
64 | 0 | * the build the files are combined into a binary bundle that is linked into the |
65 | 0 | * executable. This means that loading the resource files are efficient (as they |
66 | 0 | * are already in memory, shared with other instances) and simple (no need to |
67 | 0 | * check for things like I/O errors or locate the files in the filesystem). It |
68 | 0 | * also makes it easier to create relocatable applications. |
69 | 0 | * |
70 | 0 | * Resource files can also be marked as compressed. Such files will be included |
71 | 0 | * in the resource bundle in a compressed form, but will be automatically |
72 | 0 | * uncompressed when the resource is used. This is very useful e.g. for larger |
73 | 0 | * text files that are parsed once (or rarely) and then thrown away. |
74 | 0 | * |
75 | 0 | * Resource files can also be marked to be preprocessed, by setting the value of the |
76 | 0 | * `preprocess` attribute to a comma-separated list of preprocessing options. |
77 | 0 | * The only options currently supported are: |
78 | 0 | * |
79 | 0 | * - `xml-stripblanks` which will use the [`xmllint`](man:xmllint(1)) command |
80 | 0 | * to strip ignorable whitespace from the XML file. For this to work, |
81 | 0 | * the `XMLLINT` environment variable must be set to the full path to |
82 | 0 | * the xmllint executable, or xmllint must be in the `PATH`; otherwise |
83 | 0 | * the preprocessing step is skipped. |
84 | 0 | * |
85 | 0 | * - `to-pixdata` (deprecated since gdk-pixbuf 2.32) which will use the |
86 | 0 | * `gdk-pixbuf-pixdata` command to convert images to the [`GdkPixdata`](https://docs.gtk.org/gdk-pixbuf/class.Pixdata.html) |
87 | 0 | * format, which allows you to create pixbufs directly using the data inside |
88 | 0 | * the resource file, rather than an (uncompressed) copy of it. For this, the |
89 | 0 | * `gdk-pixbuf-pixdata` program must be in the `PATH`, or the |
90 | 0 | * `GDK_PIXBUF_PIXDATA` environment variable must be set to the full path to |
91 | 0 | * the `gdk-pixbuf-pixdata` executable; otherwise the resource compiler will |
92 | 0 | * abort. `to-pixdata` has been deprecated since gdk-pixbuf 2.32, as |
93 | 0 | * `GResource` supports embedding modern image formats just as well. Instead |
94 | 0 | * of using it, embed a PNG or SVG file in your `GResource`. |
95 | 0 | * |
96 | 0 | * - `json-stripblanks` which will use the |
97 | 0 | * [`json-glib-format`](man:json-glib-format(1)) command to strip ignorable |
98 | 0 | * whitespace from the JSON file. For this to work, the `JSON_GLIB_FORMAT` |
99 | 0 | * environment variable must be set to the full path to the |
100 | 0 | * `json-glib-format` executable, or it must be in the `PATH`; otherwise the |
101 | 0 | * preprocessing step is skipped. In addition, at least version 1.6 of |
102 | 0 | * `json-glib-format` is required. |
103 | 0 | * |
104 | 0 | * Resource files will be exported in the `GResource` namespace using the |
105 | 0 | * combination of the given `prefix` and the filename from the `file` element. |
106 | 0 | * The `alias` attribute can be used to alter the filename to expose them at a |
107 | 0 | * different location in the resource namespace. Typically, this is used to |
108 | 0 | * include files from a different source directory without exposing the source |
109 | 0 | * directory in the resource namespace, as in the example below. |
110 | 0 | * |
111 | 0 | * Resource bundles are created by the |
112 | 0 | * [`glib-compile-resources`](glib-compile-resources.html) program |
113 | 0 | * which takes an XML file that describes the bundle, and a set of files that |
114 | 0 | * the XML references. These are combined into a binary resource bundle. |
115 | 0 | * |
116 | 0 | * An example resource description: |
117 | 0 | * ```xml |
118 | 0 | * <?xml version="1.0" encoding="UTF-8"?> |
119 | 0 | * <gresources> |
120 | 0 | * <gresource prefix="/org/gtk/Example"> |
121 | 0 | * <file>data/splashscreen.png</file> |
122 | 0 | * <file compressed="true">dialog.ui</file> |
123 | 0 | * <file preprocess="xml-stripblanks">menumarkup.xml</file> |
124 | 0 | * <file alias="example.css">data/example.css</file> |
125 | 0 | * </gresource> |
126 | 0 | * </gresources> |
127 | 0 | * ``` |
128 | 0 | * |
129 | 0 | * This will create a resource bundle with the following files: |
130 | 0 | * ``` |
131 | 0 | * /org/gtk/Example/data/splashscreen.png |
132 | 0 | * /org/gtk/Example/dialog.ui |
133 | 0 | * /org/gtk/Example/menumarkup.xml |
134 | 0 | * /org/gtk/Example/example.css |
135 | 0 | * ``` |
136 | 0 | * |
137 | 0 | * Note that all resources in the process share the same namespace, so use |
138 | 0 | * Java-style path prefixes (like in the above example) to avoid conflicts. |
139 | 0 | * |
140 | 0 | * You can then use [`glib-compile-resources`](glib-compile-resources.html) to |
141 | 0 | * compile the XML to a binary bundle that you can load with |
142 | 0 | * [func@Gio.Resource.load]. However, it’s more common to use the |
143 | 0 | * `--generate-source` and `--generate-header` arguments to create a source file |
144 | 0 | * and header to link directly into your application. |
145 | 0 | * This will generate `get_resource()`, `register_resource()` and |
146 | 0 | * `unregister_resource()` functions, prefixed by the `--c-name` argument passed |
147 | 0 | * to [`glib-compile-resources`](glib-compile-resources.html). `get_resource()` |
148 | 0 | * returns the generated `GResource` object. The register and unregister |
149 | 0 | * functions register the resource so its files can be accessed using |
150 | 0 | * [func@Gio.resources_lookup_data]. |
151 | 0 | * |
152 | 0 | * Once a `GResource` has been created and registered all the data in it can be |
153 | 0 | * accessed globally in the process by using API calls like |
154 | 0 | * [func@Gio.resources_open_stream] to stream the data or |
155 | 0 | * [func@Gio.resources_lookup_data] to get a direct pointer to the data. You can |
156 | 0 | * also use URIs like `resource:///org/gtk/Example/data/splashscreen.png` with |
157 | 0 | * [iface@Gio.File] to access the resource data. |
158 | 0 | * |
159 | 0 | * Some higher-level APIs, such as [`GtkApplication`](https://docs.gtk.org/gtk4/class.Application.html), |
160 | 0 | * will automatically load resources from certain well-known paths in the |
161 | 0 | * resource namespace as a convenience. See the documentation for those APIs |
162 | 0 | * for details. |
163 | 0 | * |
164 | 0 | * There are two forms of the generated source, the default version uses the |
165 | 0 | * compiler support for constructor and destructor functions (where available) |
166 | 0 | * to automatically create and register the `GResource` on startup or library |
167 | 0 | * load time. If you pass `--manual-register`, two functions to |
168 | 0 | * register/unregister the resource are created instead. This requires an |
169 | 0 | * explicit initialization call in your application/library, but it works on all |
170 | 0 | * platforms, even on the minor ones where constructors are not supported. |
171 | 0 | * (Constructor support is available for at least Win32, Mac OS and Linux.) |
172 | 0 | * |
173 | 0 | * Note that resource data can point directly into the data segment of e.g. a |
174 | 0 | * library, so if you are unloading libraries during runtime you need to be very |
175 | 0 | * careful with keeping around pointers to data from a resource, as this goes |
176 | 0 | * away when the library is unloaded. However, in practice this is not generally |
177 | 0 | * a problem, since most resource accesses are for your own resources, and |
178 | 0 | * resource data is often used once, during parsing, and then released. |
179 | 0 | * |
180 | 0 | * # Overlays |
181 | 0 | * |
182 | 0 | * When debugging a program or testing a change to an installed version, it is |
183 | 0 | * often useful to be able to replace resources in the program or library, |
184 | 0 | * without recompiling, for debugging or quick hacking and testing purposes. |
185 | 0 | * Since GLib 2.50, it is possible to use the `G_RESOURCE_OVERLAYS` environment |
186 | 0 | * variable to selectively overlay resources with replacements from the |
187 | 0 | * filesystem. It is a `G_SEARCHPATH_SEPARATOR`-separated list of substitutions |
188 | 0 | * to perform during resource lookups. It is ignored when running in a setuid |
189 | 0 | * process. |
190 | 0 | * |
191 | 0 | * A substitution has the form |
192 | 0 | * |
193 | 0 | * ``` |
194 | 0 | * /org/gtk/libgtk=/home/desrt/gtk-overlay |
195 | 0 | * ``` |
196 | 0 | * |
197 | 0 | * The part before the `=` is the resource subpath for which the overlay |
198 | 0 | * applies. The part after is a filesystem path which contains files and |
199 | 0 | * subdirectories as you would like to be loaded as resources with the |
200 | 0 | * equivalent names. |
201 | 0 | * |
202 | 0 | * In the example above, if an application tried to load a resource with the |
203 | 0 | * resource path `/org/gtk/libgtk/ui/gtkdialog.ui` then `GResource` would check |
204 | 0 | * the filesystem path `/home/desrt/gtk-overlay/ui/gtkdialog.ui`. If a file was |
205 | 0 | * found there, it would be used instead. This is an overlay, not an outright |
206 | 0 | * replacement, which means that if a file is not found at that path, the |
207 | 0 | * built-in version will be used instead. Whiteouts are not currently |
208 | 0 | * supported. |
209 | 0 | * |
210 | 0 | * Substitutions must start with a slash, and must not contain a trailing slash |
211 | 0 | * before the `=`. The path after the slash should ideally be absolute, but |
212 | 0 | * this is not strictly required. It is possible to overlay the location of a |
213 | 0 | * single resource with an individual file. |
214 | 0 | * |
215 | 0 | * Since: 2.32 |
216 | 0 | */ |
217 | 0 |
|
218 | 0 | /** |
219 | 0 | * GStaticResource: |
220 | 0 | * |
221 | 0 | * #GStaticResource is an opaque data structure and can only be accessed |
222 | 0 | * using the following functions. |
223 | 0 | **/ |
224 | 0 | typedef gboolean (* CheckCandidate) (const gchar *candidate, gpointer user_data); |
225 | 0 |
|
226 | 0 | static gboolean |
227 | 0 | open_overlay_stream (const gchar *candidate, |
228 | 0 | gpointer user_data) |
229 | 0 | { |
230 | 0 | GInputStream **res = (GInputStream **) user_data; |
231 | 0 | GError *error = NULL; |
232 | 0 | GFile *file; |
233 | |
|
234 | 0 | file = g_file_new_for_path (candidate); |
235 | 0 | *res = (GInputStream *) g_file_read (file, NULL, &error); |
236 | |
|
237 | 0 | if (*res) |
238 | 0 | { |
239 | 0 | g_message ("Opened file '%s' as a resource overlay", candidate); |
240 | 0 | } |
241 | 0 | else |
242 | 0 | { |
243 | 0 | if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) |
244 | 0 | g_warning ("Can't open overlay file '%s': %s", candidate, error->message); |
245 | 0 | g_error_free (error); |
246 | 0 | } |
247 | |
|
248 | 0 | g_object_unref (file); |
249 | |
|
250 | 0 | return *res != NULL; |
251 | 0 | } |
252 | | |
253 | | static gboolean |
254 | | get_overlay_bytes (const gchar *candidate, |
255 | | gpointer user_data) |
256 | 0 | { |
257 | 0 | GBytes **res = (GBytes **) user_data; |
258 | 0 | GMappedFile *mapped_file; |
259 | 0 | GError *error = NULL; |
260 | |
|
261 | 0 | mapped_file = g_mapped_file_new (candidate, FALSE, &error); |
262 | |
|
263 | 0 | if (mapped_file) |
264 | 0 | { |
265 | 0 | g_message ("Mapped file '%s' as a resource overlay", candidate); |
266 | 0 | *res = g_mapped_file_get_bytes (mapped_file); |
267 | 0 | g_mapped_file_unref (mapped_file); |
268 | 0 | } |
269 | 0 | else |
270 | 0 | { |
271 | 0 | if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
272 | 0 | g_warning ("Can't mmap overlay file '%s': %s", candidate, error->message); |
273 | 0 | g_error_free (error); |
274 | 0 | } |
275 | |
|
276 | 0 | return *res != NULL; |
277 | 0 | } |
278 | | |
279 | | static gboolean |
280 | | enumerate_overlay_dir (const gchar *candidate, |
281 | | gpointer user_data) |
282 | 0 | { |
283 | 0 | GHashTable **hash = (GHashTable **) user_data; |
284 | 0 | GError *error = NULL; |
285 | 0 | GDir *dir; |
286 | 0 | const gchar *name; |
287 | |
|
288 | 0 | dir = g_dir_open (candidate, 0, &error); |
289 | 0 | if (dir) |
290 | 0 | { |
291 | 0 | if (*hash == NULL) |
292 | | /* note: keep in sync with same line below */ |
293 | 0 | *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
294 | |
|
295 | 0 | g_message ("Enumerating directory '%s' as resource overlay", candidate); |
296 | |
|
297 | 0 | while ((name = g_dir_read_name (dir))) |
298 | 0 | { |
299 | 0 | gchar *fullname = g_build_filename (candidate, name, NULL); |
300 | | |
301 | | /* match gvdb behaviour by suffixing "/" on dirs */ |
302 | 0 | if (g_file_test (fullname, G_FILE_TEST_IS_DIR)) |
303 | 0 | g_hash_table_add (*hash, g_strconcat (name, "/", NULL)); |
304 | 0 | else |
305 | 0 | g_hash_table_add (*hash, g_strdup (name)); |
306 | |
|
307 | 0 | g_free (fullname); |
308 | 0 | } |
309 | |
|
310 | 0 | g_dir_close (dir); |
311 | 0 | } |
312 | 0 | else |
313 | 0 | { |
314 | 0 | if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) |
315 | 0 | g_warning ("Can't enumerate overlay directory '%s': %s", candidate, error->message); |
316 | 0 | g_error_free (error); |
317 | 0 | return FALSE; |
318 | 0 | } |
319 | | |
320 | | /* We may want to enumerate results from more than one overlay |
321 | | * directory. |
322 | | */ |
323 | 0 | return FALSE; |
324 | 0 | } |
325 | | |
326 | | typedef struct { |
327 | | gsize size; |
328 | | guint32 flags; |
329 | | } InfoData; |
330 | | |
331 | | static gboolean |
332 | | get_overlay_info (const gchar *candidate, |
333 | | gpointer user_data) |
334 | 0 | { |
335 | 0 | InfoData *info = user_data; |
336 | 0 | GStatBuf buf; |
337 | |
|
338 | 0 | if (g_stat (candidate, &buf) < 0) |
339 | 0 | return FALSE; |
340 | | |
341 | 0 | info->size = buf.st_size; |
342 | 0 | info->flags = G_RESOURCE_FLAGS_NONE; |
343 | |
|
344 | 0 | return TRUE; |
345 | 0 | } |
346 | | |
347 | | static gboolean |
348 | | g_resource_find_overlay (const gchar *path, |
349 | | CheckCandidate check, |
350 | | gpointer user_data) |
351 | 35 | { |
352 | | /* This is a null-terminated array of replacement strings (with '=' inside) */ |
353 | 35 | static const gchar * const *overlay_dirs; |
354 | 35 | gboolean res = FALSE; |
355 | 35 | gint path_len = -1; |
356 | 35 | gint i; |
357 | | |
358 | | /* We try to be very fast in case there are no overlays. Otherwise, |
359 | | * we can take a bit more time... |
360 | | */ |
361 | | |
362 | 35 | if (g_once_init_enter_pointer (&overlay_dirs)) |
363 | 1 | { |
364 | 1 | gboolean is_setuid = GLIB_PRIVATE_CALL (g_check_setuid) (); |
365 | 1 | const gchar * const *result; |
366 | 1 | const gchar *envvar; |
367 | | |
368 | | /* Don’t load overlays if setuid, as they could allow reading privileged |
369 | | * files. */ |
370 | 1 | envvar = !is_setuid ? g_getenv ("G_RESOURCE_OVERLAYS") : NULL; |
371 | 1 | if (envvar != NULL) |
372 | 0 | { |
373 | 0 | gchar **parts; |
374 | 0 | gint j; |
375 | |
|
376 | 0 | parts = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, 0); |
377 | | |
378 | | /* Sanity check the parts, dropping those that are invalid. |
379 | | * 'i' may grow faster than 'j'. |
380 | | */ |
381 | 0 | for (i = j = 0; parts[i]; i++) |
382 | 0 | { |
383 | 0 | gchar *part = parts[i]; |
384 | 0 | gchar *eq; |
385 | |
|
386 | 0 | eq = strchr (part, '='); |
387 | 0 | if (eq == NULL) |
388 | 0 | { |
389 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks '='. Ignoring.", part); |
390 | 0 | g_free (part); |
391 | 0 | continue; |
392 | 0 | } |
393 | | |
394 | 0 | if (eq == part) |
395 | 0 | { |
396 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path before '='. Ignoring.", part); |
397 | 0 | g_free (part); |
398 | 0 | continue; |
399 | 0 | } |
400 | | |
401 | 0 | if (eq[1] == '\0') |
402 | 0 | { |
403 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks path after '='. Ignoring", part); |
404 | 0 | g_free (part); |
405 | 0 | continue; |
406 | 0 | } |
407 | | |
408 | 0 | if (part[0] != '/') |
409 | 0 | { |
410 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' lacks leading '/'. Ignoring.", part); |
411 | 0 | g_free (part); |
412 | 0 | continue; |
413 | 0 | } |
414 | | |
415 | 0 | if (eq[-1] == '/') |
416 | 0 | { |
417 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' has trailing '/' before '='. Ignoring", part); |
418 | 0 | g_free (part); |
419 | 0 | continue; |
420 | 0 | } |
421 | | |
422 | 0 | if (!g_path_is_absolute (eq + 1)) |
423 | 0 | { |
424 | 0 | g_critical ("G_RESOURCE_OVERLAYS segment '%s' does not have an absolute path after '='. Ignoring", part); |
425 | 0 | g_free (part); |
426 | 0 | continue; |
427 | 0 | } |
428 | | |
429 | 0 | g_message ("Adding GResources overlay '%s'", part); |
430 | 0 | parts[j++] = part; |
431 | 0 | } |
432 | |
|
433 | 0 | parts[j] = NULL; |
434 | |
|
435 | 0 | result = (const gchar **) parts; |
436 | 0 | } |
437 | 1 | else |
438 | 1 | { |
439 | | /* We go out of the way to avoid malloc() in the normal case |
440 | | * where the environment variable is not set. |
441 | | */ |
442 | 1 | static const gchar *const empty_strv[0 + 1] = { 0 }; |
443 | 1 | result = empty_strv; |
444 | 1 | } |
445 | | |
446 | 1 | g_once_init_leave_pointer (&overlay_dirs, result); |
447 | 1 | } |
448 | | |
449 | 35 | for (i = 0; overlay_dirs[i]; i++) |
450 | 0 | { |
451 | 0 | const gchar *src; |
452 | 0 | gint src_len; |
453 | 0 | const gchar *dst; |
454 | 0 | gint dst_len; |
455 | 0 | gchar *candidate; |
456 | |
|
457 | 0 | { |
458 | 0 | gchar *eq; |
459 | | |
460 | | /* split the overlay into src/dst */ |
461 | 0 | src = overlay_dirs[i]; |
462 | 0 | eq = strchr (src, '='); |
463 | 0 | g_assert (eq); /* we checked this already */ |
464 | 0 | src_len = eq - src; |
465 | 0 | dst = eq + 1; |
466 | | /* hold off on dst_len because we will probably fail the checks below */ |
467 | 0 | } |
468 | | |
469 | 0 | if (path_len == -1) |
470 | 0 | path_len = strlen (path); |
471 | | |
472 | | /* The entire path is too short to match the source */ |
473 | 0 | if (path_len < src_len) |
474 | 0 | continue; |
475 | | |
476 | | /* It doesn't match the source */ |
477 | 0 | if (memcmp (path, src, src_len) != 0) |
478 | 0 | continue; |
479 | | |
480 | | /* The prefix matches, but it's not a complete path component */ |
481 | 0 | if (path[src_len] && path[src_len] != '/') |
482 | 0 | continue; |
483 | | |
484 | | /* OK. Now we need this. */ |
485 | 0 | dst_len = strlen (dst); |
486 | | |
487 | | /* The candidate will be composed of: |
488 | | * |
489 | | * dst + remaining_path + nul |
490 | | */ |
491 | 0 | candidate = g_malloc (dst_len + (path_len - src_len) + 1); |
492 | 0 | memcpy (candidate, dst, dst_len); |
493 | 0 | memcpy (candidate + dst_len, path + src_len, path_len - src_len); |
494 | 0 | candidate[dst_len + (path_len - src_len)] = '\0'; |
495 | | |
496 | | /* No matter what, 'r' is what we need, including the case where |
497 | | * we are trying to enumerate a directory. |
498 | | */ |
499 | 0 | res = (* check) (candidate, user_data); |
500 | 0 | g_free (candidate); |
501 | |
|
502 | 0 | if (res) |
503 | 0 | break; |
504 | 0 | } |
505 | | |
506 | 35 | return res; |
507 | 35 | } |
508 | | |
509 | | /** |
510 | | * g_resource_error_quark: |
511 | | * |
512 | | * Gets the #GResource Error Quark. |
513 | | * |
514 | | * Returns: a #GQuark |
515 | | * |
516 | | * Since: 2.32 |
517 | | */ |
518 | | G_DEFINE_QUARK (g-resource-error-quark, g_resource_error) |
519 | | |
520 | | /** |
521 | | * g_resource_ref: |
522 | | * @resource: A #GResource |
523 | | * |
524 | | * Atomically increments the reference count of @resource by one. This |
525 | | * function is MT-safe and may be called from any thread. |
526 | | * |
527 | | * Returns: The passed in #GResource |
528 | | * |
529 | | * Since: 2.32 |
530 | | **/ |
531 | | GResource * |
532 | | g_resource_ref (GResource *resource) |
533 | 14 | { |
534 | 14 | g_atomic_int_inc (&resource->ref_count); |
535 | 14 | return resource; |
536 | 14 | } |
537 | | |
538 | | /** |
539 | | * g_resource_unref: |
540 | | * @resource: A #GResource |
541 | | * |
542 | | * Atomically decrements the reference count of @resource by one. If the |
543 | | * reference count drops to 0, all memory allocated by the resource is |
544 | | * released. This function is MT-safe and may be called from any |
545 | | * thread. |
546 | | * |
547 | | * Since: 2.32 |
548 | | **/ |
549 | | void |
550 | | g_resource_unref (GResource *resource) |
551 | 12 | { |
552 | 12 | if (g_atomic_int_dec_and_test (&resource->ref_count)) |
553 | 0 | { |
554 | 0 | gvdb_table_free (resource->table); |
555 | 0 | g_free (resource); |
556 | 0 | } |
557 | 12 | } |
558 | | |
559 | | /*< internal > |
560 | | * g_resource_new_from_table: |
561 | | * @table: (transfer full): a GvdbTable |
562 | | * |
563 | | * Returns: (transfer full): a new #GResource for @table |
564 | | */ |
565 | | static GResource * |
566 | | g_resource_new_from_table (GvdbTable *table) |
567 | 2 | { |
568 | 2 | GResource *resource; |
569 | | |
570 | 2 | resource = g_new (GResource, 1); |
571 | 2 | resource->ref_count = 1; |
572 | 2 | resource->table = table; |
573 | | |
574 | 2 | return resource; |
575 | 2 | } |
576 | | |
577 | | static void |
578 | | g_resource_error_from_gvdb_table_error (GError **g_resource_error, |
579 | | GError *gvdb_table_error /* (transfer full) */) |
580 | 0 | { |
581 | 0 | if (g_error_matches (gvdb_table_error, G_FILE_ERROR, G_FILE_ERROR_INVAL)) |
582 | 0 | g_set_error_literal (g_resource_error, |
583 | 0 | G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL, |
584 | 0 | gvdb_table_error->message); |
585 | 0 | else |
586 | 0 | g_propagate_error (g_resource_error, g_steal_pointer (&gvdb_table_error)); |
587 | 0 | g_clear_error (&gvdb_table_error); |
588 | 0 | } |
589 | | |
590 | | /** |
591 | | * g_resource_new_from_data: |
592 | | * @data: A #GBytes |
593 | | * @error: return location for a #GError, or %NULL |
594 | | * |
595 | | * Creates a GResource from a reference to the binary resource bundle. |
596 | | * This will keep a reference to @data while the resource lives, so |
597 | | * the data should not be modified or freed. |
598 | | * |
599 | | * If you want to use this resource in the global resource namespace you need |
600 | | * to register it with g_resources_register(). |
601 | | * |
602 | | * Note: @data must be backed by memory that is at least pointer aligned. |
603 | | * Otherwise this function will internally create a copy of the memory since |
604 | | * GLib 2.56, or in older versions fail and exit the process. |
605 | | * |
606 | | * If @data is empty or corrupt, %G_RESOURCE_ERROR_INTERNAL will be returned. |
607 | | * |
608 | | * Returns: (transfer full): a new #GResource, or %NULL on error |
609 | | * |
610 | | * Since: 2.32 |
611 | | **/ |
612 | | GResource * |
613 | | g_resource_new_from_data (GBytes *data, |
614 | | GError **error) |
615 | 2 | { |
616 | 2 | GvdbTable *table; |
617 | 2 | gboolean unref_data = FALSE; |
618 | 2 | GError *local_error = NULL; |
619 | | |
620 | 2 | if (((guintptr) g_bytes_get_data (data, NULL)) % sizeof (gpointer) != 0) |
621 | 0 | { |
622 | 0 | data = g_bytes_new (g_bytes_get_data (data, NULL), |
623 | 0 | g_bytes_get_size (data)); |
624 | 0 | unref_data = TRUE; |
625 | 0 | } |
626 | | |
627 | 2 | table = gvdb_table_new_from_bytes (data, TRUE, &local_error); |
628 | | |
629 | 2 | if (unref_data) |
630 | 0 | g_bytes_unref (data); |
631 | | |
632 | 2 | if (table == NULL) |
633 | 0 | { |
634 | 0 | g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error)); |
635 | 0 | return NULL; |
636 | 0 | } |
637 | | |
638 | 2 | return g_resource_new_from_table (table); |
639 | 2 | } |
640 | | |
641 | | /** |
642 | | * g_resource_load: |
643 | | * @filename: (type filename): the path of a filename to load, in the GLib filename encoding |
644 | | * @error: return location for a #GError, or %NULL |
645 | | * |
646 | | * Loads a binary resource bundle and creates a #GResource representation of it, allowing |
647 | | * you to query it for data. |
648 | | * |
649 | | * If you want to use this resource in the global resource namespace you need |
650 | | * to register it with g_resources_register(). |
651 | | * |
652 | | * If @filename is empty or the data in it is corrupt, |
653 | | * %G_RESOURCE_ERROR_INTERNAL will be returned. If @filename doesn’t exist, or |
654 | | * there is an error in reading it, an error from g_mapped_file_new() will be |
655 | | * returned. |
656 | | * |
657 | | * Returns: (transfer full): a new #GResource, or %NULL on error |
658 | | * |
659 | | * Since: 2.32 |
660 | | **/ |
661 | | GResource * |
662 | | g_resource_load (const gchar *filename, |
663 | | GError **error) |
664 | 0 | { |
665 | 0 | GvdbTable *table; |
666 | 0 | GError *local_error = NULL; |
667 | |
|
668 | 0 | table = gvdb_table_new (filename, FALSE, &local_error); |
669 | 0 | if (table == NULL) |
670 | 0 | { |
671 | 0 | g_resource_error_from_gvdb_table_error (error, g_steal_pointer (&local_error)); |
672 | 0 | return NULL; |
673 | 0 | } |
674 | | |
675 | 0 | return g_resource_new_from_table (table); |
676 | 0 | } |
677 | | |
678 | | static gboolean |
679 | | do_lookup (GResource *resource, |
680 | | const gchar *path, |
681 | | GResourceLookupFlags lookup_flags, |
682 | | gsize *size, |
683 | | guint32 *flags, |
684 | | const void **data, |
685 | | gsize *data_size, |
686 | | GError **error) |
687 | 20 | { |
688 | 20 | char *free_path = NULL; |
689 | 20 | gsize path_len; |
690 | 20 | gboolean res = FALSE; |
691 | 20 | GVariant *value; |
692 | | |
693 | | /* Drop any trailing slash. */ |
694 | 20 | path_len = strlen (path); |
695 | 20 | if (path_len >= 1 && path[path_len-1] == '/') |
696 | 0 | { |
697 | 0 | path = free_path = g_strdup (path); |
698 | 0 | free_path[path_len-1] = 0; |
699 | 0 | } |
700 | | |
701 | 20 | value = gvdb_table_get_raw_value (resource->table, path); |
702 | | |
703 | 20 | if (value == NULL) |
704 | 0 | { |
705 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
706 | 0 | _("The resource at “%s” does not exist"), |
707 | 0 | path); |
708 | 0 | } |
709 | 20 | else |
710 | 20 | { |
711 | 20 | guint32 _size, _flags; |
712 | 20 | GVariant *array; |
713 | | |
714 | 20 | g_variant_get (value, "(uu@ay)", |
715 | 20 | &_size, |
716 | 20 | &_flags, |
717 | 20 | &array); |
718 | | |
719 | 20 | _size = GUINT32_FROM_LE (_size); |
720 | 20 | _flags = GUINT32_FROM_LE (_flags); |
721 | | |
722 | 20 | if (size) |
723 | 8 | *size = _size; |
724 | 20 | if (flags) |
725 | 20 | *flags = _flags; |
726 | 20 | if (data) |
727 | 12 | *data = g_variant_get_data (array); |
728 | 20 | if (data_size) |
729 | 12 | { |
730 | | /* Don't report trailing newline that non-compressed files has */ |
731 | 12 | if (_flags & G_RESOURCE_FLAGS_COMPRESSED) |
732 | 12 | *data_size = g_variant_get_size (array); |
733 | 0 | else |
734 | 0 | *data_size = g_variant_get_size (array) - 1; |
735 | 12 | } |
736 | 20 | g_variant_unref (array); |
737 | 20 | g_variant_unref (value); |
738 | | |
739 | 20 | res = TRUE; |
740 | 20 | } |
741 | | |
742 | 20 | g_free (free_path); |
743 | 20 | return res; |
744 | 20 | } |
745 | | |
746 | | /** |
747 | | * g_resource_open_stream: |
748 | | * @resource: A #GResource |
749 | | * @path: A pathname inside the resource |
750 | | * @lookup_flags: A #GResourceLookupFlags |
751 | | * @error: return location for a #GError, or %NULL |
752 | | * |
753 | | * Looks for a file at the specified @path in the resource and |
754 | | * returns a #GInputStream that lets you read the data. |
755 | | * |
756 | | * @lookup_flags controls the behaviour of the lookup. |
757 | | * |
758 | | * Returns: (transfer full): #GInputStream or %NULL on error. |
759 | | * Free the returned object with g_object_unref() |
760 | | * |
761 | | * Since: 2.32 |
762 | | **/ |
763 | | GInputStream * |
764 | | g_resource_open_stream (GResource *resource, |
765 | | const gchar *path, |
766 | | GResourceLookupFlags lookup_flags, |
767 | | GError **error) |
768 | 12 | { |
769 | 12 | const void *data; |
770 | 12 | gsize data_size; |
771 | 12 | guint32 flags; |
772 | 12 | GInputStream *stream, *stream2; |
773 | | |
774 | 12 | if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error)) |
775 | 0 | return NULL; |
776 | | |
777 | 12 | stream = g_memory_input_stream_new_from_data (data, data_size, NULL); |
778 | 12 | g_object_set_data_full (G_OBJECT (stream), "g-resource", |
779 | 12 | g_resource_ref (resource), |
780 | 12 | (GDestroyNotify)g_resource_unref); |
781 | | |
782 | 12 | if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
783 | 12 | { |
784 | 12 | GZlibDecompressor *decompressor = |
785 | 12 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
786 | | |
787 | 12 | stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor)); |
788 | 12 | g_object_unref (decompressor); |
789 | 12 | g_object_unref (stream); |
790 | 12 | stream = stream2; |
791 | 12 | } |
792 | | |
793 | 12 | return stream; |
794 | 12 | } |
795 | | |
796 | | /** |
797 | | * g_resource_lookup_data: |
798 | | * @resource: A #GResource |
799 | | * @path: A pathname inside the resource |
800 | | * @lookup_flags: A #GResourceLookupFlags |
801 | | * @error: return location for a #GError, or %NULL |
802 | | * |
803 | | * Looks for a file at the specified @path in the resource and |
804 | | * returns a #GBytes that lets you directly access the data in |
805 | | * memory. |
806 | | * |
807 | | * The data is always followed by a zero byte, so you |
808 | | * can safely use the data as a C string. However, that byte |
809 | | * is not included in the size of the GBytes. |
810 | | * |
811 | | * For uncompressed resource files this is a pointer directly into |
812 | | * the resource bundle, which is typically in some readonly data section |
813 | | * in the program binary. For compressed files we allocate memory on |
814 | | * the heap and automatically uncompress the data. |
815 | | * |
816 | | * @lookup_flags controls the behaviour of the lookup. |
817 | | * |
818 | | * Returns: (transfer full): #GBytes or %NULL on error. |
819 | | * Free the returned object with g_bytes_unref() |
820 | | * |
821 | | * Since: 2.32 |
822 | | **/ |
823 | | GBytes * |
824 | | g_resource_lookup_data (GResource *resource, |
825 | | const gchar *path, |
826 | | GResourceLookupFlags lookup_flags, |
827 | | GError **error) |
828 | 0 | { |
829 | 0 | const void *data; |
830 | 0 | guint32 flags; |
831 | 0 | gsize data_size; |
832 | 0 | gsize size; |
833 | |
|
834 | 0 | if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error)) |
835 | 0 | return NULL; |
836 | | |
837 | 0 | if (size == 0) |
838 | 0 | return g_bytes_new_with_free_func ("", 0, (GDestroyNotify) g_resource_unref, g_resource_ref (resource)); |
839 | 0 | else if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
840 | 0 | { |
841 | 0 | char *uncompressed, *d; |
842 | 0 | const char *s; |
843 | 0 | GConverterResult res; |
844 | 0 | gsize d_size, s_size; |
845 | 0 | gsize bytes_read, bytes_written; |
846 | | |
847 | |
|
848 | 0 | GZlibDecompressor *decompressor = |
849 | 0 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
850 | |
|
851 | 0 | uncompressed = g_malloc (size + 1); |
852 | |
|
853 | 0 | s = data; |
854 | 0 | s_size = data_size; |
855 | 0 | d = uncompressed; |
856 | 0 | d_size = size; |
857 | |
|
858 | 0 | do |
859 | 0 | { |
860 | 0 | res = g_converter_convert (G_CONVERTER (decompressor), |
861 | 0 | s, s_size, |
862 | 0 | d, d_size, |
863 | 0 | G_CONVERTER_INPUT_AT_END, |
864 | 0 | &bytes_read, |
865 | 0 | &bytes_written, |
866 | 0 | NULL); |
867 | 0 | if (res == G_CONVERTER_ERROR) |
868 | 0 | { |
869 | 0 | g_free (uncompressed); |
870 | 0 | g_object_unref (decompressor); |
871 | |
|
872 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL, |
873 | 0 | _("The resource at “%s” failed to decompress"), |
874 | 0 | path); |
875 | 0 | return NULL; |
876 | |
|
877 | 0 | } |
878 | 0 | s += bytes_read; |
879 | 0 | s_size -= bytes_read; |
880 | 0 | d += bytes_written; |
881 | 0 | d_size -= bytes_written; |
882 | 0 | } |
883 | 0 | while (res != G_CONVERTER_FINISHED); |
884 | | |
885 | 0 | uncompressed[size] = 0; /* Zero terminate */ |
886 | |
|
887 | 0 | g_object_unref (decompressor); |
888 | |
|
889 | 0 | return g_bytes_new_take (uncompressed, size); |
890 | 0 | } |
891 | 0 | else |
892 | 0 | return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource)); |
893 | 0 | } |
894 | | |
895 | | /** |
896 | | * g_resource_get_info: |
897 | | * @resource: A #GResource |
898 | | * @path: A pathname inside the resource |
899 | | * @lookup_flags: A #GResourceLookupFlags |
900 | | * @size: (out) (optional): a location to place the length of the contents of the file, |
901 | | * or %NULL if the length is not needed |
902 | | * @flags: (out) (optional): a location to place the flags about the file, |
903 | | * or %NULL if the length is not needed |
904 | | * @error: return location for a #GError, or %NULL |
905 | | * |
906 | | * Looks for a file at the specified @path in the resource and |
907 | | * if found returns information about it. |
908 | | * |
909 | | * @lookup_flags controls the behaviour of the lookup. |
910 | | * |
911 | | * Returns: %TRUE if the file was found. %FALSE if there were errors |
912 | | * |
913 | | * Since: 2.32 |
914 | | **/ |
915 | | gboolean |
916 | | g_resource_get_info (GResource *resource, |
917 | | const gchar *path, |
918 | | GResourceLookupFlags lookup_flags, |
919 | | gsize *size, |
920 | | guint32 *flags, |
921 | | GError **error) |
922 | 8 | { |
923 | 8 | return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error); |
924 | 8 | } |
925 | | |
926 | | /** |
927 | | * g_resource_enumerate_children: |
928 | | * @resource: A #GResource |
929 | | * @path: A pathname inside the resource |
930 | | * @lookup_flags: A #GResourceLookupFlags |
931 | | * @error: return location for a #GError, or %NULL |
932 | | * |
933 | | * Returns all the names of children at the specified @path in the resource. |
934 | | * The return result is a %NULL terminated list of strings which should |
935 | | * be released with g_strfreev(). |
936 | | * |
937 | | * If @path is invalid or does not exist in the #GResource, |
938 | | * %G_RESOURCE_ERROR_NOT_FOUND will be returned. |
939 | | * |
940 | | * @lookup_flags controls the behaviour of the lookup. |
941 | | * |
942 | | * Returns: (array zero-terminated=1) (transfer full): an array of constant strings |
943 | | * |
944 | | * Since: 2.32 |
945 | | **/ |
946 | | gchar ** |
947 | | g_resource_enumerate_children (GResource *resource, |
948 | | const gchar *path, |
949 | | GResourceLookupFlags lookup_flags, |
950 | | GError **error) |
951 | 30 | { |
952 | 30 | gchar local_str[256]; |
953 | 30 | const gchar *path_with_slash; |
954 | 30 | gchar **children; |
955 | 30 | gchar *free_path = NULL; |
956 | 30 | gsize path_len; |
957 | | |
958 | | /* |
959 | | * Size of 256 is arbitrarily chosen based on being large enough |
960 | | * for pretty much everything we come across, but not cumbersome |
961 | | * on the stack. It also matches common cacheline sizes. |
962 | | */ |
963 | | |
964 | 30 | if (*path == 0) |
965 | 0 | { |
966 | 0 | if (error) |
967 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
968 | 0 | _("The resource at “%s” does not exist"), |
969 | 0 | path); |
970 | 0 | return NULL; |
971 | 0 | } |
972 | | |
973 | 30 | path_len = strlen (path); |
974 | | |
975 | 30 | if G_UNLIKELY (path[path_len-1] != '/') |
976 | 24 | { |
977 | 24 | if (path_len < sizeof (local_str) - 2) |
978 | 24 | { |
979 | | /* |
980 | | * We got a path that does not have a trailing /. It is not the |
981 | | * ideal use of this API as we require trailing / for our lookup |
982 | | * into gvdb. Some degenerate application configurations can hit |
983 | | * this code path quite a bit, so we try to avoid using the |
984 | | * g_strconcat()/g_free(). |
985 | | */ |
986 | 24 | memcpy (local_str, path, path_len); |
987 | 24 | local_str[path_len] = '/'; |
988 | 24 | local_str[path_len+1] = 0; |
989 | 24 | path_with_slash = local_str; |
990 | 24 | } |
991 | 0 | else |
992 | 0 | { |
993 | 0 | path_with_slash = free_path = g_strconcat (path, "/", NULL); |
994 | 0 | } |
995 | 24 | } |
996 | 6 | else |
997 | 6 | { |
998 | 6 | path_with_slash = path; |
999 | 6 | } |
1000 | | |
1001 | 30 | children = gvdb_table_list (resource->table, path_with_slash); |
1002 | 30 | g_free (free_path); |
1003 | | |
1004 | 30 | if (children == NULL) |
1005 | 18 | { |
1006 | 18 | if (error) |
1007 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1008 | 0 | _("The resource at “%s” does not exist"), |
1009 | 0 | path); |
1010 | 18 | return NULL; |
1011 | 18 | } |
1012 | | |
1013 | 12 | return children; |
1014 | 30 | } |
1015 | | |
1016 | | static GRWLock resources_lock; |
1017 | | static GList *registered_resources; |
1018 | | |
1019 | | /* This is updated atomically, so we can append to it and check for NULL outside the |
1020 | | lock, but all other accesses are done under the write lock */ |
1021 | | static GStaticResource *lazy_register_resources; |
1022 | | |
1023 | | static void |
1024 | | g_resources_register_unlocked (GResource *resource) |
1025 | 2 | { |
1026 | 2 | registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource)); |
1027 | 2 | } |
1028 | | |
1029 | | static void |
1030 | | g_resources_unregister_unlocked (GResource *resource) |
1031 | 0 | { |
1032 | 0 | if (g_list_find (registered_resources, resource) == NULL) |
1033 | 0 | { |
1034 | 0 | g_warning ("Tried to remove not registered resource"); |
1035 | 0 | } |
1036 | 0 | else |
1037 | 0 | { |
1038 | 0 | registered_resources = g_list_remove (registered_resources, resource); |
1039 | 0 | g_resource_unref (resource); |
1040 | 0 | } |
1041 | 0 | } |
1042 | | |
1043 | | /** |
1044 | | * g_resources_register: |
1045 | | * @resource: A #GResource |
1046 | | * |
1047 | | * Registers the resource with the process-global set of resources. |
1048 | | * Once a resource is registered the files in it can be accessed |
1049 | | * with the global resource lookup functions like g_resources_lookup_data(). |
1050 | | * |
1051 | | * Since: 2.32 |
1052 | | **/ |
1053 | | void |
1054 | | g_resources_register (GResource *resource) |
1055 | 0 | { |
1056 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1057 | 0 | g_resources_register_unlocked (resource); |
1058 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1059 | 0 | } |
1060 | | |
1061 | | /** |
1062 | | * g_resources_unregister: |
1063 | | * @resource: A #GResource |
1064 | | * |
1065 | | * Unregisters the resource from the process-global set of resources. |
1066 | | * |
1067 | | * Since: 2.32 |
1068 | | **/ |
1069 | | void |
1070 | | g_resources_unregister (GResource *resource) |
1071 | 0 | { |
1072 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1073 | 0 | g_resources_unregister_unlocked (resource); |
1074 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1075 | 0 | } |
1076 | | |
1077 | | /** |
1078 | | * g_resources_open_stream: |
1079 | | * @path: A pathname inside the resource |
1080 | | * @lookup_flags: A #GResourceLookupFlags |
1081 | | * @error: return location for a #GError, or %NULL |
1082 | | * |
1083 | | * Looks for a file at the specified @path in the set of |
1084 | | * globally registered resources and returns a #GInputStream |
1085 | | * that lets you read the data. |
1086 | | * |
1087 | | * @lookup_flags controls the behaviour of the lookup. |
1088 | | * |
1089 | | * Returns: (transfer full): #GInputStream or %NULL on error. |
1090 | | * Free the returned object with g_object_unref() |
1091 | | * |
1092 | | * Since: 2.32 |
1093 | | **/ |
1094 | | GInputStream * |
1095 | | g_resources_open_stream (const gchar *path, |
1096 | | GResourceLookupFlags lookup_flags, |
1097 | | GError **error) |
1098 | 12 | { |
1099 | 12 | GInputStream *res = NULL; |
1100 | 12 | GList *l; |
1101 | 12 | GInputStream *stream; |
1102 | | |
1103 | 12 | if (g_resource_find_overlay (path, open_overlay_stream, &res)) |
1104 | 0 | return res; |
1105 | | |
1106 | 12 | register_lazy_static_resources (); |
1107 | | |
1108 | 12 | g_rw_lock_reader_lock (&resources_lock); |
1109 | | |
1110 | 12 | for (l = registered_resources; l != NULL; l = l->next) |
1111 | 12 | { |
1112 | 12 | GResource *r = l->data; |
1113 | 12 | GError *my_error = NULL; |
1114 | | |
1115 | 12 | stream = g_resource_open_stream (r, path, lookup_flags, &my_error); |
1116 | 12 | if (stream == NULL && |
1117 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1118 | 0 | { |
1119 | 0 | g_clear_error (&my_error); |
1120 | 0 | } |
1121 | 12 | else |
1122 | 12 | { |
1123 | 12 | if (stream == NULL) |
1124 | 0 | g_propagate_error (error, my_error); |
1125 | 12 | res = stream; |
1126 | 12 | break; |
1127 | 12 | } |
1128 | 12 | } |
1129 | | |
1130 | 12 | if (l == NULL) |
1131 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1132 | 0 | _("The resource at “%s” does not exist"), |
1133 | 0 | path); |
1134 | | |
1135 | 12 | g_rw_lock_reader_unlock (&resources_lock); |
1136 | | |
1137 | 12 | return res; |
1138 | 12 | } |
1139 | | |
1140 | | /** |
1141 | | * g_resources_lookup_data: |
1142 | | * @path: A pathname inside the resource |
1143 | | * @lookup_flags: A #GResourceLookupFlags |
1144 | | * @error: return location for a #GError, or %NULL |
1145 | | * |
1146 | | * Looks for a file at the specified @path in the set of |
1147 | | * globally registered resources and returns a #GBytes that |
1148 | | * lets you directly access the data in memory. |
1149 | | * |
1150 | | * The data is always followed by a zero byte, so you |
1151 | | * can safely use the data as a C string. However, that byte |
1152 | | * is not included in the size of the GBytes. |
1153 | | * |
1154 | | * For uncompressed resource files this is a pointer directly into |
1155 | | * the resource bundle, which is typically in some readonly data section |
1156 | | * in the program binary. For compressed files we allocate memory on |
1157 | | * the heap and automatically uncompress the data. |
1158 | | * |
1159 | | * @lookup_flags controls the behaviour of the lookup. |
1160 | | * |
1161 | | * Returns: (transfer full): #GBytes or %NULL on error. |
1162 | | * Free the returned object with g_bytes_unref() |
1163 | | * |
1164 | | * Since: 2.32 |
1165 | | **/ |
1166 | | GBytes * |
1167 | | g_resources_lookup_data (const gchar *path, |
1168 | | GResourceLookupFlags lookup_flags, |
1169 | | GError **error) |
1170 | 0 | { |
1171 | 0 | GBytes *res = NULL; |
1172 | 0 | GList *l; |
1173 | 0 | GBytes *data; |
1174 | |
|
1175 | 0 | if (g_resource_find_overlay (path, get_overlay_bytes, &res)) |
1176 | 0 | return res; |
1177 | | |
1178 | 0 | register_lazy_static_resources (); |
1179 | |
|
1180 | 0 | g_rw_lock_reader_lock (&resources_lock); |
1181 | |
|
1182 | 0 | for (l = registered_resources; l != NULL; l = l->next) |
1183 | 0 | { |
1184 | 0 | GResource *r = l->data; |
1185 | 0 | GError *my_error = NULL; |
1186 | |
|
1187 | 0 | data = g_resource_lookup_data (r, path, lookup_flags, &my_error); |
1188 | 0 | if (data == NULL && |
1189 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1190 | 0 | { |
1191 | 0 | g_clear_error (&my_error); |
1192 | 0 | } |
1193 | 0 | else |
1194 | 0 | { |
1195 | 0 | if (data == NULL) |
1196 | 0 | g_propagate_error (error, my_error); |
1197 | 0 | res = data; |
1198 | 0 | break; |
1199 | 0 | } |
1200 | 0 | } |
1201 | |
|
1202 | 0 | if (l == NULL) |
1203 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1204 | 0 | _("The resource at “%s” does not exist"), |
1205 | 0 | path); |
1206 | |
|
1207 | 0 | g_rw_lock_reader_unlock (&resources_lock); |
1208 | |
|
1209 | 0 | return res; |
1210 | 0 | } |
1211 | | |
1212 | | /** |
1213 | | * g_resources_enumerate_children: |
1214 | | * @path: A pathname inside the resource |
1215 | | * @lookup_flags: A #GResourceLookupFlags |
1216 | | * @error: return location for a #GError, or %NULL |
1217 | | * |
1218 | | * Returns all the names of children at the specified @path in the set of |
1219 | | * globally registered resources. |
1220 | | * The return result is a %NULL terminated list of strings which should |
1221 | | * be released with g_strfreev(). |
1222 | | * |
1223 | | * @lookup_flags controls the behaviour of the lookup. |
1224 | | * |
1225 | | * Returns: (array zero-terminated=1) (transfer full): an array of constant strings |
1226 | | * |
1227 | | * Since: 2.32 |
1228 | | **/ |
1229 | | gchar ** |
1230 | | g_resources_enumerate_children (const gchar *path, |
1231 | | GResourceLookupFlags lookup_flags, |
1232 | | GError **error) |
1233 | 15 | { |
1234 | 15 | GHashTable *hash = NULL; |
1235 | 15 | GList *l; |
1236 | 15 | char **children; |
1237 | 15 | int i; |
1238 | | |
1239 | | /* This will enumerate actual files found in overlay directories but |
1240 | | * will not enumerate the overlays themselves. For example, if we |
1241 | | * have an overlay "/org/gtk=/path/to/files" and we enumerate "/org" |
1242 | | * then we will not see "gtk" in the result set unless it is provided |
1243 | | * by another resource file. |
1244 | | * |
1245 | | * This is probably not going to be a problem since if we are doing |
1246 | | * such an overlay, we probably will already have that path. |
1247 | | */ |
1248 | 15 | g_resource_find_overlay (path, enumerate_overlay_dir, &hash); |
1249 | | |
1250 | 15 | register_lazy_static_resources (); |
1251 | | |
1252 | 15 | g_rw_lock_reader_lock (&resources_lock); |
1253 | | |
1254 | 45 | for (l = registered_resources; l != NULL; l = l->next) |
1255 | 30 | { |
1256 | 30 | GResource *r = l->data; |
1257 | | |
1258 | 30 | children = g_resource_enumerate_children (r, path, 0, NULL); |
1259 | | |
1260 | 30 | if (children != NULL) |
1261 | 12 | { |
1262 | 12 | if (hash == NULL) |
1263 | | /* note: keep in sync with same line above */ |
1264 | 7 | hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
1265 | | |
1266 | 30 | for (i = 0; children[i] != NULL; i++) |
1267 | 18 | g_hash_table_add (hash, children[i]); |
1268 | 12 | g_free (children); |
1269 | 12 | } |
1270 | 30 | } |
1271 | | |
1272 | 15 | g_rw_lock_reader_unlock (&resources_lock); |
1273 | | |
1274 | 15 | if (hash == NULL) |
1275 | 8 | { |
1276 | 8 | if (error) |
1277 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1278 | 0 | _("The resource at “%s” does not exist"), |
1279 | 0 | path); |
1280 | 8 | return NULL; |
1281 | 8 | } |
1282 | 7 | else |
1283 | 7 | { |
1284 | 7 | children = (gchar **) g_hash_table_get_keys_as_array (hash, NULL); |
1285 | 7 | g_hash_table_steal_all (hash); |
1286 | 7 | g_hash_table_destroy (hash); |
1287 | | |
1288 | 7 | return children; |
1289 | 7 | } |
1290 | 15 | } |
1291 | | |
1292 | | /** |
1293 | | * g_resources_get_info: |
1294 | | * @path: A pathname inside the resource |
1295 | | * @lookup_flags: A #GResourceLookupFlags |
1296 | | * @size: (out) (optional): a location to place the length of the contents of the file, |
1297 | | * or %NULL if the length is not needed |
1298 | | * @flags: (out) (optional): a location to place the #GResourceFlags about the file, |
1299 | | * or %NULL if the flags are not needed |
1300 | | * @error: return location for a #GError, or %NULL |
1301 | | * |
1302 | | * Looks for a file at the specified @path in the set of |
1303 | | * globally registered resources and if found returns information about it. |
1304 | | * |
1305 | | * @lookup_flags controls the behaviour of the lookup. |
1306 | | * |
1307 | | * Returns: %TRUE if the file was found. %FALSE if there were errors |
1308 | | * |
1309 | | * Since: 2.32 |
1310 | | **/ |
1311 | | gboolean |
1312 | | g_resources_get_info (const gchar *path, |
1313 | | GResourceLookupFlags lookup_flags, |
1314 | | gsize *size, |
1315 | | guint32 *flags, |
1316 | | GError **error) |
1317 | 8 | { |
1318 | 8 | gboolean res = FALSE; |
1319 | 8 | GList *l; |
1320 | 8 | gboolean r_res; |
1321 | 8 | InfoData info; |
1322 | | |
1323 | 8 | if (g_resource_find_overlay (path, get_overlay_info, &info)) |
1324 | 0 | { |
1325 | 0 | if (size) |
1326 | 0 | *size = info.size; |
1327 | 0 | if (flags) |
1328 | 0 | *flags = info.flags; |
1329 | |
|
1330 | 0 | return TRUE; |
1331 | 0 | } |
1332 | | |
1333 | 8 | register_lazy_static_resources (); |
1334 | | |
1335 | 8 | g_rw_lock_reader_lock (&resources_lock); |
1336 | | |
1337 | 8 | for (l = registered_resources; l != NULL; l = l->next) |
1338 | 8 | { |
1339 | 8 | GResource *r = l->data; |
1340 | 8 | GError *my_error = NULL; |
1341 | | |
1342 | 8 | r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error); |
1343 | 8 | if (!r_res && |
1344 | 0 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
1345 | 0 | { |
1346 | 0 | g_clear_error (&my_error); |
1347 | 0 | } |
1348 | 8 | else |
1349 | 8 | { |
1350 | 8 | if (!r_res) |
1351 | 0 | g_propagate_error (error, my_error); |
1352 | 8 | res = r_res; |
1353 | 8 | break; |
1354 | 8 | } |
1355 | 8 | } |
1356 | | |
1357 | 8 | if (l == NULL) |
1358 | 0 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
1359 | 0 | _("The resource at “%s” does not exist"), |
1360 | 0 | path); |
1361 | | |
1362 | 8 | g_rw_lock_reader_unlock (&resources_lock); |
1363 | | |
1364 | 8 | return res; |
1365 | 8 | } |
1366 | | |
1367 | | /* This code is to handle registration of resources very early, from a constructor. |
1368 | | * At that point we'd like to do minimal work, to avoid ordering issues. For instance, |
1369 | | * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable |
1370 | | * before the first call to g_malloc. |
1371 | | * |
1372 | | * So, what we do at construction time is that we just register a static structure on |
1373 | | * a list of resources that need to be initialized, and then later, when doing any lookups |
1374 | | * in the global list of registered resources, or when getting a reference to the |
1375 | | * lazily initialized resource we lazily create and register all the GResources on |
1376 | | * the lazy list. |
1377 | | * |
1378 | | * To avoid having to use locks in the constructor, and having to grab the writer lock |
1379 | | * when checking the lazy registering list we update lazy_register_resources in |
1380 | | * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all |
1381 | | * operations except: |
1382 | | * * check if there are any resources to lazily initialize |
1383 | | * * Add a static resource to the lazy init list |
1384 | | * Do use the full writer lock for protection. |
1385 | | */ |
1386 | | |
1387 | | static void |
1388 | | register_lazy_static_resources_unlocked (void) |
1389 | 1 | { |
1390 | 1 | GStaticResource *list = g_atomic_pointer_get (&lazy_register_resources); |
1391 | | |
1392 | 1 | while (!g_atomic_pointer_compare_and_exchange_full (&lazy_register_resources, list, NULL, &list)) |
1393 | 0 | ; |
1394 | | |
1395 | 3 | while (list != NULL) |
1396 | 2 | { |
1397 | 2 | GBytes *bytes = g_bytes_new_static (list->data, list->data_len); |
1398 | 2 | GResource *resource = g_resource_new_from_data (bytes, NULL); |
1399 | 2 | if (resource) |
1400 | 2 | { |
1401 | 2 | g_resources_register_unlocked (resource); |
1402 | 2 | g_atomic_pointer_set (&list->resource, resource); |
1403 | 2 | } |
1404 | 2 | g_bytes_unref (bytes); |
1405 | | |
1406 | 2 | list = list->next; |
1407 | 2 | } |
1408 | 1 | } |
1409 | | |
1410 | | static void |
1411 | | register_lazy_static_resources (void) |
1412 | 35 | { |
1413 | 35 | if (g_atomic_pointer_get (&lazy_register_resources) == NULL) |
1414 | 34 | return; |
1415 | | |
1416 | 1 | g_rw_lock_writer_lock (&resources_lock); |
1417 | 1 | register_lazy_static_resources_unlocked (); |
1418 | 1 | g_rw_lock_writer_unlock (&resources_lock); |
1419 | 1 | } |
1420 | | |
1421 | | /** |
1422 | | * g_static_resource_init: |
1423 | | * @static_resource: pointer to a static #GStaticResource |
1424 | | * |
1425 | | * Initializes a GResource from static data using a |
1426 | | * GStaticResource. |
1427 | | * |
1428 | | * This is normally used by code generated by |
1429 | | * [glib-compile-resources][glib-compile-resources] |
1430 | | * and is not typically used by other code. |
1431 | | * |
1432 | | * Since: 2.32 |
1433 | | **/ |
1434 | | void |
1435 | | g_static_resource_init (GStaticResource *static_resource) |
1436 | 2 | { |
1437 | 2 | GStaticResource *next; |
1438 | | |
1439 | 2 | do |
1440 | 2 | { |
1441 | 2 | next = g_atomic_pointer_get (&lazy_register_resources); |
1442 | 2 | static_resource->next = next; |
1443 | 2 | } |
1444 | 2 | while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource)); |
1445 | 2 | } |
1446 | | |
1447 | | /** |
1448 | | * g_static_resource_fini: |
1449 | | * @static_resource: pointer to a static #GStaticResource |
1450 | | * |
1451 | | * Finalized a GResource initialized by g_static_resource_init(). |
1452 | | * |
1453 | | * This is normally used by code generated by |
1454 | | * [glib-compile-resources][glib-compile-resources] |
1455 | | * and is not typically used by other code. |
1456 | | * |
1457 | | * Since: 2.32 |
1458 | | **/ |
1459 | | void |
1460 | | g_static_resource_fini (GStaticResource *static_resource) |
1461 | 0 | { |
1462 | 0 | GResource *resource; |
1463 | |
|
1464 | 0 | g_rw_lock_writer_lock (&resources_lock); |
1465 | |
|
1466 | 0 | register_lazy_static_resources_unlocked (); |
1467 | |
|
1468 | 0 | resource = g_atomic_pointer_exchange (&static_resource->resource, NULL); |
1469 | 0 | if (resource) |
1470 | 0 | { |
1471 | | /* There should be at least two references to the resource now: one for |
1472 | | * static_resource->resource, and one in the registered_resources list. */ |
1473 | 0 | g_assert (g_atomic_int_get (&resource->ref_count) >= 2); |
1474 | | |
1475 | 0 | g_resources_unregister_unlocked (resource); |
1476 | 0 | g_resource_unref (resource); |
1477 | 0 | } |
1478 | | |
1479 | 0 | g_rw_lock_writer_unlock (&resources_lock); |
1480 | 0 | } |
1481 | | |
1482 | | /** |
1483 | | * g_static_resource_get_resource: |
1484 | | * @static_resource: pointer to a static #GStaticResource |
1485 | | * |
1486 | | * Gets the GResource that was registered by a call to g_static_resource_init(). |
1487 | | * |
1488 | | * This is normally used by code generated by |
1489 | | * [glib-compile-resources][glib-compile-resources] |
1490 | | * and is not typically used by other code. |
1491 | | * |
1492 | | * Returns: (transfer none): a #GResource |
1493 | | * |
1494 | | * Since: 2.32 |
1495 | | **/ |
1496 | | GResource * |
1497 | | g_static_resource_get_resource (GStaticResource *static_resource) |
1498 | 0 | { |
1499 | 0 | register_lazy_static_resources (); |
1500 | |
|
1501 | | return g_atomic_pointer_get (&static_resource->resource); |
1502 | 0 | } |