gvfs.c 4.98 KB
Newer Older
1
/* GIO - GLib Input, Output and Streaming Library
Matthias Clasen's avatar
Matthias Clasen committed
2
 *
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23
#include <string.h>
#include "gvfs.h"
24
#include "glib-private.h"
25
#include "glocalvfs.h"
26
#include "gresourcefile.h"
Alexander Larsson's avatar
Alexander Larsson committed
27
#include "giomodule-priv.h"
28 29
#include "glibintl.h"

30

31 32
/**
 * SECTION:gvfs
Matthias Clasen's avatar
Matthias Clasen committed
33
 * @short_description: Virtual File System
Matthias Clasen's avatar
Matthias Clasen committed
34
 * @include: gio/gio.h
Matthias Clasen's avatar
Matthias Clasen committed
35
 *
36 37
 * Entry point for using GIO functionality.
 *
Matthias Clasen's avatar
Matthias Clasen committed
38
 */
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53
G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);

static void
g_vfs_class_init (GVfsClass *klass)
{
}

static void
g_vfs_init (GVfs *vfs)
{
}

/**
 * g_vfs_is_active:
54
 * @vfs: a #GVfs.
Matthias Clasen's avatar
Matthias Clasen committed
55
 *
56
 * Checks if the VFS is active.
Matthias Clasen's avatar
Matthias Clasen committed
57 58 59 60
 *
 * Returns: %TRUE if construction of the @vfs was successful
 *     and it is now active.
 */
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
gboolean
g_vfs_is_active (GVfs *vfs)
{
  GVfsClass *class;

  g_return_val_if_fail (G_IS_VFS (vfs), FALSE);

  class = G_VFS_GET_CLASS (vfs);

  return (* class->is_active) (vfs);
}


/**
 * g_vfs_get_file_for_path:
76
 * @vfs: a #GVfs.
77
 * @path: a string containing a VFS path.
Matthias Clasen's avatar
Matthias Clasen committed
78
 *
79
 * Gets a #GFile for @path.
Matthias Clasen's avatar
Matthias Clasen committed
80 81
 *
 * Returns: (transfer full): a #GFile.
Matthias Clasen's avatar
Matthias Clasen committed
82
 *     Free the returned object with g_object_unref().
Matthias Clasen's avatar
Matthias Clasen committed
83
 */
84
GFile *
85
g_vfs_get_file_for_path (GVfs       *vfs,
Matthias Clasen's avatar
Matthias Clasen committed
86
                         const char *path)
87 88
{
  GVfsClass *class;
Matthias Clasen's avatar
Matthias Clasen committed
89
 
90 91 92 93 94 95 96 97 98 99
  g_return_val_if_fail (G_IS_VFS (vfs), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  class = G_VFS_GET_CLASS (vfs);

  return (* class->get_file_for_path) (vfs, path);
}

/**
 * g_vfs_get_file_for_uri:
100
 * @vfs: a#GVfs.
Matthias Clasen's avatar
Matthias Clasen committed
101 102
 * @uri: a string containing a URI
 *
103
 * Gets a #GFile for @uri.
Matthias Clasen's avatar
Matthias Clasen committed
104
 *
105
 * This operation never fails, but the returned object
Matthias Clasen's avatar
Matthias Clasen committed
106
 * might not support any I/O operation if the URI
Matthias Clasen's avatar
Matthias Clasen committed
107
 * is malformed or if the URI scheme is not supported.
Matthias Clasen's avatar
Matthias Clasen committed
108 109
 *
 * Returns: (transfer full): a #GFile.
Matthias Clasen's avatar
Matthias Clasen committed
110
 *     Free the returned object with g_object_unref().
Matthias Clasen's avatar
Matthias Clasen committed
111
 */
112
GFile *
113
g_vfs_get_file_for_uri (GVfs       *vfs,
Matthias Clasen's avatar
Matthias Clasen committed
114
                        const char *uri)
115 116
{
  GVfsClass *class;
Matthias Clasen's avatar
Matthias Clasen committed
117
 
118 119 120 121 122
  g_return_val_if_fail (G_IS_VFS (vfs), NULL);
  g_return_val_if_fail (uri != NULL, NULL);

  class = G_VFS_GET_CLASS (vfs);

123
  /* This is an unfortunate placement, but we really
Matthias Clasen's avatar
Matthias Clasen committed
124 125 126 127
   * need to check this before chaining to the vfs,
   * because we want to support resource uris for
   * all vfs:es, even those that predate resources.
   */
128 129 130
  if (g_str_has_prefix (uri, "resource:"))
    return _g_resource_file_new (uri);

131 132 133 134 135
  return (* class->get_file_for_uri) (vfs, uri);
}

/**
 * g_vfs_get_supported_uri_schemes:
136
 * @vfs: a #GVfs.
Matthias Clasen's avatar
Matthias Clasen committed
137
 *
Matthias Clasen's avatar
Matthias Clasen committed
138
 * Gets a list of URI schemes supported by @vfs.
Matthias Clasen's avatar
Matthias Clasen committed
139
 *
140
 * Returns: (transfer none): a %NULL-terminated array of strings.
Matthias Clasen's avatar
Matthias Clasen committed
141
 *     The returned array belongs to GIO and must
Matthias Clasen's avatar
Matthias Clasen committed
142
 *     not be freed or modified.
Matthias Clasen's avatar
Matthias Clasen committed
143
 */
144 145 146 147
const gchar * const *
g_vfs_get_supported_uri_schemes (GVfs *vfs)
{
  GVfsClass *class;
Matthias Clasen's avatar
Matthias Clasen committed
148

149 150 151 152 153 154 155 156 157
  g_return_val_if_fail (G_IS_VFS (vfs), NULL);

  class = G_VFS_GET_CLASS (vfs);

  return (* class->get_supported_uri_schemes) (vfs);
}

/**
 * g_vfs_parse_name:
158
 * @vfs: a #GVfs.
159
 * @parse_name: a string to be parsed by the VFS module.
Matthias Clasen's avatar
Matthias Clasen committed
160 161 162
 *
 * This operation never fails, but the returned object might
 * not support any I/O operations if the @parse_name cannot
163
 * be parsed by the #GVfs module.
Matthias Clasen's avatar
Matthias Clasen committed
164
 *
165
 * Returns: (transfer full): a #GFile for the given @parse_name.
Matthias Clasen's avatar
Matthias Clasen committed
166
 *     Free the returned object with g_object_unref().
Matthias Clasen's avatar
Matthias Clasen committed
167
 */
168
GFile *
169
g_vfs_parse_name (GVfs       *vfs,
Matthias Clasen's avatar
Matthias Clasen committed
170
                  const char *parse_name)
171 172
{
  GVfsClass *class;
Matthias Clasen's avatar
Matthias Clasen committed
173

174 175 176 177 178
  g_return_val_if_fail (G_IS_VFS (vfs), NULL);
  g_return_val_if_fail (parse_name != NULL, NULL);

  class = G_VFS_GET_CLASS (vfs);

179 180 181
  if (g_str_has_prefix (parse_name, "resource:"))
    return _g_resource_file_new (parse_name);

182 183 184 185 186
  return (* class->parse_name) (vfs, parse_name);
}

/**
 * g_vfs_get_default:
Matthias Clasen's avatar
Matthias Clasen committed
187
 *
188
 * Gets the default #GVfs for the system.
Matthias Clasen's avatar
Matthias Clasen committed
189 190 191
 *
 * Returns: (transfer none): a #GVfs.
 */
192 193 194
GVfs *
g_vfs_get_default (void)
{
195 196
  if (GLIB_PRIVATE_CALL (g_check_setuid) ())
    return g_vfs_get_local ();
197
  return _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME,
Matthias Clasen's avatar
Matthias Clasen committed
198 199
                                   "GIO_USE_VFS",
                                   (GIOModuleVerifyFunc)g_vfs_is_active);
200 201 202 203
}

/**
 * g_vfs_get_local:
Matthias Clasen's avatar
Matthias Clasen committed
204
 *
205
 * Gets the local #GVfs for the system.
Matthias Clasen's avatar
Matthias Clasen committed
206
 *
207
 * Returns: (transfer none): a #GVfs.
Matthias Clasen's avatar
Matthias Clasen committed
208
 */
209 210 211 212 213 214
GVfs *
g_vfs_get_local (void)
{
  static gsize vfs = 0;

  if (g_once_init_enter (&vfs))
215
    g_once_init_leave (&vfs, (gsize)_g_local_vfs_new ());
216 217 218

  return G_VFS (vfs);
}