chain up unconditionally in finalize() and dispose(). Also don't
[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 <glocalfile.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include "glibintl.h"
32
33 #include "gioalias.h"
34
35 #define CHUNK_SIZE 1000
36
37   /* TODO:
38    *  It would be nice to use the dirent->d_type to check file type without
39    *  needing to stat each files on linux and other systems that support it.
40    *  (question: does that following symlink or not?)
41    */
42
43 #ifdef G_OS_WIN32
44 #define USE_GDIR
45 #endif
46
47 #ifndef USE_GDIR
48
49 #include <sys/types.h>
50 #include <dirent.h>
51 #include <errno.h>
52
53 typedef struct {
54   char *name;
55   long inode;
56 } DirEntry;
57
58 #endif
59
60 struct _GLocalFileEnumerator
61 {
62   GFileEnumerator parent;
63
64   GFileAttributeMatcher *matcher;
65   char *filename;
66   char *attributes;
67   GFileQueryInfoFlags flags;
68
69   gboolean got_parent_info;
70   GLocalParentFileInfo parent_info;
71   
72 #ifdef USE_GDIR
73   GDir *dir;
74 #else
75   DIR *dir;
76   DirEntry *entries;
77   int entries_pos;
78   gboolean at_end;
79 #endif
80   
81   gboolean follow_symlinks;
82 };
83
84 #define g_local_file_enumerator_get_type _g_local_file_enumerator_get_type
85 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
86
87 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
88                                                      GCancellable     *cancellable,
89                                                      GError          **error);
90 static gboolean   g_local_file_enumerator_close     (GFileEnumerator  *enumerator,
91                                                      GCancellable     *cancellable,
92                                                      GError          **error);
93
94
95 static void
96 free_entries (GLocalFileEnumerator *local)
97 {
98 #ifndef USE_GDIR
99   int i;
100
101   if (local->entries != NULL)
102     {
103       for (i = 0; local->entries[i].name != NULL; i++)
104         g_free (local->entries[i].name);
105       
106       g_free (local->entries);
107     }
108 #endif
109 }
110
111 static void
112 g_local_file_enumerator_finalize (GObject *object)
113 {
114   GLocalFileEnumerator *local;
115
116   local = G_LOCAL_FILE_ENUMERATOR (object);
117
118   g_free (local->filename);
119   g_file_attribute_matcher_unref (local->matcher);
120   if (local->dir)
121     {
122 #ifdef USE_GDIR
123       g_dir_close (local->dir);
124 #else
125       closedir (local->dir);
126 #endif      
127       local->dir = NULL;
128     }
129
130   free_entries (local);
131
132   G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize (object);
133 }
134
135
136 static void
137 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
138 {
139   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140   GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
141   
142   gobject_class->finalize = g_local_file_enumerator_finalize;
143
144   enumerator_class->next_file = g_local_file_enumerator_next_file;
145   enumerator_class->close_fn = g_local_file_enumerator_close;
146 }
147
148 static void
149 g_local_file_enumerator_init (GLocalFileEnumerator *local)
150 {
151 }
152
153 #ifdef USE_GDIR
154 static void
155 convert_file_to_io_error (GError **error,
156                           GError  *file_error)
157 {
158   int new_code;
159
160   if (file_error == NULL)
161     return;
162   
163   new_code = G_IO_ERROR_FAILED;
164   
165   if (file_error->domain == G_FILE_ERROR) 
166     {
167       switch (file_error->code) 
168         {
169         case G_FILE_ERROR_NOENT:
170           new_code = G_IO_ERROR_NOT_FOUND;
171           break;
172         case G_FILE_ERROR_ACCES:
173           new_code = G_IO_ERROR_PERMISSION_DENIED;
174           break;
175         case G_FILE_ERROR_NOTDIR:
176           new_code = G_IO_ERROR_NOT_DIRECTORY;
177           break;
178         default:
179           break;
180         }
181     }
182   
183   g_set_error (error, G_IO_ERROR,
184                new_code,
185                "%s", file_error->message);
186 }
187 #endif
188
189 GFileEnumerator *
190 _g_local_file_enumerator_new (GLocalFile *file,
191                               const char           *attributes,
192                               GFileQueryInfoFlags   flags,
193                               GCancellable         *cancellable,
194                               GError              **error)
195 {
196   GLocalFileEnumerator *local;
197   char *filename;
198
199   filename = g_file_get_path (G_FILE (file));
200
201 #ifdef USE_GDIR
202   GError *dir_error;
203   GDir *dir;
204   
205   dir_error = NULL;
206   dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
207   if (dir == NULL) 
208     {
209       if (error != NULL)
210         {
211           convert_file_to_io_error (error, dir_error);
212           g_error_free (dir_error);
213         }
214       g_free (filename);
215       return NULL;
216     }
217 #else
218   DIR *dir;
219   int errsv;
220
221   dir = opendir (filename);
222   if (dir == NULL)
223     {
224       errsv = errno;
225
226       g_set_error (error, G_IO_ERROR,
227                    g_io_error_from_errno (errsv),
228                    "%s", g_strerror (errsv));
229       g_free (filename);
230       return NULL;
231     }
232
233 #endif
234   
235   local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
236                         "container", file,
237                         NULL);
238
239   local->dir = dir;
240   local->filename = filename;
241   local->matcher = g_file_attribute_matcher_new (attributes);
242   local->flags = flags;
243   
244   return G_FILE_ENUMERATOR (local);
245 }
246
247 #ifndef USE_GDIR
248 static int
249 sort_by_inode (const void *_a, const void *_b)
250 {
251   const DirEntry *a, *b;
252
253   a = _a;
254   b = _b;
255   return a->inode - b->inode;
256 }
257
258 static const char *
259 next_file_helper (GLocalFileEnumerator *local)
260 {
261   struct dirent *entry;
262   const char *filename;
263   int i;
264
265   if (local->at_end)
266     return NULL;
267   
268   if (local->entries == NULL ||
269       (local->entries[local->entries_pos].name == NULL))
270     {
271       if (local->entries == NULL)
272         local->entries = g_new (DirEntry, CHUNK_SIZE + 1);
273       else
274         {
275           /* Restart by clearing old names */
276           for (i = 0; local->entries[i].name != NULL; i++)
277             g_free (local->entries[i].name);
278         }
279       
280       for (i = 0; i < CHUNK_SIZE; i++)
281         {
282           entry = readdir (local->dir);
283           while (entry 
284                  && (0 == strcmp (entry->d_name, ".") ||
285                      0 == strcmp (entry->d_name, "..")))
286             entry = readdir (local->dir);
287
288           if (entry)
289             {
290               local->entries[i].name = g_strdup (entry->d_name);
291               local->entries[i].inode = entry->d_ino;
292             }
293           else
294             break;
295         }
296       local->entries[i].name = NULL;
297       local->entries_pos = 0;
298       
299       qsort (local->entries, i, sizeof (DirEntry), sort_by_inode);
300     }
301
302   filename = local->entries[local->entries_pos++].name;
303   if (filename == NULL)
304     local->at_end = TRUE;
305     
306   return filename;
307 }
308
309 #endif
310
311 static GFileInfo *
312 g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
313                                    GCancellable     *cancellable,
314                                    GError          **error)
315 {
316   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
317   const char *filename;
318   char *path;
319   GFileInfo *info;
320   GError *my_error;
321
322   if (!local->got_parent_info)
323     {
324       _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
325       local->got_parent_info = TRUE;
326     }
327   
328  next_file:
329
330 #ifdef USE_GDIR
331   filename = g_dir_read_name (local->dir);
332 #else
333   filename = next_file_helper (local);
334 #endif
335
336   if (filename == NULL)
337     return NULL;
338
339   my_error = NULL;
340   path = g_build_filename (local->filename, filename, NULL);
341   info = _g_local_file_info_get (filename, path,
342                                  local->matcher,
343                                  local->flags,
344                                  &local->parent_info,
345                                  &my_error); 
346   g_free (path);
347
348   if (info == NULL)
349     {
350       /* Failed to get info */
351       /* If the file does not exist there might have been a race where
352        * the file was removed between the readdir and the stat, so we
353        * ignore the file. */
354       if (my_error->domain == G_IO_ERROR &&
355           my_error->code == G_IO_ERROR_NOT_FOUND)
356         {
357           g_error_free (my_error);
358           goto next_file;
359         }
360       else
361         g_propagate_error (error, my_error);
362     }
363
364   return info;
365 }
366
367 static gboolean
368 g_local_file_enumerator_close (GFileEnumerator  *enumerator,
369                                GCancellable     *cancellable,
370                                GError          **error)
371 {
372   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
373
374   if (local->dir)
375     {
376 #ifdef USE_GDIR
377       g_dir_close (local->dir);
378 #else
379       closedir (local->dir);
380 #endif
381       local->dir = NULL;
382     }
383
384   return TRUE;
385 }
386
387