1 /* GIO - GLib Input, Output and Streaming Library
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>
26 #include <glocalfileenumerator.h>
27 #include <glocalfileinfo.h>
28 #include <glocalfile.h>
35 #define CHUNK_SIZE 1000
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?)
49 #include <sys/types.h>
60 struct _GLocalFileEnumerator
62 GFileEnumerator parent;
64 GFileAttributeMatcher *matcher;
67 GFileQueryInfoFlags flags;
69 gboolean got_parent_info;
70 GLocalParentFileInfo parent_info;
81 gboolean follow_symlinks;
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);
87 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
88 GCancellable *cancellable,
90 static gboolean g_local_file_enumerator_close (GFileEnumerator *enumerator,
91 GCancellable *cancellable,
96 free_entries (GLocalFileEnumerator *local)
101 if (local->entries != NULL)
103 for (i = 0; local->entries[i].name != NULL; i++)
104 g_free (local->entries[i].name);
106 g_free (local->entries);
112 g_local_file_enumerator_finalize (GObject *object)
114 GLocalFileEnumerator *local;
116 local = G_LOCAL_FILE_ENUMERATOR (object);
118 g_free (local->filename);
119 g_file_attribute_matcher_unref (local->matcher);
123 g_dir_close (local->dir);
125 closedir (local->dir);
130 free_entries (local);
132 if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
133 (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
138 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
143 gobject_class->finalize = g_local_file_enumerator_finalize;
145 enumerator_class->next_file = g_local_file_enumerator_next_file;
146 enumerator_class->close_fn = g_local_file_enumerator_close;
150 g_local_file_enumerator_init (GLocalFileEnumerator *local)
156 convert_file_to_io_error (GError **error,
161 if (file_error == NULL)
164 new_code = G_IO_ERROR_FAILED;
166 if (file_error->domain == G_FILE_ERROR)
168 switch (file_error->code)
170 case G_FILE_ERROR_NOENT:
171 new_code = G_IO_ERROR_NOT_FOUND;
173 case G_FILE_ERROR_ACCES:
174 new_code = G_IO_ERROR_PERMISSION_DENIED;
176 case G_FILE_ERROR_NOTDIR:
177 new_code = G_IO_ERROR_NOT_DIRECTORY;
184 g_set_error (error, G_IO_ERROR,
186 "%s", file_error->message);
191 _g_local_file_enumerator_new (GLocalFile *file,
192 const char *attributes,
193 GFileQueryInfoFlags flags,
194 GCancellable *cancellable,
197 GLocalFileEnumerator *local;
200 filename = g_file_get_path (G_FILE (file));
207 dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
212 convert_file_to_io_error (error, dir_error);
213 g_error_free (dir_error);
222 dir = opendir (filename);
227 g_set_error (error, G_IO_ERROR,
228 g_io_error_from_errno (errsv),
229 "%s", g_strerror (errsv));
236 local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
241 local->filename = filename;
242 local->matcher = g_file_attribute_matcher_new (attributes);
243 local->flags = flags;
245 return G_FILE_ENUMERATOR (local);
250 sort_by_inode (const void *_a, const void *_b)
252 const DirEntry *a, *b;
256 return a->inode - b->inode;
260 next_file_helper (GLocalFileEnumerator *local)
262 struct dirent *entry;
263 const char *filename;
269 if (local->entries == NULL ||
270 (local->entries[local->entries_pos].name == NULL))
272 if (local->entries == NULL)
273 local->entries = g_new (DirEntry, CHUNK_SIZE + 1);
276 /* Restart by clearing old names */
277 for (i = 0; local->entries[i].name != NULL; i++)
278 g_free (local->entries[i].name);
281 for (i = 0; i < CHUNK_SIZE; i++)
283 entry = readdir (local->dir);
285 && (0 == strcmp (entry->d_name, ".") ||
286 0 == strcmp (entry->d_name, "..")))
287 entry = readdir (local->dir);
291 local->entries[i].name = g_strdup (entry->d_name);
292 local->entries[i].inode = entry->d_ino;
297 local->entries[i].name = NULL;
298 local->entries_pos = 0;
300 qsort (local->entries, i, sizeof (DirEntry), sort_by_inode);
303 filename = local->entries[local->entries_pos++].name;
304 if (filename == NULL)
305 local->at_end = TRUE;
313 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
314 GCancellable *cancellable,
317 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
318 const char *filename;
323 if (!local->got_parent_info)
325 _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
326 local->got_parent_info = TRUE;
332 filename = g_dir_read_name (local->dir);
334 filename = next_file_helper (local);
337 if (filename == NULL)
341 path = g_build_filename (local->filename, filename, NULL);
342 info = _g_local_file_info_get (filename, path,
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)
358 g_error_free (my_error);
362 g_propagate_error (error, my_error);
369 g_local_file_enumerator_close (GFileEnumerator *enumerator,
370 GCancellable *cancellable,
373 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
378 g_dir_close (local->dir);
380 closedir (local->dir);