/src/irssi/subprojects/glib-2.74.7/gio/gresourcefile.c
Line | Count | Source |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 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 | | * Author: Alexander Larsson <alexl@redhat.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include <string.h> |
26 | | |
27 | | #include "gresource.h" |
28 | | #include "gresourcefile.h" |
29 | | #include "gfileattribute.h" |
30 | | #include <gfileattribute-priv.h> |
31 | | #include <gfileinfo-priv.h> |
32 | | #include "gfile.h" |
33 | | #include "gfilemonitor.h" |
34 | | #include "gseekable.h" |
35 | | #include "gfileinputstream.h" |
36 | | #include "gfileinfo.h" |
37 | | #include "gfileenumerator.h" |
38 | | #include "gcontenttype.h" |
39 | | #include "gioerror.h" |
40 | | #include <glib/gstdio.h> |
41 | | #include "glibintl.h" |
42 | | |
43 | | struct _GResourceFile |
44 | | { |
45 | | GObject parent_instance; |
46 | | |
47 | | char *path; |
48 | | }; |
49 | | |
50 | | struct _GResourceFileEnumerator |
51 | | { |
52 | | GFileEnumerator parent; |
53 | | |
54 | | GFileAttributeMatcher *matcher; |
55 | | char *path; |
56 | | char *attributes; |
57 | | GFileQueryInfoFlags flags; |
58 | | int index; |
59 | | |
60 | | char **children; |
61 | | }; |
62 | | |
63 | | struct _GResourceFileEnumeratorClass |
64 | | { |
65 | | GFileEnumeratorClass parent_class; |
66 | | }; |
67 | | |
68 | | typedef struct _GResourceFileEnumerator GResourceFileEnumerator; |
69 | | typedef struct _GResourceFileEnumeratorClass GResourceFileEnumeratorClass; |
70 | | |
71 | | static void g_resource_file_file_iface_init (GFileIface *iface); |
72 | | |
73 | | static GFileAttributeInfoList *resource_writable_attributes = NULL; |
74 | | static GFileAttributeInfoList *resource_writable_namespaces = NULL; |
75 | | |
76 | | static GType _g_resource_file_enumerator_get_type (void); |
77 | | |
78 | 0 | #define G_TYPE_RESOURCE_FILE_ENUMERATOR (_g_resource_file_enumerator_get_type ()) |
79 | 0 | #define G_RESOURCE_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_RESOURCE_FILE_ENUMERATOR, GResourceFileEnumerator)) |
80 | | #define G_RESOURCE_FILE_ENUMERATOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_RESOURCE_FILE_ENUMERATOR, GResourceFileEnumeratorClass)) |
81 | | #define G_IS_RESOURCE_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_RESOURCE_FILE_ENUMERATOR)) |
82 | | #define G_IS_RESOURCE_FILE_ENUMERATOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_RESOURCE_FILE_ENUMERATOR)) |
83 | | #define G_RESOURCE_FILE_ENUMERATOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_RESOURCE_FILE_ENUMERATOR, GResourceFileEnumeratorClass)) |
84 | | |
85 | 0 | #define G_TYPE_RESOURCE_FILE_INPUT_STREAM (_g_resource_file_input_stream_get_type ()) |
86 | 0 | #define G_RESOURCE_FILE_INPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_RESOURCE_FILE_INPUT_STREAM, GResourceFileInputStream)) |
87 | | #define G_RESOURCE_FILE_INPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_RESOURCE_FILE_INPUT_STREAM, GResourceFileInputStreamClass)) |
88 | | #define G_IS_RESOURCE_FILE_INPUT_STREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_RESOURCE_FILE_INPUT_STREAM)) |
89 | | #define G_IS_RESOURCE_FILE_INPUT_STREAM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_RESOURCE_FILE_INPUT_STREAM)) |
90 | | #define G_RESOURCE_FILE_INPUT_STREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_RESOURCE_FILE_INPUT_STREAM, GResourceFileInputStreamClass)) |
91 | | |
92 | | typedef struct _GResourceFileInputStream GResourceFileInputStream; |
93 | | typedef struct _GResourceFileInputStreamClass GResourceFileInputStreamClass; |
94 | | |
95 | | #define g_resource_file_get_type _g_resource_file_get_type |
96 | 0 | G_DEFINE_TYPE_WITH_CODE (GResourceFile, g_resource_file, G_TYPE_OBJECT, |
97 | 0 | G_IMPLEMENT_INTERFACE (G_TYPE_FILE, |
98 | 0 | g_resource_file_file_iface_init)) |
99 | 0 |
|
100 | 0 | #define g_resource_file_enumerator_get_type _g_resource_file_enumerator_get_type |
101 | 0 | G_DEFINE_TYPE (GResourceFileEnumerator, g_resource_file_enumerator, G_TYPE_FILE_ENUMERATOR) |
102 | 0 |
|
103 | 0 | static GFileEnumerator *_g_resource_file_enumerator_new (GResourceFile *file, |
104 | 0 | const char *attributes, |
105 | 0 | GFileQueryInfoFlags flags, |
106 | 0 | GCancellable *cancellable, |
107 | 0 | GError **error); |
108 | 0 |
|
109 | 0 |
|
110 | 0 | static GType _g_resource_file_input_stream_get_type (void) G_GNUC_CONST; |
111 | 0 |
|
112 | 0 | static GFileInputStream *_g_resource_file_input_stream_new (GInputStream *stream, GFile *file); |
113 | 0 |
|
114 | 0 |
|
115 | 0 | static void |
116 | 0 | g_resource_file_finalize (GObject *object) |
117 | 0 | { |
118 | 0 | GResourceFile *resource; |
119 | |
|
120 | 0 | resource = G_RESOURCE_FILE (object); |
121 | |
|
122 | 0 | g_free (resource->path); |
123 | |
|
124 | 0 | G_OBJECT_CLASS (g_resource_file_parent_class)->finalize (object); |
125 | 0 | } |
126 | | |
127 | | static void |
128 | | g_resource_file_class_init (GResourceFileClass *klass) |
129 | 0 | { |
130 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
131 | |
|
132 | 0 | gobject_class->finalize = g_resource_file_finalize; |
133 | |
|
134 | 0 | resource_writable_attributes = g_file_attribute_info_list_new (); |
135 | 0 | resource_writable_namespaces = g_file_attribute_info_list_new (); |
136 | 0 | } |
137 | | |
138 | | static void |
139 | | g_resource_file_init (GResourceFile *resource) |
140 | 0 | { |
141 | 0 | } |
142 | | |
143 | | static inline gchar * |
144 | | scan_backwards (const gchar *begin, |
145 | | const gchar *end, |
146 | | gchar c) |
147 | 0 | { |
148 | 0 | while (end >= begin) |
149 | 0 | { |
150 | 0 | if (*end == c) |
151 | 0 | return (gchar *)end; |
152 | 0 | end--; |
153 | 0 | } |
154 | | |
155 | 0 | return NULL; |
156 | 0 | } |
157 | | |
158 | | static inline void |
159 | | pop_to_previous_part (const gchar *begin, |
160 | | gchar **out) |
161 | 0 | { |
162 | 0 | if (*out > begin) |
163 | 0 | *out = scan_backwards (begin, *out - 1, '/'); |
164 | 0 | } |
165 | | |
166 | | /* |
167 | | * canonicalize_filename: |
168 | | * @in: the path to be canonicalized |
169 | | * |
170 | | * The path @in may contain non-canonical path pieces such as "../" |
171 | | * or duplicated "/". This will resolve those into a form that only |
172 | | * contains a single / at a time and resolves all "../". The resulting |
173 | | * path must also start with a /. |
174 | | * |
175 | | * Returns: the canonical form of the path |
176 | | */ |
177 | | static char * |
178 | | canonicalize_filename (const char *in) |
179 | 0 | { |
180 | 0 | gchar *bptr; |
181 | 0 | char *out; |
182 | |
|
183 | 0 | bptr = out = g_malloc (strlen (in) + 2); |
184 | 0 | *out = '/'; |
185 | |
|
186 | 0 | while (*in != 0) |
187 | 0 | { |
188 | 0 | g_assert (*out == '/'); |
189 | | |
190 | | /* move past slashes */ |
191 | 0 | while (*in == '/') |
192 | 0 | in++; |
193 | | |
194 | | /* Handle ./ ../ .\0 ..\0 */ |
195 | 0 | if (*in == '.') |
196 | 0 | { |
197 | | /* If this is ../ or ..\0 move up */ |
198 | 0 | if (in[1] == '.' && (in[2] == '/' || in[2] == 0)) |
199 | 0 | { |
200 | 0 | pop_to_previous_part (bptr, &out); |
201 | 0 | in += 2; |
202 | 0 | continue; |
203 | 0 | } |
204 | | |
205 | | /* If this is ./ skip past it */ |
206 | 0 | if (in[1] == '/' || in[1] == 0) |
207 | 0 | { |
208 | 0 | in += 1; |
209 | 0 | continue; |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | /* Scan to the next path piece */ |
214 | 0 | while (*in != 0 && *in != '/') |
215 | 0 | *(++out) = *(in++); |
216 | | |
217 | | /* Add trailing /, compress the rest on the next go round. */ |
218 | 0 | if (*in == '/') |
219 | 0 | *(++out) = *(in++); |
220 | 0 | } |
221 | | |
222 | | /* Trim trailing / from path */ |
223 | 0 | if (out > bptr && *out == '/') |
224 | 0 | *out = 0; |
225 | 0 | else |
226 | 0 | *(++out) = 0; |
227 | |
|
228 | 0 | return bptr; |
229 | 0 | } |
230 | | |
231 | | static GFile * |
232 | | g_resource_file_new_for_path (const char *path) |
233 | 0 | { |
234 | 0 | GResourceFile *resource = g_object_new (G_TYPE_RESOURCE_FILE, NULL); |
235 | |
|
236 | 0 | resource->path = canonicalize_filename (path); |
237 | |
|
238 | 0 | return G_FILE (resource); |
239 | 0 | } |
240 | | |
241 | | GFile * |
242 | | _g_resource_file_new (const char *uri) |
243 | 0 | { |
244 | 0 | GFile *resource; |
245 | 0 | char *path; |
246 | |
|
247 | 0 | path = g_uri_unescape_string (uri + strlen ("resource:"), NULL); |
248 | 0 | resource = g_resource_file_new_for_path (path); |
249 | 0 | g_free (path); |
250 | |
|
251 | 0 | return G_FILE (resource); |
252 | 0 | } |
253 | | |
254 | | static gboolean |
255 | | g_resource_file_is_native (GFile *file) |
256 | 0 | { |
257 | 0 | return FALSE; |
258 | 0 | } |
259 | | |
260 | | static gboolean |
261 | | g_resource_file_has_uri_scheme (GFile *file, |
262 | | const char *uri_scheme) |
263 | 0 | { |
264 | 0 | return g_ascii_strcasecmp (uri_scheme, "resource") == 0; |
265 | 0 | } |
266 | | |
267 | | static char * |
268 | | g_resource_file_get_uri_scheme (GFile *file) |
269 | 0 | { |
270 | 0 | return g_strdup ("resource"); |
271 | 0 | } |
272 | | |
273 | | static char * |
274 | | g_resource_file_get_basename (GFile *file) |
275 | 0 | { |
276 | 0 | gchar *base; |
277 | |
|
278 | 0 | base = strrchr (G_RESOURCE_FILE (file)->path, '/'); |
279 | 0 | return g_strdup (base + 1); |
280 | 0 | } |
281 | | |
282 | | static char * |
283 | | g_resource_file_get_path (GFile *file) |
284 | 0 | { |
285 | 0 | return NULL; |
286 | 0 | } |
287 | | |
288 | | static char * |
289 | | g_resource_file_get_uri (GFile *file) |
290 | 0 | { |
291 | 0 | char *escaped, *res; |
292 | 0 | escaped = g_uri_escape_string (G_RESOURCE_FILE (file)->path, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE); |
293 | 0 | res = g_strconcat ("resource://", escaped, NULL); |
294 | 0 | g_free (escaped); |
295 | 0 | return res; |
296 | 0 | } |
297 | | |
298 | | static char * |
299 | | g_resource_file_get_parse_name (GFile *file) |
300 | 0 | { |
301 | 0 | return g_resource_file_get_uri (file); |
302 | 0 | } |
303 | | |
304 | | static GFile * |
305 | | g_resource_file_get_parent (GFile *file) |
306 | 0 | { |
307 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
308 | 0 | GResourceFile *parent; |
309 | 0 | gchar *end; |
310 | |
|
311 | 0 | end = strrchr (resource->path, '/'); |
312 | |
|
313 | 0 | if (end == G_RESOURCE_FILE (file)->path) |
314 | 0 | return NULL; |
315 | | |
316 | 0 | parent = g_object_new (G_TYPE_RESOURCE_FILE, NULL); |
317 | 0 | parent->path = g_strndup (resource->path, |
318 | 0 | end - resource->path); |
319 | |
|
320 | 0 | return G_FILE (parent); |
321 | 0 | } |
322 | | |
323 | | static GFile * |
324 | | g_resource_file_dup (GFile *file) |
325 | 0 | { |
326 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
327 | |
|
328 | 0 | return g_resource_file_new_for_path (resource->path); |
329 | 0 | } |
330 | | |
331 | | static guint |
332 | | g_resource_file_hash (GFile *file) |
333 | 0 | { |
334 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
335 | |
|
336 | 0 | return g_str_hash (resource->path); |
337 | 0 | } |
338 | | |
339 | | static gboolean |
340 | | g_resource_file_equal (GFile *file1, |
341 | | GFile *file2) |
342 | 0 | { |
343 | 0 | GResourceFile *resource1 = G_RESOURCE_FILE (file1); |
344 | 0 | GResourceFile *resource2 = G_RESOURCE_FILE (file2); |
345 | |
|
346 | 0 | return g_str_equal (resource1->path, resource2->path); |
347 | 0 | } |
348 | | |
349 | | static const char * |
350 | | match_prefix (const char *path, |
351 | | const char *prefix) |
352 | 0 | { |
353 | 0 | int prefix_len; |
354 | |
|
355 | 0 | prefix_len = strlen (prefix); |
356 | 0 | if (strncmp (path, prefix, prefix_len) != 0) |
357 | 0 | return NULL; |
358 | | |
359 | | /* Handle the case where prefix is the root, so that |
360 | | * the IS_DIR_SEPRARATOR check below works */ |
361 | 0 | if (prefix_len > 0 && |
362 | 0 | prefix[prefix_len-1] == '/') |
363 | 0 | prefix_len--; |
364 | |
|
365 | 0 | return path + prefix_len; |
366 | 0 | } |
367 | | |
368 | | static gboolean |
369 | | g_resource_file_prefix_matches (GFile *parent, |
370 | | GFile *descendant) |
371 | 0 | { |
372 | 0 | GResourceFile *parent_resource = G_RESOURCE_FILE (parent); |
373 | 0 | GResourceFile *descendant_resource = G_RESOURCE_FILE (descendant); |
374 | 0 | const char *remainder; |
375 | |
|
376 | 0 | remainder = match_prefix (descendant_resource->path, parent_resource->path); |
377 | 0 | if (remainder != NULL && *remainder == '/') |
378 | 0 | return TRUE; |
379 | 0 | return FALSE; |
380 | 0 | } |
381 | | |
382 | | static char * |
383 | | g_resource_file_get_relative_path (GFile *parent, |
384 | | GFile *descendant) |
385 | 0 | { |
386 | 0 | GResourceFile *parent_resource = G_RESOURCE_FILE (parent); |
387 | 0 | GResourceFile *descendant_resource = G_RESOURCE_FILE (descendant); |
388 | 0 | const char *remainder; |
389 | |
|
390 | 0 | remainder = match_prefix (descendant_resource->path, parent_resource->path); |
391 | |
|
392 | 0 | if (remainder != NULL && *remainder == '/') |
393 | 0 | return g_strdup (remainder + 1); |
394 | 0 | return NULL; |
395 | 0 | } |
396 | | |
397 | | static GFile * |
398 | | g_resource_file_resolve_relative_path (GFile *file, |
399 | | const char *relative_path) |
400 | 0 | { |
401 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
402 | 0 | char *filename; |
403 | 0 | GFile *child; |
404 | |
|
405 | 0 | if (relative_path[0] == '/') |
406 | 0 | return g_resource_file_new_for_path (relative_path); |
407 | | |
408 | 0 | filename = g_build_path ("/", resource->path, relative_path, NULL); |
409 | 0 | child = g_resource_file_new_for_path (filename); |
410 | 0 | g_free (filename); |
411 | |
|
412 | 0 | return child; |
413 | 0 | } |
414 | | |
415 | | static GFileEnumerator * |
416 | | g_resource_file_enumerate_children (GFile *file, |
417 | | const char *attributes, |
418 | | GFileQueryInfoFlags flags, |
419 | | GCancellable *cancellable, |
420 | | GError **error) |
421 | 0 | { |
422 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
423 | 0 | return _g_resource_file_enumerator_new (resource, |
424 | 0 | attributes, flags, |
425 | 0 | cancellable, error); |
426 | 0 | } |
427 | | |
428 | | static GFile * |
429 | | g_resource_file_get_child_for_display_name (GFile *file, |
430 | | const char *display_name, |
431 | | GError **error) |
432 | 0 | { |
433 | 0 | GFile *new_file; |
434 | |
|
435 | 0 | new_file = g_file_get_child (file, display_name); |
436 | |
|
437 | 0 | return new_file; |
438 | 0 | } |
439 | | |
440 | | static GFileInfo * |
441 | | g_resource_file_query_info (GFile *file, |
442 | | const char *attributes, |
443 | | GFileQueryInfoFlags flags, |
444 | | GCancellable *cancellable, |
445 | | GError **error) |
446 | 0 | { |
447 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
448 | 0 | GError *my_error = NULL; |
449 | 0 | GFileInfo *info; |
450 | 0 | GFileAttributeMatcher *matcher; |
451 | 0 | gboolean res; |
452 | 0 | gsize size = 0; |
453 | 0 | guint32 resource_flags = 0; |
454 | 0 | char **children; |
455 | 0 | gboolean is_dir; |
456 | 0 | char *base; |
457 | |
|
458 | 0 | is_dir = FALSE; |
459 | 0 | children = g_resources_enumerate_children (resource->path, 0, NULL); |
460 | 0 | if (children != NULL) |
461 | 0 | { |
462 | 0 | g_strfreev (children); |
463 | 0 | is_dir = TRUE; |
464 | 0 | } |
465 | | |
466 | | /* root is always there */ |
467 | 0 | if (strcmp ("/", resource->path) == 0) |
468 | 0 | is_dir = TRUE; |
469 | |
|
470 | 0 | if (!is_dir) |
471 | 0 | { |
472 | 0 | res = g_resources_get_info (resource->path, 0, &size, &resource_flags, &my_error); |
473 | 0 | if (!res) |
474 | 0 | { |
475 | 0 | if (g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
476 | 0 | { |
477 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
478 | 0 | _("The resource at “%s” does not exist"), |
479 | 0 | resource->path); |
480 | 0 | } |
481 | 0 | else |
482 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
483 | 0 | my_error->message); |
484 | 0 | g_clear_error (&my_error); |
485 | 0 | return FALSE; |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | 0 | matcher = g_file_attribute_matcher_new (attributes); |
490 | |
|
491 | 0 | info = g_file_info_new (); |
492 | 0 | base = g_resource_file_get_basename (file); |
493 | 0 | g_file_info_set_name (info, base); |
494 | 0 | g_file_info_set_display_name (info, base); |
495 | |
|
496 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ, TRUE); |
497 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE, FALSE); |
498 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE, FALSE); |
499 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME, FALSE); |
500 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE, FALSE); |
501 | 0 | _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH, FALSE); |
502 | |
|
503 | 0 | if (is_dir) |
504 | 0 | { |
505 | 0 | g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); |
506 | 0 | } |
507 | 0 | else |
508 | 0 | { |
509 | 0 | GBytes *bytes; |
510 | 0 | char *content_type; |
511 | |
|
512 | 0 | g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR); |
513 | 0 | g_file_info_set_size (info, size); |
514 | |
|
515 | 0 | if ((_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) || |
516 | 0 | ((~resource_flags & G_RESOURCE_FLAGS_COMPRESSED) && |
517 | 0 | _g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))) && |
518 | 0 | (bytes = g_resources_lookup_data (resource->path, 0, NULL))) |
519 | 0 | { |
520 | 0 | const guchar *data; |
521 | 0 | gsize data_size; |
522 | |
|
523 | 0 | data = g_bytes_get_data (bytes, &data_size); |
524 | 0 | content_type = g_content_type_guess (base, data, data_size, NULL); |
525 | |
|
526 | 0 | g_bytes_unref (bytes); |
527 | 0 | } |
528 | 0 | else |
529 | 0 | content_type = NULL; |
530 | |
|
531 | 0 | if (content_type) |
532 | 0 | { |
533 | 0 | _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE, content_type); |
534 | 0 | _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type); |
535 | |
|
536 | 0 | g_free (content_type); |
537 | 0 | } |
538 | 0 | } |
539 | |
|
540 | 0 | g_free (base); |
541 | 0 | g_file_attribute_matcher_unref (matcher); |
542 | |
|
543 | 0 | return info; |
544 | 0 | } |
545 | | |
546 | | static GFileInfo * |
547 | | g_resource_file_query_filesystem_info (GFile *file, |
548 | | const char *attributes, |
549 | | GCancellable *cancellable, |
550 | | GError **error) |
551 | 0 | { |
552 | 0 | GFileInfo *info; |
553 | 0 | GFileAttributeMatcher *matcher; |
554 | |
|
555 | 0 | info = g_file_info_new (); |
556 | |
|
557 | 0 | matcher = g_file_attribute_matcher_new (attributes); |
558 | 0 | if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE)) |
559 | 0 | g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "resource"); |
560 | |
|
561 | 0 | if (g_file_attribute_matcher_matches (matcher, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY)) g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE); |
562 | |
|
563 | 0 | g_file_attribute_matcher_unref (matcher); |
564 | |
|
565 | 0 | return info; |
566 | 0 | } |
567 | | |
568 | | static GFileAttributeInfoList * |
569 | | g_resource_file_query_settable_attributes (GFile *file, |
570 | | GCancellable *cancellable, |
571 | | GError **error) |
572 | 0 | { |
573 | 0 | return g_file_attribute_info_list_ref (resource_writable_attributes); |
574 | 0 | } |
575 | | |
576 | | static GFileAttributeInfoList * |
577 | | g_resource_file_query_writable_namespaces (GFile *file, |
578 | | GCancellable *cancellable, |
579 | | GError **error) |
580 | 0 | { |
581 | 0 | return g_file_attribute_info_list_ref (resource_writable_namespaces); |
582 | 0 | } |
583 | | |
584 | | static GFileInputStream * |
585 | | g_resource_file_read (GFile *file, |
586 | | GCancellable *cancellable, |
587 | | GError **error) |
588 | 0 | { |
589 | 0 | GResourceFile *resource = G_RESOURCE_FILE (file); |
590 | 0 | GError *my_error = NULL; |
591 | 0 | GInputStream *stream; |
592 | 0 | GFileInputStream *res; |
593 | |
|
594 | 0 | stream = g_resources_open_stream (resource->path, 0, &my_error); |
595 | |
|
596 | 0 | if (stream == NULL) |
597 | 0 | { |
598 | 0 | if (g_error_matches (my_error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND)) |
599 | 0 | { |
600 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
601 | 0 | _("The resource at “%s” does not exist"), |
602 | 0 | resource->path); |
603 | 0 | } |
604 | 0 | else |
605 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
606 | 0 | my_error->message); |
607 | 0 | g_clear_error (&my_error); |
608 | 0 | return NULL; |
609 | 0 | } |
610 | | |
611 | 0 | res = _g_resource_file_input_stream_new (stream, file); |
612 | 0 | g_object_unref (stream); |
613 | 0 | return res; |
614 | 0 | } |
615 | | |
616 | | typedef GFileMonitor GResourceFileMonitor; |
617 | | typedef GFileMonitorClass GResourceFileMonitorClass; |
618 | | |
619 | | GType g_resource_file_monitor_get_type (void); |
620 | | |
621 | 0 | G_DEFINE_TYPE (GResourceFileMonitor, g_resource_file_monitor, G_TYPE_FILE_MONITOR) |
622 | 0 |
|
623 | 0 | static gboolean |
624 | 0 | g_resource_file_monitor_cancel (GFileMonitor *monitor) |
625 | 0 | { |
626 | 0 | return TRUE; |
627 | 0 | } |
628 | | |
629 | | static void |
630 | | g_resource_file_monitor_init (GResourceFileMonitor *monitor) |
631 | 0 | { |
632 | 0 | } |
633 | | |
634 | | static void |
635 | | g_resource_file_monitor_class_init (GResourceFileMonitorClass *class) |
636 | 0 | { |
637 | 0 | class->cancel = g_resource_file_monitor_cancel; |
638 | 0 | } |
639 | | |
640 | | static GFileMonitor * |
641 | | g_resource_file_monitor_file (GFile *file, |
642 | | GFileMonitorFlags flags, |
643 | | GCancellable *cancellable, |
644 | | GError **error) |
645 | 0 | { |
646 | 0 | return g_object_new (g_resource_file_monitor_get_type (), NULL); |
647 | 0 | } |
648 | | |
649 | | static GFile * |
650 | | g_resource_file_set_display_name (GFile *file, |
651 | | const char *display_name, |
652 | | GCancellable *cancellable, |
653 | | GError **error) |
654 | 0 | { |
655 | 0 | g_set_error_literal (error, |
656 | 0 | G_IO_ERROR, |
657 | 0 | G_IO_ERROR_NOT_SUPPORTED, |
658 | 0 | _("Resource files cannot be renamed")); |
659 | 0 | return NULL; |
660 | 0 | } |
661 | | |
662 | | static void |
663 | | g_resource_file_file_iface_init (GFileIface *iface) |
664 | 0 | { |
665 | 0 | iface->dup = g_resource_file_dup; |
666 | 0 | iface->hash = g_resource_file_hash; |
667 | 0 | iface->equal = g_resource_file_equal; |
668 | 0 | iface->is_native = g_resource_file_is_native; |
669 | 0 | iface->has_uri_scheme = g_resource_file_has_uri_scheme; |
670 | 0 | iface->get_uri_scheme = g_resource_file_get_uri_scheme; |
671 | 0 | iface->get_basename = g_resource_file_get_basename; |
672 | 0 | iface->get_path = g_resource_file_get_path; |
673 | 0 | iface->get_uri = g_resource_file_get_uri; |
674 | 0 | iface->get_parse_name = g_resource_file_get_parse_name; |
675 | 0 | iface->get_parent = g_resource_file_get_parent; |
676 | 0 | iface->prefix_matches = g_resource_file_prefix_matches; |
677 | 0 | iface->get_relative_path = g_resource_file_get_relative_path; |
678 | 0 | iface->resolve_relative_path = g_resource_file_resolve_relative_path; |
679 | 0 | iface->get_child_for_display_name = g_resource_file_get_child_for_display_name; |
680 | 0 | iface->set_display_name = g_resource_file_set_display_name; |
681 | 0 | iface->enumerate_children = g_resource_file_enumerate_children; |
682 | 0 | iface->query_info = g_resource_file_query_info; |
683 | 0 | iface->query_filesystem_info = g_resource_file_query_filesystem_info; |
684 | 0 | iface->query_settable_attributes = g_resource_file_query_settable_attributes; |
685 | 0 | iface->query_writable_namespaces = g_resource_file_query_writable_namespaces; |
686 | 0 | iface->read_fn = g_resource_file_read; |
687 | 0 | iface->monitor_file = g_resource_file_monitor_file; |
688 | |
|
689 | 0 | iface->supports_thread_contexts = TRUE; |
690 | 0 | } |
691 | | |
692 | | static GFileInfo *g_resource_file_enumerator_next_file (GFileEnumerator *enumerator, |
693 | | GCancellable *cancellable, |
694 | | GError **error); |
695 | | static gboolean g_resource_file_enumerator_close (GFileEnumerator *enumerator, |
696 | | GCancellable *cancellable, |
697 | | GError **error); |
698 | | |
699 | | static void |
700 | | g_resource_file_enumerator_finalize (GObject *object) |
701 | 0 | { |
702 | 0 | GResourceFileEnumerator *resource; |
703 | |
|
704 | 0 | resource = G_RESOURCE_FILE_ENUMERATOR (object); |
705 | |
|
706 | 0 | g_strfreev (resource->children); |
707 | 0 | g_free (resource->path); |
708 | 0 | g_free (resource->attributes); |
709 | |
|
710 | 0 | G_OBJECT_CLASS (g_resource_file_enumerator_parent_class)->finalize (object); |
711 | 0 | } |
712 | | |
713 | | static void |
714 | | g_resource_file_enumerator_class_init (GResourceFileEnumeratorClass *klass) |
715 | 0 | { |
716 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
717 | 0 | GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass); |
718 | |
|
719 | 0 | gobject_class->finalize = g_resource_file_enumerator_finalize; |
720 | |
|
721 | 0 | enumerator_class->next_file = g_resource_file_enumerator_next_file; |
722 | 0 | enumerator_class->close_fn = g_resource_file_enumerator_close; |
723 | 0 | } |
724 | | |
725 | | static void |
726 | | g_resource_file_enumerator_init (GResourceFileEnumerator *resource) |
727 | 0 | { |
728 | 0 | } |
729 | | |
730 | | static GFileEnumerator * |
731 | | _g_resource_file_enumerator_new (GResourceFile *file, |
732 | | const char *attributes, |
733 | | GFileQueryInfoFlags flags, |
734 | | GCancellable *cancellable, |
735 | | GError **error) |
736 | 0 | { |
737 | 0 | GResourceFileEnumerator *resource; |
738 | 0 | char **children; |
739 | 0 | gboolean res; |
740 | |
|
741 | 0 | children = g_resources_enumerate_children (file->path, 0, NULL); |
742 | 0 | if (children == NULL && |
743 | 0 | strcmp ("/", file->path) != 0) |
744 | 0 | { |
745 | 0 | res = g_resources_get_info (file->path, 0, NULL, NULL, NULL); |
746 | 0 | if (res) |
747 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY, |
748 | 0 | _("The resource at “%s” is not a directory"), |
749 | 0 | file->path); |
750 | 0 | else |
751 | 0 | g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
752 | 0 | _("The resource at “%s” does not exist"), |
753 | 0 | file->path); |
754 | 0 | return NULL; |
755 | 0 | } |
756 | | |
757 | 0 | resource = g_object_new (G_TYPE_RESOURCE_FILE_ENUMERATOR, |
758 | 0 | "container", file, |
759 | 0 | NULL); |
760 | |
|
761 | 0 | resource->children = children; |
762 | 0 | resource->path = g_strdup (file->path); |
763 | 0 | resource->attributes = g_strdup (attributes); |
764 | 0 | resource->flags = flags; |
765 | |
|
766 | 0 | return G_FILE_ENUMERATOR (resource); |
767 | 0 | } |
768 | | |
769 | | static GFileInfo * |
770 | | g_resource_file_enumerator_next_file (GFileEnumerator *enumerator, |
771 | | GCancellable *cancellable, |
772 | | GError **error) |
773 | 0 | { |
774 | 0 | GResourceFileEnumerator *resource = G_RESOURCE_FILE_ENUMERATOR (enumerator); |
775 | 0 | char *path; |
776 | 0 | GFileInfo *info; |
777 | 0 | GFile *file; |
778 | |
|
779 | 0 | if (resource->children == NULL || |
780 | 0 | resource->children[resource->index] == NULL) |
781 | 0 | return NULL; |
782 | | |
783 | 0 | path = g_build_path ("/", resource->path, resource->children[resource->index++], NULL); |
784 | 0 | file = g_resource_file_new_for_path (path); |
785 | 0 | g_free (path); |
786 | |
|
787 | 0 | info = g_file_query_info (file, |
788 | 0 | resource->attributes, |
789 | 0 | resource->flags, |
790 | 0 | cancellable, |
791 | 0 | error); |
792 | |
|
793 | 0 | g_object_unref (file); |
794 | |
|
795 | 0 | return info; |
796 | 0 | } |
797 | | |
798 | | static gboolean |
799 | | g_resource_file_enumerator_close (GFileEnumerator *enumerator, |
800 | | GCancellable *cancellable, |
801 | | GError **error) |
802 | 0 | { |
803 | 0 | return TRUE; |
804 | 0 | } |
805 | | |
806 | | |
807 | | struct _GResourceFileInputStream |
808 | | { |
809 | | GFileInputStream parent_instance; |
810 | | GInputStream *stream; |
811 | | GFile *file; |
812 | | }; |
813 | | |
814 | | struct _GResourceFileInputStreamClass |
815 | | { |
816 | | GFileInputStreamClass parent_class; |
817 | | }; |
818 | | |
819 | | #define g_resource_file_input_stream_get_type _g_resource_file_input_stream_get_type |
820 | 0 | G_DEFINE_TYPE (GResourceFileInputStream, g_resource_file_input_stream, G_TYPE_FILE_INPUT_STREAM) |
821 | 0 |
|
822 | 0 | static gssize g_resource_file_input_stream_read (GInputStream *stream, |
823 | 0 | void *buffer, |
824 | 0 | gsize count, |
825 | 0 | GCancellable *cancellable, |
826 | 0 | GError **error); |
827 | 0 | static gssize g_resource_file_input_stream_skip (GInputStream *stream, |
828 | 0 | gsize count, |
829 | 0 | GCancellable *cancellable, |
830 | 0 | GError **error); |
831 | 0 | static gboolean g_resource_file_input_stream_close (GInputStream *stream, |
832 | 0 | GCancellable *cancellable, |
833 | 0 | GError **error); |
834 | 0 | static goffset g_resource_file_input_stream_tell (GFileInputStream *stream); |
835 | 0 | static gboolean g_resource_file_input_stream_can_seek (GFileInputStream *stream); |
836 | 0 | static gboolean g_resource_file_input_stream_seek (GFileInputStream *stream, |
837 | 0 | goffset offset, |
838 | 0 | GSeekType type, |
839 | 0 | GCancellable *cancellable, |
840 | 0 | GError **error); |
841 | 0 | static GFileInfo *g_resource_file_input_stream_query_info (GFileInputStream *stream, |
842 | 0 | const char *attributes, |
843 | 0 | GCancellable *cancellable, |
844 | 0 | GError **error); |
845 | 0 |
|
846 | 0 | static void |
847 | 0 | g_resource_file_input_stream_finalize (GObject *object) |
848 | 0 | { |
849 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (object); |
850 | |
|
851 | 0 | g_object_unref (file->stream); |
852 | 0 | g_object_unref (file->file); |
853 | 0 | G_OBJECT_CLASS (g_resource_file_input_stream_parent_class)->finalize (object); |
854 | 0 | } |
855 | | |
856 | | static void |
857 | | g_resource_file_input_stream_class_init (GResourceFileInputStreamClass *klass) |
858 | 0 | { |
859 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
860 | 0 | GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass); |
861 | 0 | GFileInputStreamClass *file_stream_class = G_FILE_INPUT_STREAM_CLASS (klass); |
862 | |
|
863 | 0 | gobject_class->finalize = g_resource_file_input_stream_finalize; |
864 | |
|
865 | 0 | stream_class->read_fn = g_resource_file_input_stream_read; |
866 | 0 | stream_class->skip = g_resource_file_input_stream_skip; |
867 | 0 | stream_class->close_fn = g_resource_file_input_stream_close; |
868 | 0 | file_stream_class->tell = g_resource_file_input_stream_tell; |
869 | 0 | file_stream_class->can_seek = g_resource_file_input_stream_can_seek; |
870 | 0 | file_stream_class->seek = g_resource_file_input_stream_seek; |
871 | 0 | file_stream_class->query_info = g_resource_file_input_stream_query_info; |
872 | 0 | } |
873 | | |
874 | | static void |
875 | | g_resource_file_input_stream_init (GResourceFileInputStream *info) |
876 | 0 | { |
877 | 0 | } |
878 | | |
879 | | static GFileInputStream * |
880 | | _g_resource_file_input_stream_new (GInputStream *in_stream, GFile *file) |
881 | 0 | { |
882 | 0 | GResourceFileInputStream *stream; |
883 | |
|
884 | 0 | stream = g_object_new (G_TYPE_RESOURCE_FILE_INPUT_STREAM, NULL); |
885 | 0 | stream->stream = g_object_ref (in_stream); |
886 | 0 | stream->file = g_object_ref (file); |
887 | |
|
888 | 0 | return G_FILE_INPUT_STREAM (stream); |
889 | 0 | } |
890 | | |
891 | | static gssize |
892 | | g_resource_file_input_stream_read (GInputStream *stream, |
893 | | void *buffer, |
894 | | gsize count, |
895 | | GCancellable *cancellable, |
896 | | GError **error) |
897 | 0 | { |
898 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
899 | 0 | return g_input_stream_read (file->stream, |
900 | 0 | buffer, count, cancellable, error); |
901 | 0 | } |
902 | | |
903 | | static gssize |
904 | | g_resource_file_input_stream_skip (GInputStream *stream, |
905 | | gsize count, |
906 | | GCancellable *cancellable, |
907 | | GError **error) |
908 | 0 | { |
909 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
910 | 0 | return g_input_stream_skip (file->stream, |
911 | 0 | count, cancellable, error); |
912 | 0 | } |
913 | | |
914 | | static gboolean |
915 | | g_resource_file_input_stream_close (GInputStream *stream, |
916 | | GCancellable *cancellable, |
917 | | GError **error) |
918 | 0 | { |
919 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
920 | 0 | return g_input_stream_close (file->stream, |
921 | 0 | cancellable, error); |
922 | 0 | } |
923 | | |
924 | | |
925 | | static goffset |
926 | | g_resource_file_input_stream_tell (GFileInputStream *stream) |
927 | 0 | { |
928 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
929 | |
|
930 | 0 | if (!G_IS_SEEKABLE (file->stream)) |
931 | 0 | return 0; |
932 | | |
933 | 0 | return g_seekable_tell (G_SEEKABLE (file->stream)); |
934 | 0 | } |
935 | | |
936 | | static gboolean |
937 | | g_resource_file_input_stream_can_seek (GFileInputStream *stream) |
938 | 0 | { |
939 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
940 | |
|
941 | 0 | return G_IS_SEEKABLE (file->stream) && g_seekable_can_seek (G_SEEKABLE (file->stream)); |
942 | 0 | } |
943 | | |
944 | | static gboolean |
945 | | g_resource_file_input_stream_seek (GFileInputStream *stream, |
946 | | goffset offset, |
947 | | GSeekType type, |
948 | | GCancellable *cancellable, |
949 | | GError **error) |
950 | 0 | { |
951 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
952 | |
|
953 | 0 | if (!G_IS_SEEKABLE (file->stream)) |
954 | 0 | { |
955 | 0 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
956 | 0 | _("Input stream doesn’t implement seek")); |
957 | 0 | return FALSE; |
958 | 0 | } |
959 | | |
960 | 0 | return g_seekable_seek (G_SEEKABLE (file->stream), |
961 | 0 | offset, type, cancellable, error); |
962 | 0 | } |
963 | | |
964 | | static GFileInfo * |
965 | | g_resource_file_input_stream_query_info (GFileInputStream *stream, |
966 | | const char *attributes, |
967 | | GCancellable *cancellable, |
968 | | GError **error) |
969 | 0 | { |
970 | 0 | GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); |
971 | |
|
972 | 0 | return g_file_query_info (file->file, attributes, 0, cancellable, error); |
973 | 0 | } |