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