2.15.4
[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, 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 <gio/gdummyfile.h>
28 #include <sys/types.h>
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32
33 #include "gioalias.h"
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_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
49                                                          g_define_type_id,
50                                                          "local",
51                                                          0))
52 static void
53 g_local_vfs_finalize (GObject *object)
54 {
55   /* must chain up */
56   G_OBJECT_CLASS (g_local_vfs_parent_class)->finalize (object);
57 }
58
59 static void
60 g_local_vfs_init (GLocalVfs *vfs)
61 {
62 }
63
64 /**
65  * g_local_vfs_new:
66  *
67  * Returns a new #GVfs handle for a local vfs.
68  *
69  * Returns: a new #GVfs handle.
70  **/
71 GVfs *
72 _g_local_vfs_new (void)
73 {
74   return g_object_new (G_TYPE_LOCAL_VFS, NULL);
75 }
76
77 static GFile *
78 g_local_vfs_get_file_for_path (GVfs       *vfs,
79                                const char *path)
80 {
81   return _g_local_file_new (path);
82 }
83
84 static GFile *
85 g_local_vfs_get_file_for_uri (GVfs       *vfs,
86                               const char *uri)
87 {
88   char *path;
89   GFile *file;
90
91   path = g_filename_from_uri (uri, NULL, NULL);
92
93   if (path != NULL)
94     file = _g_local_file_new (path);
95   else
96     file = _g_dummy_file_new (uri);
97
98   g_free (path);
99
100   return file;
101 }
102
103 static const gchar * const *
104 g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
105 {
106   static const gchar * uri_schemes[] = { "file", NULL };
107
108   return uri_schemes;
109 }
110
111 static GFile *
112 g_local_vfs_parse_name (GVfs       *vfs,
113                         const char *parse_name)
114 {
115   GFile *file;
116   char *filename;
117   char *user_name;
118   char *user_prefix;
119   const char *user_start, *user_end;
120   char *rest;
121   struct passwd *passwd_file_entry;
122   
123   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
124   g_return_val_if_fail (parse_name != NULL, NULL);
125
126   if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
127     filename = g_filename_from_uri (parse_name, NULL, NULL);
128   else
129     {
130       if (*parse_name == '~')
131         {
132           parse_name ++;
133           user_start = parse_name;
134           
135           while (*parse_name != 0 && *parse_name != '/')
136             parse_name++;
137           
138           user_end = parse_name;
139
140           if (user_end == user_start)
141             user_prefix = g_strdup (g_get_home_dir ());
142           else
143             {
144 #ifdef HAVE_PWD_H
145               user_name = g_strndup (user_start, user_end - user_start);
146               passwd_file_entry = getpwnam (user_name);
147               g_free (user_name);
148               
149               if (passwd_file_entry != NULL &&
150                   passwd_file_entry->pw_dir != NULL)
151                 user_prefix = g_strdup (passwd_file_entry->pw_dir);
152               else
153 #endif
154                 user_prefix = g_strdup (g_get_home_dir ());
155             }
156
157           rest = NULL;
158           if (*user_end != 0)
159             rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
160           
161           filename = g_build_filename (user_prefix, rest, NULL);
162           g_free (rest);
163           g_free (user_prefix);
164         }
165       else
166         filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
167     }
168   
169   if (filename == NULL)
170     filename = g_strdup (parse_name);
171     
172   file = _g_local_file_new (filename);
173   g_free (filename);
174
175   return file;
176 }
177
178 static gboolean
179 g_local_vfs_is_active (GVfs *vfs)
180 {
181   return TRUE;
182 }
183
184 static void
185 g_local_vfs_class_init (GLocalVfsClass *class)
186 {
187   GObjectClass *object_class;
188   GVfsClass *vfs_class;
189   
190   object_class = (GObjectClass *) class;
191
192   object_class->finalize = g_local_vfs_finalize;
193
194   vfs_class = G_VFS_CLASS (class);
195
196   vfs_class->is_active = g_local_vfs_is_active;
197   vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
198   vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;
199   vfs_class->get_supported_uri_schemes = g_local_vfs_get_supported_uri_schemes;
200   vfs_class->parse_name = g_local_vfs_parse_name;
201 }