[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / glocalvfs.c
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 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 "glocalvfs.h"
23 #include "glocalfile.h"
24 #include "giomodule.h"
25 #include "giomodule-priv.h"
26 #include "gvfs.h"
27 #include <gio/gdummyfile.h>
28 #include <sys/types.h>
29 #ifdef G_OS_UNIX
30 #include <pwd.h>
31 #endif
32 #include <string.h>
33
34
35 struct _GLocalVfs
36 {
37   GVfs parent;
38 };
39
40 struct _GLocalVfsClass
41 {
42   GVfsClass parent_class;
43   
44 };
45
46 #define g_local_vfs_get_type _g_local_vfs_get_type
47 G_DEFINE_TYPE_WITH_CODE (GLocalVfs, g_local_vfs, G_TYPE_VFS,
48                          _g_io_modules_ensure_extension_points_registered ();
49                          g_io_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
50                                                          g_define_type_id,
51                                                          "local",
52                                                          0))
53 static void
54 g_local_vfs_finalize (GObject *object)
55 {
56   /* must chain up */
57   G_OBJECT_CLASS (g_local_vfs_parent_class)->finalize (object);
58 }
59
60 static void
61 g_local_vfs_init (GLocalVfs *vfs)
62 {
63 }
64
65 /**
66  * g_local_vfs_new:
67  *
68  * Returns a new #GVfs handle for a local vfs.
69  *
70  * Returns: a new #GVfs handle.
71  **/
72 GVfs *
73 _g_local_vfs_new (void)
74 {
75   return g_object_new (G_TYPE_LOCAL_VFS, NULL);
76 }
77
78 static GFile *
79 g_local_vfs_get_file_for_path (GVfs       *vfs,
80                                const char *path)
81 {
82   return _g_local_file_new (path);
83 }
84
85 static GFile *
86 g_local_vfs_get_file_for_uri (GVfs       *vfs,
87                               const char *uri)
88 {
89   char *path;
90   GFile *file;
91   char *stripped_uri, *hash;
92   
93   if (strchr (uri, '#') != NULL)
94     {
95       stripped_uri = g_strdup (uri);
96       hash = strchr (stripped_uri, '#');
97       *hash = 0;
98     }
99   else
100     stripped_uri = (char *)uri;
101       
102   path = g_filename_from_uri (stripped_uri, NULL, NULL);
103
104   if (stripped_uri != uri)
105     g_free (stripped_uri);
106   
107   if (path != NULL)
108     file = _g_local_file_new (path);
109   else
110     file = _g_dummy_file_new (uri);
111
112   g_free (path);
113
114   return file;
115 }
116
117 static const gchar * const *
118 g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
119 {
120   static const gchar * uri_schemes[] = { "file", NULL };
121
122   return uri_schemes;
123 }
124
125 static GFile *
126 g_local_vfs_parse_name (GVfs       *vfs,
127                         const char *parse_name)
128 {
129   GFile *file;
130   char *filename;
131   char *user_prefix;
132   const char *user_start, *user_end;
133   char *rest;
134   
135   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
136   g_return_val_if_fail (parse_name != NULL, NULL);
137
138   if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
139     filename = g_filename_from_uri (parse_name, NULL, NULL);
140   else
141     {
142       if (*parse_name == '~')
143         {
144           parse_name ++;
145           user_start = parse_name;
146           
147           while (*parse_name != 0 && *parse_name != '/')
148             parse_name++;
149           
150           user_end = parse_name;
151
152           if (user_end == user_start)
153             user_prefix = g_strdup (g_get_home_dir ());
154           else
155             {
156 #ifdef G_OS_UNIX
157               struct passwd *passwd_file_entry;
158               char *user_name;
159
160               user_name = g_strndup (user_start, user_end - user_start);
161               passwd_file_entry = getpwnam (user_name);
162               g_free (user_name);
163               
164               if (passwd_file_entry != NULL &&
165                   passwd_file_entry->pw_dir != NULL)
166                 user_prefix = g_strdup (passwd_file_entry->pw_dir);
167               else
168 #endif
169                 user_prefix = g_strdup (g_get_home_dir ());
170             }
171
172           rest = NULL;
173           if (*user_end != 0)
174             rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
175           
176           filename = g_build_filename (user_prefix, rest, NULL);
177           g_free (rest);
178           g_free (user_prefix);
179         }
180       else
181         filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
182     }
183   
184   if (filename == NULL)
185     filename = g_strdup (parse_name);
186     
187   file = _g_local_file_new (filename);
188   g_free (filename);
189
190   return file;
191 }
192
193 static gboolean
194 g_local_vfs_is_active (GVfs *vfs)
195 {
196   return TRUE;
197 }
198
199 static void
200 g_local_vfs_class_init (GLocalVfsClass *class)
201 {
202   GObjectClass *object_class;
203   GVfsClass *vfs_class;
204   
205   object_class = (GObjectClass *) class;
206
207   object_class->finalize = g_local_vfs_finalize;
208
209   vfs_class = G_VFS_CLASS (class);
210
211   vfs_class->is_active = g_local_vfs_is_active;
212   vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
213   vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;
214   vfs_class->get_supported_uri_schemes = g_local_vfs_get_supported_uri_schemes;
215   vfs_class->parse_name = g_local_vfs_parse_name;
216 }