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>
33 * It would be nice to use the dirent->d_type to check file type without
34 * needing to stat each files on linux and other systems that support it.
35 * (question: does that following symlink or not?)
39 struct _GLocalFileEnumerator
41 GFileEnumerator parent;
43 GFileAttributeMatcher *matcher;
47 GFileQueryInfoFlags flags;
49 gboolean got_parent_info;
50 GLocalParentFileInfo parent_info;
52 gboolean follow_symlinks;
55 #define g_local_file_enumerator_get_type _g_local_file_enumerator_get_type
56 G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR);
58 static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
59 GCancellable *cancellable,
61 static gboolean g_local_file_enumerator_close (GFileEnumerator *enumerator,
62 GCancellable *cancellable,
67 g_local_file_enumerator_finalize (GObject *object)
69 GLocalFileEnumerator *local;
71 local = G_LOCAL_FILE_ENUMERATOR (object);
73 g_free (local->filename);
74 g_file_attribute_matcher_unref (local->matcher);
77 g_dir_close (local->dir);
81 if (G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize)
82 (*G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize) (object);
87 g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90 GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
92 gobject_class->finalize = g_local_file_enumerator_finalize;
94 enumerator_class->next_file = g_local_file_enumerator_next_file;
95 enumerator_class->close = g_local_file_enumerator_close;
99 g_local_file_enumerator_init (GLocalFileEnumerator *local)
104 convert_file_to_io_error (GError **error,
109 if (file_error == NULL)
112 new_code = G_IO_ERROR_FAILED;
114 if (file_error->domain == G_FILE_ERROR) {
115 switch (file_error->code) {
116 case G_FILE_ERROR_NOENT:
117 new_code = G_IO_ERROR_NOT_FOUND;
119 case G_FILE_ERROR_ACCES:
120 new_code = G_IO_ERROR_PERMISSION_DENIED;
122 case G_FILE_ERROR_NOTDIR:
123 new_code = G_IO_ERROR_NOT_DIRECTORY;
130 g_set_error (error, G_IO_ERROR,
132 "%s", file_error->message);
136 _g_local_file_enumerator_new (const char *filename,
137 const char *attributes,
138 GFileQueryInfoFlags flags,
139 GCancellable *cancellable,
142 GLocalFileEnumerator *local;
147 dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
149 convert_file_to_io_error (error, dir_error);
150 g_error_free (dir_error);
154 local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);
157 local->filename = g_strdup (filename);
158 local->matcher = g_file_attribute_matcher_new (attributes);
159 local->flags = flags;
161 return G_FILE_ENUMERATOR (local);
165 g_local_file_enumerator_next_file (GFileEnumerator *enumerator,
166 GCancellable *cancellable,
169 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
170 const char *filename;
173 GError *my_error = NULL;
175 if (!local->got_parent_info)
177 _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
178 local->got_parent_info = TRUE;
183 filename = g_dir_read_name (local->dir);
184 if (filename == NULL)
187 path = g_build_filename (local->filename, filename, NULL);
188 info = _g_local_file_info_get (filename, path,
197 /* Failed to get info */
198 /* If the file does not exist there might have been a race where
199 * the file was removed between the readdir and the stat, so we
200 * ignore the file. */
201 if (my_error->domain == G_IO_ERROR &&
202 my_error->code == G_IO_ERROR_NOT_FOUND)
204 g_error_free (my_error);
208 g_propagate_error (error, my_error);
215 g_local_file_enumerator_close (GFileEnumerator *enumerator,
216 GCancellable *cancellable,
219 GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
223 g_dir_close (local->dir);