1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "glib-private.h"
27 #include "glocalvfs.h"
28 #include "gresourcefile.h"
29 #include "giomodule-priv.h"
36 * Entry point for using GIO functionality.
39 static GRWLock additional_schemes_lock;
41 typedef struct _GVfsPrivate {
42 GHashTable *additional_schemes;
43 char const **supported_schemes;
47 GVfsFileLookupFunc uri_func;
49 GDestroyNotify uri_destroy;
51 GVfsFileLookupFunc parse_name_func;
52 gpointer parse_name_data;
53 GDestroyNotify parse_name_destroy;
56 G_DEFINE_TYPE_WITH_PRIVATE (GVfs, g_vfs, G_TYPE_OBJECT)
59 g_vfs_dispose (GObject *object)
61 GVfs *vfs = G_VFS (object);
62 GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
64 g_clear_pointer (&priv->additional_schemes, g_hash_table_destroy);
65 g_clear_pointer (&priv->supported_schemes, g_free);
67 G_OBJECT_CLASS (g_vfs_parent_class)->dispose (object);
71 g_vfs_class_init (GVfsClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 object_class->dispose = g_vfs_dispose;
78 resource_parse_name (GVfs *vfs,
79 const char *parse_name,
82 if (g_str_has_prefix (parse_name, "resource:"))
83 return _g_resource_file_new (parse_name);
89 resource_get_file_for_uri (GVfs *vfs,
93 return _g_resource_file_new (uri);
97 g_vfs_uri_lookup_func_closure_free (gpointer data)
99 GVfsURISchemeData *closure = data;
101 if (closure->uri_destroy)
102 closure->uri_destroy (closure->uri_data);
103 if (closure->parse_name_destroy)
104 closure->parse_name_destroy (closure->parse_name_data);
110 g_vfs_init (GVfs *vfs)
112 GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
113 priv->additional_schemes =
114 g_hash_table_new_full (g_str_hash, g_str_equal,
115 g_free, g_vfs_uri_lookup_func_closure_free);
117 g_vfs_register_uri_scheme (vfs, "resource",
118 resource_get_file_for_uri, NULL, NULL,
119 resource_parse_name, NULL, NULL);
126 * Checks if the VFS is active.
128 * Returns: %TRUE if construction of the @vfs was successful
129 * and it is now active.
132 g_vfs_is_active (GVfs *vfs)
136 g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
138 class = G_VFS_GET_CLASS (vfs);
140 return (* class->is_active) (vfs);
145 * g_vfs_get_file_for_path:
147 * @path: a string containing a VFS path.
149 * Gets a #GFile for @path.
151 * Returns: (transfer full): a #GFile.
152 * Free the returned object with g_object_unref().
155 g_vfs_get_file_for_path (GVfs *vfs,
160 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
161 g_return_val_if_fail (path != NULL, NULL);
163 class = G_VFS_GET_CLASS (vfs);
165 return (* class->get_file_for_path) (vfs, path);
169 parse_name_internal (GVfs *vfs,
170 const char *parse_name)
172 GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
174 GVfsURISchemeData *closure;
177 g_rw_lock_reader_lock (&additional_schemes_lock);
178 g_hash_table_iter_init (&iter, priv->additional_schemes);
180 while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &closure))
182 ret = closure->parse_name_func (vfs, parse_name,
183 closure->parse_name_data);
189 g_rw_lock_reader_unlock (&additional_schemes_lock);
195 get_file_for_uri_internal (GVfs *vfs,
198 GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
201 GVfsURISchemeData *closure;
203 scheme = g_uri_parse_scheme (uri);
207 g_rw_lock_reader_lock (&additional_schemes_lock);
208 closure = g_hash_table_lookup (priv->additional_schemes, scheme);
211 ret = closure->uri_func (vfs, uri, closure->uri_data);
213 g_rw_lock_reader_unlock (&additional_schemes_lock);
220 * g_vfs_get_file_for_uri:
222 * @uri: a string containing a URI
224 * Gets a #GFile for @uri.
226 * This operation never fails, but the returned object
227 * might not support any I/O operation if the URI
228 * is malformed or if the URI scheme is not supported.
230 * Returns: (transfer full): a #GFile.
231 * Free the returned object with g_object_unref().
234 g_vfs_get_file_for_uri (GVfs *vfs,
240 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
241 g_return_val_if_fail (uri != NULL, NULL);
243 class = G_VFS_GET_CLASS (vfs);
245 ret = get_file_for_uri_internal (vfs, uri);
247 ret = (* class->get_file_for_uri) (vfs, uri);
249 g_assert (ret != NULL);
251 return g_steal_pointer (&ret);
255 * g_vfs_get_supported_uri_schemes:
258 * Gets a list of URI schemes supported by @vfs.
260 * Returns: (transfer none): a %NULL-terminated array of strings.
261 * The returned array belongs to GIO and must
262 * not be freed or modified.
264 const gchar * const *
265 g_vfs_get_supported_uri_schemes (GVfs *vfs)
269 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
271 priv = g_vfs_get_instance_private (vfs);
273 if (!priv->supported_schemes)
276 const char * const *default_schemes;
277 const char *additional_scheme;
278 GPtrArray *supported_schemes;
281 class = G_VFS_GET_CLASS (vfs);
283 default_schemes = (* class->get_supported_uri_schemes) (vfs);
284 supported_schemes = g_ptr_array_new ();
286 for (; default_schemes && *default_schemes; default_schemes++)
287 g_ptr_array_add (supported_schemes, (gpointer) *default_schemes);
289 g_rw_lock_reader_lock (&additional_schemes_lock);
290 g_hash_table_iter_init (&iter, priv->additional_schemes);
292 while (g_hash_table_iter_next
293 (&iter, (gpointer *) &additional_scheme, NULL))
294 g_ptr_array_add (supported_schemes, (gpointer) additional_scheme);
296 g_rw_lock_reader_unlock (&additional_schemes_lock);
298 g_ptr_array_add (supported_schemes, NULL);
300 g_free (priv->supported_schemes);
301 priv->supported_schemes =
302 (char const **) g_ptr_array_free (supported_schemes, FALSE);
305 return priv->supported_schemes;
311 * @parse_name: a string to be parsed by the VFS module.
313 * This operation never fails, but the returned object might
314 * not support any I/O operations if the @parse_name cannot
315 * be parsed by the #GVfs module.
317 * Returns: (transfer full): a #GFile for the given @parse_name.
318 * Free the returned object with g_object_unref().
321 g_vfs_parse_name (GVfs *vfs,
322 const char *parse_name)
327 g_return_val_if_fail (G_IS_VFS (vfs), NULL);
328 g_return_val_if_fail (parse_name != NULL, NULL);
330 class = G_VFS_GET_CLASS (vfs);
332 ret = parse_name_internal (vfs, parse_name);
336 return (* class->parse_name) (vfs, parse_name);
339 static GVfs *vfs_default_singleton = NULL; /* (owned) (atomic) */
344 * Gets the default #GVfs for the system.
346 * Returns: (not nullable) (transfer none): a #GVfs, which will be the local
347 * file system #GVfs if no other implementation is available.
350 g_vfs_get_default (void)
352 if (GLIB_PRIVATE_CALL (g_check_setuid) ())
353 return g_vfs_get_local ();
355 if (g_once_init_enter_pointer (&vfs_default_singleton))
359 singleton = _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME,
361 (GIOModuleVerifyFunc) g_vfs_is_active);
363 g_once_init_leave_pointer (&vfs_default_singleton, singleton);
366 return vfs_default_singleton;
372 * Gets the local #GVfs for the system.
374 * Returns: (transfer none): a #GVfs.
377 g_vfs_get_local (void)
379 static GVfs *vfs = 0;
381 if (g_once_init_enter_pointer (&vfs))
382 g_once_init_leave_pointer (&vfs, _g_local_vfs_new ());
388 * g_vfs_register_uri_scheme:
390 * @scheme: an URI scheme, e.g. "http"
391 * @uri_func: (scope notified) (nullable): a #GVfsFileLookupFunc
392 * @uri_data: (nullable): custom data passed to be passed to @uri_func, or %NULL
393 * @uri_destroy: (nullable): function to be called when unregistering the
394 * URI scheme, or when @vfs is disposed, to free the resources used
395 * by the URI lookup function
396 * @parse_name_func: (scope notified) (nullable): a #GVfsFileLookupFunc
397 * @parse_name_data: (nullable): custom data passed to be passed to
398 * @parse_name_func, or %NULL
399 * @parse_name_destroy: (nullable): function to be called when unregistering the
400 * URI scheme, or when @vfs is disposed, to free the resources used
401 * by the parse name lookup function
403 * Registers @uri_func and @parse_name_func as the #GFile URI and parse name
404 * lookup functions for URIs with a scheme matching @scheme.
405 * Note that @scheme is registered only within the running application, as
406 * opposed to desktop-wide as it happens with GVfs backends.
408 * When a #GFile is requested with an URI containing @scheme (e.g. through
409 * g_file_new_for_uri()), @uri_func will be called to allow a custom
410 * constructor. The implementation of @uri_func should not be blocking, and
411 * must not call g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme().
413 * When g_file_parse_name() is called with a parse name obtained from such file,
414 * @parse_name_func will be called to allow the #GFile to be created again. In
415 * that case, it's responsibility of @parse_name_func to make sure the parse
416 * name matches what the custom #GFile implementation returned when
417 * g_file_get_parse_name() was previously called. The implementation of
418 * @parse_name_func should not be blocking, and must not call
419 * g_vfs_register_uri_scheme() or g_vfs_unregister_uri_scheme().
421 * It's an error to call this function twice with the same scheme. To unregister
422 * a custom URI scheme, use g_vfs_unregister_uri_scheme().
424 * Returns: %TRUE if @scheme was successfully registered, or %FALSE if a handler
425 * for @scheme already exists.
430 g_vfs_register_uri_scheme (GVfs *vfs,
432 GVfsFileLookupFunc uri_func,
434 GDestroyNotify uri_destroy,
435 GVfsFileLookupFunc parse_name_func,
436 gpointer parse_name_data,
437 GDestroyNotify parse_name_destroy)
440 GVfsURISchemeData *closure;
442 g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
443 g_return_val_if_fail (scheme != NULL, FALSE);
445 priv = g_vfs_get_instance_private (vfs);
447 g_rw_lock_reader_lock (&additional_schemes_lock);
448 closure = g_hash_table_lookup (priv->additional_schemes, scheme);
449 g_rw_lock_reader_unlock (&additional_schemes_lock);
454 closure = g_new0 (GVfsURISchemeData, 1);
455 closure->uri_func = uri_func;
456 closure->uri_data = uri_data;
457 closure->uri_destroy = uri_destroy;
458 closure->parse_name_func = parse_name_func;
459 closure->parse_name_data = parse_name_data;
460 closure->parse_name_destroy = parse_name_destroy;
462 g_rw_lock_writer_lock (&additional_schemes_lock);
463 g_hash_table_insert (priv->additional_schemes, g_strdup (scheme), closure);
464 g_rw_lock_writer_unlock (&additional_schemes_lock);
466 /* Invalidate supported schemes */
467 g_clear_pointer (&priv->supported_schemes, g_free);
473 * g_vfs_unregister_uri_scheme:
475 * @scheme: an URI scheme, e.g. "http"
477 * Unregisters the URI handler for @scheme previously registered with
478 * g_vfs_register_uri_scheme().
480 * Returns: %TRUE if @scheme was successfully unregistered, or %FALSE if a
481 * handler for @scheme does not exist.
486 g_vfs_unregister_uri_scheme (GVfs *vfs,
492 g_return_val_if_fail (G_IS_VFS (vfs), FALSE);
493 g_return_val_if_fail (scheme != NULL, FALSE);
495 priv = g_vfs_get_instance_private (vfs);
497 g_rw_lock_writer_lock (&additional_schemes_lock);
498 res = g_hash_table_remove (priv->additional_schemes, scheme);
499 g_rw_lock_writer_unlock (&additional_schemes_lock);
503 /* Invalidate supported schemes */
504 g_clear_pointer (&priv->supported_schemes, g_free);