More coding style fixes
[platform/upstream/glib.git] / gio / glocalfileenumerator.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
25 #include <glib.h>
26 #include <glocalfileenumerator.h>
27 #include <glocalfileinfo.h>
28 #include "glibintl.h"
29
30 #include "gioalias.h"
31
32   /* TODO:
33    *  It would be nice to use the dirent->d_type to check file type without
34    *  needing to stat each files on linux and other systems that support it.
35    *  (question: does that following symlink or not?)
36    */
37   
38
39 struct _GLocalFileEnumerator
40 {
41   GFileEnumerator parent;
42
43   GFileAttributeMatcher *matcher;
44   GDir *dir;
45   char *filename;
46   char *attributes;
47   GFileQueryInfoFlags flags;
48
49   gboolean got_parent_info;
50   GLocalParentFileInfo parent_info;
51   
52   gboolean follow_symlinks;
53 };
54
55 #define g_local_file_enumerator_get_type _g_local_file_enumerator_get_type
56 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
57
58 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
59                                                      GCancellable     *cancellable,
60                                                      GError          **error);
61 static gboolean   g_local_file_enumerator_close     (GFileEnumerator  *enumerator,
62                                                      GCancellable     *cancellable,
63                                                      GError          **error);
64
65
66 static void
67 g_local_file_enumerator_finalize (GObject *object)
68 {
69   GLocalFileEnumerator *local;
70
71   local = G_LOCAL_FILE_ENUMERATOR (object);
72
73   g_free (local->filename);
74   g_file_attribute_matcher_unref (local->matcher);
75   if (local->dir)
76     {
77       g_dir_close (local->dir);
78       local->dir = NULL;
79     }
80   
81   if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
82     (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
83 }
84
85
86 static void
87 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
88 {
89   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90   GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
91   
92   gobject_class->finalize = g_local_file_enumerator_finalize;
93
94   enumerator_class->next_file = g_local_file_enumerator_next_file;
95   enumerator_class->close = g_local_file_enumerator_close;
96 }
97
98 static void
99 g_local_file_enumerator_init (GLocalFileEnumerator *local)
100 {
101 }
102
103 static void
104 convert_file_to_io_error (GError **error,
105                           GError  *file_error)
106 {
107   int new_code;
108
109   if (file_error == NULL)
110     return;
111   
112   new_code = G_IO_ERROR_FAILED;
113   
114   if (file_error->domain == G_FILE_ERROR) 
115     {
116       switch (file_error->code) 
117         {
118         case G_FILE_ERROR_NOENT:
119           new_code = G_IO_ERROR_NOT_FOUND;
120           break;
121         case G_FILE_ERROR_ACCES:
122           new_code = G_IO_ERROR_PERMISSION_DENIED;
123           break;
124         case G_FILE_ERROR_NOTDIR:
125           new_code = G_IO_ERROR_NOT_DIRECTORY;
126           break;
127         default:
128           break;
129         }
130     }
131   
132   g_set_error (error, G_IO_ERROR,
133                new_code,
134                "%s", file_error->message);
135 }
136
137 GFileEnumerator *
138 _g_local_file_enumerator_new (const char           *filename,
139                               const char           *attributes,
140                               GFileQueryInfoFlags   flags,
141                               GCancellable         *cancellable,
142                               GError              **error)
143 {
144   GLocalFileEnumerator *local;
145   GDir *dir;
146   GError *dir_error;
147
148   dir_error = NULL;
149   dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
150   if (dir == NULL) 
151     {
152       convert_file_to_io_error (error, dir_error);
153       g_error_free (dir_error);
154       return NULL;
155     }
156   
157   local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);
158
159   local->dir = dir;
160   local->filename = g_strdup (filename);
161   local->matcher = g_file_attribute_matcher_new (attributes);
162   local->flags = flags;
163   
164   return G_FILE_ENUMERATOR (local);
165 }
166
167 static GFileInfo *
168 g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
169                                    GCancellable     *cancellable,
170                                    GError          **error)
171 {
172   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
173   const char *filename;
174   char *path;
175   GFileInfo *info;
176   GError *my_error = NULL;
177
178   if (!local->got_parent_info)
179     {
180       _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
181       local->got_parent_info = TRUE;
182     }
183   
184  next_file:
185   
186   filename = g_dir_read_name (local->dir);
187   if (filename == NULL)
188     return NULL;
189
190   path = g_build_filename (local->filename, filename, NULL);
191   info = _g_local_file_info_get (filename, path,
192                                  local->matcher,
193                                  local->flags,
194                                  &local->parent_info,
195                                  &my_error); 
196   g_free (path);
197   
198   if (info == NULL)
199     {
200       /* Failed to get info */
201       /* If the file does not exist there might have been a race where
202        * the file was removed between the readdir and the stat, so we
203        * ignore the file. */
204       if (my_error->domain == G_IO_ERROR &&
205           my_error->code == G_IO_ERROR_NOT_FOUND)
206         {
207           g_error_free (my_error);
208           goto next_file;
209         }
210       else
211         g_propagate_error (error, my_error);
212     }
213
214   return info;
215 }
216
217 static gboolean
218 g_local_file_enumerator_close (GFileEnumerator  *enumerator,
219                                GCancellable     *cancellable,
220                                GError          **error)
221 {
222   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
223
224   if (local->dir)
225     {
226       g_dir_close (local->dir);
227       local->dir = NULL;
228     }
229
230   return TRUE;
231 }
232
233