Coverage Report

Created: 2025-06-13 06:55

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