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