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