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