Bug 536252 – GFileEnumerator should allow access to the containing GFile
[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   if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
133     (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
134 }
135
136
137 static void
138 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
139 {
140   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141   GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
142   
143   gobject_class->finalize = g_local_file_enumerator_finalize;
144
145   enumerator_class->next_file = g_local_file_enumerator_next_file;
146   enumerator_class->close_fn = g_local_file_enumerator_close;
147 }
148
149 static void
150 g_local_file_enumerator_init (GLocalFileEnumerator *local)
151 {
152 }
153
154 #ifdef USE_GDIR
155 static void
156 convert_file_to_io_error (GError **error,
157                           GError  *file_error)
158 {
159   int new_code;
160
161   if (file_error == NULL)
162     return;
163   
164   new_code = G_IO_ERROR_FAILED;
165   
166   if (file_error->domain == G_FILE_ERROR) 
167     {
168       switch (file_error->code) 
169         {
170         case G_FILE_ERROR_NOENT:
171           new_code = G_IO_ERROR_NOT_FOUND;
172           break;
173         case G_FILE_ERROR_ACCES:
174           new_code = G_IO_ERROR_PERMISSION_DENIED;
175           break;
176         case G_FILE_ERROR_NOTDIR:
177           new_code = G_IO_ERROR_NOT_DIRECTORY;
178           break;
179         default:
180           break;
181         }
182     }
183   
184   g_set_error (error, G_IO_ERROR,
185                new_code,
186                "%s", file_error->message);
187 }
188 #endif
189
190 GFileEnumerator *
191 _g_local_file_enumerator_new (GLocalFile *file,
192                               const char           *attributes,
193                               GFileQueryInfoFlags   flags,
194                               GCancellable         *cancellable,
195                               GError              **error)
196 {
197   GLocalFileEnumerator *local;
198   char *filename;
199
200   filename = g_file_get_path (G_FILE (file));
201
202 #ifdef USE_GDIR
203   GError *dir_error;
204   GDir *dir;
205   
206   dir_error = NULL;
207   dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
208   if (dir == NULL) 
209     {
210       if (error != NULL)
211         {
212           convert_file_to_io_error (error, dir_error);
213           g_error_free (dir_error);
214         }
215       g_free (filename);
216       return NULL;
217     }
218 #else
219   DIR *dir;
220   int errsv;
221
222   dir = opendir (filename);
223   if (dir == NULL)
224     {
225       errsv = errno;
226
227       g_set_error (error, G_IO_ERROR,
228                    g_io_error_from_errno (errsv),
229                    "%s", g_strerror (errsv));
230       g_free (filename);
231       return NULL;
232     }
233
234 #endif
235   
236   local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
237                         "container", file,
238                         NULL);
239
240   local->dir = dir;
241   local->filename = filename;
242   local->matcher = g_file_attribute_matcher_new (attributes);
243   local->flags = flags;
244   
245   return G_FILE_ENUMERATOR (local);
246 }
247
248 #ifndef USE_GDIR
249 static int
250 sort_by_inode (const void *_a, const void *_b)
251 {
252   const DirEntry *a, *b;
253
254   a = _a;
255   b = _b;
256   return a->inode - b->inode;
257 }
258
259 static const char *
260 next_file_helper (GLocalFileEnumerator *local)
261 {
262   struct dirent *entry;
263   const char *filename;
264   int i;
265
266   if (local->at_end)
267     return NULL;
268   
269   if (local->entries == NULL ||
270       (local->entries[local->entries_pos].name == NULL))
271     {
272       if (local->entries == NULL)
273         local->entries = g_new (DirEntry, CHUNK_SIZE + 1);
274       else
275         {
276           /* Restart by clearing old names */
277           for (i = 0; local->entries[i].name != NULL; i++)
278             g_free (local->entries[i].name);
279         }
280       
281       for (i = 0; i < CHUNK_SIZE; i++)
282         {
283           entry = readdir (local->dir);
284           while (entry 
285                  && (0 == strcmp (entry->d_name, ".") ||
286                      0 == strcmp (entry->d_name, "..")))
287             entry = readdir (local->dir);
288
289           if (entry)
290             {
291               local->entries[i].name = g_strdup (entry->d_name);
292               local->entries[i].inode = entry->d_ino;
293             }
294           else
295             break;
296         }
297       local->entries[i].name = NULL;
298       local->entries_pos = 0;
299       
300       qsort (local->entries, i, sizeof (DirEntry), sort_by_inode);
301     }
302
303   filename = local->entries[local->entries_pos++].name;
304   if (filename == NULL)
305     local->at_end = TRUE;
306     
307   return filename;
308 }
309
310 #endif
311
312 static GFileInfo *
313 g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
314                                    GCancellable     *cancellable,
315                                    GError          **error)
316 {
317   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
318   const char *filename;
319   char *path;
320   GFileInfo *info;
321   GError *my_error;
322
323   if (!local->got_parent_info)
324     {
325       _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
326       local->got_parent_info = TRUE;
327     }
328   
329  next_file:
330
331 #ifdef USE_GDIR
332   filename = g_dir_read_name (local->dir);
333 #else
334   filename = next_file_helper (local);
335 #endif
336
337   if (filename == NULL)
338     return NULL;
339
340   my_error = NULL;
341   path = g_build_filename (local->filename, filename, NULL);
342   info = _g_local_file_info_get (filename, path,
343                                  local->matcher,
344                                  local->flags,
345                                  &local->parent_info,
346                                  &my_error); 
347   g_free (path);
348
349   if (info == NULL)
350     {
351       /* Failed to get info */
352       /* If the file does not exist there might have been a race where
353        * the file was removed between the readdir and the stat, so we
354        * ignore the file. */
355       if (my_error->domain == G_IO_ERROR &&
356           my_error->code == G_IO_ERROR_NOT_FOUND)
357         {
358           g_error_free (my_error);
359           goto next_file;
360         }
361       else
362         g_propagate_error (error, my_error);
363     }
364
365   return info;
366 }
367
368 static gboolean
369 g_local_file_enumerator_close (GFileEnumerator  *enumerator,
370                                GCancellable     *cancellable,
371                                GError          **error)
372 {
373   GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
374
375   if (local->dir)
376     {
377 #ifdef USE_GDIR
378       g_dir_close (local->dir);
379 #else
380       closedir (local->dir);
381 #endif
382       local->dir = NULL;
383     }
384
385   return TRUE;
386 }
387
388