Improvde #include order consistency
[glib.git] / gio / glocalfileenumerator.c
blob5cba475e5b81d402ca4d2c8ce96e543b4d0e7835
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
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"
35 #define CHUNK_SIZE 1000
37 #ifdef G_OS_WIN32
38 #define USE_GDIR
39 #endif
41 #ifndef USE_GDIR
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <errno.h>
47 typedef struct {
48 char *name;
49 long inode;
50 GFileType type;
51 } DirEntry;
53 #endif
55 struct _GLocalFileEnumerator
57 GFileEnumerator parent;
59 GFileAttributeMatcher *matcher;
60 GFileAttributeMatcher *reduced_matcher;
61 char *filename;
62 char *attributes;
63 GFileQueryInfoFlags flags;
65 gboolean got_parent_info;
66 GLocalParentFileInfo parent_info;
68 #ifdef USE_GDIR
69 GDir *dir;
70 #else
71 DIR *dir;
72 DirEntry *entries;
73 int entries_pos;
74 gboolean at_end;
75 #endif
77 gboolean follow_symlinks;
80 #define g_local_file_enumerator_get_type _g_local_file_enumerator_get_type
81 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
83 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
84 GCancellable *cancellable,
85 GError **error);
86 static gboolean g_local_file_enumerator_close (GFileEnumerator *enumerator,
87 GCancellable *cancellable,
88 GError **error);
91 static void
92 free_entries (GLocalFileEnumerator *local)
94 #ifndef USE_GDIR
95 int i;
97 if (local->entries != NULL)
99 for (i = 0; local->entries[i].name != NULL; i++)
100 g_free (local->entries[i].name);
102 g_free (local->entries);
104 #endif
107 static void
108 g_local_file_enumerator_finalize (GObject *object)
110 GLocalFileEnumerator *local;
112 local = G_LOCAL_FILE_ENUMERATOR (object);
114 if (local->got_parent_info)
115 _g_local_file_info_free_parent_info (&local->parent_info);
116 g_free (local->filename);
117 g_file_attribute_matcher_unref (local->matcher);
118 g_file_attribute_matcher_unref (local->reduced_matcher);
119 if (local->dir)
121 #ifdef USE_GDIR
122 g_dir_close (local->dir);
123 #else
124 closedir (local->dir);
125 #endif
126 local->dir = NULL;
129 free_entries (local);
131 G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize (object);
135 static void
136 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
141 gobject_class->finalize = g_local_file_enumerator_finalize;
143 enumerator_class->next_file = g_local_file_enumerator_next_file;
144 enumerator_class->close_fn = g_local_file_enumerator_close;
147 static void
148 g_local_file_enumerator_init (GLocalFileEnumerator *local)
152 #ifdef USE_GDIR
153 static void
154 convert_file_to_io_error (GError **error,
155 GError *file_error)
157 int new_code;
159 if (file_error == NULL)
160 return;
162 new_code = G_IO_ERROR_FAILED;
164 if (file_error->domain == G_FILE_ERROR)
166 switch (file_error->code)
168 case G_FILE_ERROR_NOENT:
169 new_code = G_IO_ERROR_NOT_FOUND;
170 break;
171 case G_FILE_ERROR_ACCES:
172 new_code = G_IO_ERROR_PERMISSION_DENIED;
173 break;
174 case G_FILE_ERROR_NOTDIR:
175 new_code = G_IO_ERROR_NOT_DIRECTORY;
176 break;
177 case G_FILE_ERROR_MFILE:
178 new_code = G_IO_ERROR_TOO_MANY_OPEN_FILES;
179 break;
180 default:
181 break;
185 g_set_error_literal (error, G_IO_ERROR,
186 new_code,
187 file_error->message);
189 #else
190 static GFileAttributeMatcher *
191 g_file_attribute_matcher_subtract_attributes (GFileAttributeMatcher *matcher,
192 const char * attributes)
194 GFileAttributeMatcher *result, *tmp;
196 tmp = g_file_attribute_matcher_new (attributes);
197 result = g_file_attribute_matcher_subtract (matcher, tmp);
198 g_file_attribute_matcher_unref (tmp);
200 return result;
202 #endif
204 GFileEnumerator *
205 _g_local_file_enumerator_new (GLocalFile *file,
206 const char *attributes,
207 GFileQueryInfoFlags flags,
208 GCancellable *cancellable,
209 GError **error)
211 GLocalFileEnumerator *local;
212 char *filename = g_file_get_path (G_FILE (file));
214 #ifdef USE_GDIR
215 GError *dir_error;
216 GDir *dir;
218 dir_error = NULL;
219 dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
220 if (dir == NULL)
222 if (error != NULL)
224 convert_file_to_io_error (error, dir_error);
225 g_error_free (dir_error);
227 g_free (filename);
228 return NULL;
230 #else
231 DIR *dir;
232 int errsv;
234 dir = opendir (filename);
235 if (dir == NULL)
237 errsv = errno;
239 g_set_error_literal (error, G_IO_ERROR,
240 g_io_error_from_errno (errsv),
241 g_strerror (errsv));
242 g_free (filename);
243 return NULL;
246 #endif
248 local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
249 "container", file,
250 NULL);
252 local->dir = dir;
253 local->filename = filename;
254 local->matcher = g_file_attribute_matcher_new (attributes);
255 #ifndef USE_GDIR
256 local->reduced_matcher = g_file_attribute_matcher_subtract_attributes (local->matcher,
257 G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES","
258 "standard::type");
259 #endif
260 local->flags = flags;
262 return G_FILE_ENUMERATOR (local);
265 #ifndef USE_GDIR
266 static int
267 sort_by_inode (const void *_a, const void *_b)
269 const DirEntry *a, *b;
271 a = _a;
272 b = _b;
273 return a->inode - b->inode;
276 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
277 static GFileType
278 file_type_from_dirent (char d_type)
280 switch (d_type)
282 case DT_BLK:
283 case DT_CHR:
284 case DT_FIFO:
285 case DT_SOCK:
286 return G_FILE_TYPE_SPECIAL;
287 case DT_DIR:
288 return G_FILE_TYPE_DIRECTORY;
289 case DT_LNK:
290 return G_FILE_TYPE_SYMBOLIC_LINK;
291 case DT_REG:
292 return G_FILE_TYPE_REGULAR;
293 case DT_UNKNOWN:
294 default:
295 return G_FILE_TYPE_UNKNOWN;
298 #endif
300 static const char *
301 next_file_helper (GLocalFileEnumerator *local, GFileType *file_type)
303 struct dirent *entry;
304 const char *filename;
305 int i;
307 if (local->at_end)
308 return NULL;
310 if (local->entries == NULL ||
311 (local->entries[local->entries_pos].name == NULL))
313 if (local->entries == NULL)
314 local->entries = g_new (DirEntry, CHUNK_SIZE + 1);
315 else
317 /* Restart by clearing old names */
318 for (i = 0; local->entries[i].name != NULL; i++)
319 g_free (local->entries[i].name);
322 for (i = 0; i < CHUNK_SIZE; i++)
324 entry = readdir (local->dir);
325 while (entry
326 && (0 == strcmp (entry->d_name, ".") ||
327 0 == strcmp (entry->d_name, "..")))
328 entry = readdir (local->dir);
330 if (entry)
332 local->entries[i].name = g_strdup (entry->d_name);
333 local->entries[i].inode = entry->d_ino;
334 #if HAVE_STRUCT_DIRENT_D_TYPE
335 local->entries[i].type = file_type_from_dirent (entry->d_type);
336 #else
337 local->entries[i].type = G_FILE_TYPE_UNKNOWN;
338 #endif
340 else
341 break;
343 local->entries[i].name = NULL;
344 local->entries_pos = 0;
346 qsort (local->entries, i, sizeof (DirEntry), sort_by_inode);
349 filename = local->entries[local->entries_pos].name;
350 if (filename == NULL)
351 local->at_end = TRUE;
353 *file_type = local->entries[local->entries_pos].type;
355 local->entries_pos++;
357 return filename;
360 #endif
362 static GFileInfo *
363 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
364 GCancellable *cancellable,
365 GError **error)
367 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
368 const char *filename;
369 char *path;
370 GFileInfo *info;
371 GError *my_error;
372 GFileType file_type;
374 if (!local->got_parent_info)
376 _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
377 local->got_parent_info = TRUE;
380 next_file:
382 #ifdef USE_GDIR
383 filename = g_dir_read_name (local->dir);
384 file_type = G_FILE_TYPE_UNKNOWN;
385 #else
386 filename = next_file_helper (local, &file_type);
387 #endif
389 if (filename == NULL)
390 return NULL;
392 my_error = NULL;
393 path = g_build_filename (local->filename, filename, NULL);
394 if (file_type == G_FILE_TYPE_UNKNOWN ||
395 (file_type == G_FILE_TYPE_SYMBOLIC_LINK && !(local->flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
397 info = _g_local_file_info_get (filename, path,
398 local->matcher,
399 local->flags,
400 &local->parent_info,
401 &my_error);
403 else
405 info = _g_local_file_info_get (filename, path,
406 local->reduced_matcher,
407 local->flags,
408 &local->parent_info,
409 &my_error);
410 if (info)
412 _g_local_file_info_get_nostat (info, filename, path, local->matcher);
413 g_file_info_set_file_type (info, file_type);
414 if (file_type == G_FILE_TYPE_SYMBOLIC_LINK)
415 g_file_info_set_is_symlink (info, TRUE);
418 g_free (path);
420 if (info == NULL)
422 /* Failed to get info */
423 /* If the file does not exist there might have been a race where
424 * the file was removed between the readdir and the stat, so we
425 * ignore the file. */
426 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
428 g_error_free (my_error);
429 goto next_file;
431 else
432 g_propagate_error (error, my_error);
435 return info;
438 static gboolean
439 g_local_file_enumerator_close (GFileEnumerator *enumerator,
440 GCancellable *cancellable,
441 GError **error)
443 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
445 if (local->dir)
447 #ifdef USE_GDIR
448 g_dir_close (local->dir);
449 #else
450 closedir (local->dir);
451 #endif
452 local->dir = NULL;
455 return TRUE;