Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 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.1 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 | | * Author: Alexander Larsson <alexl@redhat.com> |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | #include <string.h> |
23 | | #include "gvfs.h" |
24 | | #include "glib-private.h" |
25 | | #include "glocalvfs.h" |
26 | | #include "gresourcefile.h" |
27 | | #include "giomodule-priv.h" |
28 | | #include "glibintl.h" |
29 | | |
30 | | |
31 | | /** |
32 | | * SECTION:gvfs |
33 | | * @short_description: Virtual File System |
34 | | * @include: gio/gio.h |
35 | | * |
36 | | * Entry point for using GIO functionality. |
37 | | * |
38 | | */ |
39 | | |
40 | | static GRWLock additional_schemes_lock; |
41 | | |
42 | | typedef struct _GVfsPrivate { |
43 | | GHashTable *additional_schemes; |
44 | | char const **supported_schemes; |
45 | | } GVfsPrivate; |
46 | | |
47 | | typedef struct { |
48 | | GVfsFileLookupFunc uri_func; |
49 | | gpointer uri_data; |
50 | | GDestroyNotify uri_destroy; |
51 | | |
52 | | GVfsFileLookupFunc parse_name_func; |
53 | | gpointer parse_name_data; |
54 | | GDestroyNotify parse_name_destroy; |
55 | | } GVfsURISchemeData; |
56 | | |
57 | | G_DEFINE_TYPE_WITH_PRIVATE (GVfs, g_vfs, G_TYPE_OBJECT) |
58 | | |
59 | | static void |
60 | | g_vfs_dispose (GObject *object) |
61 | 0 | { |
62 | 0 | GVfs *vfs = G_VFS (object); |
63 | 0 | GVfsPrivate *priv = g_vfs_get_instance_private (vfs); |
64 | |
|
65 | 0 | g_clear_pointer (&priv->additional_schemes, g_hash_table_destroy); |
66 | 0 | g_clear_pointer (&priv->supported_schemes, g_free); |
67 | |
|
68 | 0 | G_OBJECT_CLASS (g_vfs_parent_class)->dispose (object); |
69 | 0 | } |
70 | | |
71 | | static void |
72 | | g_vfs_class_init (GVfsClass *klass) |
73 | 0 | { |
74 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
75 | 0 | object_class->dispose = g_vfs_dispose; |
76 | 0 | } |
77 | | |
78 | | static GFile * |
79 | | resource_parse_name (GVfs *vfs, |
80 | | const char *parse_name, |
81 | | gpointer user_data) |
82 | 0 | { |
83 | 0 | if (g_str_has_prefix (parse_name, "resource:")) |
84 | 0 | return _g_resource_file_new (parse_name); |
85 | | |
86 | 0 | return NULL; |
87 | 0 | } |
88 | | |
89 | | static GFile * |
90 | | resource_get_file_for_uri (GVfs *vfs, |
91 | | const char *uri, |
92 | | gpointer user_data) |
93 | 0 | { |
94 | 0 | return _g_resource_file_new (uri); |
95 | 0 | } |
96 | | |
97 | | static void |
98 | | g_vfs_uri_lookup_func_closure_free (gpointer data) |
99 | 0 | { |
100 | 0 | GVfsURISchemeData *closure = data; |
101 | |
|
102 | 0 | if (closure->uri_destroy) |
103 | 0 | closure->uri_destroy (closure->uri_data); |
104 | 0 | if (closure->parse_name_destroy) |
105 | 0 | closure->parse_name_destroy (closure->parse_name_data); |
106 | |
|
107 | 0 | g_free (closure); |
108 | 0 | } |
109 | | |
110 | | static void |
111 | | g_vfs_init (GVfs *vfs) |
112 | 0 | { |
113 | 0 | GVfsPrivate *priv = g_vfs_get_instance_private (vfs); |
114 | 0 | priv->additional_schemes = |
115 | 0 | g_hash_table_new_full (g_str_hash, g_str_equal, |
116 | 0 | g_free, g_vfs_uri_lookup_func_closure_free); |
117 | |
|
118 | 0 | g_vfs_register_uri_scheme (vfs, "resource", |
119 | 0 | resource_get_file_for_uri, NULL, NULL, |
120 | 0 | resource_parse_name, NULL, NULL); |
121 | 0 | } |
122 | | |
123 | | /** |
124 | | * g_vfs_is_active: |
125 | | * @vfs: a #GVfs. |
126 | | * |
127 | | * Checks if the VFS is active. |
128 | | * |
129 | | * Returns: %TRUE if construction of the @vfs was successful |
130 | | * and it is now active. |
131 | | */ |
132 | | gboolean |
133 | | g_vfs_is_active (GVfs *vfs) |
134 | 0 | { |
135 | 0 | GVfsClass *class; |
136 | |
|
137 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), FALSE); |
138 | | |
139 | 0 | class = G_VFS_GET_CLASS (vfs); |
140 | |
|
141 | 0 | return (* class->is_active) (vfs); |
142 | 0 | } |
143 | | |
144 | | |
145 | | /** |
146 | | * g_vfs_get_file_for_path: |
147 | | * @vfs: a #GVfs. |
148 | | * @path: a string containing a VFS path. |
149 | | * |
150 | | * Gets a #GFile for @path. |
151 | | * |
152 | | * Returns: (transfer full): a #GFile. |
153 | | * Free the returned object with g_object_unref(). |
154 | | */ |
155 | | GFile * |
156 | | g_vfs_get_file_for_path (GVfs *vfs, |
157 | | const char *path) |
158 | 0 | { |
159 | 0 | GVfsClass *class; |
160 | | |
161 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), NULL); |
162 | 0 | g_return_val_if_fail (path != NULL, NULL); |
163 | | |
164 | 0 | class = G_VFS_GET_CLASS (vfs); |
165 | |
|
166 | 0 | return (* class->get_file_for_path) (vfs, path); |
167 | 0 | } |
168 | | |
169 | | static GFile * |
170 | | parse_name_internal (GVfs *vfs, |
171 | | const char *parse_name) |
172 | 0 | { |
173 | 0 | GVfsPrivate *priv = g_vfs_get_instance_private (vfs); |
174 | 0 | GHashTableIter iter; |
175 | 0 | GVfsURISchemeData *closure; |
176 | 0 | GFile *ret = NULL; |
177 | |
|
178 | 0 | g_rw_lock_reader_lock (&additional_schemes_lock); |
179 | 0 | g_hash_table_iter_init (&iter, priv->additional_schemes); |
180 | |
|
181 | 0 | while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &closure)) |
182 | 0 | { |
183 | 0 | ret = closure->parse_name_func (vfs, parse_name, |
184 | 0 | closure->parse_name_data); |
185 | |
|
186 | 0 | if (ret) |
187 | 0 | break; |
188 | 0 | } |
189 | |
|
190 | 0 | g_rw_lock_reader_unlock (&additional_schemes_lock); |
191 | |
|
192 | 0 | return ret; |
193 | 0 | } |
194 | | |
195 | | static GFile * |
196 | | get_file_for_uri_internal (GVfs *vfs, |
197 | | const char *uri) |
198 | 0 | { |
199 | 0 | GVfsPrivate *priv = g_vfs_get_instance_private (vfs); |
200 | 0 | GFile *ret = NULL; |
201 | 0 | char *scheme; |
202 | 0 | GVfsURISchemeData *closure; |
203 | |
|
204 | 0 | scheme = g_uri_parse_scheme (uri); |
205 | 0 | if (scheme == NULL) |
206 | 0 | return NULL; |
207 | | |
208 | 0 | g_rw_lock_reader_lock (&additional_schemes_lock); |
209 | 0 | closure = g_hash_table_lookup (priv->additional_schemes, scheme); |
210 | |
|
211 | 0 | if (closure) |
212 | 0 | ret = closure->uri_func (vfs, uri, closure->uri_data); |
213 | |
|
214 | 0 | g_rw_lock_reader_unlock (&additional_schemes_lock); |
215 | |
|
216 | 0 | g_free (scheme); |
217 | 0 | return ret; |
218 | 0 | } |
219 | | |
220 | | /** |
221 | | * g_vfs_get_file_for_uri: |
222 | | * @vfs: a#GVfs. |
223 | | * @uri: a string containing a URI |
224 | | * |
225 | | * Gets a #GFile for @uri. |
226 | | * |
227 | | * This operation never fails, but the returned object |
228 | | * might not support any I/O operation if the URI |
229 | | * is malformed or if the URI scheme is not supported. |
230 | | * |
231 | | * Returns: (transfer full): a #GFile. |
232 | | * Free the returned object with g_object_unref(). |
233 | | */ |
234 | | GFile * |
235 | | g_vfs_get_file_for_uri (GVfs *vfs, |
236 | | const char *uri) |
237 | 0 | { |
238 | 0 | GVfsClass *class; |
239 | 0 | GFile *ret = NULL; |
240 | | |
241 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), NULL); |
242 | 0 | g_return_val_if_fail (uri != NULL, NULL); |
243 | | |
244 | 0 | class = G_VFS_GET_CLASS (vfs); |
245 | |
|
246 | 0 | ret = get_file_for_uri_internal (vfs, uri); |
247 | 0 | if (!ret) |
248 | 0 | ret = (* class->get_file_for_uri) (vfs, uri); |
249 | |
|
250 | 0 | g_assert (ret != NULL); |
251 | | |
252 | 0 | return g_steal_pointer (&ret); |
253 | 0 | } |
254 | | |
255 | | /** |
256 | | * g_vfs_get_supported_uri_schemes: |
257 | | * @vfs: a #GVfs. |
258 | | * |
259 | | * Gets a list of URI schemes supported by @vfs. |
260 | | * |
261 | | * Returns: (transfer none): a %NULL-terminated array of strings. |
262 | | * The returned array belongs to GIO and must |
263 | | * not be freed or modified. |
264 | | */ |
265 | | const gchar * const * |
266 | | g_vfs_get_supported_uri_schemes (GVfs *vfs) |
267 | 0 | { |
268 | 0 | GVfsPrivate *priv; |
269 | |
|
270 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), NULL); |
271 | | |
272 | 0 | priv = g_vfs_get_instance_private (vfs); |
273 | |
|
274 | 0 | if (!priv->supported_schemes) |
275 | 0 | { |
276 | 0 | GVfsClass *class; |
277 | 0 | const char * const *default_schemes; |
278 | 0 | const char *additional_scheme; |
279 | 0 | GPtrArray *supported_schemes; |
280 | 0 | GHashTableIter iter; |
281 | |
|
282 | 0 | class = G_VFS_GET_CLASS (vfs); |
283 | |
|
284 | 0 | default_schemes = (* class->get_supported_uri_schemes) (vfs); |
285 | 0 | supported_schemes = g_ptr_array_new (); |
286 | |
|
287 | 0 | for (; default_schemes && *default_schemes; default_schemes++) |
288 | 0 | g_ptr_array_add (supported_schemes, (gpointer) *default_schemes); |
289 | |
|
290 | 0 | g_rw_lock_reader_lock (&additional_schemes_lock); |
291 | 0 | g_hash_table_iter_init (&iter, priv->additional_schemes); |
292 | |
|
293 | 0 | while (g_hash_table_iter_next |
294 | 0 | (&iter, (gpointer *) &additional_scheme, NULL)) |
295 | 0 | g_ptr_array_add (supported_schemes, (gpointer) additional_scheme); |
296 | |
|
297 | 0 | g_rw_lock_reader_unlock (&additional_schemes_lock); |
298 | |
|
299 | 0 | g_ptr_array_add (supported_schemes, NULL); |
300 | |
|
301 | 0 | g_free (priv->supported_schemes); |
302 | 0 | priv->supported_schemes = |
303 | 0 | (char const **) g_ptr_array_free (supported_schemes, FALSE); |
304 | 0 | } |
305 | |
|
306 | 0 | return priv->supported_schemes; |
307 | 0 | } |
308 | | |
309 | | /** |
310 | | * g_vfs_parse_name: |
311 | | * @vfs: a #GVfs. |
312 | | * @parse_name: a string to be parsed by the VFS module. |
313 | | * |
314 | | * This operation never fails, but the returned object might |
315 | | * not support any I/O operations if the @parse_name cannot |
316 | | * be parsed by the #GVfs module. |
317 | | * |
318 | | * Returns: (transfer full): a #GFile for the given @parse_name. |
319 | | * Free the returned object with g_object_unref(). |
320 | | */ |
321 | | GFile * |
322 | | g_vfs_parse_name (GVfs *vfs, |
323 | | const char *parse_name) |
324 | 0 | { |
325 | 0 | GVfsClass *class; |
326 | 0 | GFile *ret; |
327 | |
|
328 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), NULL); |
329 | 0 | g_return_val_if_fail (parse_name != NULL, NULL); |
330 | | |
331 | 0 | class = G_VFS_GET_CLASS (vfs); |
332 | |
|
333 | 0 | ret = parse_name_internal (vfs, parse_name); |
334 | 0 | if (ret) |
335 | 0 | return ret; |
336 | | |
337 | 0 | return (* class->parse_name) (vfs, parse_name); |
338 | 0 | } |
339 | | |
340 | | static GVfs *vfs_default_singleton = NULL; /* (owned) (atomic) */ |
341 | | |
342 | | /** |
343 | | * g_vfs_get_default: |
344 | | * |
345 | | * Gets the default #GVfs for the system. |
346 | | * |
347 | | * Returns: (not nullable) (transfer none): a #GVfs, which will be the local |
348 | | * file system #GVfs if no other implementation is available. |
349 | | */ |
350 | | GVfs * |
351 | | g_vfs_get_default (void) |
352 | 0 | { |
353 | 0 | if (GLIB_PRIVATE_CALL (g_check_setuid) ()) |
354 | 0 | return g_vfs_get_local (); |
355 | | |
356 | 0 | if (g_once_init_enter (&vfs_default_singleton)) |
357 | 0 | { |
358 | 0 | GVfs *singleton; |
359 | |
|
360 | 0 | singleton = _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME, |
361 | 0 | "GIO_USE_VFS", |
362 | 0 | (GIOModuleVerifyFunc) g_vfs_is_active); |
363 | |
|
364 | 0 | g_once_init_leave (&vfs_default_singleton, singleton); |
365 | 0 | } |
366 | |
|
367 | 0 | return vfs_default_singleton; |
368 | 0 | } |
369 | | |
370 | | /** |
371 | | * g_vfs_get_local: |
372 | | * |
373 | | * Gets the local #GVfs for the system. |
374 | | * |
375 | | * Returns: (transfer none): a #GVfs. |
376 | | */ |
377 | | GVfs * |
378 | | g_vfs_get_local (void) |
379 | 0 | { |
380 | 0 | static gsize vfs = 0; |
381 | |
|
382 | 0 | if (g_once_init_enter (&vfs)) |
383 | 0 | g_once_init_leave (&vfs, (gsize)_g_local_vfs_new ()); |
384 | |
|
385 | 0 | return G_VFS (vfs); |
386 | 0 | } |
387 | | |
388 | | /** |
389 | | * g_vfs_register_uri_scheme: |
390 | | * @vfs: a #GVfs |
391 | | * @scheme: an URI scheme, e.g. "http" |
392 | | * @uri_func: (scope notified) (nullable): a #GVfsFileLookupFunc |
393 | | * @uri_data: (nullable): custom data passed to be passed to @uri_func, or %NULL |
394 | | * @uri_destroy: (nullable): function to be called when unregistering the |
395 | | * URI scheme, or when @vfs is disposed, to free the resources used |
396 | | * by the URI lookup function |
397 | | * @parse_name_func: (scope notified) (nullable): a #GVfsFileLookupFunc |
398 | | * @parse_name_data: (nullable): custom data passed to be passed to |
399 | | * @parse_name_func, or %NULL |
400 | | * @parse_name_destroy: (nullable): function to be called when unregistering the |
401 | | * URI scheme, or when @vfs is disposed, to free the resources used |
402 | | * by the parse name lookup function |
403 | | * |
404 | | * Registers @uri_func and @parse_name_func as the #GFile URI and parse name |
405 | | * lookup functions for URIs with a scheme matching @scheme. |
406 | | * Note that @scheme is registered only within the running application, as |
407 | | * opposed to desktop-wide as it happens with GVfs backends. |
408 | | * |
409 | | * When a #GFile is requested with an URI containing @scheme (e.g. through |
410 | | * g_file_new_for_uri()), @uri_func will be called to allow a custom |
411 | | * constructor. The implementation of @uri_func should not be blocking, and |
412 | | * must not call g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme(). |
413 | | * |
414 | | * When g_file_parse_name() is called with a parse name obtained from such file, |
415 | | * @parse_name_func will be called to allow the #GFile to be created again. In |
416 | | * that case, it's responsibility of @parse_name_func to make sure the parse |
417 | | * name matches what the custom #GFile implementation returned when |
418 | | * g_file_get_parse_name() was previously called. The implementation of |
419 | | * @parse_name_func should not be blocking, and must not call |
420 | | * g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme(). |
421 | | * |
422 | | * It's an error to call this function twice with the same scheme. To unregister |
423 | | * a custom URI scheme, use g_vfs_unregister_uri_scheme(). |
424 | | * |
425 | | * Returns: %TRUE if @scheme was successfully registered, or %FALSE if a handler |
426 | | * for @scheme already exists. |
427 | | * |
428 | | * Since: 2.50 |
429 | | */ |
430 | | gboolean |
431 | | g_vfs_register_uri_scheme (GVfs *vfs, |
432 | | const char *scheme, |
433 | | GVfsFileLookupFunc uri_func, |
434 | | gpointer uri_data, |
435 | | GDestroyNotify uri_destroy, |
436 | | GVfsFileLookupFunc parse_name_func, |
437 | | gpointer parse_name_data, |
438 | | GDestroyNotify parse_name_destroy) |
439 | 0 | { |
440 | 0 | GVfsPrivate *priv; |
441 | 0 | GVfsURISchemeData *closure; |
442 | |
|
443 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), FALSE); |
444 | 0 | g_return_val_if_fail (scheme != NULL, FALSE); |
445 | | |
446 | 0 | priv = g_vfs_get_instance_private (vfs); |
447 | |
|
448 | 0 | g_rw_lock_reader_lock (&additional_schemes_lock); |
449 | 0 | closure = g_hash_table_lookup (priv->additional_schemes, scheme); |
450 | 0 | g_rw_lock_reader_unlock (&additional_schemes_lock); |
451 | |
|
452 | 0 | if (closure != NULL) |
453 | 0 | return FALSE; |
454 | | |
455 | 0 | closure = g_new0 (GVfsURISchemeData, 1); |
456 | 0 | closure->uri_func = uri_func; |
457 | 0 | closure->uri_data = uri_data; |
458 | 0 | closure->uri_destroy = uri_destroy; |
459 | 0 | closure->parse_name_func = parse_name_func; |
460 | 0 | closure->parse_name_data = parse_name_data; |
461 | 0 | closure->parse_name_destroy = parse_name_destroy; |
462 | |
|
463 | 0 | g_rw_lock_writer_lock (&additional_schemes_lock); |
464 | 0 | g_hash_table_insert (priv->additional_schemes, g_strdup (scheme), closure); |
465 | 0 | g_rw_lock_writer_unlock (&additional_schemes_lock); |
466 | | |
467 | | /* Invalidate supported schemes */ |
468 | 0 | g_clear_pointer (&priv->supported_schemes, g_free); |
469 | |
|
470 | 0 | return TRUE; |
471 | 0 | } |
472 | | |
473 | | /** |
474 | | * g_vfs_unregister_uri_scheme: |
475 | | * @vfs: a #GVfs |
476 | | * @scheme: an URI scheme, e.g. "http" |
477 | | * |
478 | | * Unregisters the URI handler for @scheme previously registered with |
479 | | * g_vfs_register_uri_scheme(). |
480 | | * |
481 | | * Returns: %TRUE if @scheme was successfully unregistered, or %FALSE if a |
482 | | * handler for @scheme does not exist. |
483 | | * |
484 | | * Since: 2.50 |
485 | | */ |
486 | | gboolean |
487 | | g_vfs_unregister_uri_scheme (GVfs *vfs, |
488 | | const char *scheme) |
489 | 0 | { |
490 | 0 | GVfsPrivate *priv; |
491 | 0 | gboolean res; |
492 | |
|
493 | 0 | g_return_val_if_fail (G_IS_VFS (vfs), FALSE); |
494 | 0 | g_return_val_if_fail (scheme != NULL, FALSE); |
495 | | |
496 | 0 | priv = g_vfs_get_instance_private (vfs); |
497 | |
|
498 | 0 | g_rw_lock_writer_lock (&additional_schemes_lock); |
499 | 0 | res = g_hash_table_remove (priv->additional_schemes, scheme); |
500 | 0 | g_rw_lock_writer_unlock (&additional_schemes_lock); |
501 | |
|
502 | 0 | if (res) |
503 | 0 | { |
504 | | /* Invalidate supported schemes */ |
505 | 0 | g_clear_pointer (&priv->supported_schemes, g_free); |
506 | |
|
507 | 0 | return TRUE; |
508 | 0 | } |
509 | | |
510 | 0 | return FALSE; |
511 | 0 | } |