nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GIO - GLib Input, Output and Streaming Library |
2 | * |
||
3 | * Copyright © 2011 Red Hat, Inc |
||
4 | * |
||
5 | * This library is free software; you can redistribute it and/or |
||
6 | * modify it under the terms of the GNU Lesser General Public |
||
7 | * License as published by the Free Software Foundation; either |
||
8 | * version 2 of the License, or (at your option) any later version. |
||
9 | * |
||
10 | * This library is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||
13 | * Lesser General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU Lesser General |
||
16 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
||
17 | * |
||
18 | * Authors: Alexander Larsson <alexl@redhat.com> |
||
19 | */ |
||
20 | |||
21 | #include "config.h" |
||
22 | |||
23 | #include <string.h> |
||
24 | |||
25 | #include "gresource.h" |
||
26 | #include <gvdb/gvdb-reader.h> |
||
27 | #include <gi18n-lib.h> |
||
28 | #include <gio/gmemoryinputstream.h> |
||
29 | #include <gio/gzlibdecompressor.h> |
||
30 | #include <gio/gconverterinputstream.h> |
||
31 | |||
32 | struct _GResource |
||
33 | { |
||
34 | int ref_count; |
||
35 | |||
36 | GvdbTable *table; |
||
37 | }; |
||
38 | |||
39 | static void register_lazy_static_resources (void); |
||
40 | |||
41 | G_DEFINE_BOXED_TYPE (GResource, g_resource, g_resource_ref, g_resource_unref) |
||
42 | |||
43 | /** |
||
44 | * SECTION:gresource |
||
45 | * @short_description: Resource framework |
||
46 | * @include: gio/gio.h |
||
47 | * |
||
48 | * Applications and libraries often contain binary or textual data that is |
||
49 | * really part of the application, rather than user data. For instance |
||
50 | * #GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files, |
||
51 | * icons, etc. These are often shipped as files in `$datadir/appname`, or |
||
52 | * manually included as literal strings in the code. |
||
53 | * |
||
54 | * The #GResource API and the [glib-compile-resources][glib-compile-resources] program |
||
55 | * provide a convenient and efficient alternative to this which has some nice properties. You |
||
56 | * maintain the files as normal files, so its easy to edit them, but during the build the files |
||
57 | * are combined into a binary bundle that is linked into the executable. This means that loading |
||
58 | * the resource files are efficient (as they are already in memory, shared with other instances) and |
||
59 | * simple (no need to check for things like I/O errors or locate the files in the filesystem). It |
||
60 | * also makes it easier to create relocatable applications. |
||
61 | * |
||
62 | * Resource files can also be marked as compressed. Such files will be included in the resource bundle |
||
63 | * in a compressed form, but will be automatically uncompressed when the resource is used. This |
||
64 | * is very useful e.g. for larger text files that are parsed once (or rarely) and then thrown away. |
||
65 | * |
||
66 | * Resource files can also be marked to be preprocessed, by setting the value of the |
||
67 | * `preprocess` attribute to a comma-separated list of preprocessing options. |
||
68 | * The only options currently supported are: |
||
69 | * |
||
70 | * `xml-stripblanks` which will use the xmllint command |
||
71 | * to strip ignorable whitespace from the XML file. For this to work, |
||
72 | * the `XMLLINT` environment variable must be set to the full path to |
||
73 | * the xmllint executable, or xmllint must be in the `PATH`; otherwise |
||
74 | * the preprocessing step is skipped. |
||
75 | * |
||
76 | * `to-pixdata` which will use the gdk-pixbuf-pixdata command to convert |
||
77 | * images to the GdkPixdata format, which allows you to create pixbufs directly using the data inside |
||
78 | * the resource file, rather than an (uncompressed) copy if it. For this, the gdk-pixbuf-pixdata |
||
79 | * program must be in the PATH, or the `GDK_PIXBUF_PIXDATA` environment variable must be |
||
80 | * set to the full path to the gdk-pixbuf-pixdata executable; otherwise the resource compiler will |
||
81 | * abort. |
||
82 | * |
||
83 | * Resource bundles are created by the [glib-compile-resources][glib-compile-resources] program |
||
84 | * which takes an XML file that describes the bundle, and a set of files that the XML references. These |
||
85 | * are combined into a binary resource bundle. |
||
86 | * |
||
87 | * An example resource description: |
||
88 | * |[ |
||
89 | * <?xml version="1.0" encoding="UTF-8"?> |
||
90 | * <gresources> |
||
91 | * <gresource prefix="/org/gtk/Example"> |
||
92 | * <file>data/splashscreen.png</file> |
||
93 | * <file compressed="true">dialog.ui</file> |
||
94 | * <file preprocess="xml-stripblanks">menumarkup.xml</file> |
||
95 | * </gresource> |
||
96 | * </gresources> |
||
97 | * ]| |
||
98 | * |
||
99 | * This will create a resource bundle with the following files: |
||
100 | * |[ |
||
101 | * /org/gtk/Example/data/splashscreen.png |
||
102 | * /org/gtk/Example/dialog.ui |
||
103 | * /org/gtk/Example/menumarkup.xml |
||
104 | * ]| |
||
105 | * |
||
106 | * Note that all resources in the process share the same namespace, so use Java-style |
||
107 | * path prefixes (like in the above example) to avoid conflicts. |
||
108 | * |
||
109 | * You can then use [glib-compile-resources][glib-compile-resources] to compile the XML to a |
||
110 | * binary bundle that you can load with g_resource_load(). However, its more common to use the --generate-source and |
||
111 | * --generate-header arguments to create a source file and header to link directly into your application. |
||
112 | * This will generate `get_resource()`, `register_resource()` and |
||
113 | * `unregister_resource()` functions, prefixed by the `--c-name` argument passed |
||
114 | * to [glib-compile-resources][glib-compile-resources]. `get_resource()` returns |
||
115 | * the generated #GResource object. The register and unregister functions |
||
116 | * register the resource so its files can be accessed using |
||
117 | * g_resources_lookup_data(). |
||
118 | * |
||
119 | * Once a #GResource has been created and registered all the data in it can be accessed globally in the process by |
||
120 | * using API calls like g_resources_open_stream() to stream the data or g_resources_lookup_data() to get a direct pointer |
||
121 | * to the data. You can also use URIs like "resource:///org/gtk/Example/data/splashscreen.png" with #GFile to access |
||
122 | * the resource data. |
||
123 | * |
||
124 | * There are two forms of the generated source, the default version uses the compiler support for constructor |
||
125 | * and destructor functions (where available) to automatically create and register the #GResource on startup |
||
126 | * or library load time. If you pass --manual-register two functions to register/unregister the resource is instead |
||
127 | * created. This requires an explicit initialization call in your application/library, but it works on all platforms, |
||
128 | * even on the minor ones where this is not available. (Constructor support is available for at least Win32, Mac OS and Linux.) |
||
129 | * |
||
130 | * Note that resource data can point directly into the data segment of e.g. a library, so if you are unloading libraries |
||
131 | * during runtime you need to be very careful with keeping around pointers to data from a resource, as this goes away |
||
132 | * when the library is unloaded. However, in practice this is not generally a problem, since most resource accesses |
||
133 | * is for your own resources, and resource data is often used once, during parsing, and then released. |
||
134 | * |
||
135 | * Since: 2.32 |
||
136 | */ |
||
137 | |||
138 | /** |
||
139 | * GStaticResource: |
||
140 | * |
||
141 | * #GStaticResource is an opaque data structure and can only be accessed |
||
142 | * using the following functions. |
||
143 | **/ |
||
144 | |||
145 | /** |
||
146 | * g_resource_error_quark: |
||
147 | * |
||
148 | * Gets the #GResource Error Quark. |
||
149 | * |
||
150 | * Returns: a #GQuark |
||
151 | * |
||
152 | * Since: 2.32 |
||
153 | */ |
||
154 | G_DEFINE_QUARK (g-resource-error-quark, g_resource_error) |
||
155 | |||
156 | /** |
||
157 | * g_resource_ref: |
||
158 | * @resource: A #GResource |
||
159 | * |
||
160 | * Atomically increments the reference count of @resource by one. This |
||
161 | * function is MT-safe and may be called from any thread. |
||
162 | * |
||
163 | * Returns: The passed in #GResource |
||
164 | * |
||
165 | * Since: 2.32 |
||
166 | **/ |
||
167 | GResource * |
||
168 | g_resource_ref (GResource *resource) |
||
169 | { |
||
170 | g_atomic_int_inc (&resource->ref_count); |
||
171 | return resource; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * g_resource_unref: |
||
176 | * @resource: A #GResource |
||
177 | * |
||
178 | * Atomically decrements the reference count of @resource by one. If the |
||
179 | * reference count drops to 0, all memory allocated by the resource is |
||
180 | * released. This function is MT-safe and may be called from any |
||
181 | * thread. |
||
182 | * |
||
183 | * Since: 2.32 |
||
184 | **/ |
||
185 | void |
||
186 | g_resource_unref (GResource *resource) |
||
187 | { |
||
188 | if (g_atomic_int_dec_and_test (&resource->ref_count)) |
||
189 | { |
||
190 | gvdb_table_unref (resource->table); |
||
191 | g_free (resource); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /*< internal > |
||
196 | * g_resource_new_from_table: |
||
197 | * @table: (transfer full): a GvdbTable |
||
198 | * |
||
199 | * Returns: (transfer full): a new #GResource for @table |
||
200 | */ |
||
201 | static GResource * |
||
202 | g_resource_new_from_table (GvdbTable *table) |
||
203 | { |
||
204 | GResource *resource; |
||
205 | |||
206 | resource = g_new (GResource, 1); |
||
207 | resource->ref_count = 1; |
||
208 | resource->table = table; |
||
209 | |||
210 | return resource; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * g_resource_new_from_data: |
||
215 | * @data: A #GBytes |
||
216 | * @error: return location for a #GError, or %NULL |
||
217 | * |
||
218 | * Creates a GResource from a reference to the binary resource bundle. |
||
219 | * This will keep a reference to @data while the resource lives, so |
||
220 | * the data should not be modified or freed. |
||
221 | * |
||
222 | * If you want to use this resource in the global resource namespace you need |
||
223 | * to register it with g_resources_register(). |
||
224 | * |
||
225 | * Returns: (transfer full): a new #GResource, or %NULL on error |
||
226 | * |
||
227 | * Since: 2.32 |
||
228 | **/ |
||
229 | GResource * |
||
230 | g_resource_new_from_data (GBytes *data, |
||
231 | GError **error) |
||
232 | { |
||
233 | GvdbTable *table; |
||
234 | |||
235 | table = gvdb_table_new_from_data (g_bytes_get_data (data, NULL), |
||
236 | g_bytes_get_size (data), |
||
237 | TRUE, |
||
238 | g_bytes_ref (data), |
||
239 | (GvdbRefFunc)g_bytes_ref, |
||
240 | (GDestroyNotify)g_bytes_unref, |
||
241 | error); |
||
242 | |||
243 | if (table == NULL) |
||
244 | return NULL; |
||
245 | |||
246 | return g_resource_new_from_table (table); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * g_resource_load: |
||
251 | * @filename: (type filename): the path of a filename to load, in the GLib filename encoding |
||
252 | * @error: return location for a #GError, or %NULL |
||
253 | * |
||
254 | * Loads a binary resource bundle and creates a #GResource representation of it, allowing |
||
255 | * you to query it for data. |
||
256 | * |
||
257 | * If you want to use this resource in the global resource namespace you need |
||
258 | * to register it with g_resources_register(). |
||
259 | * |
||
260 | * Returns: (transfer full): a new #GResource, or %NULL on error |
||
261 | * |
||
262 | * Since: 2.32 |
||
263 | **/ |
||
264 | GResource * |
||
265 | g_resource_load (const gchar *filename, |
||
266 | GError **error) |
||
267 | { |
||
268 | GvdbTable *table; |
||
269 | |||
270 | table = gvdb_table_new (filename, FALSE, error); |
||
271 | if (table == NULL) |
||
272 | return NULL; |
||
273 | |||
274 | return g_resource_new_from_table (table); |
||
275 | } |
||
276 | |||
277 | static |
||
278 | gboolean do_lookup (GResource *resource, |
||
279 | const gchar *path, |
||
280 | GResourceLookupFlags lookup_flags, |
||
281 | gsize *size, |
||
282 | guint32 *flags, |
||
283 | const void **data, |
||
284 | gsize *data_size, |
||
285 | GError **error) |
||
286 | { |
||
287 | char *free_path = NULL; |
||
288 | gsize path_len; |
||
289 | gboolean res = FALSE; |
||
290 | GVariant *value; |
||
291 | |||
292 | path_len = strlen (path); |
||
293 | if (path[path_len-1] == '/') |
||
294 | { |
||
295 | path = free_path = g_strdup (path); |
||
296 | free_path[path_len-1] = 0; |
||
297 | } |
||
298 | |||
299 | value = gvdb_table_get_raw_value (resource->table, path); |
||
300 | |||
301 | if (value == NULL) |
||
302 | { |
||
303 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
304 | _("The resource at '%s' does not exist"), |
||
305 | path); |
||
306 | } |
||
307 | else |
||
308 | { |
||
309 | guint32 _size, _flags; |
||
310 | GVariant *array; |
||
311 | |||
312 | g_variant_get (value, "(uu@ay)", |
||
313 | &_size, |
||
314 | &_flags, |
||
315 | &array); |
||
316 | |||
317 | _size = GUINT32_FROM_LE (_size); |
||
318 | _flags = GUINT32_FROM_LE (_flags); |
||
319 | |||
320 | if (size) |
||
321 | *size = _size; |
||
322 | if (flags) |
||
323 | *flags = _flags; |
||
324 | if (data) |
||
325 | *data = g_variant_get_data (array); |
||
326 | if (data_size) |
||
327 | { |
||
328 | /* Don't report trailing newline that non-compressed files has */ |
||
329 | if (_flags & G_RESOURCE_FLAGS_COMPRESSED) |
||
330 | *data_size = g_variant_get_size (array); |
||
331 | else |
||
332 | *data_size = g_variant_get_size (array) - 1; |
||
333 | } |
||
334 | g_variant_unref (array); |
||
335 | g_variant_unref (value); |
||
336 | |||
337 | res = TRUE; |
||
338 | } |
||
339 | |||
340 | g_free (free_path); |
||
341 | return res; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * g_resource_open_stream: |
||
346 | * @resource: A #GResource |
||
347 | * @path: A pathname inside the resource |
||
348 | * @lookup_flags: A #GResourceLookupFlags |
||
349 | * @error: return location for a #GError, or %NULL |
||
350 | * |
||
351 | * Looks for a file at the specified @path in the resource and |
||
352 | * returns a #GInputStream that lets you read the data. |
||
353 | * |
||
354 | * @lookup_flags controls the behaviour of the lookup. |
||
355 | * |
||
356 | * Returns: (transfer full): #GInputStream or %NULL on error. |
||
357 | * Free the returned object with g_object_unref() |
||
358 | * |
||
359 | * Since: 2.32 |
||
360 | **/ |
||
361 | GInputStream * |
||
362 | g_resource_open_stream (GResource *resource, |
||
363 | const gchar *path, |
||
364 | GResourceLookupFlags lookup_flags, |
||
365 | GError **error) |
||
366 | { |
||
367 | const void *data; |
||
368 | gsize data_size; |
||
369 | guint32 flags; |
||
370 | GInputStream *stream, *stream2; |
||
371 | |||
372 | if (!do_lookup (resource, path, lookup_flags, NULL, &flags, &data, &data_size, error)) |
||
373 | return NULL; |
||
374 | |||
375 | stream = g_memory_input_stream_new_from_data (data, data_size, NULL); |
||
376 | g_object_set_data_full (G_OBJECT (stream), "g-resource", |
||
377 | g_resource_ref (resource), |
||
378 | (GDestroyNotify)g_resource_unref); |
||
379 | |||
380 | if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
||
381 | { |
||
382 | GZlibDecompressor *decompressor = |
||
383 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
||
384 | |||
385 | stream2 = g_converter_input_stream_new (stream, G_CONVERTER (decompressor)); |
||
386 | g_object_unref (decompressor); |
||
387 | g_object_unref (stream); |
||
388 | stream = stream2; |
||
389 | } |
||
390 | |||
391 | return stream; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * g_resource_lookup_data: |
||
396 | * @resource: A #GResource |
||
397 | * @path: A pathname inside the resource |
||
398 | * @lookup_flags: A #GResourceLookupFlags |
||
399 | * @error: return location for a #GError, or %NULL |
||
400 | * |
||
401 | * Looks for a file at the specified @path in the resource and |
||
402 | * returns a #GBytes that lets you directly access the data in |
||
403 | * memory. |
||
404 | * |
||
405 | * The data is always followed by a zero byte, so you |
||
406 | * can safely use the data as a C string. However, that byte |
||
407 | * is not included in the size of the GBytes. |
||
408 | * |
||
409 | * For uncompressed resource files this is a pointer directly into |
||
410 | * the resource bundle, which is typically in some readonly data section |
||
411 | * in the program binary. For compressed files we allocate memory on |
||
412 | * the heap and automatically uncompress the data. |
||
413 | * |
||
414 | * @lookup_flags controls the behaviour of the lookup. |
||
415 | * |
||
416 | * Returns: (transfer full): #GBytes or %NULL on error. |
||
417 | * Free the returned object with g_bytes_unref() |
||
418 | * |
||
419 | * Since: 2.32 |
||
420 | **/ |
||
421 | GBytes * |
||
422 | g_resource_lookup_data (GResource *resource, |
||
423 | const gchar *path, |
||
424 | GResourceLookupFlags lookup_flags, |
||
425 | GError **error) |
||
426 | { |
||
427 | const void *data; |
||
428 | guint32 flags; |
||
429 | gsize data_size; |
||
430 | gsize size; |
||
431 | |||
432 | if (!do_lookup (resource, path, lookup_flags, &size, &flags, &data, &data_size, error)) |
||
433 | return NULL; |
||
434 | |||
435 | if (flags & G_RESOURCE_FLAGS_COMPRESSED) |
||
436 | { |
||
437 | char *uncompressed, *d; |
||
438 | const char *s; |
||
439 | GConverterResult res; |
||
440 | gsize d_size, s_size; |
||
441 | gsize bytes_read, bytes_written; |
||
442 | |||
443 | |||
444 | GZlibDecompressor *decompressor = |
||
445 | g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_ZLIB); |
||
446 | |||
447 | uncompressed = g_malloc (size + 1); |
||
448 | |||
449 | s = data; |
||
450 | s_size = data_size; |
||
451 | d = uncompressed; |
||
452 | d_size = size; |
||
453 | |||
454 | do |
||
455 | { |
||
456 | res = g_converter_convert (G_CONVERTER (decompressor), |
||
457 | s, s_size, |
||
458 | d, d_size, |
||
459 | G_CONVERTER_INPUT_AT_END, |
||
460 | &bytes_read, |
||
461 | &bytes_written, |
||
462 | NULL); |
||
463 | if (res == G_CONVERTER_ERROR) |
||
464 | { |
||
465 | g_free (uncompressed); |
||
466 | g_object_unref (decompressor); |
||
467 | |||
468 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_INTERNAL, |
||
469 | _("The resource at '%s' failed to decompress"), |
||
470 | path); |
||
471 | return NULL; |
||
472 | |||
473 | } |
||
474 | s += bytes_read; |
||
475 | s_size -= bytes_read; |
||
476 | d += bytes_written; |
||
477 | d_size -= bytes_written; |
||
478 | } |
||
479 | while (res != G_CONVERTER_FINISHED); |
||
480 | |||
481 | uncompressed[size] = 0; /* Zero terminate */ |
||
482 | |||
483 | g_object_unref (decompressor); |
||
484 | |||
485 | return g_bytes_new_take (uncompressed, size); |
||
486 | } |
||
487 | else |
||
488 | return g_bytes_new_with_free_func (data, data_size, (GDestroyNotify)g_resource_unref, g_resource_ref (resource)); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * g_resource_get_info: |
||
493 | * @resource: A #GResource |
||
494 | * @path: A pathname inside the resource |
||
495 | * @lookup_flags: A #GResourceLookupFlags |
||
496 | * @size: (out) (allow-none): a location to place the length of the contents of the file, |
||
497 | * or %NULL if the length is not needed |
||
498 | * @flags: (out) (allow-none): a location to place the flags about the file, |
||
499 | * or %NULL if the length is not needed |
||
500 | * @error: return location for a #GError, or %NULL |
||
501 | * |
||
502 | * Looks for a file at the specified @path in the resource and |
||
503 | * if found returns information about it. |
||
504 | * |
||
505 | * @lookup_flags controls the behaviour of the lookup. |
||
506 | * |
||
507 | * Returns: %TRUE if the file was found. %FALSE if there were errors |
||
508 | * |
||
509 | * Since: 2.32 |
||
510 | **/ |
||
511 | gboolean |
||
512 | g_resource_get_info (GResource *resource, |
||
513 | const gchar *path, |
||
514 | GResourceLookupFlags lookup_flags, |
||
515 | gsize *size, |
||
516 | guint32 *flags, |
||
517 | GError **error) |
||
518 | { |
||
519 | return do_lookup (resource, path, lookup_flags, size, flags, NULL, NULL, error); |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * g_resource_enumerate_children: |
||
524 | * @resource: A #GResource |
||
525 | * @path: A pathname inside the resource |
||
526 | * @lookup_flags: A #GResourceLookupFlags |
||
527 | * @error: return location for a #GError, or %NULL |
||
528 | * |
||
529 | * Returns all the names of children at the specified @path in the resource. |
||
530 | * The return result is a %NULL terminated list of strings which should |
||
531 | * be released with g_strfreev(). |
||
532 | * |
||
533 | * If @path is invalid or does not exist in the #GResource, |
||
534 | * %G_RESOURCE_ERROR_NOT_FOUND will be returned. |
||
535 | * |
||
536 | * @lookup_flags controls the behaviour of the lookup. |
||
537 | * |
||
538 | * Returns: (array zero-terminated=1) (transfer full): an array of constant strings |
||
539 | * |
||
540 | * Since: 2.32 |
||
541 | **/ |
||
542 | gchar ** |
||
543 | g_resource_enumerate_children (GResource *resource, |
||
544 | const gchar *path, |
||
545 | GResourceLookupFlags lookup_flags, |
||
546 | GError **error) |
||
547 | { |
||
548 | gchar **children; |
||
549 | gsize path_len; |
||
550 | char *path_with_slash; |
||
551 | |||
552 | if (*path == 0) |
||
553 | { |
||
554 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
555 | _("The resource at '%s' does not exist"), |
||
556 | path); |
||
557 | return NULL; |
||
558 | } |
||
559 | |||
560 | path_len = strlen (path); |
||
561 | if (path[path_len-1] != '/') |
||
562 | path_with_slash = g_strconcat (path, "/", NULL); |
||
563 | else |
||
564 | path_with_slash = g_strdup (path); |
||
565 | |||
566 | children = gvdb_table_list (resource->table, path_with_slash); |
||
567 | g_free (path_with_slash); |
||
568 | |||
569 | if (children == NULL) |
||
570 | { |
||
571 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
572 | _("The resource at '%s' does not exist"), |
||
573 | path); |
||
574 | return NULL; |
||
575 | } |
||
576 | |||
577 | return children; |
||
578 | } |
||
579 | |||
580 | static GRWLock resources_lock; |
||
581 | static GList *registered_resources; |
||
582 | |||
583 | /* This is updated atomically, so we can append to it and check for NULL outside the |
||
584 | lock, but all other accesses are done under the write lock */ |
||
585 | static GStaticResource *lazy_register_resources; |
||
586 | |||
587 | static void |
||
588 | g_resources_register_unlocked (GResource *resource) |
||
589 | { |
||
590 | registered_resources = g_list_prepend (registered_resources, g_resource_ref (resource)); |
||
591 | } |
||
592 | |||
593 | static void |
||
594 | g_resources_unregister_unlocked (GResource *resource) |
||
595 | { |
||
596 | if (g_list_find (registered_resources, resource) == NULL) |
||
597 | { |
||
598 | g_warning ("Tried to remove not registered resource"); |
||
599 | } |
||
600 | else |
||
601 | { |
||
602 | registered_resources = g_list_remove (registered_resources, resource); |
||
603 | g_resource_unref (resource); |
||
604 | } |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * g_resources_register: |
||
609 | * @resource: A #GResource |
||
610 | * |
||
611 | * Registers the resource with the process-global set of resources. |
||
612 | * Once a resource is registered the files in it can be accessed |
||
613 | * with the global resource lookup functions like g_resources_lookup_data(). |
||
614 | * |
||
615 | * Since: 2.32 |
||
616 | **/ |
||
617 | void |
||
618 | g_resources_register (GResource *resource) |
||
619 | { |
||
620 | g_rw_lock_writer_lock (&resources_lock); |
||
621 | g_resources_register_unlocked (resource); |
||
622 | g_rw_lock_writer_unlock (&resources_lock); |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * g_resources_unregister: |
||
627 | * @resource: A #GResource |
||
628 | * |
||
629 | * Unregisters the resource from the process-global set of resources. |
||
630 | * |
||
631 | * Since: 2.32 |
||
632 | **/ |
||
633 | void |
||
634 | g_resources_unregister (GResource *resource) |
||
635 | { |
||
636 | g_rw_lock_writer_lock (&resources_lock); |
||
637 | g_resources_unregister_unlocked (resource); |
||
638 | g_rw_lock_writer_unlock (&resources_lock); |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * g_resources_open_stream: |
||
643 | * @path: A pathname inside the resource |
||
644 | * @lookup_flags: A #GResourceLookupFlags |
||
645 | * @error: return location for a #GError, or %NULL |
||
646 | * |
||
647 | * Looks for a file at the specified @path in the set of |
||
648 | * globally registered resources and returns a #GInputStream |
||
649 | * that lets you read the data. |
||
650 | * |
||
651 | * @lookup_flags controls the behaviour of the lookup. |
||
652 | * |
||
653 | * Returns: (transfer full): #GInputStream or %NULL on error. |
||
654 | * Free the returned object with g_object_unref() |
||
655 | * |
||
656 | * Since: 2.32 |
||
657 | **/ |
||
658 | GInputStream * |
||
659 | g_resources_open_stream (const gchar *path, |
||
660 | GResourceLookupFlags lookup_flags, |
||
661 | GError **error) |
||
662 | { |
||
663 | GInputStream *res = NULL; |
||
664 | GList *l; |
||
665 | GInputStream *stream; |
||
666 | |||
667 | register_lazy_static_resources (); |
||
668 | |||
669 | g_rw_lock_reader_lock (&resources_lock); |
||
670 | |||
671 | for (l = registered_resources; l != NULL; l = l->next) |
||
672 | { |
||
673 | GResource *r = l->data; |
||
674 | GError *my_error = NULL; |
||
675 | |||
676 | stream = g_resource_open_stream (r, path, lookup_flags, &my_error); |
||
677 | if (stream == NULL && |
||
678 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
||
679 | { |
||
680 | g_clear_error (&my_error); |
||
681 | } |
||
682 | else |
||
683 | { |
||
684 | if (stream == NULL) |
||
685 | g_propagate_error (error, my_error); |
||
686 | res = stream; |
||
687 | break; |
||
688 | } |
||
689 | } |
||
690 | |||
691 | if (l == NULL) |
||
692 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
693 | _("The resource at '%s' does not exist"), |
||
694 | path); |
||
695 | |||
696 | g_rw_lock_reader_unlock (&resources_lock); |
||
697 | |||
698 | return res; |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * g_resources_lookup_data: |
||
703 | * @path: A pathname inside the resource |
||
704 | * @lookup_flags: A #GResourceLookupFlags |
||
705 | * @error: return location for a #GError, or %NULL |
||
706 | * |
||
707 | * Looks for a file at the specified @path in the set of |
||
708 | * globally registered resources and returns a #GBytes that |
||
709 | * lets you directly access the data in memory. |
||
710 | * |
||
711 | * The data is always followed by a zero byte, so you |
||
712 | * can safely use the data as a C string. However, that byte |
||
713 | * is not included in the size of the GBytes. |
||
714 | * |
||
715 | * For uncompressed resource files this is a pointer directly into |
||
716 | * the resource bundle, which is typically in some readonly data section |
||
717 | * in the program binary. For compressed files we allocate memory on |
||
718 | * the heap and automatically uncompress the data. |
||
719 | * |
||
720 | * @lookup_flags controls the behaviour of the lookup. |
||
721 | * |
||
722 | * Returns: (transfer full): #GBytes or %NULL on error. |
||
723 | * Free the returned object with g_bytes_unref() |
||
724 | * |
||
725 | * Since: 2.32 |
||
726 | **/ |
||
727 | GBytes * |
||
728 | g_resources_lookup_data (const gchar *path, |
||
729 | GResourceLookupFlags lookup_flags, |
||
730 | GError **error) |
||
731 | { |
||
732 | GBytes *res = NULL; |
||
733 | GList *l; |
||
734 | GBytes *data; |
||
735 | |||
736 | register_lazy_static_resources (); |
||
737 | |||
738 | g_rw_lock_reader_lock (&resources_lock); |
||
739 | |||
740 | for (l = registered_resources; l != NULL; l = l->next) |
||
741 | { |
||
742 | GResource *r = l->data; |
||
743 | GError *my_error = NULL; |
||
744 | |||
745 | data = g_resource_lookup_data (r, path, lookup_flags, &my_error); |
||
746 | if (data == NULL && |
||
747 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
||
748 | { |
||
749 | g_clear_error (&my_error); |
||
750 | } |
||
751 | else |
||
752 | { |
||
753 | if (data == NULL) |
||
754 | g_propagate_error (error, my_error); |
||
755 | res = data; |
||
756 | break; |
||
757 | } |
||
758 | } |
||
759 | |||
760 | if (l == NULL) |
||
761 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
762 | _("The resource at '%s' does not exist"), |
||
763 | path); |
||
764 | |||
765 | g_rw_lock_reader_unlock (&resources_lock); |
||
766 | |||
767 | return res; |
||
768 | } |
||
769 | |||
770 | /** |
||
771 | * g_resources_enumerate_children: |
||
772 | * @path: A pathname inside the resource |
||
773 | * @lookup_flags: A #GResourceLookupFlags |
||
774 | * @error: return location for a #GError, or %NULL |
||
775 | * |
||
776 | * Returns all the names of children at the specified @path in the set of |
||
777 | * globally registered resources. |
||
778 | * The return result is a %NULL terminated list of strings which should |
||
779 | * be released with g_strfreev(). |
||
780 | * |
||
781 | * @lookup_flags controls the behaviour of the lookup. |
||
782 | * |
||
783 | * Returns: (array zero-terminated=1) (transfer full): an array of constant strings |
||
784 | * |
||
785 | * Since: 2.32 |
||
786 | **/ |
||
787 | gchar ** |
||
788 | g_resources_enumerate_children (const gchar *path, |
||
789 | GResourceLookupFlags lookup_flags, |
||
790 | GError **error) |
||
791 | { |
||
792 | GHashTable *hash = NULL; |
||
793 | GList *l; |
||
794 | char **children; |
||
795 | int i; |
||
796 | |||
797 | register_lazy_static_resources (); |
||
798 | |||
799 | g_rw_lock_reader_lock (&resources_lock); |
||
800 | |||
801 | for (l = registered_resources; l != NULL; l = l->next) |
||
802 | { |
||
803 | GResource *r = l->data; |
||
804 | |||
805 | children = g_resource_enumerate_children (r, path, 0, NULL); |
||
806 | |||
807 | if (children != NULL) |
||
808 | { |
||
809 | if (hash == NULL) |
||
810 | hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
||
811 | |||
812 | for (i = 0; children[i] != NULL; i++) |
||
813 | g_hash_table_insert (hash, children[i], children[i]); |
||
814 | g_free (children); |
||
815 | } |
||
816 | } |
||
817 | |||
818 | g_rw_lock_reader_unlock (&resources_lock); |
||
819 | |||
820 | if (hash == NULL) |
||
821 | { |
||
822 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
823 | _("The resource at '%s' does not exist"), |
||
824 | path); |
||
825 | return NULL; |
||
826 | } |
||
827 | else |
||
828 | { |
||
829 | GHashTableIter iter; |
||
830 | const char *key; |
||
831 | guint n_children; |
||
832 | n_children = g_hash_table_size (hash); |
||
833 | children = g_new (char *, n_children + 1); |
||
834 | i = 0; |
||
835 | |||
836 | g_hash_table_iter_init (&iter, hash); |
||
837 | while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL)) |
||
838 | children[i++] = g_strdup (key); |
||
839 | children[i++] = NULL; |
||
840 | |||
841 | g_hash_table_destroy (hash); |
||
842 | |||
843 | return children; |
||
844 | } |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * g_resources_get_info: |
||
849 | * @path: A pathname inside the resource |
||
850 | * @lookup_flags: A #GResourceLookupFlags |
||
851 | * @size: (out) (allow-none): a location to place the length of the contents of the file, |
||
852 | * or %NULL if the length is not needed |
||
853 | * @flags: (out) (allow-none): a location to place the flags about the file, |
||
854 | * or %NULL if the length is not needed |
||
855 | * @error: return location for a #GError, or %NULL |
||
856 | * |
||
857 | * Looks for a file at the specified @path in the set of |
||
858 | * globally registered resources and if found returns information about it. |
||
859 | * |
||
860 | * @lookup_flags controls the behaviour of the lookup. |
||
861 | * |
||
862 | * Returns: %TRUE if the file was found. %FALSE if there were errors |
||
863 | * |
||
864 | * Since: 2.32 |
||
865 | **/ |
||
866 | gboolean |
||
867 | g_resources_get_info (const gchar *path, |
||
868 | GResourceLookupFlags lookup_flags, |
||
869 | gsize *size, |
||
870 | guint32 *flags, |
||
871 | GError **error) |
||
872 | { |
||
873 | gboolean res = FALSE; |
||
874 | GList *l; |
||
875 | gboolean r_res; |
||
876 | |||
877 | register_lazy_static_resources (); |
||
878 | |||
879 | g_rw_lock_reader_lock (&resources_lock); |
||
880 | |||
881 | for (l = registered_resources; l != NULL; l = l->next) |
||
882 | { |
||
883 | GResource *r = l->data; |
||
884 | GError *my_error = NULL; |
||
885 | |||
886 | r_res = g_resource_get_info (r, path, lookup_flags, size, flags, &my_error); |
||
887 | if (!r_res && |
||
888 | g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
||
889 | { |
||
890 | g_clear_error (&my_error); |
||
891 | } |
||
892 | else |
||
893 | { |
||
894 | if (!r_res) |
||
895 | g_propagate_error (error, my_error); |
||
896 | res = r_res; |
||
897 | break; |
||
898 | } |
||
899 | } |
||
900 | |||
901 | if (l == NULL) |
||
902 | g_set_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND, |
||
903 | _("The resource at '%s' does not exist"), |
||
904 | path); |
||
905 | |||
906 | g_rw_lock_reader_unlock (&resources_lock); |
||
907 | |||
908 | return res; |
||
909 | } |
||
910 | |||
911 | /* This code is to handle registration of resources very early, from a constructor. |
||
912 | * At that point we'd like to do minimal work, to avoid ordering issues. For instance, |
||
913 | * we're not allowed to use g_malloc, as the user need to be able to call g_mem_set_vtable |
||
914 | * before the first call to g_malloc. |
||
915 | * |
||
916 | * So, what we do at construction time is that we just register a static structure on |
||
917 | * a list of resources that need to be initialized, and then later, when doing any lookups |
||
918 | * in the global list of registered resources, or when getting a reference to the |
||
919 | * lazily initialized resource we lazily create and register all the GResources on |
||
920 | * the lazy list. |
||
921 | * |
||
922 | * To avoid having to use locks in the constructor, and having to grab the writer lock |
||
923 | * when checking the lazy registering list we update lazy_register_resources in |
||
924 | * a lock-less fashion (atomic prepend-only, atomic replace with NULL). However, all |
||
925 | * operations except: |
||
926 | * * check if there are any resources to lazily initialize |
||
927 | * * Add a static resource to the lazy init list |
||
928 | * Do use the full writer lock for protection. |
||
929 | */ |
||
930 | |||
931 | static void |
||
932 | register_lazy_static_resources_unlocked (void) |
||
933 | { |
||
934 | GStaticResource *list; |
||
935 | |||
936 | do |
||
937 | list = lazy_register_resources; |
||
938 | while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, list, NULL)); |
||
939 | |||
940 | while (list != NULL) |
||
941 | { |
||
942 | GBytes *bytes = g_bytes_new_static (list->data, list->data_len); |
||
943 | GResource *resource = g_resource_new_from_data (bytes, NULL); |
||
944 | if (resource) |
||
945 | { |
||
946 | g_resources_register_unlocked (resource); |
||
947 | g_atomic_pointer_set (&list->resource, resource); |
||
948 | } |
||
949 | g_bytes_unref (bytes); |
||
950 | |||
951 | list = list->next; |
||
952 | } |
||
953 | } |
||
954 | |||
955 | static void |
||
956 | register_lazy_static_resources (void) |
||
957 | { |
||
958 | if (g_atomic_pointer_get (&lazy_register_resources) == NULL) |
||
959 | return; |
||
960 | |||
961 | g_rw_lock_writer_lock (&resources_lock); |
||
962 | register_lazy_static_resources_unlocked (); |
||
963 | g_rw_lock_writer_unlock (&resources_lock); |
||
964 | } |
||
965 | |||
966 | /** |
||
967 | * g_static_resource_init: |
||
968 | * @static_resource: pointer to a static #GStaticResource |
||
969 | * |
||
970 | * Initializes a GResource from static data using a |
||
971 | * GStaticResource. |
||
972 | * |
||
973 | * This is normally used by code generated by |
||
974 | * [glib-compile-resources][glib-compile-resources] |
||
975 | * and is not typically used by other code. |
||
976 | * |
||
977 | * Since: 2.32 |
||
978 | **/ |
||
979 | void |
||
980 | g_static_resource_init (GStaticResource *static_resource) |
||
981 | { |
||
982 | gpointer next; |
||
983 | |||
984 | do |
||
985 | { |
||
986 | next = lazy_register_resources; |
||
987 | static_resource->next = next; |
||
988 | } |
||
989 | while (!g_atomic_pointer_compare_and_exchange (&lazy_register_resources, next, static_resource)); |
||
990 | } |
||
991 | |||
992 | /** |
||
993 | * g_static_resource_fini: |
||
994 | * @static_resource: pointer to a static #GStaticResource |
||
995 | * |
||
996 | * Finalized a GResource initialized by g_static_resource_init(). |
||
997 | * |
||
998 | * This is normally used by code generated by |
||
999 | * [glib-compile-resources][glib-compile-resources] |
||
1000 | * and is not typically used by other code. |
||
1001 | * |
||
1002 | * Since: 2.32 |
||
1003 | **/ |
||
1004 | void |
||
1005 | g_static_resource_fini (GStaticResource *static_resource) |
||
1006 | { |
||
1007 | GResource *resource; |
||
1008 | |||
1009 | g_rw_lock_writer_lock (&resources_lock); |
||
1010 | |||
1011 | register_lazy_static_resources_unlocked (); |
||
1012 | |||
1013 | resource = g_atomic_pointer_get (&static_resource->resource); |
||
1014 | if (resource) |
||
1015 | { |
||
1016 | g_atomic_pointer_set (&static_resource->resource, NULL); |
||
1017 | g_resources_unregister_unlocked (resource); |
||
1018 | g_resource_unref (resource); |
||
1019 | } |
||
1020 | |||
1021 | g_rw_lock_writer_unlock (&resources_lock); |
||
1022 | } |
||
1023 | |||
1024 | /** |
||
1025 | * g_static_resource_get_resource: |
||
1026 | * @static_resource: pointer to a static #GStaticResource |
||
1027 | * |
||
1028 | * Gets the GResource that was registered by a call to g_static_resource_init(). |
||
1029 | * |
||
1030 | * This is normally used by code generated by |
||
1031 | * [glib-compile-resources][glib-compile-resources] |
||
1032 | * and is not typically used by other code. |
||
1033 | * |
||
1034 | * Returns: (transfer none): a #GResource |
||
1035 | * |
||
1036 | * Since: 2.32 |
||
1037 | **/ |
||
1038 | GResource * |
||
1039 | g_static_resource_get_resource (GStaticResource *static_resource) |
||
1040 | { |
||
1041 | register_lazy_static_resources (); |
||
1042 | |||
1043 | return g_atomic_pointer_get (&static_resource->resource); |
||
1044 | } |